Skip to content

Commit e23e4a5

Browse files
committed
Switch ratio calculations to closure %58
1 parent 6aa6206 commit e23e4a5

5 files changed

Lines changed: 46 additions & 35 deletions

File tree

src/App.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export default function App({
3636
ckbBalance,
3737
ickbBalance,
3838
tipHeader,
39+
calculateRatio,
3940
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
4041
} = useQuery(l1StateOptions(walletConfig, isFrozen)).data!;
4142

@@ -77,7 +78,7 @@ export default function App({
7778
rawText: symbol + text,
7879
setRawText,
7980
amount,
80-
tipHeader,
81+
calculateRatio,
8182
isFrozen,
8283
ckbNative,
8384
ickbNative,

src/Form.tsx

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
import {
2-
calculateRatio,
32
direction2Symbol,
3+
previewConversion,
44
reservedCKB,
55
symbol2Direction,
66
toText,
77
} from "./utils.ts";
8-
import { CKB, max, min, type I8Header } from "@ickb/lumos-utils";
8+
import { CKB, max, min } from "@ickb/lumos-utils";
9+
import type { OrderRatio } from "@ickb/v1-core";
910
import type { JSX } from "react";
1011

1112
export default function Form({
1213
rawText,
1314
setRawText,
1415
amount,
15-
tipHeader,
16+
calculateRatio,
1617
isFrozen,
1718
ckbNative,
1819
ickbNative,
@@ -24,7 +25,7 @@ export default function Form({
2425
rawText: string;
2526
setRawText: (s: string) => void;
2627
amount: bigint;
27-
tipHeader: I8Header;
28+
calculateRatio: (isCkb2Udt: boolean, amount: bigint) => OrderRatio;
2829
isFrozen: boolean;
2930
ckbNative: bigint;
3031
ickbNative: bigint;
@@ -98,10 +99,10 @@ export default function Form({
9899
99100
</button>
100101
<span className="text-center">
101-
{approxConversion(isCkb2Udt, CKB, tipHeader) + " " + b.name}
102+
{textConversionPreview(isCkb2Udt, CKB, calculateRatio)} {b.name}
102103
</span>
103104
<span className="col-span-3 text-center text-3xl text-amber-400">
104-
{approxConversion(isCkb2Udt, amount, tipHeader)}
105+
{textConversionPreview(isCkb2Udt, amount, calculateRatio)}
105106
</span>
106107
<span className="text-amber-400">{display(b.native, "✅")}</span>
107108
<span className="text-2xl text-amber-400">{b.name}</span>
@@ -127,17 +128,12 @@ function display(shannons: bigint, prefix: string): JSX.Element {
127128
);
128129
}
129130

130-
function approxConversion(
131+
function textConversionPreview(
131132
isCkb2Udt: boolean,
132133
amount: bigint,
133-
tipHeader: I8Header,
134+
calculateRatio: (isCkb2Udt: boolean, amount: bigint) => OrderRatio,
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+
return toText(
137+
previewConversion(isCkb2Udt, amount, calculateRatio(isCkb2Udt, amount)),
138+
);
143139
}

src/queries.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@ import {
2020
} from "@ickb/lumos-utils";
2121
import {
2222
ickbDelta,
23+
ickbExchangeRatio,
2324
ickbLogicScript,
2425
ickbPoolSifter,
2526
ickbSifter,
2627
limitOrderScript,
2728
orderSifter,
2829
ownedOwnerScript,
30+
type OrderRatio,
2931
} from "@ickb/v1-core";
3032
import {
3133
txInfoPadding,
@@ -44,6 +46,7 @@ export interface L1StateType {
4446
ckbAvailable: bigint;
4547
ickbAvailable: bigint;
4648
tipHeader: Readonly<I8Header>;
49+
calculateRatio: (isCkb2Udt: boolean, amount: bigint) => OrderRatio;
4750
txBuilder: (isCkb2Udt: boolean, amount: bigint) => TxInfo;
4851
hasMatchable: boolean;
4952
}
@@ -73,6 +76,7 @@ export function l1StateOptions(
7376
ckbBalance: 6n * CKB * CKB,
7477
ickbBalance: 3n * CKB * CKB,
7578
tipHeader: headerPlaceholder,
79+
calculateRatio: () => ({ ckbMultiplier: 1n, udtMultiplier: 1n }),
7680
txBuilder: () => txInfoPadding,
7781
hasMatchable: false,
7882
},
@@ -217,6 +221,18 @@ async function getL1State(walletConfig: WalletConfig): Promise<L1StateType> {
217221
const calculateFee = (tx: TransactionSkeletonType): bigint =>
218222
calculateTxFee(txSize(tx) + txSizeOverhead, feeRate);
219223

224+
const currentRatio = ickbExchangeRatio(tipHeader);
225+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
226+
const calculateRatio = (isCkb2Udt: boolean, _amount: bigint): OrderRatio => {
227+
return {
228+
...currentRatio,
229+
udtMultiplier:
230+
currentRatio.udtMultiplier +
231+
// Pay 0.1% fee to bot
232+
(isCkb2Udt ? 1n : -1n) * (currentRatio.udtMultiplier / 1000n),
233+
};
234+
};
235+
220236
const txBuilder = (isCkb2Udt: boolean, amount: bigint): TxInfo => {
221237
if (amount > 0n) {
222238
return convert(
@@ -225,6 +241,7 @@ async function getL1State(walletConfig: WalletConfig): Promise<L1StateType> {
225241
amount,
226242
ickbPool,
227243
tipHeader,
244+
calculateRatio,
228245
calculateFee,
229246
walletConfig,
230247
);
@@ -248,6 +265,7 @@ async function getL1State(walletConfig: WalletConfig): Promise<L1StateType> {
248265
ckbAvailable,
249266
ickbAvailable,
250267
tipHeader,
268+
calculateRatio,
251269
txBuilder,
252270
hasMatchable,
253271
};

src/transaction.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import {
2222
type OrderRatio,
2323
} from "@ickb/v1-core";
2424
import {
25-
calculateRatio,
2625
maxEpoch,
2726
orderMaturityEstimate,
2827
txInfoPadding,
@@ -108,6 +107,7 @@ export function convert(
108107
amount: bigint,
109108
deposits: readonly ExtendedDeposit[],
110109
tipHeader: I8Header,
110+
calculateRatio: (isCkb2Udt: boolean, amount: bigint) => OrderRatio,
111111
calculateFee: (tx: TransactionSkeletonType) => bigint,
112112
walletConfig: WalletConfig,
113113
): Readonly<ConversionAttempt> {
@@ -132,8 +132,6 @@ export function convert(
132132
}
133133
}
134134
Object.freeze(ickbPool);
135-
136-
const ratio = calculateRatio(isCkb2Udt, tipHeader);
137135
const depositAmount = ckbSoftCapPerDeposit(tipHeader);
138136
const N = isCkb2Udt ? Number(amount / depositAmount) : ickbPool.length;
139137
const txCache = Array<TxInfo | undefined>(N);
@@ -146,10 +144,10 @@ export function convert(
146144
isCkb2Udt,
147145
amount,
148146
txInfo,
149-
ratio,
150147
depositAmount,
151148
ickbPool,
152149
tipHeader,
150+
calculateRatio,
153151
calculateFee,
154152
walletConfig,
155153
));
@@ -164,10 +162,10 @@ function convertAttempt(
164162
isCkb2Udt: boolean,
165163
amount: bigint,
166164
txInfo: TxInfo,
167-
ratio: OrderRatio,
168165
depositAmount: bigint,
169166
ickbPool: readonly MyExtendedDeposit[],
170167
tipHeader: I8Header,
168+
calculateRatio: (isCkb2Udt: boolean, amount: bigint) => OrderRatio,
171169
calculateFee: (tx: TransactionSkeletonType) => bigint,
172170
walletConfig: WalletConfig,
173171
): ConversionAttempt {
@@ -209,6 +207,7 @@ function convertAttempt(
209207

210208
let fee = txInfo.fee;
211209
if (amount > 0n) {
210+
const ratio = calculateRatio(isCkb2Udt, amount);
212211
tx = orderMint(
213212
tx,
214213
accountLocks[0],
@@ -218,7 +217,7 @@ function convertAttempt(
218217
isCkb2Udt ? ratio : undefined,
219218
isCkb2Udt ? undefined : ratio,
220219
);
221-
// 0.1% fee to bot
220+
// Take in account the fee paid to the bot
222221
fee += isCkb2Udt
223222
? amount -
224223
ickb2Ckb(

src/utils.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +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";
16+
import type { OrderRatio } from "@ickb/v1-core";
1717

1818
export interface RootConfig extends ChainConfig {
1919
queryClient: QueryClient;
@@ -134,16 +134,13 @@ 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+
export function previewConversion(
139138
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-
};
139+
amount: bigint,
140+
ratio: OrderRatio,
141+
): bigint {
142+
const { ckbMultiplier, udtMultiplier } = ratio;
143+
return isCkb2Udt
144+
? (amount * ckbMultiplier) / udtMultiplier
145+
: (amount * udtMultiplier) / ckbMultiplier;
149146
}

0 commit comments

Comments
 (0)