無知

갈 길이 먼 공부 일기

기술 공부/블록체인

스마트 컨트랙트 (5-3) | Solidity(솔리디티) Units and Globally Available Variables

moozii 2022. 4. 18. 19:10

@Units and Globally Available Variables https://docs.soliditylang.org/en/v0.8.13/units-and-global-variables.html

 

1. Units

솔리디티에서는 시간이나 이더의 단위를 나타내는 unit을 지원한다.

 

(1) Ether

Ether Units: A literal number can take a suffix of wei, gwei or ether to specify a subdenomination of Ether, where Ether numbers without a postfix are assumed to be Wei.
assert(1 wei == 1);
assert(1 gwei == 1e9);
assert(1 ether == 1e18);​

https://docs.soliditylang.org/en/v0.8.13/units-and-global-variables.html 

 

이더의 단위를 표현할 수 있어서 내부적으로 이를 왼쪽의 wei로 바꾸어서 계산하는 데에 쓰인다

 

 

 

(2) Time

Time Units: Suffixes like seconds, minutes, hours, days and weeks after literal numbers can be used to specify units of time where seconds are the base unit and units are considered naively in the following way:

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

Take care if you perform calendar calculations using these units, because not every year equals 365 days and not even every day has 24 hours because of leap seconds. Due to the fact that leap seconds cannot be predicted, an exact calendar library has to be updated by an external oracle.

These suffixes cannot be applied to variables. For example, if you want to interpret a function parameter in days, you can in the following way:
function f(uint start, uint daysAfter) public {
    if (block.timestamp >= start + daysAfter * 1 days) {
      // ...
    }
}​

https://docs.soliditylang.org/en/v0.8.13/units-and-global-variables.html#time-units 

 

시간 값이 내부적으로는 기본 단위인 초로 계산되어 저장된다.
now는 특정 시기부터 지금까지의 시간을 초 단위로 표현한 것이다.

 

 

 

 

 

2. Globally Available Variables

(1) Block and Transaction Properties

Special Variables and Functions:
There are special variables and functions which always exist in the global namespace and are mainly used to provide information about the blockchain or are general-use utility functions.

Block and Transaction Properties

blockhash(uint blockNumber) returns (bytes32): hash of the given block when blocknumber is one of the 256 most recent blocks; otherwise returns zero

block.basefee (uint): current block’s base fee (EIP-3198 and EIP-1559)
block.chainid (uint): current chain id
block.coinbase (address payable): current block miner’s address
block.difficulty (uint): current block difficulty
block.gaslimit (uint): current block gaslimit
block.number (uint): current block number
block.timestamp (uint): current block timestamp as seconds since unix epoch

gasleft() returns (uint256): remaining gas

msg.data (bytes calldata): complete calldata
msg.sender (address): sender of the message (current call)
msg.sig (bytes4): first four bytes of the calldata (i.e. function identifier)
msg.value (uint): number of wei sent with the message
tx.gasprice (uint): gas price of the transaction
tx.origin (address): sender of the transaction (full call chain)

https://docs.soliditylang.org/en/v0.8.13/units-and-global-variables.html#special-variables-and-functions 

 

Contract가 이더리움 가상 머신에서 실행될 때 사용할 수 있는 global variable들은 현재 이더리움 블록체인 정보를 제공한다. contract는 externally owned account에서 호출하거나 다른 contract가 message call을 해서 실행되는데,

 

이때 contract에게 전달된 메시지 정보를 msg object를 통해서 읽을 수 있다.

- msg.sender는 이 contract를 호출한 account의 주소다. (externally owned account이거나, 다른 contract의 어드레스)
- msg.value는 이 메시지를 통해서 전달된 ether(wei unit)이다.
- msg.data는 이 contract로 넘겨진 data다.
- msg.sig는 data의 첫 4바이트다. 어떤 메소드가 불리는지를 알 수 있다.


Tx object는 transaction과 관련된 정보를 제공한다.
- tx.gasprice는 현재 transaction의 gas price를 알려준다.

- tx.origin은 transaction의 시작 externally owned account의 address를 알려준다.


block object는 block과 관련된 정보를 제공한다.

- block.coinbase는 현재 블록을 채굴한 account의 주소다
- block.difficulty는 현재 블록 작업 증명의 어려운 정도다
- block.gaslimit은 현재 블록이 사용 가능한 최대 gas 값이다
- block.number는 현재 블록 넘버를 의미한다
- block.timestamp는 채굴 시 기록된 시간 정보다.

 

 

 

(2) Members of Address Types

<address>.balance (uint256): balance of the Address in Wei
<address>.code (bytes memory): code at the Address (can be empty)
<address>.codehash (bytes32): the codehash of the Address
<address payable>.transfer(uint256 amount): send given amount of Wei to Address, reverts on failure, forwards 2300 gas stipend, not adjustable
<address payable>.send(uint256 amount): returns (bool),
send given amount of Wei to Address, returns false on failure, forwards 2300 gas stipend, not adjustable
<address>.call(bytes memory): returns (bool, bytes memory),
issue low-level CALL with the given payload, returns success condition and return data, forwards all available gas, adjustable
<address>.delegatecall(bytes memory): returns (bool, bytes memory),
issue low-level DELEGATECALL with the given payload, returns success condition and return data, forwards all available gas, adjustable
<address>.staticcall(bytes memory): returns (bool, bytes memory),
issue low-level STATICCALL with the given payload, returns success condition and return data, forwards all available gas, adjustable

https://docs.soliditylang.org/en/v0.8.13/units-and-global-variables.html#members-of-address-types 


The address type comes in two flavours, which are largely identical:
- address: Holds a 20 byte value (size of an Ethereum address).
- address payable: Same as address, but with the additional members transfer and send.

The idea behind this distinction is that address payable is an address you can send Ether to, while you are not supposed to send Ether to a plain address, for example because it might be a smart contract that was not built to accept Ether.

https://docs.soliditylang.org/en/v0.8.13/types.html#address 

 

Address object는 address와 관련된 정보를 제공한다.
- address.balance는 어드레스의 잔고다.

- address.transfer, address.send는 이더를 해당 address로 송금한다.

- address.call, address.callcode, address.delegatecall은 payload를 갖고 다른 contract를 부르는 저 수준 메소드다.

 

 

 

 

[출처] https://docs.soliditylang.org/en/v0.8.13/units-and-global-variables.html

 

Units and Globally Available Variables — Solidity 0.8.13 documentation

» Units and Globally Available Variables Edit on GitHub Units and Globally Available Variables Ether Units A literal number can take a suffix of wei, gwei or ether to specify a subdenomination of Ether, where Ether numbers without a postfix are assumed to

docs.soliditylang.org