From 2b704ce43599acb821182a819fb97032dbedb76e Mon Sep 17 00:00:00 2001 From: Lucas Jiang Date: Mon, 6 May 2024 11:57:05 +0800 Subject: [PATCH] upgrade swap --- .../__tests__/__snapshots__/swap.test.ts.snap | 24 +++++++++---------- packages/math/__tests__/swap.test.ts | 7 ++---- packages/math/src/swap.ts | 4 ++-- 3 files changed, 16 insertions(+), 19 deletions(-) diff --git a/packages/math/__tests__/__snapshots__/swap.test.ts.snap b/packages/math/__tests__/__snapshots__/swap.test.ts.snap index 910d10fc9..d4d3db118 100644 --- a/packages/math/__tests__/__snapshots__/swap.test.ts.snap +++ b/packages/math/__tests__/__snapshots__/swap.test.ts.snap @@ -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", }, ] `; @@ -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", }, ] `; @@ -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", }, ] `; diff --git a/packages/math/__tests__/swap.test.ts b/packages/math/__tests__/swap.test.ts index 4d3d54c60..ea2f798e3 100644 --- a/packages/math/__tests__/swap.test.ts +++ b/packages/math/__tests__/swap.test.ts @@ -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( diff --git a/packages/math/src/swap.ts b/packages/math/src/swap.ts index 983812e91..37b6ab79f 100644 --- a/packages/math/src/swap.ts +++ b/packages/math/src/swap.ts @@ -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"; @@ -47,7 +47,7 @@ export const routesThroughPools = ({ }; export const getRoutesForTrade = ( - assets: Asset[], + assets: AssetList[], { trade, pairs,