Skip to content

Commit

Permalink
refactor(btc): Batch fetching CKB RGB++ live cells (#261)
Browse files Browse the repository at this point in the history
* refactor: Batch query ckb live cells
* chore: Add changeset

---------

Co-authored-by: Flouse <1297478+Flouse@users.noreply.github.com>
  • Loading branch information
duanyytop and Flouse authored Jul 27, 2024
1 parent f816626 commit 80cc35d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 26 deletions.
5 changes: 5 additions & 0 deletions .changeset/shy-tables-turn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rgbpp-sdk/btc': minor
---

refactor: Batch fetching ckb rgb++ live cells
43 changes: 17 additions & 26 deletions packages/btc/src/api/sendRgbppUtxos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,54 +54,45 @@ export async function createSendRgbppUtxosBuilder(props: SendRgbppUtxosProps): P
const config = networkTypeToConfig(props.source.networkType);
const isCkbMainnet = props.source.networkType === NetworkType.MAINNET;

// Batch querying live cells from CkbCollector
const ckbLiveCells = await Promise.all(
ckbVirtualTx.inputs.map((ckbInput) => {
return limitPromiseBatchSize(async () => {
const ckbLiveCell = await props.ckbCollector.getLiveCell(ckbInput.previousOutput!);
const isRgbppLock = isRgbppLockCell(ckbLiveCell.output, isCkbMainnet);
const lockArgs = isRgbppLock ? unpackRgbppLockArgs(ckbLiveCell.output.lock.args) : undefined;
return {
ckbLiveCell,
isRgbppLock,
lockArgs,
};
});
}),
const rgbppLockArgsList = (
await props.ckbCollector.getLiveCells(ckbVirtualTx.inputs.map((input) => input.previousOutput!))
).map((cell) =>
isRgbppLockCell(cell.output, isCkbMainnet) ? unpackRgbppLockArgs(cell.output.lock.args) : undefined,
);

// Batch querying UTXO from BtcAssetsApi
const btcUtxos = await Promise.all(
ckbLiveCells.map(({ ckbLiveCell, isRgbppLock }) => {
if (isRgbppLock) {
const args = unpackRgbppLockArgs(ckbLiveCell.output.lock.args);
return limitPromiseBatchSize(() => props.source.getUtxo(args.btcTxid, args.outIndex, props.onlyConfirmedUtxos));
rgbppLockArgsList.map((rgbppLockArgs) => {
if (rgbppLockArgs) {
return limitPromiseBatchSize(() =>
props.source.getUtxo(rgbppLockArgs.btcTxid, rgbppLockArgs.outIndex, props.onlyConfirmedUtxos),
);
}
return undefined;
}),
);

// Handle and check inputs
for (let i = 0; i < ckbVirtualTx.inputs.length; i++) {
const { lockArgs, isRgbppLock } = ckbLiveCells[i];
const rgbppLockArgs = rgbppLockArgsList[i];

// Add to inputs if all the following conditions are met:
// 1. input.lock.args can be unpacked to RgbppLockArgs
// 2. utxo can be found via the DataSource.getUtxo() API
// 3. utxo is not duplicated in the inputs
if (isRgbppLock) {
const args = lockArgs!;
if (rgbppLockArgs) {
const utxo = btcUtxos[i];
if (!utxo) {
throw TxBuildError.withComment(ErrorCodes.CANNOT_FIND_UTXO, `hash: ${args.btcTxid}, index: ${args.outIndex}`);
throw TxBuildError.withComment(
ErrorCodes.CANNOT_FIND_UTXO,
`hash: ${rgbppLockArgs.btcTxid}, index: ${rgbppLockArgs.outIndex}`,
);
}

const foundInInputs = btcInputs.some((v) => v.txid === utxo.txid && v.vout === utxo.vout);
if (foundInInputs) {
continue;
if (!foundInInputs) {
btcInputs.push(utxo);
}

btcInputs.push(utxo);
}
}

Expand Down

1 comment on commit 80cc35d

@github-actions
Copy link

Choose a reason for hiding this comment

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

New snapshot version of the rgbpp-sdk packages have been released:

Name Version
@rgbpp-sdk/btc 0.0.0-snap-20240727021540
@rgbpp-sdk/ckb 0.0.0-snap-20240727021540
rgbpp 0.0.0-snap-20240727021540
@rgbpp-sdk/service 0.0.0-snap-20240727021540

Please sign in to comment.