Skip to Content
dApps

wallet_watchAsset

Fordert die Wallet auf, einen neuen Token zu verfolgen. Der Nutzer wird aufgefordert, den angegebenen Token zur Liste der in seiner Wallet verfolgten Tokens hinzuzufügen.


Parameter

object mit:

FeldTypErforderlichBeschreibung
typestringJaToken-Typ; diese Seite zeigt ERC20
optionsobjectJaToken-Details
options.addressstringJaToken-Vertragsadresse
options.symbolstringJaToken-Symbol (1-11 Zeichen)
options.decimalsnumberJaDezimalstellen des Tokens
options.imagestringNeinURL des Token-Bilds

Rückgabe

boolean - true, wenn der Token hinzugefügt wurde, false andernfalls


Beispiel

const wasAdded = await window.$onekey.ethereum.request({ method: 'wallet_watchAsset', params: { type: 'ERC20', options: { address: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', symbol: 'USDC', decimals: 6, image: 'https://cryptologos.cc/logos/usd-coin-usdc-logo.png' } } }) if (wasAdded) { console.log('Token added to wallet') } else { console.log('User declined to add token') }

Mehrere Tokens hinzufügen

const tokens = [ { address: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', symbol: 'USDC', decimals: 6 }, { address: '0xdAC17F958D2ee523a2206206994597C13D831ec7', symbol: 'USDT', decimals: 6 }, { address: '<TOKEN_CONTRACT_ADDRESS>', symbol: 'DAI', decimals: 18 } ] async function addTokens(tokenList) { for (const token of tokenList) { try { await window.$onekey.ethereum.request({ method: 'wallet_watchAsset', params: { type: 'ERC20', options: token } }) } catch (error) { console.error(`Failed to add ${token.symbol}:`, error) } } }

Eigenen Projekt-Token hinzufügen

async function suggestToken() { const chainId = await window.$onekey.ethereum.request({ method: 'eth_chainId' }) // Different addresses per network const tokenAddresses = { '0x1': '0x...MainnetAddress', '0x89': '0x...PolygonAddress', '0xa4b1': '0x...ArbitrumAddress' } const address = tokenAddresses[chainId] if (!address) { throw new Error('Token not available on this network') } return window.$onekey.ethereum.request({ method: 'wallet_watchAsset', params: { type: 'ERC20', options: { address, symbol: 'TOKEN', decimals: 18, image: 'https://example.com/token-logo.png' } } }) }

Fehler

CodeNachrichtBeschreibung
4001Nutzer hat die Anfrage abgelehntNutzer hat das Hinzufügen des Tokens abgelehnt
-32602Ungültige ParameterUngültige Token-Parameter

Hinweise

  • Diese Seite behandelt ERC20; prüfen Sie jeden anderen Token-Typ anhand der Ziel-Wallet-Version
  • Die Wallet verfolgt den Token möglicherweise bereits (gibt dennoch zurück: true)
  • Die Bild-URL sollte HTTPS verwenden und erreichbar sein
  • Für ein gutes Nutzererlebnis sollte das Symbol dem On-Chain-Symbol entsprechen
  • Spezifiziert durch EIP-747 
Zuletzt aktualisiert am