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.

2 Answers 2

2

As @Morten mentioned, what allows your bid function to receive ETH is the payable keyword, and to obtain the amount of ETH received, you use msg.value.

To actually send ETH when calling this function from your web3 app, you need to pass an additional parameter to your method, commonly known as transaction overrides. It's an object that can include a value property. Using ethers it would be something like this:

import { Contract, ethers } from 'ethers';
import SimpleAution from '@/artifacts/solidity/contracts/SimpleAution.sol/SimpleAution.json';

const provider = new ethers.providers.Web3Provider(window.ethereum);

const signer = provider.getSigner();

// address of the smart contract
const contractAddress = '0x12345...abcde';

// as the operation we're going to do is a transaction,
// we pass the signer instead of the provider
const contract = new ethers.Contract(contractAddress, SimpleAution.abi, signer);


try {
  const txOverrides = {
    // amount of ETH to send
    value: ethers.utils.parseEther('0.01'),
  };
  // call smart contract function passing the overrides including the ETH value
  const transaction = await contract.bid(txOverrides);

  console.log('transaction :>> ', transaction);
  // wait for the transaction to actually settle in the blockchain
  await transaction.wait();
} catch (err) {
  console.error(error);
}

You can also include gasLimit, gasPrice or nonce in the overrides, more details here.

I've explained this in more detail in this article in my blog

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
    Commented Aug 20, 2020 at 12:17
  • This happens outside Solidity. In web3, for instance, when you create a transaction, add value: wei_to_send as a property. Commented Jan 12, 2021 at 14:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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