add hooks to fetch route (#739)
* add hooks to fetch route * add missing routes endpoint for pion1
This commit is contained in:
parent
6bba31f924
commit
254df8cd6e
@ -9,7 +9,7 @@ import Text from 'components/common/Text'
|
||||
import TokenInputWithSlider from 'components/common/TokenInput/TokenInputWithSlider'
|
||||
import { BN_ZERO } from 'constants/math'
|
||||
import useAllAssets from 'hooks/assets/useAllAssets'
|
||||
import useDepositActions from 'hooks/HLS/useDepositActions'
|
||||
import useDepositActions from 'hooks/hls/useDepositActions'
|
||||
import useBorrowAsset from 'hooks/useBorrowAsset'
|
||||
import useCurrentWalletBalance from 'hooks/useCurrentWalletBalance'
|
||||
import useHealthComputer from 'hooks/useHealthComputer'
|
||||
|
@ -2,7 +2,7 @@ import React, { useCallback, useMemo } from 'react'
|
||||
|
||||
import DropDownButton from 'components/common/Button/DropDownButton'
|
||||
import { ArrowDownLine, Cross, HandCoins, Plus, Scale } from 'components/common/Icons'
|
||||
import useCloseHlsStakingPosition from 'hooks/HLS/useClosePositionActions'
|
||||
import useCloseHlsStakingPosition from 'hooks/hls/useClosePositionActions'
|
||||
import useStore from 'store'
|
||||
|
||||
export const MANAGE_META = { id: 'manage' }
|
||||
|
@ -32,6 +32,7 @@ const Pion1: ChainConfig = {
|
||||
pyth: 'neutron15ldst8t80982akgr8w8ekcytejzkmfpgdkeq4xgtge48qs7435jqp87u3t',
|
||||
},
|
||||
endpoints: {
|
||||
routes: 'https://app.astroport.fi/api/routes',
|
||||
rest: 'https://rest-palvus.pion-1.ntrn.tech/',
|
||||
rpc: 'https://rpc-palvus.pion-1.ntrn.tech/',
|
||||
swap: 'https://testnet-neutron.astroport.fi/swap',
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { ChainInfoID } from 'types/enums/wallet'
|
||||
import { NETWORK } from 'types/enums/network'
|
||||
import { ChainInfoID } from 'types/enums/wallet'
|
||||
|
||||
import Osmosis1 from './osmosis-1'
|
||||
|
||||
|
@ -135,6 +135,7 @@ const Osmosis1: ChainConfig = {
|
||||
rest: 'https://osmosis-node.marsprotocol.io/GGSFGSFGFG34/osmosis-lcd-front/',
|
||||
swap: 'https://app.osmosis.zone',
|
||||
explorer: 'https://www.mintscan.io/osmosis/transactions/',
|
||||
routes: 'https://sqs.osmosis.zone/router',
|
||||
pools:
|
||||
(process.env.NEXT_PUBLIC_OSMOSIS_REST ||
|
||||
'https://osmosis-node.marsprotocol.io/GGSFGSFGFG34/osmosis-lcd-front/') +
|
||||
|
66
src/hooks/trade/useRoutes.ts
Normal file
66
src/hooks/trade/useRoutes.ts
Normal file
@ -0,0 +1,66 @@
|
||||
import useSWR from 'swr'
|
||||
|
||||
import useChainConfig from 'hooks/useChainConfig'
|
||||
import { ChainInfoID } from 'types/enums/wallet'
|
||||
import { BN } from 'utils/helpers'
|
||||
|
||||
export default function useRoutes(denomIn: string, denomOut: string, amount: BigNumber) {
|
||||
const chainConfig = useChainConfig()
|
||||
const isOsmosis = [ChainInfoID.Osmosis1, ChainInfoID.OsmosisDevnet].includes(chainConfig.id)
|
||||
|
||||
const osmosisRoute = useSWR<SwapRouteInfo | null>(
|
||||
isOsmosis &&
|
||||
`${chainConfig.endpoints.routes}/quote?tokenIn=${denomIn}&tokenOutDenom=${denomOut}&amount=${amount}`,
|
||||
async (url: string) => {
|
||||
try {
|
||||
const resp = await fetch(url)
|
||||
const route = (await resp.json()) as OsmosisRouteResponse
|
||||
|
||||
return {
|
||||
priceImpact: BN(route.price_impact),
|
||||
fee: BN(route.effective_fee),
|
||||
route: {
|
||||
osmo: {
|
||||
swaps: route.route[0].pools.map((pool) => ({
|
||||
pool_id: pool.id.toString(),
|
||||
to: pool.token_out_denom,
|
||||
})),
|
||||
},
|
||||
},
|
||||
}
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
const astroportRoute = useSWR<SwapRouteInfo | null>(
|
||||
!isOsmosis &&
|
||||
`${chainConfig.endpoints.routes}?start=${denomIn}&end=${denomOut}&amount=${amount}&chainId=${chainConfig.id}&limit=1`,
|
||||
async (url: string) => {
|
||||
try {
|
||||
const resp = await fetch(url)
|
||||
const route = (await resp.json())[0] as AstroportRouteResponse
|
||||
|
||||
return {
|
||||
priceImpact: BN(route.price_impact),
|
||||
fee: BN(0), // TODO: Fees are not implemented yet on Astroport endpoint
|
||||
route: {
|
||||
astro: {
|
||||
swaps: route.swaps.map((swap) => ({
|
||||
from: swap.from,
|
||||
to: swap.to,
|
||||
})),
|
||||
},
|
||||
},
|
||||
}
|
||||
} catch (e) {
|
||||
return null
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
if (isOsmosis) return osmosisRoute
|
||||
|
||||
return astroportRoute
|
||||
}
|
1
src/types/interfaces/chain.d.ts
vendored
1
src/types/interfaces/chain.d.ts
vendored
@ -29,6 +29,7 @@ interface ChainConfig {
|
||||
swap: string
|
||||
explorer: string
|
||||
pools: string
|
||||
routes: string
|
||||
aprs: {
|
||||
vaults: string
|
||||
stride: string
|
||||
|
76
src/types/interfaces/route.d.ts
vendored
76
src/types/interfaces/route.d.ts
vendored
@ -11,3 +11,79 @@ type Page =
|
||||
| 'hls-staking'
|
||||
| 'governance'
|
||||
| 'execute'
|
||||
|
||||
type OsmosisRouteResponse = {
|
||||
amount_in: {
|
||||
denom: string
|
||||
amount: string
|
||||
}
|
||||
amount_out: string
|
||||
route: OsmosisRoute[]
|
||||
effective_fee: string
|
||||
price_impact: string
|
||||
}
|
||||
|
||||
type OsmosisRoute = {
|
||||
pools: OsmosisRoutePool[]
|
||||
'has-cw-pool': boolean
|
||||
out_amount: string
|
||||
in_amount: string
|
||||
}
|
||||
|
||||
type OsmosisRoutePool = {
|
||||
id: number
|
||||
type: number
|
||||
balances: []
|
||||
spread_factor: string
|
||||
token_out_denom: string
|
||||
taker_fee: string
|
||||
}
|
||||
|
||||
type SwapRouteInfo = {
|
||||
priceImpact: BigNumber
|
||||
fee: BigNumber
|
||||
route: OsmosisSwap | AstroportSwap
|
||||
}
|
||||
|
||||
type OsmosisSwap = {
|
||||
osmo: {
|
||||
swaps: {
|
||||
pool_id: string
|
||||
to: string
|
||||
}[]
|
||||
}
|
||||
}
|
||||
|
||||
type AstroportSwap = {
|
||||
astro: {
|
||||
swaps: {
|
||||
from: string
|
||||
to: string
|
||||
}[]
|
||||
}
|
||||
}
|
||||
|
||||
type AstroportRouteResponse = {
|
||||
id: string
|
||||
swaps: AstroportRoute[]
|
||||
denom_in: string
|
||||
decimals_in: number
|
||||
price_in: number
|
||||
value_in: string
|
||||
amount_in: string
|
||||
denom_out: string
|
||||
decimals_out: number
|
||||
price_out: number
|
||||
value_out: string
|
||||
amount_out: string
|
||||
price_difference: number
|
||||
price_impact: number
|
||||
}
|
||||
|
||||
type AstroportRoute = {
|
||||
contract_addr: string
|
||||
from: string
|
||||
to: string
|
||||
type: string
|
||||
illiquid: boolean
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user