From ebe05b12fd78c74accee472322c1a73bff05f4c5 Mon Sep 17 00:00:00 2001 From: Bob van der Helm <34470358+bobthebuidlr@users.noreply.github.com> Date: Thu, 11 Jan 2024 15:03:02 +0100 Subject: [PATCH] fix swap fee for osmosis (#725) --- .../{getPools.ts => getOsmosisSwapFee.ts} | 15 ++++++++-- .../TradeModule/SwapForm/TradeSummary.tsx | 2 +- src/hooks/useSwapFee.ts | 23 ++++++++------- src/types/interfaces/swap.d.ts | 29 +++++++++++++++++++ src/utils/constants.ts | 1 + 5 files changed, 55 insertions(+), 15 deletions(-) rename src/api/swap/{getPools.ts => getOsmosisSwapFee.ts} (62%) create mode 100644 src/types/interfaces/swap.d.ts diff --git a/src/api/swap/getPools.ts b/src/api/swap/getOsmosisSwapFee.ts similarity index 62% rename from src/api/swap/getPools.ts rename to src/api/swap/getOsmosisSwapFee.ts index 9c86d053..4577e687 100644 --- a/src/api/swap/getPools.ts +++ b/src/api/swap/getOsmosisSwapFee.ts @@ -1,14 +1,23 @@ -export default async function getPools( +import { BN_ZERO } from 'constants/math' +import { STANDARD_SWAP_FEE } from 'utils/constants' + +export default async function getOsmosisSwapFee( chainConfig: ChainConfig, poolIds: string[], -): Promise { +): Promise { const promises = poolIds.map((poolId) => fetch(chainConfig.endpoints.pools.replace('POOL_ID', poolId)), ) const responses = await Promise.all(promises) - return await Promise.all(responses.map(async (pool) => (await pool.json()).pool as Pool)) + const pools = await Promise.all(responses.map(async (pool) => (await pool.json()).pool as Pool)) + + if (!pools?.length) return STANDARD_SWAP_FEE + + return pools + .reduce((acc, pool) => acc.plus(pool?.pool_params?.swap_fee || STANDARD_SWAP_FEE), BN_ZERO) + .toNumber() } interface Pool { diff --git a/src/components/Trade/TradeModule/SwapForm/TradeSummary.tsx b/src/components/Trade/TradeModule/SwapForm/TradeSummary.tsx index 856fc7e7..1be62835 100644 --- a/src/components/Trade/TradeModule/SwapForm/TradeSummary.tsx +++ b/src/components/Trade/TradeModule/SwapForm/TradeSummary.tsx @@ -62,7 +62,7 @@ export default function TradeSummary(props: Props) { const assets = useAllAssets() const sellAssetPrice = usePrice(sellAsset.denom) // FIXME: ⛓️ Swap fee needs to be chainagnostic! - const swapFee = useSwapFee([]) + const swapFee = useSwapFee(route.map((route) => route.pool_id)) const [showSummary, setShowSummary] = useToggle() const { liquidationPrice, isUpdatingLiquidationPrice } = useLiquidationPrice( props.liquidationPrice, diff --git a/src/hooks/useSwapFee.ts b/src/hooks/useSwapFee.ts index 1a377c09..13471f30 100644 --- a/src/hooks/useSwapFee.ts +++ b/src/hooks/useSwapFee.ts @@ -1,22 +1,23 @@ import useSWR from 'swr' -import getSwapFees from 'api/swap/getPools' -import { BN_ZERO } from 'constants/math' +import getOsmosisSwapFee from 'api/swap/getOsmosisSwapFee' import useChainConfig from 'hooks/useChainConfig' - -const STANDARD_SWAP_FEE = 0.002 +import { ChainInfoID } from 'types/enums/wallet' +import { STANDARD_SWAP_FEE } from 'utils/constants' export default function useSwapFee(poolIds: string[]) { const chainConfig = useChainConfig() - const { data: pools } = useSWR( + const { data: swapFee } = useSWR( `chains/${chainConfig.id}/swapFees/${poolIds.join(',')}`, - () => getSwapFees(chainConfig, poolIds), + () => { + if (chainConfig.id === ChainInfoID.Pion1) { + return STANDARD_SWAP_FEE + } + + return getOsmosisSwapFee(chainConfig, poolIds) + }, {}, ) - if (!pools?.length) return STANDARD_SWAP_FEE - - return pools - .reduce((acc, pool) => acc.plus(pool?.pool_params?.swap_fee || STANDARD_SWAP_FEE), BN_ZERO) - .toNumber() + return swapFee ?? STANDARD_SWAP_FEE } diff --git a/src/types/interfaces/swap.d.ts b/src/types/interfaces/swap.d.ts new file mode 100644 index 00000000..0ac53726 --- /dev/null +++ b/src/types/interfaces/swap.d.ts @@ -0,0 +1,29 @@ +export interface AstroportSwapRouteResponse { + amount_in: string + amount_out: string + decimals_in: number + decimals_out: number + denom_in: string + denom_out: string + id: string + price_difference: number + price_impact: number + price_in: number + price_out: number + swaps: Swap[] + value_in: string + value_out: string +} + +export interface AstroportSwapResponse { + contract_addr: string + from: string + illiquid: boolean + to: string + type: SwapPoolType +} + +export enum AstroportSwapPoolType { + XYK = 'xyk', + PCL = 'pcl', +} diff --git a/src/utils/constants.ts b/src/utils/constants.ts index 775be632..1bab6b48 100644 --- a/src/utils/constants.ts +++ b/src/utils/constants.ts @@ -21,3 +21,4 @@ export const DEFAULT_PORTFOLIO_STATS = [ ] export const ENABLE_AUTO_REPAY = true +export const STANDARD_SWAP_FEE = 0.002