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

Commit 30e5a24

Browse files
Fix: Prevented incorrect error handling on getting token balances and corrected cGHS address for Alfajores (#205)
### Description We couldn't get balances on Alfajores because of the incorrect error handling on getting token balances - it was trying to get the balance of the token with the address that is not correct (cGHS, this way we faced the issue). Now, I replaced it with `try catch` block to prevent throwing an error when there's an issue with some of the tokens. Instead, we load all the tokens we can and display the responsive console error to make it more understandable and prevent problems like this. ### Other changes Updated address for the 'cGHS' token. ### Tested While token/s addresses are correct. 1. Navigate to the preview and connect your wallet where you have tokens on the Alfajores. 2. Change the network to Alfajores. 3. Wait until the balances are loaded. While token/s addresses are not correct 1. Check out the branch and change any token address to an incorrect one. 2. Run locally and connect your wallet where you have tokens on the Alfajores. 3. Change the network to Alfajores. 4. Wait until the warning notification toast is loaded and the console error is displayed. ### Related issues - Fixes #issue number here ### Checklist before requesting a review - [ ] I have performed a self-review of my own code - [ ] I have added tests that prove my fix is effective or that my feature works - [ ] The PR title follows the [conventions](https://www.notion.so/Git-Branching-and-Commit-Message-Conventions-18f66f7d06444cfcbac5725ffbc7c04a?pvs=4#9355048863c549ef92fe210a8a1298aa) - [ ] I have run the [regression tests](https://www.notion.so/Mento-Web-App-Regression-Tests-37bd43a7da8d4e38b65993320a33d557) Co-authored-by: chapati <philip.paetz@me.com>
1 parent acdc35a commit 30e5a24

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)