Skip to content
Open
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
5 changes: 0 additions & 5 deletions eslint-suppressions.json
Original file line number Diff line number Diff line change
Expand Up @@ -1616,11 +1616,6 @@
"count": 1
}
},
"packages/stellar-wallet-snap/src/utils/async.test.ts": {
"@typescript-eslint/explicit-function-return-type": {
"count": 6
}
},
"packages/stellar-wallet-snap/src/utils/snap.ts": {
"no-restricted-syntax": {
"count": 1
Expand Down
1 change: 1 addition & 0 deletions packages/snap-networks-utils/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Add `InFlightCoalescer`, exported from a new `./dedupe` entry point, which coalesces concurrent async operations by key so callers share one in-flight run ([#149](https://github.com/MetaMask/internal-snaps/pull/149))
- Add shared async batching utilities. ([#211](https://github.com/MetaMask/internal-snaps/pull/211))
- Add origin permission helpers ([#193](https://github.com/MetaMask/internal-snaps/pull/193))
- `createOriginPermissions` for building origin-to-method maps
- `validateOrigin` for checking an origin against a permission map
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {

describe('batchesAll', () => {
it('throws when batchSize is less than 1', async () => {
const run = async () => batchesAll([1], 0, async (value) => value);
const run = async (): Promise<number[]> =>
batchesAll([1], 0, async (value) => value);
await expect(run()).rejects.toThrow(RangeError);
});

Expand Down Expand Up @@ -62,7 +63,8 @@ describe('batchesAll', () => {

describe('batchesAllSettled', () => {
it('throws when batchSize is less than 1', async () => {
const run = async () => batchesAllSettled([1], 0, async (value) => value);
const run = async (): Promise<PromiseSettledResult<number>[]> =>
batchesAllSettled([1], 0, async (value) => value);
await expect(run()).rejects.toThrow(RangeError);
});

Expand Down Expand Up @@ -95,11 +97,11 @@ describe('batchesAllSettled', () => {

it('records rejected promises without failing the whole batch', async () => {
const fns = [
async () => 10,
async () => {
async (): Promise<number> => 10,
async (): Promise<never> => {
throw new Error('boom');
},
async () => 90,
async (): Promise<number> => 90,
];

const settled = await batchesAllSettled(fns, 2, async (fn) => fn());
Expand Down Expand Up @@ -153,7 +155,7 @@ describe('chunks', () => {
});

it('throws when chunkSize is less than 1', () => {
const run = () => chunks(['a', 'b', 'c'], 0);
const run = (): string[][] => chunks(['a', 'b', 'c'], 0);
expect(run).toThrow(RangeError);
});
});
Expand Down
7 changes: 7 additions & 0 deletions packages/snap-networks-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ export { buildUrl } from './buildUrl/buildUrl';
export type { BuildUrlParams } from './buildUrl/buildUrl';
export { sanitizeControlCharacters, sanitizeUri } from './sanitize';
export { UrlStruct } from './urlStruct/urlStruct';
export {
batchesAll,
batchesAllSettled,
batchesAllSettledWithChunks,
batchesAllWithChunks,
chunks,
} from './async';
export { Logger, LogLevel } from './logger';
export type {
LoggerOptions,
Expand Down
2 changes: 1 addition & 1 deletion packages/solana-wallet-snap/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/internal-snaps.git"
},
"source": {
"shasum": "mmsPn3F/291/s6sb7cq9t9PbNYFCSk26kQ9bIpw54r8=",
"shasum": "xVsSMBagSbDp3D78uRkhm+EK0pwvxWvEWEH8rByadhY=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,58 @@ describe('TokenApiClient', () => {
await client.getTokensMetadata(tokenAddresses);

expect(mockFetch).toHaveBeenCalledTimes(2);
expect(
mockFetch.mock.calls.map(([url]) =>
new URL(url as string).searchParams.get('assetIds')?.split(','),
),
).toStrictEqual([tokenAddresses.slice(0, 50), tokenAddresses.slice(50)]);
});

it('merges metadata returned from consecutive chunks', async () => {
const thirdTokenAddress = tokenAddressToCaip19(
Network.Mainnet,
'9GCihgDB8fe6KNjn2MYtkzZcRjQy3t9GHdC8uHYmW2hr',
);
const tokenAddresses = [
tokenAddressToCaip19(
Network.Mainnet,
'1GCihgDB8fe6KNjn2MYtkzZcRjQy3t9GHdC8uHYmW2hr',
),
tokenAddressToCaip19(
Network.Mainnet,
'7GCihgDB8fe6KNjn2MYtkzZcRjQy3t9GHdC8uHYmW2hr',
),
thirdTokenAddress,
];
const chunkedConfigProvider = {
get: jest.fn().mockReturnValue({
tokenApi: { baseUrl: 'https://some-mock-url.com', chunkSize: 2 },
staticApi: { baseUrl: 'https://some-mock-static-url.com' },
}),
} as unknown as ConfigProvider;
client = new TokenApiClient(chunkedConfigProvider, mockFetch, mockLogger);
mockFetch
.mockResolvedValueOnce({
ok: true,
json: jest.fn().mockResolvedValueOnce(MOCK_METADATA_RESPONSE),
})
.mockResolvedValueOnce({
ok: true,
json: jest.fn().mockResolvedValueOnce([
{
decimals: 6,
assetId: thirdTokenAddress,
name: 'Popcat 3',
symbol: 'POPCAT3',
},
]),
});

const metadata = await client.getTokensMetadata(tokenAddresses);

expect(mockFetch).toHaveBeenCalledTimes(2);
expect(Object.keys(metadata)).toStrictEqual(tokenAddresses);
expect(metadata[thirdTokenAddress]?.name).toBe('Popcat 3');
});

it('rejects caip19Ids that are invalid', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { UrlStruct, buildUrl } from '@metamask/snap-networks-utils';
import { UrlStruct, buildUrl, chunks } from '@metamask/snap-networks-utils';
import type { Logger } from '@metamask/snap-networks-utils';
import type { FungibleAssetMetadata } from '@metamask/snaps-sdk';
import { array, assert } from '@metamask/superstruct';
Expand Down Expand Up @@ -99,16 +99,14 @@ export class TokenApiClient {
);
}

// Split addresses into chunks
const chunks: TokenCaipAssetType[][] = [];
for (let i = 0; i < supportedAssetTypes.length; i += this.#chunkSize) {
chunks.push(supportedAssetTypes.slice(i, i + this.#chunkSize));
}
const assetTypeChunks = chunks(supportedAssetTypes, this.#chunkSize);

// Fetch metadata for each chunk
const tokenMetadataResponses = (
await Promise.all(
chunks.map(async (chunk) => this.#fetchTokenMetadataBatch(chunk)),
assetTypeChunks.map(async (chunk) =>
this.#fetchTokenMetadataBatch(chunk),
),
)
).flat();

Expand Down
34 changes: 0 additions & 34 deletions packages/solana-wallet-snap/src/core/utils/retry.ts

This file was deleted.

8 changes: 7 additions & 1 deletion packages/stellar-wallet-snap/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ export * from './serialization';
export * from './number';
export * from './caip';
export * from './buffer';
export * from './async';
export {
batchesAll,
batchesAllSettled,
batchesAllSettledWithChunks,
batchesAllWithChunks,
chunks,
} from '@metamask/snap-networks-utils';
export * from './assert';
export * from './array';
export * from './i18n';
2 changes: 1 addition & 1 deletion packages/tron-wallet-snap/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/internal-snaps.git"
},
"source": {
"shasum": "JjgAZZwXVK0y4gNHI1erLqSZV5tyn2n5xgbJl7mFT60=",
"shasum": "W7oo+4KV26sALyE4TL49MrZn3zdlj4H6SigPDYlmCrg=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,50 @@ describe('TokenApiClient', () => {
await client.getTokensMetadata(tokenAddresses);

expect(mockFetch).toHaveBeenCalledTimes(2);
expect(
mockFetch.mock.calls.map(([url]) =>
new URL(url as string).searchParams.get('assetIds')?.split(','),
),
).toStrictEqual([tokenAddresses.slice(0, 50), tokenAddresses.slice(50)]);
});

it('merges metadata returned from consecutive chunks', async () => {
const thirdTokenAddress =
`${Networks[Network.Mainnet].caip2Id}/trc20:THirdTokenAddressForChunkingTest` as TokenCaipAssetType;
const tokenAddresses = [
`${Networks[Network.Mainnet].caip2Id}/trc20:TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t` as TokenCaipAssetType,
`${Networks[Network.Mainnet].caip2Id}/trc20:TUpMhErZL2fhh4sVNULAbNKLokS4GjC1F4` as TokenCaipAssetType,
thirdTokenAddress,
];
const chunkedConfigProvider = {
get: jest.fn().mockReturnValue({
tokenApi: { baseUrl: 'https://some-mock-url.com', chunkSize: 2 },
staticApi: { baseUrl: 'https://some-mock-static-url.com' },
}),
} as unknown as ConfigProvider;
client = new TokenApiClient(chunkedConfigProvider, mockFetch, mockLogger);
mockFetch
.mockResolvedValueOnce({
ok: true,
json: jest.fn().mockResolvedValueOnce(MOCK_METADATA_RESPONSE),
})
.mockResolvedValueOnce({
ok: true,
json: jest.fn().mockResolvedValueOnce([
{
decimals: 6,
assetId: thirdTokenAddress,
name: 'Third token',
symbol: 'THIRD',
},
]),
});

const metadata = await client.getTokensMetadata(tokenAddresses);

expect(mockFetch).toHaveBeenCalledTimes(2);
expect(Object.keys(metadata)).toStrictEqual(tokenAddresses);
expect(metadata[thirdTokenAddress]?.name).toBe('Third token');
});

it('rejects caip19Ids that are invalid', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { UrlStruct, buildUrl } from '@metamask/snap-networks-utils';
import { UrlStruct, buildUrl, chunks } from '@metamask/snap-networks-utils';
import type { Logger } from '@metamask/snap-networks-utils';
import type { FungibleAssetMetadata } from '@metamask/snaps-sdk';
import { array, assert } from '@metamask/superstruct';
Expand Down Expand Up @@ -106,20 +106,14 @@ export class TokenApiClient {
);
}

// Split addresses into chunks
const chunks: TokenCaipAssetType[][] = [];
for (
let index = 0;
index < supportedAssetTypes.length;
index += this.#chunkSize
) {
chunks.push(supportedAssetTypes.slice(index, index + this.#chunkSize));
}
const assetTypeChunks = chunks(supportedAssetTypes, this.#chunkSize);

// Fetch metadata for each chunk
const tokenMetadataResponses = (
await Promise.all(
chunks.map(async (chunk) => this.#fetchTokenMetadataBatch(chunk)),
assetTypeChunks.map(async (chunk) =>
this.#fetchTokenMetadataBatch(chunk),
),
)
).flat();

Expand Down