Journey to Solidity (7) Units and Global Variables

​01

Ether unit (Ether)

Regardless of whether it is a virtual currency or a real "stable currency", they all have their own currency units. Of course, as the virtual currency traded on the Ethereum blockchain, Ethereum also has its own set of currency units.

At present, Ether is mainly divided into three types: wei, gwei and ether. Maybe you have seen finney and szabo before, but these two were deleted as early as solidity 0.7.0, but gwei was newly added in solidity 0.6.11!

It is very easy to use, just follow the numbers followed by the Ethereum units. Of course, the numbers and the Ethereum units need to be separated by spaces. You should be aware of this!

If no ether unit is added, it defaults to wei.

assert(1 wei == 1);assert(1 gwei == 1e9);assert(1 ether == 1e18);

Regarding the usage of assert, we will talk about it later. It can be understood as an assertion, which is used to determine whether the expression is true. Otherwise, an error will be thrown!

02

time unit

Speaking of time units, you will definitely think of hours, minutes, seconds, days, and weeks in life.

Solidity time units include: seconds, minutes, hours, days, and weeks. Seconds is the default unit of time.

Early versions (before but not including solidity 0.5.0) also had a year to indicate the time of year, which was abolished due to leap years.

  • 1 == 1 seconds

  • 1 minutes == 60 seconds

  • 1 hours == 60 minutes

  • 1 days == 24 hours

  • 1 weeks == 7 days

uint today = 今天的时间戳;uint tomorrow = today + 1 days;

03

Special variables (global variables)

They are some variables and functions preset by Solidity in the global namespace, used to obtain blockchain information and some common tool functions.

04

Block and transaction properties

blockhash(uint blockNumber) returns (bytes32): The block hash of the specified block - only available for the latest 256 blocks and excluding the current block, otherwise 0 is returned.

  • block.basefee(uint): The base fee of the current block

  • block.chainid(uint): current chain id

  • block.coinbase(address): The address of the miner who mined the current block

  • block.difficulty(uint): current block difficulty

  • block.gaslimit(uint): current block gas limit

  • block.number(uint): current block number

  • block.timestamp(uint): Timestamp in seconds of the current block since unix epoch

  • gasleft() returns(uint256): remaining gas

  • msg.data(bytes): complete calldata

  • msg.sender(address): message sender (current call)

  • msg.sig(bytes4): the first 4 bytes of calldata (that is, the function identifier)

  • msg.value(uint): The number of wei sent with the message

  • tx.gasprice(uint): gas price of transaction

  • tx.origin(address): Transaction initiator (complete call chain)

Don't rely on block.timestamp and blockhash to generate random numbers unless you know exactly what you are doing.

Both timestamps and block hashes can be affected by mining miners to some extent. For example, a malicious miner in the mining community could run the casino contract's payout function with a given hash, and if they didn't receive the money, they could try again with a different hash.

The timestamp of the current block must be strictly greater than the timestamp of the last block, but this ensures that it is also required to be two consecutive blocks on the authoritative chain.

In Solidity 0.7.0, now (an alias for block.timestamp) has been removed.

Copyright statement: This article is an original article by CSDN blogger "Zhen Qicai" and follows the CC 4.0 BY-SA copyright agreement. Please attach the original source link and this statement when reprinting.

Original link:

https://blog.csdn.net/coco2d_x2014/article/details/128311510

If you want to learn more articles about blockchain, please follow the WeChat public account: BSN Study Club

Guess you like

Origin blog.csdn.net/BSN_yanxishe/article/details/134854352