Skip to content

feat: add type_hash to rgbpp assets routes #184

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

Merged
merged 4 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
33 changes: 29 additions & 4 deletions src/routes/rgbpp/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Server } from 'http';
import z from 'zod';
import { Cell } from './types';
import { UTXO } from '../../services/bitcoin/schema';
import { computeScriptHash } from '@ckb-lumos/lumos/utils';

const assetsRoute: FastifyPluginCallback<Record<never, never>, Server, ZodTypeProvider> = (fastify, _, done) => {
fastify.get(
Expand All @@ -16,7 +17,13 @@ const assetsRoute: FastifyPluginCallback<Record<never, never>, Server, ZodTypePr
btc_txid: z.string(),
}),
response: {
200: z.array(Cell),
200: z.array(
Cell.merge(
z.object({
typeHash: z.string().optional(),
}),
),
),
},
},
},
Expand All @@ -36,7 +43,13 @@ const assetsRoute: FastifyPluginCallback<Record<never, never>, Server, ZodTypePr
});

const batchCells = await fastify.rgbppCollector.getRgbppCellsByBatchRequest(utxos);
return batchCells.flat();
return batchCells.flat().map((cell) => {
const typeHash = cell.cellOutput.type ? computeScriptHash(cell.cellOutput.type) : undefined;
return {
...cell,
typeHash,
};
});
},
);

Expand All @@ -51,7 +64,13 @@ const assetsRoute: FastifyPluginCallback<Record<never, never>, Server, ZodTypePr
vout: z.coerce.number(),
}),
response: {
200: z.array(Cell),
200: z.array(
Cell.merge(
z.object({
typeHash: z.string().optional(),
}),
),
),
},
},
},
Expand All @@ -68,7 +87,13 @@ const assetsRoute: FastifyPluginCallback<Record<never, never>, Server, ZodTypePr
};

const batchCells = await fastify.rgbppCollector.getRgbppCellsByBatchRequest([utxo]);
return batchCells.flat();
return batchCells.flat().map((cell) => {
const typeHash = cell.cellOutput.type ? computeScriptHash(cell.cellOutput.type) : undefined;
return {
...cell,
typeHash,
};
});
},
);

