Skip to content

Commit

Permalink
Merge pull request utxostack#191 from ckb-cell/feat/dynamic-fetch-cel…
Browse files Browse the repository at this point in the history
…l-dep

feat(ckb|example): Dynamic fetching cell deps deployed by TypeID
  • Loading branch information
Flouse authored Jun 2, 2024
2 parents b7a8309 + 1310d40 commit 207ca98
Show file tree
Hide file tree
Showing 14 changed files with 207 additions and 63 deletions.
8 changes: 5 additions & 3 deletions examples/xudt-on-ckb/1-issue-xudt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ import {
getUniqueTypeScript,
u128ToLe,
encodeRgbppTokenInfo,
getXudtDep,
getUniqueTypeDep,
SECP256K1_WITNESS_LOCK_SIZE,
calculateTransactionFee,
generateUniqueTypeArgs,
calculateXudtTokenInfoCellCapacity,
fetchTypeIdCellDeps,
} from 'rgbpp/ckb';
import { CKB_PRIVATE_KEY, ckbAddress, collector, isMainnet } from './env';

Expand Down Expand Up @@ -77,7 +76,10 @@ const issueXudt = async ({ xudtTotalAmount, tokenInfo }: { xudtTotalAmount: bigi
const emptyWitness = { lock: '', inputType: '', outputType: '' };
const witnesses = inputs.map((_, index) => (index === 0 ? emptyWitness : '0x'));

const cellDeps = [getSecp256k1CellDep(isMainnet), getUniqueTypeDep(isMainnet), getXudtDep(isMainnet)];
const cellDeps = [
getSecp256k1CellDep(isMainnet),
...(await fetchTypeIdCellDeps(isMainnet, { xudt: true, unique: true })),
];

const unsignedTx = {
version: '0x0',
Expand Down
5 changes: 2 additions & 3 deletions examples/xudt-on-ckb/2-transfer-xudt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ import {
MIN_CAPACITY,
append0x,
u128ToLe,
getXudtDep,
getUniqueTypeDep,
SECP256K1_WITNESS_LOCK_SIZE,
calculateTransactionFee,
NoXudtLiveCellError,
fetchTypeIdCellDeps,
} from 'rgbpp/ckb';
import { CKB_PRIVATE_KEY, ckbAddress, collector, isMainnet } from './env';

Expand Down Expand Up @@ -104,7 +103,7 @@ const transferXudt = async ({ xudtType, receivers }: XudtTransferParams) => {
const emptyWitness = { lock: '', inputType: '', outputType: '' };
const witnesses = inputs.map((_, index) => (index === 0 ? emptyWitness : '0x'));

const cellDeps = [getSecp256k1CellDep(isMainnet), getUniqueTypeDep(isMainnet), getXudtDep(isMainnet)];
const cellDeps = [getSecp256k1CellDep(isMainnet), ...(await fetchTypeIdCellDeps(isMainnet, { xudt: true }))];

const unsignedTx = {
version: '0x0',
Expand Down
4 changes: 2 additions & 2 deletions packages/ckb/src/collector/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,9 @@ export class Collector {
return { inputs, sumInputsCapacity, sumAmount };
}

async getLiveCell(outPoint: CKBComponents.OutPoint): Promise<CKBComponents.LiveCell> {
async getLiveCell(outPoint: CKBComponents.OutPoint, withData = true): Promise<CKBComponents.LiveCell> {
const ckb = new CKB(this.ckbNodeUrl);
const { cell } = await ckb.rpc.getLiveCell(outPoint, true);
const { cell } = await ckb.rpc.getLiveCell(outPoint, withData);
return cell;
}
}
11 changes: 3 additions & 8 deletions packages/ckb/src/rgbpp/btc-jump-ckb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
calculateRgbppCellCapacity,
calculateTransactionFee,
deduplicateList,
fetchTypeIdCellDeps,
isLockArgsSizeExceeded,
isScriptEqual,
isUDTTypeSupported,
Expand All @@ -23,13 +24,7 @@ import {
isRgbppCapacitySufficientForChange,
} from '../utils/rgbpp';
import { Hex, IndexerCell } from '../types';
import {
RGBPP_WITNESS_PLACEHOLDER,
getRgbppLockConfigDep,
getRgbppLockDep,
getSecp256k1CellDep,
getXudtDep,
} from '../constants';
import { RGBPP_WITNESS_PLACEHOLDER, getSecp256k1CellDep } from '../constants';
import { addressToScript, getTransactionSize } from '@nervosnetwork/ckb-sdk-utils';

/**
Expand Down Expand Up @@ -139,7 +134,7 @@ export const genBtcJumpCkbVirtualTx = async ({
outputsData.push(otherRgbppCell.outputData);
}

const cellDeps = [getRgbppLockDep(isMainnet), getXudtDep(isMainnet), getRgbppLockConfigDep(isMainnet)];
const cellDeps = await fetchTypeIdCellDeps(isMainnet, { rgbpp: true, xudt: true });
if (needPaymasterCell) {
cellDeps.push(getSecp256k1CellDep(isMainnet));
}
Expand Down
10 changes: 2 additions & 8 deletions packages/ckb/src/rgbpp/btc-time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@ import {
import {
BTC_JUMP_CONFIRMATION_BLOCKS,
SECP256K1_WITNESS_LOCK_SIZE,
getBtcTimeLockConfigDep,
getBtcTimeLockDep,
getBtcTimeLockScript,
getSecp256k1CellDep,
getXudtDep,
} from '../constants';
import { BTCTimeUnlock } from '../schemas/generated/rgbpp';
import { BtcTimeCellStatusParams, BtcTimeCellsParams, Hex, SignBtcTimeCellsTxParams } from '../types';
Expand All @@ -23,6 +20,7 @@ import {
btcTxIdFromBtcTimeLockArgs,
calculateTransactionFee,
compareInputs,
fetchTypeIdCellDeps,
genBtcTimeLockArgs,
lockScriptFromBtcTimeLockArgs,
transformSpvProof,
Expand Down Expand Up @@ -62,11 +60,7 @@ export const buildBtcTimeCellsSpentTx = async ({

const outputsData = sortedBtcTimeCells.map((cell) => cell.outputData);

const cellDeps: CKBComponents.CellDep[] = [
getBtcTimeLockDep(isMainnet),
getXudtDep(isMainnet),
getBtcTimeLockConfigDep(isMainnet),
];
const cellDeps: CKBComponents.CellDep[] = await fetchTypeIdCellDeps(isMainnet, { btcTime: true, xudt: true });

const witnesses: Hex[] = [];

Expand Down
11 changes: 3 additions & 8 deletions packages/ckb/src/rgbpp/btc-transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
calculateRgbppCellCapacity,
calculateTransactionFee,
deduplicateList,
fetchTypeIdCellDeps,
isScriptEqual,
isUDTTypeSupported,
u128ToLe,
Expand All @@ -33,11 +34,8 @@ import {
MIN_CAPACITY,
RGBPP_WITNESS_PLACEHOLDER,
SECP256K1_WITNESS_LOCK_SIZE,
getRgbppLockConfigDep,
getRgbppLockDep,
getRgbppLockScript,
getSecp256k1CellDep,
getXudtDep,
} from '../constants';
import {
addressToScript,
Expand Down Expand Up @@ -172,7 +170,7 @@ export const genBtcTransferCkbVirtualTx = async ({
handleNonTargetRgbppCells(outputs.length);
}

const cellDeps = [getRgbppLockDep(isMainnet), getXudtDep(isMainnet), getRgbppLockConfigDep(isMainnet)];
const cellDeps = await fetchTypeIdCellDeps(isMainnet, { rgbpp: true, xudt: true });
if (needPaymasterCell) {
cellDeps.push(getSecp256k1CellDep(isMainnet));
}
Expand Down Expand Up @@ -287,12 +285,9 @@ export const genBtcBatchTransferCkbVirtualTx = async ({
}

const cellDeps = [
getRgbppLockDep(isMainnet),
getXudtDep(isMainnet),
getRgbppLockConfigDep(isMainnet),
...(await fetchTypeIdCellDeps(isMainnet, { rgbpp: true, xudt: true })),
getSecp256k1CellDep(isMainnet),
];

const witnesses: Hex[] = [];
const lockArgsSet: Set<string> = new Set();
for (const cell of rgbppCells) {
Expand Down
7 changes: 4 additions & 3 deletions packages/ckb/src/rgbpp/ckb-jump-btc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import {
calculateRgbppCellCapacity,
calculateTransactionFee,
calculateUdtCellCapacity,
fetchTypeIdCellDeps,
isTypeAssetSupported,
u128ToLe,
} from '../utils';
import { genRgbppLockScript } from '../utils/rgbpp';
import { MAX_FEE, MIN_CAPACITY, RGBPP_TX_WITNESS_MAX_SIZE, getXudtDep } from '../constants';
import { MAX_FEE, MIN_CAPACITY, RGBPP_TX_WITNESS_MAX_SIZE } from '../constants';
import { addressToScript, getTransactionSize } from '@nervosnetwork/ckb-sdk-utils';

/**
Expand Down Expand Up @@ -98,7 +99,7 @@ export const genCkbJumpBtcVirtualTx = async ({
});
outputsData.push('0x');

const cellDeps = [getXudtDep(isMainnet)];
const cellDeps = await fetchTypeIdCellDeps(isMainnet, { xudt: true });
const witnesses = inputs.map(() => '0x');

const ckbRawTx: CKBComponents.RawTransaction = {
Expand Down Expand Up @@ -206,7 +207,7 @@ export const genCkbBatchJumpBtcVirtualTx = async ({
});
outputsData.push('0x');

const cellDeps = [getXudtDep(isMainnet)];
const cellDeps = await fetchTypeIdCellDeps(isMainnet, { xudt: true });
const witnesses = inputs.map(() => '0x');

const ckbRawTx: CKBComponents.RawTransaction = {
Expand Down
12 changes: 2 additions & 10 deletions packages/ckb/src/rgbpp/launch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
append0x,
calculateRgbppTokenInfoCellCapacity,
calculateTransactionFee,
fetchTypeIdCellDeps,
generateUniqueTypeArgs,
u128ToLe,
} from '../utils';
Expand All @@ -19,13 +20,9 @@ import {
MAX_FEE,
RGBPP_TX_WITNESS_MAX_SIZE,
RGBPP_WITNESS_PLACEHOLDER,
getRgbppLockDep,
getXudtDep,
getXudtTypeScript,
getUniqueTypeScript,
getUniqueTypeDep,
UNLOCKABLE_LOCK_SCRIPT,
getRgbppLockConfigDep,
} from '../constants';
import { getTransactionSize, scriptToHash } from '@nervosnetwork/ckb-sdk-utils';

Expand Down Expand Up @@ -80,12 +77,7 @@ export const genRgbppLaunchCkbVirtualTx = async ({
];

const outputsData = [append0x(u128ToLe(launchAmount)), encodeRgbppTokenInfo(rgbppTokenInfo)];
const cellDeps = [
getRgbppLockDep(isMainnet),
getRgbppLockConfigDep(isMainnet),
getXudtDep(isMainnet),
getUniqueTypeDep(isMainnet),
];
const cellDeps = await fetchTypeIdCellDeps(isMainnet, { rgbpp: true, xudt: true, unique: true });

const witnesses: Hex[] = inputs.map((_, index) => (index === 0 ? RGBPP_WITNESS_PLACEHOLDER : '0x'));

Expand Down
6 changes: 2 additions & 4 deletions packages/ckb/src/spore/cluster.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { RgbppCkbVirtualTx } from '../types/rgbpp';
import { packRawClusterData } from '@spore-sdk/core';
import { append0x, calculateTransactionFee } from '../utils';
import { append0x, calculateTransactionFee, fetchTypeIdCellDeps } from '../utils';
import { buildPreLockArgs, calculateCommitment, genRgbppLockScript } from '../utils/rgbpp';
import { CreateClusterCkbVirtualTxParams, Hex, SporeVirtualTxResult } from '../types';
import {
RGBPP_TX_WITNESS_MAX_SIZE,
RGBPP_WITNESS_PLACEHOLDER,
getClusterTypeDep,
getClusterTypeScript,
getRgbppLockConfigDep,
getRgbppLockDep,
getRgbppLockScript,
} from '../constants';
import { generateClusterCreateCoBuild, generateClusterId } from '../utils/spore';
Expand Down Expand Up @@ -62,7 +60,7 @@ export const genCreateClusterCkbVirtualTx = async ({
},
];
const outputsData: Hex[] = [bytesToHex(packRawClusterData(clusterData))];
const cellDeps = [getRgbppLockDep(isMainnet), getRgbppLockConfigDep(isMainnet), getClusterTypeDep(isMainnet)];
const cellDeps = [...(await fetchTypeIdCellDeps(isMainnet, { rgbpp: true })), getClusterTypeDep(isMainnet)];
const sporeCoBuild = generateClusterCreateCoBuild(outputs[0], outputsData[0]);
const witnesses = [RGBPP_WITNESS_PLACEHOLDER, sporeCoBuild];

Expand Down
13 changes: 4 additions & 9 deletions packages/ckb/src/spore/leap.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BtcTimeCellsParams, RgbppCkbVirtualTx } from '../types/rgbpp';
import { append0x, calculateTransactionFee } from '../utils';
import { append0x, calculateTransactionFee, fetchTypeIdCellDeps } from '../utils';
import {
btcTxIdFromBtcTimeLockArgs,
buildSpvClientCellDep,
Expand All @@ -19,10 +19,6 @@ import {
BTC_JUMP_CONFIRMATION_BLOCKS,
RGBPP_TX_WITNESS_MAX_SIZE,
RGBPP_WITNESS_PLACEHOLDER,
getBtcTimeLockConfigDep,
getBtcTimeLockDep,
getRgbppLockConfigDep,
getRgbppLockDep,
getRgbppLockScript,
getSporeTypeDep,
} from '../constants';
Expand Down Expand Up @@ -80,7 +76,7 @@ export const genLeapSporeFromBtcToCkbVirtualTx = async ({
},
];
const outputsData: Hex[] = [sporeCell.outputData];
const cellDeps = [getRgbppLockDep(isMainnet), getRgbppLockConfigDep(isMainnet), getSporeTypeDep(isMainnet)];
const cellDeps = [...(await fetchTypeIdCellDeps(isMainnet, { rgbpp: true })), getSporeTypeDep(isMainnet)];
const sporeCoBuild = generateSporeTransferCoBuild([sporeCell], outputs);
const witnesses = [RGBPP_WITNESS_PLACEHOLDER, sporeCoBuild];

Expand Down Expand Up @@ -142,9 +138,8 @@ export const buildSporeBtcTimeCellsSpentTx = async ({
const outputsData = sortedBtcTimeCells.map((cell) => cell.outputData);

const cellDeps: CKBComponents.CellDep[] = [
getBtcTimeLockDep(isMainnet),
...(await fetchTypeIdCellDeps(isMainnet, { btcTime: true })),
getSporeTypeDep(isMainnet),
getBtcTimeLockConfigDep(isMainnet),
];

const witnesses: Hex[] = [];
Expand Down Expand Up @@ -233,7 +228,7 @@ export const genLeapSporeFromCkbToBtcRawTx = async ({
},
];
const outputsData: Hex[] = [sporeCell.outputData];
const cellDeps = [getRgbppLockDep(isMainnet), getRgbppLockConfigDep(isMainnet), getSporeTypeDep(isMainnet)];
const cellDeps = [...(await fetchTypeIdCellDeps(isMainnet, { rgbpp: true })), getSporeTypeDep(isMainnet)];
const sporeCoBuild = generateSporeTransferCoBuild([sporeCell], outputs);
const witnesses = [RGBPP_WITNESS_PLACEHOLDER, sporeCoBuild];

Expand Down
8 changes: 3 additions & 5 deletions packages/ckb/src/spore/spore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
append0x,
calculateRgbppSporeCellCapacity,
calculateTransactionFee,
fetchTypeIdCellDeps,
isClusterSporeTypeSupported,
} from '../utils';
import { buildPreLockArgs, calculateCommitment, genRgbppLockScript } from '../utils/rgbpp';
Expand All @@ -23,8 +24,6 @@ import {
RGBPP_WITNESS_PLACEHOLDER,
SECP256K1_WITNESS_LOCK_SIZE,
getClusterTypeDep,
getRgbppLockConfigDep,
getRgbppLockDep,
getRgbppLockScript,
getSecp256k1CellDep,
getSporeTypeDep,
Expand Down Expand Up @@ -117,8 +116,7 @@ export const genCreateSporeCkbVirtualTx = async ({
];
const outputsData: Hex[] = [clusterCell.outputData, ...sporeOutputsData];
const cellDeps = [
getRgbppLockDep(isMainnet),
getRgbppLockConfigDep(isMainnet),
...(await fetchTypeIdCellDeps(isMainnet, { rgbpp: true })),
getClusterTypeDep(isMainnet),
getSporeTypeDep(isMainnet),
clusterCellDep,
Expand Down Expand Up @@ -323,7 +321,7 @@ export const genTransferSporeCkbVirtualTx = async ({
},
];
const outputsData: Hex[] = [sporeCell.outputData];
const cellDeps = [getRgbppLockDep(isMainnet), getRgbppLockConfigDep(isMainnet), getSporeTypeDep(isMainnet)];
const cellDeps = [...(await fetchTypeIdCellDeps(isMainnet, { rgbpp: true })), getSporeTypeDep(isMainnet)];
const sporeCoBuild = generateSporeTransferCoBuild([sporeCell], outputs);
const witnesses = [RGBPP_WITNESS_PLACEHOLDER, sporeCoBuild];

Expand Down
Loading

0 comments on commit 207ca98

Please sign in to comment.