solidity - Ethers.js,向智能合约汇款(接收功能)

标签 solidity ethers.js decentralized-applications

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

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

最佳答案

当您要调用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
});

关于solidity - Ethers.js,向智能合约汇款(接收功能),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69825236/

相关文章:

git - Git 是分布式的还是去中心化的?

constructor - Solidity 解析器错误 : Expected identifier but got '='

ethereum - 为什么这个 Solidity 函数在断言后返回 0?

javascript - 使用 Ethers JS 估算 gas 价格

javascript - 将大数(十六进制值)转换为字符串或数字

ethereum - 如何使用不同的地址来调用安全帽测试和脚本中的函数?

solidity - 如何在NFT智能合约中编写销毁函数?

blockchain - ton-solidity中的TL-B方案是什么以及如何使用?

reactjs - 如何将安全帽导入此存储库

blockchain - 去中心化系统和分布式系统有什么区别?