Skip to content

Commit

Permalink
feat: add /rgbpp/v1/address/:address/assets route
Browse files Browse the repository at this point in the history
  • Loading branch information
ahonn committed Mar 15, 2024
1 parent cf24e76 commit 452f818
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 8 deletions.
57 changes: 57 additions & 0 deletions src/routes/rgbpp/address.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { FastifyPluginCallback } from 'fastify';
import { Server } from 'http';
import validateBitcoinAddress from '../../utils/validators';
import { ZodTypeProvider } from 'fastify-type-provider-zod';
import z from 'zod';
import { OutputCell } from './types';
import { append0x, u32ToLe } from '../../utils/hex';
import { genRgbppLockScript } from '@rgbpp-sdk/ckb/lib/utils/rgbpp';

const addressRoutes: FastifyPluginCallback<Record<never, never>, Server, ZodTypeProvider> = (fastify, _, done) => {
fastify.addHook('preHandler', async (request) => {
const { address } = request.params as { address: string };
const valid = validateBitcoinAddress(address);
if (!valid) {
throw fastify.httpErrors.badRequest('Invalid bitcoin address');
}
});

fastify.get(
'/:address/assets',
{
schema: {
params: z.object({
address: z.string(),
}),
response: {
200: z.array(OutputCell),
},
},
},
async (request) => {
const { address } = request.params;
const utxos = await fastify.electrs.getUtxoByAddress(address);
const cells = await Promise.all(
utxos.map(async (utxo) => {
const { txid, vout } = utxo;
const args = append0x(`${u32ToLe(vout)}${txid}`);
const lockScript = genRgbppLockScript(args, process.env.NETWORK === 'mainnet');
const collector = fastify.ckbIndexer.collector({
lock: lockScript,
});
const collect = collector.collect();
const cells: OutputCell[] = [];
for await (const cell of collect) {
cells.push(cell as unknown as OutputCell);
}
return cells;
}),
);
return cells.flat();
},
);

done();
};

export default addressRoutes;
11 changes: 3 additions & 8 deletions src/routes/rgbpp/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import { FastifyPluginCallback } from 'fastify';
import { ZodTypeProvider } from 'fastify-type-provider-zod';
import { Server } from 'http';
import z from 'zod';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
import { getRgbppLockScript } from '@rgbpp-sdk/ckb';
import { genRgbppLockScript } from '@rgbpp-sdk/ckb/lib/utils/rgbpp';
import { append0x, u32ToLe } from '../../utils/hex';
import { OutputCell } from './types';

Expand All @@ -24,14 +22,11 @@ const assetsRoute: FastifyPluginCallback<Record<never, never>, Server, ZodTypePr
},
async (request) => {
const { txid, vout } = request.params;
const lockScript = getRgbppLockScript(process.env.NETWORK === 'mainnet');
const args = append0x(`${u32ToLe(vout)}${txid}`);
const lockScript = genRgbppLockScript(args, process.env.NETWORK === 'mainnet');

const collector = fastify.ckbIndexer.collector({
lock: {
...lockScript,
args,
},
lock: lockScript,
});

const collect = collector.collect();
Expand Down

0 comments on commit 452f818

Please sign in to comment.