Introducing Ethereum Development – Part 5 – Deployment

on

In this series, we have followed fictional character Alice as she builds out a donation system with Ethereum. So far, she has built and tested a smart contract and would now like to deploy it (before starting work on the frontend GUI for her app). After some research on the web, she decides to try out three different approaches with Mist, Truffle and pure JS. To deploy a contract, we need two key pieces of data – the contract ABI (Application Binary Interface) and compiled bytecode.

Bytecode
In our examples, we have used the high level language Solidity because it has excellent Javascript support. To execute this code on the blockchain, we need to compile it to bytecode. The Ethereum Virtual Machine (EVM) runs bytecode instructions on a virtual machine – allowing the same code to run on multiple platforms. Function names and input parameters are hashed during compilation so we also need to provide additional information in the form of an ABI so that other accounts use our contract.

ABI
The Application Binary Interface provides the set of functions and arguments required to interact with a deployed contract as a JSON object. The function definitions in the ABI are hashed when making calls to a deployed contract so it can call methods and retrieve data.

There are many ways to deploy contract code. We can use:

Mist
Mist is a popular Ethereum wallet application that runs on Mac, Windows and Linux. It provides a number of tools for interacting with the blockchain including a Dapp browser and Remix IDE – a GUI for writing and deploying contracts. We can simply past contract code into the Remix IDE and click a button to deploy. This tool is also available on the web (you don’t have to download Mist and run a full node to access it).

Remix IDE screenshot

Just Javascript
Using the bytecode and ABI we generated with Remix IDE we can use web3 library to deploy a contract.




Simple Contract Deployment





view rawexampleDeployment.js hosted with ❤ by GitHub
If you are browsing via Mist or Chrome with MetaMask plugin enabled, clicking this button will deploy the example contract code (you’ll need to wait a minute until it’s mined for a response)

Deploy example contract

Truffle
Truffle has a slightly more advanced mechanism for deployment. With the previous two methods, we are unable to update our contract if we found a bug. To work around this we can make use of a second contract to route requests to the latest deployed version of our contract code. Note that if a contract contains data (like ours), this would not be migrated and we would need to refactor the code to separate the data we want to keep from the methods that act on that data. Truffle uses the following contract code when issuing command truffle migrate

pragma solidity ^0.4.4;

contract Migrations {
address public owner;
uint public last_completed_migration;

modifier restricted() {
if (msg.sender == owner) _;
}

function Migrations() {
owner = msg.sender;
}

function setCompleted(uint completed) restricted {
last_completed_migration = completed;
}

function upgrade(address new_address) restricted {
Migrations upgraded = Migrations(new_address);
upgraded.setCompleted(last_completed_migration);
}
}
view rawsolidityMigrate.sol.js hosted with ❤ by GitHub
Relaying without Truffle
We could also use a new feature in the Homestead release of Ethereum called DELEGATECALL. If a method called on a contract does not exist a default unnamed function is called. We can take advantage of this to forward requests and calls to the actual address of our contract, as in this example.

contract Relay {
address public currentVersion;
address public owner;

function Relay(address initAddr){
currentVersion = initAddr;
owner = msg.sender;
}

function update(address newAddress){
if(msg.sender != owner) throw;
currentVersion = newAddress;
}

function(){
if(!currentVersion.delegatecall(msg.data)) throw;
}
}
view rawRelayContract.sol.js hosted with ❤ by GitHub