TRON Developer Guide—TRON-WEB—Getting Started

TRON DAO
3 min readJun 16, 2019

--

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

Creating an Instance

Tron-Web Object

To instantiate a TronWeb object within your DApp, please see the API reference forinstantiating 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.HttpProviderinstance.

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_addressproperty 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.

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.

--

--

TRON DAO
TRON DAO

Written by TRON DAO

The official Medium of TRON DAO.

No responses yet