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

Show insufficient funds popup #318

Merged
merged 1 commit into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
16 changes: 10 additions & 6 deletions packages/keychain/src/components/Auth/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import { Field } from "@cartridge/ui";
import { VStack, Button } from "@chakra-ui/react";
import { Container } from "../Container";
import { Form as FormikForm, Field as FormikField, Formik } from "formik";
import { PORTAL_FOOTER_MIN_HEIGHT, PortalBanner, PortalFooter } from "components";
import {
PORTAL_FOOTER_MIN_HEIGHT,
PortalBanner,
PortalFooter,
} from "components";
import { useCallback, useState } from "react";
import Controller from "utils/controller";
import { FormValues, LoginProps } from "./types";
Expand Down Expand Up @@ -43,23 +47,23 @@ export function Login({
} = await fetchAccount(values.username);
address = contractAddress;

await doLogin(values.username, credentialId, publicKey)
onSuccess(new Controller(address, publicKey, credentialId))
await doLogin(values.username, credentialId, publicKey);
onSuccess(new Controller(address, publicKey, credentialId));

log({ type: "webauthn_login", address });
} catch (e) {
setError(e)
setError(e);

log({
type: "webauthn_login_error",
payload: {
error: e?.message,
},
address,
})
});
}

setIsLoading(false)
setIsLoading(false);
},
[log, onSuccess],
);
Expand Down
2 changes: 1 addition & 1 deletion packages/keychain/src/components/Container/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export function Container({
);
}

export const FOOTER_HEIGHT = 40
export const FOOTER_HEIGHT = 40;

