Skip to content

Commit 848fa4b

Browse files
committed
feat: gearbox protocol all merkle rewards
1 parent d0eeaf7 commit 848fa4b

File tree

3 files changed

+148
-141
lines changed

3 files changed

+148
-141
lines changed

src/sdk/sdk-legacy/gearboxRewards/api.ts

Lines changed: 59 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,13 @@ import { toBigInt, TypedObjectUtils } from "../../utils";
1010
import { GearboxBackendApi } from "../core/endpoint";
1111
import type { PoolData_Legacy } from "../core/pool";
1212
import type { TokenData } from "../tokens/tokenData";
13-
import {
14-
iAirdropDistributorAbi,
15-
iFarmingPoolAbi,
16-
iMulticall3Abi,
17-
} from "../types";
13+
import { iAirdropDistributorAbi, iFarmingPoolAbi } from "../types";
1814
import { toBN } from "../utils/formatter";
1915
import { BigIntMath } from "../utils/math";
16+
import type { ExtraRewardApy } from "./apy";
2017
import type {
21-
MerkleXYZRewardsCampaignsResponse,
2218
MerkleXYZUserRewardsResponse,
19+
MerkleXYZV4CampaignsResponse,
2320
} from "./merklAPI";
2421
import { MerkleXYZApi } from "./merklAPI";
2522

@@ -89,29 +86,17 @@ export interface FarmInfo {
8986
symbol: SupportedToken;
9087
}
9188

92-
type PoolsWithExtraRewardsList = Record<NetworkType, Array<Address>>;
93-
94-
const DEFAULT_POOLS_WITH_EXTRA_REWARDS: PoolsWithExtraRewardsList = {
95-
Mainnet: [
96-
"0x7354EC6E852108411e681D13E11185c3a2567981", // dtBTCV3
97-
],
98-
Arbitrum: [],
99-
Optimism: [],
100-
Base: [],
101-
};
102-
10389
type ReportHandler = (e: unknown, description?: string) => void;
10490

10591
export interface GetLmRewardsInfoProps {
10692
pools: Record<Address, PoolData_Legacy>;
10793
tokensList: Record<Address, TokenData>;
10894
provider: PublicClient;
95+
}
10996

110-
multicallAddress: Address;
111-
112-
poolsWithExtraRewards?: PoolsWithExtraRewardsList;
113-
network: NetworkType;
114-
reportError?: ReportHandler;
97+
export interface GetExtraRewardsProps {
98+
chainId: number;
99+
tokensList: Record<Address, TokenData>;
115100
}
116101

