-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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 detected tokens added to wrong network #22814
Changes from all commits
b4c1cb8
7b066d6
6550633
2ad1f4f
5e6b428
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 |
---|---|---|
|
@@ -65,12 +65,7 @@ export default class DetectTokensController extends StaticIntervalPollingControl | |
this.useTokenDetection = | ||
this.preferences?.store.getState().useTokenDetection; | ||
this.selectedAddress = getCurrentSelectedAccount().address; | ||
this.tokenAddresses = this.tokensController?.state.tokens.map((token) => { | ||
return token.address; | ||
}); | ||
this.setIntervalLength(interval); | ||
this.hiddenTokens = this.tokensController?.state.ignoredTokens; | ||
this.detectedTokens = this.tokensController?.state.detectedTokens; | ||
this.chainId = this.getChainIdFromNetworkStore(); | ||
this._trackMetaMetricsEvent = trackMetaMetricsEvent; | ||
|
||
|
@@ -101,15 +96,6 @@ export default class DetectTokensController extends StaticIntervalPollingControl | |
} | ||
}); | ||
|
||
tokensController?.subscribe( | ||
({ tokens = [], ignoredTokens = [], detectedTokens = [] }) => { | ||
this.tokenAddresses = tokens.map((token) => { | ||
return token.address; | ||
}); | ||
this.hiddenTokens = ignoredTokens; | ||
this.detectedTokens = detectedTokens; | ||
}, | ||
); | ||
messenger.subscribe('NetworkController:stateChange', () => { | ||
if (this.chainId !== this.getChainIdFromNetworkStore()) { | ||
const chainId = this.getChainIdFromNetworkStore(); | ||
|
@@ -169,13 +155,19 @@ export default class DetectTokensController extends StaticIntervalPollingControl | |
const tokensToDetect = []; | ||
for (const tokenAddress in tokenListUsed) { | ||
if ( | ||
!this.tokenAddresses.find((address) => | ||
!this.tokensController.state.allTokens?.[chainIdAgainstWhichToDetect]?.[ | ||
addressAgainstWhichToDetect | ||
]?.find(({ address }) => | ||
isEqualCaseInsensitive(address, tokenAddress), | ||
) && | ||
!this.hiddenTokens.find((address) => | ||
!this.tokensController.state.allIgnoredTokens?.[ | ||
chainIdAgainstWhichToDetect | ||
]?.[addressAgainstWhichToDetect]?.find((address) => | ||
isEqualCaseInsensitive(address, tokenAddress), | ||
) && | ||
!this.detectedTokens.find(({ address }) => | ||
!this.tokensController.state.allDetectedTokens?.[ | ||
chainIdAgainstWhichToDetect | ||
]?.[addressAgainstWhichToDetect]?.find(({ address }) => | ||
isEqualCaseInsensitive(address, tokenAddress), | ||
) | ||
) { | ||
|
@@ -192,6 +184,9 @@ export default class DetectTokensController extends StaticIntervalPollingControl | |
result = await this.assetsContractController.getBalancesInSingleCall( | ||
addressAgainstWhichToDetect, | ||
tokensSlice, | ||
this.network.findNetworkClientIdByChainId( | ||
chainIdAgainstWhichToDetect, | ||
), | ||
Comment on lines
+187
to
+189
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 feels like maybe the sketchiest one. We weren't passing an explicit This was the only way I found to go from chain id -> 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. Hey Brian, yeah so this is the best option available for right now. You've correctly identified an issue with this flow. We have implemented an alternative polling solution that we will be leveraging shortly but you've highlighted one piece that we're missing. @shanejonas we will need to pass along the networkClientId in the 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. created a ticket to do that cleanup: https://app.zenhub.com/workspaces/devex-63bee08a4e3b9d001108416e/issues/gh/metamask/metamask-planning/2097 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. |
||
); | ||
} catch (error) { | ||
warn( | ||
|
@@ -247,7 +242,8 @@ export default class DetectTokensController extends StaticIntervalPollingControl | |
*/ | ||
restartTokenDetection({ selectedAddress, chainId } = {}) { | ||
const addressAgainstWhichToDetect = selectedAddress ?? this.selectedAddress; | ||
const chainIdAgainstWhichToDetect = chainId ?? this.chainId; | ||
const chainIdAgainstWhichToDetect = | ||
chainId ?? this.getChainIdFromNetworkStore(); | ||
Comment on lines
+245
to
+246
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. During a network switch, |
||
if (!(this.isActive && addressAgainstWhichToDetect)) { | ||
return; | ||
} | ||
|
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.
tokensController?.subscribe
was typically the last event to fire during a chain switch. ThereforetokenAddresses
,hiddenTokens
, anddetectedTokens
were still from the old chain during detection, even when (most) everything else is pointing to the new chain. SincetokensController
has state per-chain, we can just grab the tokens for the correct chain instead of waiting to be updated via subscription.