Skip to content

Commit

Permalink
fix: get token balance by wagmi
Browse files Browse the repository at this point in the history
  • Loading branch information
galvin96 committed Jan 10, 2025
1 parent aaa16e4 commit 6d8f6a4
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/constants/addresses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { FACTORY_ADDRESS } from '@ixswap1/v2-sdk'
import { constructSameAddressMap } from '../utils/constructSameAddressMap'
import { SupportedChainId } from './chains'
import { isProd } from 'utils/isEnvMode'
import { Address } from 'viem'

type ContractAddressRecord = Record<number, string>

Expand Down Expand Up @@ -75,7 +76,7 @@ export const TGE_CHAINS_WITH_KYC = ENV_SUPPORTED_TGE_CHAINS || [
SUPPORTED_TGE_CHAINS.BASE_SEPOLIA,
]
// the rest are same as kovan for now
export const IXS_ADDRESS: { [key: number]: string } = {
export const IXS_ADDRESS: { [key: number]: Address } = {
[1]: '0x73d7c860998CA3c01Ce8c808F5577d94d545d1b4',
[4]: '0xA1997c88a60dCe7BF92A3644DA21e1FfC8F96dC2',
[3]: '0xA1997c88a60dCe7BF92A3644DA21e1FfC8F96dC2',
Expand Down
17 changes: 12 additions & 5 deletions src/pages/DexV2/Lock/components/CurrencyInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import { Input as NumericalInput } from 'components/NumericalInput'
import { useActiveWeb3React } from 'hooks/web3'
import { Trans } from '@lingui/macro'
import useTheme from 'hooks/useTheme'
import { formatCurrencyAmount } from 'utils/formatCurrencyAmount'
import { formatCurrencySymbol } from 'utils/formatCurrencySymbol'
import { useCurrencyBalance } from 'state/wallet/hooks'
import { useCurrencyBalanceV2 } from 'state/wallet/hooks'
import { TYPE } from 'theme'
import { AssetLogo } from 'components/CurrencyInputPanel/AssetLogo'
import { FiatValue } from 'components/CurrencyInputPanel/FiatValue'
import { Flex } from 'rebass'
import { Button } from '@mui/material'
import { IXS_ADDRESS } from 'constants/addresses'
import { fNum } from 'lib/balancer/utils/numbers'

interface CurrencyInputPanelProps {
value: string
Expand All @@ -33,8 +34,14 @@ export default function CurrencyInputPanel({
priceImpact,
...rest
}: CurrencyInputPanelProps) {
const { account } = useActiveWeb3React()
const selectedCurrencyBalance = useCurrencyBalance(account ?? undefined, currency ?? undefined)
const { account, chainId } = useActiveWeb3React()
const currencyBalance = useCurrencyBalanceV2({
account,
chainId,
currency: IXS_ADDRESS[chainId ?? 1],
})
const selectedCurrencyBalance = currencyBalance?.formatted

const theme = useTheme()
const decimals = currency?.tokenInfo?.decimals || 18
const onChangeInput = (val: string) => {
Expand Down Expand Up @@ -93,7 +100,7 @@ export default function CurrencyInputPanel({
<Trans>
Balance:
<span style={{ color: '#292933', fontWeight: 600, marginLeft: 4 }}>
{formatCurrencyAmount(selectedCurrencyBalance, decimals)} {currency.symbol}
{fNum('token', selectedCurrencyBalance)}
</span>
</Trans>
) : null}
Expand Down
32 changes: 31 additions & 1 deletion src/state/wallet/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Currency, Token, CurrencyAmount, Ether } from '@ixswap1/sdk-core'
import JSBI from 'jsbi'
import { useMemo, useState, useEffect } from 'react'
import { useMemo, useState } from 'react'
import { useActiveWeb3React } from '../../hooks/web3'
import { useAllTokens } from '../../hooks/Tokens'
import { useMulticall2Contract, useTokenContract } from '../../hooks/useContract'
Expand All @@ -11,6 +11,9 @@ import ERC20ABI from 'abis/erc20.json'
import { Erc20Interface } from 'abis/types/Erc20'
import { useSelector } from 'react-redux'
import { AppState } from 'state'
import { Address } from 'viem'
import { wagmiConfig } from 'components/Web3Provider'
import { getBalance, GetBalanceReturnType } from '@wagmi/core'

/**
* Returns a map of the given addresses to their eventually consistent ETH balances.
Expand Down Expand Up @@ -164,3 +167,30 @@ export const useWalletState = () => {

return walletState;
};

export const useCurrencyBalanceV2 = ({
currency,
chainId,
account,
}: {
currency: Address,
chainId: number,
account: Address,
}) => {
const [balance, setBalance] = useState<GetBalanceReturnType | undefined>(undefined)

useMemo(async () => {
if (!currency || !account) return
const amount = await getBalance(
wagmiConfig,
{
address: account,
chainId,
token: currency,
}
)
setBalance(amount)
}, [currency, account])

return balance
}

0 comments on commit 6d8f6a4

Please sign in to comment.