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 token detection fallback #4928

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
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
25 changes: 8 additions & 17 deletions packages/assets-controllers/src/TokenDetectionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -707,20 +707,14 @@ export class TokenDetectionController extends StaticIntervalPollingController<To
supportedNetworks,
);

// If API succeeds and no chains are left for RPC detection, we can return early
if (
apiResult?.result === 'success' &&
chainsToDetectUsingRpc.length === 0
) {
return;
// If the account API call failed, have those chains fall back to RPC detection
if (apiResult?.result === 'failed') {
this.#addChainsToRpcDetection(
chainsToDetectUsingRpc,
chainsToDetectUsingAccountAPI,
clientNetworks,
);
}

// If API fails or chainsToDetectUsingRpc still has items, add chains to RPC detection
this.#addChainsToRpcDetection(
Copy link
Contributor Author

@bergeron bergeron Nov 14, 2024

Choose a reason for hiding this comment

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

This seems like it was just adding all the account API chains, so it was doing an additional, redundant RPC based detection even when the account api just succeeded for those chains.

The above condition is not sufficient to short circuit, when you have a mix of chains that do and do not support account api.

If we want to keep an rpc based fallback if the account API fails, we could instead make the condition something like:

if (apiResult?.result === 'failed') {
  this.#addChainsToRpcDetection(...)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Let me know if either of these fixes makes sense to you @salimtb

Copy link
Contributor

@salimtb salimtb Nov 14, 2024

Choose a reason for hiding this comment

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

yes, i got the idea and the fix makes sense to me. Just to confirm, given that the unit test is currently failing, with this fix, do we revert to the RPC method if the call to the account API fails or not called ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I updated it to keep the RPC fallback if the account API request fails.

chainsToDetectUsingRpc,
chainsToDetectUsingAccountAPI,
clientNetworks,
);
}

// Proceed with RPC detection if there are chains remaining in chainsToDetectUsingRpc
Expand Down Expand Up @@ -822,10 +816,7 @@ export class TokenDetectionController extends StaticIntervalPollingController<To
.getMultiNetworksBalances(selectedAddress, chainIds, supportedNetworks)
.catch(() => null);

if (
!tokenBalancesByChain ||
Object.keys(tokenBalancesByChain).length === 0
) {
if (tokenBalancesByChain === null) {
Copy link
Contributor Author

@bergeron bergeron Nov 14, 2024

Choose a reason for hiding this comment

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

This used to return failed if the api response was empty. But that's a valid response if it's an empty account with no tokens. Now it returns failed if the request is not http 200

return { result: 'failed' } as const;
}

Expand Down
Loading