Skip to Content
dApps

Erste Schritte

Aktivieren Sie die Erweiterung, wählen Sie ein Konto aus und verbinden Sie einen API-Client mit der vorgesehenen Chain. Erstellen Sie Transaktionsnutzlasten aus aktuellen Chain-Metadaten; die verkürzten Hexwerte in dieser Anleitung sind Platzhalter.

Erfahren Sie, wie Sie Polkadot- und Substrate-Chains erkennen, verbinden und damit interagieren.

OneKey implementiert die Polkadot.js-Erweiterungsschnittstelle für die hier dokumentierten Vorgänge.


Provider erkennen

// OneKey registers as a Polkadot.js extension const provider = window.$onekey?.polkadot // Or use the standard web3Enable API import { web3Enable, web3Accounts } from '@polkadot/extension-dapp'

Erweiterung aktivieren

// Using OneKey provider directly const enabled = await provider.web3Enable('My dApp') if (enabled) { const accounts = await provider.web3Accounts() console.log('Accounts:', accounts) }

import { web3Enable, web3Accounts } from '@polkadot/extension-dapp' // Enable all extensions (including OneKey) const extensions = await web3Enable('My dApp') if (extensions.length === 0) { throw new Error('No extension found') } // Get all accounts const allAccounts = await web3Accounts() console.log('Accounts:', allAccounts)

Konten abrufen

const accounts = await provider.web3Accounts() accounts.forEach(account => { console.log({ address: account.address, // Substrate address name: account.name, // Account name type: account.type, // 'sr25519' | 'ed25519' | 'ecdsa' genesisHash: account.genesisHash, // Optional chain genesis }) })

Kontoänderungen abonnieren

const unsubscribe = provider.web3AccountsSubscribe((accounts) => { console.log('Accounts changed:', accounts) }) // Later: unsubscribe()

Mit der Polkadot.js-API verwenden

Einrichtung

npm install @polkadot/api @polkadot/extension-dapp

Verbinden und Transaktion signieren

import { ApiPromise, WsProvider } from '@polkadot/api' import { web3Enable, web3FromAddress, web3Accounts } from '@polkadot/extension-dapp' // Enable extensions await web3Enable('My dApp') // Get accounts const accounts = await web3Accounts() const account = accounts[0] if (!account) throw new Error("No account was selected") // Connect to chain const wsProvider = new WsProvider('wss://rpc.polkadot.io') const api = await ApiPromise.create({ provider: wsProvider }) // Get injector const injector = await web3FromAddress(account.address) // Create and sign transaction const tx = api.tx.balances.transferKeepAlive( recipientAddress, '10000000000' // 1 DOT = 10^10 planck on Polkadot ) // Sign and send const hash = await tx.signAndSend( account.address, { signer: injector.signer } ) console.log('Transaction hash:', hash.toHex())
Zuletzt aktualisiert am