-
Notifications
You must be signed in to change notification settings - Fork 273
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9b8fe0f
commit 09b5880
Showing
7 changed files
with
160 additions
and
75 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { getSwapQuote } from '@/api'; | ||
import { isApiError } from '@/core/utils/isApiResponseError'; | ||
import type { Token } from '@/token'; | ||
import { usdcToken } from '@/token/constants'; | ||
import type { Dispatch, SetStateAction } from 'react'; | ||
|
||
type UseExchangeRateParams = { | ||
token: Token; | ||
selectedInputType: 'crypto' | 'fiat'; | ||
setExchangeRate: Dispatch<SetStateAction<number>>; | ||
setExchangeRateLoading: Dispatch<SetStateAction<boolean>>; | ||
}; | ||
|
||
export async function useExchangeRate({ | ||
token, | ||
selectedInputType, | ||
setExchangeRate, | ||
setExchangeRateLoading, | ||
}: UseExchangeRateParams) { | ||
console.log('useing exchange rate hook'); | ||
if (!token) { | ||
return; | ||
} | ||
|
||
if (token.address === usdcToken.address) { | ||
setExchangeRate(1); | ||
return; | ||
} | ||
|
||
setExchangeRateLoading(true); | ||
|
||
const fromToken = selectedInputType === 'crypto' ? token : usdcToken; | ||
const toToken = selectedInputType === 'crypto' ? usdcToken : token; | ||
|
||
try { | ||
const response = await getSwapQuote({ | ||
amount: '1', // hardcoded amount because we only need the exchange rate | ||
from: fromToken, | ||
to: toToken, | ||
useAggregator: false, | ||
}); | ||
if (isApiError(response)) { | ||
console.error('Error fetching exchange rate:', response.error); | ||
return; | ||
} | ||
const rate = | ||
selectedInputType === 'crypto' | ||
? 1 / Number(response.fromAmountUSD) | ||
: Number(response.toAmount) / 10 ** response.to.decimals; | ||
setExchangeRate(rate); | ||
} catch (error) { | ||
console.error('Uncaught error fetching exchange rate:', error); | ||
} finally { | ||
setExchangeRateLoading(false); | ||
} | ||
} |
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
25 changes: 13 additions & 12 deletions
25
src/wallet/components/WalletAdvancedSend/components/AddressSelector.tsx
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