Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/assets-controller/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- Fix `PriceDataSource` supported-network filtering so it reads the object-shaped `partialSupport` returned by the Price API `/v2/supportedNetworks` endpoint ([#10582](https://github.com/MetaMask/core/pull/10582))
- On the v5 balance path, stop seeding every enabled chain's native onto an account when the update arrives after that account is no longer selected ([#10567](https://github.com/MetaMask/core/pull/10567))
- On unlock, drop stored balances whose chain namespace is outside the selected account's scopes ([#10567](https://github.com/MetaMask/core/pull/10567))

Expand Down
154 changes: 128 additions & 26 deletions packages/assets-controller/src/data-sources/PriceDataSource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,17 @@ function createMiddlewareContext(overrides?: Partial<Context>): Context {
function createMockApiClient(
priceResponse: Record<string, unknown> = {},
supportedNetworks: string[] = ['eip155:1'],
partialSupportNetworks: string[] = [],
): MockApiClient {
return {
prices: {
fetchV3SpotPrices: jest.fn().mockResolvedValue(priceResponse),
fetchPriceV2SupportedNetworks: jest.fn().mockResolvedValue({
fullSupport: supportedNetworks,
partialSupport: [],
partialSupport: {
spotPricesV2: [],
spotPricesV3: partialSupportNetworks,
},
Comment on lines +93 to +96

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

lol I remember now 😄

}),
},
};
Expand Down Expand Up @@ -118,6 +122,7 @@ function setupController(
getSelectedCurrency?: () => SupportedCurrency;
pollInterval?: number;
supportedNetworks?: string[];
partialSupportNetworks?: string[];
getAssetsState?: () => AssetsControllerState;
} = {},
): SetupResult {
Expand All @@ -127,10 +132,15 @@ function setupController(
getSelectedCurrency = (): SupportedCurrency => 'usd',
pollInterval,
supportedNetworks = ['eip155:1'],
partialSupportNetworks = [],
getAssetsState: getAssetsStateOverride,
} = options;

const apiClient = createMockApiClient(priceResponse, supportedNetworks);
const apiClient = createMockApiClient(
priceResponse,
supportedNetworks,
partialSupportNetworks,
);

const getAssetsState =
getAssetsStateOverride ??
Expand Down Expand Up @@ -201,7 +211,7 @@ describe('PriceDataSource', () => {
const { controller, apiClient } = setupController({
balanceState: {
'mock-account-id': {
[MOCK_NATIVE_ASSET]: { amount: '1000000000000000000' },
[MOCK_NATIVE_ASSET]: { amount: '1.5' },
},
},
priceResponse: {
Expand Down Expand Up @@ -233,7 +243,7 @@ describe('PriceDataSource', () => {
balanceState: {
'mock-account-id': {
'not-a-valid-caip19': { amount: '999' },
[MOCK_NATIVE_ASSET]: { amount: '1000000000000000000' },
[MOCK_NATIVE_ASSET]: { amount: '1.5' },
},
},
priceResponse: {
Expand Down Expand Up @@ -304,7 +314,7 @@ describe('PriceDataSource', () => {
getSelectedCurrency: () => 'eur',
balanceState: {
'mock-account-id': {
[MOCK_NATIVE_ASSET]: { amount: '1000000000000000000' },
[MOCK_NATIVE_ASSET]: { amount: '1.5' },
},
},
priceResponse: {
Expand All @@ -326,10 +336,10 @@ describe('PriceDataSource', () => {
const { controller, apiClient } = setupController({
balanceState: {
'mock-account-id': {
[MOCK_NATIVE_ASSET]: { amount: '1000000000000000000' },
[MOCK_NATIVE_ASSET]: { amount: '1.5' },
},
'other-account-id': {
[MOCK_TOKEN_ASSET]: { amount: '1000000' },
[MOCK_TOKEN_ASSET]: { amount: '1000' },
},
},
priceResponse: {
Expand All @@ -353,8 +363,8 @@ describe('PriceDataSource', () => {
const { controller, apiClient } = setupController({
balanceState: {
'mock-account-id': {
[MOCK_NATIVE_ASSET]: { amount: '1000000000000000000' },
[polygonAsset]: { amount: '5000000000000000000' },
[MOCK_NATIVE_ASSET]: { amount: '1.5' },
[polygonAsset]: { amount: '250' },
},
},
priceResponse: {
Expand All @@ -373,6 +383,98 @@ describe('PriceDataSource', () => {
controller.destroy();
});

it('fetch includes assets on partialSupport.spotPricesV3 networks', async () => {
const polygonAsset =
'eip155:137/erc20:0x0000000000000000000000000000000000001010' as Caip19AssetId;
const { controller, apiClient } = setupController({
balanceState: {
'mock-account-id': {
[MOCK_NATIVE_ASSET]: { amount: '1.5' },
[polygonAsset]: { amount: '250' },
},
},
priceResponse: {
[MOCK_NATIVE_ASSET]: createMockPriceData(2500),
[polygonAsset]: createMockPriceData(0.5),
},
supportedNetworks: ['eip155:1'],
partialSupportNetworks: ['eip155:137'],
});

await controller.fetch(
createDataRequest({ chainIds: [CHAIN_MAINNET, CHAIN_POLYGON] }),
);

expect(apiClient.prices.fetchV3SpotPrices).toHaveBeenCalledWith(
expect.arrayContaining([MOCK_NATIVE_ASSET, polygonAsset]),
expect.anything(),
);

controller.destroy();
});

it('fetch excludes assets only listed under partialSupport.spotPricesV2', async () => {
const polygonAsset =
'eip155:137/erc20:0x0000000000000000000000000000000000001010' as Caip19AssetId;
const { controller, apiClient } = setupController({
balanceState: {
'mock-account-id': {
[MOCK_NATIVE_ASSET]: { amount: '1.5' },
[polygonAsset]: { amount: '250' },
},
},
priceResponse: {
[MOCK_NATIVE_ASSET]: createMockPriceData(2500),
},
});
apiClient.prices.fetchPriceV2SupportedNetworks.mockResolvedValue({
fullSupport: ['eip155:1'],
partialSupport: {
spotPricesV2: ['eip155:137'],
spotPricesV3: [],
},
});

await controller.fetch(
createDataRequest({ chainIds: [CHAIN_MAINNET, CHAIN_POLYGON] }),
);

expect(apiClient.prices.fetchV3SpotPrices).toHaveBeenCalledWith(
[MOCK_NATIVE_ASSET],
expect.anything(),
);

controller.destroy();
});

it('fetch tolerates a legacy array-shaped partialSupport', async () => {
const polygonAsset =
'eip155:137/erc20:0x0000000000000000000000000000000000001010' as Caip19AssetId;
const { controller, apiClient } = setupController({
balanceState: {
'mock-account-id': {
[polygonAsset]: { amount: '250' },
},
},
priceResponse: {
[polygonAsset]: createMockPriceData(0.5),
},
});
apiClient.prices.fetchPriceV2SupportedNetworks.mockResolvedValue({
fullSupport: ['eip155:1'],
partialSupport: ['eip155:137'],
});

await controller.fetch(createDataRequest({ chainIds: [CHAIN_POLYGON] }));

expect(apiClient.prices.fetchV3SpotPrices).toHaveBeenCalledWith(
[polygonAsset],
expect.anything(),
);

controller.destroy();
});

it('fetch filters out non-priceable assets', async () => {
const tronBandwidthAsset =
'tron:0x2b6653dc/slip44:bandwidth' as Caip19AssetId;
Expand All @@ -383,7 +485,7 @@ describe('PriceDataSource', () => {
const { controller, apiClient } = setupController({
balanceState: {
'mock-account-id': {
[MOCK_NATIVE_ASSET]: { amount: '1000000000000000000' },
[MOCK_NATIVE_ASSET]: { amount: '1.5' },
[tronBandwidthAsset]: { amount: '1000' },
[tronEnergyAsset]: { amount: '5000' },
[tronStakedAsset]: { amount: '10000' },
Expand All @@ -408,8 +510,8 @@ describe('PriceDataSource', () => {
const { controller } = setupController({
balanceState: {
'mock-account-id': {
[MOCK_NATIVE_ASSET]: { amount: '1000000000000000000' },
[MOCK_TOKEN_ASSET]: { amount: '1000000' },
[MOCK_NATIVE_ASSET]: { amount: '1.5' },
[MOCK_TOKEN_ASSET]: { amount: '1000' },
},
},
priceResponse: {
Expand All @@ -432,7 +534,7 @@ describe('PriceDataSource', () => {
const { controller, apiClient } = setupController({
balanceState: {
'mock-account-id': {
[MOCK_NATIVE_ASSET]: { amount: '1000000000000000000' },
[MOCK_NATIVE_ASSET]: { amount: '1.5' },
},
},
});
Expand Down Expand Up @@ -466,7 +568,7 @@ describe('PriceDataSource', () => {
const { controller, assetsUpdateHandler } = setupController({
balanceState: {
'mock-account-id': {
[MOCK_NATIVE_ASSET]: { amount: '1000000000000000000' },
[MOCK_NATIVE_ASSET]: { amount: '1.5' },
},
},
priceResponse: {
Expand Down Expand Up @@ -498,7 +600,7 @@ describe('PriceDataSource', () => {
pollInterval: 5000,
balanceState: {
'mock-account-id': {
[MOCK_NATIVE_ASSET]: { amount: '1000000000000000000' },
[MOCK_NATIVE_ASSET]: { amount: '1.5' },
},
},
priceResponse: {
Expand Down Expand Up @@ -526,7 +628,7 @@ describe('PriceDataSource', () => {
pollInterval: 60000,
balanceState: {
'mock-account-id': {
[MOCK_NATIVE_ASSET]: { amount: '1000000000000000000' },
[MOCK_NATIVE_ASSET]: { amount: '1.5' },
},
},
priceResponse: {
Expand Down Expand Up @@ -559,7 +661,7 @@ describe('PriceDataSource', () => {
const { controller, apiClient } = setupController({
balanceState: {
'mock-account-id': {
[MOCK_NATIVE_ASSET]: { amount: '1000000000000000000' },
[MOCK_NATIVE_ASSET]: { amount: '1.5' },
},
},
priceResponse: {
Expand Down Expand Up @@ -713,7 +815,7 @@ describe('PriceDataSource', () => {
pollInterval: 5000,
balanceState: {
'mock-account-id': {
[MOCK_NATIVE_ASSET]: { amount: '1000000000000000000' },
[MOCK_NATIVE_ASSET]: { amount: '1.5' },
},
},
priceResponse: {
Expand Down Expand Up @@ -942,7 +1044,7 @@ describe('PriceDataSource', () => {
const { controller, apiClient } = setupController({
balanceState: {
'mock-account-id': {
[MOCK_NATIVE_ASSET]: { amount: '1000000000000000000' },
[MOCK_NATIVE_ASSET]: { amount: '1.5' },
},
},
priceResponse: {
Expand All @@ -966,7 +1068,7 @@ describe('PriceDataSource', () => {
pollInterval: 10_000,
balanceState: {
'mock-account-id': {
[MOCK_NATIVE_ASSET]: { amount: '1000000000000000000' },
[MOCK_NATIVE_ASSET]: { amount: '1.5' },
},
},
priceResponse: {
Expand Down Expand Up @@ -1001,7 +1103,7 @@ describe('PriceDataSource', () => {
pollInterval,
balanceState: {
'mock-account-id': {
[MOCK_NATIVE_ASSET]: { amount: '1000000000000000000' },
[MOCK_NATIVE_ASSET]: { amount: '1.5' },
},
},
priceResponse: {
Expand Down Expand Up @@ -1122,7 +1224,7 @@ describe('PriceDataSource', () => {
const { controller, apiClient } = setupController({
balanceState: {
'mock-account-id': {
[MOCK_NATIVE_ASSET]: { amount: '1000000000000000000' },
[MOCK_NATIVE_ASSET]: { amount: '1.5' },
},
},
priceResponse: {
Expand Down Expand Up @@ -1163,7 +1265,7 @@ describe('PriceDataSource', () => {
const { controller, apiClient } = setupController({
balanceState: {
'mock-account-id': {
[MOCK_NATIVE_ASSET]: { amount: '1000000000000000000' },
[MOCK_NATIVE_ASSET]: { amount: '1.5' },
},
},
priceResponse: {
Expand All @@ -1186,7 +1288,7 @@ describe('PriceDataSource', () => {
({
assetsBalance: {
'mock-account-id': {
[MOCK_NATIVE_ASSET]: { amount: '1000000000000000000' },
[MOCK_NATIVE_ASSET]: { amount: '1.5' },
},
},
}) as AssetsControllerState,
Expand All @@ -1206,8 +1308,8 @@ describe('PriceDataSource', () => {
pollInterval: 5000,
balanceState: {
'mock-account-id': {
[MOCK_NATIVE_ASSET]: { amount: '1000000000000000000' },
[polygonAsset]: { amount: '5000000000000000000' },
[MOCK_NATIVE_ASSET]: { amount: '1.5' },
[polygonAsset]: { amount: '250' },
},
},
priceResponse: {
Expand Down
Loading
Loading