TronWeb SDK

website·Indexed 33 days ago

https://tronweb.network/

A JavaScript SDK for interacting with the TRON network. Version 6.5.0 provides tools for account creation, TRX and token transfers (TRC10, TRC20, TRC721), and smart contract interaction using methods like call(), send(), and the abiV2 standard. The SDK includes namespaces for contract reading and writing, message signing and verification via signMessageV2, and a comprehensive API for querying block and account data.

Tokens
75.1K
Snippets
345
Records
483
Agent score
70%

What's inside TronWeb

  1. Retrieve TRC10 token information by ID using getTokenByID

    The tronWeb.trx.getTokenByID method allows you to query detailed information about a TRC10 token using its unique token ID. It returns an object containing the token's name, abbreviation, total supply, precision, owner address, and other metadata.
    // Usage: tronWeb.trx.getTokenByID(tokenID);
    
    // Example: Retrieve info for token ID 1002000
    const tokenInfo = await tronWeb.trx.getTokenByID(1002000);
    console.log(tokenInfo);
    
    /* 
    Expected Output:
    {
      owner_address: '4137fa1a56eb8c503624701d776d95f6dae1d9f0d6',
      name: 'BitTorrent',
      abbr: 'BTT',
      total_supply: 990000000000000000,
      trx_num: 1,
      precision: 6,
      num: 1,
      start_time: 1548000000000,
      end_time: 1548000001000,
      description: 'Official Token of BitTorrent Protocol',
      url: 'www.bittorrent.com',
      id: '1002000'
    }
    */
  2. Query TRC10 tokens issued by an account address

    Use the tronWeb.trx.getTokensIssuedByAddress method to retrieve information about TRC10 tokens created by a specific account. The method accepts an address in either HexString or Base58 format and returns an object containing the token details (such as name, abbreviation, total supply, and owner address) keyed by the token name.
    // Example using Base58 address
    const tokens = await tronWeb.trx.getTokensIssuedByAddress("TF5Bn4cJCT6GVeUgyCN4rBhDg42KBrpAjg");
    console.log(tokens);
    
    // Example using HexString address
    const tokensHex = await tronWeb.trx.getTokensIssuedByAddress("4137fa1a56eb8c503624701d776d95f6dae1d9f0d6");
    console.log(tokensHex);
  3. Retrieve transaction details using getTransactionInfo

    Use tronWeb.trx.getTransactionInfo(transactionID) to get detailed information about a specific transaction. This includes the transaction fees, block number, timestamp, contract results, receipt (energy usage and status), logs, and internal transactions or VM events.
    const transactionID = "0daa9f2507c4e79e39391ea165bb76ed018c4cd69d7da129edf9e95f0dae99e2";
    const info = await tronWeb.trx.getTransactionInfo(transactionID);
    console.log(info);
  4. Understand TRON Bandwidth and Energy resources

    Every transaction on TRON consumes resources to prevent network spam and manage computational load:

    • Bandwidth: Used for the data size of all transactions. It can be obtained via a daily free allowance or by freezing TRX. If an account lacks sufficient bandwidth, TRX is burned as a transaction fee.
    • Energy: Required specifically for smart contract operations (deployment and triggering). Energy is obtained only by freezing TRX, and the cost depends on the contract's computational complexity.
  5. Prevent precision loss using tronWeb.trx.raw

    The tronWeb.trx.raw namespace mirrors the chain-reading methods of tronWeb.trx but issues GET requests with int64_as_string=true. This ensures that protobuf int64/uint64 fields are serialized as JSON strings, preventing the precision loss that occurs when JavaScript parses large numbers into IEEE-754 doubles (which happens for values above 2^53 - 1 or Number.MAX_SAFE_INTEGER).

    Only int64/uint64 fields are converted to strings; proto int32 and enum fields remain as numbers.

    // Using trx.raw: int64 fields arrive as strings (exact precision)
    const balance = await tronWeb.trx.raw.getBalance('TTSFjEG3Lu9WkHdp4JrWYhbGP6K1REqnGQ');
    console.log(balance); // '9223372036854775807'
    
    // Using regular trx: returns a number (lossy above Number.MAX_SAFE_INTEGER)
    const lossyBalance = await tronWeb.trx.getBalance('TTSFjEG3Lu9WkHdp4JrWYhbGP6K1REqnGQ');
    console.log(lossyBalance); // 9223372036854776000
  6. Use BigNumber for precise arithmetic in TronWeb

    The BigNumber class (bundled from bignumber.js) is used to handle large numbers or hexadecimal strings to avoid precision loss during arithmetic operations. It is primarily intended for calculations and display, not for direct transmission to TronWeb APIs.
    import { BigNumber } from 'tronweb';
    const value = new BigNumber('200000000000000000000001');
  7. Retrieve TRON node information using getNodeInfo

    The tronWeb.trx.getNodeInfo() method returns detailed information about the connected TRON node. This includes the node's synchronization status, configuration settings, machine hardware statistics (CPU, memory, OS), and a list of connected peers.
    const nodeInfo = await tronWeb.trx.getNodeInfo();
    console.log(nodeInfo);
  8. Retrieve TRC10 token details by ID using getTokenFromID

    The tronWeb.trx.getTokenFromID method allows you to query detailed information about a TRC10 token using its numeric token ID. The returned object includes the token's name, abbreviation, total supply, precision, owner address, and other metadata.
    // You can pass the token ID as either a Number or a String
    const tokenInfo = await tronWeb.trx.getTokenFromID(1002000);
    
    // Example Output:
    // {
    //   owner_address: '4137fa1a56eb8c503624701d776d95f6dae1d9f0d6',
    //   name: 'BitTorrent',
    //   abbr: 'BTT',
    //   total_supply: 990000000000000000,
    //   trx_num: 1,
    //   precision: 6,
    //   num: 1,
    //   start_time: 1548000000000,
    //   end_time: 1548000001000,
    //   description: 'Official Token of BitTorrent Protocol',
    //   url: 'www.bittorrent.com',
    //   id: '1002000'
    // }
  9. Understand the TRON transaction lifecycle

    Sending a transaction involves five distinct steps. A common mistake is reporting 'Success' after Step 3; however, the actual contract execution result is only known at Step 5.

    1. Simulate & Build: Dry-run the call to check for reverts and build the unsigned transaction.
    2. Sign: The user confirms the transaction in their wallet.
    3. Broadcast: The signed transaction is submitted to a node. Success here only means the node accepted the transaction.
    4. Confirm: The transaction is packed into a block and solidified.
    5. Result: The execution result is read (e.g., checking for REVERT or OUT_OF_ENERGY).
  10. Retrieve the latest block information using getCurrentBlock

    Use tronWeb.trx.getCurrentBlock() to retrieve detailed information about the most recent block on the TRON network. The returned object includes the block ID, the block header (containing the block number, timestamp, and parent hash), and a list of all transactions contained within that block.
    const block = await tronWeb.trx.getCurrentBlock();
    console.log(block);
    
    /* 
    Example Output:
    {
      blockID: '00000000000dc73a584b91846ed22d8359f1cf45407c9159b4bac9f1e2b018de',
      block_header: {
        raw_data: {
          number: 902970,
          txTrieRoot: '6ada20705dc3b74d089dc8188792d0162747d634fd6deec60717c1b4fc518af0',
          witness_address: '41f16412b9a17ee9408646e2a21e16478f72ed1e95',
          parentHash: '00000000000dc739de1b856885cd8b5a8837bbd0cfd433833bee19e7b5466bd1',
          version: 9,
          timestamp: 1578303678000
        },
        witness_signature: 'b81af83a8a0c65bfa7d9224a0ba3c653dce338f1e2147e68d3a7b66194dfaffc719f7d03db719b1065059bdcdbe97652b28662df594279db849000b69c222fc900'
      },
      transactions: [
        {
          txID: '90be204670e96c06ed4e0e0a4d456afe6cec09febde545b097dbdad1f05dfe0c',
          raw_data_hex: '...',
          // ... other transaction fields
        }
      ]
    }
    */