[英]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.