-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(apps/swap): use fiat value from AMM in CurrencyInput
- Loading branch information
1 parent
0e479aa
commit bb849e4
Showing
8 changed files
with
119 additions
and
21 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
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
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
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './useClientTrade' | ||
export * from './useUSDCPrice' |
97 changes: 97 additions & 0 deletions
97
packages/wagmi/src/future/hooks/trade/hooks/useUSDCPrice.ts
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,97 @@ | ||
import { useMemo } from 'react' | ||
import { useFeeData } from 'wagmi' | ||
import { Amount, Price, Type, USDC } from '@sushiswap/currency' | ||
import { ChainId } from '@sushiswap/chain' | ||
import { useQuery } from '@tanstack/react-query' | ||
import { usePoolsCodeMap } from '../../pools' | ||
import { Router } from '@sushiswap/router' | ||
import { ZERO } from '@sushiswap/math' | ||
import { parseUnits } from 'ethers/lib/utils.js' | ||
|
||
export const useUSDCPrice = ({ | ||
currency, | ||
chainId, | ||
enabled = true, | ||
}: { | ||
currency: Type | undefined | ||
chainId: ChainId | undefined | ||
enabled?: boolean | ||
}) => { | ||
const { usdc, usdcIn } = useMemo(() => { | ||
const usdc = | ||
chainId && chainId in USDC | ||
? USDC[chainId as keyof typeof USDC] | ||
: undefined | ||
|
||
return { | ||
usdc, | ||
usdcIn: usdc ? parseUnits('1000', usdc.decimals) : undefined, | ||
} | ||
}, [chainId]) | ||
|
||
const { data: feeData } = useFeeData({ chainId: chainId }) | ||
|
||
const _chainId = chainId || 1 | ||
|
||
const { data: poolsCodeMap } = usePoolsCodeMap({ | ||
chainId: _chainId, | ||
currencyA: usdc, | ||
currencyB: currency, | ||
enabled: Boolean(chainId && usdc && currency), | ||
withBentoPools: true, | ||
}) | ||
|
||
return useQuery({ | ||
queryKey: [ | ||
'useUSDCPrice', | ||
{ | ||
chainId, | ||
currency, | ||
poolsCodeMap, | ||
}, | ||
], | ||
queryFn: async () => { | ||
if ( | ||
!chainId || | ||
!poolsCodeMap || | ||
!usdc || | ||
!usdcIn || | ||
!currency || | ||
!feeData?.gasPrice | ||
) | ||
return null | ||
|
||
if (currency.equals(usdc)) { | ||
const amount = Amount.fromRawAmount(usdc, usdcIn.toString()) | ||
return new Price({ baseAmount: amount, quoteAmount: amount }) | ||
} | ||
|
||
const route = Router.findBestRoute( | ||
poolsCodeMap, | ||
chainId, | ||
usdc, | ||
usdcIn, | ||
currency, | ||
feeData.gasPrice.toNumber(), | ||
) | ||
|
||
if (route) { | ||
const amountOut = Amount.fromRawAmount( | ||
currency, | ||
route.amountOutBN.toString(), | ||
) | ||
const amountIn = Amount.fromRawAmount(usdc, route.amountInBN.toString()) | ||
|
||
return amountOut.greaterThan(ZERO) | ||
? new Price({ baseAmount: amountOut, quoteAmount: amountIn }) | ||
: null | ||
} | ||
|
||
return null | ||
}, | ||
refetchInterval: 10000, | ||
enabled: Boolean( | ||
enabled && chainId && poolsCodeMap && feeData && usdcIn && currency, | ||
), | ||
}) | ||
} |