πŸ“š Tronsell Wiki

TronWeb Library Guide β€” TRON Developer Reference

Complete guide to TronWeb, the official JavaScript library for TRON blockchain development. Learn installation, configuration, core APIs, and practical examples.

πŸ“š TronWeb at a Glance
Library TypeOfficial JavaScript SDK
PurposeTRON blockchain interaction
Installationnpm install tronweb
Key FeaturesTransactions, Contracts, Accounts
EnvironmentNode.js + Browser
Docsdevelopers.tron.network

πŸ“šWhat is TronWeb?

TronWeb is the official JavaScript library for interacting with the TRON blockchain. It provides a simple, intuitive API for building decentralized applications (dApps), wallets, and blockchain integrations.

TronWeb handles the complexity of:

  • Transaction building: Creating and structuring transactions
  • Signing: Signing transactions with private keys or hardware wallets
  • Broadcasting: Sending transactions to the TRON network
  • Smart contract interaction: Calling contract functions (read and write)
  • Account management: Creating wallets, importing private keys
  • Blockchain queries: Getting account info, block data, transaction history
πŸ’‘ Why TronWeb?

TronWeb is the most popular and best-supported library for TRON development. It's maintained by the TRON Foundation and used by thousands of dApps, wallets, and services.

πŸ“¦Installation

TronWeb can be installed in both Node.js and browser environments.

// Node.js (npm)
npm install tronweb

// Browser (CDN)
<script src="https://cdn.jsdelivr.net/npm/tronweb/dist/TronWeb.js"></script>

// ES Module
import TronWeb from 'tronweb';
πŸ’‘ Version Compatibility

Always check the TronWeb version compatibility with your TRON node version. The latest versions support the most recent TRON protocol features.

βš™οΈConfiguration

Initialize TronWeb with your RPC endpoints and optional private key:

// Basic initialization (mainnet)
const tronWeb = new TronWeb({
  fullHost: 'https://api.trongrid.io',
  privateKey: 'your_private_key_here'
});

// With separate nodes
const tronWeb = new TronWeb({
  fullNode: 'https://api.trongrid.io',
  solidityNode: 'https://api.trongrid.io',
  eventServer: 'https://api.trongrid.io',
  privateKey: 'your_private_key_here'
});

// Testnet (Shasta)
const tronWeb = new TronWeb({
  fullHost: 'https://api.shasta.trongrid.io',
  privateKey: 'your_private_key_here'
});
πŸ’‘ Private Key Security

Never hardcode private keys in your code. Use environment variables or secure key management systems. For browser dApps, use TronLink or WalletConnect for signing.

πŸ”‘Core APIs

TronWeb is organized into several modules:

Module Purpose Key Methods
trx TRX and transaction operations sendTransaction(), getAccount(), getBalance(), freezeBalance()
contract Smart contract interaction at(), deploy(), call()
transactionBuilder Build custom transactions sendTrx(), triggerSmartContract()
account Account management create(), fromPrivateKey()
utils Utility functions toHex(), fromHex(), addressToHex()
πŸ’‘ Module Structure

TronWeb's modular design makes it easy to use only the parts you need. The trx module is the most commonly used for basic blockchain operations.

πŸ’ΈSending Transactions

Here's how to send TRX and TRC20 tokens using TronWeb:

// Send TRX
const tx = await tronWeb.trx.sendTransaction(
  'TXYZ...XXXX',
  10 // 10 TRX
);
console.log('TXID:', tx.txid);

// Send USDT TRC20
const contractAddress = 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t';
const usdt = await tronWeb.contract().at(contractAddress);
const tx2 = await usdt.transfer(
  'TXYZ...XXXX',
  100_000_000 // 100 USDT (6 decimals)
).send();
console.log('USDT TXID:', tx2.txid);
πŸ’‘ Decimal Handling

TRC20 tokens like USDT have 6 decimals. Always multiply the amount by 10^6. For TRX, use 10^6 (1 TRX = 1,000,000 SUN).

πŸ“œSmart Contract Interaction

TronWeb makes smart contract interaction simple:

