On this page
TRON
Integrate TRON blockchain using OneKey’s TronLink-compatible provider. Access via window.$onekey.tron or window.tronWeb.
OneKey exposes a TronLink-compatible provider and TronWeb integration. Feature availability depends on the wallet version and selected network.
Quick Links
Provider Detection
// OneKey injects both the provider and TronWeb instances
const provider = window.$onekey?.tron
// TronWeb is available globally after connection
let tronWeb = provider?.tronWeb
if (!provider) {
throw new Error('OneKey TRON provider not detected')
}
// Check if it's TronLink compatible
console.log('isTronLink:', provider.isTronLink) // trueQuick Start
Request Accounts
// Request connection
const result = await provider.request({
method: 'tron_requestAccounts'
})
if (result.code === 200) {
tronWeb = provider.tronWeb
if (!tronWeb) throw new Error('OneKey TronWeb is not ready')
console.log('Connected!')
const address = tronWeb.defaultAddress.base58
console.log('Address:', address)
} else {
console.log('Connection rejected:', result.message)
}Check Connection Status
// Check if TronWeb is ready
if (provider.ready && tronWeb && tronWeb.defaultAddress.base58) {
console.log('Connected:', tronWeb.defaultAddress.base58)
} else {
console.log('Not connected')
}Using TronWeb
After connection, use provider.tronWeb to keep requests associated with OneKey:
Get Balance
const balance = await tronWeb.trx.getBalance(tronWeb.defaultAddress.base58)
console.log('Balance:', tronWeb.fromSun(balance), 'TRX')Send TRX
const tx = await tronWeb.trx.sendTransaction(
'TRecipientAddress...',
tronWeb.toSun(10) // 10 TRX
)
console.log('Transaction:', tx.txid)Send TRC20 Token
const contractAddress = 'TTokenContractAddress...'
const contract = await tronWeb.contract().at(contractAddress)
// Get decimals
const decimals = await contract.decimals().call()
// Transfer tokens
const amount = (100n * 10n ** BigInt(decimals.toString())).toString() // 100 tokens, exact integer
const tx = await contract.transfer(
'TRecipientAddress...',
amount
).send()
console.log('Transaction:', tx)Transactions
Sign Transaction
Sign a transaction without broadcasting:
// Build transaction
const tx = await tronWeb.transactionBuilder.sendTrx(
'TRecipientAddress...',
tronWeb.toSun(10),
tronWeb.defaultAddress.base58
)
// Sign via provider
const signedTx = await provider.sign(tx)
console.log('Signed transaction:', signedTx)
// Broadcast manually
const result = await tronWeb.trx.sendRawTransaction(signedTx)Smart Contract Interaction
// Call contract method
const contract = await tronWeb.contract().at(contractAddress)
// Read (view function)
const result = await contract.someViewFunction().call()
// Write (requires signing)
const tx = await contract.someWriteFunction(param1, param2).send({
feeLimit: 100_000_000, // 100 TRX
callValue: 0,
})Trigger Smart Contract
const tx = await tronWeb.transactionBuilder.triggerSmartContract(
contractAddress,
'transfer(address,uint256)',
{ feeLimit: 100_000_000 },
[
{ type: 'address', value: recipientAddress },
{ type: 'uint256', value: amount }
],
tronWeb.defaultAddress.base58
)
const signedTx = await provider.sign(tx.transaction)
const result = await tronWeb.trx.sendRawTransaction(signedTx)Message Signing
Sign Message V1 (Hex)
Sign hex-encoded messages:
const message = tronWeb.toHex('Hello TRON!')
const signature = await provider.signMessage(message)
console.log('Signature:', signature)Sign Message V2 (UTF-8)
Sign UTF-8 messages directly:
const message = 'Hello TRON!'
const signature = await provider.signMessageV2(message)
console.log('Signature:', signature)Verify Signature
const message = 'Hello TRON!'
const signature = await provider.signMessageV2(message)
// Verify using TronWeb
const address = await tronWeb.trx.verifyMessageV2(message, signature)
console.log('Signer:', address)Event Handling
Listen for Account Changes
provider.on('accountsChanged', (accounts) => {
if (accounts[0]) {
console.log('Account changed:', accounts[0])
} else {
console.log('Disconnected')
}
})Listen for Network Changes
provider.on('chainChanged', (chainId) => {
console.log('Network changed:', chainId)
// Recommend page reload for network changes
})Window Events (TronLink Compatible)
// Listen for TronLink initialization
window.addEventListener('tronLink#initialized', () => {
console.log('TronLink initialized')
})
// Listen for messages
window.addEventListener('message', (event) => {
if (event.source === window && event.data?.isTronLink && event.data?.message) {
const { action, data } = event.data.message
console.log('TronLink event:', action, data)
}
})API Reference
Provider Methods
| Method | Description |
|---|---|
request({ method, params }) | Generic JSON-RPC request |
sign(transaction) | Sign a transaction |
signMessage(hexMessage) | Sign hex message (V1) |
signMessageV2(message) | Sign UTF-8 message (V2) |
Request Methods
| Method | Description |
|---|---|
tron_requestAccounts | Request connection |
tron_getProviderState | Get provider state |
tron_signTransaction | Sign transaction |
signMessageV1 | Sign message V1 |
signMessageV2 | Sign message V2 |
Response Codes
For tron_requestAccounts, inspect the resolved response’s code. The current provider implementation returns 4001 for an already pending request and 4000 when no accounts are returned or the request fails. These are not Ethereum JSON-RPC error meanings.
| Code | Description |
|---|---|
| 200 | Success |
| 4000 | User rejected |
| 4001 | Request in queue |
Events
| Event | Description |
|---|---|
accountsChanged | Account changed |
chainChanged | Network changed |
connect | Connected |
disconnect | Disconnected |
Using SunWeb
For DappChain (side chain) operations:
const sunWeb = window.sunWeb
// Main chain to side chain
await sunWeb.depositTrx(
amount,
depositFee,
feeLimit
)
// Side chain to main chain
await sunWeb.withdrawTrx(
amount,
withdrawFee,
feeLimit
)Error Handling
try {
const result = await provider.request({
method: 'tron_requestAccounts'
})
if (result.code === 4001) {
console.log('A connection request is already pending')
} else if (result.code !== 200) {
console.log('Connection was not approved:', result.message)
}
} catch (error) {
console.error('Request failed:', error)
}Migration from TronLink
OneKey is TronLink-compatible. Update your detection logic:
// Before (TronLink only)
const tronWeb = window.tronWeb
// After (OneKey with TronLink fallback)
const provider = window.$onekey?.tron || window.tronLink
const tronWeb = window.tronWeb // Compatibility global; verify which wallet owns it