-
Notifications
You must be signed in to change notification settings - Fork 44
refactor: remove user balance store dependency in deposit gauge reward feature #1765
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
base: fix/pool-user-balances-not-loading-correctly
Are you sure you want to change the base?
Changes from all commits
7814768
8c90ce0
a6e33d9
16df28b
c5648fa
d827f8e
8b418f3
256ab5a
ae07c60
81f339b
5e3ef1c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,4 @@ | ||
| import { enforce, group, test } from 'vest' | ||
| import useStore from '@/dex/store/useStore' | ||
| import { formatNumber } from '@ui/utils' | ||
| import { t } from '@ui-kit/lib/i18n' | ||
| import { TIME_FRAMES } from '@ui-kit/lib/model' | ||
|
|
@@ -15,20 +14,23 @@ import { AddRewardParams, DepositRewardApproveParams, DepositRewardParams } from | |
| export const gaugeAddRewardValidationGroup = ({ distributorId, rewardTokenId }: AddRewardParams) => | ||
| group('gaugeAddRewardValidationGroup', () => { | ||
| test('distributorId', () => addressValidationFn(distributorId)) | ||
|
|
||
| test('rewardTokenId', () => tokenIdValidationFn(rewardTokenId)) | ||
| }) | ||
|
|
||
| export const gaugeDepositRewardApproveValidationGroup = ({ rewardTokenId, amount }: DepositRewardApproveParams) => | ||
| export const gaugeDepositRewardApproveValidationGroup = ({ | ||
| rewardTokenId, | ||
| amount, | ||
| userBalance, | ||
| }: DepositRewardApproveParams) => | ||
| group('gaugeDepositRewardApproveValidationGroup', () => { | ||
| test('rewardTokenId', () => tokenIdValidationFn(rewardTokenId)) | ||
| test('amount', () => validateAmount({ rewardTokenId, amount })) | ||
| test('amount', () => validateAmount({ rewardTokenId, amount, userBalance })) | ||
| }) | ||
|
|
||
| export const gaugeDepositRewardValidationGroup = ({ rewardTokenId, amount, epoch }: DepositRewardParams) => | ||
| export const gaugeDepositRewardValidationGroup = ({ rewardTokenId, amount, epoch, userBalance }: DepositRewardParams) => | ||
| group('gaugeDepositRewardValidationGroup', () => { | ||
| test('rewardTokenId', () => tokenIdValidationFn(rewardTokenId)) | ||
| test('amount', () => validateAmount({ rewardTokenId, amount })) | ||
| test('amount', () => validateAmount({ rewardTokenId, amount, userBalance })) | ||
| test('epoch', () => { | ||
| enforce(epoch) | ||
| .message(t`Epoch is required`) | ||
|
|
@@ -55,18 +57,17 @@ export const gaugeDepositRewardValidationSuite = createValidationSuite((data: De | |
| gaugeDepositRewardValidationGroup(data) | ||
| }) | ||
|
|
||
| function validateAmount({ rewardTokenId, amount }: DepositRewardApproveParams) { | ||
| function validateAmount({ rewardTokenId, amount, userBalance }: DepositRewardApproveParams) { | ||
| amountValidationFn(amount) | ||
| if (!rewardTokenId || !amount) return | ||
|
|
||
| const state = useStore.getState() | ||
| const userBalancesMapper = state.userBalances.userBalancesMapper | ||
| const tokenBalance = userBalancesMapper[rewardTokenId] | ||
|
|
||
| if (!tokenBalance) return | ||
| enforce(userBalance).condition((userBalance) => ({ | ||
| pass: userBalance != null && +userBalance > 0, | ||
| message: t`Wallet balance is zero for the selected reward token`, | ||
| })) | ||
|
|
||
| enforce(amount).condition((amount) => ({ | ||
| pass: +amount < +tokenBalance, | ||
| message: t`Amount ${formatNumber(amount, { decimals: 5 })} > wallet balance ${formatNumber(tokenBalance, { decimals: 5 })}`, | ||
| pass: +amount <= +userBalance!, | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This fixes a bug where we used |
||
| message: t`Amount ${formatNumber(amount, { decimals: 5 })} > wallet balance ${formatNumber(userBalance, { decimals: 5 })}`, | ||
| })) | ||
| } | ||
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,13 +2,13 @@ import { MouseEvent, useCallback, useMemo } from 'react' | |
| import { useFormContext } from 'react-hook-form' | ||
| import { Address, isAddressEqual } from 'viem' | ||
| import { ethAddress } from 'viem' | ||
| import { useConnection } from 'wagmi' | ||
| import { | ||
| useDepositRewardApproveIsMutating, | ||
| useDepositRewardIsMutating, | ||
| useGaugeRewardsDistributors, | ||
| } from '@/dex/entities/gauge' | ||
| import { useNetworkByChain } from '@/dex/entities/networks' | ||
| import { useIsSignerConnected, useSignerAddress, useTokensBalances } from '@/dex/entities/signer' | ||
| import { type DepositRewardFormValues, DepositRewardStep } from '@/dex/features/deposit-gauge-reward/types' | ||
| import { | ||
| FlexItemAmount, | ||
|
|
@@ -26,26 +26,20 @@ import { formatNumber } from '@ui/utils' | |
| import { type TokenOption, TokenSelector } from '@ui-kit/features/select-token' | ||
| import { t } from '@ui-kit/lib/i18n' | ||
| import { useTokenUsdRates } from '@ui-kit/lib/model/entities/token-usd-rate' | ||
| import { useTokenBalances } from '@ui-kit/queries/token-balance.query' | ||
|
|
||
| export const AmountTokenInput = ({ chainId, poolId }: { chainId: ChainId; poolId: string }) => { | ||
| const { setValue, getValues, formState, watch } = useFormContext<DepositRewardFormValues>() | ||
| const rewardTokenId = watch('rewardTokenId') | ||
| const amount = watch('amount') | ||
| const epoch = watch('epoch') | ||
|
|
||
| const { data: signerAddress } = useSignerAddress() | ||
| const { data: haveSigner } = useIsSignerConnected() | ||
| const { address: signerAddress } = useConnection() | ||
| const isMaxLoading = useStore((state) => state.quickSwap.isMaxLoading) | ||
| const { | ||
| data: { networkId }, | ||
| } = useNetworkByChain({ chainId }) | ||
|
|
||
| const userBalancesMapper = useStore((state) => state.userBalances.userBalancesMapper) | ||
| const userTokens = Object.entries(userBalancesMapper) | ||
| .filter(([, balance]) => parseFloat(balance ?? '0') > 0) | ||
| .map(([address]) => address) | ||
| const { data: tokenPrices } = useTokenUsdRates({ chainId, tokenAddresses: userTokens }) | ||
|
|
||
| const { tokensMapper } = useTokensMapper(chainId) | ||
|
|
||
| const { data: rewardDistributors, isPending: isPendingRewardDistributors } = useGaugeRewardsDistributors({ | ||
|
|
@@ -56,11 +50,6 @@ export const AmountTokenInput = ({ chainId, poolId }: { chainId: ChainId; poolId | |
| const isMutatingDepositRewardApprove = useDepositRewardApproveIsMutating({ chainId, poolId, rewardTokenId, amount }) | ||
| const isMutatingDepositReward = useDepositRewardIsMutating({ chainId, poolId, rewardTokenId, amount, epoch }) | ||
|
|
||
| const { | ||
| data: [tokenBalance], | ||
| isLoading: isTokenBalancesLoading, | ||
| } = useTokensBalances([rewardTokenId]) | ||
|
|
||
| const filteredTokens = useMemo<TokenOption[]>(() => { | ||
| if (isPendingRewardDistributors || !rewardDistributors || !signerAddress) return [] | ||
|
|
||
|
|
@@ -89,6 +78,19 @@ export const AmountTokenInput = ({ chainId, poolId }: { chainId: ChainId; poolId | |
| }, [isPendingRewardDistributors, rewardDistributors, signerAddress, tokensMapper, getValues, networkId, setValue]) | ||
|
|
||
| const token = filteredTokens.find((x) => x.address === rewardTokenId) | ||
| const tokenAddresses = filteredTokens.map((t) => t.address) | ||
|
|
||
| const { data: tokenPrices } = useTokenUsdRates({ chainId, tokenAddresses }) | ||
| const { data: tokenBalances, isLoading: isTokenBalancesLoading } = useTokenBalances({ | ||
| chainId, | ||
| userAddress: signerAddress, | ||
| tokenAddresses, | ||
| }) | ||
|
|
||
| const rewardTokenBalance = useMemo( | ||
| () => rewardTokenId && tokenBalances && tokenBalances[rewardTokenId], | ||
| [rewardTokenId, tokenBalances], | ||
| ) | ||
|
|
||
| const onChangeAmount = useCallback( | ||
| (amount: string) => { | ||
|
|
@@ -109,10 +111,10 @@ export const AmountTokenInput = ({ chainId, poolId }: { chainId: ChainId; poolId | |
| const onMaxButtonClick = useCallback( | ||
| (e?: MouseEvent<HTMLButtonElement>) => { | ||
| e?.preventDefault() | ||
| if (!tokenBalance) return | ||
| setValue('amount', tokenBalance, { shouldValidate: true }) | ||
| if (!rewardTokenBalance) return | ||
| setValue('amount', rewardTokenBalance, { shouldValidate: true }) | ||
| }, | ||
| [tokenBalance, setValue], | ||
| [rewardTokenBalance, setValue], | ||
| ) | ||
|
|
||
| const isDisabled = isMutatingDepositReward || isMutatingDepositRewardApprove | ||
|
|
@@ -129,10 +131,10 @@ export const AmountTokenInput = ({ chainId, poolId }: { chainId: ChainId; poolId | |
| id="deposit-amount" | ||
| type="number" | ||
| labelProps={ | ||
| haveSigner && { | ||
| signerAddress && { | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We ought to replace |
||
| label: t`Avail.`, | ||
| descriptionLoading: isTokenBalancesLoading, | ||
| description: formatNumber(tokenBalance, { decimals: 5 }), | ||
| description: formatNumber(rewardTokenBalance), | ||
| } | ||
| } | ||
| testId="deposit-amount" | ||
|
|
@@ -154,7 +156,7 @@ export const AmountTokenInput = ({ chainId, poolId }: { chainId: ChainId; poolId | |
| selectedToken={token} | ||
| tokens={filteredTokens} | ||
| disabled={isDisabled} | ||
| balances={userBalancesMapper} | ||
| balances={tokenBalances} | ||
| tokenPrices={tokenPrices} | ||
| onToken={onChangeToken} | ||
| /> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,6 @@ | ||
| import { useEffect } from 'react' | ||
| import { FormProvider, useForm } from 'react-hook-form' | ||
| import { useConnection } from 'wagmi' | ||
| import AlertFormError from '@/dex/components/AlertFormError' | ||
| import { useGaugeRewardsDistributors } from '@/dex/entities/gauge' | ||
| import { DepositRewardDefaultValues, depositRewardValidationSuite } from '@/dex/features/deposit-gauge-reward/model' | ||
|
|
@@ -16,6 +18,7 @@ import { FormErrorsDisplay } from '@ui/FormErrorsDisplay' | |
| import { BlockSkeleton } from '@ui/skeleton' | ||
| import { FormContainer, FormFieldsContainer, GroupedFieldsContainer } from '@ui/styled-containers' | ||
| import { formDefaultOptions } from '@ui-kit/lib/model/form' | ||
| import { useTokenBalance } from '@ui-kit/queries/token-balance.query' | ||
|
|
||
| export const DepositReward = ({ chainId, poolId }: { chainId: ChainId; poolId: string }) => { | ||
| const { isPending: isPendingRewardDistributors } = useGaugeRewardsDistributors({ | ||
|
|
@@ -29,6 +32,15 @@ export const DepositReward = ({ chainId, poolId }: { chainId: ChainId; poolId: s | |
| defaultValues: DepositRewardDefaultValues, | ||
| }) | ||
|
|
||
| const rewardTokenId = methods.watch('rewardTokenId') | ||
| const { address: userAddress } = useConnection() | ||
| const { data: userBalance } = useTokenBalance({ chainId, userAddress, tokenAddress: rewardTokenId }) | ||
|
|
||
| // Sync userBalance from query into form for validation | ||
| useEffect(() => { | ||
| methods.setValue('userBalance', userBalance, { shouldValidate: true }) | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Initially I looked at vest contexts together with react form hook, but that turned out to be a disaster as contexts aren't reactive. So I applied the same trick as we did for the new borrow form, just make the user balance part of the form values. |
||
| }, [userBalance, methods]) | ||
|
|
||
| if (isPendingRewardDistributors) { | ||
| return <BlockSkeleton height={440} /> | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried something like
userBalance.greaterThan(0)but it didn't show the message. This is a bit more verbose but at least it works