试图在函数中调用.transfer()会使其失败。我知道.send()和.transfer()之间的区别在于.send不抛出。该函数在使用.send()时工作正常,但在使用.transfer时失败。我把调职电话具体化,以缩小失误的可能性。这是我的代码:
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);
}编辑:做了一个额外的测试,看看它是否有效。以下测试也失败:
function test() public payable {
msg.sender.transfer(500);
}发布于 2019-05-03 08:06:39
既然你的合同是:
msg.sender.transfer(1);
最好的形式是:
require(address(this).balance >=1, "Contract underfunded");
很可能你的sends因为资金不足而失败了,合同忽视了失败案例,只是在继续--这正是transfer所要解决的问题。
在这个阶段,也有可能出了什么问题。我很想对此进行评论,以便进行测试。
removeAuction(_id, _buyer);
希望能帮上忙。
https://ethereum.stackexchange.com/questions/70324
复制相似问题