Skip to content

Commit

Permalink
refactor: Clean ckb lib import packages
Browse files Browse the repository at this point in the history
  • Loading branch information
duanyytop committed Jul 5, 2024
1 parent 6d0c830 commit 37636bd
Show file tree
Hide file tree
Showing 17 changed files with 120 additions and 216 deletions.
3 changes: 1 addition & 2 deletions packages/ckb/src/collector/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import axios from 'axios';
import CKB from '@nervosnetwork/ckb-sdk-core';
import { toCamelcase } from '../utils/case-parser';
import { CollectConfig, CollectResult, CollectUdtResult, IndexerCell } from '../types/collector';
import { MIN_CAPACITY } from '../constants';
import { CapacityNotEnoughError, IndexerError, UdtAmountNotEnoughError } from '../error';
import { isRgbppLockCellIgnoreChain, leToU128, remove0x } from '../utils';
import { isRgbppLockCellIgnoreChain, leToU128, remove0x, toCamelcase } from '../utils';
import { Hex } from '../types';

interface IndexerScript {
Expand Down
3 changes: 0 additions & 3 deletions packages/ckb/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import * as ckbUtils from '@nervosnetwork/ckb-sdk-utils';
export const serializeScript = ckbUtils.serializeScript;

export * from './schemas/generated/blockchain';
export * from './schemas/generated/rgbpp';
export * from './collector';
Expand Down
12 changes: 8 additions & 4 deletions packages/ckb/src/paymaster/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import * as ckbUtils from '@nervosnetwork/ckb-sdk-utils';
import { ConstructPaymasterParams } from '../types/rgbpp';
import { NoLiveCellError } from '../error';
import { CKB_UNIT, MAX_FEE, SECP256K1_WITNESS_LOCK_SIZE, getSecp256k1CellDep } from '../constants';
import { append0x, calculateTransactionFee } from '../utils';

const { AddressPrefix, addressToScript, getTransactionSize, privateKeyToAddress } = ckbUtils;
import {
append0x,
calculateTransactionFee,
AddressPrefix,
addressToScript,
getTransactionSize,
privateKeyToAddress,
} from '../utils';

const SECP256K1_MIN_CAPACITY = BigInt(61) * CKB_UNIT;

Expand Down
11 changes: 4 additions & 7 deletions packages/ckb/src/rgbpp/btc-jump-ckb.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { RgbppCkbVirtualTx, BtcJumpCkbVirtualTxParams, BtcJumpCkbVirtualTxResult } from '../types/rgbpp';
import { blockchain } from '@ckb-lumos/base';
import { TypeAssetNotSupportedError } from '../error';
import {
append0x,
Expand All @@ -11,8 +10,8 @@ import {
isScriptEqual,
isUDTTypeSupported,
u128ToLe,
} from '../utils';
import {
addressToScript,
getTransactionSize,
buildPreLockArgs,
calculateCommitment,
throwErrorWhenTxInputsExceeded,
Expand All @@ -22,12 +21,10 @@ import {
genRgbppLockScript,
throwErrorWhenRgbppCellsInvalid,
isRgbppCapacitySufficientForChange,
} from '../utils/rgbpp';
blockchain,
} from '../utils';
import { Hex, IndexerCell } from '../types';
import { RGBPP_WITNESS_PLACEHOLDER, getSecp256k1CellDep } from '../constants';
import * as ckbUtils from '@nervosnetwork/ckb-sdk-utils';

const { addressToScript, getTransactionSize } = ckbUtils;

/**
* Generate the virtual ckb transaction for the jumping tx from BTC to CKB
Expand Down
12 changes: 4 additions & 8 deletions packages/ckb/src/rgbpp/btc-time.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as ckbUtils from '@nervosnetwork/ckb-sdk-utils';
import {
BTC_JUMP_CONFIRMATION_BLOCKS,
SECP256K1_WITNESS_LOCK_SIZE,
Expand All @@ -16,20 +15,17 @@ import {
genBtcTimeLockArgs,
lockScriptFromBtcTimeLockArgs,
transformSpvProof,
} from '../utils';
import { buildSpvClientCellDep } from '../utils';
import { blockchain } from '@ckb-lumos/base';
import signWitnesses from '@nervosnetwork/ckb-sdk-core/lib/signWitnesses.js';

const {
addressToScript,
bytesToHex,
getTransactionSize,
rawTransactionToHash,
scriptToHash,
serializeOutPoint,
serializeWitnessArgs,
} = ckbUtils;
buildSpvClientCellDep,
blockchain,
signWitnesses,
} from '../utils';

export const buildBtcTimeUnlockWitness = (btcTxProof: Hex): Hex => {
const btcTimeUnlock = BTCTimeUnlock.pack({ btcTxProof: blockchain.Bytes.pack(btcTxProof) });
Expand Down
16 changes: 8 additions & 8 deletions packages/ckb/src/rgbpp/btc-transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
BtcTransferVirtualTxResult,
RgbppCkbVirtualTx,
} from '../types/rgbpp';
import { blockchain } from '@ckb-lumos/base';
import { NoLiveCellError, NoRgbppLiveCellError, TypeAssetNotSupportedError } from '../error';
import {
append0x,
Expand All @@ -17,8 +16,11 @@ import {
isScriptEqual,
isUDTTypeSupported,
u128ToLe,
} from '../utils';
import {
addressToScript,
getTransactionSize,
rawTransactionToHash,
scriptToHash,
serializeWitnessArgs,
buildPreLockArgs,
calculateCommitment,
compareInputs,
Expand All @@ -27,7 +29,9 @@ import {
isRgbppCapacitySufficientForChange,
throwErrorWhenRgbppCellsInvalid,
throwErrorWhenTxInputsExceeded,
} from '../utils/rgbpp';
blockchain,
signWitnesses,
} from '../utils';
import { Hex, IndexerCell } from '../types';
import {
MAX_FEE,
Expand All @@ -37,10 +41,6 @@ import {
getRgbppLockScript,
getSecp256k1CellDep,
} from '../constants';
import * as ckbUtils from '@nervosnetwork/ckb-sdk-utils';
import signWitnesses from '@nervosnetwork/ckb-sdk-core/lib/signWitnesses.js';

const { addressToScript, getTransactionSize, rawTransactionToHash, scriptToHash, serializeWitnessArgs } = ckbUtils;

/**
* Generate the virtual ckb transaction for the btc transfer tx
Expand Down
16 changes: 9 additions & 7 deletions packages/ckb/src/rgbpp/ckb-builder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as ckbUtils from '@nervosnetwork/ckb-sdk-utils';
import {
UpdateCkbTxWithRealBtcTxIdParams,
AppendPaymasterCellAndSignTxParams,
Expand All @@ -14,14 +13,17 @@ import {
replaceLockArgsWithRealBtcTxId,
transformSpvProof,
u8ToHex,
bytesToHex,
getTransactionSize,
rawTransactionToHash,
scriptToHash,
serializeWitnessArgs,
blockchain,
signWitnesses,
} from '../utils';
import { InputsCapacityNotEnoughError } from '../error';
import signWitnesses from '@nervosnetwork/ckb-sdk-core/lib/signWitnesses.js';
import { buildSpvClientCellDep } from '../utils';
import { RGBPPUnlock, Uint16 } from '../schemas/generated/rgbpp';
import { Bytes } from '@ckb-lumos/base/lib/blockchain.js';

const { bytesToHex, getTransactionSize, rawTransactionToHash, scriptToHash, serializeWitnessArgs } = ckbUtils;

export const buildRgbppUnlockWitness = (
btcTxBytes: Hex,
Expand All @@ -32,14 +34,14 @@ export const buildRgbppUnlockWitness = (
const inputLen = append0x(u8ToHex(inputsLen));
const outputLen = append0x(u8ToHex(outputsLen));

const btcTx = Bytes.pack(append0x(btcTxBytes));
const btcTx = blockchain.Bytes.pack(append0x(btcTxBytes));

const version = Uint16.pack([0, 0]);
const rgbppUnlock = RGBPPUnlock.pack({
version,
extraData: { inputLen, outputLen },
btcTx,
btcTxProof: bytesToHex(Bytes.pack(append0x(btcTxProof))),
btcTxProof: bytesToHex(blockchain.Bytes.pack(append0x(btcTxProof))),
});
return append0x(bytesToHex(rgbppUnlock));
};
Expand Down
9 changes: 4 additions & 5 deletions packages/ckb/src/rgbpp/ckb-jump-btc.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { CkbBatchJumpBtcVirtualTxParams, CkbJumpBtcVirtualTxParams } from '../types/rgbpp';
import { blockchain } from '@ckb-lumos/base';
import { NoLiveCellError, NoXudtLiveCellError, TypeAssetNotSupportedError } from '../error';
import {
append0x,
Expand All @@ -9,12 +8,12 @@ import {
fetchTypeIdCellDeps,
isTypeAssetSupported,
u128ToLe,
addressToScript,
getTransactionSize,
genRgbppLockScript,
blockchain,
} from '../utils';
import { genRgbppLockScript } from '../utils/rgbpp';
import { MAX_FEE, MIN_CAPACITY, RGBPP_TX_WITNESS_MAX_SIZE } from '../constants';
import * as ckbUtils from '@nervosnetwork/ckb-sdk-utils';

const { addressToScript, getTransactionSize } = ckbUtils;

/**
* Generate the virtual ckb transaction for the jumping tx from CKB to BTC
Expand Down
9 changes: 3 additions & 6 deletions packages/ckb/src/rgbpp/launch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import {
fetchTypeIdCellDeps,
generateUniqueTypeArgs,
u128ToLe,
} from '../utils';
import {
getTransactionSize,
scriptToHash,
buildPreLockArgs,
calculateCommitment,
encodeRgbppTokenInfo,
genBtcTimeLockScript,
genRgbppLockScript,
} from '../utils/rgbpp';
} from '../utils';
import { Hex } from '../types';
import {
MAX_FEE,
Expand All @@ -24,9 +24,6 @@ import {
getUniqueTypeScript,
UNLOCKABLE_LOCK_SCRIPT,
} from '../constants';
import * as ckbUtils from '@nervosnetwork/ckb-sdk-utils';

const { getTransactionSize, scriptToHash } = ckbUtils;

/**
* Generate the virtual ckb transaction for the btc transfer tx
Expand Down
20 changes: 13 additions & 7 deletions packages/ckb/src/spore/cluster.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
import { RgbppCkbVirtualTx } from '../types/rgbpp';
import { packRawClusterData } from '@spore-sdk/core';
import { append0x, calculateTransactionFee, fetchTypeIdCellDeps } from '../utils';
import { buildPreLockArgs, calculateCommitment, genRgbppLockScript } from '../utils/rgbpp';
import { RgbppCkbVirtualTx } from '../types/rgbpp';
import {
append0x,
calculateTransactionFee,
fetchTypeIdCellDeps,
bytesToHex,
getTransactionSize,
buildPreLockArgs,
calculateCommitment,
genRgbppLockScript,
generateClusterCreateCoBuild,
generateClusterId,
} from '../utils';
import { CreateClusterCkbVirtualTxParams, Hex, SporeVirtualTxResult } from '../types';
import {
RGBPP_TX_WITNESS_MAX_SIZE,
RGBPP_WITNESS_PLACEHOLDER,
getClusterTypeDep,
getClusterTypeScript,
} from '../constants';
import { generateClusterCreateCoBuild, generateClusterId } from '../utils/spore';
import { NoRgbppLiveCellError } from '../error';
import * as ckbUtils from '@nervosnetwork/ckb-sdk-utils';

const { bytesToHex, getTransactionSize } = ckbUtils;

/**
* Generate the virtual ckb transaction for creating cluster
Expand Down
18 changes: 11 additions & 7 deletions packages/ckb/src/spore/leap.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
import { BtcTimeCellsParams, RgbppCkbVirtualTx } from '../types/rgbpp';
import { append0x, calculateTransactionFee, fetchTypeIdCellDeps } from '../utils';
import {
append0x,
calculateTransactionFee,
fetchTypeIdCellDeps,
addressToScript,
getTransactionSize,
serializeOutPoint,
serializeWitnessArgs,
btcTxIdFromBtcTimeLockArgs,
buildSpvClientCellDep,
calculateCommitment,
compareInputs,
genBtcTimeLockScript,
lockScriptFromBtcTimeLockArgs,
transformSpvProof,
} from '../utils/rgbpp';
generateSporeTransferCoBuild,
throwErrorWhenSporeCellsInvalid,
blockchain,
} from '../utils';
import {
Hex,
LeapSporeFromBtcToCkbVirtualTxParams,
Expand All @@ -22,14 +31,9 @@ import {
getRgbppLockScript,
getSporeTypeDep,
} from '../constants';
import { generateSporeTransferCoBuild, throwErrorWhenSporeCellsInvalid } from '../utils/spore';
import { NoRgbppLiveCellError } from '../error';
import * as ckbUtils from '@nervosnetwork/ckb-sdk-utils';
import { blockchain } from '@ckb-lumos/base';
import { buildBtcTimeUnlockWitness } from '../rgbpp';

const { addressToScript, getTransactionSize, serializeOutPoint, serializeWitnessArgs } = ckbUtils;

/**
* Generate the virtual ckb transaction for leaping spore from BTC to CKB
* @param collector The collector that collects CKB live cells and transactions
Expand Down
26 changes: 14 additions & 12 deletions packages/ckb/src/spore/spore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,21 @@ import {
calculateTransactionFee,
fetchTypeIdCellDeps,
isClusterSporeTypeSupported,
addressToScript,
bytesToHex,
getTransactionSize,
rawTransactionToHash,
scriptToHash,
serializeWitnessArgs,
buildPreLockArgs,
calculateCommitment,
genRgbppLockScript,
generateSporeCreateCoBuild,
generateSporeId,
generateSporeTransferCoBuild,
throwErrorWhenSporeCellsInvalid,
signWitnesses,
} from '../utils';
import { buildPreLockArgs, calculateCommitment, genRgbppLockScript } from '../utils/rgbpp';
import {
AppendIssuerCellToSporeCreate,
BuildAppendingIssuerCellTxParams,
Expand All @@ -29,23 +42,12 @@ import {
getSporeTypeDep,
getSporeTypeScript,
} from '../constants';
import {
generateSporeCreateCoBuild,
generateSporeId,
generateSporeTransferCoBuild,
throwErrorWhenSporeCellsInvalid,
} from '../utils/spore';
import {
NoLiveCellError,
NoRgbppLiveCellError,
RgbppUtxoBindMultiTypeAssetsError,
TypeAssetNotSupportedError,
} from '../error';
import * as ckbUtils from '@nervosnetwork/ckb-sdk-utils';
import signWitnesses from '@nervosnetwork/ckb-sdk-core/lib/signWitnesses.js';

const { addressToScript, bytesToHex, getTransactionSize, rawTransactionToHash, scriptToHash, serializeWitnessArgs } =
ckbUtils;

/**
* Generate the virtual ckb transaction for creating spores
Expand Down
23 changes: 21 additions & 2 deletions packages/ckb/src/utils/ckb-tx.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { calculateTransactionFee as calculateTxFee } from '@nervosnetwork/ckb-sdk-utils/lib/calculateTransactionFee.js';
import { RawClusterData, packRawClusterData, SporeDataProps, packRawSporeData } from '@spore-sdk/core';
import signWitnesses from '@nervosnetwork/ckb-sdk-core/lib/signWitnesses.js';
import * as ckbUtils from '@nervosnetwork/ckb-sdk-utils';
import { remove0x, u64ToLe } from './hex';
import {
CKB_UNIT,
Expand All @@ -9,12 +11,29 @@ import {
getXudtTypeScript,
} from '../constants';
import { Hex, RgbppTokenInfo } from '../types';
import * as ckbUtils from '@nervosnetwork/ckb-sdk-utils';
import { encodeRgbppTokenInfo, genBtcTimeLockScript } from './rgbpp';
import { Collector } from '../collector';
import { NoLiveCellError } from '../error';

const { PERSONAL, blake2b, hexToBytes, serializeInput, serializeScript } = ckbUtils;
export { blockchain } from '@ckb-lumos/base';
export { signWitnesses };
export const {
PERSONAL,
blake2b,
hexToBytes,
serializeInput,
serializeScript,
AddressPrefix,
addressToScript,
privateKeyToAddress,
bytesToHex,
getTransactionSize,
rawTransactionToHash,
scriptToHash,
serializeOutPoint,
serializeOutput,
serializeWitnessArgs,
} = ckbUtils;

export const calculateTransactionFee = (txSize: number, feeRate?: bigint): bigint => {
const rate = feeRate ?? BigInt(1100);
Expand Down
Loading

1 comment on commit 37636bd

@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-20240705102348

Please sign in to comment.