Skip to content

Commit

Permalink
Update: Major Changes
Browse files Browse the repository at this point in the history
- ConnectKit
+ RainbowKit
+ ButtonGuards (Balance, Approve, Connect)
+ ClientOnly items
+ Navigation
+ Footer w/ Socials
  • Loading branch information
l3wi committed Apr 17, 2023
1 parent b695a98 commit 2ff69ce
Show file tree
Hide file tree
Showing 13 changed files with 1,154 additions and 964 deletions.
72 changes: 72 additions & 0 deletions components/guards/ApproveGuard.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { BigNumber, constants, ethers, utils } from "ethers";
import React, { useState } from "react";
import {
erc20ABI,
useAccount,
useContractRead,
useContractWrite,
usePrepareContractWrite
} from "wagmi";

type Props = {
token: `0x${string}`;
spender: `0x${string}`;
amount: BigNumber;
children?: React.ReactNode;
};

export default function ApproveGuard(props: Props) {
const { token, spender, amount } = props;
const { address } = useAccount();

const { data } = useContractRead({
address: token,
abi: erc20ABI,
functionName: "allowance",
args: [address || constants.AddressZero, spender],
watch: true
});

const approved = data ? data.gte(amount) : false;
return (
<div>
{approved || !token || !address ? (
props.children
) : (
<ApproveButton token={token} spender={spender} />
)}
</div>
);
}

const ApproveButton = ({
token,
spender
}: {
token: `0x${string}`;
spender: `0x${string}`;
}) => {
const { config } = usePrepareContractWrite({
address: token,
abi: erc20ABI,
functionName: "approve",
args: [spender, ethers.constants.MaxUint256]
});

const { write } = useContractWrite({
...config,
onSettled(data, error) {
if (error) console.log("Failed", error);
if (data) console.log("Success", data);
}
});

return (
<button
onClick={() => write && write()}
className="w-full py-3 mb-4 font-bold text-white bg-indigo-600 rounded hover:bg-indigo-500"
>
Approve Token
</button>
);
};
32 changes: 32 additions & 0 deletions components/guards/BalanceGuard.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { BigNumber, constants, ethers, utils } from "ethers";
import React, { useState } from "react";
import { useAccount, useBalance } from "wagmi";

type Props = {
token: `0x${string}`;
amount: BigNumber;
children?: React.ReactNode;
};

export default function BalanceGuard(props: Props) {
const { token, amount } = props;
const { address } = useAccount();
const { data } = useBalance({ token, address });

const enoughTokens = data ? data.value.gte(amount) : true;

return (
<div>
{enoughTokens && (!token || !address || data?.value.isZero) ? (
props.children
) : (
<button
disabled
className="w-full py-3 mb-4 font-bold text-white bg-indigo-600 rounded hover:bg-indigo-500"
>
Not Enough Balance
</button>
)}
</div>
);
}
26 changes: 26 additions & 0 deletions components/guards/ConnectGuard.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { useConnectModal } from "@rainbow-me/rainbowkit";
import React from "react";
import { useAccount } from "wagmi";

type Props = {
children?: React.ReactNode;
};

export default function ConnectGuard(props: Props) {
const { address, isConnected } = useAccount();
return <div>{isConnected ? props.children : <ConnectButton />}</div>;
}

const ConnectButton: React.FC<{ smol?: boolean }> = ({ smol }) => {
const { openConnectModal } = useConnectModal();
return (
<>
<button
onClick={() => openConnectModal && openConnectModal()}
className="w-full py-3 mb-4 font-bold text-white bg-indigo-600 rounded hover:bg-indigo-500"
>
Connect Wallet
</button>
</>
);
};
101 changes: 101 additions & 0 deletions components/layout/footer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { useNetwork } from "wagmi";
import ClientOnly from "../../utils/clientOnly";

const socials = {
discord: "https://discord.com/invite/vPws9Vp",
twitter: "https://twitter.com/dsumoney",
github: "https://github.com/emptysetsquad/emptyset",
};

export default () => {
const { chain } = useNetwork();
return (
<footer className="container flex flex-col items-center py-8 mx-auto sm:flex-row">
<span className="inline-flex justify-center mt-4 space-x-5 sm:ml-auto sm:mt-0 sm:justify-start">
{socials.discord && (
<a
href={socials.discord}
target={"_blank"}
rel={"noopener"}
className="text-gray-400 hover:text-gray-500"
>
<span className="sr-only">Facebook</span>
<svg
width="24px"
height="24px"
fill="currentColor"
viewBox="0 -28.5 256 256"
preserveAspectRatio="xMidYMid"
>
<g>
<path
d="M216.856339,16.5966031 C200.285002,8.84328665 182.566144,3.2084988 164.041564,0 C161.766523,4.11318106 159.108624,9.64549908 157.276099,14.0464379 C137.583995,11.0849896 118.072967,11.0849896 98.7430163,14.0464379 C96.9108417,9.64549908 94.1925838,4.11318106 91.8971895,0 C73.3526068,3.2084988 55.6133949,8.86399117 39.0420583,16.6376612 C5.61752293,67.146514 -3.4433191,116.400813 1.08711069,164.955721 C23.2560196,181.510915 44.7403634,191.567697 65.8621325,198.148576 C71.0772151,190.971126 75.7283628,183.341335 79.7352139,175.300261 C72.104019,172.400575 64.7949724,168.822202 57.8887866,164.667963 C59.7209612,163.310589 61.5131304,161.891452 63.2445898,160.431257 C105.36741,180.133187 151.134928,180.133187 192.754523,160.431257 C194.506336,161.891452 196.298154,163.310589 198.110326,164.667963 C191.183787,168.842556 183.854737,172.420929 176.223542,175.320965 C180.230393,183.341335 184.861538,190.991831 190.096624,198.16893 C211.238746,191.588051 232.743023,181.531619 254.911949,164.955721 C260.227747,108.668201 245.831087,59.8662432 216.856339,16.5966031 Z M85.4738752,135.09489 C72.8290281,135.09489 62.4592217,123.290155 62.4592217,108.914901 C62.4592217,94.5396472 72.607595,82.7145587 85.4738752,82.7145587 C98.3405064,82.7145587 108.709962,94.5189427 108.488529,108.914901 C108.508531,123.290155 98.3405064,135.09489 85.4738752,135.09489 Z M170.525237,135.09489 C157.88039,135.09489 147.510584,123.290155 147.510584,108.914901 C147.510584,94.5396472 157.658606,82.7145587 170.525237,82.7145587 C183.391518,82.7145587 193.761324,94.5189427 193.539891,108.914901 C193.539891,123.290155 183.391518,135.09489 170.525237,135.09489 Z"
fillRule="nonzero"
></path>
</g>
</svg>
</a>
)}

{socials.twitter && (
<a
href={socials.twitter}
target={"_blank"}
rel={"noopener"}
className="text-gray-400 hover:text-gray-500"
>
<span className="sr-only">Twitter</span>
<svg
className="w-6 h-6"
fill="currentColor"
viewBox="0 0 24 24"
aria-hidden="true"
>
<path d="M8.29 20.251c7.547 0 11.675-6.253 11.675-11.675 0-.178 0-.355-.012-.53A8.348 8.348 0 0022 5.92a8.19 8.19 0 01-2.357.646 4.118 4.118 0 001.804-2.27 8.224 8.224 0 01-2.605.996 4.107 4.107 0 00-6.993 3.743 11.65 11.65 0 01-8.457-4.287 4.106 4.106 0 001.27 5.477A4.072 4.072 0 012.8 9.713v.052a4.105 4.105 0 003.292 4.022 4.095 4.095 0 01-1.853.07 4.108 4.108 0 003.834 2.85A8.233 8.233 0 012 18.407a11.616 11.616 0 006.29 1.84"></path>
</svg>
</a>
)}

{socials.github && (
<a
href={socials.github}
target={"_blank"}
rel={"noopener"}
className="text-gray-400 hover:text-gray-500"
>
<span className="sr-only">GitHub</span>
<svg
className="w-6 h-6"
fill="currentColor"
viewBox="0 0 24 24"
aria-hidden="true"
>
<path
fillRule="evenodd"
d="M12 2C6.477 2 2 6.484 2 12.017c0 4.425 2.865 8.18 6.839 9.504.5.092.682-.217.682-.483 0-.237-.008-.868-.013-1.703-2.782.605-3.369-1.343-3.369-1.343-.454-1.158-1.11-1.466-1.11-1.466-.908-.62.069-.608.069-.608 1.003.07 1.531 1.032 1.531 1.032.892 1.53 2.341 1.088 2.91.832.092-.647.35-1.088.636-1.338-2.22-.253-4.555-1.113-4.555-4.951 0-1.093.39-1.988 1.029-2.688-.103-.253-.446-1.272.098-2.65 0 0 .84-.27 2.75 1.026A9.564 9.564 0 0112 6.844c.85.004 1.705.115 2.504.337 1.909-1.296 2.747-1.027 2.747-1.027.546 1.379.202 2.398.1 2.651.64.7 1.028 1.595 1.028 2.688 0 3.848-2.339 4.695-4.566 4.943.359.309.678.92.678 1.855 0 1.338-.012 2.419-.012 2.747 0 .268.18.58.688.482A10.019 10.019 0 0022 12.017C22 6.484 17.522 2 12 2z"
clipRule="evenodd"
></path>
</svg>
</a>
)}
</span>

<ClientOnly>
<div className="py-2 text-xs font-medium text-gray-300 flex w-full items-center justify-center md:justify-end">
<svg className="blur-[1px]" height="20" width="20">
<circle cx="10" cy="10" r="5" fill={chain ? "green" : "red"} />
</svg>{" "}
{chain ? (
<>
{" "}
Connected to{" "}
<span className="ml-1 text-indigo-400 ">{chain?.name}</span>
</>
) : (
"Disconnected"
)}
</div>
</ClientOnly>
</footer>
);
};
73 changes: 73 additions & 0 deletions components/layout/header.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { ConnectButton } from "@rainbow-me/rainbowkit";
import Link from "next/link";
import { useRouter } from "next/router";
import ClientOnly from "../../utils/clientOnly";

export default () => {
const router = useRouter();
return (
<section className="w-full text-gray-700 bg-white tails-selected-element">
<div className="flex flex-col flex-wrap items-center justify-between py-6 mx-auto md:flex-row max-w-7xl">
<div className="relative flex flex-row justify-between w-full md:w-max md:justify-center mb-4 md:mb-0">
<Link
href="/"
className="flex items-center font-medium text-gray-900 lg:w-auto lg:items-center lg:justify-center md:mb-0"
>
<h1 className="text-4xl font-bold">starter.</h1>
</Link>
</div>
<span className="md:hidden pb-4">
<ConnectButton
chainStatus={{ smallScreen: "full", largeScreen: "icon" }}
/>
</span>
<div className="inline-flex flex-col items-center sm:flex-row sm:ml-12 lg:justify-end">
<nav className="flex flex-wrap items-center space-x-4 text-xs font-semibold tracking-wide uppercase sm:space-x-6 mr-8">
<NextLink title={"Home"} href={"/"} />
<NextLink title={"Google"} href={"https://google.com"} />
</nav>
<span className="hidden md:visible md:inline-block">
<ClientOnly>
<ConnectButton
chainStatus={{ smallScreen: "name", largeScreen: "icon" }}
showBalance={{ smallScreen: false, largeScreen: false }}
/>
</ClientOnly>
</span>
</div>
</div>
</section>
);
};

