Skip to content

Commit

Permalink
Merge pull request #85 from ckb-cell/fix/rgbpp-transaction-query
Browse files Browse the repository at this point in the history
fix: fix btc time lock rgbpp transaction query
  • Loading branch information
Flouse authored Aug 7, 2024
2 parents 250fcc1 + 85eb045 commit 1e00d45
Showing 1 changed file with 61 additions and 19 deletions.
80 changes: 61 additions & 19 deletions backend/src/modules/rgbpp/transaction/transaction.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ 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';

@Injectable()
export class RgbppTransactionService {
Expand All @@ -21,7 +22,7 @@ export class RgbppTransactionService {
private ckbRpcService: CkbRpcWebsocketService,
private bitcoinApiService: BitcoinApiService,
private configService: ConfigService<Env>,
) { }
) {}

public async getLatestTransactions(
page: number,
Expand All @@ -48,6 +49,34 @@ export class RgbppTransactionService {

public async getTransactionByBtcTxid(txid: string): Promise<RgbppBaseTransaction | null> {
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<RgbppBaseTransaction | null> {
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<RgbppDigest | null> {
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);
Expand Down Expand Up @@ -75,7 +104,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;
}
}
Expand All @@ -84,23 +113,36 @@ export class RgbppTransactionService {
return null;
}

public async getTransaction(txidOrTxHash: string): Promise<RgbppBaseTransaction | null> {
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<RgbppDigest | null> {
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;
}
}

0 comments on commit 1e00d45

Please sign in to comment.