LOADING

智能合约语言 Solidity – 以太单位及时间单位

智能合约语言 Solidity以太单位时间单位

电报联系方式

货币单位(Ether Units)

一个数字常量(字面量)后面跟随一个后缀wei, finney,szabo或ether,这个后缀就是货币单位。不同的单位可以转换。不含任何后缀的默认单位是wei。 不同的以太币单位转换关系如下:

  • 1以太(ether)等于1000芬尼(finney)。
  • 1以太等于1,000,000萨博(szabo)。
  • 1以太等于1,000,000,000,000,000微以太(wei)。

这表示不同的货币单位之间的转换关系,其中以太是最大的单位,而其他单位是以10的幂次方关联的。

以太币单位其实是密码学家的名字,是以太坊创始人为了纪念他们在数字货币的领域的贡献。他们分别是:
wei: Wei Dai 戴伟 密码学家 ,发表 B-money
finney: Hal Finney 芬尼 密码学家、工作量证明机制(POW)提出
szabo: Nick Szabo 尼克萨博 密码学家、智能合约的提出者

我们可以使用一下代码验证一个转换关系:

pragma solidity ^0.4.16;

contract testUnit {
function tf() public pure returns (bool) {
if (1 ether == 1000 finney){
return true;
}
return false;
}

function ts() public pure returns (bool) {
if (1 ether == 1000000 szabo){
return true;
}
return false;
}

function tgw() public pure returns (bool) {
if (1 ether == 1000000000000000000 wei){
return true;
}
return false;
}
}

时间单位(Time Units)

时间单位: seconds, minutes, hours, days, weeks, years均可做为后缀,并进行相互转换,规则如下:

  • 1 == 1 seconds (默认是seconds为单位)
  • 1 minutes == 60 seconds
  • 1 hours == 60 minutes
  • 1 days == 24 hours
  • 1 weeks = 7 days
  • 1 years = 365 days

进行日期计算时需要小心,因为现实中的时间并不是均匀的。每年并非都是恰好365天,每天也不是确切的24小时,这是由于闰年和闰秒的存在。

由于这些时间变化无法在智能合约中自动预测,所以需要依赖外部数据源或预言机(oracle)来获取精确的日历信息。这样,智能合约可以通过外部数据源来更新和调整时间,以确保准确的日期计算。

这些后缀不能用于变量。如果想对输入的变量说明其不同的单位,可以使用下面的方式:

pragma solidity ^0.4.16;

contract testTUnit {

function currTimeInSeconds() public pure returns (uint256){
return now;
}

function f(uint start, uint daysAfter) public {
if (now >= start + daysAfter * 1 days) {
// …
}
}
}

开发联系:DEXDAO

© 版权声明

相关文章

暂无评论

暂无评论...