0

Trying to call .transfer() within a function makes it fail. I am aware that the difference between .send() and .transfer() is that .send dont throw. The function works properly when using .send(), but fails when using .transfer. I simiplified the transfer-call to narrow down the fault-possibilities. Here is my code:

contract AuctionHouse is ItemOwnership {
constructor () public payable {}
function () external payable {}


function purchaseAuction(uint _id, address _buyer) external payable {

    Auction memory auction = auctions[auctionIndexes[_id]];
    require(
        msg.sender == _buyer ||
        approvedForAll[_buyer][msg.sender], 
        "You must have authority over purchasing account");
    require(isOnAuction(_id), "That item is not on auction");
    require(auction.startTime + auction.expiration > now, "auction is expired");
    require(_buyer != ownerOfItem[_id], "Can't purchase your own item");

    //remove item from auctionhouse and transfer ownership
    removeAuction(_id, _buyer);

    //This solution of transferring an auction prevents re-entrancy attacks by 
    //transferring the item and taking it off the auction house before transferring the currency.
    //Converts from Wei to Finney
    msg.sender.transfer(1); // <------------------------------

    //send out event
    emit AuctionPurchased(_id, auction.price, auction.seller, _buyer);
}

edit: Made an extra test just to see if it worked. The following test also fails:

    function test() public payable {
       msg.sender.transfer(500);
    }
  • 1
    I would assume that the contract has an insufficient ether balance. There are other possible reasons for a transfer to fail, but they're all much less likely in a simple test like this. – user19510 May 2 at 13:25
1

Since your contract does:

msg.sender.transfer(1);

It would be good form to first:

require(address(this).balance >=1, "Contract underfunded");

It's possible your sends were failing due to insufficient funds and the contract was ignoring the fail case and just carrying on - precisely what transfer is meant to address.

It's also possible something is going wrong at this stage. I would be eager to comment it out for testing.

removeAuction(_id, _buyer);

Hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.