将ETH从帐户1转移到2

发布于 2025-02-09 02:26:13 字数 575 浏览 0 评论 0原文

我正在尝试将以太从帐户1转到帐户2。这是代码:

function pay(address _from, uint256 _tokenId) external payable noReentrant {
    uint256 balance = msg.sender.balance;
    require(balance > 0.011 ether, "There is not enough eth");        

    payable(__ADDRESS2__).transfer(0.001 ether);
}

receive() external payable {
}

fallback() external payable {
}

因此,MSG.SENDER是帐户1,此帐户希望将ETH转移到帐户2。两个帐户都有Ethers :

错误:恢复事务:函数呼叫无法执行

我正在使用硬汉测试此功能。因此,我有几个疑问:

  1. 这是将令牌从帐户1转移到2的正确方法吗?在这里,我不是在谈论将ETH从帐户1转移到合同的地址。
  2. 如果是这样,那怎么了?

I'm trying to transfer ether from an Account 1 to an Account 2. This is the code:

function pay(address _from, uint256 _tokenId) external payable noReentrant {
    uint256 balance = msg.sender.balance;
    require(balance > 0.011 ether, "There is not enough eth");        

    payable(__ADDRESS2__).transfer(0.001 ether);
}

receive() external payable {
}

fallback() external payable {
}

So here the msg.sender is Account 1 and this account wants to transfer eth to Account 2. Both accounts have ethers but it's throwing this error:

Error: Transaction reverted: function call failed to execute

I'm using hardhat to test this function. So I have a couple of doubts:

  1. Is this the correct way to transfer tokens from Account 1 to 2? And here I'm not talking about transfering eth from Account 1 to address of the contract.
  2. If it is, then, what's wrong with it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

筱武穆 2025-02-16 02:26:13

首先,您提出的代码从合同到 address2 ,因此交易可能会恢复,因为合同没有足够的以太状态。您应该检查呼叫者是否会向您发送足够的以太进行传输,即require(msg.value> = 0.001 Ether)

关于最后两个问题:

  1. 不,此代码说明了以太转移。如果要转移ERC20代币,请 oz库成为一个好的开始。
  2. 上面解释了其恢复的原因。发送以太与发送ERC20(或任何其他)令牌有很大不同,请考虑检查OpenZeppelin库。

First of all, the code you presented trasnfers ether from the contract to ADDRESS2, so the transaction probably reverts because the contract doesn't have enough ether. You should've checked that the caller sends you enough ether to transfer, i.e require(msg.value >= 0.001 ether).

Concerning the last two questions:

  1. No, this code illustrates ether transfers. If you want to transfer ERC20 tokens, the OZ library would be a good start.
  2. The reason it reverts is explained above. Sending ether is very different from sending ERC20 (or any other) tokens, consider checking the OpenZeppelin library.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文