This TypeScript module is maintained in the style of the MetaMask team.
yarn add @metamask/connect-tron
or
npm install @metamask/connect-tron
or
bun add @metamask/connect-tron
If your application already uses tronwallet-adapter, you can simply add this adapter to your existing list of adapters.
This adapter follows the default tronwallet adapter interface, so there is nothing specific to configure.
import { TronLinkAdapter } from 'tronwallet-adapter';
import { MetaMaskAdapter } from '@metamask/connect-tron';
const adapters = [
new TronLinkAdapter(),
new MetaMaskAdapter(), // Add MetaMask adapter
];
// Use the adapters list as usual in your appYou can now use MetaMask as a TRON wallet in your application without any further changes.
import { MetaMaskAdapter } from '@metamask/connect-tron';
import TronWeb from 'tronweb';
const tronWeb = new TronWeb({
fullHost: 'https://api.trongrid.io',
headers: { 'TRON-PRO-API-KEY': 'your api key' },
});
const adapter = new MetaMaskAdapter();
await adapter.connect();
// then you can get address
console.log(adapter.address);
// create a send TRX transaction
const unSignedTransaction = await tronWeb.transactionBuilder.sendTrx(targetAddress, 100, adapter.address);
// using adapter to sign the transaction
const signedTransaction = await adapter.signTransaction(unSignedTransaction);
// broadcast the transaction
await tronWeb.trx.sendRawTransaction(signedTransaction);The MetaMaskAdapter implements the standard TronWallet Adapter interface with the following properties and methods:
name: The adapter name ('MetaMask')url: MetaMask website URLicon: Base64 encoded icon data URLreadyState: Current wallet ready state (Loading,Found, orNotFound)state: Current adapter state (DisconnectorConnected)address: Connected wallet address (ornullif not connected)connecting: Boolean indicating if connection is in progressconnected: Boolean indicating if wallet is connected (computed property)
Connects to the MetaMask wallet.
await adapter.connect();Disconnects from the wallet.
await adapter.disconnect();Signs a message with the connected wallet.
const signature = await adapter.signMessage('Hello, TRON!');Signs a transaction with the connected wallet.
const signedTx = await adapter.signTransaction(unsignedTransaction);Switches to a different blockchain network.
await adapter.switchChain('0x2b6653dc'); // MainnetmultiSign(): Multi-signature operations are not supported by this adapter.
The adapter emits the following events:
connect: Emitted when wallet is connecteddisconnect: Emitted when wallet is disconnectedaccountsChanged: Emitted when the active account changeschainChanged: Emitted when the network/chain changesreadyStateChanged: Emitted when the wallet's ready state changesstateChanged: Emitted when the adapter state changeserror: Emitted when an error occurs