Skip to main content

Deploy using Truffle

Overview

Truffle is a blockchain development environment, which you can use to create and test smart contracts by levering an Ethereum Virtual Machine.

What you will learn

This guide aims at teaching how to create a smart contract using Truffle and deploying it on ESC Testnet.

What you will do

  • Install and set up Truffle
  • Deploy contract on ESC Testnet
  • Check the deployment status on the ESC Testnet Block Explorer.

Setting up the development environment

There are a few technical requirements before we start. Please install the following:

Once we have those installed, we only need one command to install Truffle:

npm install -g truffle

To verify that Truffle is installed properly, type truffle version on a terminal. If you see an error, make sure that your npm modules are added to your path.

note

What follows is an adapted version of the Truffle quickstart guide article.

Creating a project

MetaCoin project

We will use one of Truffle's boilerplates which you can find on their Truffle Boxes page. MetaCoin box creates a token that can be transferred between accounts.

  1. Start by creating a new directory for this Truffle project:
mkdir MetaCoin
cd MetaCoin
  1. Download the MetaCoin box:
truffle unbox metacoin

With that last step, you have created a Truffle project cointaining folders with contracts, deployment, testing and configuration files.

This is the smart contract data from the metacoin.sol file:

// SPDX-License-Identifier: MIT
// Tells the Solidity compiler to compile only from v0.8.13 to v0.9.0
pragma solidity ^0.8.13;

import "./ConvertLib.sol";

// This is just a simple example of a coin-like contract.
// It is not ERC20 compatible and cannot be expected to talk to other
// coin/token contracts.

contract MetaCoin {
mapping (address => uint) balances;

event Transfer(address indexed _from, address indexed _to, uint256 _value);

constructor() {
balances[tx.origin] = 10000;
}

function sendCoin(address receiver, uint amount) public returns(bool sufficient) {
if (balances[msg.sender] < amount) return false;
balances[msg.sender] -= amount;
balances[receiver] += amount;
emit Transfer(msg.sender, receiver, amount);
return true;
}

function getBalanceInEth(address addr) public view returns(uint){
return ConvertLib.convert(getBalance(addr),2);
}

function getBalance(address addr) public view returns(uint) {
return balances[addr];
}
}
note

Notice that ConvertLib is being imported just after the pragma statement. In this project, there are actually two smart contracts that will be deployed at the end: one is Metacoin, contatining all the send and balance logic; the other is ConvertLib, a library used to convert values.

Testing the contract

You can run a Solidity and Javascript tests.

  1. In a terminal, run the Solidity test:
truffle test ./test/TestMetaCoin.sol

You should see the following output:

  1. Run the JavaScript test:
truffle test ./test/metacoin.js

You should see the following output:

Compiling the contract

Compile the smart contract:

truffle compile

You will see the following output:

Configuring the smart contract

Before actually depolying the contract, you need to set up the truffle-config.js file, inserting network and compilers data.

  • Go to truffle-config.js
  • Update the truffle-config with elastos-network-crendentials.
const HDWalletProvider = require("@truffle/hdwallet-provider");
const fs = require("fs");
const mnemonic = fs.readFileSync(".secret").toString().trim();

module.exports = {
networks: {
development: {
host: "127.0.0.1", // Localhost (default: none)
port: 8545, // Standard Ethereum port (default: none)
network_id: "*", // Any network (default: none)
},
elastos: {
provider: () =>
new HDWalletProvider(mnemonic, `https://esc-testnet.elastos.io/api`),
network_id: 80001,
confirmations: 2,
timeoutBlocks: 200,
skipDryRun: true,
},
},

// Set default mocha options here, use special reporters etc.
mocha: {
// timeout: 100000
},

// Configure your compilers
compilers: {
solc: {
version: "0.6.12",
},
},
};

Notice, it requires mnemonic to be passed in for elastosProvider, this is the seed phrase for the account you'd like to deploy from. Create a new .secret file in the root directory and enter your 12-word mnemonic seed phrase to get started. To get the seed words from Metamask wallet, you can go to Metamask settings, then from the menu, choose Security and Privacy where you will see a button that says "reveal seed words".

Deploying on Elastos network

Add ELA to your wallet using https://esc-faucet.elastos.io/

Run this command in the root of the the project directory:

note

Remember your address, transaction_hash and other details provided would differ. Above is just to provide an idea of the structure.

Congratulations! You have successfully deployed a Smart Contract using Truffle. Now you can interact with it check its deployment status here: https://esc-testnet.elastos.io/.