Solidity-007-以太币单位

162 阅读1分钟

背景

通过学习Solidity,然后输出文章检验自己的学习成果github仓库

基础知识

  • 以太币单位
单位WeiWei值
Wei11 Wei
Kwei (babbage)1,0001e3 Wei
Mwei (lovelace)1,000,0001e6 Wei
Gwei (shannon)1,000,000,0001e9 Wei
Microether (szabo)1,000,000,000,0001e12 Wei
Milliether (finney)1,000,000,000,000,0001e15 Wei
Ether1,000,000,000,000,000,0001e18 Wei
  • 交易需要支付ether
  • Solidity中,单位之间的换算是在数字后边加上weigweiether 来实现的
  • Solidity中,后面没有单位,缺省为wei
  • Solidity 0.7.0版本开始,finneyszabo 被移除了
  • gweiSolidity 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 等于 101810^{18} wei
uint public oneGwei = 1 gwei;  
bool public isOneGwei = 1 gwei == 1e9;  
  • 1 gwei 等于 10910^9 wei