Skip to content

chore: refactor SwapsController so it extends from BaseControllerV2 #25681

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

Merged
merged 28 commits into from
Jul 17, 2024

Conversation

nikoferro
Copy link
Contributor

@nikoferro nikoferro commented Jul 4, 2024

Description

This PR refactors SwapsController so it extends from BaseController (v2). This refactor also involves migrating the current set of unit tests to Typescript

Open in GitHub Codespaces

Related issues

Fixes:

Manual testing steps

All tests are in the green, but the best approach would be test swaps manually end to end.

Screenshots/Recordings

No visual impact

Before

N/A

After

N/A

Pre-merge author checklist

Pre-merge reviewer checklist

  • I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed).
  • I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots.

@metamaskbot metamaskbot added the team-swaps DEPRECATED: Use "team-swaps-and-bridge" instead label Jul 4, 2024
Copy link

codecov bot commented Jul 9, 2024

Codecov Report

Attention: Patch coverage is 66.30435% with 62 lines in your changes missing coverage. Please review.

Project coverage is 69.97%. Comparing base (5ee57a6) to head (b8b7ff7).
Report is 37 commits behind head on develop.

Files Patch % Lines
app/scripts/controllers/swaps.ts 66.29% 60 Missing ⚠️
app/scripts/metamask-controller.js 33.33% 2 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop   #25681      +/-   ##
===========================================
+ Coverage    69.77%   69.97%   +0.20%     
===========================================
  Files         1376     1390      +14     
  Lines        48403    48958     +555     
  Branches     13348    13460     +112     
===========================================
+ Hits         33773    34258     +485     
- Misses       14630    14700      +70     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@nikoferro nikoferro added type-refactor needs-qa Label will automate into QA workspace labels Jul 9, 2024
@nikoferro nikoferro changed the title chore: swaps controller extending from base controller v2 chore: refactor SwapsController so it extends from BaseControllerV2 Jul 9, 2024
@nikoferro nikoferro marked this pull request as ready for review July 9, 2024 11:45
@nikoferro nikoferro requested a review from a team as a code owner July 9, 2024 11:45
@nikoferro nikoferro removed the needs-qa Label will automate into QA workspace label Jul 9, 2024
@metamaskbot
Copy link
Collaborator

