0

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.

1
  • Alternatively, instead of accepting the ETH equivalent of 200$, you could make your contract simply accept 200 USDC (or some other USD stablecoin.)
    – dwardu
    Jun 15 at 14:05

2 Answers 2

1

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
  }

1
0

Consider using $DAI or $USDC for the minting payment instead of price feed from oracle.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.