添加资产到TronLink

TronLink的tronWeb及tronLink对象提供了API给开发者向TronLink添加Token,使其展示在TronLink资产列表中。

开发者可以在项目中提供按钮给用户, 直接将指定的Token添加到用户TronLink Chrome插件的资产展示列表中。

📘

注:

目前只支持主网和Nile测试网资产添加,不支持shasta测试网资产添加

request函数及参数说明:

await tronWeb.request({
    method: 'wallet_watchAsset',
    params: <WatchAssetParams>,
})

TronWeb请求调用TronLink插件的某个方法。

参数说明
request 接收一个参数,该参数是一个object,包含method和params
method: 调用的TronLink插件的方法,目前支持wallet_watchAsset
params: 上面method方法的参数。如下为wallet_watchAsset的参数说明

interface WatchAssetParams {
  type: 'TRC20'; // 支持的token类型(`trc10`, `trc20`, `trc721`) 
  options: {  address: string;   // token的合约地址 或者 token id, 必传
              symbol?: string;   // 占位(目前未使用),可选 
              decimals?: number; // 占位(目前未使用),可选 
              image?: string;    // 占位(目前未使用),可选
           }; 
}

添加TRC-10资产:

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<script>
        var obj = setInterval(async ()=>{
          //if (window.tronLink.tronWeb) 
            if (window.tronWeb && window.tronWeb.defaultAddress.base58) {
                clearInterval(obj)
              //var tronweb = window.tronLink.tronWeb
                var tronweb = window.tronWeb
                var tx = await tronweb.request({method: 'wallet_watchAsset',
                                                params:{type: 'trc10',
                                                        options: {address: '1002000'},
                                                        },
                                                }
                                               )

            }
        }, 10)
    </script>
</body>
</html>

代码执行时,TronLink会弹出添加窗口,用户点击确定添加TRC10资产,或者取消添加。

1041

点击”添加”按钮,资产被添加到资产列表,如下图所示。

356

添加TRC-20资产:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<script>
        var obj = setInterval(async ()=>{
          //if (window.tronLink.tronWeb) 
            if (window.tronWeb && window.tronWeb.defaultAddress.base58) {
                clearInterval(obj)
              //var tronweb = window.tronLink.tronWeb
                var tronweb = window.tronWeb
                var tx = await tronweb.request({method: 'wallet_watchAsset',
                                                params:{type: 'trc20',
                                                        options: {address: 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t'},
                                                        },
                                                }
                                               )

            }
        }, 10)
    </script>
</body>
</html>

代码执行时,TronLink会弹出如下窗口,用户来确定添加或者取消添加。

1236

点击”添加”后,此资产显示到了资产列表中,如下图

359

添加TRC-721资产:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<script>
        var obj = setInterval(async ()=>{
          //if (window.tronLink.tronWeb) 
            if (window.tronWeb && window.tronWeb.defaultAddress.base58) {
                clearInterval(obj)
              //var tronweb = window.tronLink.tronWeb
                var tronweb = window.tronWeb
                var tx = await tronweb.request({method: 'wallet_watchAsset',
                                                params:{type: 'trc721',
                                                        options: {address: 'TCzUYnFSwtH2bJkynGB46tWxWjdTQqL1SG'},
                                                        },
                                                }
                                               )

            }
        }, 10)
    </script>
</body>
</html>

执行上述代码后,用户需要确认添加或取消添加。

1041

添加完成后,该资产显示到收藏品列表中

356