Development
EVM Smart Contracts
Truffle: Deploy Smart Contract

Truffle: Deploy EVM Smart Contract

Truffle is a development environment, testing framework, and asset pipeline for Ethereum, aiming to make life as an Ethereum developer easier. This environment specializes in smart contract development and features a number of great functionalities that help dApp developers tremendously.

Setting up the development environment

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

# install Truffle
npm install -g truffle
 
# install hdwallet-provider package
npm install @truffle/hdwallet-provider --save

Create Truffle Project

Let’s create the sample project and go through these steps to try out compiling and deploying the sample contract.

# install Truffle
cd ProjectFolder
 
# Clone sample project from Truffle called MetaCoin
truffle unbox metacoin
 
# Remove default smart contracts
rm -rf MetaCoin.sol
rm -rf ConvertLib.sol

Configure network options in truffle.config.js

const HDWalletProvider = require('@truffle/hdwallet-provider');
 
const privateKey = "PRIVATE_KEY_DEPLOYER" // Please enter private key of deployer here
module.exports = {
  networks: {
    aioztestnet: {
      provider: () => new HDWalletProvider(privateKey, "https://eth-ds.testnet.aioz.network"),
      network_id: 4102,
      confirmations: 1,
      timeoutBlocks: 200,
      skipDryRun: true
    },
  },
  compilers: {
    solc: {
      version: "^0.8.0"
    }
  }
};
 

You need to configure Network RPC and owner account private key to deploy smart contract.

Write a Smart Contract

Create a new smart contract HelloAIOZ.sol in folder /contracts with below source code:

// SPDX-License-Identifier: Unlicense
// Specifies that the source code is for a version of Solidity greater than 0.6.8
pragma solidity ^0.8.0;
 
contract HelloAIOZ {
 
    // The public variables, which can be accessible from outside a contract
    string public status;
 
    constructor(string memory initStatus) {
        // setting `status` to initial value
        status = initStatus;
    }
 
    // A publicly accessible function to updates `status`
    function update(string memory newStatus) public {
        status = newStatus;
    }
}

This is the most basic smart contract on Solidity. The smart contract stores the status message and lets everyone on the blockchain read/update status.

Compile Smart Contract

This command will compile all smart contracts in /contracts folder.

truffle compile

Deploying on AIOZ Network Testnet

Modify file 2_deploy_contracts.js in migrations folder, source code as below:

const HelloAIOZ = artifacts.require("./HelloAIOZ");
 
module.exports = function(deployer) {
  deployer.deploy(HelloAIOZ, "testavalue your initial value here");
};

Run this command in root of the project directory:

truffle migrate --network aioztestnet  

Contract will be deployed successfully on AIOZ Network Testnet, it will display output as follow:

Compiling your contracts...
===========================
 Fetching solc version list from solc-bin. Attempt #1
 Fetching solc version list from solc-bin. Attempt #1
> Everything is up to date, there is nothing to compile.
 
 
Starting migrations...
======================
> Network name:    'aioztestnet'
> Network id:      4102
> Block gas limit: 4294967295 (0xffffffff)
 
 
2_deploy_contracts.js
=====================
 
   Deploying 'HelloAIOZ'
   ---------------------
   > transaction hash:    0x4674cc7884aab1d8ccda4b54226156dd718576c1bd68c0c12a5b8bccb8766e18
   > Blocks: 1            Seconds: 8
   > contract address:    0x0ECEbc0Fb7A080878AB35aa31b8E131135C92E12
   > block number:        2195825
   > block timestamp:     1655956698
   > account:             0x2993D403b9FBFCec3d2dB34a9BFCF11FFaEe062d
   > balance:             0.0976104575
   > gas used:            455463 (0x6f327)
   > gas price:           2.5 gwei
   > value sent:          0 ETH
   > total cost:          0.0011386575 ETH
 
   Pausing for 2 confirmations...
 
   -------------------------------
   > confirmation number: 1 (block: 2195826)
   > confirmation number: 2 (block: 2195827)
   > Saving migration to chain.
   > Saving artifacts
   -------------------------------------
   > Total cost:        0.0011386575 ETH
 
Summary
=======
> Total deployments:   1
> Final cost:          0.0011386575 ETH

You can check the deployment status here: https://testnet.explorer.aioz.network/ (opens in a new tab)