Skip to content
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

fix: add token claims per network #4046

Merged
merged 2 commits into from
Oct 17, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ADDRESSES } from '@oasisdex/addresses'

Check failure on line 1 in features/omni-kit/components/details-section/OmniDetailSectionRewardsClaims.tsx

View workflow job for this annotation

GitHub Actions / Linter

Run autofix to sort these imports!

Check failure on line 1 in features/omni-kit/components/details-section/OmniDetailSectionRewardsClaims.tsx

View workflow job for this annotation

GitHub Actions / Linter

Run autofix to sort these imports!
import { Network } from '@oasisdex/dma-library'
import { networkIdToLibraryNetwork } from 'actions/aave-like/helpers'
import type BigNumber from 'bignumber.js'
Expand All @@ -15,8 +15,22 @@

import { OmniDetailsSectionContentRewardsLoadingState } from './OmniDetailsSectionContentRewardsLoadingState'
import { OmniRewardsClaims } from './OmniRewardsClaims'
import { NetworkIds } from 'blockchain/networks'

const claimableErc20 = ['ENA', 'SENA']
const claimableErc20: Record<NetworkIds, string[]> = {
[NetworkIds.MAINNET]: ['ENA', 'SENA'],
[NetworkIds.OPTIMISMMAINNET]: [],
[NetworkIds.ARBITRUMMAINNET]: [],
[NetworkIds.BASEMAINNET]: [],
[NetworkIds.POLYGONMAINNET]: [],
[NetworkIds.POLYGONMUMBAI]: [],
[NetworkIds.BASEGOERLI]: [],
[NetworkIds.EMPTYNET]: [],
[NetworkIds.GOERLI]: [],
[NetworkIds.HARDHAT]: [],
[NetworkIds.ARBITRUMGOERLI]: [],
[NetworkIds.OPTIMISMGOERLI]: [],
}

type Claim = {
token: string
Expand All @@ -36,7 +50,7 @@
useEffect(() => {
if (dpmProxy) {
// Existing ERC20 claims logic
claimableErc20.forEach((token) => {
claimableErc20[networkId].forEach((token) => {
Copy link
Contributor

@coderabbitai coderabbitai bot Oct 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Handle Undefined claimableErc20[networkId] to Prevent Runtime Errors

If networkId is not a key in the claimableErc20 object, accessing claimableErc20[networkId] will return undefined, leading to a runtime error when calling forEach.

Apply this diff to ensure safe access and prevent potential runtime errors:

-          claimableErc20[networkId].forEach((token) => {
+          (claimableErc20[networkId] || []).forEach((token) => {

This modification defaults to an empty array when claimableErc20[networkId] is undefined, ensuring that the forEach method always functions correctly, even for unrecognized networks.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
claimableErc20[networkId].forEach((token) => {
(claimableErc20[networkId] || []).forEach((token) => {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok I'll agree. But don't get cocky. Thats only because I like you. If somone modifies the NetwrokIds and there's a cese we use it as index - that should be caught by ts

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@halaprix, glad to hear that you've agreed to the change! You're absolutely right, TypeScript should catch such cases if someone modifies the NetworkIds. Thank you!

🐰😊


✏️ Learnings added
Learnt from: halaprix
PR: OasisDEX/oasis-borrow#4046
File: features/omni-kit/components/details-section/OmniDetailSectionRewardsClaims.tsx:53-53
Timestamp: 2024-10-17T18:29:27.516Z
Learning: In TypeScript code, we rely on TypeScript's type checking to catch cases where `NetworkIds` are modified and used as an index, ensuring that accessing `claimableErc20[networkId]` is safe.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.


If you found this review helpful, would you consider giving us a shout-out on X?
Thank you for using CodeRabbit!

tokenBalance({ token, account: dpmProxy, networkId: networkId })
.then((balance) => {
if (balance.gt(zero)) {
Expand Down
Loading