背景
通过学习Solidity,然后输出文章检验自己的学习成果github仓库
基础知识
- 以太币单位
| 单位 | Wei | Wei值 |
|---|---|---|
| Wei | 1 | 1 Wei |
| Kwei (babbage) | 1,000 | 1e3 Wei |
| Mwei (lovelace) | 1,000,000 | 1e6 Wei |
| Gwei (shannon) | 1,000,000,000 | 1e9 Wei |
| Microether (szabo) | 1,000,000,000,000 | 1e12 Wei |
| Milliether (finney) | 1,000,000,000,000,000 | 1e15 Wei |
| Ether | 1,000,000,000,000,000,000 | 1e18 Wei |
- 交易需要支付
ether - 在
Solidity中,单位之间的换算是在数字后边加上wei,gwei或ether来实现的 - 在
Solidity中,后面没有单位,缺省为wei - 从
Solidity 0.7.0版本开始,finney和szabo被移除了 gwei在Solidity 0.6.11版本中添加,因此在0.6.11之前的版本中不可用
例子
该例子是测试了以太币的单位换算
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract EtherUnits {
uint public oneWei = 1 wei;
bool public isOneWei = 1 wei == 1;
uint public oneEther = 1 ether;
bool public isOneEther = 1 ether == 1e18;
uint public oneGwei = 1 gwei;
bool public isOneGwei = 1 gwei == 1e9;
}
程序解析
uint public oneWei = 1 wei;
bool public isOneWei = 1 wei == 1;
1 wei等于1,若没填写单位,默认是以wei为单位
uint public oneEther = 1 ether;
bool public isOneEther = 1 ether == 1e18;
- 1
ether等于wei
uint public oneGwei = 1 gwei;
bool public isOneGwei = 1 gwei == 1e9;
- 1
gwei等于wei