添加代币

概述

DApps 上的按钮允许用户直接将指定的代币添加到他们的 TronLink 用户扩展上的资产列表中。

规格

例子复制

const res = await tronWeb.request({
  method: 'wallet_watchAsset',
  params: {
    type: 'TRC20',
    options: {
      address: 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t'
    }
  },
});

参数复制

interface WatchAssetParams {
  type: 'trc10' | 'trc20' | 'trc721';
  options: {
    address: string;
    symbol?: string;
    decimals?: number;
    image?: string;
  }
}
  • 方法:wallet_watchAsset 固定字符串
  • params:WatchAssetParams,具体参数如下:
    • 类型:目前仅支持“trc10”、“trc20”、“trc721”
    • 选项:
      • address:代币的合约地址或代币id,必填
      • 符号:占位符(当前未使用),可选
      • 小数:占位符(当前未使用),可选
      • 图像:占位符(当前未使用),可选

返回

此方法没有返回值

相互作用

添加 TRC10 资产复制

if (window.tronLink.ready) {
  const tronweb = tronLink.tronWeb;
  try {
    tronweb.request({
      method: 'wallet_watchAsset',
      params: {
        type: 'trc10',
        options: {
          address: '1002000'
        },
      },
    });
  } catch (e) {}
}

代码执行时会弹出TronLink添加TRC10资产的弹窗,用户可以点击“添加”或者“取消”。





Would you like to share your thoughts?

Your email address will not be published. Required fields are marked *