Skip to content

Commit

Permalink
Merge pull request #230 from utxostack/feat/usdi-rusd-info
Browse files Browse the repository at this point in the history
Add compatible xUDT info whitelist
  • Loading branch information
duanyytop authored Jan 15, 2025
2 parents d918ab9 + 3f0b996 commit 3ef9b41
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "btc-assets-api",
"version": "2.5.5",
"version": "2.5.6",
"title": "Bitcoin/RGB++ Assets API",
"description": "",
"main": "index.js",
Expand Down
30 changes: 30 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,33 @@ export const BTC_MAINNET_SPV_START_BLOCK_HEIGHT = 837300;

export const IS_MAINNET = env.NETWORK === NetworkType.mainnet.toString();
export const TESTNET_TYPE = TestnetTypeMap[env.NETWORK];

// Using unique cell as xUDT information is recommended and refer: https://github.com/utxostack/unique-cell/metadata
export const COMPATIBLE_UDT_INFO_WHITELIST = [
{
// USDI: mainnet and testnet codeHashes and information
// https://explorer.nervos.org/xudt/0x0cae1430516a0c744f6b7c7cb484615f31fb7348327e012181de98f763d02faf
// https://testnet.explorer.nervos.org/xudt/0x07ac97b5ff3df4b49f59a59f4d80d33d22c1263a57467c512c93b9c29b7a0de3
codeHashes: [
'0xbfa35a9c38a676682b65ade8f02be164d48632281477e36f8dc2f41f79e56bfc',
'0xcc9dc33ef234e14bc788c43a4848556a5fb16401a04662fc55db9bb201987037',
],
hashType: 'type',
name: 'USDI',
symbol: 'USDI',
decimal: 6,
},
{
// RUSD: mainnet and testnet codeHashes and information
// https://explorer.nervos.org/xudt/0x71ff665b40ba044b1981ea9a8965189559c8e01e8cdfa34a3cc565e1f870a95c
// https://testnet.explorer.nervos.org/xudt/0x45b32a2bc4285d0a09678eb11960ddc8707bc2779887a09d482e9bfe9a2cdf52
codeHashes: [
'0x26a33e0815888a4a0614a0b7d09fa951e0993ff21e55905510104a0b1312032b',
'0x1142755a044bf2ee358cba9f2da187ce928c91cd4dc8692ded0337efa677d21a',
],
hashType: 'type',
name: 'RUSD',
symbol: 'RUSD',
decimal: 8,
},
];
31 changes: 28 additions & 3 deletions src/services/ckb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import DataCache from './base/data-cache';
import { scriptToHash } from '@nervosnetwork/ckb-sdk-utils';
import { Cell } from '../routes/rgbpp/types';
import { uniq } from 'lodash';
import { IS_MAINNET } from '../constants';
import { COMPATIBLE_UDT_INFO_WHITELIST, IS_MAINNET } from '../constants';
import { decodeMetadata, decodeTokenInfo } from '@utxostack/metadata';

export type TransactionWithStatus = Awaited<ReturnType<CKBRPC['getTransaction']>>;
Expand Down Expand Up @@ -187,6 +187,27 @@ export default class CKBClient {
};
}

/**
* Get the information data of the given compatible xudt type script
* @param xudtTypeScript - the compatible xudt type script
*/
public getCompatibleXudtWhitelistInfo(xudtTypeScript: Script) {
const { codeHash, hashType } = xudtTypeScript;
const data = COMPATIBLE_UDT_INFO_WHITELIST.find((item) => {
if (item.codeHashes.includes(codeHash) && item.hashType === hashType) {
return item;
}
});
if (!data) {
return null;
}
return {
name: data.name,
symbol: data.symbol,
decimal: data.decimal,
};
}

