Skip to content

Commit

Permalink
[GSW-581] feat: Add test environments and test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
jinoosss committed Nov 29, 2023
1 parent e7c077a commit a94474b
Show file tree
Hide file tree
Showing 6 changed files with 748 additions and 21 deletions.
159 changes: 155 additions & 4 deletions packages/swap-router/src/swap-router/swap-router-default.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,52 @@
import { printEstimateRouteInfo, printPoolInfo } from "../common";
import { sumBigInts } from "../common/array.util";
import { Pool } from "../swap-simulator";
import { SwapRouter } from "./swap-router";

const defaultPool = [
{
poolPath: "gno.land/r/bar:gno.land/r/baz:500",
tokenAPath: "gno.land/r/bar",
tokenBPath: "gno.land/r/baz",
fee: 500,
tokenABalance: 9937690n,
tokenBBalance: 10943746n,
tickSpacing: 10,
maxLiquidityPerTick: 103951672670308,
price: 1.2904451607773892,
sqrtPriceX96: 102239599540632204908365252323n,
tick: 5099,
feeProtocol: 0,
tokenAProtocolFee: 0,
tokenBProtocolFee: 0,
liquidity: 291453167n,
ticks: [4000, 6000, 5000],
tickBitmaps: {
"1":
"28269553036454149273332760011908996998438272973151439048218347582187896832",
"2": "309485009821345068724781056",
},
positions: [
{
liquidity: 144812895n,
owner: "g1htpxzv2dkplvzg50nd8fswrneaxmdpwn459thx",
tickLower: 4000,
tickUpper: 6000,
tokenAOwed: 0n,
tokenBOwed: 0n,
},
{
liquidity: 146640272n,
owner: "g1htpxzv2dkplvzg50nd8fswrneaxmdpwn459thx",
tickLower: 5000,
tickUpper: 6000,
tokenAOwed: 0n,
tokenBOwed: 0n,
},
],
},
];

