This section covers how to create and deploy Smart Contracts using Truffle.

You can install Truffle using npm, the Node package manager. Please refer to the following link for instructions on installing npm.

$ npm install --g truffle

If Truffle is intalled, use 'truffle init' command to initialize.

$ truffle init

Starting init...

> Copying project files to /Users/user/Projects/Solidity/truffle-test

Init successful, sweet!

Try our scaffold commands to get started:
  $ truffle create contract YourContractName # scaffold a contract
  $ truffle create test YourTestName         # scaffold a test

$ tree
├── contracts
├── migrations
├── test
└── truffle-config.js

4 directories, 1 file

If you execute 'truffle init', 'Truffle project' is created. The generated Truffle project consists of build/contracts, contracts, migrations, tests, and truffle-config.js.

  • build/contracts : save the result (abi, bitecode) of the compiled contract written in solidity

  • contract : smart contract solidity files

  • migrations : deploy code(js format)

  • test : test code(js format)

  • truffle-config.js : solidity compiler version setting, network setting and others` configuration

Use the 'truffle create contract' command to create a simple smart contract template.

/ SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;

contract Greeter {
  constructor() public {

Apply the 'Greater.sol' that you created earlier on this template.

// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;

contract Greeter {
    string private greeting;

    constructor(string memory _greeting) public {
        greeting = _greeting;

    function greet() public view returns (string memory) {
        return greeting;

    function setGreeting(string memory _greeting) public {
        greeting = _greeting;

To compile and deploy the 'Greater.sol' smart contract, you have to change the 'Truffle-config.js' file.

You need to add HDWalletProvider and privKeys in the 'Truffle-config.js' file to set up the account that you use for deployment. Private keys can be imported after you create an account through the Web Wallet or Metamask.

const HDWalletProvider = require('@truffle/hdwallet-provoder');
const privKeys = ["privatekey from Web Wallet"];

To use the WEMIX3.0 Testnet, add the network settings as show below.

networks: {
  wemix_testnet: {
    provider: ()=> new HDWalletProvider(privKeys, ""),
    network_id: 1112,
    maxFeePerGas: 100000000010,
    maxPriorityFeePerGas: 100000000000

Set the version of the Solidity Compiler you want to use. The current latest version is 0.8.x, and the exmaple uses version 0.8.11. If it is installed directly on the operating system that uses the complier, you can annotate the docker settings in the same way as the initial settings. To install it by using docker, you need to set 'docker:true' as show below and docker must be installed. If you use the optimizer, you can turn off the annotation and set the appropriate settings.

compilers: {
    solc: {
      version: "0.8.11",    // Fetch exact version from solc-bin (default: truffle's version)
      docker: true,         // Use "0.5.1" you've installed locally with docker (default: false)
      // settings: {          // See the solidity docs for advice about optimization and evmVersion
      //  optimizer: {
      //    enabled: false,
      //    runs: 200
      //  },
      //  evmVersion: "byzantium"

After completing the configuration of the truffle-config.js file, you can execute a smart contract compilation. Running the truffle compile <file name> in the truffle project folder will compile the solidity file you specified as a target. The complied solidity contract are stored in the build/contracts folder in the format, <contract name>.json.

truffle compile <file name>
ex) truffle compile Greeter.sol

To deploy the compiled smart contract on the blockchain, you need to write the script shown below and save it as 1_deploy_contracts.js.

var Greeter = artifacts.require("./Greeter.sol");

module.exports = function(deployer) {
  deployer.deploy(Greeter, "Hello!")
    .then(() => Greeter.deployed());

You can deploy smart contracts individually by loading the smart contracts that you want to deploy with artifacts.require (<artifacts file name stored in build/contacts>), and executing deployer.deploy(contract_artifacts,{constructor arguments}).

truffle migrate <file name>
ex) truffle migrate 1_deploy_contracts.js

The deployed script can be executed with the truffle migrate <file name> above. If you don`t use <file name>, it is performed in the numerical order used in the script name.

The result of the deployment in the WEMIX3.0 Testnet is as follows. For deployment, you must have WEMIX in the account that you want to use for deployment. You can get WEMIX you need from Testnet Faucet.

❯ truffle migrate --reset --network wemix_testnet

Compiling your contracts...
> Everything is up to date, there is nothing to compile.

Starting migrations...
> Network name:    'wemix_testnet'
> Network id:      1112
> Block gas limit: 105000000 (0x6422c40)


   Deploying 'Greeter'
   > transaction hash:    0xf180dc9041ca140547b62f1a600e2ab920cfe4776d4a5590511df3051d1d09b7
   > Blocks: 0            Seconds: 0
   > contract address:    0x450F45A74b6dC7b38E25Ca84F91C6a909B2e5728
   > block number:        21511136
   > block timestamp:     1679563304
   > account:             0x1134397c2E4aE380B7D4aa446c59D07977fCeB87
   > balance:             99.933323083999672507
   > gas used:            327493 (0x4ff45)
   > gas price:           100.000000001 gwei
   > value sent:          0 ETH
   > total cost:          0.032749300000327493 ETH

   > Saving artifacts
   > Total cost:     0.032749300000327493 ETH

> Total deployments:   1
> Final cost:          0.032749300000327493 ETH

You can test the deployed smart contract using the javascript file as follows.

const migration = artifacts.require("Migrations");

contract("Migration", async () => {
    describe("Migration test", async () => {
        it("setCompleted", async () => {
            const migrations = await migration.deployed();
            await migrations.setCompleted(0);
  • contract : test target contract

  • describe : test case collection

  • it : each test case

truffle test <test file name>

When the test code is complete, save the test file as <testName.test.js> format in the test folder and execute the truffle test <testName.test.js> to perform the test.

Last updated