Member-only story
Making a Birthday Contract.
Let’s make a Birthday smart contract in Solidity.
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:
- To have a contract record it’s birth on the blockchain.
- To have the contract tell you it’s Birthday.
- Bonus / Extra Credit: Return the Birthday in a Human Readable way without the need of external UI’s.
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 :
The first thing this contract does is record it’s birthday via the now native function which is saved into a variable bday by the constructor:
uint public bday;function BirthDay() public {…