|
| 1 | +import { Trans } from '@lingui/macro'; |
| 2 | +import { Box, Button, CircularProgress, Typography } from '@mui/material'; |
| 3 | +import { useState } from 'react'; |
| 4 | +import { ComputedReserveData } from 'src/hooks/app-data-provider/useAppDataProvider'; |
| 5 | +import { useWeb3Context } from 'src/libs/hooks/useWeb3Context'; |
| 6 | +import { selectCurrentReserves } from 'src/store/poolSelectors'; |
| 7 | +import { useRootStore } from 'src/store/root'; |
| 8 | + |
| 9 | +import { TxSuccessView } from '../FlowCommons/Success'; |
| 10 | +import { DetailsNumberLine } from '../FlowCommons/TxModalDetails'; |
| 11 | +import Turnstile from './Turnstile'; |
| 12 | +import { getNormalizedMintAmount } from './utils'; |
| 13 | + |
| 14 | +export const CaptchaFaucetModalContent = ({ underlyingAsset }: { underlyingAsset: string }) => { |
| 15 | + const { readOnlyModeAddress } = useWeb3Context(); |
| 16 | + const { account, currentMarket, currentMarketData } = useRootStore(); |
| 17 | + const reserves = useRootStore((state) => selectCurrentReserves(state)); |
| 18 | + |
| 19 | + const [captchaToken, setCaptchaToken] = useState<string>(''); |
| 20 | + const [loading, setLoading] = useState<boolean>(false); |
| 21 | + const [captchaLoading, setCaptchaLoading] = useState<boolean>(true); |
| 22 | + const [txHash, setTxHash] = useState<string>(''); |
| 23 | + const [error, setError] = useState<string>(''); |
| 24 | + |
| 25 | + const faucetUrl = `${process.env.NEXT_PUBLIC_API_BASEURL}/faucet`; |
| 26 | + const siteKey = process.env.NEXT_PUBLIC_TURNSTILE_SITE_KEY as string; |
| 27 | + |
| 28 | + const poolReserve = reserves.find( |
| 29 | + (reserve) => reserve.underlyingAsset === underlyingAsset |
| 30 | + ) as ComputedReserveData; |
| 31 | + |
| 32 | + const normalizedAmount = getNormalizedMintAmount(poolReserve.symbol, poolReserve.decimals); |
| 33 | + |
| 34 | + const captchaVerify = (token: string) => { |
| 35 | + setCaptchaToken(token); |
| 36 | + setCaptchaLoading(false); |
| 37 | + }; |
| 38 | + |
| 39 | + const faucet = async () => { |
| 40 | + try { |
| 41 | + setTxHash(''); |
| 42 | + setLoading(true); |
| 43 | + setError(''); |
| 44 | + const response = await fetch(faucetUrl, { |
| 45 | + method: 'POST', |
| 46 | + headers: { |
| 47 | + Accept: 'application/json', |
| 48 | + 'Content-Type': 'application/json', |
| 49 | + }, |
| 50 | + body: JSON.stringify({ |
| 51 | + address: account, |
| 52 | + captchaToken, |
| 53 | + market: currentMarket, |
| 54 | + tokenAddress: poolReserve.underlyingAsset, |
| 55 | + tokenSymbol: poolReserve.symbol, |
| 56 | + faucetAddress: currentMarketData.addresses.FAUCET, |
| 57 | + }), |
| 58 | + }); |
| 59 | + const data = await response.json(); |
| 60 | + if (!response.ok) { |
| 61 | + throw new Error(data.msg); |
| 62 | + } |
| 63 | + setTxHash(data.msg); |
| 64 | + } catch (e: unknown) { |
| 65 | + if (e instanceof Error && e.message) { |
| 66 | + setError(e.message); |
| 67 | + } else { |
| 68 | + setError('An error occurred trying to send the transaction'); |
| 69 | + } |
| 70 | + } finally { |
| 71 | + setLoading(false); |
| 72 | + } |
| 73 | + }; |
| 74 | + |
| 75 | + if (txHash) { |
| 76 | + return ( |
| 77 | + <TxSuccessView |
| 78 | + txHash={txHash} |
| 79 | + action={<Trans>will receive</Trans>} |
| 80 | + symbol={poolReserve.symbol} |
| 81 | + amount={normalizedAmount} |
| 82 | + /> |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + return ( |
| 87 | + <> |
| 88 | + <Turnstile sitekey={siteKey} onVerify={captchaVerify} autoResetOnExpire /> |
| 89 | + <Typography variant="h2" sx={{ mb: 6 }}> |
| 90 | + <Trans>Faucet</Trans> {poolReserve.symbol} |
| 91 | + </Typography> |
| 92 | + <Box |
| 93 | + sx={(theme) => ({ |
| 94 | + p: 3, |
| 95 | + border: `1px solid ${theme.palette.divider}`, |
| 96 | + borderRadius: '4px', |
| 97 | + '.MuiBox-root:last-of-type': { |
| 98 | + mb: 0, |
| 99 | + }, |
| 100 | + })} |
| 101 | + > |
| 102 | + <DetailsNumberLine |
| 103 | + description={<Trans>Amount</Trans>} |
| 104 | + iconSymbol={poolReserve.symbol} |
| 105 | + symbol={poolReserve.symbol} |
| 106 | + value={normalizedAmount} |
| 107 | + /> |
| 108 | + </Box> |
| 109 | + <Typography variant="helperText" color="error.main"> |
| 110 | + {error} |
| 111 | + </Typography> |
| 112 | + <Box sx={{ display: 'flex', flexDirection: 'column', mt: 12 }}> |
| 113 | + <Button |
| 114 | + variant="contained" |
| 115 | + disabled={loading || !captchaToken || readOnlyModeAddress !== undefined} |
| 116 | + onClick={faucet} |
| 117 | + size="large" |
| 118 | + sx={{ minHeight: '44px' }} |
| 119 | + > |
| 120 | + {(loading || captchaLoading) && ( |
| 121 | + <CircularProgress color="inherit" size="16px" sx={{ mr: 2 }} /> |
| 122 | + )} |
| 123 | + {<Trans>Faucet {poolReserve.symbol}</Trans>} |
| 124 | + </Button> |
| 125 | + </Box> |
| 126 | + </> |
| 127 | + ); |
| 128 | +}; |
0 commit comments