Skip to content

Commit

Permalink
Merge pull request #27 from nervina-labs/feat/estimate-witness-size
Browse files Browse the repository at this point in the history
Release v0.3.2
  • Loading branch information
duanyytop authored Jul 8, 2024
2 parents 9f2bbe8 + 1d24be1 commit 1f35955
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
- uses: actions/[email protected]
with:
node-version: '20'
- uses: pnpm/action-setup@v2.2.4
- uses: pnpm/action-setup@v4.0.0
with:
version: 8
- name: Install dependency
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nervina-labs/ckb-dex",
"version": "0.3.1",
"version": "0.3.2",
"description": "The JavaScript SDK for CKB DEX",
"author": "duanyytop <[email protected]>",
"license": "MIT",
Expand Down
3 changes: 2 additions & 1 deletion src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ export const MIN_CAPACITY = BigInt(63) * BigInt(10000_0000)
export const WITNESS_NATIVE_MODE = '01'
export const WITNESS_SUBKEY_MODE = '02'
export const SECP256R1_PUBKEY_SIG_LEN = (1 + 64 + 64) * 2
export const JOYID_ESTIMATED_WITNESS_LOCK_SIZE = 129 + 1000
export const JOYID_ESTIMATED_WITNESS_SIZE = 129 + 1000
export const DEFAULT_ESTIMATED_WITNESS_SIZE = 129

const TestnetInfo = {
JoyIDLockScript: {
Expand Down
14 changes: 11 additions & 3 deletions src/order/batchMaker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { addressToScript, blake160, getTransactionSize, serializeScript, serializeWitnessArgs } from '@nervosnetwork/ckb-sdk-utils'
import { blockchain } from '@ckb-lumos/base'
import { getDexLockScript, getCotaTypeScript, getJoyIDCellDep, MAX_FEE, JOYID_ESTIMATED_WITNESS_LOCK_SIZE, CKB_UNIT } from '../constants'
import {
getDexLockScript,
getCotaTypeScript,
getJoyIDCellDep,
MAX_FEE,
JOYID_ESTIMATED_WITNESS_SIZE,
CKB_UNIT,
DEFAULT_ESTIMATED_WITNESS_SIZE,
} from '../constants'
import { Hex, SubkeyUnlockReq, MakerParams, CKBAsset } from '../types'
import { append0x } from '../utils'
import { NoCotaCellException, NoLiveCellException, NFTException, NoSupportUDTAssetException } from '../exceptions'
Expand All @@ -17,7 +25,7 @@ import { OrderArgs } from './orderArgs'
import { calculateNFTMakerListPackage } from './maker'

export const buildMultiNftsMakerTx = async (
{ collector, joyID, seller, fee, ckbAsset = CKBAsset.SPORE }: MakerParams,
{ collector, joyID, seller, fee, estimateWitnessSize, ckbAsset = CKBAsset.SPORE }: MakerParams,
nfts: { totalValue: bigint; assetType: string }[],
) => {
let txFee = fee ?? MAX_FEE
Expand Down Expand Up @@ -172,7 +180,7 @@ export const buildMultiNftsMakerTx = async (
}

if (txFee === MAX_FEE) {
const txSize = getTransactionSize(tx) + (joyID ? JOYID_ESTIMATED_WITNESS_LOCK_SIZE : 0)
const txSize = getTransactionSize(tx) + (joyID ? JOYID_ESTIMATED_WITNESS_SIZE : estimateWitnessSize ?? DEFAULT_ESTIMATED_WITNESS_SIZE)
const estimatedTxFee = calculateTransactionFee(txSize)
txFee = estimatedTxFee
const estimatedChangeCapacity = changeCapacity + (MAX_FEE - estimatedTxFee)
Expand Down
12 changes: 10 additions & 2 deletions src/order/cancel.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { addressToScript, blake160, getTransactionSize, serializeScript, serializeWitnessArgs } from '@nervosnetwork/ckb-sdk-utils'
import { getCotaTypeScript, getJoyIDCellDep, getDexCellDep, MAX_FEE, JOYID_ESTIMATED_WITNESS_LOCK_SIZE } from '../constants'
import {
getCotaTypeScript,
getJoyIDCellDep,
getDexCellDep,
MAX_FEE,
JOYID_ESTIMATED_WITNESS_SIZE,
DEFAULT_ESTIMATED_WITNESS_SIZE,
} from '../constants'
import { CKBAsset, CancelParams, Hex, SubkeyUnlockReq, TakerResult } from '../types'
import { append0x } from '../utils'
import { AssetException, NoCotaCellException, NoLiveCellException } from '../exceptions'
Expand All @@ -22,6 +29,7 @@ export const buildCancelTx = async ({
seller,
orderOutPoints,
fee,
estimateWitnessSize,
ckbAsset = CKBAsset.XUDT,
}: CancelParams): Promise<TakerResult> => {
let txFee = fee ?? MAX_FEE
Expand Down Expand Up @@ -151,7 +159,7 @@ export const buildCancelTx = async ({
}

if (txFee === MAX_FEE) {
const txSize = getTransactionSize(tx) + (joyID ? JOYID_ESTIMATED_WITNESS_LOCK_SIZE : 0)
const txSize = getTransactionSize(tx) + (joyID ? JOYID_ESTIMATED_WITNESS_SIZE : estimateWitnessSize ?? DEFAULT_ESTIMATED_WITNESS_SIZE)
const estimatedTxFee = calculateTransactionFee(txSize)
txFee = estimatedTxFee
const estimatedChangeCapacity = changeCapacity + (MAX_FEE - estimatedTxFee)
Expand Down
13 changes: 11 additions & 2 deletions src/order/maker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { addressToScript, blake160, getTransactionSize, serializeScript, serializeWitnessArgs } from '@nervosnetwork/ckb-sdk-utils'
import { blockchain } from '@ckb-lumos/base'
import { getDexLockScript, getCotaTypeScript, getJoyIDCellDep, MAX_FEE, JOYID_ESTIMATED_WITNESS_LOCK_SIZE, CKB_UNIT } from '../constants'
import {
getDexLockScript,
getCotaTypeScript,
getJoyIDCellDep,
MAX_FEE,
JOYID_ESTIMATED_WITNESS_SIZE,
CKB_UNIT,
DEFAULT_ESTIMATED_WITNESS_SIZE,
} from '../constants'
import { Hex, SubkeyUnlockReq, MakerParams, MakerResult, CKBAsset } from '../types'
import { append0x, remove0x, u128ToLe } from '../utils'
import { AssetException, NoCotaCellException, NoLiveCellException, NFTException } from '../exceptions'
Expand Down Expand Up @@ -55,6 +63,7 @@ export const buildMakerTx = async ({
totalValue,
assetType,
fee,
estimateWitnessSize,
ckbAsset = CKBAsset.XUDT,
}: MakerParams): Promise<MakerResult> => {
let txFee = fee ?? MAX_FEE
Expand Down Expand Up @@ -229,7 +238,7 @@ export const buildMakerTx = async ({
}

if (txFee === MAX_FEE) {
const txSize = getTransactionSize(tx) + (joyID ? JOYID_ESTIMATED_WITNESS_LOCK_SIZE : 0)
const txSize = getTransactionSize(tx) + (joyID ? JOYID_ESTIMATED_WITNESS_SIZE : estimateWitnessSize ?? DEFAULT_ESTIMATED_WITNESS_SIZE)
const estimatedTxFee = calculateTransactionFee(txSize)
txFee = estimatedTxFee
const estimatedChangeCapacity = changeCapacity + (MAX_FEE - estimatedTxFee)
Expand Down
13 changes: 11 additions & 2 deletions src/order/taker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { addressToScript, blake160, getTransactionSize, serializeScript, serializeWitnessArgs } from '@nervosnetwork/ckb-sdk-utils'
import { getCotaTypeScript, getJoyIDCellDep, getDexCellDep, MAX_FEE, JOYID_ESTIMATED_WITNESS_LOCK_SIZE, CKB_UNIT } from '../constants'
import {
getCotaTypeScript,
getJoyIDCellDep,
getDexCellDep,
MAX_FEE,
JOYID_ESTIMATED_WITNESS_SIZE,
CKB_UNIT,
DEFAULT_ESTIMATED_WITNESS_SIZE,
} from '../constants'
import { CKBAsset, Hex, SubkeyUnlockReq, TakerParams, TakerResult } from '../types'
import { append0x } from '../utils'
import { AssetException, NoCotaCellException, NoLiveCellException } from '../exceptions'
Expand Down Expand Up @@ -76,6 +84,7 @@ export const buildTakerTx = async ({
buyer,
orderOutPoints,
fee,
estimateWitnessSize,
ckbAsset = CKBAsset.XUDT,
}: TakerParams): Promise<TakerResult> => {
let txFee = fee ?? MAX_FEE
Expand Down Expand Up @@ -226,7 +235,7 @@ export const buildTakerTx = async ({
}

if (txFee === MAX_FEE) {
const txSize = getTransactionSize(tx) + (joyID ? JOYID_ESTIMATED_WITNESS_LOCK_SIZE : 0)
const txSize = getTransactionSize(tx) + (joyID ? JOYID_ESTIMATED_WITNESS_SIZE : estimateWitnessSize ?? DEFAULT_ESTIMATED_WITNESS_SIZE)
const estimatedTxFee = calculateTransactionFee(txSize)
txFee = estimatedTxFee
const estimatedChangeCapacity = changeCapacity + (MAX_FEE - estimatedTxFee)
Expand Down
1 change: 1 addition & 0 deletions src/types/dex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ interface BaseParams {
fee?: bigint
joyID?: JoyIDConfig
ckbAsset?: CKBAsset
estimateWitnessSize?: number
}

export interface MakerParams extends BaseParams {
Expand Down

0 comments on commit 1f35955

Please sign in to comment.