@dynamic-labs/tron

中文文档
5.16.0发布于 2026/11/11MITTypeScriptESM + CJS近 30 天下载 1.2万

A React SDK for implementing wallet web3 authentication and authorization to your website.

原始使用文档
文档目录24 节
  1. Overview
  2. Installation
  3. Supported Wallets
  4. Usage
  5. Basic Integration
  6. Working with Tron Wallets
  7. Accessing TronWeb
  8. Signing Messages
  9. Working with TRC20 Tokens
  10. Network Information
  11. Supported Networks
  12. Custom Networks
  13. API Reference
  14. TronWallet Class
  15. isTronWallet()
  16. Architecture
  17. Development
  18. Build
  19. Test
  20. Lint
  21. TypeScript Support
  22. Resources
  23. Support
  24. License

@dynamic-labs/tron

Tron blockchain integration for the Dynamic SDK using the industry-standard TronWallet Adapter.

Overview

This package provides seamless integration with Tron wallets in your dApp using the TronWallet Adapter standard. By leveraging the TronWallet Adapter, you get access to 9+ Tron wallets through a single, unified interface with automatic wallet detection, connection management, and standardized transaction handling.

Installation

npm install @dynamic-labs/tron

Supported Wallets

This package supports the following Tron wallets through TronWallet Adapter:

WalletTypeDescription
OKX WalletBrowser ExtensionMulti-chain wallet with Tron support
Bitget WalletBrowser ExtensionFormerly BitKeep
TokenPocketMobile/ExtensionMulti-chain wallet
Trust WalletMobile/ExtensionPopular multi-chain wallet

Usage

Basic Integration

Integrate all Tron wallets into your Dynamic configuration:

import { DynamicContextProvider } from '@dynamic-labs/sdk-react-core';
import { TronWalletConnectors } from '@dynamic-labs/tron';

function App() {
  return (
    <DynamicContextProvider
      settings={{
        environmentId: 'your-environment-id',
        walletConnectors: [TronWalletConnectors()],
      }}
    >
      {/* Your app content */}
    </DynamicContextProvider>
  );
}

Working with Tron Wallets

Use the isTronWallet type guard to safely access Tron-specific functionality:

import { useDynamicContext } from '@dynamic-labs/sdk-react-core';
import { isTronWallet } from '@dynamic-labs/tron';

function MyComponent() {
  const { primaryWallet } = useDynamicContext();

  const handleSendTrx = async () => {
    if (!isTronWallet(primaryWallet)) {
      console.error('Not a Tron wallet');
      return;
    }

    try {
      // Send 10 TRX
      const result = await primaryWallet.sendTrx('TRecipientAddress...', 10);
      console.log('Transaction sent:', result.txid);
    } catch (error) {
      console.error('Transaction failed:', error);
    }
  };

  return <button onClick={handleSendTrx}>Send TRX</button>;
}

Accessing TronWeb

Get direct access to the TronWeb instance:

import { useDynamicContext } from '@dynamic-labs/sdk-react-core';
import { isTronWallet } from '@dynamic-labs/tron';

function MyComponent() {
  const { primaryWallet } = useDynamicContext();

  const getTronWebInstance = () => {
    if (!isTronWallet(primaryWallet)) return null;

    const tronWeb = primaryWallet.getTronWeb();
    return tronWeb;
  };

  const checkBalance = async () => {
    const tronWeb = getTronWebInstance();
    if (!tronWeb) return;

    const balance = await tronWeb.trx.getBalance(primaryWallet.address);
    console.log('Balance (in SUN):', balance);
    console.log('Balance (in TRX):', balance / 1_000_000);
  };

  return <button onClick={checkBalance}>Check Balance</button>;
}

Signing Messages

Sign messages with Tron wallets (uses signMessageV2):

import { useDynamicContext } from '@dynamic-labs/sdk-react-core';
import { isTronWallet } from '@dynamic-labs/tron';

function MyComponent() {
  const { primaryWallet } = useDynamicContext();

  const handleSignMessage = async () => {
    if (!isTronWallet(primaryWallet)) return;

    try {
      const message = 'Hello, Tron!';
      const signature = await primaryWallet.signMessage(message);
      console.log('Signature:', signature);

      // Verify the signature
      const tronWeb = primaryWallet.getTronWeb();
      if (tronWeb) {
        const recoveredAddress = await tronWeb.trx.verifyMessageV2(
          message,
          signature,
        );
        console.log('Verified:', recoveredAddress === primaryWallet.address);
      }
    } catch (error) {
      console.error('Signing failed:', error);
    }
  };

  return <button onClick={handleSignMessage}>Sign Message</button>;
}

Working with TRC20 Tokens

Get token balances and interact with TRC20 tokens:

import { useDynamicContext } from '@dynamic-labs/sdk-react-core';
import { isTronWallet } from '@dynamic-labs/tron';

function MyComponent() {
  const { primaryWallet } = useDynamicContext();

  const getUSDTBalance = async () => {
    if (!isTronWallet(primaryWallet)) return;

    // USDT contract address on Tron mainnet
    const usdtContract = 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t';

    try {
      const balance = await primaryWallet.getTokenBalance(usdtContract);
      console.log('USDT Balance:', balance);
    } catch (error) {
      console.error('Failed to get token balance:', error);
    }
  };

  return <button onClick={getUSDTBalance}>Get USDT Balance</button>;
}

