From 5251f3a152842775015a9fbf96e8879e8f3c6364 Mon Sep 17 00:00:00 2001 From: tommasini <46944231+tommasini@users.noreply.github.com> Date: Mon, 3 Jun 2024 13:32:44 +0100 Subject: [PATCH 1/4] fix: Mutex never gets released on refresh function of AccountTrackerController (#4270) ## Explanation Currently the mutex do not get released unless there is an error thrown by the `getBalanceFromChain` function Fixed by move the release of the murex to the finally block. It was also removed unnecessary catch block. Now the mutex gets released when the logic of refresh runs successfully ### Fixed - Ensure mutex is released when refresh succeeds - Previously the `refresh` method would remain locked indefinitely after it was run successfully. The mutex was only released upon failure. ## References ## Changelog ### `@metamask/assets-controllers` - **FIXED**: Mutex released on the finally block of refresh 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 --- packages/assets-controllers/src/AccountTrackerController.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/assets-controllers/src/AccountTrackerController.ts b/packages/assets-controllers/src/AccountTrackerController.ts index 3596790358..63f37fc9aa 100644 --- a/packages/assets-controllers/src/AccountTrackerController.ts +++ b/packages/assets-controllers/src/AccountTrackerController.ts @@ -285,9 +285,8 @@ export class AccountTrackerController extends StaticIntervalPollingControllerV1< [chainId]: accountsForChain, }, }); - } catch (err) { + } finally { releaseLock(); - throw err; } }; From 00c043e97937157ee62951d4f11f2541f22393a9 Mon Sep 17 00:00:00 2001 From: legobeat <109787230+legobeat@users.noreply.github.com> Date: Mon, 3 Jun 2024 22:25:57 +0900 Subject: [PATCH 2/4] deps: @metamask/keyring-api@^6.1.1->^6.4.0 (#4355) ## Explanation Bump to latest `@metamask/providers`. ## References This is causing issues: - https://github.com/MetaMask/keyring-api/issues/331 ## Changelog ### `@metamask/accounts-controller` - **CHANGED**: Bump `@metamask/accounts-controller` from `^6.1.1` to `^6.4.0` ### `@metamask/assets-controller` - **CHANGED**: Bump `@metamask/assets-controller` from `^6.1.1` to `^6.4.0` ### `@metamask/chain-controller` - **CHANGED**: Bump `@metamask/chain-controller` from `^6.1.1` to `^6.4.0` ### `@metamask/keyring-controller` - **CHANGED**: Bump `@metamask/keyring-controller` from `^6.1.1` to `^6.4.0` ## 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 --------- Co-authored-by: Daniel Rocha <68558152+danroc@users.noreply.github.com> Co-authored-by: Daniel Rocha --- packages/accounts-controller/package.json | 2 +- .../src/AccountsController.test.ts | 21 ++-- .../accounts-controller/src/tests/mocks.ts | 7 +- packages/assets-controllers/package.json | 2 +- packages/chain-controller/package.json | 2 +- packages/keyring-controller/package.json | 2 +- yarn.lock | 105 +++++++++--------- 7 files changed, 73 insertions(+), 68 deletions(-) diff --git a/packages/accounts-controller/package.json b/packages/accounts-controller/package.json index 2d754d4ce2..040fec02a2 100644 --- a/packages/accounts-controller/package.json +++ b/packages/accounts-controller/package.json @@ -44,7 +44,7 @@ "@ethereumjs/util": "^8.1.0", "@metamask/base-controller": "^6.0.0", "@metamask/eth-snap-keyring": "^4.1.1", - "@metamask/keyring-api": "^6.1.1", + "@metamask/keyring-api": "^6.4.0", "@metamask/snaps-sdk": "^4.2.0", "@metamask/snaps-utils": "^7.4.0", "@metamask/utils": "^8.3.0", diff --git a/packages/accounts-controller/src/AccountsController.test.ts b/packages/accounts-controller/src/AccountsController.test.ts index 385bf21187..930a569cd4 100644 --- a/packages/accounts-controller/src/AccountsController.test.ts +++ b/packages/accounts-controller/src/AccountsController.test.ts @@ -7,7 +7,6 @@ import { BtcAccountType, BtcMethod, EthAccountType, - EthErc4337Method, EthMethod, } from '@metamask/keyring-api'; import { KeyringTypes } from '@metamask/keyring-controller'; @@ -46,7 +45,7 @@ const mockGetKeyringForAccount = jest.fn(); const mockGetKeyringByType = jest.fn(); const mockGetAccounts = jest.fn(); -const EOA_METHODS = [ +const ETH_EOA_METHODS = [ EthMethod.PersonalSign, EthMethod.Sign, EthMethod.SignTransaction, @@ -55,11 +54,17 @@ const EOA_METHODS = [ EthMethod.SignTypedDataV4, ] as const; +const ETH_ERC_4337_METHODS = [ + EthMethod.PatchUserOperation, + EthMethod.PrepareUserOperation, + EthMethod.SignUserOperation, +] as const; + const mockAccount: InternalAccount = { id: 'mock-id', address: '0x123', options: {}, - methods: [...EOA_METHODS], + methods: [...ETH_EOA_METHODS], type: EthAccountType.Eoa, metadata: { name: 'Account 1', @@ -73,7 +78,7 @@ const mockAccount2: InternalAccount = { id: 'mock-id2', address: '0x1234', options: {}, - methods: [...EOA_METHODS], + methods: [...ETH_EOA_METHODS], type: EthAccountType.Eoa, metadata: { name: 'Account 2', @@ -87,7 +92,7 @@ const mockAccount3: InternalAccount = { id: 'mock-id3', address: '0x3333', options: {}, - methods: [...EOA_METHODS], + methods: [...ETH_EOA_METHODS], type: EthAccountType.Eoa, metadata: { name: '', @@ -106,7 +111,7 @@ const mockAccount4: InternalAccount = { id: 'mock-id4', address: '0x4444', options: {}, - methods: [...EOA_METHODS], + methods: [...ETH_EOA_METHODS], type: EthAccountType.Eoa, metadata: { name: 'Custom Name', @@ -183,8 +188,8 @@ function createExpectedInternalAccount({ lastSelected?: number; }): InternalAccount { const accountTypeToMethods = { - [`${EthAccountType.Eoa}`]: [...Object.values(EthMethod)], - [`${EthAccountType.Erc4337}`]: [...Object.values(EthErc4337Method)], + [`${EthAccountType.Eoa}`]: [...Object.values(ETH_EOA_METHODS)], + [`${EthAccountType.Erc4337}`]: [...Object.values(ETH_ERC_4337_METHODS)], [`${BtcAccountType.P2wpkh}`]: [...Object.values(BtcMethod)], }; diff --git a/packages/accounts-controller/src/tests/mocks.ts b/packages/accounts-controller/src/tests/mocks.ts index 59a9892a1a..daebd1fbc3 100644 --- a/packages/accounts-controller/src/tests/mocks.ts +++ b/packages/accounts-controller/src/tests/mocks.ts @@ -6,7 +6,6 @@ import { BtcAccountType, BtcMethod, EthAccountType, - EthErc4337Method, EthMethod, } from '@metamask/keyring-api'; import { KeyringTypes } from '@metamask/keyring-controller'; @@ -50,9 +49,9 @@ export const createMockInternalAccount = ({ break; case EthAccountType.Erc4337: methods = [ - EthErc4337Method.PatchUserOperation, - EthErc4337Method.PrepareUserOperation, - EthErc4337Method.SignUserOperation, + EthMethod.PatchUserOperation, + EthMethod.PrepareUserOperation, + EthMethod.SignUserOperation, ]; break; case BtcAccountType.P2wpkh: diff --git a/packages/assets-controllers/package.json b/packages/assets-controllers/package.json index bd3b61d8e3..4536f6db48 100644 --- a/packages/assets-controllers/package.json +++ b/packages/assets-controllers/package.json @@ -73,7 +73,7 @@ "devDependencies": { "@metamask/auto-changelog": "^3.4.4", "@metamask/ethjs-provider-http": "^0.3.0", - "@metamask/keyring-api": "^6.1.1", + "@metamask/keyring-api": "^6.4.0", "@types/jest": "^27.4.1", "@types/lodash": "^4.14.191", "@types/node": "^16.18.54", diff --git a/packages/chain-controller/package.json b/packages/chain-controller/package.json index 6b2d71ca31..f023fb1c98 100644 --- a/packages/chain-controller/package.json +++ b/packages/chain-controller/package.json @@ -43,7 +43,7 @@ "dependencies": { "@metamask/base-controller": "^6.0.0", "@metamask/chain-api": "^0.0.1", - "@metamask/keyring-api": "^6.1.1", + "@metamask/keyring-api": "^6.4.0", "@metamask/snaps-controllers": "^8.1.1", "@metamask/snaps-sdk": "^4.2.0", "@metamask/snaps-utils": "^7.4.0", diff --git a/packages/keyring-controller/package.json b/packages/keyring-controller/package.json index 102d1e9e91..68520c71b6 100644 --- a/packages/keyring-controller/package.json +++ b/packages/keyring-controller/package.json @@ -48,7 +48,7 @@ "@metamask/eth-hd-keyring": "^7.0.1", "@metamask/eth-sig-util": "^7.0.1", "@metamask/eth-simple-keyring": "^6.0.1", - "@metamask/keyring-api": "^6.1.1", + "@metamask/keyring-api": "^6.4.0", "@metamask/message-manager": "^10.0.0", "@metamask/utils": "^8.3.0", "async-mutex": "^0.5.0", diff --git a/yarn.lock b/yarn.lock index e78b424eee..52d067820f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1617,7 +1617,7 @@ __metadata: "@metamask/auto-changelog": ^3.4.4 "@metamask/base-controller": ^6.0.0 "@metamask/eth-snap-keyring": ^4.1.1 - "@metamask/keyring-api": ^6.1.1 + "@metamask/keyring-api": ^6.4.0 "@metamask/keyring-controller": ^17.0.0 "@metamask/snaps-controllers": ^8.1.1 "@metamask/snaps-sdk": ^4.2.0 @@ -1735,7 +1735,7 @@ __metadata: "@metamask/controller-utils": ^11.0.0 "@metamask/eth-query": ^4.0.0 "@metamask/ethjs-provider-http": ^0.3.0 - "@metamask/keyring-api": ^6.1.1 + "@metamask/keyring-api": ^6.4.0 "@metamask/keyring-controller": ^17.0.0 "@metamask/metamask-eth-abis": ^3.1.1 "@metamask/network-controller": ^19.0.0 @@ -1876,7 +1876,7 @@ __metadata: "@metamask/auto-changelog": ^3.4.4 "@metamask/base-controller": ^6.0.0 "@metamask/chain-api": ^0.0.1 - "@metamask/keyring-api": ^6.1.1 + "@metamask/keyring-api": ^6.4.0 "@metamask/snaps-controllers": ^8.1.1 "@metamask/snaps-sdk": ^4.2.0 "@metamask/snaps-utils": ^7.4.0 @@ -2227,12 +2227,12 @@ __metadata: linkType: hard "@metamask/eth-snap-keyring@npm:^4.1.1": - version: 4.1.1 - resolution: "@metamask/eth-snap-keyring@npm:4.1.1" + version: 4.2.1 + resolution: "@metamask/eth-snap-keyring@npm:4.2.1" dependencies: "@ethereumjs/tx": ^4.2.0 "@metamask/eth-sig-util": ^7.0.1 - "@metamask/keyring-api": ^6.1.1 + "@metamask/keyring-api": ^6.3.1 "@metamask/snaps-controllers": ^8.1.1 "@metamask/snaps-sdk": ^4.2.0 "@metamask/snaps-utils": ^7.4.0 @@ -2240,7 +2240,7 @@ __metadata: "@types/uuid": ^9.0.1 superstruct: ^1.0.3 uuid: ^9.0.0 - checksum: a5d1c1ee83988a7bb829c2eaf6b9a7035c880c4a381d2a32d91aa1a554c97740232159afac93ddbb493cadba53757be2febd844f6f00fa91f21e73a9c6e3d92d + checksum: cd4eb41c878e619ea3f270439fc32e68f1d75ce92cf0232d5a21d62b6b62b2d9f2d7085078b5d2d85eb94690fd027045de1f741fce73ae7222f67935ec63c2ac languageName: node linkType: hard @@ -2450,7 +2450,7 @@ __metadata: languageName: unknown linkType: soft -"@metamask/key-tree@npm:^9.0.0, @metamask/key-tree@npm:^9.1.0": +"@metamask/key-tree@npm:^9.1.1": version: 9.1.1 resolution: "@metamask/key-tree@npm:9.1.1" dependencies: @@ -2463,19 +2463,19 @@ __metadata: languageName: node linkType: hard -"@metamask/keyring-api@npm:^6.1.1": - version: 6.1.1 - resolution: "@metamask/keyring-api@npm:6.1.1" +"@metamask/keyring-api@npm:^6.3.1, @metamask/keyring-api@npm:^6.4.0": + version: 6.4.0 + resolution: "@metamask/keyring-api@npm:6.4.0" dependencies: "@metamask/snaps-sdk": ^4.2.0 - "@metamask/utils": ^8.3.0 - "@types/uuid": ^9.0.1 + "@metamask/utils": ^8.4.0 + "@types/uuid": ^9.0.8 bech32: ^2.0.0 superstruct: ^1.0.3 - uuid: ^9.0.0 + uuid: ^9.0.1 peerDependencies: - "@metamask/providers": ">=15 <17" - checksum: 5a9ed008e19062c84ec8fd019ad29f9ebb7d8d8464bbe5da70ad26e6aceb57e4d98a9762e7cd9fea4ac7de0cdc08bfc0a5bf598770749aa9abdbe6d1840fb627 + "@metamask/providers": ">=15 <18" + checksum: 7845ed5fa73db3165703c2142b6062d03ca5fea329b54d28f424dee2bb393edc1f9a015e771289ef7236c31f30355bf2c52ad74bb47cf531c09c5eec66e06b00 languageName: node linkType: hard @@ -2495,7 +2495,7 @@ __metadata: "@metamask/eth-hd-keyring": ^7.0.1 "@metamask/eth-sig-util": ^7.0.1 "@metamask/eth-simple-keyring": ^6.0.1 - "@metamask/keyring-api": ^6.1.1 + "@metamask/keyring-api": ^6.4.0 "@metamask/message-manager": ^10.0.0 "@metamask/scure-bip39": ^2.1.1 "@metamask/utils": ^8.3.0 @@ -2804,7 +2804,7 @@ __metadata: languageName: unknown linkType: soft -"@metamask/post-message-stream@npm:^8.0.0": +"@metamask/post-message-stream@npm:^8.1.0": version: 8.1.0 resolution: "@metamask/post-message-stream@npm:8.1.0" dependencies: @@ -2856,9 +2856,9 @@ __metadata: languageName: unknown linkType: soft -"@metamask/providers@npm:^16.1.0": - version: 16.1.0 - resolution: "@metamask/providers@npm:16.1.0" +"@metamask/providers@npm:^17.0.0": + version: 17.0.0 + resolution: "@metamask/providers@npm:17.0.0" dependencies: "@metamask/json-rpc-engine": ^8.0.1 "@metamask/json-rpc-middleware-stream": ^7.0.1 @@ -2871,8 +2871,9 @@ __metadata: fast-deep-equal: ^3.1.3 is-stream: ^2.0.0 readable-stream: ^3.6.2 - webextension-polyfill: ^0.10.0 - checksum: 85e40140f342a38112c3d7cee436751a2be4c575cc4f815ab48a73b549abc2d756bf4a10e4b983e91dbd38076601f992531edb6d8d674aebceae32ef7e299275 + peerDependencies: + webextension-polyfill: ^0.10.0 || ^0.11.0 || ^0.12.0 + checksum: 330e369458edc68d743d87b8b2597cdacac58df01b5fc31f565ae5dacee2390ee23693fb10fa451c6146665e87475a4c8f54163407eb05fceeb698900e34f9e6 languageName: node linkType: hard @@ -3015,8 +3016,8 @@ __metadata: linkType: hard "@metamask/snaps-controllers@npm:^8.1.1": - version: 8.1.1 - resolution: "@metamask/snaps-controllers@npm:8.1.1" + version: 8.3.1 + resolution: "@metamask/snaps-controllers@npm:8.3.1" dependencies: "@metamask/approval-controller": ^6.0.2 "@metamask/base-controller": ^5.0.2 @@ -3025,12 +3026,12 @@ __metadata: "@metamask/object-multiplex": ^2.0.0 "@metamask/permission-controller": ^9.0.2 "@metamask/phishing-controller": ^9.0.1 - "@metamask/post-message-stream": ^8.0.0 + "@metamask/post-message-stream": ^8.1.0 "@metamask/rpc-errors": ^6.2.1 "@metamask/snaps-registry": ^3.1.0 - "@metamask/snaps-rpc-methods": ^8.1.0 - "@metamask/snaps-sdk": ^4.1.0 - "@metamask/snaps-utils": ^7.3.0 + "@metamask/snaps-rpc-methods": ^9.1.2 + "@metamask/snaps-sdk": ^4.4.1 + "@metamask/snaps-utils": ^7.4.1 "@metamask/utils": ^8.3.0 "@xstate/fsm": ^2.0.0 browserify-zlib: ^0.2.0 @@ -3043,11 +3044,11 @@ __metadata: readable-web-to-node-stream: ^3.0.2 tar-stream: ^3.1.7 peerDependencies: - "@metamask/snaps-execution-environments": ^6.1.0 + "@metamask/snaps-execution-environments": ^6.3.0 peerDependenciesMeta: "@metamask/snaps-execution-environments": optional: true - checksum: 6b3d68a48bae8a70f1f59043de6636c2ad5b2d8e427e40c1b124fc7a35a7dccc77031987ee404a8927dd3d53b4c82782abb80e768ef1defad378dbe2fa2b4a13 + checksum: d2fccfc9a4fdea68c89755a0e93e292eafdbe28515fcf1f5ba761d4fb057ae2f1732d242f776cd089ea8dfbd0f84d9d2151778ba529fd9b5b4c7b00460a612ab languageName: node linkType: hard @@ -3063,49 +3064,49 @@ __metadata: languageName: node linkType: hard -"@metamask/snaps-rpc-methods@npm:^8.1.0": - version: 8.1.0 - resolution: "@metamask/snaps-rpc-methods@npm:8.1.0" +"@metamask/snaps-rpc-methods@npm:^9.1.2": + version: 9.1.2 + resolution: "@metamask/snaps-rpc-methods@npm:9.1.2" dependencies: - "@metamask/key-tree": ^9.0.0 + "@metamask/key-tree": ^9.1.1 "@metamask/permission-controller": ^9.0.2 "@metamask/rpc-errors": ^6.2.1 - "@metamask/snaps-sdk": ^4.1.0 - "@metamask/snaps-utils": ^7.3.0 + "@metamask/snaps-sdk": ^4.4.1 + "@metamask/snaps-utils": ^7.4.1 "@metamask/utils": ^8.3.0 "@noble/hashes": ^1.3.1 superstruct: ^1.0.3 - checksum: 343da447508c1d5a0757640bb6aa3a7b3979294574ce0600f5a011c2918eb1842ae20c93c0967cf49da622dae99af73f6b243fdfbf65046c5f638dc52d04600d + checksum: dffe041f69ae8593c080155b9338ed86997fd0e23098ccadbc80a2a17a461d3744008b30b419a49be93dbc8482c2f01f6c9fcbf844f58cebdae2439b81353d4b languageName: node linkType: hard -"@metamask/snaps-sdk@npm:^4.1.0, @metamask/snaps-sdk@npm:^4.2.0": - version: 4.2.0 - resolution: "@metamask/snaps-sdk@npm:4.2.0" +"@metamask/snaps-sdk@npm:^4.2.0, @metamask/snaps-sdk@npm:^4.4.1": + version: 4.4.1 + resolution: "@metamask/snaps-sdk@npm:4.4.1" dependencies: - "@metamask/key-tree": ^9.1.0 - "@metamask/providers": ^16.1.0 + "@metamask/key-tree": ^9.1.1 + "@metamask/providers": ^17.0.0 "@metamask/rpc-errors": ^6.2.1 "@metamask/utils": ^8.3.0 fast-xml-parser: ^4.3.4 superstruct: ^1.0.3 - checksum: f9b0e6d7600680183e69d419f5a802208fdc119c7d1226a74076f3b8b8c581850b135392c2f35c391305fc37406973afeb19d8909101580ec16b63fd2f200a8c + checksum: 29dfc36821e77d033ddc1b8f1b8924b4880aca41a25e1767741b50659990a79d3026f3975613090342e98d0cf8d876a0e003edb23ff39d2927dc6473d5c441f9 languageName: node linkType: hard -"@metamask/snaps-utils@npm:^7.3.0, @metamask/snaps-utils@npm:^7.4.0": - version: 7.4.0 - resolution: "@metamask/snaps-utils@npm:7.4.0" +"@metamask/snaps-utils@npm:^7.4.0, @metamask/snaps-utils@npm:^7.4.1": + version: 7.4.1 + resolution: "@metamask/snaps-utils@npm:7.4.1" dependencies: "@babel/core": ^7.23.2 "@babel/types": ^7.23.0 "@metamask/base-controller": ^5.0.2 - "@metamask/key-tree": ^9.1.0 + "@metamask/key-tree": ^9.1.1 "@metamask/permission-controller": ^9.0.2 "@metamask/rpc-errors": ^6.2.1 "@metamask/slip44": ^3.1.0 "@metamask/snaps-registry": ^3.1.0 - "@metamask/snaps-sdk": ^4.2.0 + "@metamask/snaps-sdk": ^4.4.1 "@metamask/utils": ^8.3.0 "@noble/hashes": ^1.3.1 "@scure/base": ^1.1.1 @@ -3119,7 +3120,7 @@ __metadata: ses: ^1.1.0 superstruct: ^1.0.3 validate-npm-package-name: ^5.0.0 - checksum: 1fb072f7262fa0f6685c85a3b44ce75805a87c13449c871e4dde0f6ac3c8cc62cc18ac51ae7eabc399165353abe6d08f3f4ee419cb1fe80518a202423b51660a + checksum: d1d6d3c769c33df88fb6e4fc852cdfe1e400b25b1cae020e729f1bfe8a094804cf901700afbbf1372cc1e95f697127b5847bf3a85b46b403ba2ae64ee5750d22 languageName: node linkType: hard @@ -4000,7 +4001,7 @@ __metadata: languageName: node linkType: hard -"@types/uuid@npm:^9.0.1": +"@types/uuid@npm:^9.0.1, @types/uuid@npm:^9.0.8": version: 9.0.8 resolution: "@types/uuid@npm:9.0.8" checksum: b8c60b7ba8250356b5088302583d1704a4e1a13558d143c549c408bf8920535602ffc12394ede77f8a8083511b023704bc66d1345792714002bfa261b17c5275 @@ -12023,7 +12024,7 @@ __metadata: languageName: node linkType: hard -"webextension-polyfill@npm:>=0.10.0 <1.0, webextension-polyfill@npm:^0.10.0": +"webextension-polyfill@npm:>=0.10.0 <1.0": version: 0.10.0 resolution: "webextension-polyfill@npm:0.10.0" checksum: 4a59036bda571360c2c0b2fb03fe1dc244f233946bcf9a6766f677956c40fd14d270aaa69cdba95e4ac521014afbe4008bfa5959d0ac39f91c990eb206587f91 From 076a65726f9d4470513853e267cbabcb251a232e Mon Sep 17 00:00:00 2001 From: salimtb Date: Mon, 3 Jun 2024 17:46:19 +0200 Subject: [PATCH 3/4] fix: fix market data of assets controller if no data (#4361) ## Explanation fix the case if market data are not available for given token. ## References ## Changelog ### `@metamask/assets-controller` - **** : catch the case when data are not available for given token ## 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 --- .../token-prices-service/codefi-v2.test.ts | 53 +++++++++++++++++++ .../src/token-prices-service/codefi-v2.ts | 2 +- 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/packages/assets-controllers/src/token-prices-service/codefi-v2.test.ts b/packages/assets-controllers/src/token-prices-service/codefi-v2.test.ts index 7660277bc3..74d52436a4 100644 --- a/packages/assets-controllers/src/token-prices-service/codefi-v2.test.ts +++ b/packages/assets-controllers/src/token-prices-service/codefi-v2.test.ts @@ -479,6 +479,59 @@ describe('CodefiTokenPricesServiceV2', () => { }); }); + it('should correctly handle null market data for a token address', async () => { + nock('https://price.api.cx.metamask.io') + .get('/v2/chains/1/spot-prices') + .query({ + tokenAddresses: + '0x0000000000000000000000000000000000000000,0xAAA,0xBBB,0xCCC', + vsCurrency: 'ETH', + includeMarketData: 'true', + }) + .reply(200, { + '0x0000000000000000000000000000000000000000': { + price: 14, + currency: 'ETH', + }, + '0xaaa': null, // Simulating API returning null for market data + '0xbbb': { + price: 33689.98134554716, + currency: 'ETH', + }, + '0xccc': { + price: 148.1344197578456, + currency: 'ETH', + }, + }); + + const result = await new CodefiTokenPricesServiceV2().fetchTokenPrices({ + chainId: '0x1', + tokenAddresses: ['0xAAA', '0xBBB', '0xCCC'], + currency: 'ETH', + }); + + expect(result).toStrictEqual({ + '0x0000000000000000000000000000000000000000': { + tokenAddress: '0x0000000000000000000000000000000000000000', + value: 14, + currency: 'ETH', + price: 14, + }, + '0xBBB': { + tokenAddress: '0xBBB', + value: 33689.98134554716, + currency: 'ETH', + price: 33689.98134554716, + }, + '0xCCC': { + tokenAddress: '0xCCC', + value: 148.1344197578456, + currency: 'ETH', + price: 148.1344197578456, + }, + }); + }); + it('throws if the request fails consistently', async () => { nock('https://price.api.cx.metamask.io') .get('/v2/chains/1/spot-prices') diff --git a/packages/assets-controllers/src/token-prices-service/codefi-v2.ts b/packages/assets-controllers/src/token-prices-service/codefi-v2.ts index 0ffcaaa1f3..973a98a62a 100644 --- a/packages/assets-controllers/src/token-prices-service/codefi-v2.ts +++ b/packages/assets-controllers/src/token-prices-service/codefi-v2.ts @@ -455,7 +455,7 @@ export class CodefiTokenPricesServiceV2 const marketData = addressCryptoDataMap[lowercasedTokenAddress]; - if (marketData === undefined) { + if (!marketData) { return obj; } From 9bb4043e4842fc1d8f903bde28ae7dbcdcc859e5 Mon Sep 17 00:00:00 2001 From: Mark Stacey Date: Tue, 4 Jun 2024 09:46:04 -0230 Subject: [PATCH 4/4] feat: Allow overwriting built-in keyring builders (#4362) ## Explanation The KeyringController comes with two built-in keyrings: Simple and HD. Unfortunately it's impossible to overwrite these because when we build a new keyring, we look for the first keyring builder in the list that matches the type we want to build, and the built-in keyrings are always first. The order has been switched so that built-in keyrings come second, after custom keyring builders. This allows them to be overwritten. This makes it possible to test behavior that is specific to simple or HD keyrings. This is something that I wanted to do in the mobile repository. ## References N/A ## Changelog ### `@metamask/keyring-controller` #### Added - Add support for overwriting built-in keyring builders for the Simple and HD keyring. ## 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 --- .../src/KeyringController.test.ts | 45 +++++++++++++++++++ .../src/KeyringController.ts | 2 +- 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/packages/keyring-controller/src/KeyringController.test.ts b/packages/keyring-controller/src/KeyringController.test.ts index 018c136539..48b1130f61 100644 --- a/packages/keyring-controller/src/KeyringController.test.ts +++ b/packages/keyring-controller/src/KeyringController.test.ts @@ -11,6 +11,7 @@ import { SignTypedDataVersion, encrypt, } from '@metamask/eth-sig-util'; +import SimpleKeyring from '@metamask/eth-simple-keyring/dist/simple-keyring'; import type { EthKeyring } from '@metamask/keyring-api'; import { wordlist } from '@metamask/scure-bip39/dist/wordlists/english'; import type { KeyringClass } from '@metamask/utils'; @@ -100,6 +101,32 @@ describe('KeyringController', () => { }), ).toThrow(KeyringControllerError.UnsupportedEncryptionKeyExport); }); + + it('allows overwriting the built-in Simple keyring builder', async () => { + const mockSimpleKeyringBuilder = + // @ts-expect-error The simple keyring doesn't yet conform to the KeyringClass type + buildKeyringBuilderWithSpy(SimpleKeyring); + await withController( + { keyringBuilders: [mockSimpleKeyringBuilder] }, + async ({ controller }) => { + await controller.addNewKeyring(KeyringTypes.simple); + + expect(mockSimpleKeyringBuilder).toHaveBeenCalledTimes(1); + }, + ); + }); + + it('allows overwriting the built-in HD keyring builder', async () => { + const mockHdKeyringBuilder = buildKeyringBuilderWithSpy(HDKeyring); + await withController( + { keyringBuilders: [mockHdKeyringBuilder] }, + async () => { + // This is called as part of initializing the controller + // because the first keyring is assumed to always be an HD keyring + expect(mockHdKeyringBuilder).toHaveBeenCalledTimes(1); + }, + ); + }); }); describe('addNewAccount', () => { @@ -3538,3 +3565,21 @@ async function withController( messenger, }); } + +/** + * Construct a keyring builder with a spy. + * + * @param KeyringConstructor - The constructor to use for building the keyring. + * @returns A keyring builder that uses `jest.fn()` to spy on invocations. + */ +function buildKeyringBuilderWithSpy(KeyringConstructor: KeyringClass): { + (): EthKeyring; + type: string; +} { + const keyringBuilderWithSpy: { (): EthKeyring; type?: string } = jest + .fn() + .mockImplementation((...args) => new KeyringConstructor(...args)); + keyringBuilderWithSpy.type = KeyringConstructor.type; + // Not sure why TypeScript isn't smart enough to infer that `type` is set here. + return keyringBuilderWithSpy as { (): EthKeyring; type: string }; +} diff --git a/packages/keyring-controller/src/KeyringController.ts b/packages/keyring-controller/src/KeyringController.ts index 1d657295ad..4979f4f7e0 100644 --- a/packages/keyring-controller/src/KeyringController.ts +++ b/packages/keyring-controller/src/KeyringController.ts @@ -611,7 +611,7 @@ export class KeyringController extends BaseController< }); this.#keyringBuilders = keyringBuilders - ? defaultKeyringBuilders.concat(keyringBuilders) + ? keyringBuilders.concat(defaultKeyringBuilders) : defaultKeyringBuilders; this.#encryptor = encryptor;