Skip to content

Commit

Permalink
refactor to common bridge components (amount, chain, fees)
Browse files Browse the repository at this point in the history
  • Loading branch information
karooolis committed Apr 17, 2024
1 parent 329d542 commit 253d1b9
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 138 deletions.
14 changes: 11 additions & 3 deletions packages/account-kit/src/steps/gas-tank/DepositContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import { DirectDepositContent } from "./DirectDepositContent";
import { AccountModalTitle } from "../../AccoutModalTitle";
import { AccountModalSection } from "../../AccountModalSection";
import { GasTankStateContent } from "./GasTankStateContent";
import { ChainSelect } from "./components/ChainSelect";
import { AmountInput } from "./components/AmountInput";
import { BalancesFees } from "./components/BalancesFees";

type DepositMethod = "direct" | "bridge" | "relay" | null;

Expand Down Expand Up @@ -43,9 +46,14 @@ export function DepositContent() {
Add funds from your wallet to your tank to fund transactions for any MUD apps on Chain Name.
</p>

{chain.id === userAccountChainId && (
<DirectDepositContent amount={depositAmount} setAmount={setDepositAmount} />
)}
<div className="flex gap-[12px]">
<ChainSelect />
<AmountInput amount={depositAmount} setAmount={setDepositAmount} />
</div>

<BalancesFees />

{chain.id === userAccountChainId && <DirectDepositContent amount={depositAmount} />}
{chain.sourceId === userAccountChainId && <StandardBridgeContent />}
{chain.id !== userAccountChainId && chain.sourceId !== userAccountChainId && <RelayLinkContent />}
</div>
Expand Down
136 changes: 2 additions & 134 deletions packages/account-kit/src/steps/gas-tank/DirectDepositContent.tsx
Original file line number Diff line number Diff line change
@@ -1,131 +1,25 @@
import { ReactNode, Ref, forwardRef } from "react";
import { parseEther } from "viem";
import { twMerge } from "tailwind-merge";
import { AccountModalSection } from "../../AccountModalSection";
import { useAccount, useBalance, useConfig as useWagmiConfig, useWriteContract } from "wagmi";
import { useAccount, useConfig as useWagmiConfig, useWriteContract } from "wagmi";
import GasTankAbi from "@latticexyz/gas-tank/out/IWorld.sol/IWorld.abi.json";
import * as Select from "@radix-ui/react-select";
import { useConfig } from "../../AccountKitProvider";
import { getGasTankBalanceQueryKey } from "../../useGasTankBalance";
import { waitForTransactionReceipt } from "wagmi/actions";
import { useQueryClient } from "@tanstack/react-query";
import { useOnboardingSteps } from "../../useOnboardingSteps";
import { Button } from "../../ui/Button";
import { Shadow } from "../../ui/Shadow";
import { useEstimatedFees } from "./hooks/useEstimatedFees";

type SelectItemProps = {
value: string;
children: ReactNode;
className?: string;
disabled?: boolean;
};

type AmountProps = {
amount: string | undefined;
setAmount: (amount: string) => void;
};

const AmountInput = ({ amount, setAmount }: AmountProps) => {
return (
<input
className="w-full px-[16px] border border-neutral-300"
type="number"
value={amount}
onChange={(evt) => setAmount(evt.target.value)}
placeholder="0.01"
step={0.000000000000000001}
/>
);
};

const SelectItem = forwardRef<HTMLDivElement, SelectItemProps>(function SelectItem(
{ children, className, ...props },
forwardedRef,
) {
return (
<Select.Item
className={twMerge(
"text-[13px] leading-none text-violet11 rounded-[3px] flex items-center",
"h-[25px] pr-[35px] pl-[25px] relative select-none data-[disabled]:text-mauve8",
"data-[disabled]:pointer-events-none data-[highlighted]:outline-none",
"data-[highlighted]:bg-violet9 data-[highlighted]:text-violet1",
className,
)}
ref={forwardedRef as Ref<HTMLDivElement>}
{...props}
>
<Select.ItemText>{children}</Select.ItemText>
<Select.ItemIndicator className="absolute left-0 w-[25px] inline-flex items-center justify-center">
{/* <CheckIcon /> */}
CheckIcon
</Select.ItemIndicator>
</Select.Item>
);
});

function ChainSelect() {
return (
<Select.Root defaultValue="17069">
<Select.Trigger
className={twMerge(
"inline-flex items-center justify-center h-[50px] w-[70px]",
"text-[13px] leading-none gap-[5px]",
"bg-white",
"border border-neutral-300",
"outline-none",
)}
aria-label="Food"
>
<Shadow>
<Select.Value placeholder="Chain" />
<Select.Icon>{/* <ChevronDownIcon /> */}</Select.Icon>
</Shadow>
</Select.Trigger>

<Select.Portal>
<Shadow>
<Select.Content className="overflow-hidden bg-white rounded-md shadow-[0px_10px_38px_-10px_rgba(22,_23,_24,_0.35),0px_10px_20px_-15px_rgba(22,_23,_24,_0.2)]">
<Select.ScrollUpButton className="flex items-center justify-center h-[25px] bg-white text-violet11 cursor-default">
{/* <ChevronUpIcon /> */}
Up
</Select.ScrollUpButton>
<Select.Viewport className="p-[5px]">
<Select.Group>
<Select.Label className="px-[25px] text-xs leading-[25px]">Select chain:</Select.Label>
<SelectItem value="17069">Redstone</SelectItem>
<SelectItem value="1">Ethereum</SelectItem>
<SelectItem value="7777777">Zora</SelectItem>
</Select.Group>
</Select.Viewport>
<Select.ScrollDownButton className="flex items-center justify-center h-[25px] bg-white text-violet11 cursor-default">
{/* <ChevronDownIcon /> */}
Down
</Select.ScrollDownButton>
</Select.Content>
</Shadow>
</Select.Portal>
</Select.Root>
);
}

type DirectDepositContentProps = {
amount: string | undefined;
setAmount: (amount: string) => void;
};

export function DirectDepositContent({ amount, setAmount }: DirectDepositContentProps) {
export function DirectDepositContent({ amount }: DirectDepositContentProps) {
const queryClient = useQueryClient();
const wagmiConfig = useWagmiConfig();
const { chain, gasTankAddress } = useConfig();
const { resetStep } = useOnboardingSteps();
const estimatedFees = useEstimatedFees();
const userAccount = useAccount();
const userAccountAddress = userAccount.address;
const userBalance = useBalance({
chainId: chain.id,
address: userAccountAddress,
});
const { writeContractAsync, isPending, error } = useWriteContract({
mutation: {
onSuccess: async (hash) => {
Expand Down Expand Up @@ -158,32 +52,6 @@ export function DirectDepositContent({ amount, setAmount }: DirectDepositContent
<div className="flex flex-col gap-2">
{error ? <div>{String(error)}</div> : null}

<div className="flex gap-[12px]">
<ChainSelect />
<AmountInput amount={amount} setAmount={setAmount} />
</div>

<div>
<ul>
<li className="flex justify-between py-[8px] border-b border-black border-opacity-10">
<span className="opacity-50">Available to deposit</span>
<span className="font-medium">
{userBalance?.data?.formatted || "..."} ETH <span className="text-neutral-800">($3,605.21)</span>
</span>
</li>
<li className="flex justify-between py-[8px] border-b border-black border-opacity-10">
<span className="opacity-50">Estimated fee</span>
<span className="font-medium">
{estimatedFees || "..."} ETH <span className="text-neutral-800">($3.40)</span>
</span>
</li>
<li className="flex justify-between py-[8px]">
<span className="opacity-50">Transfer time</span>
<span className="font-medium">~5 seconds</span>
</li>
</ul>
</div>

<Button className="w-full" pending={!userAccountAddress || isPending} onClick={handleDeposit}>
Deposit
</Button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useAccount } from "wagmi";
import { useConfig } from "../../AccountKitProvider";
import { RelayLinkContent } from "./RelayLinkContent";
import { StandardBridgeContent } from "./StandardBridgeContent";
import { DirectDepositContent } from "./DirectDepositContent";
import { DirectDepositContent } from "./DirectDepositContent.1";
import { AccountModalSection } from "../../AccountModalSection";
import { AccountModalTitle } from "../../AccoutModalTitle";
import { GasTankStateContent } from "./GasTankStateContent";
Expand Down
17 changes: 17 additions & 0 deletions packages/account-kit/src/steps/gas-tank/components/AmountInput.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export type AmountProps = {
amount: string | undefined;
setAmount: (amount: string) => void;
};

export const AmountInput = ({ amount, setAmount }: AmountProps) => {
return (
<input
className="w-full px-[16px] border border-neutral-300"
type="number"
value={amount}
onChange={(evt) => setAmount(evt.target.value)}
placeholder="0.01"
step={1e-18}
/>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { AccountModalSection } from "../../../AccountModalSection";
import { useAccount, useBalance } from "wagmi";
import { useConfig } from "../../../AccountKitProvider";
import { useEstimatedFees } from "../hooks/useEstimatedFees";

export function BalancesFees() {
const { chain } = useConfig();
const estimatedFees = useEstimatedFees();
const userAccount = useAccount();
const userAccountAddress = userAccount.address;
const userBalance = useBalance({
chainId: chain.id,
address: userAccountAddress,
});

return (
<AccountModalSection>
<div className="flex flex-col gap-2">
<ul>
<li className="flex justify-between py-[8px] border-b border-black border-opacity-10">
<span className="opacity-50">Available to deposit</span>
<span className="font-medium">
{userBalance?.data?.formatted || "..."} ETH <span className="text-neutral-800">($3,605.21)</span>
</span>
</li>
<li className="flex justify-between py-[8px] border-b border-black border-opacity-10">
<span className="opacity-50">Estimated fee</span>
<span className="font-medium">
{estimatedFees || "..."} ETH <span className="text-neutral-800">($3.40)</span>
</span>
</li>
<li className="flex justify-between py-[8px]">
<span className="opacity-50">Transfer time</span>
<span className="font-medium">~5 seconds</span>
</li>
</ul>
</div>
</AccountModalSection>
);
}
79 changes: 79 additions & 0 deletions packages/account-kit/src/steps/gas-tank/components/ChainSelect.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { ReactNode, Ref, forwardRef } from "react";
import { twMerge } from "tailwind-merge";
import * as Select from "@radix-ui/react-select";
import { Shadow } from "../../../ui/Shadow";

type SelectItemProps = {
value: string;
children: ReactNode;
className?: string;
disabled?: boolean;
};
const SelectItem = forwardRef<HTMLDivElement, SelectItemProps>(function SelectItem(
{ children, className, ...props },
forwardedRef,
) {
return (
<Select.Item
className={twMerge(
"text-[13px] leading-none text-violet11 rounded-[3px] flex items-center",
"h-[25px] pr-[35px] pl-[25px] relative select-none data-[disabled]:text-mauve8",
"data-[disabled]:pointer-events-none data-[highlighted]:outline-none",
"data-[highlighted]:bg-violet9 data-[highlighted]:text-violet1",
className,
)}
ref={forwardedRef as Ref<HTMLDivElement>}
{...props}
>
<Select.ItemText>{children}</Select.ItemText>
<Select.ItemIndicator className="absolute left-0 w-[25px] inline-flex items-center justify-center">
{/* <CheckIcon /> */}
CheckIcon
</Select.ItemIndicator>
</Select.Item>
);
});
export function ChainSelect() {
return (
<Select.Root defaultValue="17069">
<Select.Trigger
className={twMerge(
"inline-flex items-center justify-center h-[50px] w-[70px]",
"text-[13px] leading-none gap-[5px]",
"bg-white",
"border border-neutral-300",
"outline-none",
)}
aria-label="Food"
>
<Shadow>
<Select.Value placeholder="Chain" />
<Select.Icon>{/* <ChevronDownIcon /> */}</Select.Icon>
</Shadow>
</Select.Trigger>

<Select.Portal>
<Shadow>
<Select.Content className="overflow-hidden bg-white rounded-md shadow-[0px_10px_38px_-10px_rgba(22,_23,_24,_0.35),0px_10px_20px_-15px_rgba(22,_23,_24,_0.2)]">
<Select.ScrollUpButton className="flex items-center justify-center h-[25px] bg-white text-violet11 cursor-default">
{/* <ChevronUpIcon /> */}
Up
</Select.ScrollUpButton>
<Select.Viewport className="p-[5px]">
<Select.Group>
<Select.Label className="px-[25px] text-xs leading-[25px]">Select chain:</Select.Label>
<SelectItem value="17069">Redstone</SelectItem>
<SelectItem value="1">Ethereum</SelectItem>
<SelectItem value="7777777">Zora</SelectItem>
</Select.Group>
</Select.Viewport>
<Select.ScrollDownButton className="flex items-center justify-center h-[25px] bg-white text-violet11 cursor-default">
{/* <ChevronDownIcon /> */}
Down
</Select.ScrollDownButton>
</Select.Content>
</Shadow>
</Select.Portal>
</Select.Root>
);
}

0 comments on commit 253d1b9

Please sign in to comment.