TronGridJS

Introduction

TronGridJS is a Javascript library for utilizing TronGrid APIs to retrieve blockchain data from the TRON network.

Compatibility

  • Version built for Node.js v6 and above.
  • Version built for browsers with more than 0.25% market share.

TronGridJS is also compatible with front-end frameworks like:

  • Angular
  • React
  • Vue

TronGridJS can also be shipped in a Chrome extension.

Installation

TronGridJS - NPM Package

NPM

> npm install trongrid

Yarn

> yarn add trongrid

Build Steps

If you would like to download and build locally, please follow the below steps:

git clone https://github.com/TRON-US/trongrid-js.git
cd trongrid-js
yarn install
yarn dev
yarn build
yarn test

Supported APIs

TronGridJS allows for easy access to the new v1 APIs provided by TronGrid. Since TronGridJS uses TronWeb as a dependency, you need to initialize TronGridJS with TronWeb as a constructor parameter:

const tronWeb = new TronWeb({
        fullHost: node.HOST,
        privateKey: node.PRIVATE_KEY
    })
const tronGrid = new TronGrid(tronWeb)
tronGrid.account.get(accountAddress, options)
DescriptionSubstitutes
Returns information about the account at accountAddress/wallet/getaccount
ParameterDescriptionValues
onlyConfirmedShows only the situation at latest confirmed block.True | False (Default False)
tronGrid.account.getTransactions(accountAddress, options)
DescriptionSubstitutes
Returns all the transactions related to the account at accountAddress/walletextension/gettransactionfromthis
/walletextension/gettransactiontothis
ParameterDescriptionValues
only_confirmedShows only confirmed.true | false (default false)
only_unconfirmedShows only unconfirmed.true | false (default false)
only_toOnly transaction to address.true | false (default false)
only_fromOnly transaction from address.true | false (default false)
limitThe requested number of transaction per page.Default 20. Max 200.
fingerprintThe fingerprint of the last transaction returned by the previous page.
order_bySorts the results of the query. Example:
order_by=timestamp, desc
min_timestampThe minimum transaction timestamp.default 0
max_timestampThe maximum transaction timestamp.default now
tronGrid.asset.getAll(options)
DescriptionSubstitutes
Returns all the assets on the TRON platform.N/A
ParameterDescriptionValue
order_bySorts the results.Accepted fields:
id
name
total_supply
start_time
end_time
tronGrid.asset.get(assetIdentifier, options)
DescriptionSubstitutes
Returns an asset identified by either the owner address or its own ID./wallet/getassetissuebyaccount /wallet/getassetissuebyid
tronGrid.asset.getList(assetName, options)
DescriptionSubstitutes
Returns all the assets with name assetName/wallet/getassetissuelistbyname
/wallet/getassetissuelist
ParameterDescriptionValue
limitThe requested number of assets per page.Default 20. Max 200.
fingerprintThe fingerprint of the last asset returned by the previous page. When there is a pagination, the minimum limit is set to 20.
order_bySorts the results of the query.Accepted fields:
id
name
total_supply
start_time
end_time

Example:
order_by=start_time,desc (starts from the most recent ICO)

order_by=id,asc
(starts from the oldest)
only_confirmedShows only the situation at latest confirmed block.true | false (default: false)
tronGrid.block.getEvents(identifier, options)
DescriptionSubstitutes
Returns all the events of a specific block. The identifier can be either latest or a block number.N/A
tronGrid.contract.getEvents(contractAddress, options)
DescriptionSubstitutes
Returns all the events emitted by a smart contract.N/A
ParameterDescriptionValue
only_confirmedShows only confirmed.
only_unconfirmedShows only unconfirmed.
event_nameThe name of the event.
block_numberThe block number for which the events are required.
min_timestampThe minimum block timestamp.
max_timestampThe maximum block timestamp.
order_bySorts the events.Accepted values:
timestamp, asc
timestamp, desc (Default)
limitFor pagination.Default 20
Max 200
fingerprintThe fingerprint of the last event retrieved in the page.
tronGrid.transaction.getEvents(id, options)
DescriptionSubstitutes
Returns all the events emitted in the transaction specified by idN/A

Responses & Pagination

Any API will return a response with a success property, a data array, and a meta object. For example, await tronGrid.asset.getAll() will return something like:

