Skip to content

Commit ecc6f92

Browse files
committed
Switch to 0.001% fee and add checks %124
1 parent 6aa6206 commit ecc6f92

3 files changed

Lines changed: 48 additions & 28 deletions

File tree

src/Form.tsx

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
2-
calculateRatio,
2+
calculateOrderRatio,
33
direction2Symbol,
4+
calculateOrderResult,
45
reservedCKB,
56
symbol2Direction,
67
toText,
@@ -98,7 +99,7 @@ export default function Form({
9899
99100
</button>
100101
<span className="text-center">
101-
{approxConversion(isCkb2Udt, CKB, tipHeader) + " " + b.name}
102+
{approxConversion(isCkb2Udt, CKB, tipHeader)} {b.name}
102103
</span>
103104
<span className="col-span-3 text-center text-3xl text-amber-400">
104105
{approxConversion(isCkb2Udt, amount, tipHeader)}
@@ -132,12 +133,12 @@ function approxConversion(
132133
amount: bigint,
133134
tipHeader: I8Header,
134135
): string {
135-
//Worst case scenario is a 0.1% fee for bot
136-
const { ckbMultiplier, udtMultiplier } = calculateRatio(isCkb2Udt, tipHeader);
137-
138-
const convertedAmount = isCkb2Udt
139-
? (amount * ckbMultiplier) / udtMultiplier
140-
: (amount * udtMultiplier) / ckbMultiplier;
141-
142-
return toText(convertedAmount);
136+
//Worst case scenario is a 0.001% fee for bot
137+
return toText(
138+
calculateOrderResult(
139+
isCkb2Udt,
140+
amount,
141+
calculateOrderRatio(isCkb2Udt, tipHeader),
142+
),
143+
);
143144
}

src/transaction.ts

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ import {
2222
type OrderRatio,
2323
} from "@ickb/v1-core";
2424
import {
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

244252
export function addChange(

src/utils.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,16 +134,27 @@ export const txInfoPadding: TxInfo = Object.freeze({
134134
// reservedCKB are reserved for state rent in conversions
135135
export const reservedCKB = 1000n * CKB;
136136

137-
// Fix up ratio to pay 0.1% fee to bot
138-
export function calculateRatio(
137+
// Calculate ratio in a way to pay 0.001% fee to bot
138+
export function calculateOrderRatio(
139139
isCkb2Udt: boolean,
140140
tipHeader: I8Header,
141141
): OrderRatio {
142142
const { ckbMultiplier, udtMultiplier } = ickbExchangeRatio(tipHeader);
143143
return {
144144
ckbMultiplier,
145-
// Pay 0.1% fee to bot
146145
udtMultiplier:
147-
udtMultiplier + (isCkb2Udt ? 1n : -1n) * (udtMultiplier / 1000n),
146+
// Pay 0.001% fee to bot
147+
udtMultiplier + (isCkb2Udt ? 1n : -1n) * (udtMultiplier / 100000n),
148148
};
149149
}
150+
151+
export function calculateOrderResult(
152+
isCkb2Udt: boolean,
153+
amount: bigint,
154+
ratio: OrderRatio,
155+
): bigint {
156+
const { ckbMultiplier, udtMultiplier } = ratio;
157+
return isCkb2Udt
158+
? (amount * ckbMultiplier) / udtMultiplier
159+
: (amount * udtMultiplier) / ckbMultiplier;
160+
}

0 commit comments

Comments
 (0)