Day 11. Basic Syntax of Solidity – Units, Special Variables, and Functions –

Solidity includes predefined special variables for specific purposes. In this guide, we will explain some of the representative units and special variables.

目次

Ether Units

As explained on Day 4, Ethereum has three Ether units: Ether, Gwei, and Wei. In Solidity, these can also be used as ether, gwei, and wei.

In the following example, we are checking whether 1 gwei and 1e9 (which equals 1000000000) are the same. If checkGwei is executed, the value of isEqual will become true.

Solidity
contract Units {
  bool public isEqual = false;
  ...
  function checkGwei() public {
    if (1 gwei == 1e9) {
      isEqual = true;
    }
  }
  ...
}

Time Units

For representing time units, you can use seconds, minutes, hours, days, and weeks.

  • 1 == 1 seconds
  • 1 minutes == 60 seconds
  • 1 hours == 60 minutes
  • 1 days == 24 hours
  • 1 weeks == 7 days

Special Variables Related to Blocks and Transactions

block

The block is a special variable that provides information related to the current block. As seen on Day 6, understanding becomes easier when comparing with real examples of blocks on Etherscan.

  • block.number: Represents the number of the current block (the same as ‘Block Height’ on Etherscan).
  • block.timestamp: Represents the timestamp (UNIX time) of the current block.
  • block.difficulty: Represents the difficulty of the current block.

msg

The msg is a special variable that provides information related to the current transaction or message. Please refer to the transaction information section from Day 6 as well.

  • msg.sender: Represents the address that sent the transaction (the same as ‘From’ on Etherscan).
  • msg.value: Represents the amount of Ether sent with the transaction.

Members of Address Types

The address type, as mentioned on Day 8, is a data type that represents an Ethereum address. There are several commonly used member functions and variables associated with this type (members refer to elements contained within a data type).

  • balance: A variable that represents the balance (amount of Ether) of an address type instance.
  • transfer(): A function used to transfer Ether from one address type instance to another address. If the transfer is successful, Ether is sent to the recipient address. If it fails, an exception occurs.
  • send(): A function used to transfer Ether from one address type instance to another address. If the transfer is successful, true is returned. If the transfer fails, false is returned.
  • call(): A function for making external function calls to an address type instance.

The following example defines three functions in a contract named Funding: fund (to invest), withdraw (to withdraw funds), and getOwnersBalance (to get the balance of the contract owner). The fund function records who invested how much, and the withdraw function allows the contract owner to withdraw funds. The withdraw function is equipped with the onlyOwner modifier, meaning only the owner can call it. getOwnersBalance is a function needed to check how much balance the Owner has (necessary to check if the contract is operating correctly).

In the withdraw function, payable(i_owner).transfer(total); is used to send the total amount (total) to the owner’s address (where ‘i_owner’ is the owner’s address). Also, in getOwnersBalance, it returns the balance of the owner (i_owner.balance).

Solidity
// SPDX-License-Identifier: MIT
pragma solidity 0.8.13;

contract Funding {
  address[] private s_funders;
  mapping(address => uint256) private s_addressToAmount;
  address public i_owner = 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4;
  
  modifier onlyOwner() {
    require(msg.sender == i_owner, "Only contract owner can call this function");
    _;
  }
  
  function fund() public payable {
    s_funders.push(msg.sender);
    s_addressToAmount[msg.sender] += msg.value;
  }
  
  function withdraw() public onlyOwner {
    uint256 total = 0;
    for (uint256 i = 0; i < s_funders.length; i++) {
      address funder = s_funders[i];
      total += s_addressToAmount[funder];
      s_addressToAmount[funder] = 0;
    }
    
    payable(i_owner).transfer(total);
  }
  
  function getOwnersBalance() public view returns (uint256) {
      return i_owner.balance;
  }

}

The transfer on line 27 can be written in either of the following ways. send is similar to transfer, but it returns a bool result, allowing for different actions based on success or failure.

call is a low-level method used to call functions of a contract at another address. Normally, the parentheses in call("") would contain an encoded string representing the function to be called, but here the purpose is not to call a function but to send an amount, so this part is empty (‘””‘). When ‘call’ is used, it’s possible to send an amount, which is done here with {value: total}.

Solidity
bool sendSuccess = payable(i_owner).send(total); // Example of using 'send'
(bool callSuccess, bytes memory dataReturned) = payable(i_owner).call{value: total}(""); // Example of using 'call'

For msg, block, and address, we have listed the commonly used elements, but there are more member variables and functions than these. For more details, please refer to the official Solidity guides and documentation..

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

After joining IBM in 2004, the author gained extensive experience in developing and maintaining distributed systems, primarily web-based, as an engineer and PM. Later, he founded his own company, designing and developing mobile applications and backend services. He is currently leading a Tech team at a venture company specializing in robo-advisory.

コメント

コメントする

CAPTCHA


目次