@moonwell-fi/moonwell-sdk
Version:
TypeScript Interface for Moonwell
55 lines • 2.47 kB
JavaScript
import { getEnvironmentsFromArgs } from "../../../common/index.js";
import { MerklApiError, getUserMorphoRewardsData } from "./common.js";
/**
* AggregateError thrown by `getMorphoUserRewards` when one or more chains
* fail and `throwOnExternalApiError` is `true`. The `rewards` property
* carries the rewards from any chains that succeeded so callers can still
* surface partial results alongside the per-chain failures in `errors`.
*/
export 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;
}
}
export async function getMorphoUserRewards(client, args) {
const targetEnvironments = getEnvironmentsFromArgs(client, args).filter((environment) => environment.contracts.morphoViews !== undefined);
const settled = await Promise.allSettled(targetEnvironments.map((environment) => 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 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