原文地址:https://medium.com/@k3no/making-a-birthday-contract-858fd3f63618

先将datetime合约部署:https://github.com/pipermerriam/ethereum-datetime

pragma solidity ^0.4.;

contract DateTime {
/*
* Date and Time utilities for ethereum contracts
*
*/
struct _DateTime {
uint16 year;
uint8 month;
uint8 day;
uint8 hour;
uint8 minute;
uint8 second;
uint8 weekday;
} uint constant DAY_IN_SECONDS = ;
uint constant YEAR_IN_SECONDS = ;
uint constant LEAP_YEAR_IN_SECONDS = ; uint constant HOUR_IN_SECONDS = ;
uint constant MINUTE_IN_SECONDS = ; uint16 constant ORIGIN_YEAR = ; function isLeapYear(uint16 year) public pure returns (bool) {
if (year % != ) {
return false;
}
if (year % != ) {
return true;
}
if (year % != ) {
return false;
}
return true;
} function leapYearsBefore(uint year) public pure returns (uint) {
year -= ;
return year / - year / + year / ;
} function getDaysInMonth(uint8 month, uint16 year) public pure returns (uint8) {
if (month == || month == || month == || month == || month == || month == || month == ) {
return ;
}
else if (month == || month == || month == || month == ) {
return ;
}
else if (isLeapYear(year)) {
return ;
}
else {
return ;
}
} function parseTimestamp(uint timestamp) internal pure returns (_DateTime dt) {
uint secondsAccountedFor = ;
uint buf;
uint8 i; // Year
dt.year = getYear(timestamp);
buf = leapYearsBefore(dt.year) - leapYearsBefore(ORIGIN_YEAR); secondsAccountedFor += LEAP_YEAR_IN_SECONDS * buf;
secondsAccountedFor += YEAR_IN_SECONDS * (dt.year - ORIGIN_YEAR - buf); // Month
uint secondsInMonth;
for (i = ; i <= ; i++) {
secondsInMonth = DAY_IN_SECONDS * getDaysInMonth(i, dt.year);
if (secondsInMonth + secondsAccountedFor > timestamp) {
dt.month = i;
break;
}
secondsAccountedFor += secondsInMonth;
} // Day
for (i = ; i <= getDaysInMonth(dt.month, dt.year); i++) {
if (DAY_IN_SECONDS + secondsAccountedFor > timestamp) {
dt.day = i;
break;
}
secondsAccountedFor += DAY_IN_SECONDS;
} // Hour
dt.hour = getHour(timestamp); // Minute
dt.minute = getMinute(timestamp); // Second
dt.second = getSecond(timestamp); // Day of week.
dt.weekday = getWeekday(timestamp);
} function getYear(uint timestamp) public pure returns (uint16) {
uint secondsAccountedFor = ;
uint16 year;
uint numLeapYears; // Year
year = uint16(ORIGIN_YEAR + timestamp / YEAR_IN_SECONDS);
numLeapYears = leapYearsBefore(year) - leapYearsBefore(ORIGIN_YEAR); secondsAccountedFor += LEAP_YEAR_IN_SECONDS * numLeapYears;
secondsAccountedFor += YEAR_IN_SECONDS * (year - ORIGIN_YEAR - numLeapYears); while (secondsAccountedFor > timestamp) {
if (isLeapYear(uint16(year - ))) {
secondsAccountedFor -= LEAP_YEAR_IN_SECONDS;
}
else {
secondsAccountedFor -= YEAR_IN_SECONDS;
}
year -= ;
}
return year;
} function getMonth(uint timestamp) public pure returns (uint8) {
return parseTimestamp(timestamp).month;
} function getDay(uint timestamp) public pure returns (uint8) {
return parseTimestamp(timestamp).day;
} function getHour(uint timestamp) public pure returns (uint8) {
return uint8((timestamp / / ) % );
} function getMinute(uint timestamp) public pure returns (uint8) {
return uint8((timestamp / ) % );
} function getSecond(uint timestamp) public pure returns (uint8) {
return uint8(timestamp % );
} function getWeekday(uint timestamp) public pure returns (uint8) {
return uint8((timestamp / DAY_IN_SECONDS + ) % );
} function toTimestamp(uint16 year, uint8 month, uint8 day) public pure returns (uint timestamp) {
return toTimestamp(year, month, day, , , );
} function toTimestamp(uint16 year, uint8 month, uint8 day, uint8 hour) public pure returns (uint timestamp) {
return toTimestamp(year, month, day, hour, , );
} function toTimestamp(uint16 year, uint8 month, uint8 day, uint8 hour, uint8 minute) public pure returns (uint timestamp) {
return toTimestamp(year, month, day, hour, minute, );
} function toTimestamp(uint16 year, uint8 month, uint8 day, uint8 hour, uint8 minute, uint8 second) public pure returns (uint timestamp) {
uint16 i; // Year
for (i = ORIGIN_YEAR; i < year; i++) {
if (isLeapYear(i)) {
timestamp += LEAP_YEAR_IN_SECONDS;
}
else {
timestamp += YEAR_IN_SECONDS;
}
} // Month
uint8[] memory monthDayCounts;
monthDayCounts[] = ;
if (isLeapYear(year)) {
monthDayCounts[] = ;
}
else {
monthDayCounts[] = ;
}
monthDayCounts[] = ;
monthDayCounts[] = ;
monthDayCounts[] = ;
monthDayCounts[] = ;
monthDayCounts[] = ;
monthDayCounts[] = ;
monthDayCounts[] = ;
monthDayCounts[] = ;
monthDayCounts[] = ;
monthDayCounts[] = ; for (i = ; i < month; i++) {
timestamp += DAY_IN_SECONDS * monthDayCounts[i - ];
} // Day
timestamp += DAY_IN_SECONDS * (day - ); // Hour
timestamp += HOUR_IN_SECONDS * (hour); // Minute
timestamp += MINUTE_IN_SECONDS * (minute); // Second
timestamp += second; return timestamp;
}
}

