Transfer two transaction in one function

1.2k views Asked by At

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;
         
    }
1

There are 1 answers

2
Petr Hejda On BEST ANSWER

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);