Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit 5a1e550

Browse files
committed
fix: fix btc time lock rgbpp transaction query
1 parent 250fcc1 commit 5a1e550

File tree

1 file changed

+72
-19
lines changed

1 file changed

+72
-19
lines changed

backend/src/modules/rgbpp/transaction/transaction.service.ts

Lines changed: 72 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@ import { ConfigService } from '@nestjs/config';
1111
import { Env } from 'src/env';
1212
import { CkbRpcWebsocketService } from 'src/core/ckb-rpc/ckb-rpc-websocket.service';
1313
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);
1426

1527
@Injectable()
1628
export class RgbppTransactionService {
@@ -21,7 +33,7 @@ export class RgbppTransactionService {
2133
private ckbRpcService: CkbRpcWebsocketService,
2234
private bitcoinApiService: BitcoinApiService,
2335
private configService: ConfigService<Env>,
24-
) { }
36+
) {}
2537

2638
public async getLatestTransactions(
2739
page: number,
@@ -48,6 +60,34 @@ export class RgbppTransactionService {
4860

4961
public async getTransactionByBtcTxid(txid: string): Promise<RgbppBaseTransaction | null> {
5062
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) {
5191
const ckbTxs = await Promise.all(
5292
btcTx.vout.map(async (_, index) => {
5393
const args = buildRgbppLockArgs(index, btcTx.txid);
@@ -75,7 +115,7 @@ export class RgbppTransactionService {
75115
const response = await this.ckbExplorerService.getTransaction(tx.tx_hash);
76116
if (response.data.attributes.is_rgb_transaction) {
77117
const rgbppTx = RgbppTransaction.fromCkbTransaction(response.data.attributes);
78-
if (rgbppTx.btcTxid === txid) {
118+
if (rgbppTx.btcTxid === btcTx.txid) {
79119
return rgbppTx;
80120
}
81121
}
@@ -84,23 +124,36 @@ export class RgbppTransactionService {
84124
return null;
85125
}
86126

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();
101150

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;
105158
}
106159
}

0 commit comments

Comments
 (0)