Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decode unique cell token info and metadata #226

Merged
merged 6 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"start": "node ./dist/src/index.js",
"build": "tsc -p tsconfig.json",
"test": "vitest",
"format": "eslint --fix --ext .ts && prettier --write '**/*.ts'",
"coverage": "vitest run --coverage",
"postinstall": "npx simple-git-hooks"
},
Expand Down Expand Up @@ -42,12 +43,13 @@
"@fastify/swagger-ui": "^3.0.0",
"@immobiliarelabs/fastify-sentry": "^8.0.1",
"@nervosnetwork/ckb-sdk-utils": "^0.109.1",
"@rgbpp-sdk/btc": "0.0.0-snap-20250106033736",
"@rgbpp-sdk/ckb": "0.0.0-snap-20250106033736",
"@rgbpp-sdk/service": "0.0.0-snap-20250106033736",
"@rgbpp-sdk/btc": "0.0.0-snap-20250110025940",
"@rgbpp-sdk/ckb": "0.0.0-snap-20250110025940",
"@rgbpp-sdk/service": "0.0.0-snap-20250110025940",
"@sentry/node": "^7.102.1",
"@sentry/profiling-node": "^7.102.1",
"@spore-sdk/core": "^0.2.0-beta.9",
"@utxostack/metadata": "^0.0.6",
"async-retry": "^1.3.3",
"awilix": "^10.0.1",
"axios": "^1.7.7",
Expand Down
44 changes: 26 additions & 18 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

71 changes: 57 additions & 14 deletions src/services/ckb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
getUniqueTypeScript,
getXudtTypeScript,
isScriptEqual,
isTokenMetadataType,
sendCkbTx,
} from '@rgbpp-sdk/ckb';
import { Cradle } from '../container';
Expand All @@ -13,7 +14,6 @@ import { UngroupedIndexerTransaction } from '@ckb-lumos/ckb-indexer/lib/type';
import { z } from 'zod';
import * as Sentry from '@sentry/node';
import {
decodeInfoCellData,
decodeUDTHashFromInscriptionData,
getInscriptionInfoTypeScript,
isInscriptionInfoTypeScript,
Expand All @@ -25,6 +25,7 @@ import { scriptToHash } from '@nervosnetwork/ckb-sdk-utils';
import { Cell } from '../routes/rgbpp/types';
import { uniq } from 'lodash';
import { IS_MAINNET } from '../constants';
import { decodeMetadata, decodeTokenInfo, Metadata, TokenInfo } from '@utxostack/metadata';

export type TransactionWithStatus = Awaited<ReturnType<CKBRPC['getTransaction']>>;

Expand Down Expand Up @@ -147,6 +148,7 @@ export class CKBRpcError extends Error {
}
}

