Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix btc time lock rgbpp transaction query #85

Merged
merged 2 commits into from
Aug 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
Loading