Quickstart
Getting Started
The TronWeb is a JavaScript library for building Web3 projects on TRON. It consists of 4 main parts:
- TransactionBuilder for building transactions with client-side integrity check.
- Trx for requesting data from nodes or sign transactions etc.
- Contract for interacting with contracts.
- Other useful Utils like web3.js or ethers.js.
Installation
Node.js
npm install tronweb
or
yarn add tronweb
Browser
The easiest way to use TronWeb in a browser is to install it as above and copy the dist file to your working folder. For example:
cp node_modules/tronweb/dist/TronWeb.js ./js/tronweb.js
so that you can call it in your HTML page as
<script src="./js/tronweb.js"><script>
Instantiation
Then your javascript file, define TronWeb:
const TronWeb = require('tronweb');
When you instantiate TronWeb you can set
- fullHost
Supposing you are using a server that provides everything, like TronGrid, you can instantiate TronWeb as:
const tronWeb = new TronWeb({
fullHost: 'https://api.trongrid.io',
headers: { 'TRON-PRO-API-KEY': 'your api key' },
privateKey: 'your private key'
});
Like infura API Key, you can sign up for TronGrid and create your API key on the dashboard to access TRON Network data.
Note:
- Do not expose your private key in any web browser environment.
- You can instantiate TronWeb without privateKey, if you only need to use some
utils
functions such asTronWeb.utils
- If you only want to query information from the TRON Network blockchain without signing a transaction with instantiated tronweb, such as
getTransactionInfo
,triggerconstantcontract
, you can pass a public private key such as01
to instantiate TronWeb.
Querying the Tron network
You can query data of the Tron network with tronweb.trx easy as follows:
> tronWeb.trx.getBlockByNumber(12345).then(result => {console.log(result)});
Promise { <pending> }
> {
blockID: '000000000000303987c7c8ab3f5967c107a619fa47819940597e9938811a1764',
block_header: {
raw_data: {
number: 12345,
txTrieRoot: '0000000000000000000000000000000000000000000000000000000000000000',
witness_address: '414b4778beebb48abe0bc1df42e92e0fe64d0c8685',
parentHash: '0000000000003038c0a3aa1806236bc5b281633728b5fe8a14a51062522e651d',
timestamp: 1529928585000
},
witness_signature: 'cb889103aa9ce691d39df8030b54b50b12b77984684281f3490e0b802cbc364c13af773ede8d9314add0fa4d247165be82fa28721f17493c88761b7039ba1c1100'
}
}
Creating a transaction
Tronweb provides well-rounded APIs to satisfy your transaction building demands. No worry about the transaction would have tampered with because client-side integrity checking is done behind. See the example:
const transaction = await tronWeb.transactionBuilder.sendTrx("TVDGpn4hCSzJ5nkHPLetk8KQBtwaTppnkr", 100, "TNPeeaaFB7K9cmo4uQpcU32zGK8G1NYqeL");
>{
"visible": false,
"txID": "9f62a65d0616c749643c4e2620b7877efd0f04dd5b2b4cd14004570d39858d7e",
"raw_data": {
"contract": [
{
"parameter": {
"value": {
"amount": 100,
"owner_address": "418840e6c55b9ada326d211d818c34a994aeced808",
"to_address": "41d3136787e667d1e055d2cd5db4b5f6c880563049"
},
"type_url": "type.googleapis.com/protocol.TransferContract"
},
"type": "TransferContract"
}
],
"ref_block_bytes": "0add",
"ref_block_hash": "6c2763abadf9ed29",
"expiration": 1581308685000,
"timestamp": 1581308626092
},
"raw_data_hex": "0a020add22086c2763abadf9ed2940c8d5deea822e5a65080112610a2d747970652e676f6f676c65617069732e636f6d2f70726f746f636f6c2e5472616e73666572436f6e747261637412300a15418840e6c55b9ada326d211d818c34a994aeced808121541d3136787e667d1e055d2cd5db4b5f6c880563049186470ac89dbea822e"
}
Signing a transaction
Tronweb will automatically use the privateKey you provided when you initialize tronweb. Or you can provide an optional privateKey as a second argument.
const signedTransaction = tronWeb.trx.sign(transaction, privateKey);
Broadcasting
Broadcasting is as easy as follows:
tronWeb.trx.sendTransaction(signedTransaction);
Contracts
First, load a contract:
const abi = [...];
const instance = await tronWeb.contract(abi,'contractAddress');
Call read-only methods:
const result = await contract.function_name(para1,para2,...).call();
Call state changing methods:
const result = await contract.function_name(para1,para2,...).send({
feeLimit:100_000_000,
callValue:0,
tokenId:1000036,
tokenValue:100,
shouldPollResponse:true
});