Truffle

Truffle

A world class development environment, testing framework and asset pipeline for blockchains using the Ethereum Virtual Machine (EVM), aiming to make life as a developer easier.

This page will take you through the basics of creating a Truffle project and deploying a smart contract to a blockchain.

Note: This page is just a quick start based on a sample program; please refer to Truffle's documentation for more information on Truffle installation.

Install Truffle

Requirements

Install Truffle

Warning: Avoid using the sudo command when installing Truffle, this can cause permission errors.

In a terminal, use NPM to install Truffle:

npm install -g truffle

You may receive a list of warnings during installation. To confirm that Truffle was installed correctly, run:

truffle version

Creating a Project

After ensuring Truffle is installed successfully, create your own project and name it something like "truffleApp".

Create a new directory for your Truffle project

mkdir truffleApp
cd truffleApp

Initialize your project

truffle init

Once this operation is complete, you’ll now have a project structure with the following items:

  1. contracts/: Directory for Solidity contracts

  2. migrations/: Directory for scriptable deployment files

  3. test/: Directory for test files for testing your application and contracts

  4. truffle-config.js: Truffle configuration file

  5. build (visible after compile): Compiled Solidity contracts with bytecode and ABI

Create Contract

Make sure you install HDWalletProvider which we will use later:

npm install @truffle/hdwallet-provider --save

we create a file for smart contract called HelloKura.sol inside the contracts directory:

pragma solidity ^0.8.0;

contract HelloKura {
   string public greet = "Hello Kura!";
}

Coding Migrations

To deploy our HelloKura contract on Kura Testnet, we have to create a migration to get the contract on the network. Create a file in the migrations folder named “1_deploy_contract.js”.

var HelloKura = artifacts.require("HelloKura");

module.exports = function (deployer) {
    deployer.deploy(HelloKura);
};

Configuring Truffle For Kura Testnet

  1. Go to truffle-config.js (located in root directory)

  2. Update with Kura Testnet details

require('dotenv').config();
const mnemonic = process.env.MNEMONIC;
const HDWalletProvider = require('@truffle/hdwallet-provider');

module.exports = {
  networks: {
    kura: {
      provider: () => new HDWalletProvider(mnemonic, `https://rpc-kura.cross.technology/`),
      network_id: 5555,
      confirmations: 10,
      timeoutBlocks: 200,
      skipDryRun: true,
      from: "0x(your kura testnet account address)"
    }
  },
  compilers: {
    solc: {
      version: "0.8.0"
    }
  }
};

Be aware that we need to declare mnemonic which is used by HDWalletProvider in the truffle-config.js file to verify the account supplying funds during contract deployment. To set mnemonic variable, you would set it as an environment variable in .env file in the root directory.

npm i dotenv
MNEMONIC="(Your secret recovery phase)"

We can find our secret recovery phase for our account in the Metamask by going through Settings, Security & Privacy, and then Reveal Secret Recovery Phrase.

Deployment

Finally, we have everything ready and we can compile the smart contract we made:

truffle compile
truffle migrate --network kura

We would see something like below to confirm our smart contract is deployed on Kura testnet.

We can confirm this also by looking at the explorer Subscan.

Reference

Last updated