Skip to content
This repository was archived by the owner on Aug 26, 2025. It is now read-only.

Commit cc3031e

Browse files
committed
Fix: Prevented incorrect error handling on getting token balances and corrected cGHS address for Alfajores
1 parent ff6f1a1 commit cc3031e

File tree

3 files changed

+32
-11
lines changed

3 files changed

+32
-11
lines changed

src/config/tokens.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ export const TokenAddresses: Record<ChainId, Record<TokenId, Address>> = Object.
161161
[TokenId.cKES]: '0x1E0433C1769271ECcF4CFF9FDdD515eefE6CdF92',
162162
[TokenId.PUSO]: '0x5E0E3c9419C42a1B04e2525991FB1A2C467AB8bF',
163163
[TokenId.cCOP]: '0xe6A57340f0df6E020c1c0a80bC6E13048601f0d4',
164-
[TokenId.cGHS]: '0xf419dfab059c36cbafb43a088ebeb2811f9789b9',
164+
[TokenId.cGHS]: '0x295B66bE7714458Af45E6A6Ea142A5358A6cA375',
165165
},
166166
[ChainId.Baklava]: {
167167
[TokenId.CELO]: '0xdDc9bE57f553fe75752D61606B94CBD7e0264eF8',

src/features/accounts/fetchBalances.ts

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createAsyncThunk } from '@reduxjs/toolkit'
2-
import { BigNumberish, Contract } from 'ethers'
2+
import { Contract } from 'ethers'
33
import { BALANCE_STALE_TIME } from 'src/config/consts'
44
import { TokenId, getTokenAddress, getTokenOptionsByChainId } from 'src/config/tokens'
55
import { getProvider } from 'src/features/providers'
@@ -8,6 +8,8 @@ import { validateAddress } from 'src/utils/addresses'
88
import { isStale } from 'src/utils/time'
99
import { erc20ABI } from 'wagmi'
1010

11+
import { logger } from '../../utils/logger'
12+
1113
interface FetchBalancesParams {
1214
address: string
1315
chainId: number
@@ -30,15 +32,34 @@ export const fetchBalances = createAsyncThunk<
3032
}
3133
})
3234

33-
async function _fetchBalances(address: string, chainId: number) {
35+
async function _fetchBalances(address: string, chainId: number): Promise<Record<TokenId, string>> {
3436
validateAddress(address, 'fetchBalances')
3537
const tokenBalances: Partial<Record<TokenId, string>> = {}
36-
for (const tokenId of getTokenOptionsByChainId(chainId)) {
37-
const tokenAddr = getTokenAddress(tokenId, chainId)
38-
const provider = getProvider(chainId)
39-
const tokenContract = new Contract(tokenAddr, erc20ABI, provider)
40-
const balance: BigNumberish = await tokenContract.balanceOf(address)
41-
tokenBalances[tokenId] = balance.toString()
38+
for (const tokenSymbol of getTokenOptionsByChainId(chainId)) {
39+
tokenBalances[tokenSymbol] = await getTokenBalance({ address, chainId, tokenSymbol })
4240
}
4341
return tokenBalances as Record<TokenId, string>
4442
}
43+
44+
async function getTokenBalance({
45+
address,
46+
chainId,
47+
tokenSymbol,
48+
}: IGetTokenBalance): Promise<string | undefined> {
49+
const tokenAddress = getTokenAddress(tokenSymbol, chainId)
50+
const provider = getProvider(chainId)
51+
try {
52+
const tokenContract = new Contract(tokenAddress, erc20ABI, provider)
53+
return (await tokenContract.balanceOf(address)).toString()
54+
} catch (error) {
55+
// todo: Send such error to Sentry
56+
logger.error(`Error on getting balance of '${tokenSymbol}' token.`, { error })
57+
return undefined
58+
}
59+
}
60+
61+
interface IGetTokenBalance {
62+
address: string
63+
chainId: number
64+
tokenSymbol: TokenId
65+
}

src/features/polling/PollingWorker.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ export function PollingWorker() {
2626
dispatch(fetchBalances({ address, chainId }))
2727
.unwrap()
2828
.catch((err) => {
29-
toast.warn('Error retrieving account balances')
30-
logger.error('Failed to retrieve balances', err)
29+
toast.warn(`Can't retrieve account balances.\n Try to refresh the page`)
30+
logger.error('Unexpected error on retrieving balances', err)
3131
})
3232
}
3333
}

0 commit comments

Comments
 (0)