Skip to content

Commit 08dd402

Browse files
committed
feat: merkle fallback
1 parent 13e0774 commit 08dd402

3 files changed

Lines changed: 34 additions & 14 deletions

File tree

src/core/merkle/merklAPI.ts

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Address } from "viem";
2+
import { cachedAxios } from "../axios";
23

34
interface MerkleXYZChain {
45
id: number;
@@ -128,10 +129,28 @@ export interface MerklXYZV4RewardCampaign {
128129
export type MerkleXYZV4RewardCampaignResponse = Array<MerklXYZV4RewardCampaign>;
129130

130131
export class MerkleXYZApi {
131-
static getOpportunitiesByAddressUrl = (a: Address) =>
132-
`https://api.merkl.xyz/v4/opportunities?search=${a}`;
133-
static getGearboxCampaignsUrl = () =>
134-
"https://api.merkl.xyz/v4/opportunities?name=gearbox";
135-
static getGearboxRewardCampaignUrl = (campaignId: Address) =>
136-
`https://api.merkl.xyz/v4/campaigns?campaignId=${campaignId}`;
132+
private constructor() {}
133+
134+
static defaultDomain = "https://api.merkl.xyz";
135+
static angleDomain = "https://api-merkl.angle.money";
136+
137+
static getOpportunitiesByAddressUrl = (a: Address) => (domain: string) =>
138+
`${domain}/v4/opportunities?search=${a}`;
139+
140+
static getGearboxCampaignsUrl =
141+
() =>
142+
(domain = MerkleXYZApi.defaultDomain) =>
143+
`${domain}/v4/opportunities?name=gearbox`;
144+
145+
static getGearboxRewardCampaignUrl =
146+
(campaignId: Address) => (domain: string) =>
147+
`${domain}/v4/campaigns?campaignId=${campaignId}`;
148+
149+
static fetchWithFallback = async <T>(getUrl: (domain: string) => string) => {
150+
try {
151+
return await cachedAxios.get<T>(getUrl(MerkleXYZApi.defaultDomain));
152+
} catch {
153+
return await cachedAxios.get<T>(getUrl(MerkleXYZApi.angleDomain));
154+
}
155+
};
137156
}

src/pools/extraAPY/apy.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import type { AxiosResponse } from "axios";
22
import moment from "moment";
33
import type { Address } from "viem";
44

5-
import { cachedAxios } from "../../core/axios";
65
import type {
76
MerkleXYZV4CampaignsResponse,
87
MerkleXYZV4RewardCampaignResponse,
@@ -23,9 +22,10 @@ const IDS_TO_OMIT = {
2322

2423
export const getPoolExtraAPY: PoolExtraAPYHandler = async () => {
2524
// get all campaigns
26-
const res = await cachedAxios.get<MerkleXYZV4CampaignsResponse>(
27-
MerkleXYZApi.getGearboxCampaignsUrl(),
28-
);
25+
const res =
26+
await MerkleXYZApi.fetchWithFallback<MerkleXYZV4CampaignsResponse>(
27+
MerkleXYZApi.getGearboxCampaignsUrl(),
28+
);
2929
// filter out not active
3030
const currentActiveCampaigns = res.data.filter(
3131
c => c.status === "LIVE" && !BROKEN_CAMPAIGNS[c.id],
@@ -43,11 +43,13 @@ export const getPoolExtraAPY: PoolExtraAPYHandler = async () => {
4343
);
4444

4545
const aprIdsResponse: Array<
46-
PromiseSettledResult<AxiosResponse<MerkleXYZV4RewardCampaignResponse, any>>
46+
PromiseSettledResult<
47+
AxiosResponse<MerkleXYZV4RewardCampaignResponse, unknown>
48+
>
4749
> = [];
4850
for (const id of aprIdsList) {
4951
const resp = await Promise.allSettled([
50-
cachedAxios.get<MerkleXYZV4RewardCampaignResponse>(
52+
MerkleXYZApi.fetchWithFallback<MerkleXYZV4RewardCampaignResponse>(
5153
MerkleXYZApi.getGearboxRewardCampaignUrl(id.aprId),
5254
),
5355
]);

src/tokens/apy/merkle/apy.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import type { NetworkType } from "@gearbox-protocol/sdk";
22
import { getChain } from "@gearbox-protocol/sdk";
33
import type { CacheAxiosResponse } from "axios-cache-interceptor";
44
import type { Address } from "viem";
5-
import { cachedAxios } from "../../../core/axios";
65
import type { MerkleXYZV4CampaignsResponse } from "../../../core/merkle/merklAPI";
76
import { MerkleXYZApi } from "../../../core/merkle/merklAPI";
87
import type { PartialRecord } from "../../../core/utils";
@@ -33,7 +32,7 @@ const getAPYMerkle_withFilter = async (
3332
const [additionalAPYs, ...res] = await Promise.allSettled([
3433
getAdditionalAPYs(network, tokenEntries),
3534
...tokenEntries.map(([, c]) =>
36-
cachedAxios.get<MerkleXYZV4CampaignsResponse>(
35+
MerkleXYZApi.fetchWithFallback<MerkleXYZV4CampaignsResponse>(
3736
MerkleXYZApi.getOpportunitiesByAddressUrl(c.id),
3837
),
3938
),

0 commit comments

Comments
 (0)