See all articles
Deploying Ethereum’s Smart Contracts

Deploying Ethereum’s Smart Contracts

Get smart. In this article, we continue our series about Smart Contracts that build on Ethereum’s blockchain backbone, with a focus on deploying and verifying the Smart Contract we wrote previously.

Smart Contracts are the new way to verify *anything* before you (or your customers or even other smart contracts) make a transaction. It’s just one way that businesses have been using Ethereum in commercial projects - and one of the most useful.

The Ethereum Smart Contracts that we talk about today live on the Ethereum blockchain in an Ethereum-specific binary format (Ethereum Virtual Machine or EVM bytecode), although there are other options in the blockchain cosmos as it starts to bubble over. In order to build trust between your Ethereum-based Smart Contract and its users it’s good idea to verify it (meaning all source code will be available to read). This can be done on the Etherscan or EthTools websites that display transactions, token transfers, and source code for each Smart Contract. You can also search contract, transaction, etc. by its hash.

However before the contract can be verified in needs to be placed on the EVM first. Following on from our second article, developing a sound Smart Contract, let’s deploy it to the EVM and then verify it. Sound good? Let’s go!

Deploying a Smart Contract

Before deploying to the Ethereum network you need to have an Ethereum client installed and configured. The client needs to sync the Ethereum blockchain, which can be both time and storage consuming - how much have you got? To deploy a Smart Contract as soon as possible we will use Infura - a 3rd party service that us allows to run applications on an EVM without setting up the client locally.

Let’s start by visiting the Infura website to create a new account. Since Infura does not manage your private keys for security reasons, we will need the truffle-hdwallet-provider library, to sign transactions.

Before adding the provider, let’s clean up our smart contract code [Link] a little:

1. First, remove all contracts from our `contracts/` directory except `CustomerSupport.sol` and `Migrations.sol`.

2. Remove tests for previously removed contracts from the `test/` directory

3. Update `migrations/2_deploy_contracts.js` to exclude previously removed contracts - it should look like the example below:

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

Now let’s install `truffle-hdwallet-provider`:

npm install -D truffle-hdwallet-provider

We will use the Ropsten test network. The same steps apply for the main network, but with `Ropsten` you can test the whole process without spending any ETH (yet!).

We will use dotenv for storing mnemonics and credentials to `Infura` in our application. Let’s install the library first:

npm install -D dotenv

and then modify `truffle.js` file to include `truffle-hdwallet-provider`:

// trufle.js
require('dotenv').config()
var HDWalletProvider = require("truffle-hdwallet-provider");
var mnemonic = process.env.ETH_MNEMONIC;
var accessToken = process.env.INFURA_ACCESS_TOKEN;
module.exports = {
  networks: {
    development: {
      host: "localhost",
      port: 8545,
      network_id: "*" // Match any network id
    },
    ropsten: {
      provider: HDWalletProvider(mnemonic, 'https://ropsten.infura.io/' + accessToken, 0),
      network_id: 3,
      gas: 4712388
    }
  }
};

The last argument given to the `HDWalletProvider` function (`0` in out example here) is the account number that should be used - in this case it’s the first account (here where accounts are indexed starting from 0).

We also provide a Gas limit for deploying the contract so we don’t accidentally blow out our budget with a bug - `4712388` is the default value for truffle.

Now let’s go to https://ethtools.com/ropsten/ and create a new wallet on the `Ropsten` network. Then, sign in (this is important, as requesting Ether does not work for anonymous users) and visit https://ethtools.com/ropsten/tools/faucet/ to request some ETH so you can perform deployment. Remember, of course, that on the main network you would need to transfer real ETH though!

Now you can create a `.env` file in the root of the project with your mnemonic and Infura access token:

ETH_MNEMONIC='YOUR_MNEMONIC'
INFURA_ACCESS_TOKEN='YOUR_INFURA_TOKEN_TO_ROPSTEN'

Now we are ready to compile the project and deploy to the `Ropsten` network:

truffle compile
truffle migrate --reset --network ropsten

You should see an output similar to the one below:

$ truffle migrate --reset --network ropsten
Using network 'ropsten'.
Running migration: 1_initial_migration.js
  Deploying Migrations...
  ... 0x4f41e26b2356e7eb2e68349017c69749af65b143d6cf65163e17fc30cae168ef
  Migrations: 0xc76f9c3231fa43dbc5601cf1d3fb9d457b05e3e2
