单位和全局可用变量(Units and Globally Available Variables)
以太单位(Ether Units)
在 Solidity 中,数字字面量可以使用 wei、gwei 或 ether 作为后缀,表示以太币的不同子单位。如果未添加单位后缀,则默认单位为 wei。
assert(1 wei == 1);
assert(1 gwei == 1e9); // 1 gwei = 10^9 wei
assert(1 ether == 1e18); // 1 ether = 10^18 wei
这些单位后缀实质上是将数字乘以对应的 10 的幂。
注意:Solidity 从 0.7.0 版本起已移除 finney 和 szabo 单位。
时间单位(Time Units)
数值字面量也可以使用时间单位后缀,包括 seconds、minutes、hours、days 和 weeks。这些单位以 seconds 为基础进行定义:
-
1 == 1 seconds
-
1 minutes == 60