TRON Developer Hub

The TRON Developer Hub

Welcome to the TRON developer hub. You'll find comprehensive guides and documentation to help you start working with TRON as quickly as possible, as well as support if you get stuck. Let's jump right in!

Get Started    

Quickstart with TRON Docker

Introduction

This guide walks the user through the TRON Docker quick-start image setup. The image provides the user with a private network that comes with a Full Node, a Solidity Node, an Event Server, and by default, 10 TRX accounts replete with 10,000 test TRX in each account. Corresponding private keys are provided with the accounts. Thus, the purpose of the Docker quickstart is to quickly provide the user with a local test environment through which they can interact and learn about the TRON blockchain.

Dependencies

Docker
Please refer to the Docker official website to download and install the latest Docker version:

Docker Installation for Mac
Docker Installation for Windows

Node.JS Console
This will be used to interact with the Full and Solidity Nodes via Tron-Web.

Node.JS Console Download

Clone TRON Docker Quickstart

git clone https://github.com/tronprotocol/docker-tron-quickstart.git

Setup TRON Docker

What is TRON Docker?

Tron Docker Structure

Tron Docker Structure

The TRON Docker Quickstart creates a private network for TRON developers by using an image that exposes a Full Node, Solidity Node, and Event Server. The Full Node stores all transaction data and synchronizes block data with the Solidity Node. MongoDB stores event log data once you trigger an event in a smart contract, and then provides data for the event server to query. Note: the Docker image only sets up a private test-net on your local device. This means it cannot connect to either the Shasta Testnet nor the TRON Mainnet. To connect to the Mainnet or Shasta Testnet, please visit the guide for setting up a Full Node.

Setup

First, check for the latest version of TRON Docker Quickstart that should be referenced in the docker run command. When you execute the docker run command, be sure to run the latest version by specifying the corresponding tag name number from the Docker Hub page. In the below figure, the latest Docker release is 1.1.3.

The next step is to execute the docker run command. The docker run command exposes five ports: Ports 8090 and 8091 are for Full Node and Solidity Node HTTP. Ports 50051 and 50052 are for Full Node and Solidity Node gRPC. Port 8092 is for the event server. At the tail end of the command syntax, please reference the latest version number you see from the Docker Hub page.

docker run -it -p 8091:8091 -p 8092:8092 -p 8090:8090 -p 50051:50051 -p 50052:50052 --rm --name tron trontools/quickstart:1.1.3

Output

An abridged version of a successful output is shown below:

\n\nTron Quickstart v1.1.3\n
Run mongodb
Wait 3 seconds and set the events db
MongoDB shell version v4.0.3
connecting to: mongodb://127.0.0.1:27017
...

 Full Node listening on http://127.0.0.1:8090
 Solidity Node listening on http://127.0.0.1:8091
 Event Server listening on http://127.0.0.1:8092

...

Available Accounts
==================

(0) TR5TeHSD.....nJNoGvzBk (10000 TRX)
(1) TU2Asg4P.....SiAVMMSZi (10000 TRX)
(2) TPzKFpvJ.....EJ6pHkJB22 (10000 TRX)
(3) TXJJfXAG.....xdxJQtqqbT (10000 TRX)
(4) TRjepAMK.....wdsXi2jmeD (10000 TRX)
... 
# 10 Addresses Shown

Private Keys
==================

(0) F3CEA492.....7D455C89E
(1) D0FE73A0.....F27BC5AEA
(2) 14943F10.....010A40F1F
(3) D3226B71.....C8DA0023E
(4) D280195E.....25AE1F8BD
...
# 10 Corresponding Private Keys Shown

HD Wallet
==================
Mnemonic:    # 12 word Mnemonic listed here
Base HD Path:  m/44'/60'/0'/0/{account_index}

  GET /admin/accounts-generation 200  - 10828.964 ms

To verify the Full Node, Solidity Node, Event Server, and MongoDB running status, please open another Terminal window and run the following commands.

