I'm creating an Erc721a contract and I want to set the price of my tokens to 200$, and for that I used chanlink's aggregator v3 interface which returns the dollar value, but how should I use this ? If there's any sample code or any smart contract with same functionality, please share !! Thanks.
2 Answers
You can write it for example like this
function getLatestPrice() public view returns (int) {
(
/*uint80 roundID*/,
int price,
/*uint startedAt*/,
/*uint timeStamp*/,
/*uint80 answeredInRound*/
) = priceFeed.latestRoundData();
return price;
}
function mint() public payable returns(uint256 tokenId) {
int oneEthInUsd = getLatestPrice(); // 1 ETH in terms of USD
// If 1 ETH = 3274,72773030 USD
// 1 ETH : 3274,72773030 USD = X ETH : 200 USD
// X = 1 * 200 / 3274,72773030
int twoHundredUsdInEth = 200 ether / oneEthInUsd;
require(msg.value >= uint(twoHundredUsdInEth), "The Price is 200 USD");
// the rest of your minting function logic
}
Consider using $DAI or $USDC for the minting payment instead of price feed from oracle.