简体   繁体   English

MetaMask - RPC 错误:执行恢复{代码:-32000,消息:'执行恢复'}

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

相关问题 MetaMask - RPC 错误:执行恢复,简单的 NFT 迷你 Dapp - MetaMask - RPC Error: execution reverted, Simple NFT Miniting Dapp Web3.js 返回错误:执行已恢复 - Web3.js returned error: execution reverted Web3 错误:事务已被 EVM 还原: - Web3 Error: Transaction has been reverted by the EVM: Metamask 发送 RPC 错误,尽管它在 Javascript 中被捕获 - Metamask sends RPC Error although it is caught in Javascript Javascript错误会停止代码执行 - Javascript error stops code execution 为什么属性似乎被还原了? - Why is the attribute seemingly being reverted? Metamask 返回 RPC 错误:错误:[ethjs-rpc] rpc 错误,有效载荷 {“id”:5715960965837,“jsonrpc”:“2.0”,“params”: - Metamask returns RPC Error: Error: [ethjs-rpc] rpc error with payload {“id”:5715960965837,“jsonrpc”:“2.0”,“params”: 当我测试我的可靠性代码时,我收到“AssertionError: Expected transaction to be reverted with Username” - I am getting "AssertionError: Expected transaction to be reverted with Username " when I am testing my solidity code MetaMask - RPC 错误:错误:MetaMask Tx 签名:用户拒绝交易签名 - MetaMask - RPC Error: Error: MetaMask Tx Signature: User denied transaction signature 错误:通过 ropsten infura 测试网发送交易时,EVM 已恢复交易 - Error: Transaction has been reverted by the EVM while sending transaction via ropsten infura testnet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM