@dynamic-labs/tron
v4.50.3
Published
A React SDK for implementing wallet web3 authentication and authorization to your website.
Downloads
3,772
Readme
@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/tronSupported Wallets
This package supports the following Tron wallets through TronWallet Adapter:
| Wallet | Type | Description | | ----------------- | ----------------- | ------------------------------------ | | OKX Wallet | Browser Extension | Multi-chain wallet with Tron support | | Bitget Wallet | Browser Extension | Formerly BitKeep | | TokenPocket | Mobile/Extension | Multi-chain wallet | | Trust Wallet | Mobile/Extension | Popular 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 addressgetNetwork(): Promise<string | number | undefined>- Get current networksignMessage(message: string | Uint8Array): Promise<string>- Sign a messagegetBalance(address?: string): Promise<string | undefined>- Get TRX balancesendTrx(to: string, amount: number, options?: { from?: string }): Promise<BroadcastReturn>- Send TRXgetTokenBalance(tokenId: string, address?: string): Promise<number>- Get TRC20 token balancegetNetworkDetails(): Promise<{ chainId: string; name: string }>- Get network infogetTronWeb(): 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:
- Adapter Layer: Each wallet connector wraps a TronWallet Adapter instance
- Unified Interface: All wallets expose the same methods and events
- Event Handling: Standardized events for connect, disconnect, account changes, etc.
- 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 tronTest
npx nx test tronLint
npx nx lint tronTypeScript 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
- TronWallet Adapter Docs: https://walletadapter.org/docs/
- Dynamic Docs: https://docs.dynamic.xyz
- TronWeb Docs: https://developers.tron.network/docs
- Tron Network: https://tron.network
Support
- Documentation: docs.dynamic.xyz
- GitHub Issues: Report bugs or request features
- Discord: Join our community
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
