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    

Getting Started

Tron-Web 2.0

What is Tron-Web?

Tron-Web aims to deliver a unified and seamless development experience influenced by Ethereum's Web3 implementation. We have expanded upon the core concept of Web3 to unlock the functionality of TRON's unique feature set, along with offering new tools for integrating DApps in the browser, Node.js, and IoT devices.

Installing Tron-Web

You can install Tron-Web using the latest release on NPM or by downloading the latest distributable js files.

Releases

npm install tronweb

Creating an Instance

TronWeb requires both a Full Node and Solidity Node to be provided. These nodes must be running the HTTP API. You can optionally specify an event server if you wish to listen for events logged by smart contracts.

Tron-Web Class

The Tron-Web class takes a minimum of two parameters and a maximum of four. Full node and solidity node endpoints are required, while the event server endpoint is optional. You can pass a private key as the fourth argument for use when signing transactions.

import TronWeb from 'TronWeb'

const fullNode = 'https://api.trongrid.io';
const solidityNode = 'https://api.trongrid.io';
const eventServer = 'https://api.trongrid.io/';
const privateKey = 'da1463745fff9f...0d0';

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

Providers

Providers are used to provide methods of transport for requests. At the moment, Tron-Web only supports a single transport - HTTP.

HTTP Provider

If you wish to provide more options to the node endpoints, such as user authentication or custom headers, you can pass a TronWeb.providers.HttpProvider instance.

constructor()

host

The host URI of the node

timeout

30000

Optional default timeout when dispatching requests

user

false

Optional username for user authentication

password

false

Optional password for user authentication

headers

{}

Optional headers to pass to requests

statusPage

'/'

Optional status page to query when checking if the node is connected

import TronWeb from 'TronWeb'

const HttpProvider = TronWeb.providers.HttpProvider;
const fullNode = new HttpProvider('https://api.trongrid.io:8090', 'root', 'alpine');
const solidityNode = new HttpProvider('https://api.trongrid.io:8091', 'root', 'alpine');

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

Default Values

Tron-Web allows you to set a number of default values for fetching, creating, and dispatching transactions.

setDefaultBlock(blockID = false)

This allows you to set the default block (hash or number) for use when querying transactions (and in some instances, creating).

setPrivateKey(privateKey = false)

When set, this private key will sign transactions without the need to pass a private key argument to each function call.

setAddress(address = false)

For use when creating unsigned transactions. This will fill in the owner_address property for each transaction created by the HTTP API. This is automatically set if you call setPrivateKey().

Promises vs. Callbacks

Tron-Web has been designed to be as compatible with Ethereum's Web3 implementation as possible. This means we support callbacks in the majority of our functions.

That doesn't mean Tron-Web is limited in any shape or form. This means that we also support promises. By default, calling a function will return a promise. If you provide a callback function, then a promise will not be returned and the callback will act as the response trigger instead.

const tronWeb = new TronWeb(fullNode, solidityNode);

const app = async () => {
    const userBalance = await tronWeb.trx.getBalance(userAddress);
    console.log(`User's balance is: ${ userBalance }`);
};

app();
const tronWeb = new TronWeb(fullNode, solidityNode);

tronWeb.trx.getBalance(userAddress).then(userBalance => {
    console.log(`User's balance is: ${ userBalance }`);
}).catch(error => {
    console.error(error);  
});
const tronWeb = new TronWeb(fullNode, solidityNode);

tronWeb.trx.getBalance(userAddress, (error, userBalance) => {
    if(error)
        return console.error(error);
  
    console.log(`User's balance is: ${ userBalance }`);
});

Note: Don't worry if a method takes optional arguments - the last argument provided will always be treated as the callback.


What's Next

Tron-Web Class