const NextLink = ({ title, href }: { title: string; href: string }) => {
const router = useRouter();
const external = href.startsWith("http");

return (
<Link
target={external ? "_blank" : undefined}
rel={external ? "noopener" : undefined}
href={href}
className={`${
router.pathname === href ? "text-gray-500" : "text-gray-400"
} hover:text-gray-500 flex items-center`}
>
{title}
{external && (
<svg
className="pl-1"
height="12"
viewBox="0 0 123 123"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M110.6 72.5801C110.6 69.3901 113.19 66.8001 116.38 66.8001C119.57 66.8001 122.16 69.3901 122.16 72.5801V105.77C122.16 110.48 120.24 114.76 117.14 117.86C114.04 120.96 109.76 122.88 105.05 122.88H17.11C12.4 122.88 8.12 120.96 5.02 117.86C1.92 114.76 0 110.48 0 105.77V17.1901C0 12.4801 1.92 8.20006 5.02 5.10006C8.12 2.00006 12.4 0.0800553 17.11 0.0800553H50.09C53.28 0.0800553 55.87 2.67006 55.87 5.86006C55.87 9.05006 53.28 11.6401 50.09 11.6401H17.11C15.59 11.6401 14.21 12.2701 13.2 13.2701C12.19 14.2801 11.57 15.6601 11.57 17.1801V105.76C11.57 107.28 12.2 108.66 13.2 109.67C14.21 110.68 15.59 111.3 17.11 111.3H105.06C106.58 111.3 107.96 110.67 108.97 109.67C109.98 108.67 110.6 107.28 110.6 105.76V72.5801ZM112.42 17.4601L54.01 76.6001C51.78 78.8701 48.12 78.9001 45.85 76.6701C43.58 74.4401 43.55 70.7801 45.78 68.5101L101.94 11.6401H78.56C75.37 11.6401 72.78 9.05006 72.78 5.86006C72.78 2.67006 75.37 0.0800553 78.56 0.0800553H105.06C110.18 0.0800553 116.78 -0.789945 120.71 3.18006C123.19 5.69006 122.64 25.7001 122.32 37.2901C122.24 40.2901 122.17 42.5801 122.17 44.2201C122.17 47.4101 119.58 50.0001 116.39 50.0001C113.2 50.0001 110.61 47.4101 110.61 44.2201C110.61 43.9101 110.69 40.9001 110.8 36.9801C110.96 30.9401 111.93 22.9401 112.42 17.4601Z"
fill="currentColor"
/>
</svg>
)}
</Link>
);
};
25 changes: 25 additions & 0 deletions components/layout/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import Head from "next/head";
import { useRouter } from "next/router";
import { PropsWithChildren } from "react";
import Header from "./header";
import Footer from "./footer";

interface Props extends PropsWithChildren {
name: string | null;
}

const Layout: React.FC<Props> = ({ children, name }) => {
const router = useRouter();
return (
<main className="px-10 mx-auto max-w-7xl min-h-screen flex flex-col">
<Head>
<title>{name ? `${name} - DSU by Emptyset` : "DSU by Emptyset"}</title>
<link rel="icon" href="/favicon.ico" />
</Head>
<Header />
<section className="flex-grow">{children}</section>
<Footer />
</main>
);
};
export default Layout;
50 changes: 50 additions & 0 deletions constants/contracts.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
interface Contracts {
[key: number]: {
name: string;
isTestnet: boolean;
DSU: `0x${string}`;
USDC: `0x${string}`;
RESERVE: `0x${string}`;
};
}

const Contracts: Contracts = {
42161: {
name: "Arbitrum",
isTestnet: false,
DSU: "0x52C64b8998eB7C80b6F526E99E29ABdcC86B841b",
USDC: "0xff970a61a04b1ca14834a43f5de4533ebddb5cc8",
RESERVE: "0x0d49c416103Cbd276d9c3cd96710dB264e3A0c27",
},
1: {
name: "Ethereum",
isTestnet: false,
DSU: "0x605d26fbd5be761089281d5cec2ce86eea667109",
USDC: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
RESERVE: "0xD05aCe63789cCb35B9cE71d01e4d632a0486Da4B",
},
10: {
name: "Optimism",
isTestnet: false,
DSU: "0x52C64b8998eB7C80b6F526E99E29ABdcC86B841b",
USDC: "0x7f5c764cbc14f9669b88837ca1490cca17c31607",
RESERVE: "0x0d49c416103Cbd276d9c3cd96710dB264e3A0c27",
},

421613: {
name: "Arbitrum Testnet",
isTestnet: true,
DSU: "0x52C64b8998eB7C80b6F526E99E29ABdcC86B841b",
USDC: "0x6775842ae82bf2f0f987b10526768ad89d79536e",
RESERVE: "0x0d49c416103Cbd276d9c3cd96710dB264e3A0c27",
},
84531: {
name: "Base Testnet",
isTestnet: true,
DSU: "0x52C64b8998eB7C80b6F526E99E29ABdcC86B841b",
USDC: "0x7b4adf64b0d60ff97d672e473420203d52562a84",
RESERVE: "0x13b7A79e050ef2C3fDc858EFD5c066c3655be841",
},
};

export default Contracts;
Loading

0 comments on commit 2ff69ce

Please sign in to comment.