/**
* Get the unique cell data of the given xudt type script from the transaction
* @param tx - the ckb transaction that contains the unique cell
Expand Down Expand Up @@ -336,13 +357,17 @@ export default class CKBClient {
* @param script - the xudt type script
*/
public async getInfoCellData(script: Script): Promise<TokenInfoMetadata | null> {
// If the type script is in the whitelist, return the info data directly
let infoData: TokenInfoMetadata | null = this.getCompatibleXudtWhitelistInfo(script);
if (infoData) {
return infoData;
}
// If the type script is not in the whitelist, get the info data from the unique cell and metadata cell
const typeHash = computeScriptHash(script);
const cachedData = await this.dataCache.get(`type:${typeHash}`);
if (cachedData) {
return cachedData as TokenInfoMetadata;
}

let infoData: TokenInfoMetadata | null = null;
const txs = await this.getAllInfoCellTxs();
for (const tx of txs) {
// check if the unique cell is one of the info cells of the xudt type
Expand Down
37 changes: 37 additions & 0 deletions test/__fixtures__/rgbpp-utxo-pairs.mock.json
Original file line number Diff line number Diff line change
Expand Up @@ -479,5 +479,42 @@
"txIndex": "0x1"
}
]
},
{
"utxo": {
"txid": "45618e2a8e5b091fd1381cd7d67c3dececb2727fdbeb9887965b577b6c2239ab",
"vout": 0,
"value": 546,
"status": {
"confirmed": true,
"block_height": 2816899,
"block_hash": "000000000000000838d3bf989d702597827cd9830b8bedeaba49a5f2621053fa",
"block_time": 1716348699
}
},
"cells": [
{
"cellOutput": {
"capacity": "0xbe5cc1f00",
"lock": {
"args": "0x5ee4738bd0a9a6b94fc203cf76e95790e05fd740",
"codeHash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
"hashType": "type"
},
"type": {
"args": "0x878fcc6f1f08d48e87bb1c3b3d5083f23f8a39c5d5c764f253b55b998526439b",
"codeHash": "0x1142755a044bf2ee358cba9f2da187ce928c91cd4dc8692ded0337efa677d21a",
"hashType": "type"
}
},
"data": "0x00c817a8040000000000000000000000",
"outPoint": {
"index": "0x2",
"txHash": "0xe6b4da0fe4f1bad78c0d404bd92753373092c21ac58694425cf104e953a01964"
},
"txIndex": "0x1",
"typeHash": "0x45b32a2bc4285d0a09678eb11960ddc8707bc2779887a09d482e9bfe9a2cdf52"
}
]
}
]
22 changes: 22 additions & 0 deletions test/routes/rgbpp/__snapshots__/address.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5963,6 +5963,28 @@ exports[`/:btc_address/assets 1`] = `
"txIndex": "0x1",
"typeHash": "0xe5ee2fdd79aaa218bd74a821c305fa40305408fae2dbfedf8243ea2b4d7af8e4",
},
{
"cellOutput": {
"capacity": "0xbe5cc1f00",
"lock": {
"args": "0x5ee4738bd0a9a6b94fc203cf76e95790e05fd740",
"codeHash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
"hashType": "type",
},
"type": {
"args": "0x878fcc6f1f08d48e87bb1c3b3d5083f23f8a39c5d5c764f253b55b998526439b",
"codeHash": "0x1142755a044bf2ee358cba9f2da187ce928c91cd4dc8692ded0337efa677d21a",
"hashType": "type",
},
},
"data": "0x00c817a8040000000000000000000000",
"outPoint": {
"index": "0x2",
"txHash": "0xe6b4da0fe4f1bad78c0d404bd92753373092c21ac58694425cf104e953a01964",
},
"txIndex": "0x1",
"typeHash": "0x45b32a2bc4285d0a09678eb11960ddc8707bc2779887a09d482e9bfe9a2cdf52",
},
]
`;

Expand Down
12 changes: 12 additions & 0 deletions test/services/__snapshots__/rgbpp.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@

exports[`RgbppCollector > getRgbppBalanceByCells: should return the rgbpp balance by cells 1`] = `
{
"0x45b32a2bc4285d0a09678eb11960ddc8707bc2779887a09d482e9bfe9a2cdf52": {
"amount": "0x4a817c800",
"decimal": 8,
"name": "RUSD",
"symbol": "RUSD",
"type_hash": "0x45b32a2bc4285d0a09678eb11960ddc8707bc2779887a09d482e9bfe9a2cdf52",
"type_script": {
"args": "0x878fcc6f1f08d48e87bb1c3b3d5083f23f8a39c5d5c764f253b55b998526439b",
"codeHash": "0x1142755a044bf2ee358cba9f2da187ce928c91cd4dc8692ded0337efa677d21a",
"hashType": "type",
},
},
"0x6d7dd4497d1ef095644e422d30b2456cfe32c6469346c77752a4d6b56f432a73": {
"amount": "0x98aeea0",
"decimal": 8,
Expand Down

0 comments on commit 3ef9b41

Please sign in to comment.