Saving successful migration to network...
  ... 0xe17dbe7a52e869545386435d347cca776a9fadf013d0e15636594a3684695351
Saving artifacts...
Running migration: 2_deploy_contracts.js
  Deploying CustomerSupport...
  ... 0xe7930c1d96e05d40b9758aa5560f798861fa62c9a23a4f52f8edcc445fa1978e
  CustomerSupport: 0x67aedb5967b4446f708d72316d51724ab88d463e
Saving successful migration to network...
  ... 0x35fa15d0414f57e6feb0f56b0b412dc2ac0e46430e22f626e2fa867bf43575ed
Saving artifacts...

Now if you go to the ETH Tools website and paste the `CustomerSupport` address (`0xe7930c1d96e05d40b9758aa5560f798861fa62c9a23a4f52f8edcc445fa1978e` in our case) into the search input you will see your newly deployed contract:

If you switch to the `Source code` tab you will see:

> We do not have the source code for this contract. If you are the contract author, consider verifying your contract such that other users can verify that your contract does what they expect it to.

Let’s change that and get verified!

Verifying a Smart Contract

We will use the Etherscan page for verifying our smart contract - since we came across a problem when attempting to verify with the ETH tools page on the Ropsten test network. For the main network, the ETH tools page works perfectly for verification.

Let’s go to the Etherscan website and search our contract by its address and then click on `the` `Contract code` tab:

Now click `Verify and publish` link and fill all fields:

To get our Solidity compiler version:

1. Check where truffle is installed (`which truffle`)

2. In the same directory, there should be the `solcjs` binary

3. Run `solcjs` `--``version`

Once you are done, click on the `Verify and Publish` button.

Now your contract should be correctly verified:

If you go to the contract page again, you should be able to read the source code:

You will also see a new tab for reading the current state of the contract:

Unfortunately, any contract verified on `Etherscan` is not automatically verified on `EthTools`, but for the purposes of this tutorial `Etherscan` is enough.

Interacting with your Smart Contract

Since our contract is deployed and verified, let’s try to interact with it. Let’s add some people so we can rotate them.

Let’s launch the `truffle console` on the `ropsten` network:

truffle console --network ropsten

Then add some people to the pool:

truffle(ropsten)> var contract;
truffle(ropsten)> CustomerSupport.deployed().then(function(instance) { contract = instance; });
truffle(ropsten)> var people = ['Alice', 'Bob'].map(function(p) { return web3.toHex(p); });
truffle(ropsten)> contract.addPeople(people);

and check the current person:

truffle(ropsten)> var person;
truffle(ropsten)> contract.getPerson().then(function(result) { person = result; });
truffle(ropsten)> person
'0x416c696365000000000000000000000000000000000000000000000000000000'
truffle(ropsten)> web3.toUtf8(person);
'Alice'

If you refresh the `Read Smart Contract` tab on the Etherscan you will see the same result:

Let’s try to switch the person a couple of times to see if our contract is working correctly:

truffle(ropsten)> contract.switchPerson();
truffle(ropsten)> contract.getPerson().then(function(result) { person = result; });
truffle(ropsten)> person
'0x426f620000000000000000000000000000000000000000000000000000000000'
truffle(ropsten)> web3.toUtf8(person);
'Bob'
truffle(ropsten)> contract.switchPerson();
truffle(ropsten)> contract.switchPerson();
truffle(ropsten)> contract.getPerson().then(function(result) { person = result; });
truffle(ropsten)> web3.toUtf8(person);
'Bob'

As you can see, it correctly rotates people through the customer support role. Those changes are reflected on `Etherscan`, as well as the `EthTools` pages:

Ethereum is a powerful technology for running Smart Contracts - which may be arbitrary programs and not involving cryptocurrency at all, save for the fees required to run them over the Ethereum network.

Interested in developing Smart Contracts for your business? It’s a wise and forward-thinking move. Let iRonin take the lead in Smart Contracts development and other back-end development services: developing safe and secure Smart Contracts for your business dealings. Cut out the middle man and save money on financial fees. Ask us about how we can help!

Read Similar Articles