Skip to content
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

Overhaul state and hooks with SDK types/constants and zustand #4

Merged
merged 60 commits into from
Nov 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
dbd4f3a
wip sdk overhaul
zkSoju Nov 16, 2022
0ef275f
clean up all hooks
zkSoju Nov 17, 2022
5dc1930
fix errors
zkSoju Nov 17, 2022
d2c43e2
load default tokens
zkSoju Nov 17, 2022
872799c
setup user input
zkSoju Nov 17, 2022
ecc683d
change to currency a/b
zkSoju Nov 17, 2022
9ddcdd3
validate input
zkSoju Nov 17, 2022
2fd6124
update naming
zkSoju Nov 17, 2022
cfab0fa
add provide liq hook
zkSoju Nov 17, 2022
c928e41
sort out state + display balances
zkSoju Nov 17, 2022
461dbe0
abstract tab slider
zkSoju Nov 17, 2022
2db0490
add mint + withdraw with sdk
zkSoju Nov 17, 2022
b55e43e
add damn data hook
zkSoju Nov 17, 2022
6b67c19
add sync l2 hooks
zkSoju Nov 17, 2022
2827eb7
fix error when data contents null
zkSoju Nov 17, 2022
df55a7f
setup swap store
zkSoju Nov 17, 2022
be05128
Merge branch 'polygon' into use-sdk
zkSoju Nov 17, 2022
8ac292a
add swap + sync l1
zkSoju Nov 17, 2022
26d7165
add burn + swap callback hooks
zkSoju Nov 17, 2022
1ac8597
set voucher currencies
zkSoju Nov 17, 2022
bd723ad
Merge branch 'damn-hooks' into use-sdk
zkSoju Nov 17, 2022
1181ba8
update contracts
zkSoju Nov 17, 2022
327e5ef
updated addresses
exp-table Nov 17, 2022
3fb28cc
fetch reserves for damm
zkSoju Nov 17, 2022
b8c6eec
typo
exp-table Nov 17, 2022
8b3bf0e
Merge branch 'use-sdk' of https://github.com/exp-table/damm-frontend …
zkSoju Nov 17, 2022
0a0706f
move tabs
zkSoju Nov 17, 2022
00bedc2
display correct LP
exp-table Nov 17, 2022
465dd8f
Merge branch 'use-sdk' of https://github.com/exp-table/damm-frontend …
exp-table Nov 17, 2022
ebd187d
eerge branch 'use-sdk' of https://github.com/exp-table/damm-frontend …
exp-table Nov 17, 2022
0025dae
Merge branch 'use-sdk' of https://github.com/exp-table/damm-frontend …
exp-table Nov 17, 2022
54ac1bd
add toast hook
zkSoju Nov 17, 2022
24ff082
setup approvals and require sync under marked
zkSoju Nov 17, 2022
2a011b3
reactive swap
zkSoju Nov 18, 2022
8b1a343
disable input if wrong chain
zkSoju Nov 18, 2022
b367ff4
check insufficient balance
zkSoju Nov 18, 2022
3e6b5c8
fix allowance check
zkSoju Nov 18, 2022
11464fc
setup reactive provide
zkSoju Nov 18, 2022
3fe7ba1
use parsed amounts as display to handle reactiveness
zkSoju Nov 18, 2022
a022bc1
fix parsed output using wrong currency
zkSoju Nov 18, 2022
1fe1481
make swap reactive as well
zkSoju Nov 18, 2022
fe0275a
remove unused calculation
zkSoju Nov 18, 2022
18f0224
fix error when hook is enabled before args defined
zkSoju Nov 18, 2022
8d215fa
make swap async
zkSoju Nov 18, 2022
9fd4604
only fetch token on the chain it lives on
zkSoju Nov 18, 2022
b9fbfba
fix isApproved allowance check
zkSoju Nov 18, 2022
e4a56d6
ayo fix calc?
zkSoju Nov 18, 2022
383602e
using amm data for virtual reserves
exp-table Nov 18, 2022
2af4c63
fix input not being called
zkSoju Nov 18, 2022
5b24a72
fix approvals somehow wtf
zkSoju Nov 18, 2022
dcdc9e4
Merge branch 'use-sdk' of https://github.com/exp-table/damm-frontend …
zkSoju Nov 18, 2022
26eb02e
revert changes but still works wtf
zkSoju Nov 18, 2022
b3bcc82
move tab data to separate folder
zkSoju Nov 18, 2022
53ee95f
clean up tabs
zkSoju Nov 18, 2022
8c7e33d
fix input error with default state
zkSoju Nov 18, 2022
fc6f720
provide: allow typing from both sides
zkSoju Nov 18, 2022
981ebba
fix display and input display
zkSoju Nov 18, 2022
115b7a8
fix reactive swap inputs
zkSoju Nov 18, 2022
14371fd
use uniswap formatting
zkSoju Nov 18, 2022
56a10ec
Merge branch 'damn-hooks' into use-sdk
zkSoju Nov 18, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
521 changes: 293 additions & 228 deletions components/DammTabContent.tsx

Large diffs are not rendered by default.

