Image for post
Image for post

Ethereum, tokens & smart contracts.

Notes on getting started Part 5. Smarter Contracts

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


FILE: constructorContract.solpragma solidity ^0.4.0;contract constructorContract {address father;function constructorContract() {
father = msg.sender;
function whosyourfather() constant returns (address) {
return father;
Readout only, deployed using our trusty compiler from past notes:Setting up...
Reading contract Source...
saving ABI
ABI Saved
gasEstimate: 110760 + 50000
gasPrice: 20000000000
unlocking Coinbase account
Deploying contract...
Successfully deployed Contract with address: 0xc80ef52d06866cbdebb2a709d075d2508ea0c5d5
FILE: callContract.jsconsole.log('Setting up...');
const solc = require ('solc');
const Web3 = require ('web3');
console.log('Reading abi');
const contractABI = require("./constructorContract.json");
const web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
console.log('Creating contract instance');
const contract = web3.eth.contract(contractABI);
var contractInstance ="0xc80ef52d06866cbdebb2a709d075d2508ea0c5d5");
console.log ('calling contract');var returner =;
console.log('This contracts father is :' + returner);
//This contracts father is :0x001301ad1556fd419cf8970b174fe9af34267eb8


I Recommend you read the full Solidity Docs on Types, you will also be consulting it from time to time.

Value Types :

// FILE: booleanContract.solpragma solidity ^0.4.0;contract booleanContract {bool switcher;function flipSwitch() constant returns (bool) {
switcher = !switcher;
return switcher;
Welcome Good Sir to  Compiler Mk2 Setting up...
Reading contract Source...
saving ABI
ABI Saved
gasEstimate: 96783
gasPrice: 21000000000
unlocking Coinbase account
Deploying contract...
Succesfully deployed Contract with address: 0xe10e9ec821c219ca483c3a48915c3530add08724

var switcher =;
console.log('switcher state : ' + switcher); // True
// switcher was originally false.
Note: If you want to actually change the switcher boolean in the contracts storage, you would need to call flipSwitch in a transaction.


// FILE: enumContract.solpragma solidity ^0.4.0;contract enumContract {enum someThings { CHAIR, CAR, TREE, COFFEE }
someThings constant favoriteThing = someThings.COFFEE;
function getFavoriteThing() returns (uint) {
return uint(favoriteThing);
// Notice the lack of semicolon at the end of the enum values.
Deploying contract...
Succesfully deployed Contract with address: 0x5d9a2c9e85e6b46466abc42408ab80d46b61d17a
FILE : callContract.js ( skipping the header section from now on, but it is the same file, just change the ABI and the contract address.
var favoriteThing =;
console.log('Favorite Thing: (0:CHAIR, 1:CAR, 2:TREE, 3: COFFEE ) : ' + favoriteThing);
// Favorite Thing: (0:CHAIR, 1:CAR, 2:TREE, 3:COFFEE ) : 3// Notice that enum gives us a number not a string, so it is up to us to interpret it when reading it. It is also zero indexed.

Reference types :


// FILE: arrayContract.sol

pragma solidity ^0.4.0;
contract arrayContract {uint[] someNumbers = [10,13,14];function getArray() constant returns (uint) {
return someNumbers[1]; // should return 13
Deploying contract...
Succesfully deployed Contract with address: 0x97b9892deb69a94b428da3c590c7ca793341493c
var getArr =;
console.log('getArray : ' + getArr);
// getArray : 13


// FILE: structContract.solpragma solidity ^0.4.0;contract structContract {struct Stuff {
address addr;
uint[] someNumbers;
Stuff myStuff;
Stuff otherPeoplesStuff;
function structContract(){
myStuff.addr = msg.sender;
myStuff.someNumbers = [20,30,40];
otherPeoplesStuff.addr = msg.sender;
otherPeoplesStuff.someNumbers = [22,33,44];
function getMyStuff() returns (address, uint){
return (myStuff.addr, myStuff.someNumbers[1]);
function getOthersStuff() returns (address, uint){
return (otherPeoplesStuff.addr, otherPeoplesStuff.someNumbers[0]);
Deploying contract...
Succesfully deployed Contract with address: 0x8170478d82c63ddf1786157e7ab329e0925dc94f
var myStuff =;
console.log('myStuff : ' + myStuff);
var othersStuff =;
console.log('othersStuff : ' + othersStuff);
// myStuff : 0x001301ad1556fd419cf8970b174fe9af34267eb8,30
// othersStuff : 0x001301ad1556fd419cf8970b174fe9af34267eb8,22


From the Docs:mapping(_KeyType => _ValueType) mapNameHere _KeyType can be almost any type except for a mapping, a dynamically sized array, a contract, an enum and a struct._ValueType can actually be any type, including mappings.Keys can be integers, strings, or addresses, values can be any other type.
// FILE mappingContract.solpragma solidity ^0.4.0;contract mappingContract {mapping(uint => uint[]) luckyNumbers;function mappingContract(){
luckyNumbers[1] = [10,20,30];
luckyNumbers[2] = [11,22,33];
function getLucky(uint luckyKey) constant returns (uint[]){
return luckyNumbers[luckyKey];
Deploying contract...
Successfully deployed Contract with address: 0x1e5f7deadf846ae2f8402c2e3d1cc38ae556ef8e
var luckyNumbers1 =;
console.log('Lucky Numbers 1 : ' + luckyNumbers1);
var luckyNumbers2 =;
console.log('Lucky Numbers 2 : ' + luckyNumbers2);
// Lucky Numbers 1 : 10,20,30
// Lucky Numbers 2 : 11,22,33
Image for post
Image for post
Now in book form !If you are looking for an introduction to Ethereum, Solidity and Smart Contracts these notes were edited into a convenient  book for you !Available in ebook and paperback here:

Written by

AI, Software Developer, Designer :

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