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

[bug] binance errors #2711

Merged
merged 5 commits into from
Sep 25, 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
64 changes: 64 additions & 0 deletions wormhole-connect/src/hooks/useBalanceChecker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { useEffect, useState, useMemo } from 'react';
import { amount, routes } from '@wormhole-foundation/sdk';
import { getWalletConnection, TransferWallet } from 'utils/wallet';
import config from 'config';
import { useIsMounted } from './useIsMounted';

export const useBalanceChecker = (
quote?: routes.Quote<
routes.Options,
routes.ValidatedTransferParams<routes.Options>
>,
): {
feeSymbol?: string;
emreboga marked this conversation as resolved.
Show resolved Hide resolved
isCheckingBalance: boolean;
hasSufficientBalance: boolean;
walletBalance?: number;
networkCost?: number;
} => {
const [isCheckingBalance, setCheckingBalance] = useState(false);
const [state, setState] = useState<{ balance: number; cost: number } | null>(
null,
);
const isMounted = useIsMounted();

const feeSymbol = useMemo(() => {
if (!quote?.relayFee?.token) return;

return config.sdkConverter.findTokenConfigV1(
quote?.relayFee?.token,
Object.values(config.tokens),
)?.symbol;
}, [quote]);

useEffect(() => {
setCheckingBalance(true);
(async () => {
try {
if (!quote?.relayFee?.amount) return;

const wallet = getWalletConnection(TransferWallet.SENDING);
if (!wallet) return;

const cost = amount.whole(quote.relayFee.amount);
const balance = parseFloat(await wallet.getBalance());

if (isMounted.current) setState({ balance, cost });
} catch (e) {
console.error(e);
if (isMounted.current) setState(null);
} finally {
if (isMounted.current) setCheckingBalance(false);
emreboga marked this conversation as resolved.
Show resolved Hide resolved
}
})();
}, [quote, isMounted]);

return {
feeSymbol,
isCheckingBalance,
hasSufficientBalance:
!quote?.relayFee || (!!state && state?.balance > state?.cost),
walletBalance: state?.balance,
networkCost: state?.cost,
};
};
12 changes: 12 additions & 0 deletions wormhole-connect/src/hooks/useIsMounted.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { MutableRefObject, useEffect, useRef } from 'react';

export const useIsMounted = (): MutableRefObject<boolean> => {
const isMounted = useRef<boolean>(true);
useEffect(() => {
isMounted.current = true;
return () => {
isMounted.current = false;
};
}, []);
return isMounted;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import React from 'react';
import AlertBanner from 'components/v2/AlertBanner';
import { Stack, Typography, useTheme } from '@mui/material';
import { toFixedDecimals } from 'utils/balance';
import type { useBalanceChecker } from 'hooks/useBalanceChecker';

export default function WalletBalanceWarning({
isCheckingBalance,
hasSufficientBalance,
walletBalance,
emreboga marked this conversation as resolved.
Show resolved Hide resolved
networkCost,
feeSymbol,
}: ReturnType<typeof useBalanceChecker>) {
const theme = useTheme();
const content = isCheckingBalance
? ''
: `Insufficient ${feeSymbol} to cover network costs.`;

return (
<Stack direction="column" gap="10px">
<AlertBanner
warning
content={content}
show={!!isCheckingBalance || !hasSufficientBalance}
testId="wallet-balance-warning-message"
/>
{!hasSufficientBalance && !isCheckingBalance && (
<Stack>
{[
{ title: 'Wallet balance', balance: walletBalance! },
{ title: 'Network cost', balance: networkCost! },
].map((item) => (
<Stack
key={item.title}
direction="row"
justifyContent="space-between"
>
<Typography color={theme.palette.text.secondary} fontSize={14}>
{item.title}
</Typography>
<Typography color={theme.palette.text.secondary} fontSize={14}>
{!isNaN(item.balance) &&
`${toFixedDecimals(item.balance.toString(), 4)} ${feeSymbol}`}
</Typography>
</Stack>
))}
</Stack>
)}
</Stack>
);
}
27 changes: 25 additions & 2 deletions wormhole-connect/src/views/v2/Bridge/ReviewTransaction/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ import { toDecimals } from 'utils/balance';
import { useUSDamountGetter } from 'hooks/useUSDamountGetter';
import SendError from './SendError';
import { ERR_USER_REJECTED } from 'telemetry/types';
import { useBalanceChecker } from 'hooks/useBalanceChecker';
import WalletBalanceWarning from './WalletBalanceWarning';
import { QuoteResult } from 'routes/operator';

const useStyles = makeStyles()((theme) => ({
container: {
Expand All @@ -61,7 +64,7 @@ const useStyles = makeStyles()((theme) => ({

type Props = {
onClose: () => void;
quotes: any;
quotes: Record<string, QuoteResult | undefined>;
isFetchingQuotes: boolean;
};

Expand Down Expand Up @@ -115,6 +118,14 @@ const ReviewTransaction = (props: Props) => {
? sdkAmount.whole(quote.destinationNativeGas)
: undefined;

const {
isCheckingBalance,
feeSymbol,
hasSufficientBalance,
walletBalance,
networkCost,
} = useBalanceChecker(quote);

const send = async () => {
setSendError(undefined);

Expand Down Expand Up @@ -326,7 +337,11 @@ const ReviewTransaction = (props: Props) => {

return (
<Button
disabled={props.isFetchingQuotes || isTransactionInProgress}
disabled={
props.isFetchingQuotes ||
isTransactionInProgress ||
!hasSufficientBalance
}
variant="primary"
className={classes.confirmTransaction}
onClick={() => send()}
Expand Down Expand Up @@ -368,6 +383,7 @@ const ReviewTransaction = (props: Props) => {
route,
amount,
send,
hasSufficientBalance,
]);

if (!route || !walletsConnected) {
Expand Down Expand Up @@ -395,6 +411,13 @@ const ReviewTransaction = (props: Props) => {
/>
</Collapse>
)}
<WalletBalanceWarning
hasSufficientBalance={hasSufficientBalance}
isCheckingBalance={isCheckingBalance}
walletBalance={walletBalance}
networkCost={networkCost}
feeSymbol={feeSymbol}
/>
<SendError humanError={sendError} internalError={sendErrorInternal} />
{confirmTransactionButton}
</Stack>
Expand Down
2 changes: 1 addition & 1 deletion wormhole-connect/src/views/v2/Bridge/Routes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ const Routes = ({ ...props }: Props) => {
Routes
</Typography>
{props.isLoading ? (
<CircularProgress sx={{ 'align-self': 'flex-end' }} size={20} />
<CircularProgress sx={{ alignSelf: 'flex-end' }} size={20} />
) : null}
</Box>

Expand Down
Loading