94 changes: 40 additions & 54 deletions components/InputWithBalance.tsx
Original file line number Diff line number Diff line change
@@ -1,84 +1,70 @@
import { BigNumber } from "ethers";
import React from "react";
import React, { useMemo } from "react";
import { BiDollar } from "react-icons/bi";
import { useNetwork } from "wagmi";
import { validateNumber } from "../lib/utils";
import { formatCurrencyAmount } from "../lib/utils/formatCurrencyAmount";
import { ChainId, Currency, CurrencyAmount } from "../sdk";

const InputWithBalance = ({
label,
expectedChainId,
currency,
balance,
value,
setValue,
setError,
maxEnabled = false,
onUserInput,
showMaxButton,
onMax,
disabled = false,
expectedChainId,
}: {
label: string;
expectedChainId: number;
balance?: {
formatted: string;
value: BigNumber;
};
currency: Currency | undefined;
balance: CurrencyAmount<Currency> | undefined;
value: string;
setValue: (value: string) => void;
setError?: (error: string | undefined) => void;
maxEnabled?: boolean;
onUserInput?: (value: string) => void;
showMaxButton: boolean;
onMax?: () => void;
disabled?: boolean;
expectedChainId: ChainId;
}) => {
const { chain } = useNetwork();

let error = chain?.id !== expectedChainId;

const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
if (validateNumber(e.target.value)) {
setValue(e.target.value);
if (setError && balance && e.target.value !== "") {
const value = parseFloat(e.target.value) * 10 ** 6;
if (BigNumber.from(value).gt(balance.value)) {
console.log("error");
setError("Insufficient balance");
} else {
setError(undefined);
}
}
}
};
if (!chain || chain.id !== expectedChainId) {
disabled = true;
}

