Developing smart contract on go-ethereum using Truffle framework

Aung Maw
infraGeth
Published in
3 min readJan 26, 2018

Smart contract is a code that can be deployed inside blockchain. Once it is deployed, Users can trigger it by using its address. In this tutorial, the smart contract is written with solidity language and deployed on semi pubic ethereum blockchain network. The development steps are simplified by using Truffle framework.

Prerequisites

OS: Ubuntu 16.04 ( 64 bit )

RAM: 2 GB

The go-ethereum setup steps can be followed here.

Truffle Installation

First, install nodejs using nvm.

sudo apt updatesudo apt install build-essential libssl-devcurl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | shsource ~/.profilenvm ls-remotenvm install 8.9.3

Install Truffle

npm install -g truffle

Truffle TutorialToken

Following steps are referenced from BUILDING ROBUST SMART CONTRACTS WITH OPENZEPPELIN

Writing contract

Inside directory ethProject

mkdir trufflecd truffletruffle unbox tutorialtokennpm install zeppelin-solidity

Create a new file TutorialToken.sol in directory contracts with the following contents.

pragma solidity ^0.4.17;import 'zeppelin-solidity/contracts/token/ERC20/StandardToken.sol';contract TutorialToken is StandardToken {
string public name = 'TutorialToken';
string public symbol = 'TT';
uint8 public decimals = 2;
uint public INITIAL_SUPPLY = 12000;
function TutorialToken() public {
totalSupply_ = INITIAL_SUPPLY;
balances[msg.sender] = INITIAL_SUPPLY;
}
}

Create another file 2_deploy_contracts.js in directory Migrations with below contents.

var TutorialToken = artifacts.require("TutorialToken");module.exports = function(deployer) {
deployer.deploy(TutorialToken);
};

Compile contract

truffle compile

Deploying contract

Change port value to 8545 ( geth rpc port ) in truffle.js file.

Add options in console.sh file in node_app directory to access from http-rpc.

geth --datadir ../eth_node --rpc --rpcaddr "0.0.0.0" --rpccorsdomain "*" --nodiscover console

Run geth on another terminal.

cd node_app./console.sh

Unlock account to send transaction ( deploy contract ).

personal.unlockAccount(eth.accounts[0],'passphrase',1000)

deploy contract

truffle migrate
successful migration

Interacting with smart contract from web ui

Change port to 8545 in src/js/app.js as below.

app.js

Run dev web server.

npm run dev
TutorialToken Dapp

Send Token and ether between nodes

Metamask

Installation

  1. firefox
  2. chrome
starting metamask

Send Token and ether to metamask accounts

After transferring ether

Using Remix to test smart contracts on both simulator and geth network

npm install -g remixd

Run remixd with absolute path

remixd -S /.../bcWorkshop/ethProject/truffle

Use remix web UI and connect to local file server and select web3 provider with http://127.0.0.1:8545 to connect to geth.

If you want to test quickly without geth, Select javascript VM.

Testing Contract using remix

Detail steps for remix setup.

Now you have developed smart contract using truffle framework and used standard library OpenZeppelin.

To develop smart contracts further, you need to learn solidity documentation.

--

--