Proj 12: Making an Ethereum Contract (15 pts.)

What You Need

A Linux machine running a private Ethereum blockchain. You prepared one in the previous project. I assume you are using Ubuntu 16.04 64-bit in the instructions below.

Purpose

To set up an Ethereum Smart Contract. The ability to run programs like this is the main feature that distinguishes Ethereum from Bitcoin.

Task 1: Building and Installing Ethereum Compilers

To make a contract, we need a compiler. We'll install two compilers: Solidity and LLC.

Installing Dependencies

On your Linux machine, in a Terminal window, execute this command. Enter your password when you are prompted to.
sudo apt-get update
Execute these commands to install dependencies, fetch the source code, and compile it:
sudo apt install -y build-essential cmake libboost-all-dev
cd
git clone --recursive https://github.com/ethereum/solidity.git
cd solidity
mkdir build && cd build
cmake ..
make
This will take a few minutes. To complete the installation, execute this command:
sudo make install
Execute this command to set the PATH environment variable.
nano ~/.profile
At the end of this file, add this line, as shown below.
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/lib"

Save the file with Ctrl+X, Y, Enter.

Execute this command to restart the machine:

sudo reboot

Test the Installation

Execute these commands:
solc --version
lllc --version
You should see information about both compilers, as shown below. This shows that they are installed and working.


Task 2: Compile a Solidity Contract

There are two compilers available to us: LLC and Solidity. Solidity is the easier one, so we'll use it.

Making an Identity Function

Execute these commands:
cd
nano identity.sol
In nano, paste in the code shown below. This is a simple function that takes an integer as input and returns it as output.
pragma solidity ^0.4.0;
contract IdentityFunction {
  function identity (uint value) returns (uint) {
    return value;
  }
}

Save the file with Ctrl+x, y, Enter.

Compiling the Identity Function

Execute this command:
solc --optimize --bin identity.sol
The compiled code appears, in an ASCII representation of bytecode, as shown below.

Viewing the Assembly Code

Execute this command:
solc --optimize --asm identity.sol
The assembly code appears, as shown in the two images below.

This code looks strange at first, but it's a lot like other assembly code, with opcodes including ADD, XOR, and NOT, as listed here.


Task 3: Preparing for Deployment

To deploy code, we need some more tools.

Installing node.js

We must install node.js from source, because the Ubuntu repositories contain a version that is too old.

Execute these commands:

cd
wget https://nodejs.org/dist/v8.1.0/node-v8.1.0-linux-x64.tar.gz
tar xvf node-v8.1.0-linux-x64.tar.gz
sudo chown -R root.root node-v8.1.0-linux-x64
sudo mv node-v8.1.0-linux-x64 /opt/node
sudo ln -s /opt/node/bin/node /usr/bin/node
sudo ln -s /opt/node/lib/node_modules/npm/bin/npm-cli.js /usr/bin/npm

Testing the Installation

Execute these commands:
node -v
npm -v
You see version numbers, as shown below.

Installing web3.js

This library is used to interact with local or remote Ethereum nodes.

Execute this command:

npm install web3
There are a few warning messages, but no errors, as shown below.

Installing testrpc

The testrpc utility simulates an Ethereum blockchain so we can easily test deployment at no cost.

It also executes transactions immediately, without waiting for blocks to be mined.

Execute these commands:

sudo npm install --unsafe-perm -g scrypt
sudo npm install --unsafe-perm -g ethereumjs-testrpc
cd /opt/node/lib/node_modules/ethereumjs-testrpc/node_modules/scrypt
sudo ln -s /opt/node/lib/node_modules/scrypt/build

Testing the Installation

Execute this command:
testrpc
You see two lists of addresses, and then the message "Listening on localhost:8545", as shown below.

Leave this window open and running.

Saving a Screen Image

Make sure you can see the message "Listening on localhost:8545", as shown above.

Capture a full-screen image.

YOU MUST SUBMIT A FULL-SCREEN IMAGE FOR FULL CREDIT!

