Skip to content

Commit

Permalink
fix: fix migrated tokens showing up in legacy balance (race condition)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomquirk committed Feb 19, 2024
1 parent 023da28 commit 4271eb9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
18 changes: 12 additions & 6 deletions src/hooks/JBV3Token/contractReader/useTotalLegacyTokenBalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,21 @@ export function useTotalLegacyTokenBalance({
const v1UnclaimedBalance = useV1UnclaimedBalanceForV3Token()
const v1ClaimedBalance = v1TotalBalance?.sub(v1UnclaimedBalance)

const { data: v2TotalBalance } = useV2TotalBalance({ projectId })
const { data: v2UnclaimedBalance } = useV2UnclaimedBalanceForV3Token({
projectId,
})
const v2ClaimedBalance =
v2TotalBalance?.sub(v2UnclaimedBalance ?? 0) ?? BigNumber.from(0)
const { data: v2TotalBalance, loading: v2BalanceLoading } = useV2TotalBalance(
{ projectId },
)
const { data: v2UnclaimedBalance, loading: unclaimedBalanceLoading } =
useV2UnclaimedBalanceForV3Token({
projectId,
})

if (v2BalanceLoading || unclaimedBalanceLoading) return BigNumber.from(0)

const totalLegacyTokenBalance = v1TotalBalance?.add(v2TotalBalance ?? 0)

const v2ClaimedBalance =
v2TotalBalance?.sub(v2UnclaimedBalance ?? 0) ?? BigNumber.from(0)

return {
v1TotalBalance,
v1UnclaimedBalance,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BigNumber } from 'ethers'
import { useWallet } from 'hooks/Wallet'
import { useUnclaimedTokenBalance } from 'hooks/v2v3/contractReader/useUnclaimedTokenBalance'
import useV2ContractReader from 'hooks/v2v3/contractReader/useV2ContractReader'
import { useJBTokenStoreForV3Token } from '../contracts/useJBTokenStoreForV3Token'

export function useV2UnclaimedBalanceForV3Token({
Expand All @@ -10,9 +11,12 @@ export function useV2UnclaimedBalanceForV3Token({
const { userAddress } = useWallet()
const v2TokenStoreContract = useJBTokenStoreForV3Token()

return useUnclaimedTokenBalance({
userAddress,
projectId,
JBTokenStore: v2TokenStoreContract,
return useV2ContractReader<BigNumber>({
contract: v2TokenStoreContract,
functionName: 'unclaimedBalanceOf',
args:
userAddress && projectId && v2TokenStoreContract
? [userAddress, projectId]
: null,
})
}
8 changes: 1 addition & 7 deletions src/hooks/v2v3/contractReader/useUnclaimedTokenBalance.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
import { BigNumber } from 'ethers'
import { bigNumbersDiff } from 'utils/bigNumbers'

import { Contract } from 'ethers'
import { V2V3ContractName } from 'models/v2v3/contracts'
import useContractReader from './useV2ContractReader'

/** Returns unclaimed balance of user with `userAddress`. */
export function useUnclaimedTokenBalance({
projectId,
userAddress,
JBTokenStore,
}: {
projectId: number | undefined
userAddress: string | undefined
JBTokenStore?: Contract
}) {
return useContractReader<BigNumber>({
contract: JBTokenStore ?? V2V3ContractName.JBTokenStore,
contract: V2V3ContractName.JBTokenStore,
functionName: 'unclaimedBalanceOf',
args:
userAddress && projectId
? [userAddress, BigNumber.from(projectId).toHexString()]
: null,
valueDidChange: bigNumbersDiff,
})
}

0 comments on commit 4271eb9

Please sign in to comment.