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