Skip to content

Commit

Permalink
update order error message
Browse files Browse the repository at this point in the history
  • Loading branch information
duanyytop committed Feb 8, 2024
1 parent 16c1fcd commit 50511e6
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 13 deletions.
14 changes: 6 additions & 8 deletions src/collector/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import axios from 'axios'
import CKB from '@nervosnetwork/ckb-sdk-core'
import { toCamelcase } from '../utils/case-parser'
import { IndexerCell, CollectResult, IndexerCapacity, CollectXudtResult } from '../types/collector'
import { MIN_CAPACITY } from '../constants'
import { CKB_UNIT, MIN_CAPACITY } from '../constants'
import { CapacityNotEnoughException, IndexerException, XudtAmountNotEnoughException } from '../exceptions'
import { leToU128 } from '../utils'

Expand Down Expand Up @@ -117,7 +117,7 @@ export class Collector {
}
}

collectInputs(liveCells: IndexerCell[], needCapacity: bigint, fee: bigint, minCapacity?: bigint): CollectResult {
collectInputs(liveCells: IndexerCell[], needCapacity: bigint, fee: bigint, minCapacity?: bigint, errMsg?: string): CollectResult {
const changeCapacity = minCapacity ?? MIN_CAPACITY
let inputs: CKBComponents.CellInput[] = []
let sum = BigInt(0)
Expand All @@ -134,11 +134,9 @@ export class Collector {
break
}
}
if (sum < needCapacity + fee) {
throw new CapacityNotEnoughException('Capacity not enough')
}
if (sum < needCapacity + changeCapacity + fee && sum !== needCapacity + fee) {
throw new CapacityNotEnoughException('Capacity not enough for change')
if (sum < needCapacity + changeCapacity + fee) {
const message = errMsg ?? 'Insufficient free CKB balance'
throw new CapacityNotEnoughException(message)
}
return { inputs, capacity: sum }
}
Expand All @@ -162,7 +160,7 @@ export class Collector {
}
}
if (sumAmount < needAmount) {
throw new XudtAmountNotEnoughException('Capacity not enough')
throw new XudtAmountNotEnoughException('Insufficient Xudt balance')
}
return { inputs, capacity: sumCapacity, amount: sumAmount }
}
Expand Down
20 changes: 18 additions & 2 deletions src/order/cancel.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, getXudtDep, getJoyIDCellDep, getDexCellDep, MAX_FEE, JOYID_ESTIMATED_WITNESS_LOCK_SIZE } from '../constants'
import {
getCotaTypeScript,
getXudtDep,
getJoyIDCellDep,
getDexCellDep,
MAX_FEE,
JOYID_ESTIMATED_WITNESS_LOCK_SIZE,
CKB_UNIT,
} from '../constants'
import { CancelParams, SubkeyUnlockReq, TakerResult } from '../types'
import { append0x } from '../utils'
import { XudtException, NoCotaCellException, NoLiveCellException } from '../exceptions'
Expand Down Expand Up @@ -46,7 +54,15 @@ export const buildCancelTx = async ({ collector, joyID, seller, orderOutPoints,
const outputsData = xudtOutputsData

const minCellCapacity = calculateEmptyCellMinCapacity(sellerLock)
const { inputs: emptyInputs, capacity: inputsCapacity } = collector.collectInputs(emptyCells, minCellCapacity, txFee, minCellCapacity)
const needCapacity = ((minCellCapacity + minCellCapacity + CKB_UNIT) / CKB_UNIT).toString()
const errMsg = `At least ${needCapacity} free CKB (refundable) is required to cancel the sell order.`
const { inputs: emptyInputs, capacity: inputsCapacity } = collector.collectInputs(
emptyCells,
minCellCapacity,
txFee,
minCellCapacity,
errMsg,
)
const orderInputs: CKBComponents.CellInput[] = outPoints.map(outPoint => ({
previousOutput: outPoint,
since: '0x0',
Expand Down
13 changes: 12 additions & 1 deletion 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, getXudtDep, getJoyIDCellDep, MAX_FEE, JOYID_ESTIMATED_WITNESS_LOCK_SIZE } from '../constants'
import {
getDexLockScript,
getCotaTypeScript,
getXudtDep,
getJoyIDCellDep,
MAX_FEE,
JOYID_ESTIMATED_WITNESS_LOCK_SIZE,
CKB_UNIT,
} from '../constants'
import { Hex, SubkeyUnlockReq, MakerParams, MakerResult } from '../types'
import { append0x, u128ToLe } from '../utils'
import { XudtException, NoCotaCellException, NoLiveCellException } from '../exceptions'
Expand Down Expand Up @@ -36,11 +44,14 @@ export const buildMakerTx = async ({
const orderCellCapacity = calculateXudtCellCapacity(orderLock, xudtTypeScript)

const minCellCapacity = calculateEmptyCellMinCapacity(sellerLock)
const needCapacity = ((orderCellCapacity + minCellCapacity + CKB_UNIT) / CKB_UNIT).toString()
const errMsg = `At least ${needCapacity} free CKB (refundable) is required to place a sell order.`
const { inputs: emptyInputs, capacity: emptyInputsCapacity } = collector.collectInputs(
emptyCells,
orderCellCapacity,
txFee,
minCellCapacity,
errMsg,
)

const xudtCells = await collector.getCells({
Expand Down
20 changes: 18 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, getXudtDep, getJoyIDCellDep, getDexCellDep, MAX_FEE, JOYID_ESTIMATED_WITNESS_LOCK_SIZE } from '../constants'
import {
getCotaTypeScript,
getXudtDep,
getJoyIDCellDep,
getDexCellDep,
MAX_FEE,
JOYID_ESTIMATED_WITNESS_LOCK_SIZE,
CKB_UNIT,
} from '../constants'
import { Hex, SubkeyUnlockReq, TakerParams, TakerResult } from '../types'
import { append0x } from '../utils'
import { XudtException, NoCotaCellException, NoLiveCellException } from '../exceptions'
Expand Down Expand Up @@ -62,7 +70,15 @@ export const buildTakerTx = async ({ collector, joyID, buyer, orderOutPoints, fe
const outputsData = [...orderOutputsData, ...xudtOutputsData]

const minCellCapacity = calculateEmptyCellMinCapacity(buyerLock)
const { inputs: emptyInputs, capacity: inputsCapacity } = collector.collectInputs(emptyCells, needInputsCapacity, txFee, minCellCapacity)
const needCapacity = ((needInputsCapacity + minCellCapacity + CKB_UNIT) / CKB_UNIT).toString()
const errMsg = `At least ${needCapacity} free CKB is required to take the order.`
const { inputs: emptyInputs, capacity: inputsCapacity } = collector.collectInputs(
emptyCells,
needInputsCapacity,
txFee,
minCellCapacity,
errMsg,
)
const orderInputs: CKBComponents.CellInput[] = outPoints.map(outPoint => ({
previousOutput: outPoint,
since: '0x0',
Expand Down

0 comments on commit 50511e6

Please sign in to comment.