TRON
Integrate TRON blockchain using OneKey’s TronLink-compatible provider. Access via window.$onekey.tron or window.tronWeb.
OneKey’s TRON provider is fully compatible with TronLink, including TronWeb and SunWeb instances.
Quick Links
Provider Detection
// OneKey injects both the provider and TronWeb instances
const provider = window.$onekey?.tron
// TronWeb is available globally after connection
const tronWeb = window.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) {
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 (tronWeb && tronWeb.ready) {
console.log('Connected:', tronWeb.defaultAddress.base58)
} else {
console.log('Not connected')
}Using TronWeb
After connection, TronWeb instance is available globally:
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 = 100 * (10 ** decimals) // 100 tokens
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.data.isTronLink) {
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
| 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 !== 200) {
throw new Error(result.message)
}
} catch (error) {
if (error.code === 4001) {
console.log('User rejected the request')
} else {
console.error('Error:', error.message)
}
}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 // Both inject thisLast updated on