HomeGuidesAPI ReferenceChangelog
GuidesAPI ReferenceCommunityDiscordBlogFAQBug BountyAnnouncementsChange Log
Guides

Getting Started

Tron-Web 2.0

What is Tron-Web?

TronWeb is a Javascript library of TRON full node's API functions. To include the TronWeb library in your DApp, you can instantiate the TronWeb object.

Installing Tron-Web

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

npm install tronweb

❗️

Note on Version

For the latest Java-Tron Odyssey 3.5 release, the minimum compatible TronWeb version is TronWeb 2.3.0. Please ensure you have this version installed.

Creating an Instance

Tron-Web Object

To instantiate a TronWeb object within your DApp, please see the API reference for instantiating a TronWeb Object

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()
hostThe host URI of the node
timeout30000Optional default timeout when dispatching requests
userfalseOptional username for user authentication
passwordfalseOptional password for user authentication
headers{}Optional headers to pass to requests
statusPage'/'Optional status page to query when checking if the node is connected

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.

📘

Testing TronWeb

For testing TronWeb API functions, it would be best to setup a private network on your local machine using the TRON Docker Quickstart guide. The Docker guide sets up a Full Node, Solidity Node, and Event Server on your machine. You can then deploy smart contracts on your network and interact with them via TronWeb. If you wish to test TronWeb with other users, it would be best to deploy your contracts/DApps on the Shasta test network and interact from there.


What’s Next