Skip to content

Commit

Permalink
fix price impact calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
alecananian committed Jan 19, 2025
1 parent ffd70f3 commit 25521f2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 18 deletions.
14 changes: 10 additions & 4 deletions app/lib/pools.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { NetworkInfo } from "@sushiswap/tines";
import type { NetworkInfo, RToken } from "@sushiswap/tines";
import {
ConstantProductRPool,
findMultiRouteExactIn,
Expand All @@ -8,7 +8,6 @@ import { parseUnits } from "viem";

import type { AddressString, Pool, Token } from "~/types";
import { formatAmount, formatUSD } from "./currency";
import { tokenToRToken } from "./tokens";

export const quote = (amountA: bigint, reserveA: bigint, reserveB: bigint) =>
reserveA > 0 ? (amountA * reserveB) / reserveA : 0n;
Expand All @@ -31,6 +30,13 @@ export const getAmountMax = (amount: bigint, slippage: number) =>
export const getAmountMin = (amount: bigint, slippage: number) =>
amount - (amount * BigInt(Math.ceil(slippage * 1000))) / 1000n;

const tokenToRToken = ({ address, name, symbol, decimals }: Token): RToken => ({
address,
name,
symbol,
decimals,
});

export const createSwapRoute = (
tokenIn: Token,
tokenOut: Token | undefined,
Expand Down Expand Up @@ -60,8 +66,8 @@ export const createSwapRoute = (
tokenToRToken(token0),
tokenToRToken(token1),
lpFee + protocolFee + royaltiesFee,
parseUnits(reserve0, token0.decimals),
parseUnits(reserve1, token0.decimals),
BigInt(reserve0),
BigInt(reserve1),
);
},
);
Expand Down
14 changes: 0 additions & 14 deletions app/lib/tokens.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,8 @@
import type { RToken } from "@sushiswap/tines";

import type { Token } from "~/types";
import { sumArray } from "./array";
import { formatAmount } from "./currency";
import { floorBigInt } from "./number";

export const tokenToRToken = ({
address,
name,
symbol,
decimals,
}: Token): RToken => ({
name,
symbol,
address,
decimals,
});

export const countTokens = (tokens: { amount: number | string }[]) =>
sumArray(tokens.map(({ amount }) => Number(amount)));

Expand Down

0 comments on commit 25521f2

Please sign in to comment.