2

我有一个带有接收功能的智能合约:

receive() external payable {
    Wallets[msg.sender] += msg.value;
}

我有一个前端,我想使用 receive() 函数将以太币发送到这个智能合约。

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,没有“发送”功能,我需要在那里使用什么功能?多谢 !

4

1 回答 1

2

当您要调用该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
});
于 2021-11-03T20:49:52.813 回答