From fe985d22b8a3b90c4537f035aa3a817e53af4105 Mon Sep 17 00:00:00 2001 From: Ulisses Ferreira Date: Wed, 13 Nov 2024 11:42:25 +0000 Subject: [PATCH 1/4] feat: add Solana to the polled exchange rates (#4914) ## Explanation As we aim to support more chains in the MetaMask extension we will need the exchange rates of the native tokens of these chains to properly value a user's account. Let's add Solana to the cryptocurrencies that we need to keep track of price. ## References Closes [SOL-36](https://consensyssoftware.atlassian.net/jira/software/projects/SOL/boards/718/backlog?selectedIssue=SOL-36) ## Changelog ### `@metamask/assets-controller` - feat: add Solana to the polled exchange rates ## Checklist - [x] I've updated the test suite for new or updated code as appropriate - [x] I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate - [x] I've highlighted breaking changes using the "BREAKING" category above as appropriate - [x] I've prepared draft pull requests for clients and consumer packages to resolve any breaking changes [SOL-36]: https://consensyssoftware.atlassian.net/browse/SOL-36?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ --- .../RatesController/RatesController.test.ts | 34 +++++++++++++++---- .../src/RatesController/RatesController.ts | 13 ++++++- 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/packages/assets-controllers/src/RatesController/RatesController.test.ts b/packages/assets-controllers/src/RatesController/RatesController.test.ts index bf9d0fc193..f2fca24f76 100644 --- a/packages/assets-controllers/src/RatesController/RatesController.test.ts +++ b/packages/assets-controllers/src/RatesController/RatesController.test.ts @@ -100,8 +100,14 @@ describe('RatesController', () => { const { fiatCurrency, rates, cryptocurrencies } = ratesController.state; expect(ratesController).toBeDefined(); expect(fiatCurrency).toBe('usd'); - expect(Object.keys(rates)).toStrictEqual([Cryptocurrency.Btc]); - expect(cryptocurrencies).toStrictEqual([Cryptocurrency.Btc]); + expect(Object.keys(rates)).toStrictEqual([ + Cryptocurrency.Btc, + Cryptocurrency.Solana, + ]); + expect(cryptocurrencies).toStrictEqual([ + Cryptocurrency.Btc, + Cryptocurrency.Solana, + ]); }); }); @@ -119,11 +125,16 @@ describe('RatesController', () => { const publishActionSpy = jest.spyOn(messenger, 'publish'); jest.spyOn(global.Date, 'now').mockImplementation(() => getStubbedDate()); - const mockRateValue = 57715.42; + const mockBtcRateValue = 57715.42; + const mockSolRateValue = 200.48; + const fetchExchangeRateStub = jest.fn(() => { return Promise.resolve({ btc: { - eur: mockRateValue, + eur: mockBtcRateValue, + }, + sol: { + eur: mockSolRateValue, }, }); }); @@ -144,6 +155,10 @@ describe('RatesController', () => { conversionDate: 0, conversionRate: 0, }, + sol: { + conversionDate: 0, + conversionRate: 0, + }, }); await ratesController.start(); @@ -163,7 +178,11 @@ describe('RatesController', () => { expect(ratesPosUpdate).toStrictEqual({ btc: { conversionDate: MOCK_TIMESTAMP, - conversionRate: mockRateValue, + conversionRate: mockBtcRateValue, + }, + sol: { + conversionDate: MOCK_TIMESTAMP, + conversionRate: mockSolRateValue, }, }); @@ -326,7 +345,10 @@ describe('RatesController', () => { const cryptocurrencyListPreUpdate = ratesController.getCryptocurrencyList(); - expect(cryptocurrencyListPreUpdate).toStrictEqual([Cryptocurrency.Btc]); + expect(cryptocurrencyListPreUpdate).toStrictEqual([ + Cryptocurrency.Btc, + Cryptocurrency.Solana, + ]); // Just to make sure we're updating to something else than the default list expect(cryptocurrencyListPreUpdate).not.toStrictEqual( mockCryptocurrencyList, diff --git a/packages/assets-controllers/src/RatesController/RatesController.ts b/packages/assets-controllers/src/RatesController/RatesController.ts index 7abed73eed..16588ef0d0 100644 --- a/packages/assets-controllers/src/RatesController/RatesController.ts +++ b/packages/assets-controllers/src/RatesController/RatesController.ts @@ -12,8 +12,15 @@ import type { export const name = 'RatesController'; +/** + * Supported cryptocurrencies that can be used as a base currency. The value needs to be compatible + * with CryptoCompare's API which is the default source for the rates. + * + * See: https://min-api.cryptocompare.com/documentation?key=Price&cat=multipleSymbolsPriceEndpoint + */ export enum Cryptocurrency { Btc = 'btc', + Solana = 'sol', } const DEFAULT_INTERVAL = 180000; @@ -31,8 +38,12 @@ const defaultState = { conversionDate: 0, conversionRate: 0, }, + [Cryptocurrency.Solana]: { + conversionDate: 0, + conversionRate: 0, + }, }, - cryptocurrencies: [Cryptocurrency.Btc], + cryptocurrencies: [Cryptocurrency.Btc, Cryptocurrency.Solana], }; export class RatesController extends BaseController< From e06caee14c14b7d3e1a7dd05ae7930788ecc14e5 Mon Sep 17 00:00:00 2001 From: Ulisses Ferreira Date: Wed, 13 Nov 2024 15:27:08 +0000 Subject: [PATCH 2/4] Release 246.0.0 (#4919) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Explanation As we aim to support more chains in the MetaMask extension we will need the exchange rates of the native tokens of these chains to properly value a user's account. Let's add Solana to the cryptocurrencies that we need to keep track of price. ## References Closes [SOL-36](https://consensyssoftware.atlassian.net/jira/software/projects/SOL/boards/718/backlog?selectedIssue=SOL-36) ## Changelog ### `@metamask/assets-controller` - feat: add Solana to the polled exchange rates ## Checklist - [x] I've updated the test suite for new or updated code as appropriate - [x] I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate - [x] I've highlighted breaking changes using the "BREAKING" category above as appropriate - [x] I've prepared draft pull requests for clients and consumer packages to resolve any breaking changes [SOL-36]: https://consensyssoftware.atlassian.net/browse/SOL-36?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ --------- Co-authored-by: Antonio Regadas Co-authored-by: António Regadas Co-authored-by: Frederik Bolding --- package.json | 2 +- packages/assets-controllers/CHANGELOG.md | 9 ++++++++- packages/assets-controllers/package.json | 2 +- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 0779428d8c..91aacd27fd 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@metamask/core-monorepo", - "version": "245.0.0", + "version": "246.0.0", "private": true, "description": "Monorepo for packages shared between MetaMask clients", "repository": { diff --git a/packages/assets-controllers/CHANGELOG.md b/packages/assets-controllers/CHANGELOG.md index 923f49ed9b..1f3ac43839 100644 --- a/packages/assets-controllers/CHANGELOG.md +++ b/packages/assets-controllers/CHANGELOG.md @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [43.1.0] + +### Added + +- Add Solana to the polled exchange rates ([#4914](https://github.com/MetaMask/core/pull/4914)) + ## [43.0.0] ### Added @@ -1204,7 +1210,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Use Ethers for AssetsContractController ([#845](https://github.com/MetaMask/core/pull/845)) -[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@43.0.0...HEAD +[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@43.1.0...HEAD +[43.1.0]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@43.0.0...@metamask/assets-controllers@43.1.0 [43.0.0]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@42.0.0...@metamask/assets-controllers@43.0.0 [42.0.0]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@41.0.0...@metamask/assets-controllers@42.0.0 [41.0.0]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@40.0.0...@metamask/assets-controllers@41.0.0 diff --git a/packages/assets-controllers/package.json b/packages/assets-controllers/package.json index 61b73e747d..945a126119 100644 --- a/packages/assets-controllers/package.json +++ b/packages/assets-controllers/package.json @@ -1,6 +1,6 @@ { "name": "@metamask/assets-controllers", - "version": "43.0.0", + "version": "43.1.0", "description": "Controllers which manage interactions involving ERC-20, ERC-721, and ERC-1155 tokens (including NFTs)", "keywords": [ "MetaMask", From 37d2c1e4886fd63a08fce88f1c47666f9e5ee4cf Mon Sep 17 00:00:00 2001 From: Salim TOUBAL Date: Wed, 13 Nov 2024 17:28:37 +0100 Subject: [PATCH 3/4] fix: fix token import flow (#4924) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Explanation As autodetection is now multi-chain, the `addTokens` function must be adjusted by taking the `allTokens` and filter on `chainId` into account to avoid importing the wrong tokens. The `addTokens` function currently operates by taking the existing list of imported tokens ( without considering the network ) and adding the tokens specified in the `tokensToImport` parameter. This approach can result in incorrect tokens being imported, as it doesn’t filter by chain. To address this, we use `allTokens` to ensure accurate filtering by chain. ## References ## Changelog ### `@metamask/assets-controllers` - **CHANGED**: addTokens should rely now on `allTokens` ## 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 --- .../assets-controllers/src/TokensController.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/assets-controllers/src/TokensController.ts b/packages/assets-controllers/src/TokensController.ts index ba94bb2fb3..ddd7bd0295 100644 --- a/packages/assets-controllers/src/TokensController.ts +++ b/packages/assets-controllers/src/TokensController.ts @@ -462,16 +462,16 @@ export class TokensController extends BaseController< */ async addTokens(tokensToImport: Token[], networkClientId?: NetworkClientId) { const releaseLock = await this.#mutex.acquire(); - const { ignoredTokens, allDetectedTokens } = this.state; + const { allTokens, ignoredTokens, allDetectedTokens } = this.state; const importedTokensMap: { [key: string]: true } = {}; // Used later to dedupe imported tokens - const newTokensMap = Object.values(tokensToImport).reduce( - (output, token) => { - output[token.address] = token; - return output; - }, - {} as { [address: string]: Token }, - ); + const newTokensMap = [ + ...(allTokens[this.#chainId]?.[this.#getSelectedAccount().address] || []), + ...tokensToImport, + ].reduce((output, token) => { + output[token.address] = token; + return output; + }, {} as { [address: string]: Token }); try { tokensToImport.forEach((tokenToAdd) => { const { address, symbol, decimals, image, aggregators, name } = From 6859854820de887738a1f1c3663d2db00c001675 Mon Sep 17 00:00:00 2001 From: Brian Bergeron Date: Wed, 13 Nov 2024 09:19:48 -0800 Subject: [PATCH 4/4] Release 247.0.0 (#4925) Patch release of assets controllers to include https://github.com/MetaMask/core/pull/4924 --- package.json | 2 +- packages/assets-controllers/CHANGELOG.md | 9 ++++++++- packages/assets-controllers/package.json | 2 +- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 91aacd27fd..0fd6f17bed 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@metamask/core-monorepo", - "version": "246.0.0", + "version": "247.0.0", "private": true, "description": "Monorepo for packages shared between MetaMask clients", "repository": { diff --git a/packages/assets-controllers/CHANGELOG.md b/packages/assets-controllers/CHANGELOG.md index 1f3ac43839..36747a51c2 100644 --- a/packages/assets-controllers/CHANGELOG.md +++ b/packages/assets-controllers/CHANGELOG.md @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [43.1.1] + +### Changed + +- Fix a bug in `TokensController.addTokens` where tokens could be added from the wrong chain. ([#4924](https://github.com/MetaMask/core/pull/4924)) + ## [43.1.0] ### Added @@ -1210,7 +1216,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Use Ethers for AssetsContractController ([#845](https://github.com/MetaMask/core/pull/845)) -[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@43.1.0...HEAD +[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@43.1.1...HEAD +[43.1.1]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@43.1.0...@metamask/assets-controllers@43.1.1 [43.1.0]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@43.0.0...@metamask/assets-controllers@43.1.0 [43.0.0]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@42.0.0...@metamask/assets-controllers@43.0.0 [42.0.0]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@41.0.0...@metamask/assets-controllers@42.0.0 diff --git a/packages/assets-controllers/package.json b/packages/assets-controllers/package.json index 945a126119..d02bc4883d 100644 --- a/packages/assets-controllers/package.json +++ b/packages/assets-controllers/package.json @@ -1,6 +1,6 @@ { "name": "@metamask/assets-controllers", - "version": "43.1.0", + "version": "43.1.1", "description": "Controllers which manage interactions involving ERC-20, ERC-721, and ERC-1155 tokens (including NFTs)", "keywords": [ "MetaMask",