@@ -22,7 +22,8 @@ import {
2222 type OrderRatio ,
2323} from "@ickb/v1-core" ;
2424import {
25- calculateRatio ,
25+ calculateOrderRatio ,
26+ calculateOrderResult ,
2627 maxEpoch ,
2728 orderMaturityEstimate ,
2829 txInfoPadding ,
@@ -133,7 +134,7 @@ export function convert(
133134 }
134135 Object . freeze ( ickbPool ) ;
135136
136- const ratio = calculateRatio ( isCkb2Udt , tipHeader ) ;
137+ const ratio = calculateOrderRatio ( isCkb2Udt , tipHeader ) ;
137138 const depositAmount = ckbSoftCapPerDeposit ( tipHeader ) ;
138139 const N = isCkb2Udt ? Number ( amount / depositAmount ) : ickbPool . length ;
139140 const txCache = Array < TxInfo | undefined > ( N ) ;
@@ -207,7 +208,7 @@ function convertAttempt(
207208 }
208209 }
209210
210- let fee = txInfo . fee ;
211+ let orderFee = 0n ;
211212 if ( amount > 0n ) {
212213 tx = orderMint (
213214 tx ,
@@ -218,27 +219,34 @@ function convertAttempt(
218219 isCkb2Udt ? ratio : undefined ,
219220 isCkb2Udt ? undefined : ratio ,
220221 ) ;
221- // 0.1% fee to bot
222- fee += isCkb2Udt
223- ? amount -
224- ickb2Ckb (
225- ( amount * ratio . ckbMultiplier ) / ratio . udtMultiplier ,
226- tipHeader ,
227- )
228- : ickb2Ckb ( amount , tipHeader ) -
229- ( amount * ratio . udtMultiplier ) / ratio . ckbMultiplier ;
222+
223+ const convertedAmount = calculateOrderResult ( isCkb2Udt , amount , ratio ) ;
224+ orderFee += isCkb2Udt
225+ ? amount - ickb2Ckb ( convertedAmount , tipHeader )
226+ : ickb2Ckb ( amount , tipHeader ) - convertedAmount ;
230227
231228 estimatedMaturities . push (
232229 orderMaturityEstimate ( isCkb2Udt , amount , tipHeader ) ,
233230 ) ;
234231 }
235232
236233 const estimatedMaturity = maxEpoch ( estimatedMaturities ) ;
237- return addChange (
238- { ...txInfo , tx, estimatedMaturity, fee } ,
234+
235+ txInfo = addChange (
236+ { ...txInfo , tx, estimatedMaturity, fee : txInfo . fee + orderFee } ,
239237 calculateFee ,
240238 walletConfig ,
241239 ) ;
240+
241+ // Check that order provides enough fee to the bot for being matched
242+ if ( ! txInfo . error && 10n * ( txInfo . fee - orderFee ) > orderFee ) {
243+ if ( isCkb2Udt ) {
244+ return { ...txInfo , error : "CKB amount too small" } ;
245+ } else {
246+ return { ...txInfo , error : "iCKB amount too small" } ;
247+ }
248+ }
249+ return txInfo ;
242250}
243251
244252export function addChange (
0 commit comments