--- sidebar_position: 20 --- # CookBook The TronWeb Cookbook is a collection of practical, example‑driven guides and code snippets that show how to use TronWeb. With it, you can quickly: - Find a working snippet for a task you’re trying to implement. - Copy, adapt, and run code immediately. - Learn best practices. ## For developing and testing 1. Choose an http api network from [here](https://developers.tron.network/docs/networks). For example, if you want to use Nile Testnet, you can use `https://api.nileex.io`. But you can also use a local network, see [here](./Build%20local%20node.md). 2. Connect to the network you chose. 3. Write your code. You may need to use TRX in your code. For testnet, go to faucet respectively to get the TRX for testing. For local network, you can use `tre_setAccountBalance` to activate the account and get TRX. 4. Live code editor: Using Nile Testnet. ```ts live multi import { TronWeb } from "tronweb"; const tronWeb = new TronWeb({ fullHost: 'https://api.nileex.io', // Use your **test** account here. To get test TRX, go to nile faucet: https://nileex.io/join/getJoinPage. privateKey: '', }); (async () => { const account = await tronWeb.createAccount(); // Activate the account by sending 1 trx to it. const receipt = await tronWeb.trx.sendTrx(account.address.base58, 1e6); console.log(receipt); })(); ``` Using local network, this code might not work in browser because local network uses http protocol while the site is hosted on https. ```ts live multi import { TronWeb } from "tronweb"; const tronWeb = new TronWeb({ fullHost: 'http://127.0.0.1:9090', }); (async () => { const account1 = await tronWeb.createAccount(); // Activate account1 by set balance to 100000 trx. await tronWeb.fullNode.request('/tre', { method: 'tre_setAccountBalance', params: [account1.address.base58, 100000*1e6], id: 1, jsonrpc: '2.0', }, 'POST'); // Wait for 3 seconds await new Promise(r => setTimeout(r, 3000)); const account2 = await tronWeb.createAccount(); // Activate account2 by sending 1 trx to it. const receipt = await tronWeb.trx.sendTrx(account2.address.base58, 1e6, { privateKey: account1.privateKey }); console.log(receipt); })(); ``` ## Contracts manipulation Supposing you have the below contract and you want to deploy it and trigger it, you can do like follow. ```solidity pragma solidity ^0.4.18; contract funcABIV2 { uint256 public check; constructor(uint256 _check) public { check = _check; } function setCheck(uint256 _check) public { check = _check; } } ``` You can get its abi and bytecode by compiling it using [TronIDE](https://www.tronide.io/). Deploy the contract and trigger it: ```ts live multi import { TronWeb } from "tronweb"; const tronWeb = new TronWeb({ fullHost: 'https://api.nileex.io', // Use your **test** account here. To get test TRX, go to nile faucet: https://nileex.io/join/getJoinPage. privateKey: '', }); const contractInfo = { abi: [ { inputs: [ { internalType: 'uint256', name: '_check', type: 'uint256', }, ], stateMutability: 'nonpayable', type: 'constructor', }, { inputs: [], name: 'check', outputs: [ { internalType: 'uint256', name: '_check', type: 'uint256', }, ], stateMutability: 'view', type: 'function', }, { inputs: [ { internalType: 'uint256', name: '_check', type: 'uint256', }, ], name: 'setCheck', outputs: [], stateMutability: 'nonpayable', type: 'function', }, ], bytecode: '0x608060405234801561001057600080fd5b5060405160208061012583398101806040528101908080519060200190929190505050806000819055505060dc806100496000396000f3006080604052600436106049576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680632c948bd214604e578063919840ad146078575b600080fd5b348015605957600080fd5b5060766004803603810190808035906020019092919050505060a0565b005b348015608357600080fd5b50608a60aa565b6040518082815260200191505060405180910390f35b8060008190555050565b600054815600a165627a7a7230582084a638eb1bcab674b68b98bc8407c96a2e186016c5da95ffcf421c1d40d0feb60029', }; (async () => { const contract = await tronWeb.contract().new({ abi: contractInfo.abi, bytecode: contractInfo.bytecode, funcABIV2: contractInfo.abi[0], parametersV2: [1], }); const [check] = await contract.check().call(); console.log('first check:', check); await contract.setCheck(123).send(); const [check2] = await contract.check().call(); console.log('second check:', check2); })(); ``` ## Build/sign/broadcast transactions TronWeb has supported 36 transaction types. You can go [here](./API%20List/transactionBuilder/) to see all transaction types. Here we use `sendTrx` transaction as an example to see how to build/sign/broadcast a transaction: ```ts live multi import { TronWeb } from "tronweb"; const tronWeb = new TronWeb({ fullHost: 'https://api.nileex.io', // Use your **test** account here. To get test TRX, go to nile faucet: https://nileex.io/join/getJoinPage. privateKey: '', }); (async () => { const account = await tronWeb.createAccount(); // Build a transaction that sends 1 TRX to the new account const tx = await tronWeb.transactionBuilder.sendTrx(account.address.base58, 1e6); // Sign it const signedTx = await tronWeb.trx.sign(tx); // Broadcast it const receipt = await tronWeb.trx.sendRawTransaction(signedTx); // See the result console.log(receipt); })(); ``` ## Events subscribing You may need to subscribe events to get the real-time status of a transaction. Here is an example to do it: ```ts live multi import { TronWeb } from "tronweb"; const tronWeb = new TronWeb({ // We need to use TronGrid api here to get events. fullHost: 'https://nile.trongrid.io', // Use your **test** account here. To get test TRX, go to nile faucet: https://nileex.io/join/getJoinPage. privateKey: '', }); // You can use your contract address on Nile Testnet. Here we use the contract address of USDT on Nile Testnet. const contractAddress = 'TXYZopYRdj2D9XRtbG411XZZ3kM5VkAeBf'; const abi = [{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_upgradedAddress","type":"address"}],"name":"deprecate","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"}],"name":"approve","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"deprecated","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_evilUser","type":"address"}],"name":"addBlackList","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"upgradedAddress","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"maximumFee","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"_totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"unpause","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"_maker","type":"address"}],"name":"getBlackListStatus","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"paused","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_subtractedValue","type":"uint256"}],"name":"decreaseApproval","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"who","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"_value","type":"uint256"}],"name":"calcFee","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"pause","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"who","type":"address"}],"name":"oldBalanceOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"newBasisPoints","type":"uint256"},{"name":"newMaxFee","type":"uint256"}],"name":"setParams","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"amount","type":"uint256"}],"name":"issue","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_addedValue","type":"uint256"}],"name":"increaseApproval","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"amount","type":"uint256"}],"name":"redeem","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"},{"name":"_spender","type":"address"}],"name":"allowance","outputs":[{"name":"remaining","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"basisPointsRate","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"isBlackListed","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_clearedUser","type":"address"}],"name":"removeBlackList","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"MAX_UINT","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_blackListedUser","type":"address"}],"name":"destroyBlackFunds","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"inputs":[{"name":"_initialSupply","type":"uint256"},{"name":"_name","type":"string"},{"name":"_symbol","type":"string"},{"name":"_decimals","type":"uint8"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"name":"_blackListedUser","type":"address"},{"indexed":false,"name":"_balance","type":"uint256"}],"name":"DestroyedBlackFunds","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"amount","type":"uint256"}],"name":"Issue","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"amount","type":"uint256"}],"name":"Redeem","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"newAddress","type":"address"}],"name":"Deprecate","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"_user","type":"address"}],"name":"AddedBlackList","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"_user","type":"address"}],"name":"RemovedBlackList","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"feeBasisPoints","type":"uint256"},{"indexed":false,"name":"maxFee","type":"uint256"}],"name":"Params","type":"event"},{"anonymous":false,"inputs":[],"name":"Pause","type":"event"},{"anonymous":false,"inputs":[],"name":"Unpause","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"previousOwner","type":"address"},{"indexed":true,"name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"},{"indexed":true,"name":"spender","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Transfer","type":"event"}]; async function listenForEvents(txId, timeGap = 500) { while (true) { const events = await tronWeb.event.getEventsByTransactionID(txId); if (events.data.length) { return events.data; } await new Promise(r => setTimeout(r, timeGap)); console.log('wait for ' + timeGap +'ms'); } } (async () => { const contract = tronWeb.contract(abi, contractAddress); const txId = await contract.transfer('TVNevinkBb9JytBHhK2ZMsnWX5sWqJu9fx', 1e6).send(); const events = await listenForEvents(txId); console.log(events); })() ```