const pools: Pool[] = [
{
poolPath: "gno.land/r/bar:gno.land/r/wugnot:100",
Expand Down Expand Up @@ -194,16 +239,113 @@ const pools: Pool[] = [
},
];

describe("swap router of swap simulator test pool", () => {
describe("gno.land/r/bar to gno.land/r/baz", () => {
test("EXANCT_IN 10000n", async () => {
const swapRouter = new SwapRouter(defaultPool);
const estimatedRoutes = swapRouter.estimateSwapRoute(
"gno.land/r/bar",
"gno.land/r/baz",
10000n,
"EXACT_IN",
);

printEstimateRouteInfo(estimatedRoutes);

const sumAmount = sumBigInts(
estimatedRoutes.map(route => route.amountOut),
);
expect(estimatedRoutes).toHaveLength(1);
expect(estimatedRoutes[0].routeKey).toBe(
"gno.land/r/bar:gno.land/r/baz:500",
);
expect(estimatedRoutes[0].quote).toBe(100);
expect(sumAmount).toBe(16643n);
});

test("EXANCT_OUT, 10000n", async () => {
const swapRouter = new SwapRouter(defaultPool);
const estimatedRoutes = swapRouter.estimateSwapRoute(
"gno.land/r/bar",
"gno.land/r/baz",
10000n,
"EXACT_OUT",
);

printEstimateRouteInfo(estimatedRoutes);

const sumAmount = sumBigInts(
estimatedRoutes.map(route => route.amountOut),
);
expect(estimatedRoutes).toHaveLength(1);
expect(estimatedRoutes[0].routeKey).toBe(
"gno.land/r/bar:gno.land/r/baz:500",
);
expect(estimatedRoutes[0].quote).toBe(100);
expect(sumAmount).toBe(6008n);
});

describe("gno.land/r/baz to gno.land/r/bar", () => {
test("EXANCT_IN, 10000n", async () => {
const swapRouter = new SwapRouter(defaultPool);
const estimatedRoutes = swapRouter.estimateSwapRoute(
"gno.land/r/baz",
"gno.land/r/bar",
10000n,
"EXACT_IN",
);

printEstimateRouteInfo(estimatedRoutes);

const sumAmount = sumBigInts(
estimatedRoutes.map(route => route.amountOut),
);
expect(estimatedRoutes).toHaveLength(1);
expect(estimatedRoutes[0].routeKey).toBe(
"gno.land/r/bar:gno.land/r/baz:500",
);
expect(estimatedRoutes[0].quote).toBe(100);
expect(sumAmount).toBe(6001n);
});

test("EXANCT_OUT, 10000n", async () => {
const swapRouter = new SwapRouter(defaultPool);
const estimatedRoutes = swapRouter.estimateSwapRoute(
"gno.land/r/baz",
"gno.land/r/bar",
10000n,
"EXACT_OUT",
);

printEstimateRouteInfo(estimatedRoutes);

const sumAmount = sumBigInts(
estimatedRoutes.map(route => route.amountOut),
);
expect(estimatedRoutes).toHaveLength(1);
expect(estimatedRoutes[0].routeKey).toBe(
"gno.land/r/bar:gno.land/r/baz:500",
);
expect(estimatedRoutes[0].quote).toBe(100);
expect(sumAmount).toBe(16661n);
});
});
});
});

describe("swap simulator", () => {
describe("gno.land/r/bar to gno.land/r/baz", () => {
test("EXANCT_IN 10000n is 16417n", async () => {
test("EXANCT_IN 10000n", async () => {
const swapRouter = new SwapRouter(pools);
const estimatedRoutes = swapRouter.estimateSwapRoute(
"gno.land/r/bar",
"gno.land/r/baz",
10000n,
"EXACT_IN",
);

printEstimateRouteInfo(estimatedRoutes);

const sumAmount = sumBigInts(
estimatedRoutes.map(route => route.amountOut),
);
Expand All @@ -219,14 +361,17 @@ describe("swap simulator", () => {
expect(sumAmount).toBe(16417n);
});

test("EXANCT_OUT, 10000n is 27036n", async () => {
test("EXANCT_OUT, 10000n", async () => {
const swapRouter = new SwapRouter(pools);
const estimatedRoutes = swapRouter.estimateSwapRoute(
"gno.land/r/bar",
"gno.land/r/baz",
10000n,
"EXACT_OUT",
);

printEstimateRouteInfo(estimatedRoutes);

const sumAmount = sumBigInts(
estimatedRoutes.map(route => route.amountOut),
);
Expand All @@ -239,14 +384,17 @@ describe("swap simulator", () => {
});

describe("gno.land/r/baz to gno.land/r/bar", () => {
test("EXANCT_IN, 10000n is 26992n", async () => {
test("EXANCT_IN, 10000n", async () => {
const swapRouter = new SwapRouter(pools);
const estimatedRoutes = swapRouter.estimateSwapRoute(
"gno.land/r/baz",
"gno.land/r/bar",
10000n,
"EXACT_IN",
);

printEstimateRouteInfo(estimatedRoutes);

const sumAmount = sumBigInts(
estimatedRoutes.map(route => route.amountOut),
);
Expand All @@ -258,14 +406,17 @@ describe("swap simulator", () => {
expect(sumAmount).toBe(26992n);
});

test("EXANCT_OUT, 10000n is 16438n", async () => {
test("EXANCT_OUT, 10000n", async () => {
const swapRouter = new SwapRouter(pools);
const estimatedRoutes = swapRouter.estimateSwapRoute(
"gno.land/r/baz",
"gno.land/r/bar",
10000n,
"EXACT_OUT",
);

printEstimateRouteInfo(estimatedRoutes);

const sumAmount = sumBigInts(
estimatedRoutes.map(route => route.amountOut),
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { printEstimateRouteInfo } from "../common";
import { sumBigInts } from "../common/array.util";
import { makePoolsByRPC } from "../common/mapper";
import { SwapRouter } from "./swap-router";
Expand Down Expand Up @@ -57,6 +58,8 @@ describe("swap router of higher range positions pool", () => {
"EXACT_IN",
);

printEstimateRouteInfo(estimatedRoutes);

const sumAmount = sumBigInts(
estimatedRoutes.map(route => route.amountOut),
);
Expand All @@ -78,6 +81,8 @@ describe("swap router of higher range positions pool", () => {
"EXACT_OUT",
);

printEstimateRouteInfo(estimatedRoutes);

const sumAmount = sumBigInts(
estimatedRoutes.map(route => route.amountOut),
);
Expand All @@ -86,7 +91,7 @@ describe("swap router of higher range positions pool", () => {
"gno.land/r/bar:gno.land/r/baz:500",
);
expect(estimatedRoutes[0].quote).toBe(100);
expect(sumAmount).toBe(4176n);
expect(sumAmount).toBe(3253n);
});
});

Expand All @@ -100,6 +105,8 @@ describe("swap router of higher range positions pool", () => {
"EXACT_IN",
);

printEstimateRouteInfo(estimatedRoutes);

const sumAmount = sumBigInts(
estimatedRoutes.map(route => route.amountOut),
);
Expand All @@ -120,6 +127,8 @@ describe("swap router of higher range positions pool", () => {
"EXACT_OUT",
);

printEstimateRouteInfo(estimatedRoutes);

const sumAmount = sumBigInts(
estimatedRoutes.map(route => route.amountOut),
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { printEstimateRouteInfo } from "../common";
import { sumBigInts } from "../common/array.util";
import { makePoolsByRPC } from "../common/mapper";
import { SwapRouter } from "./swap-router";
Expand Down Expand Up @@ -56,6 +57,8 @@ describe("swap router of lower range positions pool", () => {
"EXACT_IN",
);

printEstimateRouteInfo(estimatedRoutes);

const sumAmount = sumBigInts(
estimatedRoutes.map(route => route.amountOut),
);
Expand All @@ -77,6 +80,8 @@ describe("swap router of lower range positions pool", () => {
"EXACT_OUT",
);

printEstimateRouteInfo(estimatedRoutes);

const sumAmount = sumBigInts(
estimatedRoutes.map(route => route.amountOut),
);
Expand All @@ -99,6 +104,8 @@ describe("swap router of lower range positions pool", () => {
"EXACT_IN",
);

printEstimateRouteInfo(estimatedRoutes);

const sumAmount = sumBigInts(
estimatedRoutes.map(route => route.amountOut),
);
Expand All @@ -119,6 +126,8 @@ describe("swap router of lower range positions pool", () => {
"EXACT_OUT",
);

printEstimateRouteInfo(estimatedRoutes);

const sumAmount = sumBigInts(
estimatedRoutes.map(route => route.amountOut),
);
Expand All @@ -127,7 +136,7 @@ describe("swap router of lower range positions pool", () => {
"gno.land/r/bar:gno.land/r/baz:500",
);
expect(estimatedRoutes[0].quote).toBe(100);
expect(sumAmount).toBe(40195n);
expect(sumAmount).toBe(8843n);
});
});
});
Loading

0 comments on commit a94474b

Please sign in to comment.