与智能合约交互时出错 - bid() 函数

问题描述 投票:0回答:1

当我与智能合约交互时,所有功能似乎都可以工作,但 bid() 除外,如果我尝试输入任何值,它会直接将其处理为错误,并给出“抱歉,出价不够”消息。

pragma solidity ^0.8.1;

contract Auction{
address payable public beneficiary;
uint public auctionEndTime;

    //Track auction status
    address public highestbidder;
    uint public highestbid;
    bool ended;
    
    mapping(address => uint) pendingReturns;
    
    event highestBidIncreased(address bidder, uint amount);
    event auctionEnded(address winner, uint amount);
    
    constructor(uint _biddingTime, address payable _beneficiary){
        beneficiary= _beneficiary;
        auctionEndTime=block.timestamp+_biddingTime;
        highestbid=0;
    }
    
    function bid(uint _bid) public payable {
        if(block.timestamp>auctionEndTime) revert('the auction has ended');
        if(msg.value<=highestbid) revert('sorry, bid is not enough');
        if(highestbid!=0){
            pendingReturns[highestbidder]+=highestbid;
        }
        highestbidder=msg.sender;
        highestbid=msg.value;
        emit highestBidIncreased(msg.sender, msg.value);
    
    }
    
    function withdraw() public payable returns(bool){
        uint amount=pendingReturns[msg.sender];
        if (amount>0){
            pendingReturns[msg.sender]=0;
        }
        if(!payable(msg.sender).send(amount)){
            pendingReturns[msg.sender]=amount;
        }
        return true;
    }
    
    function auctionEnd() public{
        if(block.timestamp<auctionEndTime) revert('The auction has not ended yet');
        if(ended) revert('the auction is already over');
    
        ended=true;
        emit auctionEnded(highestbidder, highestbid);
        beneficiary.transfer(highestbid);
    }

}

我认为我可以将 msg.value 作为参数传递,但我无法弄清楚如何在没有编译问题的情况下

debugging solidity
1个回答
0
投票

函数

bid
是应付的,
msg.value
包含交易中发送的wei数量。如果您没有将任何 wei 与交易关联,则条件
msg.value <= highestbid
将评估为 false,然后执行将恢复。

© www.soinside.com 2019 - 2024. All rights reserved.