Skip to content

Commit

Permalink
Merge branch 'main' into remove-provider-config-from-assets-contract-…
Browse files Browse the repository at this point in the history
…controller
  • Loading branch information
mcmire authored Jun 4, 2024
2 parents 930bd09 + 9bb4043 commit b0b4783
Show file tree
Hide file tree
Showing 12 changed files with 174 additions and 72 deletions.
2 changes: 1 addition & 1 deletion packages/accounts-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
21 changes: 13 additions & 8 deletions packages/accounts-controller/src/AccountsController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
BtcAccountType,
BtcMethod,
EthAccountType,
EthErc4337Method,
EthMethod,
} from '@metamask/keyring-api';
import { KeyringTypes } from '@metamask/keyring-controller';
Expand Down Expand Up @@ -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,
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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: '',
Expand All @@ -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',
Expand Down Expand Up @@ -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)],
};

Expand Down
7 changes: 3 additions & 4 deletions packages/accounts-controller/src/tests/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
BtcAccountType,
BtcMethod,
EthAccountType,
EthErc4337Method,
EthMethod,
} from '@metamask/keyring-api';
import { KeyringTypes } from '@metamask/keyring-controller';
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion packages/assets-controllers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 1 addition & 2 deletions packages/assets-controllers/src/AccountTrackerController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,8 @@ export class AccountTrackerController extends StaticIntervalPollingControllerV1<
[chainId]: accountsForChain,
},
});
} catch (err) {
} finally {
releaseLock();
throw err;
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ export class CodefiTokenPricesServiceV2

const marketData = addressCryptoDataMap[lowercasedTokenAddress];

if (marketData === undefined) {
if (!marketData) {
return obj;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/chain-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/keyring-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
45 changes: 45 additions & 0 deletions packages/keyring-controller/src/KeyringController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -3538,3 +3565,21 @@ async function withController<ReturnValue>(
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<Json>): {
(): EthKeyring<Json>;
type: string;
} {
const keyringBuilderWithSpy: { (): EthKeyring<Json>; 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<Json>; type: string };
}
2 changes: 1 addition & 1 deletion packages/keyring-controller/src/KeyringController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ export class KeyringController extends BaseController<
});

this.#keyringBuilders = keyringBuilders
? defaultKeyringBuilders.concat(keyringBuilders)
? keyringBuilders.concat(defaultKeyringBuilders)
: defaultKeyringBuilders;

this.#encryptor = encryptor;
Expand Down
Loading

0 comments on commit b0b4783

Please sign in to comment.