-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathgetAstroportRouteInfo.ts
More file actions
39 lines (36 loc) · 1.18 KB
/
getAstroportRouteInfo.ts
File metadata and controls
39 lines (36 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { FETCH_TIMEOUT } from 'constants/query'
import { byDenom } from 'utils/array'
import { fetchWithTimeout } from 'utils/fetch'
import { BN } from 'utils/helpers'
export default async function getRouteInfo(
denomIn: string,
denomOut: string,
amount: BigNumber,
assets: Asset[],
chainConfig: ChainConfig,
): Promise<SwapRouteInfo | null> {
const astroportUrl = `${chainConfig.endpoints.routes}?start=${denomIn}&end=${denomOut}&amount=${amount.toString()}&chainId=${chainConfig.id}&limit=1`
try {
const resp = await fetchWithTimeout(astroportUrl, FETCH_TIMEOUT)
const route = (await resp.json())[0] as AstroportRouteResponse
return {
amountOut: BN(route.amount_out),
priceImpact: BN(route.price_impact),
fee: BN(0), // TODO: Fees are not implemented yet on Astroport endpoint
description: [
assets.find(byDenom(denomIn))?.symbol,
...route.swaps.map((swap) => assets.find(byDenom(swap.to))?.symbol),
].join(' -> '),
route: {
astro: {
swaps: route.swaps.map((swap) => ({
from: swap.from,
to: swap.to,
})),
},
},
}
} catch (e) {
return null
}
}