Skip to content

Commit

Permalink
feat: add more info to faucet page
Browse files Browse the repository at this point in the history
  • Loading branch information
akhlopiachyi committed Apr 18, 2024
1 parent e092aa9 commit 7866619
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 10 deletions.
121 changes: 112 additions & 9 deletions components/Faucets/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Input } from "../Input";
import { DirectSecp256k1HdWallet } from '@cosmjs/proto-signing';
import { stringToPath } from '@cosmjs/crypto';
import axios from "axios";
import { Spinner } from "../Spinner";

const tabs = [
{ label: 'Mainnet', id: 'mainnet' },
Expand Down Expand Up @@ -34,7 +35,10 @@ const CONFIG = {
export const Faucets = () => {
const [currentTab, setCurrentTab] = useState<TabItem>(tabs[0]);
const [walletAddress, setWalletAddress] = useState<string>('');
const [mnemonic, setMnemonic] = useState<string>('');
const [txHash, setTxHash] = useState<string>('');
const [fundLoading, setFundLoading] = useState<boolean>(false);
const [errorMessage, setErrorMessage] = useState<string>('');

const currentConfig = useMemo(() => {
return CONFIG[currentTab.id as ('testnet' | 'devnet')] || {};
Expand All @@ -48,14 +52,18 @@ export const Faucets = () => {

setTxHash(response.data.txHash);
} catch (error) {
setErrorMessage((error as any).response.data.content[0].kind);

console.error(error);
}
}, [currentConfig?.fundWalletURL]);

const handleRequestFunds = useCallback(async () => {
setFundLoading(true);
if (walletAddress.length) {
await fundWallet(walletAddress);
}
setFundLoading(false);
}, [fundWallet, walletAddress]);

const handleGenerateWallet = useCallback(async () => {
Expand All @@ -67,18 +75,117 @@ export const Faucets = () => {
const [{ address }] = await wallet.getAccounts();

setWalletAddress(address);
setMnemonic(wallet.mnemonic);

setFundLoading(true);
await fundWallet(address);
setFundLoading(false);
}, [currentConfig.coreumHDPath, currentConfig.tokenPrefix, fundWallet]);

const handleChangeWalletAddress = useCallback((value: string | number) => {
setWalletAddress(typeof value === 'string' ? value : String(value));

setTxHash('');
setMnemonic('');
setErrorMessage('');
setFundLoading(false);
}, []);

useEffect(() => {
setWalletAddress('');
setTxHash('');
setMnemonic('');
setErrorMessage('');
setFundLoading(false);
}, [currentTab.id]);

const getErrorMessage = useCallback((err: string) => {
switch (err) {
case 'server.rate_limit':
return (
<span>Error: The Rate Limit has been surpassed, please, try again in several hours. If you need more funds for specific testing purposes, please request them in the <Link href="https://discord.com/invite/VgkhYeWmTd" target="_blank" className="text-[#25D695] font-medium">Discord</Link></span>
);
case 'server.internal_error':
return 'Message: unable to transfer tokens';
default:
return err;
}
}, []);

const renderErrorOrTxHash = useMemo(() => {
if (errorMessage) {
return (
<span className="text-[#eee] text-base font-light">{getErrorMessage(errorMessage)}</span>
);
}

return (
<Link
target="_blank"
className="text-[#25D695] text-base"
href={`${currentConfig?.explorerLink}/${txHash}`}
>
{txHash}
</Link>
);
}, [currentConfig?.explorerLink, errorMessage, getErrorMessage, txHash]);

const renderInfo = useMemo(() => {
if (!fundLoading && !(txHash.length || errorMessage.length)) {
return null;
}

if (fundLoading) {
return (
<div className="flex flex-col w-full gap-2 bg-[#0E0F10] py-4 px-6 rounded-xl text-[#5E6773] text-sm text-nowrap overflow-auto">
<div className="flex flex-col sm:flex-row sm:items-baseline w-full gap-2 text-wrap break-words">
<span className="text-nowrap min-w-[120px] w-[120px]">Tx Hash:</span>
<Spinner />
</div>
<div className="flex flex-col sm:flex-row sm:items-baseline w-full gap-2 text-wrap break-words">
<span className="text-nowrap min-w-[120px] w-[120px]">Wallet Address:</span>
<span className="text-[#eee] text-base font-light">{walletAddress}</span>
</div>
{mnemonic.length ? (
<>
<div className="flex flex-col sm:flex-row sm:items-baseline w-full gap-2 text-wrap break-words">
<span className="text-nowrap min-w-[120px] w-[120px]">Wallet Mnemonic:</span>
<span className="flex text-[#eee] text-base font-light text-wrap">{mnemonic}</span>
</div>
<div className="flex flex-col sm:flex-row sm:items-center w-full gap-2 text-wrap break-words text-[#eee] text-base">
⚠️ Mnemonic is the only way to restore you wallet, save it in a safe place.
</div>
</>
) : null}
</div>
);
}

return (
<div className="flex flex-col w-full gap-4 bg-[#0E0F10] py-4 px-6 rounded-xl text-[#5E6773] text-sm text-nowrap overflow-hidden">
<div className="flex flex-col sm:flex-row sm:items-baseline w-full gap-2 text-wrap break-words">
<span className="text-nowrap min-w-[120px] w-[120px]">Tx Hash:</span>
{renderErrorOrTxHash}
</div>
<div className="flex flex-col sm:flex-row sm:items-baseline w-full gap-2 text-wrap break-words">
<span className="text-nowrap min-w-[120px] w-[120px]">Wallet Address:</span>
<span className="text-[#eee] text-base font-light">{walletAddress}</span>
</div>
{mnemonic.length ? (
<>
<div className="flex flex-col sm:flex-row sm:items-baseline w-full gap-2 text-wrap break-words">
<span className="text-nowrap min-w-[120px] w-[120px]">Wallet Mnemonic:</span>
<span className="flex text-[#eee] text-base font-light text-wrap">{mnemonic}</span>
</div>
<div className="flex flex-col sm:flex-row sm:items-center w-full gap-2 text-wrap break-words text-[#eee] text-base">
⚠️ Mnemonic is the only way to restore you wallet, save it in a safe place.
</div>
</>
) : null}
</div>
);
}, [errorMessage.length, fundLoading, mnemonic, renderErrorOrTxHash, txHash.length, walletAddress]);

const renderContent = useMemo(() => {
switch (currentTab.id) {
case 'mainnet':
Expand All @@ -96,17 +203,17 @@ export const Faucets = () => {
case 'devnet':
return (
<div className="flex flex-col w-full gap-4">
<div className="flex w-full items-center gap-6">
<div className="flex flex-col md:flex-row w-full items-center gap-6">
<div
className="flex overflow-hidden py-4 px-6 bg-[#0E0F10] text-base font-medium font-['space grotesk'] rounded-xl w-[280px] cursor-pointer"
className="flex w-full md:w-[280px] overflow-hidden py-4 px-6 bg-[#0E0F10] text-base font-medium font-['space grotesk'] rounded-xl cursor-pointer"
onClick={handleGenerateWallet}
>
Generate Funded Wallet
</div>
<div className="flex-none text-[#9FA2AC] text-base font-['space grotesk'] font-normal">
Or
</div>
<div className="flex-1">
<div className="w-full md:w-auto flex-1">
<Input
buttonLabel="Request Fund"
onButtonClick={handleRequestFunds}
Expand All @@ -119,16 +226,12 @@ export const Faucets = () => {
/>
</div>
</div>
{txHash.length ? (
<div className="flex w-full items-center gap-2 bg-[#0E0F10] py-4 px-6 rounded-xl text-[#5E6773] text-sm text-nowrap overflow-auto">
Tx Hash: <Link target="_blank" className="text-[#25D695] text-base" href={`${currentConfig.explorerLink}/${txHash}`}>{txHash}</Link>
</div>
) : ''}
{renderInfo}
</div>
);
default:
}
}, [currentConfig?.explorerLink, currentTab.id, handleChangeWalletAddress, handleGenerateWallet, handleRequestFunds, txHash, walletAddress]);
}, [currentTab.id, handleChangeWalletAddress, handleGenerateWallet, handleRequestFunds, renderInfo, walletAddress]);

return (
<div className="flex flex-col w-full gap-10 overflow-hidden">
Expand Down
1 change: 0 additions & 1 deletion components/Layout/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import { Footer } from "../Footer";
import { Navbar } from "../Navbar";
import { motion, AnimatePresence } from "framer-motion";

interface LayoutProps {
children: React.ReactNode;
Expand Down
11 changes: 11 additions & 0 deletions components/Spinner/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const Spinner = () => {
return (
<div role="status">
<svg aria-hidden="true" className="w-4 h-4 animate-spin dark:text-[#25D695] fill-black" viewBox="0 0 100 101" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z" fill="currentColor"/>
<path d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z" fill="currentFill"/>
</svg>
<span className="sr-only">Loading...</span>
</div>
);
};

0 comments on commit 7866619

Please sign in to comment.