Save the image with the filename "YOUR NAME Proj 12a", replacing "YOUR NAME" with your real name.


Task 4: Deploying the Contract

You have one Terminal window running testrpc. Leave that window alone.

Open a second Terminal window and execute this command:

node
This starts node.js and shows a > prompt. This is an interactive console to run JavaScript commands.

At the > prompt, execute these commands to connect the web3 library to our testrpc Ethereum node:

var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
These commands have no output, so Node replies with "undefined", as shown below.

Making a Contract ABI

The Contract is in binary bytecode, but we need to interact with it via JavaScript. To do that, we need an ABI (Application Binary Interface).

We'll use an online Solidity compiler named "remix" to do that.

In a Web browser, open:

https://remix.ethereum.org/

The online compiler opens, showing sample code titled "ballot.sol", as shown below.

At the top left, click the first icon, which shows a white plus sign in a black circle.

A new pane opens, titled "Untitled.sol". Paste in this code, as shown below.

pragma solidity ^0.4.0;
contract IdentityFunction {
  function identity (uint256 input) constant returns (uint256);
}

On the right side of the compiler page, on the Contract tab, at the bottom, click the "Contract details (bytecode, interface etc.)" link.

JavaScript code appears in the Interface field. Highlight this code and copy it, as shown below.

In your Terminal window, at the > prompt, type this:

var abi =
Then paste in the JavaScript code you copied, and press Enter. The reply is "undefined" again, as shown below.

We have now stored the JavaScript in a variable named "abi".

The next step is to create an "Identity" object from that JavaScript code.

In your Terminal window, at the > prompt, type this:

var Identity = web3.eth.contract(abi);
Once again, the reply is "undefined".

Next we need to insert the bytecode we created earlier in this project.

In your Terminal window, at the > prompt, paste in this long line of text:

var bin = '60606040523415600b57fe5b5b608e8061001a6000396000f300606060405263ffffffff7c0100000000000000000000000000000000000000000000000000000000600035041663ac37eebb81146039575bfe5b3415604057fe5b6049600435605b565b60408051918252519081900360200190f35b805b9190505600a165627a7a72305820f7b2ec8f7a81287fa09c3bf1cdf6590ffd9ff4548907f8ab36a15aa131f1922f0029';
Once again, the reply is "undefined", as shown below.

Specifying the Sender's Address

We need to specify which account is sending the transaction that deploys the contract on the blockchain. We'll use the first account, which is called the "coinbase".

In your Terminal window, at the > prompt, type this:

var account = web3.eth.coinbase;
Once again, the reply is "undefined".

Deploying the Contract

In your Terminal window, at the > prompt, type this:
var tut = Identity.new({data: bin, from: account, gas: 100000});
This command has deployed the contract onto the testrpc blockchain.

Once again, the reply is "undefined", as shown below.

The Terminal window running "testrpc" shows the newly created transaction, as shown below.

Viewing the tut ABI

We have created an object named "tut". To see its ABI, in your Terminal window, at the > prompt, type this:
tut.abi
The ABI includes an identity function, as shown below.

Invoking the Identity Function

In your Terminal window, at the > prompt, type this:
tut.identity(1337)
The ABI includes an identity function, as shown below.

The function returns the number we put in, as shown below.

Saving a Screen Image

Make sure you can see c: [ 1337 ], as shown above.

Capture a full-screen image.

YOU MUST SUBMIT A FULL-SCREEN IMAGE FOR FULL CREDIT!

Save the image with the filename "YOUR NAME Proj 12b", replacing "YOUR NAME" with your real name.

Turning in your Project

Email the images to cnit.141@gmail.com with the subject line: Proj 12 from YOUR NAME.

Sources

How to Build a Private Ethereum Blockchain
Building and installing Ethereum compilers
Compiling an LLL Contract for the First Time
Ethereum JSON RPC Methods
Deploying your first LLL contract — Part 1
Deploying your first LLL contract — Part 2

Posted 7-13-17 by Sam Bowne