fix swap fee for osmosis (#725)

This commit is contained in:
Bob van der Helm 2024-01-11 15:03:02 +01:00 committed by GitHub
parent 0960f84b58
commit ebe05b12fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 55 additions and 15 deletions

View File

@ -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<Pool[]> {
): Promise<number> {
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 {

View File

@ -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,

View File

@ -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
}

29
src/types/interfaces/swap.d.ts vendored Normal file
View File

@ -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',
}

View File

@ -21,3 +21,4 @@ export const DEFAULT_PORTFOLIO_STATS = [
]
export const ENABLE_AUTO_REPAY = true
export const STANDARD_SWAP_FEE = 0.002