{
    "success": true,
    "data": [
        {
            "confirmed": true,
            "id": "1002225",
            "abbr": "DbDsgVP3GRh",
            "description": "KEYS unlock Cryptocurrency. Keys are a digital asset designed to work as medium of exchange.",
            "frozen_supply": [
                {
                    "frozen_days": 730,
                    "frozen_amount": 75926666666
                }
            ],
            "name": "KEYS",
            "num": 22778,
            "precision": 0,
            "total_supply": 227780000000,
            "trx_num": 22778,
            "url": "www.KEYS.crypto.org",
            "vote_score": 0,
            "owner_address": "4149b3dad5ef9dbab6a059fc95159efcecd5db910e",
            "start_time": 1553538720706,
            "end_time": 1553538960706
        },
        ...
    ],
    "meta": {
        "total": 2,
        "at": 1553548776704,
        "fingerprint": "8xuwf4jd2dpoSms5KzLhxY9fmCm9oJA5164Qd7T2SexRSHYCwvRAr2zJGtwJceEcGWz",
        ...
    }
}

As you can see, in the meta fields, there is the fingerprint you must pass to next request as an option in order to get next page.

Usage

Regular Use

Install TronWeb

npm install tronweb

Initialize TronWeb and create a TronGridJS instance

const TronGrid = require('trongrid');
const TronWeb = require('tronweb');

const tronWeb = new TronWeb({
    fullHost: 'https://api.trongrid.io'
});

const tronGrid = new TronGrid(tronWeb);

Example

const TronGrid = require('trongrid');
const TronWeb = require('tronweb');

const tronWeb = new TronWeb({
    fullHost: 'https://api.trongrid.io'
});

const tronGrid = new TronGrid(tronWeb);
tronGrid.setExperimental('your experimental key');

async function getAccount() {
    const address = 'TPL66VK2gCXNCD7EJg9pgJRfqcRazjhUZY';

    const options = {
        Show_assets: true,
        only_confirmed: true,
    };

    // awaiting
    const account = await tronGrid.account.get(address, options);
    console.log({account});

    // promise
    tronGrid.account.get(address, options).then(account => {
        console.log({account});
    }).catch(err => console.error(err));

    // callback
    tronGrid.account.get(address, options, (err, account) => {
        if (err)
            return console.error(err);

        console.log({account});
    });
}

async function getTransactions() {
    const address = 'TPL66VK2gCXNCD7EJg9pgJRfqcRazjhUZY';

    const options = {
        only_to: true,
        only_confirmed: true,
        limit: 100,
        order_by: 'timestamp,asc',
        min_timestamp: Date.now() - 60000 // from a minute ago to go on
    };

    // awaiting
    const transactions = await tronGrid.account.getTransactions(address, options);
    console.log({transactions});

    // promise
    tronGrid.account.getTransactions(address, options).then(transactions => {
        console.log({transactions});
    }).catch(err => console.error(err));

    // callback
    tronGrid.account.getTransactions(address, options, (err, transactions) => {
        if (err)
            return console.error(err);

        console.log({transactions});
    });
}

async function getAssets() {
    const address = 'TXk39yyhzpfbqtU1BATUzpcfQ37L8Tc4Ht';
    const options = {};

    // awaiting
    const assets = await tronGrid.asset.get(address);
    console.log({assets});

    // promise
    tronGrid.asset.get(address, options).then(assets => {
        console.log({assets});
    }).catch(err => console.error(err));

    // callback
    tronGrid.asset.get(address, options, (err, assets) => {
        if (err)
            return console.error(err);

        console.log({assets});
    });
}

getAccount();
getTransactions();
getAssets();

Plugin

You can plug TronGrid in TronWeb to substitute the standard method tronWeb.trx.getTransactionRelated(...) with a method that calls internally tronGrid.account.getTransactions(...).

To use it, the user must register TronGrid as a plugin:

tronWeb.plugin.register(TronGrid);

It may sometimes be necessary to pass some options to the plugin interface. Looking at the test in test/unit/plugin.test.js, you can register the plugin as:

let result = tronWeb.plugin.register(GetNowBlock, {
    someParameter: 'some value'
})

For the MVP, the experimental access code can be passed as an option to the plugin:

tronWeb.plugin.register(TronGrid, {experimental: ... });

After registering the plugin, call:

await tronWeb.trx.getTransactionsFromAddress(address);

and

await tronWeb.trx.getTransactionsToAddress(address);

will return results from TronGrid under the hood.