@@ -11,11 +11,10 @@ import { GearboxBackendApi } from "../core/endpoint";
1111import type { PoolData_Legacy } from "../core/pool" ;
1212import type { TokenData } from "../tokens/tokenData" ;
1313import { iAirdropDistributorAbi , iFarmingPoolAbi } from "../types" ;
14- import { toBN } from "../utils/formatter" ;
1514import { BigIntMath } from "../utils/math" ;
1615import type { ExtraRewardApy } from "./apy" ;
1716import type {
18- MerkleXYZUserRewardsResponse ,
17+ MerkleXYZUserRewardsV4Response ,
1918 MerkleXYZV4CampaignsResponse ,
2019 MerkleXYZV4RewardCampaignResponse ,
2120 MerklXYZV4RewardCampaign ,
@@ -25,22 +24,34 @@ import { MerkleXYZApi } from "./merklAPI";
2524export interface GearboxExtraMerkleLmReward {
2625 pool : Address ;
2726 poolToken : Address ;
27+
28+ rewardTokenSymbol : string ;
29+ rewardTokenDecimals : number ;
2830 rewardToken : Address ;
2931 amount : bigint ;
32+
3033 type : "extraMerkle" ;
3134}
3235export interface GearboxStakedV3LmReward {
3336 pool : Address ;
3437 poolToken : Address ;
38+
39+ rewardTokenSymbol : string ;
40+ rewardTokenDecimals : number ;
3541 rewardToken : Address ;
3642 amount : bigint ;
43+
3744 type : "stakedV3" ;
3845}
3946export interface GearboxMerkleV2LmReward {
4047 pool ?: undefined ;
4148 poolToken ?: undefined ;
49+
50+ rewardTokenSymbol : string ;
51+ rewardTokenDecimals : number ;
4252 rewardToken : Address ;
4353 amount : bigint ;
54+
4455 type : "merkleV2" ;
4556}
4657
@@ -102,6 +113,8 @@ export interface GetExtraRewardsProps {
102113}
103114
104115export interface GetLmRewardsProps {
116+ pools : Array < PoolData_Legacy > ;
117+
105118 baseRewardPoolsInfo : Record < string , FarmInfo > ;
106119 currentTokenData : Record < SupportedToken , Address > ;
107120 tokensList : Record < Address , TokenData > ;
@@ -387,14 +400,19 @@ export class GearboxRewardsApi {
387400 {
388401 amount : availableToClaimV2 ,
389402 type : "merkleV2" ,
403+
390404 rewardToken : currentTokenData . GEAR ,
405+ rewardTokenDecimals : 18 ,
406+ rewardTokenSymbol : "GEAR" ,
391407 } ,
392408 ] ;
393409
394410 return { rewards : rewards } ;
395411 }
396412
397413 static async getLmRewardsV3 ( {
414+ pools,
415+
398416 baseRewardPoolsInfo,
399417 currentTokenData,
400418 tokensList,
@@ -417,7 +435,7 @@ export class GearboxRewardsApi {
417435 args : [ account ] ,
418436 } ) ) ,
419437 } ) ,
420- axios . get < MerkleXYZUserRewardsResponse > (
438+ axios . get < MerkleXYZUserRewardsV4Response > (
421439 MerkleXYZApi . getUserRewardsUrl ( {
422440 params : {
423441 chainId : chains [ network ] . id ,
@@ -432,48 +450,77 @@ export class GearboxRewardsApi {
432450 reportError ,
433451 "v3Rewards" ,
434452 ) || [ ] ) as Array < bigint > ;
435- const merkleXYZLM = this . extractFulfilled (
453+ const merkleXYZLm = this . extractFulfilled (
436454 merkleXYZLMResponse ,
437455 reportError ,
438456 "merkleRewards" ,
439457 ) ?. data ;
440458
441- const PREFIX = "ERC20" ;
442- const REWARD_KEYS_RECORD = poolTokens . reduce < Record < string , Address > > (
443- ( acc , t ) => {
444- const key = [ PREFIX , getAddress ( t ) ] . join ( "_" ) ;
445- acc [ key ] = t ;
446- return acc ;
447- } ,
448- { } ,
449- ) ;
450-
451- const extraRewards = Object . entries ( merkleXYZLM || { } ) . reduce <
452- Array < GearboxLmReward >
453- > ( ( acc , [ k , v ] ) => {
454- const rewardToken = k . toLowerCase ( ) as Address ;
455-
456- Object . entries ( v . reasons ) . forEach ( ( [ key , reason ] ) => {
457- const poolToken = REWARD_KEYS_RECORD [ key ] ;
458- if ( poolToken && tokensList [ rewardToken ] ) {
459- acc . push ( {
460- pool : baseRewardPoolsInfo [ poolToken ] . pool ,
461- poolToken,
462- rewardToken,
463- amount : toBigInt ( reason . unclaimed || 0n ) ,
464- type : "extraMerkle" ,
465- } ) ;
466- }
459+ const poolByItsToken = Object . values ( pools ) . reduce <
460+ Record < Address , Address >
461+ > ( ( acc , p ) => {
462+ p . stakedDieselToken . forEach ( t => {
463+ if ( t ) acc [ t ] = p . address ;
467464 } ) ;
465+ p . stakedDieselToken_old . forEach ( t => {
466+ if ( t ) acc [ t ] = p . address ;
467+ } ) ;
468+
469+ acc [ p . dieselToken ] = p . address ;
468470
469471 return acc ;
470- } , [ ] ) ;
472+ } , { } ) ;
473+
474+ const extraRewards = ( merkleXYZLm || [ ] ) . reduce < Array < GearboxLmReward > > (
475+ ( acc , chainRewards ) => {
476+ chainRewards . rewards . forEach ( reward => {
477+ const rewardToken = reward . token . address . toLowerCase ( ) as Address ;
478+
479+ reward . breakdowns . forEach ( reason => {
480+ const poolToken = (
481+ ( reason . reason || "" ) . split ( "_" ) [ 1 ] || ""
482+ ) . toLowerCase ( ) as Address ;
483+
484+ const pool = (
485+ baseRewardPoolsInfo [ poolToken ] ?. pool ||
486+ poolToken ||
487+ ""
488+ ) . toLowerCase ( ) as Address ;
489+
490+ if ( poolByItsToken [ poolToken ] && poolByItsToken [ pool ] ) {
491+ const total = toBigInt ( reason . amount || 0 ) ;
492+ const claimed = toBigInt ( reason . claimed || 0 ) ;
493+
494+ acc . push ( {
495+ pool,
496+ poolToken,
497+ rewardToken,
498+ rewardTokenSymbol : reward . token . symbol ,
499+ rewardTokenDecimals : reward . token . decimals || 18 ,
500+ amount : BigIntMath . max ( total - claimed , 0n ) ,
501+ type : "extraMerkle" ,
502+ } ) ;
503+ }
504+ } ) ;
505+ } ) ;
506+
507+ return acc ;
508+ } ,
509+ [ ] ,
510+ ) ;
471511
472512 const gearboxLmRewards = poolTokens . map ( ( address , i ) : GearboxLmReward => {
513+ const info = baseRewardPoolsInfo [ address ] ;
514+ const rewardToken = currentTokenData [ info . symbol ] ;
515+
473516 return {
474- pool : baseRewardPoolsInfo [ address ] . pool ,
517+ pool : info . pool ,
475518 poolToken : address ,
476- rewardToken : currentTokenData [ baseRewardPoolsInfo [ address ] . symbol ] ,
519+
520+ rewardToken,
521+ rewardTokenDecimals : tokensList [ rewardToken ] ?. decimals || 18 ,
522+ rewardTokenSymbol : info . symbol ,
523+
477524 amount : gearboxLm [ i ] || 0n ,
478525 type : "stakedV3" ,
479526 } ;
0 commit comments