Skip to content

Commit

Permalink
add price after trade calcs
Browse files Browse the repository at this point in the history
  • Loading branch information
Robiquet committed May 8, 2024
1 parent 83d5d4d commit 84a7324
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
27 changes: 27 additions & 0 deletions lib/util/amm2.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
isValidBuyAmount,
isValidSellAmount,
calculateReserveAfterSell,
calculateSpotPriceAfterBuy,
calculateSpotPriceAfterSell,
} from "./amm2";
import { ZTG } from "@zeitgeistpm/sdk";

Expand Down Expand Up @@ -83,6 +85,31 @@ describe("amm2", () => {
});
});

describe("calculateSpotPriceAfterBuy", () => {
test("should work", () => {
const spotPrice = calculateSpotPriceAfterBuy(
new Decimal(59.00001516623987),
new Decimal(144.00003701590745),
new Decimal(543.3339883933237),
new Decimal(486),
);

expect(spotPrice.toNumber()).toEqual(0.98849704337919602199);
});
});
describe("calculateSpotPriceAfterSell", () => {
test("should work", () => {
const spotPrice = calculateSpotPriceAfterSell(
new Decimal(99.0),
new Decimal(108.04431012579187),
new Decimal(40),
new Decimal(14.27511827415865),
);

expect(spotPrice.toFixed(8)).toEqual("0.31525092");
});
});

describe("approximateMaxAmountInForBuy", () => {
//seems like correct number would be 41
test("should work", () => {
Expand Down
24 changes: 24 additions & 0 deletions lib/util/amm2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,30 @@ export const calculateSpotPrice = (
return new Decimal(0).minus(reserve).div(liquidity).exp();
};

export const calculateSpotPriceAfterBuy = (
initialReserve: Decimal, // amount of asset in the pool
liquidity: Decimal, // liqudity parameter of the pool
outcomeAssetOut: Decimal,
baseAssetAmountIn: Decimal,
) => {
const newReserve = initialReserve.minus(
outcomeAssetOut.minus(baseAssetAmountIn),
);
return calculateSpotPrice(newReserve, liquidity);
};

export const calculateSpotPriceAfterSell = (
initialReserve: Decimal, // amount of asset in the pool
liquidity: Decimal, // liqudity parameter of the pool
outcomeAssetIn: Decimal,
baseAssetAmountOut: Decimal,
) => {
const newReserve = initialReserve.plus(
outcomeAssetIn.minus(baseAssetAmountOut),
);
return calculateSpotPrice(newReserve, liquidity);
};

export const approximateMaxAmountInForBuy = (
reserve: Decimal, // amount of asset in the pool
liquidity: Decimal, // liqudity parameter of the pool
Expand Down

0 comments on commit 84a7324

Please sign in to comment.