我设计了一个小测验应用程序,每次我点击退出按钮调用智能合同上的退出函数时,只要从智能contract.But中提取用户的收入,我总是会得到上面的错误(事务错误:错误:不支付方法不能覆盖值),这是前端的退出代码。
const {ethereum} = window;
if (ethereum) {
console.log("withdrawing")
const provider = new ethers.providers.Web3Provider(ethereum, "any");
const signer = provider.getSigner();
const contract = new ethers.Contract(contractAddress, abi, signer);
try {
const tx = await contract.withdraw(userAddress, { value: earnedInWei });
await tx.wait();
console.log("Transaction Successful!");
} catch (error) {
console.error("Transaction Error: ", error);
}
} else {
console.error("No web3 provider found. Please install MetaMask or use another web3 provider");
}
}```
and below is my smart contract
```//SPDX-License-Identifier: UNLICENSEDpragma稳健性>=0.8.7;
合同ethQuiz {
// smart合同所有人的地址
应付款公有业主地址;
//确保只有所有者调用智能契约
构造函数(){
owner= payable(msg.sender); } //为smart合同供资
receive() external payable {}//供用户从smart合同中提取资金
函数提取(uint256数量)外部{
require(amount <= address(this).balance, "too much!");payable(msg.sender).transfer(amount);}//核对smart合同中的可用余额
函数getBalance()外部视图返回(uint){
return address(this).balance;}}
// smart合同的初始金额:300000000000000000 W`
发布于 2023-01-16 08:13:13
const tx = await contract.withdraw(userAddress, { value: earnedInWei });
withdraw()不是一种可支付的方法,您正在尝试向它发送以太。我相信你想做的只是const tx = await contract.withdraw(earnedInWei);
发布于 2023-01-16 08:28:23
取款不是一种支付方式,所以您不能将以太发送到此方法。@Foxxxey是正确的
做这个
const tx = await contract.withdraw(earnedInWei);
await tx.wait().then(data => {
console.log('data', data)
})https://ethereum.stackexchange.com/questions/143029
复制相似问题