0

I have the following function to mint a nft on my smart contract (I am using the open zeppelin ERC721 contract) but I would like to stimate the gas feet from my web3 dapp. I know the formula is transaction cost * current gas price and I know I can find the current gas price using something like etherscan api or the ethgasstation.info api, and I know the minimum transaction cost is 21000 but I don't know how much is the costof my mint function in other to calculate a realistic gas fee for the minting, any help to calculate or to find my transaction cost?

here is my mint function:

function mint(uint256[] memory tokens) public payable {
        require(saleIsActive, "Sale is not active");
        if (
            tokens.length == 0 || tokens > maxPurchase
        ) {
            revert("number of tokens incorrect");
        }
        if (price * tokens.length >= msg.value) {
            revert("Ether value is not correct");
        }
        if (_totalTokens.current() + tokens.length > MAX_TOKENS) {
            revert("Purchase would exceed max supply");
        }

        for (uint256 i = 0; i < tokens.length; i++) {
            _safeMint(msg.sender, gradisTokens[i]);
            _totalTokens.increment();
        }
    }
4
  • 1
    first, get rid please of those ifs, you can change them all to require(condition, revetString). What you can do is run the function on ganache or test net and see how much gas your mint function consumes for that specific input. There is a method to estimate gas price but no idea if it works correctly web3js.readthedocs.io/en/v1.5.2/…
    – Majd TL
    Oct 22, 2021 at 14:21
  • 1
    and check this ethereum.stackexchange.com/questions/100209/…
    – Majd TL
    Oct 22, 2021 at 14:22
  • @MajdTL thanks for your help. If I change the "ifs" for a require I always get the error even though the condition is the same and I don't know why
    – Jean
    Oct 22, 2021 at 20:12
  • you do need to invert the conditions, don’t you? Like require(tokens.length != 0 && tokens < maxPurchase, „revert message“) :/ for the first If
    – Majd TL
    Oct 22, 2021 at 21:48

0

Your Answer

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

Browse other questions tagged or ask your own question.