Skip to content

Commit

Permalink
Merge pull request #134 from ckb-cell/develop (release v2.1.0)
Browse files Browse the repository at this point in the history
Merge develop to main branch (release v2.1.0)
  • Loading branch information
Flouse authored May 14, 2024
2 parents 4219f0e + 2cf34c5 commit 2503793
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 25 deletions.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "btc-assets-api",
"version": "2.0.0",
"version": "2.1.0",
"title": "Bitcoin/RGB++ Assets API",
"description": "",
"main": "index.js",
Expand Down Expand Up @@ -46,6 +46,7 @@
"@rgbpp-sdk/service": "0.0.0-snap-20240430102443",
"@sentry/node": "^7.102.1",
"@sentry/profiling-node": "^7.102.1",
"async-retry": "^1.3.3",
"awilix": "^10.0.1",
"axios": "^1.6.7",
"bloom-filters": "^3.0.1",
Expand All @@ -59,12 +60,14 @@
"ioredis": "^5.3.2",
"lodash": "^4.17.21",
"multicoin-address-validator": "^0.5.16",
"p-limit": "^3.1.0",
"pino": "^8.19.0",
"std-env": "^3.7.0",
"uuid": "^9.0.1",
"zod": "^3.22.4"
},
"devDependencies": {
"@types/async-retry": "^1.4.8",
"@types/lodash": "^4.17.0",
"@types/multicoin-address-validator": "^0.5.2",
"@types/node": "^20.11.17",
Expand Down
32 changes: 30 additions & 2 deletions pnpm-lock.yaml

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

5 changes: 5 additions & 0 deletions src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ const envSchema = z
* The URL of the CKB JSON-RPC server.
*/
CKB_RPC_URL: z.string(),

/**
* The async concurrency size limit for CKB RPC requests.
*/
CKB_RPC_MAX_CONCURRENCY: z.coerce.number().default(100),
/**
* Paymaster private key, used to sign the transaction with paymaster cell.
*/
Expand Down
71 changes: 49 additions & 22 deletions src/routes/rgbpp/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import { Cell, Script } from './types';
import { buildRgbppLockArgs, genRgbppLockScript } from '@rgbpp-sdk/ckb/lib/utils/rgbpp';
import { CKBIndexerQueryOptions } from '@ckb-lumos/ckb-indexer/lib/type';
import { blockchain } from '@ckb-lumos/base';
import { UTXO } from '../../services/bitcoin/schema';
import pLimit from 'p-limit';
import asyncRetry from 'async-retry';
import z from 'zod';
import { Env } from '../../env';

const addressRoutes: FastifyPluginCallback<Record<never, never>, Server, ZodTypeProvider> = (fastify, _, done) => {
fastify.addHook('preHandler', async (request) => {
Expand All @@ -17,6 +21,34 @@ const addressRoutes: FastifyPluginCallback<Record<never, never>, Server, ZodType
}
});

const env: Env = fastify.container.resolve('env');
const limit = pLimit(env.CKB_RPC_MAX_CONCURRENCY);

async function getRgbppAssetsByUtxo(utxo: UTXO, typeScript?: Script) {
try {
const { txid, vout } = utxo;
const args = buildRgbppLockArgs(vout, txid);

const query: CKBIndexerQueryOptions = {
lock: genRgbppLockScript(args, process.env.NETWORK === 'mainnet'),
};

if (typeScript) {
query.type = typeScript;
}

const collector = fastify.ckb.indexer.collector(query).collect();
const cells: Cell[] = [];
for await (const cell of collector) {
cells.push(cell);
}
return cells;
} catch (e) {
fastify.Sentry.captureException(e);
throw e;
}
}

fastify.get(
'/:btc_address/assets',
{
Expand Down Expand Up @@ -48,30 +80,25 @@ const addressRoutes: FastifyPluginCallback<Record<never, never>, Server, ZodType
const { btc_address } = request.params;
const { type_script } = request.query;
const utxos = await fastify.bitcoin.getAddressTxsUtxo({ address: btc_address });
const cells = await Promise.all(
utxos.map(async (utxo) => {
const { txid, vout } = utxo;
const args = buildRgbppLockArgs(vout, txid);

const query: CKBIndexerQueryOptions = {
lock: genRgbppLockScript(args, process.env.NETWORK === 'mainnet'),
};

if (type_script) {
if (typeof type_script === 'string') {
query.type = blockchain.Script.unpack(type_script);
} else {
query.type = type_script;
}
}
let typeScript: Script | undefined = undefined;
if (type_script) {
if (typeof type_script === 'string') {
typeScript = blockchain.Script.unpack(type_script);
} else {
typeScript = type_script;
}
}

const collector = fastify.ckb.indexer.collector(query).collect();
const cells: Cell[] = [];
for await (const cell of collector) {
cells.push(cell);
}
return cells;
}),
const cells = await Promise.all(
utxos.map((utxo) =>
limit(() =>
asyncRetry(() => getRgbppAssetsByUtxo(utxo, typeScript), {
retries: 2,
onRetry: (e, attempt) => fastify.log.warn(`[getRgbppAssetsByUtxo] ${e.message} retry ${attempt}`),
}),
),
),
);
return cells.flat();
},
Expand Down

0 comments on commit 2503793

Please sign in to comment.