-
Notifications
You must be signed in to change notification settings - Fork 272
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: SendProvider #2078
base: main
Are you sure you want to change the base?
feat: SendProvider #2078
Changes from 14 commits
b0f51bc
c766c53
bf36e0d
0cc4028
1b97092
59ff71d
27a8976
40855cf
b0a0c74
66e1998
27739d3
ee481d0
67bad8c
2fc35a4
4a91dc4
b49a389
059d701
cfb66ef
6532df7
7e5be49
da4eef7
b58a0bf
36e4557
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,47 +2,62 @@ import { getPriceQuote } from '@/api'; | |
import type { PriceQuoteToken } from '@/api/types'; | ||
import { RequestContext } from '@/core/network/constants'; | ||
import { isApiError } from '@/internal/utils/isApiResponseError'; | ||
import type { Dispatch, SetStateAction } from 'react'; | ||
import { useEffect, useState } from 'react'; | ||
|
||
type UseExchangeRateParams = { | ||
token: PriceQuoteToken; | ||
token: PriceQuoteToken | undefined | ''; | ||
selectedInputType: 'crypto' | 'fiat'; | ||
setExchangeRate: Dispatch<SetStateAction<number>>; | ||
setExchangeRateLoading?: Dispatch<SetStateAction<boolean>>; | ||
}; | ||
|
||
export async function useExchangeRate({ | ||
export function useExchangeRate({ | ||
token, | ||
selectedInputType, | ||
setExchangeRate, | ||
setExchangeRateLoading, | ||
}: UseExchangeRateParams) { | ||
if (!token) { | ||
return; | ||
} | ||
|
||
setExchangeRateLoading?.(true); | ||
|
||
try { | ||
const response = await getPriceQuote( | ||
{ tokens: [token] }, | ||
RequestContext.Wallet, | ||
); | ||
if (isApiError(response)) { | ||
console.error('Error fetching price quote:', response.error); | ||
const [isLoading, setIsLoading] = useState<boolean>(false); | ||
const [exchangeRate, setExchangeRate] = useState<number>(0); | ||
const [error, setError] = useState<string | null>(null); | ||
|
||
useEffect(() => { | ||
if (!token) { | ||
setIsLoading(false); | ||
setExchangeRate(0); | ||
setError(null); | ||
return; | ||
} | ||
const priceQuote = response.priceQuote[0]; | ||
|
||
const rate = | ||
selectedInputType === 'crypto' | ||
? 1 / Number(priceQuote.price) | ||
: Number(priceQuote.price); | ||
|
||
setExchangeRate(rate); | ||
} catch (error) { | ||
console.error('Uncaught error fetching price quote:', error); | ||
} finally { | ||
setExchangeRateLoading?.(false); | ||
} | ||
|
||
setIsLoading(true); | ||
setError(null); | ||
|
||
getPriceQuote({ tokens: [token] }, RequestContext.Wallet) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you could wrap this in a react-query hook, i.e. https://github.com/coinbase/onchainkit/blob/main/src/nft/hooks/useMintDetails.ts and export that along with the API and then just use that hook to calculate exchange rate here or in the consumer There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @alessey, based on this feedback I created There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will this call always be in the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. right now |
||
.then((response) => { | ||
if (isApiError(response)) { | ||
setError(response.error); | ||
setExchangeRate(0); | ||
console.error('Error fetching price quote:', response.error); | ||
return; | ||
} | ||
|
||
const priceQuote = response.priceQuote[0]; | ||
const rate = | ||
selectedInputType === 'crypto' | ||
? 1 / Number(priceQuote.price) | ||
: Number(priceQuote.price); | ||
|
||
setExchangeRate(rate); | ||
setError(null); | ||
}) | ||
.catch((error) => { | ||
setError(String(error)); | ||
console.error('Uncaught error fetching price quote:', error); | ||
}) | ||
.finally(() => { | ||
setIsLoading(false); | ||
}); | ||
}, [token, selectedInputType]); | ||
|
||
return { | ||
isLoading, | ||
exchangeRate, | ||
error, | ||
}; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
token?: PriceQuoteToken | ''? why allow ''?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When I use

useExchangeRate
inSendProvider
, I'm passing in aPortfolioTokenWithFiatValue.address
:PortfolioTokenWithFiatValue
inherits fromToken
type.And Token allows empty string for address:

i suppose I could add a type assertion in SendProvider if that's preferred:

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could update the type to reflect that address: '' is only valid for ETH, but in general, I try not to pass through implementation details from other components.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think i have a better solve. in the provider, i'll test against address, not symbol. this will resolve the type narrowing issue:
token: selectedToken?.address === '' ? 'ETH' : selectedToken?.address,