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

feat: add earn controller package #5210

Merged
merged 22 commits into from
Jan 31, 2025
Merged

Conversation

amitabh94
Copy link
Contributor

@amitabh94 amitabh94 commented Jan 29, 2025

This PR adds the new earn controller that will support both existing products like Pooled staking and upcoming products like stablecoin lending .

This is the first version of the controller and currently initailizes the stake-sdk and implements functions to fetch staking data and update state.

References

Related to STAKE-896

Changelog

@metamask/earn-controller

  • ADDED: Initial release of the EarnController package for managing DeFi earning opportunities
  • ADDED: Support for pooled staking products with features:
    • Automatic state management for staking data
    • Real-time updates on network and account changes
    • Integration with @metamask/stake-sdk
    • Eligibility checks for staking products
  • ADDED: Support for stablecoin lending products with state management for:
    • Vault information (APY, liquidity, supply)

Checklist

  • I've updated the test suite for new or updated code as appropriate
  • I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate
  • I've highlighted breaking changes using the "BREAKING" category above as appropriate
  • I've prepared draft pull requests for clients and consumer packages to resolve any breaking changes

This addition enhances the MetaMask ecosystem by providing a dedicated controller for earning functionalities.
This commit significantly improves the test suite for EarnController by:
- Adding detailed tests for constructor initialization
- Implementing mock implementations for SDK and API services
- Covering SDK initialization and network change scenarios
- Testing data fetching and error handling methods
- Verifying subscription handlers for network and account changes
Copy link

socket-security bot commented Jan 29, 2025

New dependencies detected. Learn more about Socket for GitHub ↗︎

Package New capabilities Transitives Size Publisher
npm/@metamask/[email protected] network 0 184 kB metamaskbot

View full report↗︎

Copy link

socket-security bot commented Jan 29, 2025

👍 Dependency issues cleared. Learn more about Socket for GitHub ↗︎

This PR previously contained dependency changes with security issues that have been resolved, removed, or ignored.

Ignoring: npm/@metamask/[email protected]

View full report↗︎

Next steps

Take a deeper look at the dependency

Take a moment to review the security alert above. Review the linked package source code to understand the potential risk. Ensure the package is not malicious before proceeding. If you're unsure how to proceed, reach out to your security team or ask the Socket team for help at support [AT] socket [DOT] dev.

Remove the package

If you happen to install a dependency that Socket reports as Known Malware you should immediately remove it and select a different dependency. For other alert types, you may may wish to investigate alternative packages or consider if there are other ways to mitigate the specific risk posed by the dependency.

Mark a package as acceptable risk

To ignore an alert, reply with a comment starting with @SocketSecurity ignore followed by a space separated list of ecosystem/package-name@version specifiers. e.g. @SocketSecurity ignore npm/[email protected] or ignore all packages with @SocketSecurity ignore-all

@amitabh94
Copy link
Contributor Author

@SocketSecurity ignore npm/@metamask/[email protected]

This package is already being used in the mobile codebase adn is dedicated library for earn products

This commit enhances the EarnController by:
- Splitting data fetching into separate try-catch blocks for more granular error handling
- Improving SDK initialization error logging
- Updating method names for clarity (fetchAndUpdateStakingData -> refreshPooledStakingData)
- Adding additional test cases for error scenarios and edge cases
- Ensuring default state is always initialized for pooled staking
@amitabh94 amitabh94 marked this pull request as ready for review January 29, 2025 05:40
@amitabh94 amitabh94 enabled auto-merge (squash) January 29, 2025 05:41
@amitabh94 amitabh94 requested a review from mcmire January 29, 2025 05:41
@Matt561 Matt561 self-requested a review January 29, 2025 15:16
Matt561
Matt561 previously approved these changes Jan 29, 2025
Copy link
Contributor

@Matt561 Matt561 left a comment

Choose a reason for hiding this comment

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

LGTM! I'm excited to start using this 😄

eslint-warning-thresholds.json Outdated Show resolved Hide resolved
Copy link
Contributor

@mcmire mcmire left a comment

Choose a reason for hiding this comment

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

Hello! Since this is a new controller I wanted to make sure this controller follows the same or similar standards that we have for other controllers. It looks pretty good! I had some tips on better ways to write tests, I spotted a dependency that might not be in the right place, and I noticed a few things in the controller that you might want to take a closer look at. Let me know what you think.

packages/earn-controller/src/EarnController.ts Outdated Show resolved Hide resolved
packages/earn-controller/src/index.ts Outdated Show resolved Hide resolved
packages/earn-controller/package.json Outdated Show resolved Hide resolved
packages/earn-controller/package.json Outdated Show resolved Hide resolved
packages/earn-controller/package.json Outdated Show resolved Hide resolved
packages/earn-controller/src/EarnController.test.ts Outdated Show resolved Hide resolved
packages/earn-controller/src/EarnController.ts Outdated Show resolved Hide resolved
packages/earn-controller/src/EarnController.ts Outdated Show resolved Hide resolved
@amitabh94 amitabh94 force-pushed the STAKE-896-create-new-earn-controller branch from f92fe00 to f8272ba Compare January 29, 2025 21:54
This commit enhances the EarnController by:
- Splitting data fetching methods into separate functions for better modularity
- Implementing a more robust error handling approach in refreshPooledStakingData
- Updating test suite to match new implementation
- Improving network state change detection
- Simplifying SDK initialization and network change logic
@Matt561 Matt561 self-requested a review January 31, 2025 15:27
nickewansmith
nickewansmith previously approved these changes Jan 31, 2025
Copy link
Contributor

@nickewansmith nickewansmith left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@mcmire mcmire left a comment

Choose a reason for hiding this comment

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

Just one more question.

"baseUrl": "./",
"outDir": "./dist",
"rootDir": "./src",
"skipLibCheck": true
Copy link
Contributor

@mcmire mcmire Jan 31, 2025

Choose a reason for hiding this comment

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

Hmm, I just noticed this. Generally we do not turn this setting on because it means that this package is not being fully type-checked. Any issues that we would discover with dependencies are being suppressed here and we are likely to run into them again with clients.

Is there a reason why we are doing this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch! I believe I was looking at accounts-controller while trying to align the initial setup of these files.

I have removed it now and we might want to remove this check from some other controllers as well in a future PR.

Copy link
Contributor

@mcmire mcmire left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks for your patience in responding to my feedback!

@amitabh94 amitabh94 merged commit 2ec8c03 into main Jan 31, 2025
127 checks passed
@amitabh94 amitabh94 deleted the STAKE-896-create-new-earn-controller branch January 31, 2025 23:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants