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

feat: add balance check before showing transaction buttons #78

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
52 changes: 35 additions & 17 deletions src/components/chat/transactions/EvmTxCard.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { EthTransactionParams, Network, SignRequestData } from "near-safe";
import { useEffect, useState } from "react";
import { formatEther } from "viem";
import { useBalance } from "wagmi";
import { useTransaction } from "../../../hooks/useTransaction";
import { useWindowSize } from "../../../hooks/useWindowSize";
import { shortenString } from "../../../lib/utils";
Expand Down Expand Up @@ -36,6 +37,12 @@ export const EvmTxCard = ({
const [txHash, setTxHash] = useState<string | undefined>();
const { evmAddress, evmWallet, chainId } = useAccount();

const formattedEvmAddress =
evmAddress && evmAddress.startsWith("0x")
? (evmAddress as `0x${string}`)
: undefined;
const { data } = useBalance({ address: formattedEvmAddress });

if (!evmData)
return (
<p className='bitte-my-4 bitte-overflow-auto bitte-text-center'>
Expand Down Expand Up @@ -90,7 +97,7 @@ export const EvmTxCard = ({
style={{
backgroundColor: messageBackgroundColor,
borderColor: borderColor,
color: textColor
color: textColor,
}}
>
<CardHeader
Expand Down Expand Up @@ -189,23 +196,34 @@ export const EvmTxCard = ({
accountId={evmAddress}
/>
) : null}
{!isLoading && !errorMsg && !txHash ? (
<CardFooter className='bitte-flex bitte-items-center bitte-gap-6'>
<>
<Button variant='outline' className='bitte-w-1/2'>
Decline
</Button>
{data?.value &&
data?.value >
evmData.params.reduce(
(acc, transaction) => acc + BigInt(transaction.value || 0),
BigInt(0)
) ? (
!isLoading && !errorMsg && !txHash ? (
<CardFooter className='bitte-flex bitte-items-center bitte-gap-6'>
<>
<Button variant='outline' className='bitte-w-1/2'>
Decline
</Button>

<Button
className='bitte-w-1/2'
onClick={handleSmartAction}
disabled={isLoading}
>
{isLoading ? "Confirming..." : "Approve"}
</Button>
</>
</CardFooter>
) : null}
<Button
className='bitte-w-1/2'
onClick={handleSmartAction}
disabled={isLoading}
>
{isLoading ? "Confirming..." : "Approve"}
</Button>
</>
</CardFooter>
) : null
) : (
<p className='bitte-text-red-300 my-4 px-6 bitte-text-sm bitte-text-center'>
Not enough funds available to complete the transaction.
</p>
)}
</Card>
);
};
Expand Down
40 changes: 23 additions & 17 deletions src/components/chat/transactions/ReviewTransaction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -229,23 +229,29 @@ export const ReviewTransaction = ({
{result && !loading ? (
<TransactionResult result={result} accountId={accountId} />
) : null}
{!loading && !result && !errorMsg && accountId ? (
<CardFooter className='bitte-flex bitte-items-center bitte-gap-6'>
<>
<Button variant='outline' className='bitte-w-1/2'>
Decline
</Button>

<Button
variant='default'
className='bitte-w-1/2'
onClick={handleSmartAction}
>
Approve
</Button>
</>
</CardFooter>
) : null}
{balance && balance > totalDeposit ? (
!loading && !result && !errorMsg && accountId ? (
<CardFooter className='bitte-flex bitte-items-center bitte-gap-6'>
<>
<Button variant='outline' className='bitte-w-1/2'>
Decline
</Button>

<Button
variant='default'
className='bitte-w-1/2'
onClick={handleSmartAction}
>
Approve
</Button>
</>
</CardFooter>
) : null
) : (
<p className='bitte-text-red-300 my-4 px-6 bitte-text-sm bitte-text-center'>
Not enough funds available to complete the transaction.
</p>
)}
</Card>
);
};