Skip to Content
dAppsConflux

Conflux

OneKey の Fluent 互換プロバイダーで Conflux ブロックチェーンを統合します。アクセス先: window.$onekey.conflux.

OneKey の Conflux プロバイダーは、ConfluxPortal と Fluent の両ウォレットインターフェースと互換性があります。



プロバイダーの検出

// Detect OneKey Conflux provider const provider = window.$onekey?.conflux if (!provider) { throw new Error('OneKey Conflux provider not detected') } // Check compatibility flags console.log('isConfluxPortal:', provider.isConfluxPortal) // true console.log('isFluent:', provider.isFluent) // true console.log('isOneKey:', provider.isOneKey) // true

クイックスタート

アカウントを要求

const accounts = await provider.request({ method: 'cfx_requestAccounts' }) console.log('Connected:', accounts[0])

接続を確認

if (provider.isConnected()) { const accounts = await provider.request({ method: 'cfx_accounts' }) console.log('Accounts:', accounts) }

アカウントとネットワーク

チェーン ID を取得

const chainId = await provider.request({ method: 'cfx_chainId' }) console.log('Chain ID:', chainId) // e.g., '0x405' for mainnet

ネットワークバージョンを取得

const networkId = await provider.request({ method: 'net_version' }) console.log('Network ID:', networkId) // e.g., '1029' for mainnet

残高を取得

const balance = await provider.request({ method: 'cfx_getBalance', params: [address, 'latest_state'] }) console.log('Balance:', balance) // In Drip (1 CFX = 10^18 Drip)

トランザクション

トランザクションを送信

const txHash = await provider.request({ method: 'cfx_sendTransaction', params: [{ from: senderAddress, to: recipientAddress, value: '0xDE0B6B3A7640000', // 1 CFX in Drip (hex) gas: '0x5208', // 21000 }] }) console.log('Transaction hash:', txHash)

ガスの見積もり付きで送信

// Estimate gas first const gasEstimate = await provider.request({ method: 'cfx_estimateGasAndCollateral', params: [{ from: senderAddress, to: recipientAddress, value: '0xDE0B6B3A7640000', }] }) // Send with estimated values const txHash = await provider.request({ method: 'cfx_sendTransaction', params: [{ from: senderAddress, to: recipientAddress, value: '0xDE0B6B3A7640000', gas: gasEstimate.gasLimit, storageLimit: gasEstimate.storageCollateralized, }] })

コントラクトを呼び出す

const result = await provider.request({ method: 'cfx_call', params: [{ to: contractAddress, data: encodedFunctionCall, }, 'latest_state'] }) console.log('Result:', result)

イベント処理

アカウント変更を監視

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

チェーン変更を監視

provider.on('chainChanged', (chainId) => { console.log('Chain changed:', chainId) // Recommend reloading the page window.location.reload() })

接続を監視

provider.on('connect', ({ chainId, networkId }) => { console.log('Connected to:', chainId, networkId) }) provider.on('disconnect', () => { console.log('Disconnected') })

js-conflux-sdk との併用

セットアップ

npm install js-conflux-sdk

プロバイダーに接続

import { Conflux } from 'js-conflux-sdk' const conflux = new Conflux({ url: 'https://main.confluxrpc.com', networkId: 1029, }) // Use OneKey provider for signing conflux.provider = provider

SDK でトランザクションを送信

// Create and send transaction const tx = conflux.cfx.sendTransaction({ from: senderAddress, to: recipientAddress, value: Conflux.Drip.fromCFX(1), // 1 CFX }) console.log('Transaction:', tx) // Wait for confirmation const receipt = await tx.executed() console.log('Receipt:', receipt)

コントラクト操作

const contract = conflux.Contract({ abi: contractABI, address: contractAddress, }) // Read const result = await contract.someViewFunction().call() // Write const tx = contract.someWriteFunction(param1, param2).sendTransaction({ from: senderAddress, }) const receipt = await tx.executed()

API リファレンス

メソッド

メソッド説明
request({ method, params })JSON-RPC リクエストを送信
isConnected()接続状態を確認
on(event, callback)イベントを購読
off(event, callback)イベントの購読を解除

RPC メソッド

メソッド説明
cfx_requestAccounts接続を要求
cfx_accounts接続済みアカウントを取得
cfx_chainIdチェーン ID を取得
cfx_sendTransactionトランザクションを送信
cfx_callコントラクトを呼び出す (読み取り専用)
cfx_estimateGasAndCollateralガスを見積もる
cfx_getBalanceアカウント残高を取得
cfx_getTransactionByHashトランザクションを取得
cfx_getTransactionReceiptレシートを取得
net_versionネットワーク ID を取得

イベント

イベントパラメーター説明
connect{ chainId, networkId }ネットワーク接続時
disconnect-切断時
chainChangedchainIdネットワーク変更時
accountsChangedaccounts[]アカウント変更時

ネットワーク

ネットワークチェーン ID (16 進数)ネットワーク ID
メインネット0x4051029
テストネット0x11

非推奨のメソッド

以下のメソッドは引き続き動作しますが、非推奨です:

// Deprecated: use request({ method: 'cfx_requestAccounts' }) await provider.enable() // Deprecated: use request() await provider.send({ method: 'cfx_accounts' }) await provider.sendAsync({ method: 'cfx_accounts' }, callback) // Deprecated: use request({ method: 'cfx_chainId' }) provider.chainId // Deprecated: use request({ method: 'net_version' }) provider.networkVersion // Deprecated: use request({ method: 'cfx_accounts' }) provider.selectedAddress

エラー処理

try { const accounts = await provider.request({ method: 'cfx_requestAccounts' }) } catch (error) { if (error.code === 4001) { console.log('User rejected the request') } else if (error.code === -32603) { console.log('Internal error') } else { console.error('Error:', error.message) } }

主なエラーコード

コード説明
4001ユーザーがリクエストを拒否
-32700解析エラー
-32600無効なリクエスト
-32601メソッドが見つからない
-32602無効なパラメーター
-32603内部エラー

アドレス形式

Conflux は base32 アドレス (CIP-37) を使用します:

// Mainnet: cfx:... // Testnet: cfxtest:... // Example mainnet address const address = 'cfx:aak2rra2njvd77ezwjvx04kkds9fzagfe6ku8scz91' // Convert between formats using js-conflux-sdk import { format } from 'js-conflux-sdk' const hexAddress = format.hexAddress(address) const base32Address = format.address(hexAddress, 1029) // networkId
最終更新日