[英]Metamask transaction is expected to fail on a Smart Contract function that batches ether transfer
So basically I'm trying to write an application that is able to send multiple addresses specified amount of tokens or ethers, after the admin has "approved" a transaction from the dashboard.所以基本上我正在尝试编写一个应用程序,该应用程序能够在管理员从仪表板“批准”交易后向多个地址发送指定数量的令牌或以太币。
Smart Contract Code -智能合约代码 -
interface ERC20Token {
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function allowance(address owner, address spender) external view returns (uint256);
function transfer(address recipient, uint256 amount) external returns (bool);
function approve(address spender, uint256 amount) external returns (bool);
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
}
contract SimpleContract {
address public owner;
ERC20Token public TetherToken;
ERC20Token public USDCoin;
struct Transaction {
address payable recipient;
uint amount;
string coinType;
}
constructor() {
owner = msg.sender;
TetherToken = ERC20Token(0x97192842006D54AC767D004c96Dd3723194c8AcC);
USDCoin = ERC20Token(0xFE724a829fdF12F7012365dB98730EEe33742ea2);
}
function batchTransaction(Transaction[] memory transactions) public payable {
for(uint i = 0; i < transactions.length; i++) {
if(keccak256(abi.encodePacked(transactions[i].coinType)) == keccak256(abi.encodePacked("ETH"))) {
transactions[i].recipient.transfer(transactions[i].amount);
} else if (keccak256(abi.encodePacked(transactions[i].coinType)) == keccak256(abi.encodePacked("USDC"))) {
USDCoin.transferFrom(msg.sender, transactions[i].recipient, transactions[i].amount);
} else if (keccak256(abi.encodePacked(transactions[i].coinType)) == keccak256(abi.encodePacked("USDT"))) {
TetherToken.transferFrom(msg.sender, transactions[i].recipient, transactions[i].amount);
}
}
}
}
Front-end React Code that interacts with batch transaction -与批量交易交互的前端 React 代码 -
async function batchUpdate() {
let transactionsToSend = [...transactions.filter((transaction) => {
return checkedIndices?.find((tx) => tx.id === transaction._id)?.isChecked == true
}).map((tx) => ({
recipient: tx.address,
amount: utils.parseEther(tx.amount.toString()),
coinType: tx.coinType
}))]
MadRiverContract.methods.batchTransaction([...transactionsToSend]).send({ from: account }).on('receipt', (receipt) => {
updateStatusBatch('approved', checkedIndices.filter((tx) => tx.isChecked == true).map((tx) => tx.id), receipt.transactionHash)
}).catch((error) => {
alert(error.message)
console.log(error)
})
}
For interacting with the smart contract, I'm using useDapp to connect to the wallet, and web3.js only for this batchTransfer function. When I try to execute the batch transfer, this appears.为了与智能合约交互,我使用 useDapp 连接到钱包,而 web3.js 仅用于此 batchTransfer function。当我尝试执行批量转账时,会出现此消息。
What's the issue here?这里有什么问题?
I had the same error here: https://ethereum.stackexchange.com/questions/119491/this-transaction-is-expected-to-fail-trying-to-execute-it-is-expected-to-be-exp我在这里有同样的错误: https://ethereum.stackexchange.com/questions/119491/this-transaction-is-expected-to-fail-trying-to-execute-it-is-expected-to-be-exp
The problem was instead of passing string
type, I was passing a number.问题不是传递
string
类型,而是传递一个数字。 So most likely you are passing wrong argument here:所以很可能你在这里传递了错误的论点:
MadRiverContract.methods.batchTransaction([...transactionsToSend])
[...transactionsToSend]
argument is causing error [...transactionsToSend]
参数导致错误
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.