Skip to content

Commit

Permalink
Merge pull request #54 from ckb-cell/feat/check-btc-time-status
Browse files Browse the repository at this point in the history
feat(rgbpp-sdk/ckb): Check btc time cells status
  • Loading branch information
duanyytop authored Mar 26, 2024
2 parents 1729406 + fea4447 commit 04f1390
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
25 changes: 24 additions & 1 deletion packages/ckb/src/rgbpp/btc-time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ import {
getXudtDep,
} from '../constants';
import { BTCTimeUnlock } from '../schemas/generated/rgbpp';
import { BtcTimeCellsParams, Hex, SignBtcTimeCellsTxParams } from '../types';
import { BtcTimeCellStatusParams, BtcTimeCellsParams, Hex, SignBtcTimeCellsTxParams } from '../types';
import {
append0x,
btcTxIdFromBtcTimeLockArgs,
calculateTransactionFee,
compareInputs,
genBtcTimeLockArgs,
lockScriptFromBtcTimeLockArgs,
} from '../utils';
import { buildSpvClientCellDep } from '../spv';
Expand Down Expand Up @@ -178,3 +179,25 @@ export const signBtcTimeCellSpentTx = async ({

return signedTx;
};

/**
* Check if the BTC time cells have been spent. If so, it means the RGB++ asset jumping(from BTC to CKB) has been successful.
* @param collector The collector that collects CKB live cells and transactions
* @param ckbAddress The CKB address
* @param btcTxId The BTC transaction id
*/
export const isBtcTimeCellsSpent = async ({
collector,
ckbAddress,
btcTxId,
}: BtcTimeCellStatusParams): Promise<boolean> => {
const isMainnet = ckbAddress.startsWith('ckb');
const lock = addressToScript(ckbAddress);
const btcTimeLock: CKBComponents.Script = {
...getBtcTimeLockScript(isMainnet),
args: genBtcTimeLockArgs(lock, btcTxId, BTC_JUMP_CONFIRMATION_BLOCKS),
};
const btcTimeCells = await collector.getCells({ lock: btcTimeLock, isDataEmpty: false });
const isSpent = !btcTimeCells || btcTimeCells.length === 0;
return isSpent;
};
9 changes: 9 additions & 0 deletions packages/ckb/src/types/rgbpp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,12 @@ export interface UpdateCkbTxWithRealBtcTxIdParams {
btcTxId: Hex;
isMainnet: boolean;
}

export interface BtcTimeCellStatusParams {
// The collector that collects CKB live cells and transactions
collector: Collector;
// The ckb address who is the receiver of the btc time lock args
ckbAddress: Address;
// The BTC transaction id
btcTxId: Hex;
}

1 comment on commit 04f1390

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[{"name":"@rgbpp-sdk/btc","version":"0.0.0-snap-20240326094811"},{"name":"@rgbpp-sdk/ckb","version":"0.0.0-snap-20240326094811"}]

Please sign in to comment.