Expand Down
1 change: 1 addition & 0 deletions src/routes/rgbpp/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,6 @@ export const XUDTBalance = z.object({
available_amount: z.string(),
pending_amount: z.string(),
type_hash: z.string(),
type_script: Script,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another case issue is the Script is in camelCase where this type is in snake_case: https://github.com/ckb-cell/btc-assets-api/blob/f686bc1ab05719fc9942ecd01f1c1e645138cf9d/src/routes/rgbpp/types.ts#L3-L7

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

});
export type XUDTBalance = z.infer<typeof XUDTBalance>;
3 changes: 2 additions & 1 deletion src/services/rgbpp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,9 @@ export default class RgbppCollector extends BaseQueueWorker<IRgbppCollectRequest
if (!xudtBalances[typeHash]) {
xudtBalances[typeHash] = {
...infoCellData,
type_hash: typeHash,
amount: amount,
type_hash: typeHash,
type_script: type,
};
} else {
xudtBalances[typeHash].amount = BI.from(xudtBalances[typeHash].amount).add(BI.from(amount)).toHexString();
Expand Down
35 changes: 35 additions & 0 deletions test/routes/rgbpp/__snapshots__/address.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,11 @@ exports[`/:btc_address/balance - with pending_amount 1`] = `
"symbol": "LP-UTXOSwap V1",
"total_amount": "0x624eee61",
"type_hash": "0x7337d2075479d5c6789564516be1db11478e1d3ca70ad88050fe0bf3ff05acc2",
"type_script": {
"args": "0x1ba116c119d1cfd98a53e9d1a615cf2af2bb87d95515c9d217d367054cfc696b",
"codeHash": "0x25c29dc317811a6f6f3985a7a9ebc4838bd388d19d0feeecf0bcd60f6c0975bb",
"hashType": "type",
},
},
{
"available_amount": "0x98aeea0",
Expand All @@ -290,6 +295,11 @@ exports[`/:btc_address/balance - with pending_amount 1`] = `
"symbol": "JDI",
"total_amount": "0x98aeea0",
"type_hash": "0x6d7dd4497d1ef095644e422d30b2456cfe32c6469346c77752a4d6b56f432a73",
"type_script": {
"args": "0xc625c4ac0ba3ece5886d04958c3fc2d5558a21841c03577fad2d7c46e5b2b2b9",
"codeHash": "0x25c29dc317811a6f6f3985a7a9ebc4838bd388d19d0feeecf0bcd60f6c0975bb",
"hashType": "type",
},
},
{
"available_amount": "0x5f5e100",
Expand All @@ -299,6 +309,11 @@ exports[`/:btc_address/balance - with pending_amount 1`] = `
"symbol": "UBBQ",
"total_amount": "0xbebc200",
"type_hash": "0x78e21efcf107e7886eadeadecd1a01cfb88f1e5617f4438685db55b3a540d202",
"type_script": {
"args": "0x30d3fbec9ceba691770d57c6d06bdb98cf0f82bef0ca6e87687a118d6ce1e7b7",
"codeHash": "0x25c29dc317811a6f6f3985a7a9ebc4838bd388d19d0feeecf0bcd60f6c0975bb",
"hashType": "type",
},
},
{
"available_amount": "0x0",
Expand All @@ -308,6 +323,11 @@ exports[`/:btc_address/balance - with pending_amount 1`] = `
"symbol": "PDD",
"total_amount": "0x5f5e100",
"type_hash": "0x10f511f2efb0027191b97ac5b4bd77374ffdac7399e8527d76f5f9bd32e7d35b",
"type_script": {
"args": "0x8c556e92974a8dd8237719020a259d606359ac2cc958cb8bda77a1c3bb3cd93b",
"codeHash": "0x25c29dc317811a6f6f3985a7a9ebc4838bd388d19d0feeecf0bcd60f6c0975bb",
"hashType": "type",
},
},
],
}
Expand All @@ -325,6 +345,11 @@ exports[`/:btc_address/balance - without pending_amount 1`] = `
"symbol": "LP-UTXOSwap V1",
"total_amount": "0x624eee61",
"type_hash": "0x7337d2075479d5c6789564516be1db11478e1d3ca70ad88050fe0bf3ff05acc2",
"type_script": {
"args": "0x1ba116c119d1cfd98a53e9d1a615cf2af2bb87d95515c9d217d367054cfc696b",
"codeHash": "0x25c29dc317811a6f6f3985a7a9ebc4838bd388d19d0feeecf0bcd60f6c0975bb",
"hashType": "type",
},
},
{
"available_amount": "0x98aeea0",
Expand All @@ -334,6 +359,11 @@ exports[`/:btc_address/balance - without pending_amount 1`] = `
"symbol": "JDI",
"total_amount": "0x98aeea0",
"type_hash": "0x6d7dd4497d1ef095644e422d30b2456cfe32c6469346c77752a4d6b56f432a73",
"type_script": {
"args": "0xc625c4ac0ba3ece5886d04958c3fc2d5558a21841c03577fad2d7c46e5b2b2b9",
"codeHash": "0x25c29dc317811a6f6f3985a7a9ebc4838bd388d19d0feeecf0bcd60f6c0975bb",
"hashType": "type",
},
},
{
"available_amount": "0x5f5e100",
Expand All @@ -343,6 +373,11 @@ exports[`/:btc_address/balance - without pending_amount 1`] = `
"symbol": "UBBQ",
"total_amount": "0x5f5e100",
"type_hash": "0x78e21efcf107e7886eadeadecd1a01cfb88f1e5617f4438685db55b3a540d202",
"type_script": {
"args": "0x30d3fbec9ceba691770d57c6d06bdb98cf0f82bef0ca6e87687a118d6ce1e7b7",
"codeHash": "0x25c29dc317811a6f6f3985a7a9ebc4838bd388d19d0feeecf0bcd60f6c0975bb",
"hashType": "type",
},
},
],
}
Expand Down
57 changes: 57 additions & 0 deletions test/routes/rgbpp/__snapshots__/assets.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`Get RGB++ assets by BTC txid 1`] = `
[
{
"blockNumber": "0xc6067f",
"cellOutput": {
"capacity": "0x5e9f52520",
"lock": {
"args": "0x010000001770f9dc84b1e05d507bcdd46884b5e267c731071c0d2f01cb257c76049e15ca",
"codeHash": "0x61ca7a4796a4eb19ca4f0d065cb9b10ddcf002f10f7cbb810c706cb6bb5c3248",
"hashType": "type",
},
"type": {
"args": "0x7e96affc517fe2b286e309e88d6b331911fc319baa3f8a830dcf59a9756f2c25",
"codeHash": "0x25c29dc317811a6f6f3985a7a9ebc4838bd388d19d0feeecf0bcd60f6c0975bb",
"hashType": "type",
},
},
"data": "0x0040075af07507000000000000000000",
"outPoint": {
"index": "0x0",
"txHash": "0xb799ae6896875f2a1a1e765d614cf864c444242babc4bfd7808b37fdea66c4cc",
},
"txIndex": "0x2",
"typeHash": "0x27b36bd74aec3ffccb1c1351c3e3db64cba33ee1b5651059fe468fbcbb8c81bd",
},
]
`;

exports[`Get RGB++ assets by BTC txid and vout 1`] = `
[
{
"blockNumber": "0xc6067f",
"cellOutput": {
"capacity": "0x5e9f52520",
"lock": {
"args": "0x010000001770f9dc84b1e05d507bcdd46884b5e267c731071c0d2f01cb257c76049e15ca",
"codeHash": "0x61ca7a4796a4eb19ca4f0d065cb9b10ddcf002f10f7cbb810c706cb6bb5c3248",
"hashType": "type",
},
"type": {
"args": "0x7e96affc517fe2b286e309e88d6b331911fc319baa3f8a830dcf59a9756f2c25",
"codeHash": "0x25c29dc317811a6f6f3985a7a9ebc4838bd388d19d0feeecf0bcd60f6c0975bb",
"hashType": "type",
},
},
"data": "0x0040075af07507000000000000000000",
"outPoint": {
"index": "0x0",
"txHash": "0xb799ae6896875f2a1a1e765d614cf864c444242babc4bfd7808b37fdea66c4cc",
},
"txIndex": "0x2",
"typeHash": "0x27b36bd74aec3ffccb1c1351c3e3db64cba33ee1b5651059fe468fbcbb8c81bd",
},
]
`;
65 changes: 65 additions & 0 deletions test/routes/rgbpp/assets.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { beforeEach, expect, test } from 'vitest';
import { buildFastify } from '../../../src/app';
import { describe } from 'node:test';

let token: string;

describe('/rgbpp/v1/assets', () => {
beforeEach(async () => {
const fastify = buildFastify();
await fastify.ready();

const response = await fastify.inject({
method: 'POST',
url: '/token/generate',
payload: {
app: 'test',
domain: 'test.com',
},
});
const data = response.json();
token = data.token;

await fastify.close();
});

test('Get RGB++ assets by BTC txid', async () => {
const fastify = buildFastify();
await fastify.ready();

const response = await fastify.inject({
method: 'GET',
url: '/rgbpp/v1/assets/ca159e04767c25cb012f0d1c0731c767e2b58468d4cd7b505de0b184dcf97017',
headers: {
Authorization: `Bearer ${token}`,
Origin: 'https://test.com',
},
});
const data = response.json();

expect(response.statusCode).toBe(200);
expect(data).toMatchSnapshot();

await fastify.close();
});

test('Get RGB++ assets by BTC txid and vout', async () => {
const fastify = buildFastify();
await fastify.ready();

const response = await fastify.inject({
method: 'GET',
url: '/rgbpp/v1/assets/ca159e04767c25cb012f0d1c0731c767e2b58468d4cd7b505de0b184dcf97017/1',
headers: {
Authorization: `Bearer ${token}`,
Origin: 'https://test.com',
},
});
const data = response.json();

expect(response.statusCode).toBe(200);
expect(data).toMatchSnapshot();

await fastify.close();
});
});
2 changes: 1 addition & 1 deletion test/routes/rgbpp/paymaster.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Env } from '../../../src/env';

let token: string;

describe('/bitcoin/v1/paymaster', () => {
describe('/rgbpp/v1/paymaster', () => {
beforeEach(async () => {
const fastify = buildFastify();
await fastify.ready();
Expand Down
15 changes: 15 additions & 0 deletions test/services/__snapshots__/rgbpp.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,35 @@ exports[`RgbppCollector > getRgbppBalanceByCells: should return the rgbpp balanc
"name": "JoyID Dex Test Inscription",
"symbol": "JDI",
"type_hash": "0x6d7dd4497d1ef095644e422d30b2456cfe32c6469346c77752a4d6b56f432a73",
"type_script": {
"args": "0xc625c4ac0ba3ece5886d04958c3fc2d5558a21841c03577fad2d7c46e5b2b2b9",
"codeHash": "0x25c29dc317811a6f6f3985a7a9ebc4838bd388d19d0feeecf0bcd60f6c0975bb",
"hashType": "type",
},
},
"0x7337d2075479d5c6789564516be1db11478e1d3ca70ad88050fe0bf3ff05acc2": {
"amount": "0x624eee61",
"decimal": 8,
"name": "",
"symbol": "LP-UTXOSwap V1",
"type_hash": "0x7337d2075479d5c6789564516be1db11478e1d3ca70ad88050fe0bf3ff05acc2",
"type_script": {
"args": "0x1ba116c119d1cfd98a53e9d1a615cf2af2bb87d95515c9d217d367054cfc696b",
"codeHash": "0x25c29dc317811a6f6f3985a7a9ebc4838bd388d19d0feeecf0bcd60f6c0975bb",
"hashType": "type",
},
},
"0x78e21efcf107e7886eadeadecd1a01cfb88f1e5617f4438685db55b3a540d202": {
"amount": "0x5f5e100",
"decimal": 8,
"name": "Unique BBQ",
"symbol": "UBBQ",
"type_hash": "0x78e21efcf107e7886eadeadecd1a01cfb88f1e5617f4438685db55b3a540d202",
"type_script": {
"args": "0x30d3fbec9ceba691770d57c6d06bdb98cf0f82bef0ca6e87687a118d6ce1e7b7",
"codeHash": "0x25c29dc317811a6f6f3985a7a9ebc4838bd388d19d0feeecf0bcd60f6c0975bb",
"hashType": "type",
},
},
}
`;
Expand Down
Loading