Connecting Tron Web3 Wallet: A Comprehensive Guide for DApp Integration

ยท

Installation and Initialization

To begin integrating OKX Connect with your decentralized application (DApp), ensure you have the latest OKX App version (6.96.0 or higher). Follow these steps:

  1. Install via npm:

    npm install okx-connect
  2. Create a UI interface object for wallet operations:

    const connectUI = new OKXUniversalConnectUI({
      dappMetaData: {
        name: "Your DApp Name",
        icon: "https://yourdomain.com/icon.png" // 180x180 PNG recommended
      },
      actionsConfiguration: {
        modals: ['before'], // Transaction flow modals
        returnStrategy: 'tg://resolve', // For Telegram Mini Apps
        tmaReturnUrl: 'back' // Telegram wallet return behavior
      },
      uiPreferences: {
        theme: "SYSTEM" // THEME.DARK, THEME.LIGHT, or "SYSTEM"
      },
      language: "en_US" // Default language
    });

๐Ÿ‘‰ Get started with Web3 wallet integration

Wallet Connection

Establish connection to retrieve wallet addresses for transaction signing:

const connection = await connectUI.connectWallet({
  connectParams: {
    namespaces: {
      tron: {
        chains: ["tron:mainnet"],
        defaultChain: "tron:mainnet"
      }
    },
    sessionConfig: {
      redirect: "tg://resolve" // Post-connection redirect for Telegram
    }
  }
});

Key Parameters:

Transaction Processing

Creating Tron Provider

const provider = new OKXTronProvider(connectUI);

Account Information Retrieval

const accountInfo = await provider.getAccountInfo("tron:mainnet");
// Returns: { address: "TronWalletAddress" }

Message Signing

Standard Signature:

const signature = await provider.signMessage("Your message", "tron:mainnet");

V2 Signature (Recommended):

const v2Signature = await provider.signMessageV2("Secured message", "tron:mainnet");

Transaction Handling

Signing Only:

const signedTx = await provider.signTransaction(
  { /* Transaction object */ }, 
  "tron:mainnet"
);

Signing and Broadcasting:

const txHash = await provider.signAndSendTransaction(
  { /* Transaction object */ },
  "tron:mainnet"
);

๐Ÿ‘‰ Explore advanced DEX API features

Wallet Disconnection

Terminate active sessions when switching wallets:

await connectUI.disconnect();

Event Handling

Implement listeners for connection state changes:

connectUI.on('connection_update', (event) => {
  console.log('Connection state:', event);
});

Error Reference

Error CodeDescription
1000Unknown error
1001Wallet already connected
1002No active wallet connection
1003User rejected request
1004Method not supported
1005Chain not supported
1006Wallet not supported
1007Connection error

FAQ Section

Q: How do I handle different Tron networks?
A: Specify chain IDs like "tron:mainnet" or "tron:nile" in your connection parameters.

Q: What's the difference between signMessage and signMessageV2?
A: signMessageV2 uses updated cryptographic standards with enhanced security.

Q: Can I customize the wallet connection UI?
A: Yes, through the uiPreferences object during initialization.

Q: How long do wallet sessions remain active?
A: Sessions persist until explicitly disconnected or when the DApp is closed.

Q: Is there a mobile SDK available?
A: Yes, the same npm package supports both web and mobile implementations.

Q: How do I test my integration before going live?
A: Use the Tron Nile testnet ("tron:nile") for development testing.