Making a Birthday Contract.

Intermediate Solidity Ethereum Notes & Development.

Note/selfplug: If you are looking for an introduction to Ethereum, Solidity and Smart Contracts I just published an eBook on getting started:https://www.amazon.com/dp/B078CQ8L7V

The Problem / challenge:

The contract :

pragma solidity ^ 0.4.0;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 = 0x8Fc065565E3e44aef229F1D06aac009D6A524e82;
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);
}
}

Read the Contract :

https://kovan.etherscan.io/address/0x86d686cc0e2feaa63834163af8b7a5fb869bb977#readContract

Development Notes :

uint public bday;function BirthDay() public  {
bday = now;
}
bday -> 1512417464 uint256
I'll use Piper Merriam's DateTime contract:https://github.com/pipermerriam/ethereum-datetimeIf you want to check it out or use it I deployed and verified a copy in Kovan:https://kovan.etherscan.io/address/0x8fc065565e3e44aef229f1d06aac009d6a524e82
The First thing we need is to include our ABI in the form of an abstract contract:contract DateTime {Followed by the function signatures we will use:function getYear(uint timestamp) public constant returns (uint16);function getMonth(uint timestamp) public constant returns (uint8);
function getDay(uint timestamp) public constant returns (uint8);
}Upon compiling we just feed the :Birthday contract and it will include this one.

The address for our previously deployed DateTime library comes next, it is hardcoded and public although it seems sensible to provide an ownable setter and maybe make it private if need be.address public dateTimeAddr = 0x8Fc065565E3e44aef229F1D06aac009D6A524e82;

The next step is to create a globally accessible contract instance of the DateTime Library at our specific address:DateTime dateTime = DateTime(dateTimeAddr);A tongue twister of a thing that basically says this is an instance of our abstract contract at this address.

And finally we can use our library inside functions like so:
function getBirthYear() view public returns (uint16){
return dateTime.getYear(bday);
}

Wrapping up.

About the Author :

AI, Software Developer, Designer : www.k3no.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store