搭建链上数据看板#
请确保已完成了准备工作,可直接通过此案例,熟悉链上数据查询的各类方式。
方式一:按地址查询#
查询资产#
调用接口来获取集成 60 +链的,多链多协议资产数据,秒级更新。
查询币价及币种信息#
调用接口来获取集成各大交易所、DEX 及市场的多数据源的币价数据。
查询持有的 UTXO 及铭文#
查询交易历史#
调用接口来获取各个链上地址生成以来的全量交易历史数据
查询授权#
调用接口来查看地址上的授权详情
方式二:按账户批量查询#
第一步:创建账户#
创建钱包账户 (AccountId),来聚合你想查看的地址,进行批量查询代币余额和交易历史。
具体实现:调用接口来创建钱包账户。
例如,同时订阅不同链上的同一地址,实现如下:
//定义你的参数
const addresses = [
{
"chainIndex":"1",
"address":"0x561815e02bac6128bbbbc551005ddfd92a5c24db",
},
{
"chainIndex":"10",
"address":"0x561815e02bac6128bbbbc551005ddfd92a5c24db",
}
];
const getCreateWalletAccountBody = {
addresses: addresses
};
//定义辅助函数
const getCreateWalletAccountData = async () => {
const apiRequestUrl = getRequestUrl(
apiBaseUrl,
'/api/v5/wallet/account/create-wallet-account'
);
return fetch(apiRequestUrl, {
method: 'post',
headers: headersParams,
body: JSON.stringify(getCreateWalletBody),
})
.then((res) => res.json())
.then((res) => {
return res;
});
};
const { data: createWalletAccountData } = await getCreateWlletAccountData();
第二步:查询余额#
钱包账户可直接调用接口查看账户下所持有的币种,点击查钱包账户代币余额查看详情。
//定义参数
const getAssetsParams = {
accountId: '13886e05-1265-4b79-8ac3-b7ab46211001',
};
//定义辅助函数
const getAssetsData = async () => {
const apiRequestUrl = getRequestUrl(
apiBaseUrl,
'/api/v5/wallet/asset/wallet-all-token-balances',
getAssetsParams
);
return fetch(apiRequestUrl, {
method: 'GET',
headers: headersParams,
})
.then((res) => res.json())
.then((res) => {
return res;
});
};
const { data: getAssetsData } = await getAssetsData();
当查询到资产时,你将收到如下的响应:
{
"code": "0",
"data": [
{
"chainIndex": "1",
"tokenAddress": "0xdac17f958d2ee523a2206206994597c13d831ec7",
"symbol": "USDT",
"balance": "0.688467",
"tokenPrice": "0.99993",
"tokenType": "1"
},
{
"chainIndex": "1",
"tokenAddress": "0x514910771af9ca656af840dff83e8264ecf986ca",
"symbol": "LINK",
"balance": "0.000000366257612925",
"tokenPrice": "14.108071299717093",
"tokenType": "1"
}
],
"msg": "success"
}
第三步:查询交易历史#
发送交易后,使用 GET /api/v5/wallet/post-transaction/transaction-detail-by-txhash
接口并提供 txHash
以获取交易详情。
// 定义你的参数
const params = {
txhash: '0x9ab8ccccc9f778ea91ce4c0f15517672c4bd06d166e830da41ba552e744d29a5',
chainIndex: '42161'
};
// 定义辅助函数
const getTransactionDetailData = async () => {
const apiRequestUrl = getRequestUrl(
apiBaseUrl,
'/api/v5/wallet/post-transaction/transaction-detail-by-txhash',
params
);
return fetch(apiRequestUrl, {
method: 'get',
headers: headersParams,
})
.then((res) => res.json())
.then((res) => {
return res;
});
};
const { data: transactionDetailData } = await getTransactionDetailData();
在查询交易历史时,你将收到下列响应:
{
"code": "0",
"msg": "success",
"data": [
{
"chainIndex": "42161",
"height": "245222398",
"txTime": "1724253417000",
"txhash": "0x9ab8ccccc9f778ea91ce4c0f15517672c4bd06d166e830da41ba552e744d29a5",
"gasLimit": "2000000",
"gasUsed": "2000000",
"gasPrice": "10000000",
"nonce": "0",
"symbol": "ETH",
"amount": "0",
"txStatus": "success",
"methodId": "0xc9f95d32",
"l1OriginHash": "0xa6a87ba2f18cc32bbae8f3b2253a29a9617ed1eb0940d80443f6e3bf9873dbad",
"fromDetails": [
{
"address": "0xd297fa914353c44b2e33ebe05f21846f1048cfeb",
"vinIndex": "",
"preVoutIndex": "",
"txHash": "",
"isContract": false,
"amount": ""
}
],
"toDetails": [
{
"address": "0x000000000000000000000000000000000000006e",
"voutIndex": "",
"isContract": false,
"amount": ""
}
],
"internalTransactionDetails": [
{
"from": "0x0000000000000000000000000000000000000000",
"to": "0xd297fa914353c44b2e33ebe05f21846f1048cfeb",
"isFromContract": false,
"isToContract": false,
"amount": "0.02",
"state": "success"
},
{
"from": "0xd297fa914353c44b2e33ebe05f21846f1048cfeb",
"to": "0x428ab2ba90eba0a4be7af34c9ac451ab061ac010",
"isFromContract": false,
"isToContract": false,
"amount": "0.00998",
"state": "success"
},
{
"from": "0xd297fa914353c44b2e33ebe05f21846f1048cfeb",
"to": "0x428ab2ba90eba0a4be7af34c9ac451ab061ac010",
"isFromContract": false,
"isToContract": false,
"amount": "0.009977946366846017",
"state": "success"
}
],
"tokenTransferDetails": []
}
]
}
到这里,你基本上已经实现了开发一款链上数据看板所需的基础功能。
此外,钱包 API 提供了丰富接口,全方位满足交易所钱包开发者的需求,具体请查看下面的章节,了解更多。