docker exec -it tron tail -f /tron/FullNode/logs/tron.log # check full node log
docker exec -it tron tail -f /tron/SolidityNode/logs/tron.log# check solidity node log
docker exec -it tron mongo

Go back to review if you have any question

To verify the image is running correctly, execute the command docker exec -it tron ps aux. The output should be similar to below:

docker exec -it tron ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.1  20044  3456 pts/0    Ss+  22:30   0:00 bash ./start.sh
root        44  0.4  3.7 1102748 75416 pts/0   SLl+ 22:30   0:04 mongod --auth
root        76  0.0  0.0  20044   260 pts/0    S+   22:30   0:00 bash ./start.sh
root        78  0.9 18.0 3015448 365272 pts/0  Sl+  22:30   0:10 java -jar Event
root        79  0.0  0.0  20044   260 pts/0    S+   22:30   0:00 bash ./start.sh
root        81  0.0  0.0  20044   260 pts/0    S+   22:30   0:00 bash ./start.sh
root        83  1.9 13.3 3074716 270280 pts/0  Sl+  22:30   0:20 java -jar FullN
root        84  1.3 17.0 3134072 346592 pts/0  Sl+  22:30   0:13 java -jar Solid
root       123  0.0  3.1 940280 64252 pts/0    Sl+  22:30   0:00 node /tron/app
root       278  0.0  0.1  36068  3304 pts/1    Rs+  22:48   0:00 ps aux

Query Account Information

If you close the terminal or just want to display the account and private key lists again, run the following API call:

curl http://127.0.0.1:8090/admin/accounts

Tron-Studio Smart Contract

Tron-Studio is an IDE for developing, deploying, and debugging smart contracts based on TVM. The gRPC port works with the Tron Docker Full Node to register accounts, deploy smart contracts, and trigger smart contracts.

Setup

Please ensure you have the JDK 1.8 environment installed on your system. Clone the Tron-Studio repository, then navigate into the tron-studio directory.

git clone https://github.com/tronprotocol/tron-studio

Run the gradlew build:

cd ~/tron/tron-studio
./gradlew build -x test -x check  # build project

Starting a Gradle Daemon (subsequent builds will be faster)
Download http://mvnrepository.com/com/github/jengelman/gradle/plugins/shadow/2.0.2/shadow-2.0.2.pom
Download http://mvnrepository.com/org/sonarsource/scanner/gradle/sonarqube-gradle-plugin/2.6/sonarqube-gradle-plugin-2.6.pom
Download http://mvnrepository.com/com/google/protobuf/protobuf-gradle-plugin/0.8.3/protobuf-gradle-plugin-0.8.3.pom
Download http://mvnrepository.com/commons-lang/commons-lang/2.6/commons-lang-2.6.pom
Download http://mvnrepository.com/com/google/guava/guava/18.0/guava-18.0.pom
..........
..........
BUILD SUCCESSFUL in 56s
13 actionable tasks: 13 executed

Stay in the same directory and execute the jar file to start Tron-Studio:

java -jar build/libs/TronStudio.jar

The Tron-Studio IDE will launch like below:

Tron-Studio

Tron-Studio

Run a Smart Contract

Copy the following smart contract into the Tron-Studio editor.

pragma solidity ^0.4.23;

contract HelloWorld {
  // Define variable message of type string
  string message;
  event test(uint res);
  
  // Write function to change the value of variable message
  function postMessage(string value) public returns (string) {
    message = value;
    emit test(8);
    return message;
  }
  
  // Read function to fetch variable message
  function getMessage() public view returns (string){
    return message;
  }
}

Click the Compile button. A successful compile brings up a pop-up on the lower right hand corner letting you know it was successful.

Tron Studio Compile Panel

Tron Studio Compile Panel

Click on the settings button on the IDE upper right-hand corner. This brings up the Existing Settings window.

Tron-Studio Settings

Tron-Studio Settings

Adjust the Local TVM address port to reflect the Tron Docker gRPC Full Node port, which is 50051. Remember, Tron-Studio interacts with the outside environment through gRPC ports only.