// Read a contract function (view - no fee)
const contract = await tronWeb.contract().at(contractAddress);
const balance = await contract.balanceOf('TXXX...XXXX').call();
console.log('Balance:', balance / 1e6);

// Write a contract function (requires Energy)
const tx = await contract.setGreeting('Hello, TRON!').send();
console.log('TXID:', tx.txid);
  • Read operations: Use .call() β€” no fee, no transaction
  • Write operations: Use .send() β€” requires Energy/TRX
  • ABI: TronWeb can automatically detect contract ABIs from the blockchain
πŸ’‘ ABI Handling

If TronWeb can't find the ABI automatically, you can pass it manually: tronWeb.contract(abi, address)

πŸ‘€Account Management

TronWeb provides account management functions:

// Create a new wallet
const wallet = await tronWeb.account.create();
console.log('Address:', wallet.address.base58);
console.log('Private Key:', wallet.privateKey);

// Import existing wallet from private key
const imported = await tronWeb.account.fromPrivateKey(privateKey);
console.log('Imported Address:', imported.address);

// Get account info
const account = await tronWeb.trx.getAccount('TXXX...XXXX');
console.log('Account data:', account);
πŸ’‘ Key Management

Never store private keys in client-side code for production. Use TronLink (browser extension) for user-controlled key management in dApps.

πŸ“‘Event Listening

TronWeb can listen to smart contract events:

// Get transfer events
const events = await tronWeb.trx.getEventResult(
  contractAddress,
  {
    eventName: 'Transfer',
    blockNumber: { min: 1000000, max: 1001000 }
  }
);
events.forEach(event => console.log(event));
  • Event filtering: Filter by event name, block range, or contract
  • Use cases: Track token transfers, monitor contract activity, build real-time applications
πŸ’‘ Event Performance

Event queries can be heavy on the node. Use block number filters to limit results. For production, consider using an event server like TronGrid's event service.

βœ…Best Practices

  • Use environment variables: Never hardcode private keys. Use process.env or similar.
  • Handle errors: Always wrap TronWeb calls in try/catch blocks.
  • Rate limiting: Implement exponential backoff for retries when hitting rate limits.
  • Use testnet first: Test all transactions on Shasta testnet before mainnet.
  • Validate inputs: Sanitize addresses and amounts before sending.
  • Keep TronWeb updated: Regularly update to the latest version for security fixes and new features.
  • Use TronLink for dApps: Let users manage their own keys through TronLink or WalletConnect.
πŸ’‘ Production Ready

For production applications, consider running your own full node and using TronWeb with custom node endpoints for dedicated RPC access.

❓Frequently Asked Questions

What is TronWeb?

TronWeb is the official JavaScript library for interacting with the TRON blockchain. It provides a simple, intuitive API for sending transactions, interacting with smart contracts, querying blockchain data, and managing accounts. It's the essential tool for TRON dApp development.

How do I install TronWeb?

You can install TronWeb via npm: npm install tronweb. For browser usage, include the CDN script: https://cdn.jsdelivr.net/npm/tronweb/dist/TronWeb.js. TronWeb works with both Node.js and browser environments.

How do I connect TronWeb to the TRON blockchain?

Initialize TronWeb with a fullNode URL (e.g., https://api.trongrid.io for mainnet or https://api.shasta.trongrid.io for testnet). You can also optionally provide a solidityNode URL and a private key for signing transactions.

Can I use TronWeb with a hardware wallet?

Yes. TronWeb supports hardware wallets like Ledger through the TronLink browser extension or by using the tronweb.setAccount() method with a hardware wallet provider. This allows you to sign transactions without exposing your private key.

What is the difference between .call() and .send() in TronWeb?

.call() is used for read-only smart contract functions (view/pure) β€” it doesn't modify state and doesn't require a fee. .send() is used for write functions that modify state β€” it requires Energy/TRX to execute and broadcasts a transaction.

⚑ Build with Tronsell Energy

Integrate Tronsell Energy into your TronWeb applications for zero-fee USDT transfers. Simple API, instant delivery.