简体   繁体   English

错误:返回错误:处理事务时出现 VM 异常:恢复余额不足 — 给出的原因:余额不足

[英]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"这是我的 erc20 代币的转移函数,称为“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我在另一个 ERC721 合约中调用了传递函数,这是我的代码

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.这是因为当你在 mint 中调用 transfer 时,msg.sender(在 transfer 中)是调用合约(实现 mint 的那个)的地址,而不是调用 mint 的人的地址。 Therefore, token needs to have enough balance to perform the operation.因此,token 需要有足够的余额来执行操作。

The only time msg.sender stays the same is when the call is to another function in the contract. msg.sender 保持不变的唯一时间是调用合约中的另一个函数时。 Call to another contract changes the msg.sender to the address of the calling contract.调用另一个合约会将 msg.sender 更改为调用合约的地址。 Without this, anyone can use msg.sender to authorize other transactions in other contracts.没有这个,任何人都可以使用 msg.sender 来授权其他合约中的其他交易。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 松露错误:错误:处理事务时VM异常:恢复 - Truffle error: Error: VM Exception while processing transaction: revert 无法弄清楚为什么在处理事务时出现 VM 异常:恢复错误 - Cannot figure out why I am getting VM Exception while processing transaction: revert error Openzepplin众包合同获得:处理事务时VM异常:恢复错误 - Openzepplin crowdsale contract got: VM Exception while processing transaction: revert error 错误 Truffle Migrate “处理事务时出现 VM 异常:操作码无效” - Error Truffle Migrate "VM Exception while processing transaction: invalid opcode" “VM Exception while processing transaction: revert”,当运行chainlink节点并尝试部署TestnetConsumer合约时? - "VM Exception while processing transaction: revert", when running a chainlink node and try to deploy TestnetConsumer contract? solidity 交易错误,如果您发送 value 应该支付调用的 function 并且您发送的 value 应该小于您当前的余额 - solidity transaction error, The called function should be payable if you send value and the value you send should be less than your current balance 错误承诺被拒绝:使用Node.js运行余额转移(超级账本)时 - Error Promise is rejected: when running balance-transfer (hyperledger) with nodejs 以太坊交易:为什么交易后余额不会改变? - Ethereum Transaction: why balance does not change after transaction? &#39;code&#39;: -32603, &#39;message&#39;: &#39;Error: Transaction reverted without a reason string&#39; 当我尝试使用 swapExactTokensForTokens UNISWAP - 'code': -32603, 'message': 'Error: Transaction reverted without a reason string' while i'm trying to use swapExactTokensForTokens UNISWAP Solidity:在依赖于参数的查找后,由于未找到成员“余额”或不可见而出错 - Solidity : Getting error as Member “balance” not found or not visible after argument-dependent lookup
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM