This repository was archived by the owner on Jun 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: routing api integration (#2080)
* initial routing api integration * add routing api slice * display route in dialog * addressed pr feedback * switch to `get` * renamed useRouterTradeExactIn to useRouter * moving few files to later iteration * removed unnecessary `as` * switch to polling * add todo for blocknumber freshness * remove account-slippage-deadline
- Loading branch information
Justin Domingue
authored
Jul 21, 2021
1 parent
ea79fbc
commit 4d3ed5d
Showing
5 changed files
with
95 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { skipToken } from '@reduxjs/toolkit/query/react' | ||
import { Currency, CurrencyAmount } from '@uniswap/sdk-core' | ||
import ms from 'ms.macro' | ||
import { useBlockNumber } from 'state/application/hooks' | ||
import { useGetQuoteQuery } from 'state/routing/slice' | ||
import { useActiveWeb3React } from './web3' | ||
|
||
export function useRouterTradeExactIn(amountIn?: CurrencyAmount<Currency>, currencyOut?: Currency) { | ||
const { account } = useActiveWeb3React() | ||
|
||
const blockNumber = useBlockNumber() | ||
|
||
const { isLoading, isError, data } = useGetQuoteQuery( | ||
amountIn && currencyOut && account && blockNumber | ||
? { | ||
tokenInAddress: amountIn.currency.wrapped.address, | ||
tokenInChainId: amountIn.currency.chainId, | ||
tokenOutAddress: currencyOut.wrapped.address, | ||
tokenOutChainId: currencyOut.chainId, | ||
amount: amountIn.quotient.toString(), | ||
type: 'exactIn', | ||
} | ||
: skipToken, | ||
{ pollingInterval: ms`10s` } | ||
) | ||
|
||
// todo(judo): validate block number for freshness | ||
|
||
return !isLoading && !isError ? data?.routeString : undefined | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react' | ||
import { SupportedChainId } from 'constants/chains' | ||
import qs from 'qs' | ||
|
||
export interface GetQuoteResult { | ||
blockNumber: string | ||
gasPriceWei: string | ||
gasUseEstimate: string | ||
gasUseEstimateQuote: string | ||
gasUseEstimateQuoteDecimals: string | ||
gasUseEstimateUSD: string | ||
methodParameters: { calldata: string; value: string } | ||
quote: string | ||
quoteDecimals: string | ||
quoteGasAdjusted: string | ||
quoteGasAdjustedDecimals: string | ||
quoteId: string | ||
routeEdges: { | ||
fee: string | ||
id: string | ||
inId: string | ||
outId: string | ||
percent: number | ||
type: string | ||
}[] | ||
routeNodes: { chainId: number; id: string; symbol: string; type: string }[] | ||
routeString: string | ||
} | ||
|
||
export const routingApi = createApi({ | ||
baseQuery: fetchBaseQuery({ | ||
baseUrl: 'https://api.uniswap.org/v1/', | ||
}), | ||
endpoints: (build) => ({ | ||
getQuote: build.query< | ||
GetQuoteResult, | ||
{ | ||
tokenInAddress: string | ||
tokenInChainId: SupportedChainId | ||
tokenOutAddress: string | ||
tokenOutChainId: SupportedChainId | ||
amount: string | ||
type: 'exactIn' | 'exactOut' | ||
recipient?: string | ||
slippageTolerance?: string | ||
deadline?: string | ||
} | ||
>({ | ||
query: (args) => `quote?${qs.stringify(args)}`, | ||
}), | ||
}), | ||
}) | ||
|
||
export const { useGetQuoteQuery } = routingApi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters