Skip to content

Commit

Permalink
upgrade pool util
Browse files Browse the repository at this point in the history
  • Loading branch information
theothersideofgods committed May 6, 2024
1 parent eab4d60 commit 1bd2e1b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 32 deletions.
24 changes: 12 additions & 12 deletions packages/math/__tests__/__snapshots__/pool-utils.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,20 @@ exports[`Test pool calculations calcCoinsNeededForValue 10 for pool#1 1`] = `
{
"amount": "136500",
"denom": "ibc/27394FB092D2ECCD56123C74F36E4C1F926001CEADA9CA97EA622B25F41E5EB2",
"displayAmount": "0.13650013650013650014",
"displayAmount": "0.1365",
"shareTotalValue": "5",
"symbol": "ATOM",
"totalDollarValue": "226964145.58592049",
"unitRatio": "2.202991131966e-8",
"unitRatio": "2.202988928975e-8",
},
{
"amount": "549450",
"denom": "uosmo",
"displayAmount": "0.54945054945054945055",
"displayAmount": "0.54945",
"shareTotalValue": "5",
"symbol": "OSMO",
"totalDollarValue": "219898456.741054",
"unitRatio": "2.273776757737e-8",
"unitRatio": "2.27377448396e-8",
},
]
`;
Expand All @@ -51,20 +51,20 @@ exports[`Test pool calculations calcCoinsNeededForValue 10 for pool#497 1`] = `
{
"amount": "275633",
"denom": "ibc/46B44899322F3CD854D2D46DEEF881958467CDD4B3B10086DA49296BBED94BED",
"displayAmount": "0.27563395810363836825",
"displayAmount": "0.275633",
"shareTotalValue": "5",
"symbol": "JUNO",
"totalDollarValue": "33383106.20983148",
"unitRatio": "1.4977635599792e-7",
"unitRatio": "1.497758353753e-7",
},
{
"amount": "549450",
"denom": "uosmo",
"displayAmount": "0.54945054945054945055",
"displayAmount": "0.54945",
"shareTotalValue": "5",
"symbol": "OSMO",
"totalDollarValue": "32326707.4682188",
"unitRatio": "1.5467087097922e-7",
"unitRatio": "1.5467071630835e-7",
},
]
`;
Expand All @@ -74,20 +74,20 @@ exports[`Test pool calculations calcCoinsNeededForValue 10 for pool#604 1`] = `
{
"amount": "7051808",
"denom": "ibc/987C17B11ABC2B20019178ACE62929FE9840202CE79498E29FE8E5CB02B7C0A4",
"displayAmount": "7.05180822466496859125",
"displayAmount": "7.051808",
"shareTotalValue": "5",
"symbol": "STARS",
"totalDollarValue": "13817947.976249658516",
"unitRatio": "3.6184822873802e-7",
"unitRatio": "3.6184821720982e-7",
},
{
"amount": "549450",
"denom": "uosmo",
"displayAmount": "0.54945054945054945055",
"displayAmount": "0.54945",
"shareTotalValue": "5",
"symbol": "OSMO",
"totalDollarValue": "11087098.9624122",
"unitRatio": "4.5097459822007e-7",
"unitRatio": "4.5097414724547e-7",
},
]
`;
Expand Down
6 changes: 2 additions & 4 deletions packages/math/__tests__/pool-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,9 @@ export const omit = (obj, ...props) => {
describe("Test pool calculations", () => {
let osmosisAssets, pools, prices;
beforeAll(() => {
osmosisAssets = assets.find(({ chain_name }) => chain_name === 'osmosis');
const osmosisAssetList = asset_lists.find(({ chain_name }) => chain_name === 'osmosis');
osmosisAssets = [...(osmosisAssets?.assets || []), ...(osmosisAssetList?.assets || [])];
osmosisAssets = assets.filter(a => a.chain_name === 'osmosis')
pools = poolResponse.pools.map((p) => omit(p, "@type"));
prices = convertGeckoPricesToDenomPriceHash(osmosisAssets, priceResponse);
prices = convertGeckoPricesToDenomPriceHash([...assets, ...asset_lists], priceResponse);
});

cases(
Expand Down
37 changes: 21 additions & 16 deletions packages/math/src/pool-utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @ts-nocheck
import { Asset } from "@chain-registry/types";
import { AssetList } from "@chain-registry/types";
import { Pool } from "osmojs/osmosis/gamm/v1beta1/balancerPool";
import { Coin } from "osmojs/cosmos/base/v1beta1/coin";
import {
Expand All @@ -21,12 +21,11 @@ import {
getOsmoAssetByDenom,
} from "./utils";

export const calcPoolLiquidity = (assets: Asset[], pool: Pool, prices: PriceHash): string => {
export const calcPoolLiquidity = (assets: AssetList[], pool: Pool, prices: PriceHash): string => {
return pool.poolAssets
.reduce((res, { token }) => {
const liquidity = new BigNumber(token.amount)
.shiftedBy(-getExponentByDenom(assets, token.denom))
.multipliedBy(prices[token.denom]);
const symbol = osmoDenomToSymbol(assets, token.denom);
const liquidity = baseUnitsToDollarValue(assets, prices, symbol, token.amount);
return res.plus(liquidity);
}, new BigNumber(0))
.toString();
Expand All @@ -37,7 +36,7 @@ export const getPoolByGammName = (pools: Pool[], gammId: string): Pool => {
};

export const convertGammTokenToDollarValue = (
assets: Asset[],
assets: AssetList[],
coin: Coin,
pool: Pool,
prices: PriceHash
Expand All @@ -52,7 +51,7 @@ export const convertGammTokenToDollarValue = (
};

export const convertDollarValueToCoins = (
assets: Asset[],
assets: AssetList[],
value: string | number,
pool: Pool,
prices: PriceHash
Expand All @@ -78,7 +77,7 @@ export const convertDollarValueToCoins = (
};

export const convertDollarValueToShares = (
assets: Asset[],
assets: AssetList[],
value: string | number,
pool: Pool,
prices: PriceHash
Expand All @@ -93,7 +92,7 @@ export const convertDollarValueToShares = (
};

export const prettyPool = (
assets: Asset[],
assets: AssetList[],
pool: Pool,
{ includeDetails = false } = {}
): PoolPretty => {
Expand Down Expand Up @@ -143,15 +142,16 @@ export const prettyPool = (
};

export const calcCoinsNeededForValue = (
assets: Asset[],
assets: AssetList[],
prices: PriceHash,
poolInfo: PoolPretty,
value: string | number
) => {
const val = new BigNumber(value);
const coinsNeeded = poolInfo.poolAssetsPretty.map(
({ symbol, amount, denom, ratio }) => {
({ amount, denom, ratio }) => {
const valueByRatio = val.multipliedBy(ratio).toString();
const symbol = osmoDenomToSymbol(assets, denom)
const amountNeeded = dollarValueToDenomUnits(
assets,
prices,
Expand All @@ -177,7 +177,7 @@ export const calcCoinsNeededForValue = (
};

export const getCoinBalance = (
assets: Asset[],
assets: AssetList[],
prices: PriceHash,
balances: Coin[],
prettyAsset: PoolAssetPretty
Expand All @@ -190,18 +190,20 @@ export const getCoinBalance = (
return { ...coinBalance, displayValue: 0 };
}

const symbol = osmoDenomToSymbol(assets, prettyAsset.denom)

const displayValue = baseUnitsToDollarValue(
assets,
prices,
prettyAsset.symbol,
symbol,
coinBalance.amount
);

return { ...coinBalance, displayValue };
};

export const calcMaxCoinsForPool = (
assets: Asset[],
assets: AssetList[],
prices: PriceHash,
poolInfo: PoolPretty,
balances: Coin[]
Expand All @@ -218,7 +220,10 @@ export const calcMaxCoinsForPool = (
const coinValue = new BigNumber(smallestTotalDollarValue)
.multipliedBy(asset.ratio)
.toString();
const amount = dollarValueToDenomUnits(assets, prices, asset.symbol, coinValue);

const symbol = osmoDenomToSymbol(assets, asset.denom)

const amount = dollarValueToDenomUnits(assets, prices, symbol, coinValue);

return {
denom: asset.denom,
Expand Down Expand Up @@ -251,7 +256,7 @@ export const calcShareOutAmount = (
};

export const makePoolPairs = (
assets: Asset[],
assets: AssetList[],
pools: Pool[],
prices: PriceHash,
liquidityLimit = 100_000
Expand Down

0 comments on commit 1bd2e1b

Please sign in to comment.