然后获取datetime utility的地址,再部署另一个合约:

pragma solidity ^ 0.4.;
contract DateTime {
function getYear(uint timestamp) public constant returns (uint16);
function getMonth(uint timestamp) public constant returns (uint8);
function getDay(uint timestamp) public constant returns (uint8);
}
contract BirthDay {
uint public bday;
address public dateTimeAddr = 0xb23e0bfaeedcfce82ea2011f2a2726584e611e57;
DateTime dateTime = DateTime(dateTimeAddr);
function BirthDay() public {
bday = now;
}
function getBirthYear() view public returns (uint16){
return dateTime.getYear(bday);
}
function getBirthMonth() view public returns (uint8){
return dateTime.getMonth(bday);
}
function getBirthDay() view public returns (uint8){
return dateTime.getDay(bday);
}
}

Solidity 合约调用合约的更多相关文章

  1. Solidity智能合约调用智能合约

    来源:https://medium.com/@blockchain101/calling-the-function-of-another-contract-in-solidity-f9edfa921f ...

  2. Solidity truffle,部署合约到Ropsten测试链或主链,调用合约(转)

    Solidity truffle,部署合约到Ropsten测试链或主链,调用合约 转 https://blog.csdn.net/houyanhua1/article/details/89010896 ...

  3. web3 编译部署调用合约

    //导入solc 编译器 let solc = require('solc') let fs = require('fs') //读取合约 let sourceCode = fs.readFileSy ...

  4. 区块链入门(5)Truffle 项目实战,Solidity IDE, 智能合约部署

    在上一张我们学习了Truffle项目的创建,部署等相关内容,今天我们就来实战一下. 今天我们要做3件事: 1) 学习搭建一个Solidity IDE(Remix). 2) 使用这个Solidity I ...

  5. 智能合约调用另一合约中的payable方法

    参考链接: https://ethereum.stackexchange.com/questions/9705/how-can-you-call-a-payable-function-in-anoth ...

  6. Remix+Geth 实现智能合约部署和调用详解

    Remix编写智能合约 编写代码 在线调试 实现部署 调用接口 Geth实现私有链部署合约和调用接口 部署合约 调用合约 获得合约实例 通过实例调用合约接口 Remix编写智能合约 编写代码 Remi ...

  7. 以太坊系列之十七: 使用web3进行合约部署调用以及监听

    以太坊系列之十七: 使用web3进行智能合约的部署调用以及监听事件(Event) 上一篇介绍了使用golang进行智能合约的部署以及调用,但是使用go语言最大的一个问题是没法持续监听事件的发生. 比如 ...

  8. 以太坊系列之十六: 使用golang与智能合约进行交互

    以太坊系列之十六: 使用golang与智能合约进行交互 以太坊系列之十六: 使用golang与智能合约进行交互 此例子的目录结构 token contract 智能合约的golang wrapper ...

  9. 以太坊系列之十六:golang进行智能合约开发

    以太坊系列之十六: 使用golang与智能合约进行交互 以太坊系列之十六: 使用golang与智能合约进行交互 此例子的目录结构 token contract 智能合约的golang wrapper ...

