Skip to content

Commit 2503793

Browse files
authored
Merge pull request #134 from ckb-cell/develop (release v2.1.0)
Merge develop to main branch (release v2.1.0)
2 parents 4219f0e + 2cf34c5 commit 2503793

File tree

4 files changed

+88
-25
lines changed

4 files changed

+88
-25
lines changed

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "btc-assets-api",
3-
"version": "2.0.0",
3+
"version": "2.1.0",
44
"title": "Bitcoin/RGB++ Assets API",
55
"description": "",
66
"main": "index.js",
@@ -46,6 +46,7 @@
4646
"@rgbpp-sdk/service": "0.0.0-snap-20240430102443",
4747
"@sentry/node": "^7.102.1",
4848
"@sentry/profiling-node": "^7.102.1",
49+
"async-retry": "^1.3.3",
4950
"awilix": "^10.0.1",
5051
"axios": "^1.6.7",
5152
"bloom-filters": "^3.0.1",
@@ -59,12 +60,14 @@
5960
"ioredis": "^5.3.2",
6061
"lodash": "^4.17.21",
6162
"multicoin-address-validator": "^0.5.16",
63+
"p-limit": "^3.1.0",
6264
"pino": "^8.19.0",
6365
"std-env": "^3.7.0",
6466
"uuid": "^9.0.1",
6567
"zod": "^3.22.4"
6668
},
6769
"devDependencies": {
70+
"@types/async-retry": "^1.4.8",
6871
"@types/lodash": "^4.17.0",
6972
"@types/multicoin-address-validator": "^0.5.2",
7073
"@types/node": "^20.11.17",

pnpm-lock.yaml

Lines changed: 30 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/env.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ const envSchema = z
8888
* The URL of the CKB JSON-RPC server.
8989
*/
9090
CKB_RPC_URL: z.string(),
91+
92+
/**
93+
* The async concurrency size limit for CKB RPC requests.
94+
*/
95+
CKB_RPC_MAX_CONCURRENCY: z.coerce.number().default(100),
9196
/**
9297
* Paymaster private key, used to sign the transaction with paymaster cell.
9398
*/

src/routes/rgbpp/address.ts

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ import { Cell, Script } from './types';
66
import { buildRgbppLockArgs, genRgbppLockScript } from '@rgbpp-sdk/ckb/lib/utils/rgbpp';
77
import { CKBIndexerQueryOptions } from '@ckb-lumos/ckb-indexer/lib/type';
88
import { blockchain } from '@ckb-lumos/base';
9+
import { UTXO } from '../../services/bitcoin/schema';
10+
import pLimit from 'p-limit';
11+
import asyncRetry from 'async-retry';
912
import z from 'zod';
13+
import { Env } from '../../env';
1014

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

24+
const env: Env = fastify.container.resolve('env');
25+
const limit = pLimit(env.CKB_RPC_MAX_CONCURRENCY);
26+
27+
async function getRgbppAssetsByUtxo(utxo: UTXO, typeScript?: Script) {
28+
try {
29+
const { txid, vout } = utxo;
30+
const args = buildRgbppLockArgs(vout, txid);
31+
32+
const query: CKBIndexerQueryOptions = {
33+
lock: genRgbppLockScript(args, process.env.NETWORK === 'mainnet'),
34+
};
35+
36+
if (typeScript) {
37+
query.type = typeScript;
38+
}
39+
40+
const collector = fastify.ckb.indexer.collector(query).collect();
41+
const cells: Cell[] = [];
42+
for await (const cell of collector) {
43+
cells.push(cell);
44+
}
45+
return cells;
46+
} catch (e) {
47+
fastify.Sentry.captureException(e);
48+
throw e;
49+
}
50+
}
51+
2052
fastify.get(
2153
'/:btc_address/assets',
2254
{
@@ -48,30 +80,25 @@ const addressRoutes: FastifyPluginCallback<Record<never, never>, Server, ZodType
4880
const { btc_address } = request.params;
4981
const { type_script } = request.query;
5082
const utxos = await fastify.bitcoin.getAddressTxsUtxo({ address: btc_address });
51-
const cells = await Promise.all(
52-
utxos.map(async (utxo) => {
53-
const { txid, vout } = utxo;
54-
const args = buildRgbppLockArgs(vout, txid);
55-
56-
const query: CKBIndexerQueryOptions = {
57-
lock: genRgbppLockScript(args, process.env.NETWORK === 'mainnet'),
58-
};
5983

60-
if (type_script) {
61-
if (typeof type_script === 'string') {
62-
query.type = blockchain.Script.unpack(type_script);
63-
} else {
64-
query.type = type_script;
65-
}
66-
}
84+
let typeScript: Script | undefined = undefined;
85+
if (type_script) {
86+
if (typeof type_script === 'string') {
87+
typeScript = blockchain.Script.unpack(type_script);
88+
} else {
89+
typeScript = type_script;
90+
}
91+
}
6792

68-
const collector = fastify.ckb.indexer.collector(query).collect();
69-
const cells: Cell[] = [];
70-
for await (const cell of collector) {
71-
cells.push(cell);
72-
}
73-
return cells;
74-
}),
93+
const cells = await Promise.all(
94+
utxos.map((utxo) =>
95+
limit(() =>
96+
asyncRetry(() => getRgbppAssetsByUtxo(utxo, typeScript), {
97+
retries: 2,
98+
onRetry: (e, attempt) => fastify.log.warn(`[getRgbppAssetsByUtxo] ${e.message} retry ${attempt}`),
99+
}),
100+
),
101+
),
75102
);
76103
return cells.flat();
77104
},

0 commit comments

Comments
 (0)