π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
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.
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:
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() |
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:
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 operations: Use .call() β no fee, no transaction
- Write operations: Use .send() β requires Energy/TRX
- ABI: TronWeb can automatically detect contract ABIs from the blockchain
If TronWeb can't find the ABI automatically, you can pass it manually: tronWeb.contract(abi, address)
π€Account Management
TronWeb provides account management functions:
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:
- Event filtering: Filter by event name, block range, or contract
- Use cases: Track token transfers, monitor contract activity, build real-time applications
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.
For production applications, consider running your own full node and using TronWeb with custom node endpoints for dedicated RPC access.