在一个函数中转移两个交易
我想在一个功能中支付两项交易。 MSG.Sender应首先向合同支付费用。其余的仍在味精中。价值应转移给卖方。我总是会出错。为了澄清,我自己测试了这两项交易,并且它起作用了,并且我实施了合同可以接收资金的接收()功能。这是代码:
function sendMoney() public payable {
address payable seller = payable(address(this));
address payable sellerino = payable(0x910DCE3971F71Ee82785FF86B47CaB938eBB9E68);
sellerino.transfer(10);
seller.transfer(msg.value);
}
其他代码:
function buyTicketFromAttendee(uint256 _ticketId)
external
payable
{
require(eventticket[_ticketId - 1].availableForResell = true,"ticket not for sale");
uint256 _priceToPay = eventticket[_ticketId - 1].ticketPrice;
//address owner = ownerOf(_ticketId);
require((msg.value >= _priceToPay + transferFee),"not enough money");
address seller = eventticket[_ticketId - 1].seller;
address owner = eventticket[_ticketId - 1].owner;
payable(owner).transfer(transferFee);
payable(seller).transfer(msg.value - transferFee);
_transfer(address(this), msg.sender, _ticketId);
//payable(seller).transfer(_priceToPay);
eventticket[_ticketId - 1].availableForResell = false;
}
I want to pay two transactions within one function.
The msg.sender should pay a fee to the contract first. The rest which is still in msg.value should be transferred to the seller. I always get an error. To clarify i tested both transactions by their own and it worked and i have implemented a receive() function that the contract can receive the funds. Here is the code:
function sendMoney() public payable {
address payable seller = payable(address(this));
address payable sellerino = payable(0x910DCE3971F71Ee82785FF86B47CaB938eBB9E68);
sellerino.transfer(10);
seller.transfer(msg.value);
}
Here the error:
https://blockscout.mantis.hexapod.network/tx/0x343817da970dce47c74094f4766f9c7f21ee258ea848e117893afa53c4768dac/internal-transactions
Additional Code:
function buyTicketFromAttendee(uint256 _ticketId)
external
payable
{
require(eventticket[_ticketId - 1].availableForResell = true,"ticket not for sale");
uint256 _priceToPay = eventticket[_ticketId - 1].ticketPrice;
//address owner = ownerOf(_ticketId);
require((msg.value >= _priceToPay + transferFee),"not enough money");
address seller = eventticket[_ticketId - 1].seller;
address owner = eventticket[_ticketId - 1].owner;
payable(owner).transfer(transferFee);
payable(seller).transfer(msg.value - transferFee);
_transfer(address(this), msg.sender, _ticketId);
//payable(seller).transfer(_priceToPay);
eventticket[_ticketId - 1].availableForResell = false;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的摘要试图发送比合同更多的资金 - 这会导致整个交易恢复。
合同。
沿0.1 Mantis(== 100000000000000000 WEI)发送的交易,该交易反映在
msg.value
全局变量中。第一个
transfer()
发送10个WEI,因此左现在的余额是9999999999999999990 WEI(比0.1 Mantis少10个)。但是第二个Transf()
正在尝试发送100000000000000000(msg.value
),这比目前可用的合同多10个WEI。解决方案:将第二个转移降低到已经发送的金额。
Your snippet is trying to send more funds than the contract has - which causes the whole transaction to revert.
The contract has 0 MANTIS before the transaction invoking
sendMoney()
.The transaction sent along 0.1 MANTIS ( == 100000000000000000 wei), which is reflected in the
msg.value
global variable.The first
transfer()
sends 10 wei, so the left available balance is now 99999999999999990 wei (10 less than 0.1 MANTIS). But the secondtransfer()
is trying to send 100000000000000000 (themsg.value
), which is 10 wei more than the contract has available at the moment.Solution: Lower the second transfer by the already sent amount.