Skip to content

Commit

Permalink
fix(vault): refetch all balances
Browse files Browse the repository at this point in the history
  • Loading branch information
gitwoz committed Dec 18, 2024
1 parent d81ab24 commit 1a037a5
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 18 deletions.
3 changes: 3 additions & 0 deletions src/common/enums/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ export const SUPPORT_SUCCESS = 'supportSuccess'
export const SUPPORT_SUCCESS_USDT_VISITOR = 'supportSuccessUSDTVisitor'
export const SUPPORT_SUCCESS_ANIMATION = 'supportSuccessAnimation'

// Wallet
export const REFETCH_BALANCE_USDT = 'refetchBalanceUSDT'

// Universal auth
export enum UNIVERSAL_AUTH_TRIGGER {
appreciation = 'appreciation',
Expand Down
18 changes: 7 additions & 11 deletions src/components/Dialogs/WithdrawVaultUSDTDialog/Confirming.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@ import gql from 'graphql-tag'
import { useContext, useEffect, useState } from 'react'
import { FormattedMessage } from 'react-intl'

import { OPEN_WITHDRAW_VAULT_USDT_DIALOG, PATHS } from '~/common/enums'
import { formatAmount, truncate } from '~/common/utils'
import {
Dialog,
Spinner,
toast,
useRoute,
useVaultBalanceUSDT,
ViewerContext,
} from '~/components'
OPEN_WITHDRAW_VAULT_USDT_DIALOG,
PATHS,
REFETCH_BALANCE_USDT,
} from '~/common/enums'
import { formatAmount, truncate } from '~/common/utils'
import { Dialog, Spinner, toast, useRoute, ViewerContext } from '~/components'
import {
TransactionState,
WithdrawVaultUsdtMutation,
Expand Down Expand Up @@ -57,7 +54,6 @@ const Confirming: React.FC<ConfirmingProps> = ({ amount, closeDialog }) => {
const { router } = useRoute()
const viewer = useContext(ViewerContext)
const address = viewer.info.ethAddress!
const { refetch: refetchVaultBalanceUSDT } = useVaultBalanceUSDT()

const [withdraw] = useMutation<WithdrawVaultUsdtMutation>(WITHDRAW_VAULT_USDT)
const [txId, setTxId] = useState<string | null>(null)
Expand Down Expand Up @@ -111,7 +107,7 @@ const Confirming: React.FC<ConfirmingProps> = ({ amount, closeDialog }) => {
],
})

refetchVaultBalanceUSDT()
window.dispatchEvent(new CustomEvent(REFETCH_BALANCE_USDT, {}))

closeDialog()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const Tips = () => {
const viewer = useContext(ViewerContext)
const { lang } = useContext(LanguageContext)
const { address } = useAccount()
const { data: balanceUSDTData } = useBalanceUSDT({})
const { data: balanceUSDTData } = useBalanceUSDT()

const isZeroBalance = balanceUSDTData?.value && balanceUSDTData?.value < 0n
const hasEthAddress = !!viewer.info.ethAddress
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const USDTChoice: React.FC<FormProps> = ({
const { address } = useAccount()

const { data: balanceUSDTData, isLoading: balanceUSDTLoading } =
useBalanceUSDT({})
useBalanceUSDT()
const balanceUSDT = parseFloat(balanceUSDTData?.formatted || '0')

const curatorAddress = viewer.info.ethAddress
Expand Down
2 changes: 1 addition & 1 deletion src/components/Hook/useERC20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const useBalanceUSDT = ({
address: addr,
}: {
address?: string | null
}) => {
} = {}) => {
const viewer = useContext(ViewerContext)
const viewerEthAddress = viewer.info.ethAddress
const targetNetwork = featureSupportedChains.curation[0]
Expand Down
21 changes: 17 additions & 4 deletions src/views/Me/Wallet/Balance/USDT.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
contract,
OPEN_WITHDRAW_VAULT_USDT_DIALOG,
PATHS,
REFETCH_BALANCE_USDT,
} from '~/common/enums'
import { formatAmount } from '~/common/utils'
import {
Expand All @@ -19,6 +20,7 @@ import {
TextIcon,
Translate,
useBalanceUSDT,
useEventListener,
useVaultBalanceUSDT,
ViewerContext,
} from '~/components'
Expand All @@ -34,10 +36,16 @@ interface USDTBalanceProps {
export const USDTBalance = ({ currency, exchangeRate }: USDTBalanceProps) => {
const viewer = useContext(ViewerContext)
const address = viewer.info.ethAddress
const { data: balanceUSDTData, isLoading: balanceUSDTLoading } =
useBalanceUSDT({})
const { data: vaultBalanceUSDTData, isLoading: vaultBalanceUSDTLoading } =
useVaultBalanceUSDT()
const {
data: balanceUSDTData,
refetch: refetchBalanceUSDT,
isLoading: balanceUSDTLoading,
} = useBalanceUSDT({})
const {
data: vaultBalanceUSDTData,
refetch: refetchVaultBalanceUSDT,
isLoading: vaultBalanceUSDTLoading,
} = useVaultBalanceUSDT()
const balanceUSDT = parseFloat(balanceUSDTData?.formatted || '0')
const vaultBalanceUSDT = parseFloat(
formatUnits(
Expand All @@ -59,6 +67,11 @@ export const USDTBalance = ({ currency, exchangeRate }: USDTBalanceProps) => {
window.dispatchEvent(new CustomEvent(OPEN_WITHDRAW_VAULT_USDT_DIALOG, {}))
}

useEventListener(REFETCH_BALANCE_USDT, () => {
refetchBalanceUSDT()
refetchVaultBalanceUSDT()
})

if (!address && !vaultBalanceUSDT) {
return (
<section className={classes}>
Expand Down

0 comments on commit 1a037a5

Please sign in to comment.