@@ -11,6 +11,18 @@ import { ConfigService } from '@nestjs/config';
11
11
import { Env } from 'src/env' ;
12
12
import { CkbRpcWebsocketService } from 'src/core/ckb-rpc/ckb-rpc-websocket.service' ;
13
13
import { buildRgbppLockArgs , genRgbppLockScript } from '@rgbpp-sdk/ckb/lib/utils/rgbpp' ;
14
+ import * as BitcoinApiInterface from 'src/core/bitcoin-api/bitcoin-api.schema' ;
15
+ import {
16
+ getBtcTimeLockScript ,
17
+ isScriptEqual ,
18
+ remove0x ,
19
+ btcTxIdAndAfterFromBtcTimeLockArgs ,
20
+ } from '@rgbpp-sdk/ckb' ;
21
+ import { SearchKey } from 'src/core/ckb-rpc/ckb-rpc.interface' ;
22
+ import * as pLimit from 'p-limit' ;
23
+ import { BI , HashType } from '@ckb-lumos/lumos' ;
24
+
25
+ const limit = pLimit ( 100 ) ;
14
26
15
27
@Injectable ( )
16
28
export class RgbppTransactionService {
@@ -21,7 +33,7 @@ export class RgbppTransactionService {
21
33
private ckbRpcService : CkbRpcWebsocketService ,
22
34
private bitcoinApiService : BitcoinApiService ,
23
35
private configService : ConfigService < Env > ,
24
- ) { }
36
+ ) { }
25
37
26
38
public async getLatestTransactions (
27
39
page : number ,
@@ -48,6 +60,34 @@ export class RgbppTransactionService {
48
60
49
61
public async getTransactionByBtcTxid ( txid : string ) : Promise < RgbppBaseTransaction | null > {
50
62
const btcTx = await this . bitcoinApiService . getTx ( { txid } ) ;
63
+ const tx = ( await this . queryRgbppLockTx ( btcTx ) ) ?? ( await this . getRgbppBtcTimeLockTx ( btcTx ) ) ;
64
+ if ( tx ) {
65
+ return tx ;
66
+ }
67
+ return null ;
68
+ }
69
+
70
+ public async getTransaction ( txidOrTxHash : string ) : Promise < RgbppBaseTransaction | null > {
71
+ let tx : RgbppBaseTransaction | null = null ;
72
+ try {
73
+ tx = await this . getTransactionByCkbTxHash ( txidOrTxHash ) ;
74
+ } catch ( err ) {
75
+ this . logger . error ( err ) ;
76
+ }
77
+ try {
78
+ tx = await this . getTransactionByBtcTxid ( txidOrTxHash ) ;
79
+ } catch ( err ) {
80
+ this . logger . error ( err ) ;
81
+ }
82
+ return tx ;
83
+ }
84
+
85
+ public async getRgbppDigest ( txHash : string ) : Promise < RgbppDigest | null > {
86
+ const response = await this . ckbExplorerService . getRgbppDigest ( txHash ) ;
87
+ return response . data ?? null ;
88
+ }
89
+
90
+ private async queryRgbppLockTx ( btcTx : BitcoinApiInterface . Transaction ) {
51
91
const ckbTxs = await Promise . all (
52
92
btcTx . vout . map ( async ( _ , index ) => {
53
93
const args = buildRgbppLockArgs ( index , btcTx . txid ) ;
@@ -75,7 +115,7 @@ export class RgbppTransactionService {
75
115
const response = await this . ckbExplorerService . getTransaction ( tx . tx_hash ) ;
76
116
if ( response . data . attributes . is_rgb_transaction ) {
77
117
const rgbppTx = RgbppTransaction . fromCkbTransaction ( response . data . attributes ) ;
78
- if ( rgbppTx . btcTxid === txid ) {
118
+ if ( rgbppTx . btcTxid === btcTx . txid ) {
79
119
return rgbppTx ;
80
120
}
81
121
}
@@ -84,23 +124,36 @@ export class RgbppTransactionService {
84
124
return null ;
85
125
}
86
126
87
- public async getTransaction ( txidOrTxHash : string ) : Promise < RgbppBaseTransaction | null > {
88
- let tx : RgbppBaseTransaction | null = null ;
89
- try {
90
- tx = await this . getTransactionByCkbTxHash ( txidOrTxHash ) ;
91
- } catch ( err ) {
92
- this . logger . error ( err ) ;
93
- }
94
- try {
95
- tx = await this . getTransactionByBtcTxid ( txidOrTxHash ) ;
96
- } catch ( err ) {
97
- this . logger . error ( err ) ;
98
- }
99
- return tx ;
100
- }
127
+ private async getRgbppBtcTimeLockTx ( btcTx : BitcoinApiInterface . Transaction ) {
128
+ const ckbTxs = (
129
+ await Promise . all (
130
+ btcTx . vin . map ( async ( { txid, vout } ) => {
131
+ const args = buildRgbppLockArgs ( vout , txid ) ;
132
+ const lock = genRgbppLockScript ( args , this . configService . get ( 'NETWORK' ) === 'mainnet' ) ;
133
+ return this . ckbRpcService . getTransactions (
134
+ {
135
+ script : {
136
+ code_hash : lock . codeHash ,
137
+ hash_type : lock . hashType ,
138
+ args : lock . args ,
139
+ } ,
140
+ script_type : 'lock' ,
141
+ } ,
142
+ 'asc' ,
143
+ '0x64' ,
144
+ ) ;
145
+ } ) ,
146
+ )
147
+ )
148
+ . map ( ( { objects } ) => objects )
149
+ . flat ( ) ;
101
150
102
- public async getRgbppDigest ( txHash : string ) : Promise < RgbppDigest | null > {
103
- const response = await this . ckbExplorerService . getRgbppDigest ( txHash ) ;
104
- return response . data ?? null ;
151
+ for ( const ckbTx of ckbTxs ) {
152
+ const response = await this . ckbExplorerService . getTransaction ( ckbTx . tx_hash ) ;
153
+ if ( response . data . attributes . is_btc_time_lock ) {
154
+ return RgbppTransaction . fromCkbTransaction ( response . data . attributes ) ;
155
+ }
156
+ }
157
+ return null ;
105
158
}
106
159
}
0 commit comments