117102
export interface GetLmRewardsProps {
@@ -142,15 +127,53 @@ export interface ClaimLmRewardsV3Props {
142127
}
143128

144129
export class GearboxRewardsApi {
130+
static async getExtraRewards({ chainId, tokensList }: GetExtraRewardsProps) {
131+
const res = await axios.get<MerkleXYZV4CampaignsResponse>(
132+
MerkleXYZApi.getGearboxCampaignsUrl(),
133+
);
134+
const currentActiveCampaigns = res.data.filter(
135+
c => c.status === "LIVE" && c.chainId === chainId,
136+
);
137+
138+
const r = currentActiveCampaigns.reduce<
139+
Record<Address, Array<ExtraRewardApy>>
140+
>((acc, campaign) => {
141+
const rewardSource = (
142+
campaign.tokens[0]?.address || campaign.identifier
143+
).toLowerCase() as Address;
144+
145+
const allRewards = campaign.rewardsRecord.breakdowns
146+
.map((r, i) => {
147+
const tokenLc = r.token.address.toLowerCase() as Address;
148+
const { symbol = r.token.symbol } = tokensList[tokenLc] || {};
149+
150+
const apy = campaign.aprRecord.breakdowns[i]?.value || 0;
151+
152+
const apyObject: ExtraRewardApy = {
153+
token: rewardSource,
154+
balance: null,
155+
156+
apy: apy,
157+
rewardToken: tokenLc,
158+
rewardTokenSymbol: symbol,
159+
};
160+
161+
return apyObject;
162+
})
163+
.filter(r => r.apy > 0);
164+
165+
acc[rewardSource] = [...(acc[rewardSource] || []), ...allRewards];
166+
167+
return acc;
168+
}, {});
169+
170+
return r;
171+
}
172+
145173
static async getLmRewardsInfo({
146174
pools,
147175
provider,
148-
multicallAddress,
149176
tokensList,
150-
151-
poolsWithExtraRewards = DEFAULT_POOLS_WITH_EXTRA_REWARDS,
152-
network,
153-
reportError,
154177
}: GetLmRewardsInfoProps) {
155178
const poolByStakedDiesel = Object.values(pools).reduce<
156179
Record<Address, Address>
@@ -179,19 +202,6 @@ export class GearboxRewardsApi {
179202
const poolStakedTokens = TypedObjectUtils.keys(poolByStakedDiesel);
180203
const allPoolTokens = TypedObjectUtils.keys(poolByItsToken);
181204

182-
const chainId = chains[network].id;
183-
const poolTokensWithExtraReward = (
184-
poolsWithExtraRewards[network] || []
185-
).filter(p => {
186-
const token = tokensList[p.toLowerCase() as Address];
187-
188-
if (!token) {
189-
console.error(`Pool token not found ${p}`);
190-
return false;
191-
}
192-
return true;
193-
});
194-
195205
const farmInfoCalls = poolStakedTokens.map(address => ({
196206
address,
197207
abi: iFarmingPoolAbi,
@@ -213,40 +223,14 @@ export class GearboxRewardsApi {
213223
args: [],
214224
}));
215225

216-
const [mc, ...extra] = await Promise.allSettled([
217-
provider.multicall({
218-
allowFailure: false,
219-
multicallAddress: MULTICALL_ADDRESS,
220-
contracts: [
221-
{
222-
address: multicallAddress,
223-
abi: iMulticall3Abi as any,
224-
functionName: "getCurrentBlockTimestamp",
225-
args: [],
226-
},
227-
228-
...farmInfoCalls,
229-
...farmSupplyCalls,
230-
...rewardTokenCalls,
231-
],
232-
}),
233-
234-
...poolTokensWithExtraReward.map(t => {
235-
return axios.get<MerkleXYZRewardsCampaignsResponse>(
236-
MerkleXYZApi.getRewardsCampaignsUrl({
237-
params: {
238-
chainId,
239-
mainParameter: getAddress(t),
240-
},
241-
}),
242-
);
243-
}),
244-
]);
226+
const mc = await provider.multicall({
227+
allowFailure: false,
228+
multicallAddress: MULTICALL_ADDRESS,
229+
contracts: [...farmInfoCalls, ...farmSupplyCalls, ...rewardTokenCalls],
230+
});
245231

246-
const mcResponse =
247-
this.extractFulfilled(mc, reportError, "rewardsInfoMulticall") || [];
248-
const [ts = 0n, ...restMCResponse] = mcResponse;
249-
const blockTimestamp = (ts as bigint) || 0n;
232+
const mcResponse = mc;
233+
const [...restMCResponse] = mcResponse;
250234

251235
const farmInfoCallsEnd = farmInfoCalls.length;
252236
const farmInfo = restMCResponse.slice(
@@ -290,57 +274,8 @@ export class GearboxRewardsApi {
290274
{},
291275
);
292276

293-
const extraRewards = extra.reduce<Record<string, Array<FarmInfo>>>(
294-
(acc, r, index) => {
295-
const p = poolTokensWithExtraReward[index].toLowerCase() as Address;
296-
297-
const safeResp = this.extractFulfilled(
298-
r,
299-
reportError,
300-
`merkleCampaign: ${p}`,
301-
);
302-
303-
const l = safeResp?.data.reduce<Array<FarmInfo>>((infos, d) => {
304-
const started = toBigInt(d.startTimestamp || 0);
305-
const finished = toBigInt(d.endTimestamp || 0);
306-
307-
if (blockTimestamp >= started && blockTimestamp <= finished) {
308-
const rewardTokenLc = (
309-
d.rewardToken || ""
310-
).toLowerCase() as Address;
311-
const rewardTokenData = tokensList[rewardTokenLc];
312-
const reward = toBN(
313-
d.amountDecimal,
314-
rewardTokenData?.decimals || 18,
315-
);
316-
317-
if (rewardTokenData && reward > 0) {
318-
infos.push({
319-
pool: poolByItsToken[p],
320-
duration: toBigInt(d.endTimestamp - d.startTimestamp),
321-
finished,
322-
reward,
323-
balance: 0n,
324-
symbol: rewardTokenData.symbol,
325-
});
326-
}
327-
}
328-
329-
return infos;
330-
}, []);
331-
332-
if (l) {
333-
acc[p] = l;
334-
}
335-
336-
return acc;
337-
},
338-
{},
339-
);
340-
341277
const stakedTokenRewards = allPoolTokens.reduce<{
342278
base: Record<string, FarmInfo>;
343-
extra: Record<string, Array<FarmInfo>>;
344279
all: Record<string, Array<FarmInfo>>;
345280
}>(
346281
(acc, pool) => {
@@ -360,15 +295,12 @@ export class GearboxRewardsApi {
360295
}
361296
: undefined;
362297

363-
const extra = extraRewards[pool] || [];
364-
365298
if (baseReward) acc.base[pool] = baseReward;
366-
acc.extra[pool] = extra;
367-
acc.all[pool] = [...(baseReward ? [baseReward] : []), ...extra];
299+
acc.all[pool] = [...(baseReward ? [baseReward] : [])];
368300

369301
return acc;
370302
},
371-
{ base: {}, extra: {}, all: {} },
303+
{ base: {}, all: {} },
372304
);
373305

374306
const rewardPoolsSupply = allPoolTokens.reduce<Record<string, bigint>>(
@@ -383,7 +315,6 @@ export class GearboxRewardsApi {
383315
return {
384316
rewardPoolsInfo: stakedTokenRewards.all,
385317
baseRewardPoolsInfo: stakedTokenRewards.base,
386-
extraRewardPoolsInfo: stakedTokenRewards.extra,
387318
rewardPoolsSupply,
388319
};
389320
}

src/sdk/sdk-legacy/gearboxRewards/apy.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,10 @@ const ONE = PERCENTAGE_FACTOR_1KK * 10n;
3737
export interface ExtraRewardApy {
3838
token: Address;
3939
balance: bigint | null;
40-
apy: number;
4140

42-
rewardInfo: FarmInfo;
41+
apy: number;
42+
rewardToken: Address;
43+
rewardTokenSymbol: string;
4344
}
4445

4546
interface GetPoolExtraAPY_V3Props {
@@ -150,8 +151,10 @@ export class GearboxRewardsApy {
150151
return {
151152
token: stakedDieselToken,
152153
balance: null,
154+
153155
apy: r,
154-
rewardInfo: rewardPoolsInfo,
156+
rewardToken: rewardAddress,
157+
rewardTokenSymbol: rewardPoolsInfo.symbol,
155158
};
156159
}
157160

@@ -252,6 +255,13 @@ export class GearboxRewardsApy {
252255
},
253256
}) / Number(PERCENTAGE_FACTOR);
254257

255-
return { token, balance, rewardInfo, apy: r };
258+
return {
259+
token,
260+
balance,
261+
262+
apy: r,
263+
rewardToken: rewardAddress,
264+
rewardTokenSymbol: rewardInfo.symbol,
265+
};
256266
}
257267
}

0 commit comments

Comments
 (0)