UNPKG

@daimo/pay

Version:

Seamless crypto payments. Onboard users from any chain, any coin into your app with one click.

98 lines (95 loc) 3.05 kB
import { supportedChains } from '@daimo/pay-common'; import { useState, useMemo, useEffect } from 'react'; function useWalletPaymentOptions({ trpc, address, usdRequired, destChainId, destAddress, preferredChains, preferredTokens, evmChains, passthroughTokens, isDepositFlow, log }) { const [options, setOptions] = useState(null); const [isLoading, setIsLoading] = useState(false); const memoizedPreferredChains = useMemo( () => preferredChains, // eslint-disable-next-line react-hooks/exhaustive-deps [JSON.stringify(preferredChains)] ); const memoizedPreferredTokens = useMemo( () => preferredTokens, // eslint-disable-next-line react-hooks/exhaustive-deps [JSON.stringify(preferredTokens)] ); const memoizedEvmChains = useMemo( () => evmChains, // eslint-disable-next-line react-hooks/exhaustive-deps [JSON.stringify(evmChains)] ); useEffect(() => { const refreshWalletPaymentOptions = async () => { if (address == null || usdRequired == null || destChainId == null || destAddress == null) { return; } setOptions(null); setIsLoading(true); try { let newOptions = await trpc.getWalletPaymentOptions.query({ payerAddress: address, // API expects undefined for deposit flow. usdRequired: isDepositFlow ? void 0 : usdRequired, destChainId, preferredChains: memoizedPreferredChains, preferredTokens: memoizedPreferredTokens, evmChains: memoizedEvmChains }); addPassthroughTokens(newOptions, passthroughTokens, destAddress); const isSupported = (o) => supportedChains.some((c) => c.chainId === o.balance.token.chainId); const filteredOptions = newOptions.filter(isSupported); if (filteredOptions.length < newOptions.length) { log( `[WALLET]: skipping ${newOptions.length - filteredOptions.length} unsupported-chain balances on ${address}` ); } setOptions(filteredOptions); } catch (error) { console.error(error); } finally { setIsLoading(false); } }; if (address != null && usdRequired != null && destChainId != null) { refreshWalletPaymentOptions(); } }, [ address, usdRequired, destChainId, isDepositFlow, memoizedPreferredChains, memoizedPreferredTokens, memoizedEvmChains ]); return { options, isLoading }; } function addPassthroughTokens(options, passthroughTokens, passthroughAddress) { if (passthroughTokens == null) return; for (const option of options) { const tok = option.balance.token; if (option.disabledReason != null) continue; const found = passthroughTokens.find( (t) => t.address === tok.token && t.chain == tok.chainId ); if (found == null) continue; option.passthroughAddress = passthroughAddress; } } export { addPassthroughTokens, useWalletPaymentOptions }; //# sourceMappingURL=useWalletPaymentOptions.js.map