Setup DApp Environment

Introduction

Currently, we can reduce the development cost by setting up a smart contract environment on TRON's private chain, which does not consume the public chain resources. TRON's virtual machine is highly compatible with that of Ethereum, and most smart contracts written in Solidity can also run on TRON network. TRON-based smart contracts provide high TPS and free access to most scenarios for its users. This is undoubtedly a significant step for the entire smart contract community. This article introduces a basic method to deploy smart contracts and interact with them.

Prerequisites

TRON Network for Running the Contract

Deploying and using contracts requires consuming a certain amount of resources (memory, CPU, and storage etc.). Therefore, it is recommended that the developers tune and test their smart contracts on their private networks, and confirm the contract is available before deploying them on the TestNet or MainNet. Please see TRON-CLI for details on deploying a Full Node, or TRON Docker Quickstart for steps on deploying a private network.

The address of the private chain's block-creation node: TPL66VK2gCXNCD7EJg9pgJRfqcRazjhUZY

Witness Private Key: da146374a75310b9666e834ee4ad0866d6f4035967bfc76217c5a495fff9f0d0

TRON Box

TRON Box is a framework for testing, compiling, and deploying TRON smart contracts and DApps. The TRON Box Guide details setup and deployment steps.

Smart Contract Development

At this point, we recommend Remix as the coding environment for compiling and testing during the early stages. After the contract is finished, developers can copy the contract to SimpleWebCompiler for further development and then acquire ABI and ByteCode. We present a simple solidity code example of data access, to illustrate the compiling, deployment, and debugging process.

pragma solidity ^0.4.0;
    contract DataStore {
    
        mapping(uint256 => uint256) data;
    
        function set(uint256 key, uint256 value) public {
            data[key] = value;
        }
    
        function get(uint256 key) view public returns (uint256 value) {
            value = data[key];
        }
    }

1. Initiate the Private Chain

Ensure the private chain in the prerequisite has been successfully deployed by checking FullNode/logs/tron.log and see if the log message "produce block successfully" of persistent block generation appears.

2. Develop Smart Contracts

Copy the code mentioned above to Remix to compile and debug. Make sure the code logic is correct, and the code itself is bug-free.

3. Compile in SimpleWebCompiler

The TRON compiler is slightly different from that of Ethereum and is still integrating with Remix. Therefore, we are providing a temporary way of acquiring ABI and ByteCode instead of acquiring them directly from Remix. Copy the code above to SimpleWebCompiler and click the Compile button to attain ABI and ByteCode.

4. Deploy Smart Contract via TRON Box

Please refer to the TRON Box Smart Contract Deployment guide for more information.

5. Deploy via Wallet-CLI

Download Wallet-Cli, then compile the file.

# Clone Repo

git clone https://github.com/tronprotocol/wallet-cli
cd  wallet-cli

# Compile

./gradlew build
cd  build/libs

📘

Note

The default configuration of Wallet-CLI will connect to the local 127.0.0.1:50051 Full Node. If the developer needs to connect to a different node or port, it can be modified in the config.conf file.

Start Wallet-CLI

java -jar wallet-cli.jar

Once started, the instructions can be entered interactively in the command. Import the private key and check if the balance is correct.

Importwallet

<Enter your own set wallet password 2 times>
<Enter private key: 

da146374a75310b9666e834ee4ad0866d6f4035967bfc76217c5a495fff9f0d0>

Login

<Enter your own set of wallet passwords>

Getbalance

Contract Deployment

# Contract Deployment Instructions