Click OK to save the settings. Then click the refresh button to ensure the gRPC port is selected on the Local TVM. Now click on the Deploy button to deploy an instance of the of the contract.

Tron Studio Run Panel

Tron Studio Run Panel

Upon deploying the contract onto the local test-net, verify the deployment success by expanding the details of the transaction in the center bottom window. A value of success for the result category indicates a successful deployment. Now we can test the deployed contract. Expand the contract in the Deploy Contacts panel. You will see the two functions of postMessage and getMessage. Input a string value "1000 Trx" and click the postMessage button to trigger the state changing function. Then query the change by clicking the getMessage button. This displays the "1000 Trx" message in the console panel.

Tron-Web Interaction

Introduction

Thus far, a smart contract has been deployed on your local private testnet. This section walks the user through interacting with the Full Node, Solidity Node, and deployed smart contract via an API library called Tron-Web. The purpose of this guide is to give the user a feel for interacting with the eco-system.

Node.JS Console

Inside the app folder of the docker-tron-quickstart folder, there is a JavaScript file called tronWeb.js. The default addresses for Full Node, Solidity Node, and Event Server point to the local Docker Quickstart Nodes' HTTP ports.

const TronWeb = require('tronweb')

let fullNode = 'http://127.0.0.1:8090'
let solidityNode = 'http://127.0.0.1:8091'
let eventServer = 'http://127.0.0.1:8092'
let defaultPrivateKey = 'da146374a75310b9666e834ee4ad0866d6f4035967bfc76217c5a495fff9f0d0'

global.tronWeb = new TronWeb(
  fullNode,
  solidityNode,
  eventServer,
  defaultPrivateKey
)

require('repl').start({})

In a new Terminal, navigate into the docker-tron-quickstart directory. Then execute node app/tronWeb. This enters the Node.JS console.

cd docker-tron-quickstart
node app/tronWeb

Query API Call

We will run a simple API call to query our smart contract transaction information. This uses the tronWeb.trx.getTransactionInfo API call. In Tron-Studio, expanding the deployment details of the original smart contract shows the transaction_id:

Executing the API call with the transaction_id as the argument outputs the following JSON data:

JSON output in the TronQuickstart Terminal

JSON output in the TronQuickstart Terminal

Executing the API call in Terminal 1

Executing the API call in Terminal 1

State Change API Call

We will now make an API call to the postMessage function, record a new message on your private Docker blockchain, and then output that message. Create a Javascript program that has the following code. In the code, ensure your local TVM URLs and ports are correct, and substitute your smart contract address into the contractAddress field. You may also amend the postMessage string input if you wish:

const TronWeb = require('tronweb');
const HttpProvider = TronWeb.providers.HttpProvider;
const fullNode = new HttpProvider('http://127.0.0.1:8090');    //Your local TVM URL  
const solidityNode = new HttpProvider('http://127.0.0.1:8091');
const eventServer = 'http://127.0.0.1:8092';
const privateKey = 'da146374a75310b9666e834ee4ad0866d6f4035967bfc76217c5a495fff9f0d0';

const tronWeb = new TronWeb(
    fullNode,
    solidityNode,
    eventServer,
    privateKey
);

async function changestate(){

  let contractAddress = 'TAA6PHMkrQs5bZp7cBYcfdWES9EMb5BZaK';   //Your address
  let contract = await tronWeb.contract().at(contractAddress);
  let resultPost = await contract.postMessage("TRON to the Future").send();
  let resultGet = await contract.getMessage().call();
  console.log('resultGet: ', resultGet);
}

changestate()

Let us break-down the program: The program first defines the addresses and ports for your local Docker nodes and event server. This information is stored into the tronWeb object. A function called changestate is defined and then executed. The function first defines the contract object. It then executes the postMessage function using the .send() method, as well as the getMessage function using the .call() method. Finally, the function outputs the getMessage result.

The output looks like:

State changing function posted "TRON to the Future"

State changing function posted "TRON to the Future"

Congratulations! You have now set up a local TRON blockchain, deployed your smart contract, immutably changed your blockchain via TronWeb API calls, and queried the result.