Hello All! This is a bit all over the place, I think I have stared at it too long.
Below I am using provable API and a calculate function to try and mint an ERC20 token, like for like to a senders ETH value. So $10 in Eth recieved, mints 10 ERC20 tokens.
I need the function to be 'all in one', so when called, the API gets the price, returns it, the value passes through the calculator and then the _minter sends the equivalent value in tokens.
Please see my mess below, i really need help on this!!
pragma solidity ^0.8.0
function calculateTokenAmount(
uint256 _weiAmount
)
internal
view
returns (uint256)
{
return _weiAmount / pricePerTokenInWei;
}
function converterPrice( /// merged several functions.
bytes32 _queryID,
string memory _result
)
public payable
{
require(msg.sender == _owner);
require(msg.sender == provable_cbAddress());
provable_query("URL", "json(https://api.kraken.com/0/public/Ticker?pair=ETHUSD).result.XETHZUSD.c.0");
require(msg.sender == provable_cbAddress());
ethPriceInCents = parseInt(_result, 2);
pricePerTokenInWei = calculatePricePerTokenInWei(ethPriceInCents);
emit LogEthPriceInCents(ethPriceInCents);
}
function mint(address to, uint256 amount) public onlyOwner {
_mint(to, amount);
}
function calculatePricePerTokenInWei(
uint256 _ethPriceInCents
)
public
pure
returns(uint256 _pricePerTokenInWei)
{
uint256 numerator = 1 * 10 ** 20;
return numerator / _ethPriceInCents;
}