@metamask/multichain-network-controller
Version:
Multichain network controller
86 lines • 4.96 kB
JavaScript
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _MultichainNetworkService_instances, _MultichainNetworkService_fetch, _MultichainNetworkService_batchSize, _MultichainNetworkService_fetchNetworkActivityBatch;
import { assert } from "@metamask/superstruct";
import $lodash from "lodash";
const { chunk } = $lodash;
import { ActiveNetworksResponseStruct, buildActiveNetworksUrl, MULTICHAIN_ACCOUNTS_CLIENT_HEADER, MULTICHAIN_ACCOUNTS_CLIENT_ID } from "../api/accounts-api.mjs";
/**
* Service responsible for fetching network activity data from the API.
*/
export class MultichainNetworkService {
constructor({ fetch: fetchFunction, batchSize, }) {
_MultichainNetworkService_instances.add(this);
_MultichainNetworkService_fetch.set(this, void 0);
_MultichainNetworkService_batchSize.set(this, void 0);
__classPrivateFieldSet(this, _MultichainNetworkService_fetch, fetchFunction, "f");
__classPrivateFieldSet(this, _MultichainNetworkService_batchSize, batchSize ?? 20, "f");
}
/**
* Fetches active networks for the given account IDs.
* Automatically handles batching requests to comply with URL length limitations.
*
* @param accountIds - Array of CAIP-10 account IDs to fetch activity for.
* @returns Promise resolving to the combined active networks response.
* @throws Error if the response format is invalid or the request fails.
*/
async fetchNetworkActivity(accountIds) {
if (accountIds.length === 0) {
return { activeNetworks: [] };
}
if (accountIds.length <= __classPrivateFieldGet(this, _MultichainNetworkService_batchSize, "f")) {
return __classPrivateFieldGet(this, _MultichainNetworkService_instances, "m", _MultichainNetworkService_fetchNetworkActivityBatch).call(this, accountIds);
}
const batches = chunk(accountIds, __classPrivateFieldGet(this, _MultichainNetworkService_batchSize, "f"));
const batchResults = await Promise.all(batches.map((batch) => __classPrivateFieldGet(this, _MultichainNetworkService_instances, "m", _MultichainNetworkService_fetchNetworkActivityBatch).call(this, batch)));
const combinedResponse = {
activeNetworks: batchResults.flatMap((response) => response.activeNetworks),
};
return combinedResponse;
}
}
_MultichainNetworkService_fetch = new WeakMap(), _MultichainNetworkService_batchSize = new WeakMap(), _MultichainNetworkService_instances = new WeakSet(), _MultichainNetworkService_fetchNetworkActivityBatch =
/**
* Internal method to fetch a single batch of account IDs.
*
* @param accountIds - Batch of account IDs to fetch
* @returns Promise resolving to the active networks response for this batch
* @throws Error if the response format is invalid or the request fails
*/
async function _MultichainNetworkService_fetchNetworkActivityBatch(accountIds) {
try {
const url = buildActiveNetworksUrl(accountIds);
const response = await __classPrivateFieldGet(this, _MultichainNetworkService_fetch, "f").call(this, url.toString(), {
method: 'GET',
headers: {
[MULTICHAIN_ACCOUNTS_CLIENT_HEADER]: MULTICHAIN_ACCOUNTS_CLIENT_ID,
Accept: 'application/json',
},
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
assert(data, ActiveNetworksResponseStruct);
return data;
}
catch (error) {
if (error instanceof Error) {
if (error.name === 'AbortError') {
throw new Error('Request timeout: Failed to fetch active networks');
}
throw error;
}
throw new Error(`Failed to fetch active networks: ${String(error)}`);
}
};
//# sourceMappingURL=MultichainNetworkService.mjs.map