[英]MetaMask - RPC Error: execution reverted {code: -32000, message: 'execution reverted'}
I'm trying to develop a DApp using React and Solidity using an ERC20 contract, I've deployed my smart contract on Rinkeby and am able to interact with it using name()
and symbol()
, however, I cannot execute payable functions like transfer()
, the following error is displayed each time:我正在尝试使用 ERC20 合约使用 React 和 Solidity 开发 DApp,我已经在 Rinkeby 上部署了我的智能合约,并且能够使用
name()
和symbol()
与之交互,但是,我无法执行支付功能,例如transfer()
,每次都显示如下错误:
MetaMask - RPC Error: execution reverted {code: -32000, message: 'execution reverted'}
I have tried simply calling contract.transfer( ... )
and putting quotes around the amount being sent, neither works.我试过简单地调用
contract.transfer( ... )
并在发送的金额周围加上报价,但都不起作用。
JS: JS:
await contract.transfer.call(account, 50)
Sol:溶胶:
interface IERC20 {
function transfer(address recipient, uint256 amount) external returns (bool);
// Omitted other functions...
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
}
contract MyCoin is IERC20 {
function transfer(address receiver, uint256 numTokens) public override returns (bool) {
require(numTokens <= balances[msg.sender]);
balances[msg.sender] = balances[msg.sender].sub(numTokens);
balances[receiver] = balances[receiver].add(numTokens);
emit Transfer(msg.sender, receiver, numTokens);
return true;
}
}
first of all, you haven't marked the transfer() method with " payable " modifier.首先,您没有使用“ payable ”修饰符标记 transfer() 方法。 So, you're not calling any payable method, as you asked.
因此,正如您所要求的,您没有调用任何应付方法。
Secondly, when you send any amount from a metamask wallet, the contract should have either of these methods, with the logic, in case your contract receives any amount :其次,当您从元掩码钱包发送任何金额时,合约应该具有这些方法中的任何一种,并符合逻辑,以防您的合约收到任何金额:
fallback() external payable;
receive() external payable;
as then only, your contract knows what to do with incoming amount ;只有这样,您的合同才知道如何处理收到的金额;
Interacting with js:与js交互:
also, to send any "amount* and interact with the contract with js. you can use sendTransaction({}) :此外,要发送任何“金额*”并使用 js 与合同进行交互。您可以使用sendTransaction({}) :
await contractInstance.sendTransaction({
from: accountAddress,
value: web3.utils.toWei("0.001", "ether")
});
Don't do await contractInstance.receive({}) and call the receive() method directly.不要await contractInstance.receive({})并直接调用 receive() 方法。
you can see this answer, or this blog for more on this.您可以查看此答案或此博客以获取更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.