TronWeb JavaScript SDK

6.2.2 · active · verified Tue Apr 21

TronWeb is the official JavaScript SDK for interacting with the TRON blockchain, providing a comprehensive encapsulation of the TRON HTTP API. As of version 6.2.2, it continues to be actively maintained with frequent minor and patch releases, incorporating new TRON features and API quality-of-life improvements. Unlike a direct fork of Ethereum's Web3.js, TronWeb is specifically tailored to unlock TRON's unique feature set and offers distinct tools for DApp integration across browsers, Node.js environments (v16+), and IoT devices. It ships with TypeScript types, enhancing developer experience for strongly typed projects. Key features include deterministic contract address computation (CREATE2), robust transaction deserialization, and methods for retrieving real-time witness lists and transaction building parameters.

Common errors

Warnings

Install

Imports

Quickstart

Initializes TronWeb, connects to the Shasta testnet using environment variables for sensitive keys, fetches the default account's balance, and demonstrates the `getCreate2Address` utility function.

import TronWeb from 'tronweb';

// Replace with your actual TRON Grid API key and a test private key for Shasta
// NEVER use production private keys directly in code. Use environment variables for security.
const TRON_GRID_API_KEY = process.env.TRON_GRID_API_KEY ?? 'YOUR_TRONGRID_API_KEY';
const TEST_PRIVATE_KEY = process.env.TEST_PRIVATE_KEY ?? 'YOUR_SHASTA_PRIVATE_KEY_HERE'; // e.g., for a test account on Shasta

if (TRON_GRID_API_KEY === 'YOUR_TRONGRID_API_KEY' || TEST_PRIVATE_KEY === 'YOUR_SHASTA_PRIVATE_KEY_HERE') {
  console.warn('Please provide a valid TRON_GRID_API_KEY and TEST_PRIVATE_KEY in your environment variables or replace placeholders.');
  // In a real application, you might throw an error or exit.
}

const tronWeb = new TronWeb({
  fullHost: 'https://api.shasta.trongrid.io', // Official Tron testnet endpoint
  headers: { "TRON-PRO-API-KEY": TRON_GRID_API_KEY },
  privateKey: TEST_PRIVATE_KEY
});

async function getAccountInfo() {
  try {
    const address = tronWeb.defaultAddress.base58;
    if (!address) {
      console.error("No default address set. Ensure your private key is valid and connected.");
      return;
    }
    console.log(`Connected to Shasta Testnet with address: ${address}`);

    const balanceSun = await tronWeb.trx.getBalance(address);
    const balanceTrx = tronWeb.trx.fromSun(balanceSun);

    console.log(`Account Balance: ${balanceTrx} TRX`);

    // Example of using a new utility method (v6.2.2+)
    const creatorAddress = 'TXwU1rY5Gj89P4g45v3C6M5427K8K1YJ8M'; // Example creator address
    const salt = '0x1234567890abcdef1234567890abcdef'; // Example salt
    const bytecodeHash = '0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470'; // Example bytecode hash
    const create2Address = tronWeb.utils.address.getCreate2Address(creatorAddress, salt, bytecodeHash);
    console.log(`CREATE2 Address for example: ${create2Address}`);

  } catch (error) {
    console.error("Error fetching account info or using utilities:", error);
  }
}

getAccountInfo();

view raw JSON →