Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.dynamic.xyz/docs/llms.txt

Use this file to discover all available pages before exploring further.

getTronWeb

Retrieves a TronWeb instance from a wallet account, allowing you to interact with the Tron blockchain using the full TronWeb API.

Usage

import { getTronWeb, isTronWalletAccount } from "@dynamic-labs-sdk/tron";
import { getPrimaryWalletAccount } from "@dynamic-labs-sdk/client";

const walletAccount = getPrimaryWalletAccount();

if (walletAccount && isTronWalletAccount(walletAccount)) {
  const tronWeb = await getTronWeb({ walletAccount });

  // Use TronWeb to interact with the Tron blockchain
  const balance = await tronWeb.trx.getBalance(walletAccount.address);
  console.log("Balance in SUN:", balance);

  // Convert to TRX
  const balanceInTrx = tronWeb.fromSun(balance);
  console.log("Balance in TRX:", balanceInTrx);
}

Parameters

ParameterTypeDescription
walletAccountTronWalletAccountThe wallet account to get the TronWeb instance for
clientDynamicClient (optional)The Dynamic client instance. Only required when using multiple clients.

Returns

Promise<TronWeb> - A promise that resolves to a TronWeb instance from the tronweb package.

Errors

ErrorDescription
NotTronProviderErrorThrown if the wallet account’s provider is not a Tron provider

React

import { getTronWeb, isTronWalletAccount } from '@dynamic-labs-sdk/tron';
import { useWalletAccounts } from '@dynamic-labs-sdk/react-hooks';
import { useEffect, useState } from 'react';

function TronBalance() {
  const walletAccounts = useWalletAccounts();
  const walletAccount = walletAccounts.find(isTronWalletAccount);
  const [balance, setBalance] = useState('');

  useEffect(() => {
    if (!walletAccount) return;
    getTronWeb({ walletAccount }).then(async (tronWeb) => {
      const bal = await tronWeb.trx.getBalance(walletAccount.address);
      setBalance(tronWeb.fromSun(bal));
    });
  }, [walletAccount]);

  return <p>TRX balance: {balance}</p>;
}