Skip to content

Commit

Permalink
[GSW-464] feat: Add utility functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jinoosss committed Nov 8, 2023
1 parent a35748b commit be2fef5
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/web/src/utils/number-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export function toMillionFormat(value: number | string) {
}
const MILLION = 1000000;
if (num.isLessThan(MILLION)) {
return BigNumber(num).toFormat();
return BigNumber(num).toFormat(2);
}
return `${BigNumber(num).dividedBy(MILLION).toFormat(2)}m`;
}
Expand Down
66 changes: 66 additions & 0 deletions packages/web/src/utils/swap-utils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { priceToNearTick, priceToTick, tickToPrice } from "./swap-utils";

describe("tick convert to price", () => {
test("0 to 1", () => {
const tick = 0;
expect(tickToPrice(tick)).toBe(1);
});

test("10000 to 1.6486800559311758", () => {
const tick = 10000;
expect(tickToPrice(tick)).toBe(1.6486800559311758);
});

test("10001 to 1.6487624878732252", () => {
const tick = 10001;
expect(tickToPrice(tick)).toBe(1.6487624878732252);
});
});

describe("price convert to tick", () => {
test("1 to 0", () => {
const price = 1;
expect(priceToTick(price)).toBe(0);
});

test("1.6486800559311758 to 10000", () => {
const price = 1.6486800559311758;
expect(priceToTick(price)).toBe(10000);
});

test("1.6487624878732252 to 10001", () => {
const price = 1.6487624878732252;
expect(priceToTick(price)).toBe(10001);
});

test("0.60651549714 to -10001", () => {
const price = 0.60651549714;
expect(priceToTick(price)).toBe(-10001);
});
});

describe("price convert to near tick", () => {
test("1 to 0", () => {
const price = 1;
const tickSpacing = 2;
expect(priceToNearTick(price, tickSpacing)).toBe(0);
});

test("1.6486800559311758 to 10002", () => {
const price = 1.6489273641220126;
const tickSpacing = 2;
expect(priceToNearTick(price, tickSpacing)).toBe(10002);
});

test("0.60651549714 to -10002", () => {
const price = 0.60651549714;
const tickSpacing = 2;
expect(priceToNearTick(price, tickSpacing)).toBe(-10002);
});

test("0.60651549714 to -10004", () => {
const price = 0.60651549714;
const tickSpacing = 4;
expect(priceToNearTick(price, tickSpacing)).toBe(-10004);
});
});
40 changes: 40 additions & 0 deletions packages/web/src/utils/swap-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
SwapFeeTierInfoMap,
SwapFeeTierType,
} from "@constants/option.constant";
import { MAX_TICK, MIN_TICK } from "@constants/swap.constant";
import BigNumber from "bignumber.js";

export function getCurrentPriceByRaw(raw: string) {
Expand All @@ -16,3 +17,42 @@ export function makeSwapFeeTier(value: string | number): SwapFeeTierType {
}
return "NONE";
}

export function tickToPrice(tick: number) {
const pow10001 = BigNumber(1.0001).pow(tick);
return Number(BigNumber(pow10001).sqrt().toFixed(16));
}

export function priceToTick(price: number) {
const logPrice = Math.log(price ** 2);
const log10001 = Math.log(1.0001);
return Math.round(BigNumber(logPrice).dividedBy(log10001).toNumber());
}

export function priceToNearTick(price: number, tickSpacing: number) {
const tickRaw = priceToTick(price);
const mod = Math.abs(tickRaw) % tickSpacing;
const sign = Math.sign(tickRaw);

if (sign < 0) {
return tickRaw - (tickSpacing - mod);
}
return tickRaw - mod;
}

export function tickToPriceStr(tick: number, decimals?: number) {
if (tick === MIN_TICK + 1) {
return "0.00";
}
if (tick === MAX_TICK - 1) {
return "∞";
}
const decimalsLimit = decimals || 4;
const result = BigNumber(1.001 ** (tick / 2))
.toFormat(decimalsLimit)
.replace(/\.?0+$/, "");
if (result === "0") {
return "";
}
return result;
}

0 comments on commit be2fef5

Please sign in to comment.