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: export exchange rates fct #3657

Merged

Conversation

sahar-fehri
Copy link
Contributor

@sahar-fehri sahar-fehri commented Dec 13, 2023

Explanation

Export a function from assets controller to fetch token exchange rates.

This function will be used by extension to compute user token fiat amount and display it on the import token modal confirmation page.

References

Changelog

@metamask/assets-controllers

  • : Updated assetsUtil file to export fetchAndMapExchangeRates function

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

@sahar-fehri sahar-fehri requested a review from a team as a code owner December 13, 2023 11:15
@sahar-fehri sahar-fehri changed the title fix: export exchange rates fct feat: export exchange rates fct Dec 13, 2023
@sahar-fehri sahar-fehri force-pushed the feat/MMASSSETS-88-Add-TokenFiatBalance-before-import-v2 branch from aec12f9 to 584bcef Compare December 13, 2023 11:26
@sahar-fehri sahar-fehri self-assigned this Dec 13, 2023
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.

Thanks for taking care of this! I had a few comments for improvements we can make.

packages/assets-controllers/src/assetsUtil.ts Outdated Show resolved Hide resolved
packages/assets-controllers/src/assetsUtil.ts Outdated Show resolved Hide resolved
packages/assets-controllers/src/assetsUtil.ts Outdated Show resolved Hide resolved
packages/assets-controllers/src/assetsUtil.ts Outdated Show resolved Hide resolved
packages/assets-controllers/src/assetsUtil.test.ts Outdated Show resolved Hide resolved
@sahar-fehri sahar-fehri force-pushed the feat/MMASSSETS-88-Add-TokenFiatBalance-before-import-v2 branch from 584bcef to b40d809 Compare December 18, 2023 13:51
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.

A couple of comments, one about the change in codefi-v2.ts, and another for TokenRatesController, but this looks good otherwise!

@sahar-fehri sahar-fehri force-pushed the feat/MMASSSETS-88-Add-TokenFiatBalance-before-import-v2 branch from c6b1de6 to eaa67e6 Compare December 19, 2023 23:46
mcmire
mcmire previously approved these changes Dec 20, 2023
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.

This makes sense to me, thanks! I'll approve it now though I'll keep tabs on it in case #3687 forces more changes.

* The maximum number of token addresses that should be sent to the Price API in
* a single request.
*/
export const TOKEN_PRICES_BATCH_SIZE = 100;
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be a package-level export as well? I assume it's only used inside of assets-controllers, but just wanted to check.

Copy link
Contributor

Choose a reason for hiding this comment

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

I had requested that it be moved from TokenRatesController so it wasn't a package-level export, as I didn't think anyone needed to refer to this. But I guess I made an assumption, so that's a fair question — do we anticipate this being used in client code?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For now i do not expect extension to be using this.
You guys think its fine to keep it in assetsUtil.ts in that case?

Copy link
Member

Choose a reason for hiding this comment

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

I don't anticipate that we'd need this anywhere else either.

It seems more appropriate for this to be part of the service itself, rather than here or in the controller. This choice in batch size is a recommendation specific to the CodeFi V2 price API in particular. It would be ideal if we could attach this batch size to the service instead.

Copy link
Contributor

Choose a reason for hiding this comment

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

This constant exists here because the batching code is outside of the service at the moment. Perhaps we can integrate that better in another PR?

MajorLift
MajorLift previously approved these changes Dec 20, 2023
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.

Looks good!!

