From 4356d1efcf7fa59d7676fb03417b6b24ecdd4ad1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Torres?= <26875009+luistorres@users.noreply.github.com> Date: Mon, 29 Apr 2024 09:38:17 +0100 Subject: [PATCH] Add USDC balance & dashboard (#225) Why: - We need to display the token payment balance instead of sepolia ETH - We want to display live values on dashboard --- packages/web-app/app/_lib/actions.tsx | 1 - packages/web-app/app/_lib/queries.tsx | 40 ++++++++++ .../acquire-access-grant-button.tsx | 6 +- .../components/dialogs/apply-dialog/index.tsx | 2 +- .../dialogs/settings-dialog/balance.tsx | 17 ++-- .../app/_ui/components/wallet-button.tsx | 23 +++--- .../web-app/app/_ui/project/token-metrics.tsx | 80 +++++++++++++++---- yarn.lock | 5 -- 8 files changed, 127 insertions(+), 47 deletions(-) rename packages/web-app/app/_ui/components/dialogs/{ => apply-dialog}/acquire-access-grant-button.tsx (94%) diff --git a/packages/web-app/app/_lib/actions.tsx b/packages/web-app/app/_lib/actions.tsx index b9b809bb..28f41742 100644 --- a/packages/web-app/app/_lib/actions.tsx +++ b/packages/web-app/app/_lib/actions.tsx @@ -204,7 +204,6 @@ export const useSignDelegatedAccessGrant = ( }; export const useContributeToCtznd = (address: `0x${string}`) => { - const [state, setState] = useState(); const [amount, setAmount] = useState(0); const amountInWei = useMemo(() => parseEther(amount.toString()), [amount]); const { data: merkleProof } = useFetchMerkleProof(); diff --git a/packages/web-app/app/_lib/queries.tsx b/packages/web-app/app/_lib/queries.tsx index 57cc19be..af7e9cda 100644 --- a/packages/web-app/app/_lib/queries.tsx +++ b/packages/web-app/app/_lib/queries.tsx @@ -12,6 +12,9 @@ import { getServerPublicInfo, } from '../_server/info'; import { fetchAndGenerateProof } from '../_server/projects/generate-merkle-root'; +import { useAccount, useBalance } from 'wagmi'; +import { useReadCtzndSalePaymentToken } from '@/wagmi.generated'; +import { formatEther } from 'viem'; export const usePublicInfo = () => { return useQuery({ @@ -211,3 +214,40 @@ export const useFetchMerkleProof = () => { enabled: !!address, }); }; + +export const usePaymentTokenBalance = () => { + const { address } = useAccount(); + const { + data: paymentToken, + isLoading: isLoadingToken, + error: errorToken, + } = useReadCtzndSalePaymentToken(); + + const { + data: balance, + isLoading: isLoadingBalance, + error: errorBalance, + } = useBalance({ + token: paymentToken, + address, + query: { + enabled: !!paymentToken, + }, + }); + + if (!paymentToken) { + return { + data: null, + formattedValue: null, + isLoading: isLoadingToken, + error: errorToken, + }; + } + + return { + data: balance, + formattedValue: balance ? formatEther(balance.value) : null, + isLoading: isLoadingBalance || isLoadingToken, + error: errorToken || errorBalance, + }; +}; diff --git a/packages/web-app/app/_ui/components/dialogs/acquire-access-grant-button.tsx b/packages/web-app/app/_ui/components/dialogs/apply-dialog/acquire-access-grant-button.tsx similarity index 94% rename from packages/web-app/app/_ui/components/dialogs/acquire-access-grant-button.tsx rename to packages/web-app/app/_ui/components/dialogs/apply-dialog/acquire-access-grant-button.tsx index 3d6eecbf..d3e2942c 100644 --- a/packages/web-app/app/_ui/components/dialogs/acquire-access-grant-button.tsx +++ b/packages/web-app/app/_ui/components/dialogs/apply-dialog/acquire-access-grant-button.tsx @@ -4,9 +4,9 @@ import { useSignDelegatedAccessGrant } from '@/app/_lib/actions'; import { useTransaction } from 'wagmi'; import { useKyc } from '@/app/_providers/kyc/context'; import { useEffect } from 'react'; -import { Spinner } from '../svg/spinner'; -import { Check } from '../svg/check'; -import { Error } from '../svg/error'; +import { Spinner } from '../../svg/spinner'; +import { Check } from '../../svg/check'; +import { Error } from '../../svg/error'; import { arbitrum, arbitrumSepolia } from 'viem/chains'; type AcquireAccessGrantButton = { diff --git a/packages/web-app/app/_ui/components/dialogs/apply-dialog/index.tsx b/packages/web-app/app/_ui/components/dialogs/apply-dialog/index.tsx index f31b4fce..156622e3 100644 --- a/packages/web-app/app/_ui/components/dialogs/apply-dialog/index.tsx +++ b/packages/web-app/app/_ui/components/dialogs/apply-dialog/index.tsx @@ -3,7 +3,7 @@ import { useProjectPublicInfo, usePublicInfo } from '@/app/_lib/queries'; import { useIdOS } from '@/app/_providers/idos'; import { Dialog } from '@headlessui/react'; -import { AcquireAccessGrantButton } from '../acquire-access-grant-button'; +import { AcquireAccessGrantButton } from './acquire-access-grant-button'; import { Button } from '../..'; import { TProps, useDialog } from '@/app/_providers/dialog/context'; import { useMemo } from 'react'; diff --git a/packages/web-app/app/_ui/components/dialogs/settings-dialog/balance.tsx b/packages/web-app/app/_ui/components/dialogs/settings-dialog/balance.tsx index 7eb59cf6..85bf5f45 100644 --- a/packages/web-app/app/_ui/components/dialogs/settings-dialog/balance.tsx +++ b/packages/web-app/app/_ui/components/dialogs/settings-dialog/balance.tsx @@ -3,23 +3,22 @@ import { useAccount, useBalance } from 'wagmi'; import { Wallet } from '../../svg/wallet'; import { formatEther } from 'viem'; +import { usePaymentTokenBalance } from '@/app/_lib/queries'; export const Balance = () => { - const { address } = useAccount(); - const balance = useBalance({ - address: address, - blockTag: 'latest', - }); + const { data: balance, formattedValue } = usePaymentTokenBalance(); + + if (!balance) + return ( +
+ ); return ( <>- {balance?.data?.formatted ? formatEther(balance?.data.value) : null}{' '} - {balance.data?.symbol} -
+{`${formattedValue} ${balance.symbol}`}