简体   繁体   中英

Error: Returned error: VM Exception while processing transaction: revert Not Enough Balance — Reason given: Not Enough Balance

Here is the transfer function of my erc20 token which called "CBC"

function transfer(address receiver, uint numTokens) public payable returns (bool)  {
        require(numTokens <= balances[msg.sender],"Not Enough Balance");
        balances[msg.sender] = balances[msg.sender].sub(numTokens);
        balances[receiver] = balances[receiver].add(numTokens);
        emit Transfer(msg.sender, receiver, numTokens);
        return true;
    }

And I called the transfer function in another ERC721 contract, here is my code

function mint(string calldata _uri,uint value) external onlyOwner {
    token.transfer(receiverAddress,value);
    super._mint(msg.sender, tokenId);
    super._setTokenUri(tokenId, _uri);
    urlOf[tokenId] = _uri;
    tokenId = tokenId + 1;
    emit MintToken(msg.sender, tokenId, _uri,value);
}

In My tests, it always failed with the exception of not enough user balance but i can confirm that the deployer has enough balance and here is the error i am getting

Can someone tell me how to fix it as I am new to blockchain, that is appreciated

It is because when you call transfer in mint, msg.sender (in transfer) is the address of the calling contract (the one that implements mint) not the address of the person calling mint. Therefore, token needs to have enough balance to perform the operation.

The only time msg.sender stays the same is when the call is to another function in the contract. Call to another contract changes the msg.sender to the address of the calling contract. Without this, anyone can use msg.sender to authorize other transactions in other contracts.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM