Skip to content

Commit 6aa6206

Browse files
committed
Unify ratio calculations %19
1 parent b1983b0 commit 6aa6206

File tree

3 files changed

+24
-14
lines changed

3 files changed

+24
-14
lines changed

src/Form.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import {
2+
calculateRatio,
23
direction2Symbol,
34
reservedCKB,
45
symbol2Direction,
56
toText,
67
} from "./utils.ts";
78
import { CKB, max, min, type I8Header } from "@ickb/lumos-utils";
8-
import { ckb2Ickb, ickb2Ckb } from "@ickb/v1-core";
99
import type { JSX } from "react";
1010

1111
export default function Form({
@@ -132,10 +132,12 @@ function approxConversion(
132132
amount: bigint,
133133
tipHeader: I8Header,
134134
): string {
135-
let convertedAmount = isCkb2Udt
136-
? ckb2Ickb(amount, tipHeader)
137-
: ickb2Ckb(amount, tipHeader);
138135
//Worst case scenario is a 0.1% fee for bot
139-
convertedAmount -= convertedAmount / 1000n;
136+
const { ckbMultiplier, udtMultiplier } = calculateRatio(isCkb2Udt, tipHeader);
137+
138+
const convertedAmount = isCkb2Udt
139+
? (amount * ckbMultiplier) / udtMultiplier
140+
: (amount * udtMultiplier) / ckbMultiplier;
141+
140142
return toText(convertedAmount);
141143
}

src/transaction.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import {
1414
addWithdrawalRequestGroups,
1515
ickb2Ckb,
1616
ickbDeposit,
17-
ickbExchangeRatio,
1817
ickbRequestWithdrawalFrom,
1918
orderMelt,
2019
orderMint,
@@ -23,6 +22,7 @@ import {
2322
type OrderRatio,
2423
} from "@ickb/v1-core";
2524
import {
25+
calculateRatio,
2626
maxEpoch,
2727
orderMaturityEstimate,
2828
txInfoPadding,
@@ -133,14 +133,7 @@ export function convert(
133133
}
134134
Object.freeze(ickbPool);
135135

136-
const { ckbMultiplier, udtMultiplier } = ickbExchangeRatio(tipHeader);
137-
const ratio: OrderRatio = {
138-
ckbMultiplier,
139-
// Pay 0.1% fee to bot
140-
udtMultiplier:
141-
udtMultiplier + (isCkb2Udt ? 1n : -1n) * (udtMultiplier / 1000n),
142-
};
143-
136+
const ratio = calculateRatio(isCkb2Udt, tipHeader);
144137
const depositAmount = ckbSoftCapPerDeposit(tipHeader);
145138
const N = isCkb2Udt ? Number(amount / depositAmount) : ickbPool.length;
146139
const txCache = Array<TxInfo | undefined>(N);

src/utils.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
type I8Script,
1414
} from "@ickb/lumos-utils";
1515
import { parseEpoch, type EpochSinceValue } from "@ckb-lumos/base/lib/since";
16+
import { ickbExchangeRatio, type OrderRatio } from "@ickb/v1-core";
1617

1718
export interface RootConfig extends ChainConfig {
1819
queryClient: QueryClient;
@@ -132,3 +133,17 @@ export const txInfoPadding: TxInfo = Object.freeze({
132133

133134
// reservedCKB are reserved for state rent in conversions
134135
export const reservedCKB = 1000n * CKB;
136+
137+
// Fix up ratio to pay 0.1% fee to bot
138+
export function calculateRatio(
139+
isCkb2Udt: boolean,
140+
tipHeader: I8Header,
141+
): OrderRatio {
142+
const { ckbMultiplier, udtMultiplier } = ickbExchangeRatio(tipHeader);
143+
return {
144+
ckbMultiplier,
145+
// Pay 0.1% fee to bot
146+
udtMultiplier:
147+
udtMultiplier + (isCkb2Udt ? 1n : -1n) * (udtMultiplier / 1000n),
148+
};
149+
}

0 commit comments

Comments
 (0)