Ethereum, tokens & smart contracts.

Notes on getting started : Part 12. Some advanced considerations.

Previous notes in case you are just joining us:Part 1. Setting up.
Part 2. Web3.js/node.
Part 3. Solidity.
Part 4. Smart Contracts.
Part 5. Smarter Contracts.
Part 6. Tokens & Inheritance.
Part 7. ERC20 Token Standard.
Part 8. Crowdfunding and ICOs.
Part 9. Dapps & MetaMask.
Part 10. Remix, Truffle, TestRPC.
Part 11. Some intermediate considerations.

What the heck is an oracle ?

// A child is born, so a nurse in the hospital calls a registry contract(pseudo code):stKenosHospitalBirthRegistryContract.newBaby.call(name, weight, DOB, etc...);// The contracts newBaby method could then transfer or allocate funds to a healthcare plan, a university plan and a life insurance plan for some bumper to bumper coverage.
// A government sets a gas price(pseudo Code):governmentGasPriceSetter.set.call($1);// And other contracts watching:if gasPrice >= $1 
taxiFarePriceSetter.setMinimumRide.call($20);
// In order to watch a contract you would need web3, events and filters or another complex interaction in between contracts.
//  1,000 fish go to the polls and vote via a smart contract that tallies the numbers:400 - Patrick Star
300 - Sandy Cheeks
100 - Spongebob SquarePants
100 - Squidward tentacles
100 - Plankton and Karen
// And another one would then release some funds to the winner by reading the results.campaign.payWinner.call(poll.winner, payout);

Time in Solidity:

pragma solidity ^0.4.0;contract timeGetter {

function getBlockTime() public constant returns (uint) {
return block.timestamp;
}

function getNow() public constant returns (uint) {
return now; // an alias for block.timestamp
}
}// getBlockTime() 1509561096
// getNow() 1509561096
// Both are UNIX Timestamps: https://www.unixtimestamp.com
pragma solidity ^0.4.0;
contract futureTime {

function getNow() public constant returns (uint) {
return now;
}

function getDayAfter() public constant returns (uint) {
return now + 1 days;
}

function getWeekAfter() public constant returns (uint) {
return now + 1 weeks;
}

}
// GetNow 1509603281
// getDayAfter 1509689682
// getWeekAfter1510208083
Note/Caveats: Miners can manipulate timestamps to a certain degree, ( in the seconds range ). Additionally there is no calendar time, so you would need to use an external library or oracle.

Distributing Tokens:

// From the DOG Token in previous notes:
transfer(address, amount);
The cost is in between 50,000 - 80,000 wei, which translates to about $0.015 - $0.023 per transfer. Transfers for 1,000 users using the high end would be around $23 for 10,000 $230 and so on at current (late 2017) prices.
contract Airdrop {
function drop(ERC20 token, address[] recipients, uint256[] values) public {
for (uint256 i = 0; i < recipients.length; i++) {
token.transfer(recipients[i], values[i]);
}
}
}
// But your costs are the same if not slightly higher.
function withdraw() {
uint amount = pendingWithdrawals[msg.sender];
pendingWithdrawals[msg.sender] = 0;
msg.sender.transfer(amount);
}
// Costs are hard to calculate, but if you update a mapping or address/amount pairs, you are looking at about $0.008 everytime you do it, but the big advantage is you can update thousands of addresses in one go very economically.

Truffle Boxes !

Installing a Truffle Box is easy enough, just follow the Instructions ( make sure you make a new folder though as to not pollute your directory ), once installed you will have a few directories with contracts migrations,dependencies and source files for your Dapp...
Running the Truffle Box had some issues in my case I needed to sort out: - I got caught in between versions of Truffle and at first it wouldn't compile, upgrading to the latest Truffle Version solved that problem.- There was no blockchain specified, so truffle.js had to be modified to:module.exports = {
networks: {
development: {
host: "localhost",
port: 8545,
gas: 4600000,
network_id: "*" // Match any network id
}
}
};
- Additionally, there were Gas Problems, the above gas line solved them by increasing it generously, but you should fine tune the amount for main net deployment.
One last bit of setup is MetaMask in your browser, just point it to testRPC and log in...And voila:

Open Zeppelin.

$ mkdir awesomeProject && cd awesomeProject
$ npm init
$ truffle init
$ npm install zeppelin-solidity
$ truffle compile
pragma solidity ^0.4.0;import "../node_modules/zeppelin-solidity/contracts/token/MintableToken.sol";contract superToken is MintableToken {}
- crowdsale contracts: Capped, refundable and other base tokens ready for a crowd sale.- lifecycle: Super handy contracts for upgrading,pausing & destroying contracts for instance.- math: utility for safe math operations.- ownership: Added functionality related to ownership, things like claiming,contacting, etc.- payment: Just a pull payment contract for now.- Token: A number of token templates to choose from. - Other: interesting contracts for various other functionalities.

Auditing & Security

Parting Words:

Final Recap :

✨✨  Now a Book ! ✨✨If you are looking for an introduction to Ethereum, Solidity and Smart Contracts these notes were edited and revised into a book, a super convenient way to get you or your team started ! Available in ebook and paperback:https://www.amazon.com/dp/B078CQ8L7V    🙏 🙏 😊 🙏 🙏

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