return (
<div className="relative mb-4">
<input
disabled={error}
disabled={disabled}
className={`flex h-24 w-full items-start justify-between rounded-sm border border-white/5 ${
error ? "bg-transparent" : "bg-black/10"
disabled ? "bg-transparent" : "bg-black/10"
} p-4 pb-12 pt-4 font-wagmi text-xl text-white placeholder:text-white/50 focus:outline-none`}
placeholder="0.00"
value={value}
onChange={handleChange}
onChange={(e) => {
if (onUserInput) {
onUserInput(e.target.value);
}
}}
/>
<div className="absolute top-4 right-4 flex flex-col items-end">
<h4
className={`mb-2 flex h-fit items-center rounded-sm border border-white/5 px-2 py-0.5 ${
error ? "text-white/50" : "bg-black/10 text-white"
}`}
>
<BiDollar className="mr-2 rounded-sm bg-white/5 p-px" />
{label}
</h4>
{!error && (
{currency && (
<h4
className={`mb-2 flex h-fit items-center rounded-sm border border-white/5 px-2 py-0.5 ${
disabled ? "text-white/50" : "bg-black/10 text-white"
}`}
>
<BiDollar className="mr-2 rounded-sm bg-white/5 p-px" />
{currency?.symbol}
</h4>
)}
{!disabled && (
<div className="flex items-center space-x-2">
{balance && (
<p className="text-sm text-white/50">
Balance: {parseFloat(parseFloat(balance.formatted).toFixed(6))}
Balance: {formatCurrencyAmount(balance, 6)}
</p>
)}
{maxEnabled && balance && (
<button
onClick={() => {
console.log("max");
setValue(
balance.value.div(BigNumber.from(10).pow(6)).toString()
);
}}
>
{showMaxButton && balance && (
<button onClick={onMax}>
<p className="text-sm text-white">Max</p>
</button>
)}
Expand Down
9 changes: 5 additions & 4 deletions components/InteractButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,20 @@ import {
const InteractButton = ({
text,
expectedChainId,
onClick,
error = undefined,
onConfirm,
children,
}: {
text: string;
expectedChainId: number;
error?: string;
onClick: () => void;
onConfirm: () => void;
children?: React.ReactNode;
}) => {
const { address } = useAccount();
const { chain } = useNetwork();

const error = false;

return (
<>
{(() => {
Expand All @@ -36,7 +37,7 @@ const InteractButton = ({
return <>{children}</>;
}

return <Button onClick={onClick} text={text} />;
return <Button onClick={onConfirm} text={text} />;
} else {
return <InteractConnectButton />;
}
Expand Down
128 changes: 128 additions & 0 deletions components/PageWrapper.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import Main from "../components/layouts/Main";
import Article from "../components/layouts/Article";
import * as Tabs from "@radix-ui/react-tabs";
import { useState, useEffect, useRef } from "react";
import { chain, useSwitchNetwork } from "wagmi";
import { useIsMounted } from "../lib/hooks/useIsMounted";
import { BiCog } from "react-icons/bi";
import TabContainer from "../components/TabContainer";
import SwapTabContent from "../components/SwapTabContent";
import DammTabContent from "../components/DammTabContent";
import SettingsTabContent from "../components/SettingsTabContent";
import { GiPeaceDove } from "react-icons/gi";
import { useUserStore } from "../state/user/useUserStore";

const PageWrapper = ({ children }: { children?: React.ReactNode }) => {
const [isAutoSwitch, activeTab, setActiveTab] = useUserStore((state) => [
state.isAutoSwitch,
state.activeTab,
state.setActiveTab,
]);
const timerRef = useRef(0);

useEffect(() => {
return () => clearTimeout(timerRef.current);
}, []);

const { chains, error, isLoading, pendingChainId, switchNetwork } =
useSwitchNetwork();

useEffect(() => {
if (isAutoSwitch) {
if (activeTab === "damm") {
switchNetwork?.(chain.goerli.id);
}
if (activeTab === "polygon") {
switchNetwork?.(chain.polygonMumbai.id);
}
if (activeTab === "arbi") {
switchNetwork?.(chain.arbitrumGoerli.id);
}
}
}, [activeTab]);

const isMounted = useIsMounted();

if (!isMounted) return null;

return (
<Main>
<Article>
<div className="radial absolute h-screen w-full"></div>
<div className="background-gradient absolute h-full w-[100vw] opacity-20">
<div className="background-gradient-pattern" />
</div>
<div className="relative flex h-screen w-full items-start justify-center pt-48">
<div className="relative flex w-full flex-col items-start px-4 py-4">
<div className="flex w-full flex-row space-x-4">
<Tabs.Root
value={activeTab}
onValueChange={(v) => setActiveTab(v)}
defaultValue="damm"
className="w-full"
>
<Tabs.List className="relative z-10 flex flex-row">
<Tabs.Trigger
value="damm"
className="relative w-full cursor-pointer overflow-hidden rounded-sm rounded-b-none border border-b-0 border-white/5 bg-black/10 px-4 py-2 text-left transition duration-300 ease-linear hover:text-white focus:outline-none rdx-state-active:bg-[#313135] rdx-state-active:text-white rdx-state-inactive:text-white/50"
>
{activeTab === "ethereum" && (
<GiPeaceDove className="absolute right-0 -rotate-45 text-6xl text-white/5" />
)}
<p className="font-normal">dAMM</p>
</Tabs.Trigger>
<Tabs.Trigger
value="polygon"
className="relative w-full cursor-pointer overflow-hidden rounded-sm rounded-b-none border border-b-0 border-white/5 bg-black/10 px-4 py-2 text-left transition duration-300 ease-linear hover:text-white focus:outline-none rdx-state-active:bg-[#313135] rdx-state-active:text-white rdx-state-inactive:text-white/50"
>
{activeTab === "polygon" && (
<GiPeaceDove className="absolute right-0 -rotate-45 text-6xl text-white/5" />
)}
<p className="font-normal">Polygon AMM</p>
</Tabs.Trigger>
<Tabs.Trigger
value="arbi"
className="relative w-full cursor-pointer overflow-hidden rounded-sm rounded-b-none border border-b-0 border-white/5 bg-black/10 px-4 py-2 text-left transition duration-300 ease-linear hover:text-white focus:outline-none rdx-state-active:bg-[#313135] rdx-state-active:text-white rdx-state-inactive:text-white/50"
>
{activeTab === "arbitrum" && (
<GiPeaceDove className="absolute right-0 -rotate-45 text-6xl text-white/5" />
)}
<p className="font-normal">Arbitrum AMM</p>
</Tabs.Trigger>
<Tabs.Trigger
value="settings"
className="w-12 cursor-pointer rounded-sm rounded-b-none border border-b-0 border-white/5 bg-black/10 px-4 py-2 text-left transition duration-300 ease-linear hover:text-white focus:outline-none rdx-state-active:bg-[#313135] rdx-state-active:text-white rdx-state-inactive:text-white/50"
>
<BiCog className="" />
</Tabs.Trigger>
</Tabs.List>
<Tabs.Content value="ethereum">
<TabContainer>
<DammTabContent />
</TabContainer>
</Tabs.Content>
<Tabs.Content value="polygon">
<TabContainer>
{/* <SwapTabContent expectedChainId={chain.polygonMumbai.id} /> */}
</TabContainer>
</Tabs.Content>
<Tabs.Content value="arbitrum">
<TabContainer>
{/* <SwapTabContent expectedChainId={chain.arbitrumGoerli.id} /> */}
</TabContainer>
</Tabs.Content>
<Tabs.Content value="settings">
<TabContainer>
<SettingsTabContent />
</TabContainer>
</Tabs.Content>
</Tabs.Root>
</div>
</div>
</div>
</Article>
</Main>
);
};

export default PageWrapper;
6 changes: 3 additions & 3 deletions components/SettingsTabContent.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React from "react";
import { useState } from "react";
import * as Switch from "@radix-ui/react-switch";
import { useStore } from "../lib/store";
import { useUserStore } from "../state/user/useUserStore";

const SettingsTabContent = () => {
const isAutoSwitch = useStore((state) => state.isAutoSwitch);
const setAutoSwitch = useStore((state) => state.setAutoSwitch);
const isAutoSwitch = useUserStore((state) => state.isAutoSwitch);
const setAutoSwitch = useUserStore((state) => state.setAutoSwitch);

return (
<>
Expand Down
Loading