On this page
Getting Started
Connect the provider and use the TronWeb instance associated with that same wallet. Replace abbreviated addresses and contract methods with valid values. Check the selected network before preparing a transaction.
Learn how to detect, connect, and interact with TRON using OneKey’s provider.
OneKey exposes a TronLink-compatible provider and TronWeb integration. Feature availability depends on the wallet version and selected network.
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) // trueRequest 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)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)
}
})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 itLast updated on