将ETH从帐户1转移到2
我正在尝试将以太从帐户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转移到2的正确方法吗?在这里,我不是在谈论将ETH从帐户1转移到合同的地址。
- 如果是这样,那怎么了?
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:
- 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.
- If it is, then, what's wrong with it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您提出的代码从合同到 address2 ,因此交易可能会恢复,因为合同没有足够的以太状态。您应该检查呼叫者是否会向您发送足够的以太进行传输,即
require(msg.value> = 0.001 Ether)
。关于最后两个问题:
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: