1

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;
         
    }
caarkii
  • 15
  • 5

1 Answers1

1

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);
Petr Hejda
  • 40,554
  • 8
  • 72
  • 100
  • Last Question, I tried to implement the same logic into my existing function. It goes through and my NFT gets transferred but I cant seem to see the transaction to the "seller". I added the code in above in the buyTicketFromAttendee Function. Thank you very much – caarkii Apr 14 '22 at 16:07
  • @caarkii There are multiple snippets that could have reverted the transaction, and it's not a very small task to debug the whole contract state and find the specific reasons why this transaction reverted. I'd recommend going through all `require()` conditions as well as `transfer()` and other snippets that interact with other contracts in your function, comment them out, and try if it works without this snippet. By the process of elimination, you'll be able to point out all causes of the revert. – Petr Hejda Apr 14 '22 at 16:18