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

refactor(btc): Batch fetching CKB RGB++ live cells #261

Merged
merged 3 commits into from
Jul 27, 2024
Merged
Show file tree
Hide file tree
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
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