From 5a1e550680f67f287b101d203e759784bd73a15a Mon Sep 17 00:00:00 2001 From: ahonn Date: Wed, 7 Aug 2024 18:04:56 +1000 Subject: [PATCH 1/2] fix: fix btc time lock rgbpp transaction query --- .../rgbpp/transaction/transaction.service.ts | 91 +++++++++++++++---- 1 file changed, 72 insertions(+), 19 deletions(-) diff --git a/backend/src/modules/rgbpp/transaction/transaction.service.ts b/backend/src/modules/rgbpp/transaction/transaction.service.ts index ee9fc74b..0e4278bb 100644 --- a/backend/src/modules/rgbpp/transaction/transaction.service.ts +++ b/backend/src/modules/rgbpp/transaction/transaction.service.ts @@ -11,6 +11,18 @@ import { ConfigService } from '@nestjs/config'; import { Env } from 'src/env'; import { CkbRpcWebsocketService } from 'src/core/ckb-rpc/ckb-rpc-websocket.service'; import { buildRgbppLockArgs, genRgbppLockScript } from '@rgbpp-sdk/ckb/lib/utils/rgbpp'; +import * as BitcoinApiInterface from 'src/core/bitcoin-api/bitcoin-api.schema'; +import { + getBtcTimeLockScript, + isScriptEqual, + remove0x, + btcTxIdAndAfterFromBtcTimeLockArgs, +} from '@rgbpp-sdk/ckb'; +import { SearchKey } from 'src/core/ckb-rpc/ckb-rpc.interface'; +import * as pLimit from 'p-limit'; +import { BI, HashType } from '@ckb-lumos/lumos'; + +const limit = pLimit(100); @Injectable() export class RgbppTransactionService { @@ -21,7 +33,7 @@ export class RgbppTransactionService { private ckbRpcService: CkbRpcWebsocketService, private bitcoinApiService: BitcoinApiService, private configService: ConfigService, - ) { } + ) {} public async getLatestTransactions( page: number, @@ -48,6 +60,34 @@ export class RgbppTransactionService { public async getTransactionByBtcTxid(txid: string): Promise { const btcTx = await this.bitcoinApiService.getTx({ txid }); + const tx = (await this.queryRgbppLockTx(btcTx)) ?? (await this.getRgbppBtcTimeLockTx(btcTx)); + if (tx) { + return tx; + } + return null; + } + + public async getTransaction(txidOrTxHash: string): Promise { + let tx: RgbppBaseTransaction | null = null; + try { + tx = await this.getTransactionByCkbTxHash(txidOrTxHash); + } catch (err) { + this.logger.error(err); + } + try { + tx = await this.getTransactionByBtcTxid(txidOrTxHash); + } catch (err) { + this.logger.error(err); + } + return tx; + } + + public async getRgbppDigest(txHash: string): Promise { + const response = await this.ckbExplorerService.getRgbppDigest(txHash); + return response.data ?? null; + } + + private async queryRgbppLockTx(btcTx: BitcoinApiInterface.Transaction) { const ckbTxs = await Promise.all( btcTx.vout.map(async (_, index) => { const args = buildRgbppLockArgs(index, btcTx.txid); @@ -75,7 +115,7 @@ export class RgbppTransactionService { const response = await this.ckbExplorerService.getTransaction(tx.tx_hash); if (response.data.attributes.is_rgb_transaction) { const rgbppTx = RgbppTransaction.fromCkbTransaction(response.data.attributes); - if (rgbppTx.btcTxid === txid) { + if (rgbppTx.btcTxid === btcTx.txid) { return rgbppTx; } } @@ -84,23 +124,36 @@ export class RgbppTransactionService { return null; } - public async getTransaction(txidOrTxHash: string): Promise { - let tx: RgbppBaseTransaction | null = null; - try { - tx = await this.getTransactionByCkbTxHash(txidOrTxHash); - } catch (err) { - this.logger.error(err); - } - try { - tx = await this.getTransactionByBtcTxid(txidOrTxHash); - } catch (err) { - this.logger.error(err); - } - return tx; - } + private async getRgbppBtcTimeLockTx(btcTx: BitcoinApiInterface.Transaction) { + const ckbTxs = ( + await Promise.all( + btcTx.vin.map(async ({ txid, vout }) => { + const args = buildRgbppLockArgs(vout, txid); + const lock = genRgbppLockScript(args, this.configService.get('NETWORK') === 'mainnet'); + return this.ckbRpcService.getTransactions( + { + script: { + code_hash: lock.codeHash, + hash_type: lock.hashType, + args: lock.args, + }, + script_type: 'lock', + }, + 'asc', + '0x64', + ); + }), + ) + ) + .map(({ objects }) => objects) + .flat(); - public async getRgbppDigest(txHash: string): Promise { - const response = await this.ckbExplorerService.getRgbppDigest(txHash); - return response.data ?? null; + for (const ckbTx of ckbTxs) { + const response = await this.ckbExplorerService.getTransaction(ckbTx.tx_hash); + if (response.data.attributes.is_btc_time_lock) { + return RgbppTransaction.fromCkbTransaction(response.data.attributes); + } + } + return null; } } From 85eb0453c08b28f93b6028d094d3b7099b273be4 Mon Sep 17 00:00:00 2001 From: ahonn Date: Wed, 7 Aug 2024 18:30:12 +1000 Subject: [PATCH 2/2] fix: cleanup imports --- .../modules/rgbpp/transaction/transaction.service.ts | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/backend/src/modules/rgbpp/transaction/transaction.service.ts b/backend/src/modules/rgbpp/transaction/transaction.service.ts index 0e4278bb..3b8c293b 100644 --- a/backend/src/modules/rgbpp/transaction/transaction.service.ts +++ b/backend/src/modules/rgbpp/transaction/transaction.service.ts @@ -12,17 +12,6 @@ import { Env } from 'src/env'; import { CkbRpcWebsocketService } from 'src/core/ckb-rpc/ckb-rpc-websocket.service'; import { buildRgbppLockArgs, genRgbppLockScript } from '@rgbpp-sdk/ckb/lib/utils/rgbpp'; import * as BitcoinApiInterface from 'src/core/bitcoin-api/bitcoin-api.schema'; -import { - getBtcTimeLockScript, - isScriptEqual, - remove0x, - btcTxIdAndAfterFromBtcTimeLockArgs, -} from '@rgbpp-sdk/ckb'; -import { SearchKey } from 'src/core/ckb-rpc/ckb-rpc.interface'; -import * as pLimit from 'p-limit'; -import { BI, HashType } from '@ckb-lumos/lumos'; - -const limit = pLimit(100); @Injectable() export class RgbppTransactionService {