DeployContract contract_name ABI byteCode constructor params isHex fee_limit consume_user_resource_percent origin_energy_limit value token_value token_id(e.g: TRXTOKEN, use # if don't provided) <library:address,library:address,...>

Parameter Description

ParameterDescription
contract_nameContract name set by yourself
ABIABI json data obtained from SimpleWebCompiler
BytecodeABI json data obtained from SimpleWebCompiler
ConstructorWhen the contract is deployed, the constructor is called. If you need to call it, fill in the argument type of the constructor, for example: constructor(uint256,string), if not, fill in a character#
ParamsConstructor arguments, separated by commas, for example 1, "test" , if there is no constructor, fill in a character#
isHexWhether the input parameter is converted to hexadecimal
fee_limitThe upper limit of the TRX consumed by this deployment contract, in units of SUN (1 SUN = 10^-6 TRX), including consumption of CPU resources, STORAGE resources, and available balances.
consume_user_resource_percentThe percentage of resources specified for users who use this contract is an integer between [0, 100]. If it is 0, it means the user does not consume resources. If the developer resources are exhausted, the user's resources will be fully used.
origin_energy_limitThe upper limit of the energy set by the developer that is consumed by the developer during a contract call must be greater than zero. For the old contract, the parameter that sets the value is not provided, it will be saved as 0, but it will be calculated according to the 10 million energy limit. The developer can reset the value through the updateEnergyLimit interface. When setting the new value, it must be greater than 0.
valueThe amount of the transfer to the contract when the contract is deployed.
token_valueTransfer the number of TRC10 tokens.
token_idTransfer the TokenID of TRC10 token.
libraryAddress,library:address,...: If the contract contains a library, you need to specify the library address during deployment of the contract. If there is no library, you don't need to fill it out.

Obtain Contract Address

Your smart contract address will be: <contract address>

// In this case
Your smart contract address will be: TTWq4vMEYB2yibAbPV7gQ4mrqTyX92fha6

Call contract to store data, query data

// Call contract instruction

TriggerContract contract_address method args isHex fee_limit value token_value token_id(e.g: TRXTOKEN, use # if don't provided)

// Parameter Description

contract_address: The address of the previously deployed contract, in the format base58, such as: TTWq4vMEYB2yibAbPV7gQ4mrqTyX92fha6
method: The function signature of the call, such as set(uint256,uint256) or fool(), the parameter is separated by ',' and there can be no spaces.
args: If it is not hexadecimal, the natural input uses ',' split and can't have spaces. If it is hexadecimal, just fill it in directly.
is_hex: Whether the input parameter is hexadecimal, false or true
fee_limit: Similar to the case of deploycontract, it represents the upper limit of TRX consumed by this deployment contract. The unit is SUN (1 SUN = 10^-6 TRX), including the consumption of CPU resources, STORAGE resources, and available balance.
value: The amount of the transfer to the contract when the contract is deployed
token_value: Transfer the number of TRC10 tokens.
token_id: Transfer the TokenID of TRC10 token.

// Called Example
Set Mapping 1 -> 1
triggercontract TTWq4vMEYB2yibAbPV7gQ4mrqTyX92fha6 set(uint256,uint256) 1,1 false 1000000 0 0 #

// Take the value of mapping key = 1
triggercontract TTWq4vMEYB2yibAbPV7gQ4mrqTyX92fha6 get(uint256) 1 false 1000000  0000000000000000000000000000000000000000000000000000000000000000

If the function called is constant or view, wallet-cli returns the result directly. If you include a library, you need to deploy the library before deploying the contract. After deploying the library, you know the library address and fill the address into library:address,library:address,....

For example, using remix to generate a contract, bytecode is

608060405234801561001057600080fd5b5061013f806100206000396000f300608060405260043610610041576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063f75dac5a14610046575b600080fd5b34801561005257600080fd5b5061005b610071565b6040518082815260200191505060405180910390f35b600073<b>__browser/oneLibrary.sol.Math3__________<\b>634f2be91f6040518163ffffffff167c010000000000000000000000000000000000000000000000000000000002815260040160206040518083038186803b1580156100d357600080fd5b505af41580156100e7573d6000803e3d6000fd5b505050506040513d60208110156100fd57600080fd5b81019080805190602001909291905050509050905600a165627a7a7230582052333e136f236d95e9d0b59c4490a39e25dd3a3dcdc16285820ee0a7508eb8690029  

Assume the previously deployed library address is: TSEJ29gnBkxQZR3oDdLdeQtQQykpVLSk54. Then, when deploying, you need to use browser/oneLibrary.sol.Math3:TSEJ29gnBkxQZR3oDdLdeQtQQykpVLSk54 as the parameter of deploycontract.