Network Information

Get current network details:

import { useDynamicContext } from '@dynamic-labs/sdk-react-core';
import { isTronWallet } from '@dynamic-labs/tron';

function MyComponent() {
  const { primaryWallet } = useDynamicContext();

  const getNetworkInfo = async () => {
    if (!isTronWallet(primaryWallet)) return;

    const network = await primaryWallet.getNetworkDetails();
    console.log('Chain ID:', network.chainId);
    console.log('Network Name:', network.name);
  };

  return <button onClick={getNetworkInfo}>Get Network Info</button>;
}

Supported Networks

The package includes built-in support for:

  • Tron Mainnet (Chain ID: 0x2b6653dc)
  • Nile Testnet (Chain ID: 0x94a9059e)
  • Shasta Testnet (Chain ID: 0x401e93ff)

Custom Networks

You can configure custom networks when creating connectors:

import { TronLinkConnector } from '@dynamic-labs/tron';
import type { GenericNetwork } from '@dynamic-labs/types';

const customNetworks: GenericNetwork[] = [
  {
    blockExplorerUrls: ['https://custom-explorer.com'],
    chainId: '0x12345678',
    chainName: 'Custom Tron Network',
    name: 'Custom Tron Network',
    nativeCurrency: {
      decimals: 6,
      name: 'TRX',
      symbol: 'TRX',
    },
    networkId: '0x12345678',
    rpcUrls: ['https://custom-rpc.example.com'],
    vanityName: 'Custom Network',
  },
];

// Use with connector
const connector = new TronLinkConnector({
  walletBook: walletBookInstance,
  tronNetworks: customNetworks,
});

API Reference

TronWallet Class

The main wallet class for Tron wallets.

Methods

  • getAddress(): Promise<string> - Get the wallet address
  • getNetwork(): Promise<string | number | undefined> - Get current network
  • signMessage(message: string | Uint8Array): Promise<string> - Sign a message
  • getBalance(address?: string): Promise<string | undefined> - Get TRX balance
  • sendTrx(to: string, amount: number, options?: { from?: string }): Promise<BroadcastReturn> - Send TRX
  • getTokenBalance(tokenId: string, address?: string): Promise<number> - Get TRC20 token balance
  • getNetworkDetails(): Promise<{ chainId: string; name: string }> - Get network info
  • getTronWeb(): TronWeb | undefined - Get TronWeb instance

isTronWallet()

Type guard to check if a wallet is a Tron wallet.

function isTronWallet(wallet: Wallet | null | undefined): wallet is TronWallet;

Architecture

This package uses the TronWallet Adapter pattern, which provides:

  1. Adapter Layer: Each wallet connector wraps a TronWallet Adapter instance
  2. Unified Interface: All wallets expose the same methods and events
  3. Event Handling: Standardized events for connect, disconnect, account changes, etc.
  4. Network Detection: Automatic network detection and switching support

The architecture follows Dynamic's wallet connector pattern while leveraging the TronWallet Adapter's battle-tested implementations.

Development

Build

npx nx build tron

Test

npx nx test tron

Lint

npx nx lint tron

TypeScript Support

This package is written in TypeScript and provides full type definitions. Enable strict mode in your tsconfig.json for the best experience:

{
  "compilerOptions": {
    "strict": true
  }
}

Resources

Support

License

This package is part of the Dynamic SDK and follows the same licensing terms. See LICENSE for details


Built with ❤️ by the Dynamic Labs team

版本与发布记录

@dynamic-labs/tron 共发布 207 个版本,首次发布于 2025/09/16;过去 12 个月发布了 178 个版本。

版本发布日期距今
5.16.0latest2026/11/112 天前
5.15.12026/11/103 天前
5.15.02026/11/103 天前
4.104.0v4-lts2026/11/0310 天前
5.14.02026/10/3014 天前
4.103.02026/10/2816 天前

其他发布通道:alpha → 4.83.2-alpha.0,rc → 5.0.0-rc.4,v4-lts → 4.104.0。

关于 @dynamic-labs/tron 的常见问题

@dynamic-labs/tron 是什么?

A React SDK for implementing wallet web3 authentication and authorization to your website.

如何安装 @dynamic-labs/tron?

在项目目录运行 npm install @dynamic-labs/tron,或使用 pnpm add / yarn add / bun add。

@dynamic-labs/tron 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@dynamic-labs/tron 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@dynamic-labs/tron 可以商用吗?

@dynamic-labs/tron 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@dynamic-labs/tron 的下载量有多少?

根据 npm 官方下载统计,@dynamic-labs/tron 在 2026/10/13 – 2026/11/11 这 30 天内被下载 12,465 次,日均约 416 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@dynamic-labs/tron 还在维护吗?

最新版本 5.16.0 发布于 2026/11/11(2 天前);至今共发布 207 个版本,其中过去 12 个月发布了 178 个;首次发布于 2025/09/16。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。