随机推荐

  1. Java Platform SE binary已停止运行 Can't load AMD 64-bit.dll on a IA 32-bit platform错误

    这个我是在junit测试web项目时候遇到的问题,然后今天遇到一位网友也遇到了,报问题的是A卡了,所以今天做个记载,方便遇到问题的人,因为,之前我个人遇到这个问题,在网上找了很久都没有找到. 上面这个 ...

  2. 使用阿里云Code进行版本控制并配置IDEA

    1.    申请阿里code的账号,网址如下https://code.aliyun.com, 2.    申请完成之后,将账号信息发给项目负责人,由负责人加入项目中 3.    下载git,下载地址为 ...

  3. System.Web.HttpRequestValidationException: 从客户端(dbFlag=&quot;&lt;soap:Envelope xmlns...&quot;)中检测到有潜在危险的 Request.Form 值。

    System.Web.HttpRequestValidationException: 从客户端(dbFlag="<soap:Envelope xmlns...")中检测到有潜 ...

  4. JavaScript 与 Java、PHP 的比较

    网站开发的实践从设计方面开始,包括客户端编程语言.大体上说,在网页设计中使用了三种语言:HTML,CSS和Java.自从网站发明以来,HTML和CSS已经成为网页设计的基础,但是Java被用于添加网站 ...

  5. [原]zeromq框架测试报告

    一.环境: 服务器:linux 4核 16G 虚拟机 1台 客户端:linux 4核 16G 2000台(模拟) 数据包大小:1036字节 二.参数设置: ulimit -n 65536 服务端处理线 ...

  6. 解决genymotion-arm-translation.zip无法拖拽安装的问题[转]

    1.问题由来 适用情况一:当我们启动了Genymotion模拟器后,在AndroidStudio运行app时,弹出如下错误: INSTALL_FAILED_CPU_ABI_INCOMPATIABLE ...

  7. ORA-28595: Extproc 代理: DLL 路径无效解决办法

    报错信息: ORA-28595: Extproc 代理: DLL 路径无效 ORA-06512: 在 "SDE.ST_GEOMETRY_SHAPELIB_PKG", line 70 ...

  8. Weblogic配置SSl使用Https

    一 .可以开启自带的SSL连接 启动weblogic,进入左侧菜单,点击左侧的安全领域-->点击myrealm-->点击角色和策略-->点击服务器AdminServer 点击保存,w ...

  9. swift 学习-- 元组

    //元组 //定义:元组是有多个值组合而成的复合值,其中的值可以是任意类型,而且每一个元素的类型可以是不同的 let http404Error = (404, "Not Found" ...

  10. debian的bt下载工具

    sudo apt install qbittorrent 如果觉得下载慢,可以选择下载的文件,在trackers部分修改 https://github.com/ngosang/trackerslist