@@ -6,7 +6,11 @@ import { Cell, Script } from './types';
6
6
import { buildRgbppLockArgs , genRgbppLockScript } from '@rgbpp-sdk/ckb/lib/utils/rgbpp' ;
7
7
import { CKBIndexerQueryOptions } from '@ckb-lumos/ckb-indexer/lib/type' ;
8
8
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' ;
9
12
import z from 'zod' ;
13
+ import { Env } from '../../env' ;
10
14
11
15
const addressRoutes : FastifyPluginCallback < Record < never , never > , Server , ZodTypeProvider > = ( fastify , _ , done ) => {
12
16
fastify . addHook ( 'preHandler' , async ( request ) => {
@@ -17,6 +21,34 @@ const addressRoutes: FastifyPluginCallback<Record<never, never>, Server, ZodType
17
21
}
18
22
} ) ;
19
23
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
+
20
52
fastify . get (
21
53
'/:btc_address/assets' ,
22
54
{
@@ -48,30 +80,25 @@ const addressRoutes: FastifyPluginCallback<Record<never, never>, Server, ZodType
48
80
const { btc_address } = request . params ;
49
81
const { type_script } = request . query ;
50
82
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
- } ;
59
83
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
+ }
67
92
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
+ ) ,
75
102
) ;
76
103
return cells . flat ( ) ;
77
104
} ,
0 commit comments