Solidity 合约调用合约
原文地址: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 合约调用合约的更多相关文章
- Solidity智能合约调用智能合约
来源:https://medium.com/@blockchain101/calling-the-function-of-another-contract-in-solidity-f9edfa921f ...
- Solidity truffle,部署合约到Ropsten测试链或主链,调用合约(转)
Solidity truffle,部署合约到Ropsten测试链或主链,调用合约 转 https://blog.csdn.net/houyanhua1/article/details/89010896 ...
- web3 编译部署调用合约
//导入solc 编译器 let solc = require('solc') let fs = require('fs') //读取合约 let sourceCode = fs.readFileSy ...
- 区块链入门(5)Truffle 项目实战,Solidity IDE, 智能合约部署
在上一张我们学习了Truffle项目的创建,部署等相关内容,今天我们就来实战一下. 今天我们要做3件事: 1) 学习搭建一个Solidity IDE(Remix). 2) 使用这个Solidity I ...
- 智能合约调用另一合约中的payable方法
参考链接: https://ethereum.stackexchange.com/questions/9705/how-can-you-call-a-payable-function-in-anoth ...
- Remix+Geth 实现智能合约部署和调用详解
Remix编写智能合约 编写代码 在线调试 实现部署 调用接口 Geth实现私有链部署合约和调用接口 部署合约 调用合约 获得合约实例 通过实例调用合约接口 Remix编写智能合约 编写代码 Remi ...
- 以太坊系列之十七: 使用web3进行合约部署调用以及监听
以太坊系列之十七: 使用web3进行智能合约的部署调用以及监听事件(Event) 上一篇介绍了使用golang进行智能合约的部署以及调用,但是使用go语言最大的一个问题是没法持续监听事件的发生. 比如 ...
- 以太坊系列之十六: 使用golang与智能合约进行交互
以太坊系列之十六: 使用golang与智能合约进行交互 以太坊系列之十六: 使用golang与智能合约进行交互 此例子的目录结构 token contract 智能合约的golang wrapper ...
- 以太坊系列之十六:golang进行智能合约开发
以太坊系列之十六: 使用golang与智能合约进行交互 以太坊系列之十六: 使用golang与智能合约进行交互 此例子的目录结构 token contract 智能合约的golang wrapper ...
随机推荐
- fn project Function files 说明
主要是文件 func.yaml func.json 详细说明如下: An example of a function file: name: fnproject/hello version: 0.0. ...
- 【CSS3】 - 初识CSS3
.navdemo{ width:560px; height: 50px; font:bold 0/50px Arial; text-align:center; margin:40px auto 0; ...
- Windows Communication Foundation (WCF)和Windows CardSpace的示例程序
微软公司昨天发布了一个Windows Communication Foundation (WCF)和Windows CardSpace的示例程序包,内容极为丰富,从最简单的Hello World到复杂 ...
- java里的switch循环--你妹考试落榜了
总结:switch循环,不用break.那么程序每一个case都会运行到,直到遇到break停止 package com.aa; //格子区域 //3行3列的格子 public class Bu { ...
- TCP之四:TCP 滑动窗口协议 详解
滑动窗口机制 滑动窗口协议的基本原理就是在任意时刻,发送方都维持了一个连续的允许发送的帧的序号,称为发送窗口:同时,接收方也维持了一个连续的允许接收的帧的序号,称为接收窗口.发送窗口和接收窗口的序号的 ...
- 1147 Heaps
1147 Heaps(30 分) In computer science, a heap is a specialized tree-based data structure that satisfi ...
- Linux学习笔记 -- 系统目录结构
以root用户登录系统后,在当前命令窗口下输入命令: ls / 我们可以看到目录结构类似下图: 树状目录结构可以表示为: 解析: /bin:bin是Binary的缩写, 这个目录存放着最经常使用的命令 ...
- web开发 那些年基于Redis的Provider库
因为session基于本地cache,以前我们自己写分布式缓存,或者数据库存储,或者cookie加密存储,来保存用户状态信息,但较少的直接通过创建一个继承 SessionStateStoreProvi ...
- python's twelth day for me
闭包: 内层函数对外层函数的变量(非全局变量)的引用. python 遇到闭包,有一个机制,会开辟一个空间,将闭包中的全部变量放入其中.且不会随着函数的结束而关闭. 闭包的完美体现:装饰器. 打印函数 ...
- 从javascript的循环问题来看待闭包本质
第一次接触这个问题还是在我刚开始学js的时候,当时就是一头雾水,时隔一年多了,突然又想起了这个问题,在这个春气盎然的周末,我就坐下来研究下并把结果和大家分享下: 先看代码:demo.html < ...