在一个函数中转移两个交易

发布于 2025-01-21 19:49:08 字数 1518 浏览 0 评论 0原文

我想在一个功能中支付两项交易。 MSG.Sender应首先向合同支付费用。其余的仍在味精中。价值应转移给卖方。我总是会出错。为了澄清,我自己测试了这两项交易,并且它起作用了,并且我实施了合同可以接收资金的接收()功能。这是代码:

function sendMoney() public payable {

address payable seller = payable(address(this));
address payable sellerino = payable(0x910DCE3971F71Ee82785FF86B47CaB938eBB9E68);

sellerino.transfer(10);
seller.transfer(msg.value);
}

此处错误: https://blockscout.mantis.hexapod.network/tx/0x343817da970dce47c74094f4766f9c7f21ee258ea848e117893afa53c4768dac/internal-交易

其他代码:

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 技术交流群。

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

发布评论

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

评论(1

冷情 2025-01-28 19:49:08

您的摘要试图发送比合同更多的资金 - 这会导致整个交易恢复。

合同

沿0.1 Mantis(== 100000000000000000 WEI)发送的交易,该交易反映在msg.value全局变量中。

第一个transfer()发送10个WEI,因此左现在的余额是9999999999999999990 WEI(比0.1 Mantis少10个)。但是第二个Transf()正在尝试发送100000000000000000(msg.value),这比目前可用的合同多10个WEI。

解决方案:将第二个转移降低到已经发送的金额。

sellerino.transfer(10);
seller.transfer(msg.value - 10);

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 second transfer() is trying to send 100000000000000000 (the msg.value), which is 10 wei more than the contract has available at the moment.

Solution: Lower the second transfer by the already sent amount.

sellerino.transfer(10);
seller.transfer(msg.value - 10);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文