@sahar-fehri sahar-fehri dismissed stale reviews from MajorLift and mcmire via 258edef December 21, 2023 11:53
@sahar-fehri sahar-fehri force-pushed the feat/MMASSSETS-88-Add-TokenFiatBalance-before-import-v2 branch 2 times, most recently from aba4038 to cca73cc Compare December 21, 2023 11:58
* @param args.chainId - The chainId of the tokens.
* @returns The prices for the requested tokens.
*/
export async function fetchTokenContractExchangeRates({
Copy link
Member

Choose a reason for hiding this comment

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

Have you considered incorporating this into the token price service itself? That would let us remove the duplication between here and the controller.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry — this was my recommendation to bring over the batching code from the controller. My thought was that we could refactor the controller later to use this function, but at least at the moment, if the batching were in this function too, the behavior would be the same regardless of whether you used this function or the controller. Wondering if we could take care of moving the batching in another PR? That should be a non-breaking change.

Copy link
Member

Choose a reason for hiding this comment

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

Well, it might result in breaking changes, depending on how we merge the two. If we move it into the service, the other methods on the serve might no longer be needed, letting us remove those from the API. Though of course we could do that in two steps.

And overall yes I agree, we can merge this and refactor it later.

Copy link
Member

Choose a reason for hiding this comment

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

Just created an issue to track this suggestion: #4117

Gudahtt
Gudahtt previously approved these changes Dec 21, 2023
Copy link
Member

@Gudahtt Gudahtt 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 left some suggestions, one of which might require substantial changes but would leave this in a state that is easier to maintain (moving this into the token service). But I don't object to merging this as-is if you'd prefer.

@sahar-fehri sahar-fehri force-pushed the feat/MMASSSETS-88-Add-TokenFiatBalance-before-import-v2 branch from cca73cc to 6634b1c Compare December 21, 2023 17:58
@sahar-fehri sahar-fehri force-pushed the feat/MMASSSETS-88-Add-TokenFiatBalance-before-import-v2 branch from 6634b1c to 6c474eb Compare December 21, 2023 18:03
@sahar-fehri sahar-fehri merged commit cd78c1f into main Dec 21, 2023
136 checks passed
@sahar-fehri sahar-fehri deleted the feat/MMASSSETS-88-Add-TokenFiatBalance-before-import-v2 branch December 21, 2023 18:29
sahar-fehri added a commit to MetaMask/metamask-extension that referenced this pull request Jan 8, 2024
## **Description**

The goal of this PR is to fetch user token fiat balance on the import
modal confirmation page.
There is an ongoing PR on core to enable this:
MetaMask/core#3657



## **Related issues**

Fixes: https://consensyssoftware.atlassian.net/browse/MMASSETS-88
also design wise relates to this PR
#21704

## **Manual testing steps**

1. Go to home page
2. Click on import tokens button
3. Select a token
4. Click import
5. Notice the token balance in fiat on the confirmation page

## **Screenshots/Recordings**

<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->

### **Before**


![image](https://github.com/MetaMask/metamask-extension/assets/10994169/a7513ded-2960-4c6d-972d-8bb1d673f0df)
### **After**


![image](https://github.com/MetaMask/metamask-extension/assets/10994169/1232ba09-9769-476d-b4f8-12415ac55f2c)


## **Pre-merge author checklist**

- [] I’ve followed [MetaMask Coding
Standards](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/CODING_GUIDELINES.md).
- [ ] I've clearly explained what problem this PR is solving and how it
is solved.
- [ ] I've linked related issues
- [ ] I've included manual testing steps
- [ ] I've included screenshots/recordings if applicable
- [ ] I’ve included tests if applicable
- [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [ ] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.
- [ ] I’ve properly set the pull request status:
  - [ ] In case it's not yet "ready for review", I've set it to "draft".
- [ ] In case it's "ready for review", I've changed it from "draft" to
"non-draft".

## **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.
k-g-j pushed a commit to MetaMask/metamask-extension that referenced this pull request Jan 8, 2024
## **Description**

The goal of this PR is to fetch user token fiat balance on the import
modal confirmation page.
There is an ongoing PR on core to enable this:
MetaMask/core#3657



## **Related issues**

Fixes: https://consensyssoftware.atlassian.net/browse/MMASSETS-88
also design wise relates to this PR
#21704

## **Manual testing steps**

1. Go to home page
2. Click on import tokens button
3. Select a token
4. Click import
5. Notice the token balance in fiat on the confirmation page

## **Screenshots/Recordings**

<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->

### **Before**


![image](https://github.com/MetaMask/metamask-extension/assets/10994169/a7513ded-2960-4c6d-972d-8bb1d673f0df)
### **After**


![image](https://github.com/MetaMask/metamask-extension/assets/10994169/1232ba09-9769-476d-b4f8-12415ac55f2c)


## **Pre-merge author checklist**

- [] I’ve followed [MetaMask Coding
Standards](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/CODING_GUIDELINES.md).
- [ ] I've clearly explained what problem this PR is solving and how it
is solved.
- [ ] I've linked related issues
- [ ] I've included manual testing steps
- [ ] I've included screenshots/recordings if applicable
- [ ] I’ve included tests if applicable
- [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [ ] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.
- [ ] I’ve properly set the pull request status:
  - [ ] In case it's not yet "ready for review", I've set it to "draft".
- [ ] In case it's "ready for review", I've changed it from "draft" to
"non-draft".

## **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.
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