Skip to content

Commit

Permalink
Merge pull request #96 from cosmology-tech/upgrade-chain-registry-utils
Browse files Browse the repository at this point in the history
Upgrade chain registry utils
  • Loading branch information
pyramation committed May 8, 2024
2 parents 1688bb4 + 4c79f8d commit f5a3da9
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 88 deletions.
10 changes: 4 additions & 6 deletions packages/math/__tests__/apr.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// @ts-nocheck
import { assets } from 'chain-registry';
import { asset_lists } from '@chain-registry/assets';
import priceResponse from "../../../__fixtures__/coingecko/api/v3/simple/price/data.json";
import poolResponse from "../../../__fixtures__/rpc/osmosis/gamm/v1beta1/pools/data.json";
import activeGaugesResponse from "../../../__fixtures__/rpc/osmosis/incentives/v1beta1/active_gauges/data.json";
Expand All @@ -11,6 +10,7 @@ import cases from "jest-in-case";
import { omit } from "./pool-utils.test";
import { calcPoolAprs } from "../src/apr";
import { convertGeckoPricesToDenomPriceHash } from "../src/utils";
import { asset_lists } from '@chain-registry/assets';

const toCamel = (str) => {
return str.replace(/([-_][a-z])/gi, ($1) => {
Expand Down Expand Up @@ -56,13 +56,11 @@ const keysToCamel = (obj) => {
};

describe("Test APR calculations", () => {
let osmosisAssets, pools, prices;
let osmosisAssets: AssetList[], 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;
prices = convertGeckoPricesToDenomPriceHash(osmosisAssets, priceResponse);
prices = convertGeckoPricesToDenomPriceHash([...assets, ...asset_lists], priceResponse);
});

cases(
Expand Down
10 changes: 4 additions & 6 deletions packages/math/__tests__/pool-calculator.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @ts-nocheck
import { assets } from 'chain-registry';
import { assets } from 'chain-registry/mainnet';
import { asset_lists } from '@chain-registry/assets';
import priceResponse from "../../../__fixtures__/coingecko/api/v3/simple/price/data.json";
import poolResponse from "../../../__fixtures__/rpc/osmosis/gamm/v1beta1/pools/data.json";
Expand Down Expand Up @@ -56,14 +56,12 @@ const fakeBalances = [
];

describe("Test pool calculations of LiquidityPoolCalculator", () => {
let osmosisAssets, pools, prices;
let osmosisAssets: AssetList[], pools, prices;
let calculator: LiquidityPoolCalculator;
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([...asset_lists, ...assets], priceResponse);
calculator = new LiquidityPoolCalculator({ assets: osmosisAssets });
});

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
7 changes: 2 additions & 5 deletions packages/math/__tests__/swap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,13 @@ import poolResponse from "../../../__fixtures__/rpc/osmosis/gamm/v1beta1/pools/d
import { omit } from "./pool-utils.test";
import BigNumber from "bignumber.js";

const osmosisAssets = [
...(assets.find(({ chain_name }) => chain_name === 'osmosis')?.assets || []),
...(asset_lists.find(({ chain_name }) => chain_name === 'osmosis')?.assets || [])
];
const osmosisAssets = assets.filter(a => a.chain_name === 'osmosis')

describe("Test swap calculations", () => {
let pools, prices;
beforeAll(() => {
pools = poolResponse.pools.map((p) => omit(p, "@type"));
prices = convertGeckoPricesToDenomPriceHash(osmosisAssets, priceResponse);
prices = convertGeckoPricesToDenomPriceHash([...assets, ...asset_lists], priceResponse);
});

cases(
Expand Down
3 changes: 2 additions & 1 deletion packages/math/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@
},
"dependencies": {
"@chain-registry/types": "0.16.0",
"@chain-registry/utils": "1.13.1",
"@chain-registry/utils": "^1.30.0",
"bignumber.js": "9.1.2",
"chain-registry": "^1.46.7",
"decimal.js-light": "^2.5.1",
"osmojs": "^16.13.0"
},
Expand Down
6 changes: 3 additions & 3 deletions packages/math/src/pool-calculator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
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 @@ -24,11 +24,11 @@ import {
} from './pool-utils';

export interface LiquidityPoolCalculatorOptions {
assets: Asset[];
assets: AssetList[];
}

export class LiquidityPoolCalculator {
assets: Asset[];
assets: AssetList[];

constructor(options: LiquidityPoolCalculatorOptions) {
this.assets = options.assets;
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
4 changes: 2 additions & 2 deletions packages/math/src/swap.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Asset } from "@chain-registry/types";
import { AssetList } from "@chain-registry/types";
import { BigNumber } from "bignumber.js";
import { CoinDenom, Trade, PrettyPair } from "./types";
import { symbolToOsmoDenom } from "./utils";
Expand Down Expand Up @@ -47,7 +47,7 @@ export const routesThroughPools = ({
};

export const getRoutesForTrade = (
assets: Asset[],
assets: AssetList[],
{
trade,
pairs,
Expand Down
4 changes: 2 additions & 2 deletions packages/math/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Asset, AssetDenomUnit } from "@chain-registry/types";
import { AssetDenomUnit, AssetList } from "@chain-registry/types";
import { Coin } from "osmojs/cosmos/base/v1beta1/coin";
import { Duration } from "osmojs/google/protobuf/duration";
import { Pool } from "osmojs/osmosis/gamm/v1beta1/balancerPool";
Expand Down Expand Up @@ -58,7 +58,7 @@ export interface PoolPretty extends Pool {
export interface CalcPoolAprsParams {
activeGauges: Gauge[];
pool: Pool;
assets: Asset[];
assets: AssetList[];
prices: PriceHash;
superfluidPools: SuperfluidAsset[];
aprSuperfluid: string | number;
Expand Down
50 changes: 25 additions & 25 deletions packages/math/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,73 +6,73 @@ import {
PriceHash,
CoinGeckoUSDResponse,
} from "./types";
import { Asset } from "@chain-registry/types";
import { Asset, AssetList } from "@chain-registry/types";
import {
getAssetByDenom,
getDenomByCoinGeckoId,
getSymbolByChainDenom,
getChainDenomBySymbol,
getSymbolByDenom,
getDenomBySymbol,
getExponentByDenom as _getExponentByDenom,
convertCoinGeckoPricesToDenomPriceMap,
noDecimals as _noDecimals,
convertBaseUnitsToDollarValue,
convertDollarValueToDenomUnits,
convertBaseUnitsToDisplayUnits
mapCoinGeckoPricesToDenoms,
roundDown,
convertBaseUnitToDollarValue,
convertDollarValueToBaseUnit,
convertBaseUnitToDisplayUnit,
} from "@chain-registry/utils";

export const getOsmoAssetByDenom = (assets: Asset[], denom: CoinDenom): Asset => {
return getAssetByDenom(assets, denom);
export const getOsmoAssetByDenom = (assets: AssetList[], denom: CoinDenom): Asset => {
return getAssetByDenom(assets, denom) as Asset
};

export const getDenomForCoinGeckoId = (assets: Asset[], coinGeckoId: CoinGeckoToken): CoinDenom => {
export const getDenomForCoinGeckoId = (assets: AssetList[], coinGeckoId: CoinGeckoToken): CoinDenom => {
return getDenomByCoinGeckoId(assets, coinGeckoId);
};

export const osmoDenomToSymbol = (assets: Asset[], denom: CoinDenom): CoinSymbol => {
return getSymbolByChainDenom(assets, denom);
export const osmoDenomToSymbol = (assets: AssetList[], denom: CoinDenom): CoinSymbol => {
return getSymbolByDenom(assets, denom);
};

export const symbolToOsmoDenom = (assets: Asset[], token: CoinSymbol): CoinDenom => {
return getChainDenomBySymbol(assets, token);
export const symbolToOsmoDenom = (assets: AssetList[], token: CoinSymbol): CoinDenom => {
return getDenomBySymbol(assets, token);
};

export const getExponentByDenom = (assets: Asset[], denom: CoinDenom): Exponent => {
export const getExponentByDenom = (assets: AssetList[], denom: CoinDenom): Exponent => {
return _getExponentByDenom(assets, denom);
};

export const convertGeckoPricesToDenomPriceHash = (
assets: Asset[],
assets: AssetList[],
prices: CoinGeckoUSDResponse
): PriceHash => {
return convertCoinGeckoPricesToDenomPriceMap(assets, prices);
return mapCoinGeckoPricesToDenoms(assets, prices);
};

export const noDecimals = (num: number | string) => {
return _noDecimals(num);
return roundDown(num);
};

export const baseUnitsToDollarValue = (
assets: Asset[],
assets: AssetList[],
prices: PriceHash,
symbol: string,
amount: string | number
) => {
return convertBaseUnitsToDollarValue(assets, prices, symbol, amount);
return convertBaseUnitToDollarValue(assets, prices, symbol, amount);
};

export const dollarValueToDenomUnits = (
assets: Asset[],
assets: AssetList[],
prices: PriceHash,
symbol: string,
value: string | number
) => {
return convertDollarValueToDenomUnits(assets, prices, symbol, value);
return convertDollarValueToBaseUnit(assets, prices, symbol, value);
};

export const baseUnitsToDisplayUnits = (
assets: Asset[],
assets: AssetList[],
symbol: string,
amount: string | number
) => {
return convertBaseUnitsToDisplayUnits(assets, symbol, amount);
return convertBaseUnitToDisplayUnit(assets, symbol, amount);
};
Loading

0 comments on commit f5a3da9

Please sign in to comment.