type TokenInfoMetadata = TokenInfo & Partial<Metadata>;
export default class CKBClient {
public rpc: RPC;
public indexer: Indexer;
Expand Down Expand Up @@ -183,7 +185,7 @@ export default class CKBClient {
* @param index - the index of the unique cell in the transaction
* @param xudtTypeScript - the xudt type script
* reference:
* - https://github.com/ckb-cell/unique-cell
* - https://github.com/utxostack/unique-cell/metadata
*/
public getUniqueCellData(tx: TransactionWithStatus, index: number, xudtTypeScript: Script) {
// find the xudt cell index in the transaction
Expand All @@ -199,7 +201,33 @@ export default class CKBClient {
if (!encodeData) {
return null;
}
const data = decodeInfoCellData(encodeData);
const data = decodeTokenInfo(encodeData);
return data;
}

/**
* Get the token metadata data of the given xudt type script from the transaction
* @param tx - the ckb transaction that contains the token metadata cell
* @param index - the index of the token metadata cell in the transaction
* @param xudtTypeScript - the xudt type script
* reference:
* - https://github.com/utxostack/unique-cell/metadata
*/
public getTokenMetadataData(tx: TransactionWithStatus, index: number, xudtTypeScript: Script) {
// find the xudt cell index in the transaction
// generally, the xudt cell and token metadata cell are in the same transaction
const xudtCellIndex = tx.transaction.outputs.findIndex((cell) => {
return cell.type && isScriptEqual(cell.type, xudtTypeScript);
});
if (xudtCellIndex === -1) {
return null;
}

const encodeData = tx.transaction.outputsData[index];
if (!encodeData) {
return null;
}
const data = decodeMetadata(encodeData);
return data;
}

Expand All @@ -221,7 +249,7 @@ export default class CKBClient {
if (decodeUDTHashFromInscriptionData(encodeData) !== xudtTypeHash) {
return null;
}
const data = decodeInfoCellData(encodeData);
const data = decodeTokenInfo(encodeData);
return data;
}

Expand Down Expand Up @@ -290,26 +318,41 @@ export default class CKBClient {
* Get the unique cell of the given xudt type
* @param script - the xudt type script
*/
public async getInfoCellData(script: Script) {
public async getInfoCellData(script: Script): Promise<TokenInfoMetadata | null> {
const typeHash = computeScriptHash(script);
const cachedData = await this.dataCache.get(`type:${typeHash}`);
if (cachedData) {
return cachedData as ReturnType<typeof decodeInfoCellData>;
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 the info cell of the xudt type
const uniqueCellIndex = tx.transaction.outputs.findIndex((cell) => {
return cell.type && isUniqueCellTypeScript(cell.type, IS_MAINNET);
});
// check if the unique cell is one of the info cells of the xudt type
const uniqueCellIndex = tx.transaction.outputs.findIndex(
(cell) => cell.type && isUniqueCellTypeScript(cell.type, IS_MAINNET),
);
if (uniqueCellIndex !== -1) {
const infoCellData = this.getUniqueCellData(tx, uniqueCellIndex, script);
if (infoCellData) {
await this.dataCache.set(`type:${typeHash}`, infoCellData);
return infoCellData;
infoData = this.getUniqueCellData(tx, uniqueCellIndex, script);
if (infoData) {
// check if the token metadata cell is one of the info cells of the xudt type
const metadataCellIndex = tx.transaction.outputs.findIndex(
(cell) => cell.type && isTokenMetadataType(cell.type, IS_MAINNET),
);
if (metadataCellIndex !== -1) {
const metadataData = this.getTokenMetadataData(tx, metadataCellIndex, script);
if (metadataData) {
infoData = {
...infoData,
...metadataData,
};
}
}
await this.dataCache.set(`type:${typeHash}`, infoData);
return infoData;
}
}

// check if the inscription cell is the info cell of the xudt type
const inscriptionCellIndex = tx.transaction.outputs.findIndex((cell) => {
return cell.type && isInscriptionInfoTypeScript(cell.type, IS_MAINNET);
Expand Down
23 changes: 1 addition & 22 deletions src/utils/xudt.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,6 @@
import { BI } from '@ckb-lumos/lumos';
import { remove0x } from '@rgbpp-sdk/btc';
import { getUniqueTypeScript, hexToUtf8 } from '@rgbpp-sdk/ckb';

// https://github.com/ckb-cell/unique-cell?tab=readme-ov-file#xudt-information
export function decodeInfoCellData(data: string) {
const hex = remove0x(data);
const info = {
decimal: BI.from(`0x${hex.slice(0, 2)}`).toNumber(),
name: '',
symbol: '',
};

const nameSize = BI.from(`0x${hex.slice(2, 4)}`).toNumber() * 2;
if (nameSize > 0) {
info.name = hexToUtf8(`0x${hex.slice(4, 4 + nameSize)}`);
}

const symbolSize = BI.from(`0x${hex.slice(4 + nameSize, 4 + nameSize + 2)}`).toNumber() * 2;
if (symbolSize > 0) {
info.symbol = hexToUtf8(`0x${hex.slice(4 + nameSize + 2, 4 + nameSize + 2 + symbolSize)}`);
}
return info;
}
import { getUniqueTypeScript } from '@rgbpp-sdk/ckb';

export function decodeUDTHashFromInscriptionData(data: string) {
try {
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 @@ -442,5 +442,42 @@
"txIndex": "0x1"
}
]
},
{
"utxo": {
"txid": "13618e2a8e5b091fd1381cd7d67c3dececb2727fdbeb9887965b577b6c2239ab",
"vout": 0,
"value": 546,
"status": {
"confirmed": true,
"block_height": 2816899,
"block_hash": "000000000000000838d3bf989d702597827cd9830b8bedeaba49a5f2621053fa",
"block_time": 1716348699
}
},
"cells": [
{
"cellOutput": {
"capacity": "0x1b381be34e00",
"lock": {
"codeHash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
"args": "0x9787495f518fec240d7857067f3016daf705286d",
"hashType": "type"
},
"type": {
"codeHash": "0xf5da9003e31fa9301a3915fe304de9bdb80524b5f0d8fc325fb699317998ee7a",
"args": "0xa63d308c04b4c075eb1d7d5cac891cf20276e3ddb2ec855fc981c88d8134dbe2",
"hashType": "type"
}
},
"data": "0x00000000000000000000000000000000",
"outPoint": {
"txHash": "0xe4e156943c82ee5500d39343a01dda0c48946a6000f449c58ac91708576cf669",
"index": "0x2"
},
"blockNumber": "0xf17dd7",
"txIndex": "0x1"
}
]
}
]
23 changes: 23 additions & 0 deletions test/routes/rgbpp/__snapshots__/address.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5940,6 +5940,29 @@ exports[`/:btc_address/assets 1`] = `
"txIndex": "0x1",
"typeHash": "0xf0d58aed78476f0aa4fab18f45de1fdcf4f42426fb03570de236dfd36638f3ed",
},
{
"blockNumber": "0xf17dd7",
"cellOutput": {
"capacity": "0x1b381be34e00",
"lock": {
"args": "0x9787495f518fec240d7857067f3016daf705286d",
"codeHash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
"hashType": "type",
},
"type": {
"args": "0xa63d308c04b4c075eb1d7d5cac891cf20276e3ddb2ec855fc981c88d8134dbe2",
"codeHash": "0xf5da9003e31fa9301a3915fe304de9bdb80524b5f0d8fc325fb699317998ee7a",
"hashType": "type",
},
},
"data": "0x00000000000000000000000000000000",
"outPoint": {
"index": "0x2",
"txHash": "0xe4e156943c82ee5500d39343a01dda0c48946a6000f449c58ac91708576cf669",
},
"txIndex": "0x1",
"typeHash": "0xe5ee2fdd79aaa218bd74a821c305fa40305408fae2dbfedf8243ea2b4d7af8e4",
},
]
`;

Expand Down
Loading
Loading