-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApiRewardsFetcher.ts
73 lines (67 loc) · 2.48 KB
/
ApiRewardsFetcher.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { BigNumber } from "ethers";
import { parseUnits } from "ethers/lib/utils";
import { fetchUserRewards } from "../../helpers/rewards/fetchUserRewards";
import {
MorphoEpochDistribution,
RewardsData,
} from "../../helpers/rewards/rewards.types";
import { Address } from "../../types";
import { fetchJson } from "../../utils/fetchJson";
import { Fetcher } from "../Fetcher";
import { RewardsFetcher } from "../fetchers.interfaces";
import { API_URL } from "./api.constants";
export class ApiRewardsFetcher extends Fetcher implements RewardsFetcher {
async fetchRewardsData(
userAddress: Address,
root: string
): Promise<RewardsData | null> {
try {
const userRewards = await fetchUserRewards(userAddress);
if (userRewards.args && root.toLowerCase() !== userRewards.root) {
throw new Error("Invalid rewards data");
}
return {
data: {
age: {
name: userRewards.currentEpoch.age.ageName,
startTimestamp: +userRewards.currentEpoch.age.startTimestamp,
endTimestamp: +userRewards.currentEpoch.age.endTimestamp,
},
epoch: {
id: userRewards.currentEpoch.epoch.id,
name: userRewards.currentEpoch.epoch.epochName,
startTimestamp: +userRewards.currentEpoch.epoch.initialTimestamp,
endTimestamp: +userRewards.currentEpoch.epoch.finalTimestamp,
snapshotBlock: userRewards.currentEpoch.epoch.snapshotBlock,
},
transaction: userRewards.args && {
...userRewards.args,
amount: BigNumber.from(userRewards.args.amount),
},
},
balances: {
claimed: parseUnits(userRewards.claimedRewards),
claimable: parseUnits(userRewards.claimable),
claimableSoon: parseUnits(userRewards.claimableSoon),
currentEpoch: parseUnits(userRewards.currentEpochRewards),
},
};
} catch (e) {
// eslint-disable-next-line no-console
console.error("An error occured while fetching rewards data", e);
return null;
}
}
async fetchMarketsRewardsDistribution() {
const url = [API_URL, "rewards/emissions"].join("/");
try {
const res = await fetchJson<MorphoEpochDistribution>(url);
if ("error" in res) throw res.error;
return res;
} catch {
// In case of rewards fetching error, or if there is no rewards (404),
// return undefined and don't display MORPHO rewards
return undefined;
}
}
}