我有一个带有接收函数的智能合约:
receive() external payable {
Wallets[msg.sender] += msg.value;
}我有一个前端,我想使用receive()函数将Ethers发送到这个智能合约。
async function transfer() {
if(typeof window.ethereum !== 'undefined') {
const accounts = await window.ethereum.request({ method: 'eth_requestAccounts' });
const provider = new ethers.providers.Web3Provider(window.ethereum);
const signer = provider.getSigner();
const contract = new ethers.Contract(WalletAddress, Wallet.abi, signer);
const transaction = await contract.send({
from: accounts[0],
value: amount
})
await transaction.wait();
alert('ok');
setAmount('');
getBalance();
}}
Saldy,这里没有"send“函数,我需要在那里使用什么函数?非常感谢!
发布于 2021-11-03 20:49:53
当您想要调用receive()函数时,您需要向合同地址发送一个data字段为空的事务。与将ETH发送到非合同地址的方式相同。
// not defining `data` field will use the default value - empty data
const transaction = signer.sendTransaction({
from: accounts[0],
to: WalletAddress,
value: amount
});https://stackoverflow.com/questions/69825236
复制相似问题