function Wrapper({ children }: React.PropsWithChildren) {
return (
Expand Down
176 changes: 85 additions & 91 deletions packages/keychain/src/components/Execute/LowEth.tsx
Original file line number Diff line number Diff line change
@@ -1,102 +1,96 @@
import { Button, Circle, HStack, Spacer, Text, VStack } from "@chakra-ui/react";
import { AlertIcon } from "@cartridge/ui";
import { CopyIcon, EthereumIcon, StarknetIcon } from "@cartridge/ui";
import { HStack, Spacer, Text, VStack } from "@chakra-ui/react";
import { Container } from "components/Container";
import { PortalBanner } from "components/PortalBanner";
import { useState } from "react";
import { BigNumberish, constants } from "starknet";
import { formatAddress } from "utils/contracts";

export interface LowEthInfo {
label: string;
balance: string;
max: string;
lowAmount: string;
reject: () => void;
onBridge: () => void;
}

export function LowEth({
label,
const NewLowEth = ({
chainId,
address,
balance,
max,
lowAmount,
reject,
onBridge,
}: LowEthInfo) {
}: {
chainId: constants.StarknetChainId;
address: BigNumberish;
balance: BigNumberish;
}) => {
const [copied, setCopied] = useState(false);
return (
<VStack
w="full"
spacing={4.5}
pt={4.5}
borderTop="1px solid"
borderTopColor="solid.accent"
>
<HStack w="full" borderRadius="md" bg="solid.primary" px={3} py={4.5}>
<HStack color="text.secondaryAccent" w="full">
<Text
color="text.secondaryAccent"
fontSize="xs"
fontWeight="bold"
textTransform="uppercase"
>
{label}
</Text>
</HStack>

<Spacer />
<Container chainId={chainId} hideAccount>
<PortalBanner
title="Insufficient Funds"
description="You'll need more gas to complete this transaction. Send some ETH to your controller address."
/>

<VStack w="full" align="end">
<Text color="text.error" fontSize="sm">{`~${lowAmount}`}</Text>
<HStack color="text.secondary" fontSize="xs">
<Text color="currentColor">{`Bal: ${balance}`}</Text>
<Text color="currentColor">{`Max: ${max}`}</Text>
<VStack w="full">
<VStack w="full" align="flex-start" fontSize="14px">
<Text color="darkGray.400" fontSize="11px">
BALANCE
</Text>
<HStack
h="40px"
w="full"
borderRadius="4px"
overflow="hidden"
spacing="3px"
>
<HStack bgColor="darkGray.900" boxSize="full" flex="2" px="10px">
<EthereumIcon boxSize="24px" color="red.400" />{" "}
<Text color="red.400">{balance}</Text>
</HStack>
<HStack
bgColor="darkGray.900"
boxSize="full"
flex="1"
justify="center"
>
<StarknetIcon boxSize="24px" /> <Text>Sepolia</Text>
</HStack>
</HStack>
</VStack>
</HStack>

<VStack
w="full"
borderRadius="md"
px={4}
py={3}
bg="yellow.200"
align="start"
>
<HStack
w="full"
minH="42px"
borderBottom="1px solid"
borderBottomColor="solid.accent"
>
<Circle size={7} bg="yellow.300">
<AlertIcon color="yellow.800" />
</Circle>

<Text
fontSize="xs"
fontWeight="bold"
color="yellow.800"
letterSpacing="0.05em"
textTransform="uppercase"
>
Insufficient Funds
<VStack w="full" align="flex-start" fontSize="14px">
<Text color="darkGray.400" fontSize="11px">
ADDRESS
</Text>
</HStack>

<Text fontSize="xs" color="yellow.800">
You do not have enough ETH to complete the above transaction
</Text>

<HStack w="full">
<Button
colorScheme="translucent"
color="black"
flex={1}
onClick={reject}
<HStack
h="40px"
w="full"
borderRadius="4px"
overflow="hidden"
spacing="3px"
>
Reject
</Button>

<Button flex={1} onClick={onBridge}>
Add Funds
</Button>
</HStack>
<HStack
bgColor="darkGray.900"
boxSize="full"
flex="2"
px="10px"
cursor="pointer"
_hover={{
color: "darkGray.100",
}}
onClick={() => {
navigator.clipboard.writeText(address.toString());
setCopied(true);
}}
>
<Text color="inherit">{formatAddress(address)}</Text>
<Spacer />
<CopyIcon boxSize="24px" />
</HStack>
</HStack>
{copied && (
<HStack w="full" justify="center">
<Text color="darkGray.200" fontSize="12px">
COPIED
</Text>
</HStack>
)}
</VStack>
</VStack>
</VStack>
</Container>
);
}
};

export default NewLowEth;
95 changes: 34 additions & 61 deletions packages/keychain/src/components/Execute/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useCallback, useEffect, useMemo, useState } from "react";
import { Text, VStack, Spacer, Button } from "@chakra-ui/react";
import { Text, VStack, Button } from "@chakra-ui/react";

import Controller from "utils/controller";
import {
Expand All @@ -14,15 +14,14 @@ import { formatEther } from "viem";
import { ExecuteReply, ResponseCodes } from "@cartridge/controller";
import { Container } from "../Container";
import { Status } from "utils/account";
import { LowEth, LowEthInfo } from "./LowEth";
import { BridgeEth } from "../bridge/BridgeEth";
import { PortalBanner } from "../PortalBanner";
import { TransactionDuoIcon } from "@cartridge/ui";
import { Call } from "./Call";
import {
PORTAL_FOOTER_MIN_HEIGHT,
PortalFooter,
} from "components/PortalFooter";
import LowEth from "./LowEth";

export const CONTRACT_ETH =
"0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7";
Expand Down Expand Up @@ -56,8 +55,7 @@ export function Execute({
const [isLoading, setLoading] = useState<boolean>(false);
const [ethBalance, setEthBalance] = useState<bigint>();
const [ethApproved, setEthApproved] = useState<bigint>();
const [lowEthInfo, setLowEthInfo] = useState<LowEthInfo>();
const [bridging, setBridging] = useState<boolean>(false);
const [lowEth, setLowEth] = useState<boolean>(false);

const account = controller.account(chainId);
const calls = useMemo(() => {
Expand Down Expand Up @@ -146,25 +144,9 @@ export function Execute({
}

if (ethBalance < ethApproved) {
setLowEthInfo({
label: "Approved Eth",
balance: format(ethBalance),
max: format(ethApproved),
lowAmount: format((ethBalance - ethApproved) * -1n),
reject: onCancel,
onBridge: () => setBridging(true),
});
} else if (fees?.max && ethBalance < ethApproved + fees.max) {
setLowEthInfo({
label: "Network Fee",
balance: format(ethBalance),
max: format(ethApproved + fees.max),
lowAmount: format(ethBalance - (ethApproved + fees.max) * -1n),
reject: onCancel,
onBridge: () => setBridging(true),
});
setLowEth(true);
}
}, [ethBalance, ethApproved, fees?.max, onCancel]);
}, [ethBalance, ethApproved]);

const onSubmit = useCallback(async () => {
setLoading(true);
Expand All @@ -177,23 +159,21 @@ export function Execute({
});
}, [account, calls, fees, onExecute]);

const onBack = useCallback(() => {
setBridging(false);
}, []);

if (bridging) {
if (lowEth) {
return (
<Container chainId={chainId} onLogout={onLogout} onBack={onBack}>
<BridgeEth chainId={chainId} controller={controller} />
</Container>
<LowEth
chainId={chainId}
address={controller.account(chainId).address}
balance={format(ethBalance)}
/>
);
}

return (
<Container chainId={chainId} onLogout={onLogout}>
<PortalBanner Icon={TransactionDuoIcon} title="Submit Transaction" />

<VStack w="full" pb={lowEthInfo ? undefined : PORTAL_FOOTER_MIN_HEIGHT}>
<VStack w="full" pb={lowEth ? undefined : PORTAL_FOOTER_MIN_HEIGHT}>
<VStack spacing="1px" w="full" borderRadius="md" bg="solid.primary">
<VStack w="full" p={3} align="flex-start">
<Text fontSize="xs" fontWeight="bold" color="text.secondaryAccent">
Expand All @@ -216,35 +196,28 @@ export function Execute({
</VStack>
</VStack>

{lowEthInfo ? (
<>
<Spacer />
<LowEth {...lowEthInfo} />
</>
) : (
<VStack w="full">
<Fees
error={error}
chainId={chainId}
fees={fees}
balance={ethBalance && format(ethBalance)}
approved={ethApproved && format(ethApproved)}
/>

<PortalFooter>
<Button
colorScheme="colorful"
onClick={onSubmit}
isLoading={isLoading}
isDisabled={!fees}
>
submit
</Button>

<Button onClick={onCancel}>Cancel</Button>
</PortalFooter>
</VStack>
)}
<VStack w="full">
<Fees
error={error}
chainId={chainId}
fees={fees}
balance={ethBalance && format(ethBalance)}
approved={ethApproved && format(ethApproved)}
/>

<PortalFooter>
<Button
colorScheme="colorful"
onClick={onSubmit}
isLoading={isLoading}
isDisabled={!fees}
>
submit
</Button>

<Button onClick={onCancel}>Cancel</Button>
</PortalFooter>
</VStack>
</VStack>
</Container>
);
Expand Down
Loading
Loading