Skip to main content

wallet_watchAsset

Request UKey Wallet to add a token to the user's wallet asset list. After calling, the wallet will display a confirmation interface, and the user can decide whether to add it.


Inputs

Pass a single object with the fields below:

FieldVariantNeedRemarks
typestringyesAsset standard; currently ERC20 only
optionsobjectyesAsset metadata object
options.addressstringyesToken contract on the active Network
options.symbolstringyesToken ticker, 1-11 chars
options.decimalsnumberyesToken decimals
options.imagestringnoToken icon URL

return value

boolean - ​​Returns true if the addition is successful, false if the user refuses or does not add it.


Demo

const wasAdded = await window.$ukey.ethereum.request({
method: "wallet_watchAsset",
requestParams: {
type: "ERC20",
options: {
address: "0x2b4d6f8091a3c5e7f9b1d3f507192b4d6f8091a3",
symbol: "TOKA",
decimals: 6,
image: "https://cryptologos.cc/logos/usd-coin-usdc-logo.png",
},
},
});

if (wasAdded) {
console.log("Token was added to the wallet");
} else {
console.log("The user declined adding the token");
}

Add multiple tokens

const tokens = [
{
address: "0x2b4d6f8091a3c5e7f9b1d3f507192b4d6f8091a3",
symbol: "TOKA",
decimals: 6,
},
{
address: "0x4b6d8f0123456789abcdef0123456789abcdef01",
symbol: "TOKB",
decimals: 6,
},
{
address: "0xc4d2e1f0a9876543210fedcba9876543210abcde",
symbol: "TOKC",
decimals: 18,
},
];

async function registerTokenBatch(tokenList) {
for (const token of tokenList) {
try {
await window.$ukey.ethereum.request({
method: "wallet_watchAsset",
requestParams: {
type: "ERC20",
options: token,
},
});
} catch (error) {
console.error(`Failed to add ${token.symbol}:`, error);
}
}
}

Add project tokens by current network

async function requestTokenTracking() {
const chainId = await window.$ukey.ethereum.request({
method: "eth_chainId",
});

// Note: Token addresses for different networks
const tokenAddresses = {
"0x1": "0x2b4d6f8091a3c5e7f9b1d3f507192b4d6f8091a3",
"0x89": "0x4b6d8f0123456789abcdef0123456789abcdef01",
"0xa4b1": "0xc4d2e1f0a9876543210fedcba9876543210abcde",
};

const address = tokenAddresses[chainId];
if (!address) {
throw new Error("This token is not available on this network");
}

return window.$ukey.ethereum.request({
method: "wallet_watchAsset",
requestParams: {
type: "ERC20",
options: {
address,
symbol: "TOKEN",
decimals: 18,
image: "https://demo.example/token-logo.png",
},
},
});
}

error code

CodeMessageDetails
4001User rejected the requestToken addition was rejected by the user
-32602Parameters failed validationToken metadata request is invalid

illustrate

  • Currently only the ERC20 type is supported.
  • Before adding, please select the correct token contract address based on the current eth_chainId.
  • The wallet may already be tracking the token and may still return true at this time.
  • Image URLs should use HTTPS and be accessible to the user environment.
  • Token symbols, accuracy, and on-chain information should be consistent to avoid users misidentifying assets.
  • Follows EIP-747 specification.