UNPKG

@moonwell-fi/moonwell-sdk

Version:

TypeScript Interface for Moonwell

54 lines 2.41 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MorphoUserRewardsAggregateError = void 0; exports.getMorphoUserRewards = getMorphoUserRewards; const index_js_1 = require("../../../common/index.js"); const common_js_1 = require("./common.js"); class MorphoUserRewardsAggregateError extends AggregateError { constructor(errors, message, rewards) { super(errors, message); Object.defineProperty(this, "rewards", { enumerable: true, configurable: true, writable: true, value: void 0 }); this.name = "MorphoUserRewardsAggregateError"; this.rewards = rewards; } } exports.MorphoUserRewardsAggregateError = MorphoUserRewardsAggregateError; async function getMorphoUserRewards(client, args) { const targetEnvironments = (0, index_js_1.getEnvironmentsFromArgs)(client, args).filter((environment) => environment.contracts.morphoViews !== undefined); const settled = await Promise.allSettled(targetEnvironments.map((environment) => (0, common_js_1.getUserMorphoRewardsData)({ environment, account: args.userAddress, ...(args.throwOnExternalApiError !== undefined && { throwOnExternalApiError: args.throwOnExternalApiError, }), }))); const fulfilled = []; const failures = []; const failedChainIds = []; for (const [i, result] of settled.entries()) { const environment = targetEnvironments[i]; if (result.status === "fulfilled") { fulfilled.push(...result.value); continue; } const reason = result.reason; if (reason instanceof common_js_1.MerklApiError) { failures.push(reason); failedChainIds.push(reason.chainId); continue; } const baseError = reason instanceof Error ? reason : new Error(String(reason)); failures.push(new Error(`getMorphoUserRewards failed for chain ${environment.chainId}: ${baseError.message}`, { cause: baseError })); failedChainIds.push(environment.chainId); } if (failures.length > 0 && args.throwOnExternalApiError === true) { throw new MorphoUserRewardsAggregateError(failures, `getMorphoUserRewards failed for chains: ${failedChainIds.join(", ")}`, fulfilled); } return fulfilled; } //# sourceMappingURL=getMorphoUserRewards.js.map