@@ -11,6 +11,7 @@ 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' ;
14
15
15
16
@Injectable ( )
16
17
export class RgbppTransactionService {
@@ -21,7 +22,7 @@ export class RgbppTransactionService {
21
22
private ckbRpcService : CkbRpcWebsocketService ,
22
23
private bitcoinApiService : BitcoinApiService ,
23
24
private configService : ConfigService < Env > ,
24
- ) { }
25
+ ) { }
25
26
26
27
public async getLatestTransactions (
27
28
page : number ,
@@ -48,6 +49,34 @@ export class RgbppTransactionService {
48
49
49
50
public async getTransactionByBtcTxid ( txid : string ) : Promise < RgbppBaseTransaction | null > {
50
51
const btcTx = await this . bitcoinApiService . getTx ( { txid } ) ;
52
+ const tx = ( await this . queryRgbppLockTx ( btcTx ) ) ?? ( await this . getRgbppBtcTimeLockTx ( btcTx ) ) ;
53
+ if ( tx ) {
54
+ return tx ;
55
+ }
56
+ return null ;
57
+ }
58
+
59
+ public async getTransaction ( txidOrTxHash : string ) : Promise < RgbppBaseTransaction | null > {
60
+ let tx : RgbppBaseTransaction | null = null ;
61
+ try {
62
+ tx = await this . getTransactionByCkbTxHash ( txidOrTxHash ) ;
63
+ } catch ( err ) {
64
+ this . logger . error ( err ) ;
65
+ }
66
+ try {
67
+ tx = await this . getTransactionByBtcTxid ( txidOrTxHash ) ;
68
+ } catch ( err ) {
69
+ this . logger . error ( err ) ;
70
+ }
71
+ return tx ;
72
+ }
73
+
74
+ public async getRgbppDigest ( txHash : string ) : Promise < RgbppDigest | null > {
75
+ const response = await this . ckbExplorerService . getRgbppDigest ( txHash ) ;
76
+ return response . data ?? null ;
77
+ }
78
+
79
+ private async queryRgbppLockTx ( btcTx : BitcoinApiInterface . Transaction ) {
51
80
const ckbTxs = await Promise . all (
52
81
btcTx . vout . map ( async ( _ , index ) => {
53
82
const args = buildRgbppLockArgs ( index , btcTx . txid ) ;
@@ -75,7 +104,7 @@ export class RgbppTransactionService {
75
104
const response = await this . ckbExplorerService . getTransaction ( tx . tx_hash ) ;
76
105
if ( response . data . attributes . is_rgb_transaction ) {
77
106
const rgbppTx = RgbppTransaction . fromCkbTransaction ( response . data . attributes ) ;
78
- if ( rgbppTx . btcTxid === txid ) {
107
+ if ( rgbppTx . btcTxid === btcTx . txid ) {
79
108
return rgbppTx ;
80
109
}
81
110
}
@@ -84,23 +113,36 @@ export class RgbppTransactionService {
84
113
return null ;
85
114
}
86
115
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
- }
116
+ private async getRgbppBtcTimeLockTx ( btcTx : BitcoinApiInterface . Transaction ) {
117
+ const ckbTxs = (
118
+ await Promise . all (
119
+ btcTx . vin . map ( async ( { txid, vout } ) => {
120
+ const args = buildRgbppLockArgs ( vout , txid ) ;
121
+ const lock = genRgbppLockScript ( args , this . configService . get ( 'NETWORK' ) === 'mainnet' ) ;
122
+ return this . ckbRpcService . getTransactions (
123
+ {
124
+ script : {
125
+ code_hash : lock . codeHash ,
126
+ hash_type : lock . hashType ,
127
+ args : lock . args ,
128
+ } ,
129
+ script_type : 'lock' ,
130
+ } ,
131
+ 'asc' ,
132
+ '0x64' ,
133
+ ) ;
134
+ } ) ,
135
+ )
136
+ )
137
+ . map ( ( { objects } ) => objects )
138
+ . flat ( ) ;
101
139
102
- public async getRgbppDigest ( txHash : string ) : Promise < RgbppDigest | null > {
103
- const response = await this . ckbExplorerService . getRgbppDigest ( txHash ) ;
104
- return response . data ?? null ;
140
+ for ( const ckbTx of ckbTxs ) {
141
+ const response = await this . ckbExplorerService . getTransaction ( ckbTx . tx_hash ) ;
142
+ if ( response . data . attributes . is_btc_time_lock ) {
143
+ return RgbppTransaction . fromCkbTransaction ( response . data . attributes ) ;
144
+ }
145
+ }
146
+ return null ;
105
147
}
106
148
}
0 commit comments