Prototyping a Blockchain Smart Contract

Notes on building and deploying a Smart Contract in Ethereum

Image for post
Image for post
Photo by Mark Rabe on Unsplash

Smart Contracts 101

Image for post
Image for post
TCredits: escrow.com

Smart Contracts = Trustless Contracts

Shipping and Cargo

Image for post
Image for post

Ok, enough theory — lets build a very simple contract and deploy it.

Here’s what we have so far

There are basically just 4 Steps to create and deploy a smart contract to the Ethereum Blockchain

1. Start your private Ethereum network

geth — identity “yourIdentity” — init /path_to_folder/CustomGenesis.json — datadir /path_to_your_data_directory/data_directory

Remember, every blockchain has a unique genesis file.

geth --datadir /path_to_your_data_directory/data_directory --networkid 9876
geth attach /path_to_your_data_directory/YOUR_FOLDER/geth.ipc
Image for post
Image for post
javascript console

As we talked about earlier, Our (very) simple Smart Contract will do just one thing — extend a greeting every time somebody calls the contract.

pragma solidity ^0.4.0;contract greeter {     address owner;
string greeting;
function greeter(string _greeting) public {
greeting = _greeting;
owner = msg.sender;
}

function kill()
{ if (msg.sender == owner) selfdestruct(owner);
}
function greet() constant returns (string)
{
return greeting;
}
}

What the code above means:

#1 we defined a variable called  owner of the type address. This owner variable will receive the address of the account that is creating the contract (which in this case is our account)#2 We defined another variable and named it greeting . This is a string variable. greeting will store our greeting to whoever calls this contract. #3 The greeter function does two things, it assigns values to the owner and greeting variables. When we create this contract, the owner variable will store our public key, and the greeting variable will store the greeting that we want to display when somebody calls our smart contract.  (e.g. "Hello! My name is Abhi")#3. The kill () function will help us clean up useless smart contracts from our private blockchain. Only the account that created a smart contract can kill it.#4. greet() is  main function of the smart contract that will return the value of the greeting variable to the entity invoking the smart contract

2. Go here and type the contract code snippet in the Remix editor.

Image for post
Image for post
remix- this is a solidity editor and compiler
Image for post
Image for post

3. copy the code from the WEB3DEPLOY section.

Image for post
Image for post

Thing #1. It sets our greeting message

var _greeting = "Hello World! This is Abhi!" ; /* replace this with whatever greeting you want your Smart Contract to return */

Thing #2. It creates a new contract instance with all its methods and events defined in its json format

var browser_greeter_sol_greeterContract = web3.eth.contract([{"constant":false,"inputs":[],"name":"kill","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"greet","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[{"name":"_greeting","type":"string"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"}]);

Thing #3. It sets the owner of the contract , the bytecode of the contract to be deployed & the cost of deploying this contract to the Ethereum blockchain

var browser_greeter_sol_greeter = browser_greeter_sol_greeterContract.new( _greeting, 
{from: web3.eth.accounts[0],
data:'0x6060604052341561000f57600080fd5b6040516103a03803806103a0833981016040528080518201919050508060019080519060200190610041929190610088565b50336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055505061012d565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100c957805160ff19168380011785556100f7565b828001600101855582156100f7579182015b828111156100f65782518255916020019190600101906100db565b5b5090506101049190610108565b5090565b61012a91905b8082111561012657600081600090555060010161010e565b5090565b90565b6102648061013c6000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806341c0e1b514610048578063cfae32171461005d57600080fd5b341561005357600080fd5b61005b6100eb565b005b341561006857600080fd5b61007061017c565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100b0578082015181840152602081019050610095565b50505050905090810190601f1680156100dd5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16141561017a576000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16ff5b565b610184610224565b60018054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561021a5780601f106101ef5761010080835404028352916020019161021a565b820191906000526020600020905b8154815290600101906020018083116101fd57829003601f168201915b5050505050905090565b6020604051908101604052806000815250905600a165627a7a72305820c16d0d07cad4b5e82b9e94f4c8ed7e1bdbd0d52cda44575374e0b136d94f99fa0029',
gas: '4700000'},
function (e, contract)
{
console.log(e, contract);
if (typeof contract.address !== 'undefined')
{ console.log('Contract mined! address: ' +contract.address + ' transactionHash: ' + contract.transactionHash);}
})

3. Deploy the code copied from the WEB3DEPLOY section into geth terminal

Image for post
Image for post
(my terminal window, after running code snippets #1 & #2 — ignore the ‘undefined’ prompts)

Important:

Image for post
Image for post
web3.eth.accounts[0] simply fetches the public key of your first account — in case you have multiple accounts. (Sidenote: To see how many accounts you have simply type personal.listAccounts in the Javascript console.)
Image for post
Image for post

4. Start Mining

miner.start()
Image for post
Image for post
Contract is mined!

Calling our Smart Contract

var browser_greeter_sol_greeterContract = web3.eth.contract([{"constant":false,"inputs":[],"name":"kill","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"greet","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[{"name":"_greeting","type":"string"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"}]);(ABI in bold italics)
Image for post
Image for post
var myContract = web3.eth.contract([{"constant":false,"inputs":[],"name":"kill","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"greet","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[{"name":"_greeting","type":"string"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"}]); myContract.at("0x9f8cedf1f93e46fac74cb9415db8bbec85f239b4").greet();
Image for post
Image for post

Congratulations!

If you found the post helpful, please recommend it to others by clapping for this post ! If you have insights or comments, I would love to hear from you . You can also reach out to me directly on twitter.

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