简体   繁体   English

进行交易时的 Solidity 错误 - 无法估算气体; 交易可能会失败或可能需要手动限制气体

[英]Solidity Error when doing a transaction - cannot estimate gas; transaction may fail or may require manual gas limit

I get this error when doing a transaction through Metamask using this Solidity contract:使用此 Solidity 合约通过 Metamask 进行交易时出现此错误:

The error:错误:

Error: cannot estimate gas; transaction may fail or may require manual gas limit 
[ See: https://links.ethers.org/v5-errors-UNPREDICTABLE_GAS_LIMIT ] 
(reason="execution reverted", method="estimateGas", transaction=
{"from":"0x80CB17D85034EDb2Ea1D4BC7d9d512c5dD0d6000",
"to":"0x9d3cA4786e3584b198400F82CA883A581Bd3c4C0","value":{"type":"BigNumber","hex":"0x057289b00c97"},
"data":"0xde905f830000000000000000000000000000000000000000000000000000000000000060000000000000000000000000c99da3a663604c43f6296f3f95e9ea7b6481f01f000000000000000000000000bb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c00000000000000000000000000000000000000000000000000000000000000143956764a6a6c33674a726f6b6762767a456d4e64000000000000000000000000","accessList":null}, error={"code":-32603,"message":"Internal JSON-RPC error.","data":{"code":-32000,"message":"execution reverted"},"stack":"{\n  \"code\": -32603,\n  \"message\": \"Internal JSON-RPC error.\",\n  \"data\": {\n    \"code\": -32000,\n    \"message\": \"execution reverted\"\n  }

Solidity Code:坚固代码:

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.9;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/utils/math/SafeMath.sol";

// Import this file to use console.log
import "hardhat/console.sol";



contract CryptoPayments {
    using SafeMath for uint256;

    uint256 public ownerPercentage;
    address payable public owner;

    event NewPayment(string txId, address from, address to, address token, uint256 when);

    constructor() payable {
        owner = payable(msg.sender);
        ownerPercentage = 5;
    }

    function createPayment(string memory txId, address payable seller, address token) public payable {    
        uint256 ownerPortion = msg.value.mul(ownerPercentage).div(100);
        uint256 sellerPortion = msg.value - ownerPortion;

        IERC20 _token = IERC20(token);
        
        _token.transferFrom(msg.sender, address(this), msg.value);

        _token.transfer(owner, ownerPortion);
        _token.transfer(seller, sellerPortion);

        emit NewPayment(txId, msg.sender, seller, token, block.timestamp);
    }

    function setOwnerPercentage(uint256 percentage) public {
        require(msg.sender == owner, "You aren't the owner");

        ownerPercentage = percentage;
    }

    function setOwner(address newOwner) public {
        require(msg.sender == owner, "You aren't the owner");
        owner = payable(newOwner);
    }
}

JS code responsible for the execution:负责执行的JS代码:

const [ token, setToken ] = useState({
    bnb: {
        name: 'WBNB',
        selected: false,
        address: "0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c"
    },
});

const contractAddr = "<CONTRACT_ADDR_HERE>"
const trxData = {seller_wallet_address: "<WALLET_ADDRESS_HERE>"}

async function initiatePayment(){
   const provider = new ethers.providers.Web3Provider(window.ethereum)
   const signer = provider.getSigner()

   let tokenAbi = ["function approve(address _spender, uint256 _value) public returns 
   (bool success)"];
   
   let tokenContract = new ethers.Contract(token['bnb'].address, tokenAbi, signer);
   await tokenContract.approve(contractAddr, value);

   const contract = new ethers.Contract(contractAddr, ctrct.abi, signer)
   const val = await contract.createPayment(id, trxData.seller_wallet_address, 
   token['bnb'].address, {value});
}


This is your contract createPayment这是你的合约createPayment

function createPayment(string memory txId, 
                       address payable seller, 
                       address token) public payable

on client side, you are not passing correct arguments:在客户端,您没有传递正确的 arguments:

const val = await contract.createPayment(id, 
                                         trxData.seller_wallet_address, 
                                         token['bnb'].address, 
                                         {value});

what is id argument.什么是id参数。 Make sure you are passing correct value types.确保您传递正确的值类型。 If error still persists, make sure your metamask is connected to the correct network如果错误仍然存在,请确保您的元掩码已连接到正确的网络

暂无
暂无

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

相关问题 错误:无法估计gas; 交易可能会失败或可能需要手动 gas limit - Error: cannot estimate gas; transaction may fail or may require manual gas limit 在 Kadena Chainweaver 上部署合约时,交易未成功在区块中挖掘的错误:气体错误气体限制(600)超出 - Error with transaction not successfully mined in block while deploying a contract on Kadena Chainweaver: Gas Error Gas limit(600) exceeded 计算交易汽油费 - Calculate Transaction Gas Fee Solidity Fallback Function Gas 限制 - Solidity Fallback Function Gas Limit web3j-从已签署的交易中获取交易明细(金额,天然气价格,天然气限额) - web3j - Get transaction details (amount, gas price, gas limit) from signed transaction 为什么我收到这个错误? “气体估计错误,显示以下消息(见下文)。 交易 &gt; 执行可能会失败” - Why am I getting this error ? “Gas estimation errored with the following message (see below). The transaction > execution will likely fail” 同一笔交易的不同gas量 - Different gas amount for the same transaction &#39;transaction: out of gas&#39; 松露测试时,设置映射键值错误 - 'transaction: out of gas' when truffle test, set mapping key value error 为什么我收到以下 Chainlink 错误:无法估计气体 - Why am I receiving the following Chainlink error: cannot estimate gas 如何在发送交易前获得预估的gas? - How get estimated gas before sending the transaction?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM