HomeGuidesAPI ReferenceChangelog
GuidesAPI ReferenceCommunityDiscordBlogFAQBug BountyAnnouncementsChange Log
Guides

TronBox

TronBox is Tron's smart contract development, testing and deployment tool

Overview

Tronbox is a blockchain development tool that you can use to create and deploy smart contracts.

What will you learn?

Through this article, you will learn how to use Tronbox to create smart contracts and deploy them to the TRON network.

What will you do?

  • Install and configure Tronbox
  • Deploy the contract on the TRON network
  • Check contract deployment status on Tronscan

Setup the development environment

Before starting, please install the following software:

Once NodeJS has already installed, we just need one command to install Tronbox:

$ npm install -g tronbox

Type Tronbox version on the terminal to check if Tronbox is installed correctly.

Create a project

MetaCoin project

We will take the MetaCoin project as an example to introduce the related operations of Tronbox.

  1. Create a new directory for the MetaCoin project:

    $ mkdir MetaCoin
    $ cd MetaCoin
    
  2. Download the MetaCoin project:

    $ tronbox unbox metacoin
    

Now you have already created a TronBox project with contracts, test cases and configuration files. Of course you can also use the tronbox init command to initialize a new TronBox project. Several key files and folders in the TronBox project include:

File/FolderDescription
./contractThe directory storing all smart contract files.
./migrationsThe directory storing all javascript files for migration.
./testThe directory storing all test scripts for testing the smart contract.
./tronbox.jsThe configuration file of the project. Declare your Full Node address and Event Server in this file.

Here is the smart contract code in /contract/MetaCoin.sol of this example project:

pragma solidity ^0.5.4;

import "./ConvertLib.sol";

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

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

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

  address owner;

  constructor(uint initialBalance) public {
    owner = msg.sender;
    balances[msg.sender] = initialBalance;
  }

  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];
  }

  function getOwner() public view returns (address) {
    return owner;
  }
}

Configure compilation and deployment parameters

Before deploying the contract, you need to configure the private key, network, compiler version, userFeePercentage and feeLimit and other compilation and deployment parameters.

module.exports = {
  networks: {
    mainnet: {
      // Don't put your private key here:
      privateKey: process.env.PRIVATE_KEY_MAINNET,
      /*
Create a .env file (it must be gitignored) containing something like
  export PRIVATE_KEY_MAINNET=4E7Fxxxxxxxxx
Then, run the migration with:
  source .env && tronbox migrate --network mainnet

*/
      userFeePercentage: 100,
      feeLimit: 1000 * 1e6,
      fullHost: 'https://api.trongrid.io',
      network_id: '1'
    },
    shasta: {
      privateKey: process.env.PRIVATE_KEY_SHASTA,
      userFeePercentage: 50,
      feeLimit: 1000 * 1e6,
      fullHost: 'https://api.shasta.trongrid.io',
      network_id: '2'
    },
    nile: {
      privateKey: process.env.PRIVATE_KEY_NILE,
      userFeePercentage: 100,
      feeLimit: 1000 * 1e6,
      fullHost: 'https://api.nileex.io',
      network_id: '3'
    },
    compilers: {
      solc: {
        version: '0.6.0' // for compiler version
      }
    }
  },  
  // solc compiler optimize
  solc: {
    optimizer: {
      enabled: false, // default: false, true: enable solc optimize
      runs: 200
    },
    evmVersion: 'istanbul'
  }
}

Please note that tronbox.js can be configured with multiple network information. When deploying, you can select the corresponding network according to the network name.. You also need to pass the private key to Tronbox, which is the private key of the account which will deploy the contract. It is recommended to set the private key in the environment variable, and then configure the environment variable to privateKey to ensure your account security.

Compile the contract

Compile the smart contract with the following command:

$ tronbox compile

You will see the following output:

The compiled file will be located in the ./build/contracts directory.

Deploy contract

Use the following command to deploy the contract. The --network parameter can be used to specify the network to be deployed on. The network must be a network name in the networks list in tronbox.js. The following is an example of deploying contract to the shasta network:

$ tronbox migrate --network shasta 

Congratulations! You have successfully deployed the MetaCoin smart contract using Tronbox. Now you can check the deployment status in Shasta testnet browser according to the contract address, and interact with the deployed contract.