Builds ready [468c385]
Page Load Metrics (62 ± 9 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint64146932010
domContentLoaded96223157
load389962199
domInteractive96223157
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 3.85 KiB (0.12%)
  • ui: 0 Bytes (0.00%)
  • common: 0 Bytes (0.00%)

@nikoferro
Copy link
Contributor Author

Manually Tested

Mainnet
ETH -> ERC20 ✅
ERC20 -> ETH ✅
ETH -> WETH ✅
WETH -> ETH ✅
ERC20 (previously approved) -> ERC20 ✅
ERC20 (that needs approval) -> ERC20 ✅

OP Stack
ETH -> ERC20 ✅
ERC20 -> ETH ✅
ETH -> WETH ✅
WETH -> ETH ✅
ERC20 (previously approved) -> ERC20 ✅
ERC20 (that needs approval) -> ERC20 ✅

@nikoferro nikoferro self-assigned this Jul 9, 2024
@nikoferro nikoferro requested a review from a team July 9, 2024 12:36
@metamaskbot
Copy link
Collaborator

Builds ready [b5ecb14]
Page Load Metrics (77 ± 20 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint632731115024
domContentLoaded997362211
load39200774220
domInteractive997362211
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 3.86 KiB (0.12%)
  • ui: 0 Bytes (0.00%)
  • common: 0 Bytes (0.00%)

@nikoferro nikoferro requested a review from MajorLift July 11, 2024 15:45
Comment on lines +912 to +913
// Private Methods
private async _fetchSwapsNetworkConfig(chainId: ChainId) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Could we rename all private properties to consistently follow the hash-prefix naming scheme?

The hash prefix is enforced by JavaScript at runtime on the syntax level, whereas the scope keywords public/protected/private are compile-time syntactic sugar that aren't enforced (we also find it unnecessary to use the public keyword for this reason).

Suggested change
// Private Methods
private async _fetchSwapsNetworkConfig(chainId: ChainId) {
// Private Methods
async #fetchSwapsNetworkConfig(chainId: ChainId) {

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 considered this during the refactor, but the problem we have is that we need to spy on "internal" methods for testing purposes. So I cannot completely enforce privacy at runtime (at least not at the moment).

The best compromise i found was to label methods as private or public so we can check this with TypeScript.

Ideally we should redesign the controller data flow so we care only about public methods output and test only that. But for now this involves work that goes a bit out of the scope of this migration to BaseController V2

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh you're right looks like there are quite a few of those spyOn calls. I'm good with refactoring the tests in a separate ticket.


export default class SwapsController extends BaseController<
Copy link
Contributor

Choose a reason for hiding this comment

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

Probably out-of-scope for this PR, but since this controller contains polling logic, could it be refactored to inherit from StaticIntervalPollingController in the future?

Copy link
Contributor Author

@nikoferro nikoferro Jul 12, 2024

Choose a reason for hiding this comment

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

Thanks for the heads up! I didn't know we had that. I see that TokenDetectionController is using it this way also. Like you said, i consider this a out of scope for this PR, but will definitively will add this on our to-do list

@nikoferro nikoferro requested a review from MajorLift July 12, 2024 09:39
@metamaskbot
Copy link
Collaborator

Builds ready [b8b7ff7]
Page Load Metrics (219 ± 218 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint6111795168
domContentLoaded96328136
load471584219455218
domInteractive96328136
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 3.86 KiB (0.12%)
  • ui: 0 Bytes (0.00%)
  • common: 0 Bytes (0.00%)

Copy link
Contributor

@MajorLift MajorLift left a comment

Choose a reason for hiding this comment

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

LGTM!

@@ -32,7 +37,7 @@ const TEST_AGG_ID_6 = 'TEST_AGG_6';
const TEST_AGG_ID_BEST = 'TEST_AGG_BEST';
const TEST_AGG_ID_APPROVAL = 'TEST_AGG_APPROVAL';

const POLLING_TIMEOUT = SECOND * 1000;
// const POLLING_TIMEOUT = SECOND * 1000;
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need to keep this here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

its part of the todo list, kept it as a comment so the linter does not complain, but i did want to keep those values around for when we refactor to extend the polling functionality from StaticIntervalPollingController

lastEthersProviderChainId,
);
});
// TODO: Re think how to test this without exposing internal state
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this need to be finished?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

not on this PR, the current design involves testing functionality that should be internal. ideally we need to separate internal logic from the public one, and only test methods that we expose as public. for this a much bigger refactor is needed and i already added a jira task for that

@nikoferro nikoferro requested a review from infiniteflower July 16, 2024 12:36
Copy link

@metamaskbot
Copy link
Collaborator

Builds ready [113e8eb]
Page Load Metrics (63 ± 6 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint6210991136
domContentLoaded11362473
load428963126
domInteractive11362473
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 3.86 KiB (0.12%)
  • ui: 0 Bytes (0.00%)
  • common: 0 Bytes (0.00%)

@nikoferro nikoferro merged commit 0c940dc into develop Jul 17, 2024
78 of 79 checks passed
@nikoferro nikoferro deleted the swaps/controller-v2 branch July 17, 2024 13:47
@github-actions github-actions bot locked and limited conversation to collaborators Jul 17, 2024
@metamaskbot metamaskbot added the release-12.3.0 Issue or pull request that will be included in release 12.3.0 label Jul 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
release-12.3.0 Issue or pull request that will be included in release 12.3.0 team-swaps DEPRECATED: Use "team-swaps-and-bridge" instead type-refactor
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants