diff --git a/src/routes/rgbpp/assets.ts b/src/routes/rgbpp/assets.ts index 58340565..78b25b5d 100644 --- a/src/routes/rgbpp/assets.ts +++ b/src/routes/rgbpp/assets.ts @@ -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, Server, ZodTypeProvider> = (fastify, _, done) => { fastify.get( @@ -16,7 +17,13 @@ const assetsRoute: FastifyPluginCallback, Server, ZodTypePr btc_txid: z.string(), }), response: { - 200: z.array(Cell), + 200: z.array( + Cell.merge( + z.object({ + typeHash: z.string().optional(), + }), + ), + ), }, }, }, @@ -36,7 +43,13 @@ const assetsRoute: FastifyPluginCallback, 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, + }; + }); }, ); @@ -51,7 +64,13 @@ const assetsRoute: FastifyPluginCallback, Server, ZodTypePr vout: z.coerce.number(), }), response: { - 200: z.array(Cell), + 200: z.array( + Cell.merge( + z.object({ + typeHash: z.string().optional(), + }), + ), + ), }, }, }, @@ -68,7 +87,13 @@ const assetsRoute: FastifyPluginCallback, 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, + }; + }); }, ); diff --git a/src/routes/rgbpp/types.ts b/src/routes/rgbpp/types.ts index 7fe4d3d2..ea17b73b 100644 --- a/src/routes/rgbpp/types.ts +++ b/src/routes/rgbpp/types.ts @@ -91,5 +91,6 @@ export const XUDTBalance = z.object({ available_amount: z.string(), pending_amount: z.string(), type_hash: z.string(), + type_script: Script, }); export type XUDTBalance = z.infer; diff --git a/src/services/rgbpp.ts b/src/services/rgbpp.ts index 859aee35..75e2dbb5 100644 --- a/src/services/rgbpp.ts +++ b/src/services/rgbpp.ts @@ -124,8 +124,9 @@ export default class RgbppCollector extends BaseQueueWorker { + 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(); + }); +}); diff --git a/test/routes/rgbpp/paymaster.test.ts b/test/routes/rgbpp/paymaster.test.ts index a73eb1b9..67bfd361 100644 --- a/test/routes/rgbpp/paymaster.test.ts +++ b/test/routes/rgbpp/paymaster.test.ts @@ -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(); diff --git a/test/services/__snapshots__/rgbpp.test.ts.snap b/test/services/__snapshots__/rgbpp.test.ts.snap index d2ac5266..b8ab9f97 100644 --- a/test/services/__snapshots__/rgbpp.test.ts.snap +++ b/test/services/__snapshots__/rgbpp.test.ts.snap @@ -8,6 +8,11 @@ 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", @@ -15,6 +20,11 @@ exports[`RgbppCollector > getRgbppBalanceByCells: should return the rgbpp balanc "name": "", "symbol": "LP-UTXOSwap V1", "type_hash": "0x7337d2075479d5c6789564516be1db11478e1d3ca70ad88050fe0bf3ff05acc2", + "type_script": { + "args": "0x1ba116c119d1cfd98a53e9d1a615cf2af2bb87d95515c9d217d367054cfc696b", + "codeHash": "0x25c29dc317811a6f6f3985a7a9ebc4838bd388d19d0feeecf0bcd60f6c0975bb", + "hashType": "type", + }, }, "0x78e21efcf107e7886eadeadecd1a01cfb88f1e5617f4438685db55b3a540d202": { "amount": "0x5f5e100", @@ -22,6 +32,11 @@ exports[`RgbppCollector > getRgbppBalanceByCells: should return the rgbpp balanc "name": "Unique BBQ", "symbol": "UBBQ", "type_hash": "0x78e21efcf107e7886eadeadecd1a01cfb88f1e5617f4438685db55b3a540d202", + "type_script": { + "args": "0x30d3fbec9ceba691770d57c6d06bdb98cf0f82bef0ca6e87687a118d6ce1e7b7", + "codeHash": "0x25c29dc317811a6f6f3985a7a9ebc4838bd388d19d0feeecf0bcd60f6c0975bb", + "hashType": "type", + }, }, } `;