首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >.transfer失败

.transfer失败
EN

Ethereum用户
提问于 2019-05-02 11:29:24
回答 1查看 353关注 0票数 0

试图在函数中调用.transfer()会使其失败。我知道.send()和.transfer()之间的区别在于.send不抛出。该函数在使用.send()时工作正常,但在使用.transfer时失败。我把调职电话具体化,以缩小失误的可能性。这是我的代码:

代码语言:javascript
复制
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);
}

编辑:做了一个额外的测试,看看它是否有效。以下测试也失败:

代码语言:javascript
复制
    function test() public payable {
       msg.sender.transfer(500);
    }
EN

回答 1

Ethereum用户

发布于 2019-05-03 08:06:39

既然你的合同是:

msg.sender.transfer(1);

最好的形式是:

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

很可能你的sends因为资金不足而失败了,合同忽视了失败案例,只是在继续--这正是transfer所要解决的问题。

在这个阶段,也有可能出了什么问题。我很想对此进行评论,以便进行测试。

removeAuction(_id, _buyer);

希望能帮上忙。

票数 1
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/70324

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档