0

I just started to learn Solidity. In the SimpleAution contract, there are two functions, bid() and withdraw(), the former iniating a bid, transfering some ethers to the contract as deposit, the later refund the biders. Their codes as follow,

function bid(uint bidAmount) public payable {
    require(now <= auctionEndTime);
    require(bidAmount > highestBid);
    ///
    require(msg.value == bidAmount);  

    ///this represents current contract address
    //address(this).transfer(bidAmount);
    ///address(0) is empty address
    if (highestBidder != address(0) ){
        pendingReturns[highestBidder] += highestBid;
    } 
    
    highestBidder = msg.sender;
    ///highestBid = msg.value;
    highestBid = bidAmount;
    
    emit HighestBidIncreased(highestBidder, highestBid);
}
function withdraw() public returns (bool) {
    uint amount = pendingReturns[msg.sender];
    if (amount > 0){
        ///clean the returning fund first
        pendingReturns[msg.sender] = 0;
        
        ///then transfer fund back to msg.sender
        if (!msg.sender.send(amount)){
            /// if send back fund fail, give amount back to pending fund 
            pendingReturns[msg.sender] = amount;
            return false;
        }
    } 
    return true;
}

withdraw() is easy to understand, refund a bider through "msg.sender.send(amount)". However bid() doesn't have any code that indicates msg.sender transfering ethers to the contract as deposit. I tried to use "address(this).transfer(bidAmount);" to do the job, but not so sure it is right. Need help,many thanks.

0

bid is a payable function. When you send Ether to the bid function, this Ether will be added to the balance of the smart contract, and the function code is called. The amount of Ether sent is available in the function as msg.value.

2
  • Thanks, Morten. But which codes say I send Ether to the smart contract and where I can set the quantity of Ether to send? Jeff – jlp20030 Aug 20 '20 at 12:17
  • This happens outside Solidity. In web3, for instance, when you create a transaction, add value: wei_to_send as a property. – lmat - Reinstate Monica Jan 12 at 14:31

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.