2

When testing locally with Ganache, how can I view the balance of ETH sent to my ERC721 contract as part of payable functions(minting)?

I have simple mint functin

    function userMint(uint256 numberOfTokens) public payable{
    require(numberOfTokens <= MAX_MINTS_PER_TXN, "Exceeded max tokens in a txn");
    require(msg.value == (numberOfTokens * mintPrice), "Incorrect amount of ETH sent");
    //TODO validate tokens are still available (havent sold them all)
    for (uint256 i = 1; i <= numberOfTokens; i++) {
        _safeMint(msg.sender, _tokenIdCounter.current());
        _tokenIdCounter.increment();
        //EMIT EVENTS
    }
}

WHen I call it via truffle console I can see that the eth is paid but I don't see it "received" in the contract anywhere in ganache.

2
  • What's the code your working with? Oct 27 '21 at 1:29
  • I've updated the question to show the mint function.
    – gunygoogoo
    Oct 27 '21 at 1:32
3

Here is an example;

Note that ETH is not a contract so there is no balanceOf() method to be called.

This is how it's done.

uint balance = address(this).balance;

address(this) is your contract address.

This is done inside you contract with solidity so the balance will be available inside your contract.

You can also get it from the cli

Check out these methods.

https://docs.nethereum.com/en/latest/ethereum-and-clients/test-rpc/#implemented-methods

2

You can simply try this

uint256 balance = address(this).balance;

return balance;

1
  • Yes this works. I'm just curious if I was doing something wrong that ganache wasn't showing it.
    – gunygoogoo
    Oct 27 '21 at 1:41

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.