Skip to content

damoresa/ethereum-development

Repository files navigation

Ethereum development

The purpose of this repository is to serve as a step by step guide to install all the recommended tools for smart contract development and to deploy said smart contracts on the Ethereum networks - in the example we'll be deploying it to the Ropsten testnet, but the process should be the same for every other network out there.

This repository will also cover how to get funds on blockchain testnets in order to deploy your contracts and run tests in them.

You can see more detailed information on this Medium article.

Now, in order to keep it as simple as possible, we'll be using the Truffle framework in order to generate a local blockchain instance and use it's tooling for testing, compiling and deployments.

We'll also try to cover Metamask in order to manage our wallets and interact with the blockchain via our browser in case it's required.

Last but not least, we'll be using Infura to avoid having to create our own testnet blockchain node in order to deploy our contracts. Infura is a public Ethereum node you can use to access the network without connecting to it yourself.

Installing the tools

  1. Install windows-build-tools. You'll need these to build some of the tools that will be downloaded later on the tutorial.
npm install -g windows-build-tools
  1. Install Truffle
npm install -g truffle
  1. Install Ganache and Ganache CLI
# Download the appropiate Ganache distribution from the Github releases
# https://github.com/trufflesuite/ganache/releases

# Install the CLI
npm install -g ganache-cli

NOTE: This guide has been tested with the following versions:

  • Truffle: 4.1.14
  • Ganache: 1.2.2
  • Ganache CLI: 6.1.8
  1. Install Metamask:

This step is easy enough as in you can just install the plugin for your browser straight up from the official Metamask site.

In order to configure it, you'll be prompted for a password the very first time you open up the plugin. After introducing your password, you'll have to go through the usual legal yada yada and finally you'll be provided with the mnemonic 12 words phrase that you can use to recover your Metamask account. It is very important that you store that mnemonic safely and you never share it with anyone, as it can compromise your account and all of it's Ether.

After storing your mnemonic, you're done with Metamask's installation and configuration and you're able to send funds to it's generated account.

Truffle CLI on Windows disclaimer

It is important to note that when executing the truffle command on any terminal on Windows, the OS will try to execute the truffle.js configuration file instead of invoking the CLI command.

An easy workaround for this is to invoke truffle.cmd explicitly.

truffle.cmd compile

Scaffolding your first project

Truffle provides developers with the concept of boxes. Said boxes provide helpful boilerplates that allow developers to focus on developing their dapp rather than spending time structuring the entire project.

You can browse the list of existing boxes or even submit a new one at the Truffle Boxes site.

When you've chosen a box, all you have to do is download it using the unbox command.

truffle unbox <box-name>

If you wish to start a new project with no special scaffold on it, you can use the init command to scaffold an empty project.

truffle init

IMPORTANT: As of 27/09/2018, Truffle doesn't seem to support working behind a corporate proxy, meaning you have to scaffold your project by downloading the Truffle init default repository.

Getting funds

Getting funds on the main network is as easy as buying Ether on any stock house.

Deploying on Ganache

This section covers the process of deploying your smart contract to a local Ganache instance.

  1. Configure your truffle.js to point to the Ganache server
// Changed port to 7545 to support Ganache testing
module.exports = {
  networks: {
    development: {
      host: "localhost",
      port: 7545,
      network_id: "*" // Match any network id
    }
  }
};
  1. Execute the migration task:
truffle migrate
  1. Now you can see your smart contract on Ganache and you can also interact with it via any client like Web3js.

Deploying on the Ropsten testnet

First of all, you have to get some Ropsten ether. You can try any of the following faucets to allocate some ether on your Metamask account:

  1. Ropsten Ethereum Faucet
  2. Test Ether Faucet
  3. bitfwd Ropsten Faucet

Faucets will help you get ether by sending you a set amoutn of ether on request. Do not get greedy or you'll be greylisted or blocked from the faucets.

Once you've allocated some funds on your Metamask account, it's time to configure Truffle to handle the contract deployment. Get to the truffle.js file and change it as shown in the following snippet:

'use strict';

// We will be using this provider to hook our Metamask wallet
const HDWalletProvider = require('truffle-hdwallet-provider');

// Keep your secrets file on gitignore or set them on environment variables
const SECRETS = require('./secrets');

// We'll be using the Metamask mnemonic to hook with the wallet
// We'll also be using our Infura account to avoid creating a local 
// blockchain to connect to the testnet.
const ROPSTEN_MNEMONIC = process.env.TRUFFLE_ROPSTEN_MNEMONIC || SECRETS.METAMASK.MNEMONIC;
const INFURA_API_KEY = process.env.INFURA_API_KEY || SECRETS.INFURA.API;

// Changed port to 7545 to support Ganache testing
module.exports = {
    networks: {
        development: {
            host: "localhost",
            port: 7545,
            network_id: "*" // Match any network id
        },
        // Configure the Ropsten network
        ropsten: {
            provider: function () {
                return new HDWalletProvider(ROPSTEN_MNEMONIC, `https://ropsten.infura.io/v3/${INFURA_API_KEY}`)
            },
            network_id: 3
        }
    }
};

Once we're set with the configurations, all we have to do is deploy the contract using Truffle as follows:

truffle migrate --network ropsten

This command will have Truffle connect to Ropsten via Infura and deploy our smart contract using our Metamask wallet for funds.

Last but not least, we can test our deployed contract with Truffle using exec.
exec will let you execute scripts on a network of your choice. You can find an example script that creates some rooms and then retrieves their information under the queries folder on this repository. All you need is the contract hash generated after it's deployed.

In order to execute exec you have to use the following command:

truffle exec queries\queries.js --network ropsten

Invoking your smart contract from a webapp

You can check the NodeJS clients under the clients folder.
It is as easy as invoking the service from any NodeJS process - such as an API - to get your client going.

An important note is that you have to configure your caller and contract hashes on the configuration file.

A smart contract util too has also been implemented to allow instantiating the contract handler only once and easing all the contract management.

Network gas price

In order to calculate the gas price you use for your transactions you can use Eth gas station. The gas price will determine how quickly your transaction is mined by the nodes in the network - the more you pay, the faster it gets mined.

The NodeJS client provided within this example allocates the low, standard and high gas prices from the Eth gas station and uses the standard one in order to keep the gas usage optimal.