Skip to Content
dAppsTron

TRON

OneKey の TronLink 互換プロバイダーで TRON ブロックチェーンを統合します。アクセス先: window.$onekey.tron または window.tronWeb.

OneKey は TronLink 互換プロバイダーと TronWeb 統合を公開しています。機能の利用可否はウォレットのバージョンと選択中のネットワークによって異なります。



プロバイダーの検出

// 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) // true

クイックスタート

アカウントを要求

// 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 if TronWeb is ready if (provider.ready && tronWeb && tronWeb.defaultAddress.base58) { console.log('Connected:', tronWeb.defaultAddress.base58) } else { console.log('Not connected') }

TronWeb の使用

接続後は provider.tronWeb を使用し、リクエストを OneKey に関連付けたままにします:

残高を取得

const balance = await tronWeb.trx.getBalance(tronWeb.defaultAddress.base58) console.log('Balance:', tronWeb.fromSun(balance), 'TRX')

TRX を送信

const tx = await tronWeb.trx.sendTransaction( 'TRecipientAddress...', tronWeb.toSun(10) // 10 TRX ) console.log('Transaction:', tx.txid)

TRC20 トークンを送信

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)

トランザクション

トランザクションに署名

ブロードキャストせずにトランザクションに署名します:

// 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)

スマートコントラクト操作

// 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, })

スマートコントラクトを実行

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)

メッセージ署名

メッセージに署名 V1 (16 進数)

16 進数エンコードされたメッセージに署名します:

const message = tronWeb.toHex('Hello TRON!') const signature = await provider.signMessage(message) console.log('Signature:', signature)

メッセージに署名 V2 (UTF-8)

UTF-8 メッセージに直接署名します:

const message = 'Hello TRON!' const signature = await provider.signMessageV2(message) console.log('Signature:', 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)

イベント処理

アカウント変更を監視

provider.on('accountsChanged', (accounts) => { if (accounts[0]) { console.log('Account changed:', accounts[0]) } else { console.log('Disconnected') } })

ネットワーク変更を監視

provider.on('chainChanged', (chainId) => { console.log('Network changed:', chainId) // Recommend page reload for network changes })
// 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 リファレンス

プロバイダーメソッド

メソッド説明
request({ method, params })汎用 JSON-RPC リクエスト
sign(transaction)トランザクションに署名
signMessage(hexMessage)16 進数メッセージに署名 (V1)
signMessageV2(message)UTF-8 メッセージに署名 (V2)

リクエストメソッド

メソッド説明
tron_requestAccounts接続を要求
tron_getProviderStateプロバイダーの状態を取得
tron_signTransactionトランザクションに署名
signMessageV1メッセージに署名 V1
signMessageV2メッセージに署名 V2

レスポンスコード

tron_requestAccounts では、解決されたレスポンスの code を確認してください。現在のプロバイダー実装では 4001 はすでに保留中のリクエストを表し、4000 はアカウントが返らない場合やリクエスト失敗時に使用します。これらは Ethereum JSON-RPC エラーとは意味が異なります。

コード説明
200成功
4000ユーザーが拒否
4001リクエストはキュー内

イベント

イベント説明
accountsChangedアカウント変更時
chainChangedネットワーク変更時
connect接続済み
disconnect切断時

SunWeb の使用

DappChain (サイドチェーン) の操作向け:

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 )

エラー処理

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) }

OneKey は TronLink と互換性があります。検出ロジックを更新してください:

// 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
最終更新日