@@ -3,7 +3,11 @@ import { Network } from '@oasisdex/dma-library'
3
3
import { networkIdToLibraryNetwork } from 'actions/aave-like/helpers'
4
4
import type BigNumber from 'bignumber.js'
5
5
import { encodeClaimAllRewards , getAllUserRewards } from 'blockchain/better-calls/aave-like-rewards'
6
- import { encodeTransferToOwnerProxyAction , tokenBalance } from 'blockchain/better-calls/erc20'
6
+ import {
7
+ encodeApproveAndWrapProxyAction ,
8
+ encodeTransferToOwnerProxyAction ,
9
+ tokenBalance ,
10
+ } from 'blockchain/better-calls/erc20'
7
11
import { NetworkIds } from 'blockchain/networks'
8
12
import { tokenPriceStore } from 'blockchain/prices.constants'
9
13
import { getTokenByAddress } from 'blockchain/tokensMetadata'
@@ -17,7 +21,13 @@ import React, { useEffect, useReducer } from 'react'
17
21
import { OmniDetailsSectionContentRewardsLoadingState } from './OmniDetailsSectionContentRewardsLoadingState'
18
22
import { OmniRewardsClaims } from './OmniRewardsClaims'
19
23
20
- const claimableErc20 : Record < NetworkIds , string [ ] > = {
24
+ interface OmniDetailSectionRewardsClaimsInternalProps {
25
+ isEligibleForErc20Claims : boolean
26
+ isEligibleForProtocolRewards : boolean
27
+ isEligibleForMorphoLegacy : boolean
28
+ }
29
+
30
+ const claimableErc20ByNetwork : Record < NetworkIds , string [ ] > = {
21
31
[ NetworkIds . MAINNET ] : [ 'ENA' , 'SENA' ] ,
22
32
[ NetworkIds . OPTIMISMMAINNET ] : [ ] ,
23
33
[ NetworkIds . ARBITRUMMAINNET ] : [ ] ,
@@ -32,13 +42,21 @@ const claimableErc20: Record<NetworkIds, string[]> = {
32
42
[ NetworkIds . OPTIMISMGOERLI ] : [ ] ,
33
43
}
34
44
45
+ const morphoLegacyByNetwork : Partial < Record < NetworkIds , string > > = {
46
+ [ NetworkIds . MAINNET ] : 'MORPHO_LEGACY' ,
47
+ }
48
+
35
49
type Claim = {
36
50
token : string
37
51
claimable : BigNumber
38
52
tx : OmniTxData
39
53
}
40
54
41
- const OmniDetailSectionRewardsClaimsInternal : FC = ( ) => {
55
+ const OmniDetailSectionRewardsClaimsInternal : FC < OmniDetailSectionRewardsClaimsInternalProps > = ( {
56
+ isEligibleForErc20Claims,
57
+ isEligibleForProtocolRewards,
58
+ isEligibleForMorphoLegacy,
59
+ } ) => {
42
60
const {
43
61
environment : { dpmProxy, networkId, protocol, quoteAddress } ,
44
62
} = useOmniGeneralContext ( )
@@ -48,9 +66,9 @@ const OmniDetailSectionRewardsClaimsInternal: FC = () => {
48
66
} , [ ] )
49
67
50
68
useEffect ( ( ) => {
51
- if ( dpmProxy ) {
52
- // Existing ERC20 claims logic
53
- claimableErc20 [ networkId ] . forEach ( ( token ) => {
69
+ if ( ! dpmProxy ) return
70
+ if ( isEligibleForErc20Claims ) {
71
+ claimableErc20ByNetwork [ networkId ] . forEach ( ( token ) => {
54
72
tokenBalance ( { token, account : dpmProxy , networkId : networkId } )
55
73
. then ( ( balance ) => {
56
74
if ( balance . gt ( zero ) ) {
@@ -72,64 +90,93 @@ const OmniDetailSectionRewardsClaimsInternal: FC = () => {
72
90
console . error ( `Error fetching token balance for ${ token } : ${ error } ` )
73
91
} )
74
92
} )
75
-
76
- // New Aave and Spark rewards check
77
- if ( [ LendingProtocol . AaveV3 , LendingProtocol . SparkV3 ] . includes ( protocol ) ) {
78
- let rewardsControllerAddress : string | undefined
79
- let poolDataProviderAddress : string | undefined
93
+ }
94
+ if ( isEligibleForMorphoLegacy ) {
95
+ const morphoLegacyToken = morphoLegacyByNetwork [ networkId ]
96
+ if ( morphoLegacyToken ) {
80
97
const network = networkIdToLibraryNetwork ( networkId )
81
- if (
82
- protocol === LendingProtocol . AaveV3 &&
83
- network !== Network . HARDHAT &&
84
- network !== Network . LOCAL &&
85
- network !== Network . TENDERLY
86
- ) {
87
- rewardsControllerAddress = ADDRESSES [ network ] . aave . v3 . RewardsController
88
- poolDataProviderAddress = ADDRESSES [ network ] . aave . v3 . PoolDataProvider
89
- } else if (
90
- protocol === LendingProtocol . SparkV3 &&
91
- network !== Network . HARDHAT &&
92
- network !== Network . LOCAL &&
93
- network !== Network . TENDERLY
94
- ) {
95
- rewardsControllerAddress = ADDRESSES [ network ] . spark . RewardsController
96
- poolDataProviderAddress = ADDRESSES [ network ] . spark . PoolDataProvider
97
- } else {
98
- console . warn ( `Unsupported protocol or network for rewards: ${ protocol } on ${ network } ` )
99
- throw new Error ( `Unsupported protocol or network for rewards: ${ protocol } on ${ network } ` )
98
+ if ( network === Network . MAINNET ) {
99
+ tokenBalance ( { token : morphoLegacyToken , account : dpmProxy , networkId } )
100
+ . then ( ( balance ) => {
101
+ if ( balance . gt ( zero ) ) {
102
+ encodeApproveAndWrapProxyAction ( {
103
+ oldToken : morphoLegacyToken ,
104
+ newToken : 'MORPHO' ,
105
+ wrapper : ADDRESSES [ network ] . morphoblue . Wrapper ,
106
+ amount : balance ,
107
+ networkId,
108
+ } )
109
+ . then ( ( tx ) => {
110
+ dispatchClaim ( { token : 'MORPHO' , claimable : balance , tx } )
111
+ } )
112
+ . catch ( ( error ) => {
113
+ console . error (
114
+ `Error encoding approve and wrap action for MORPHO_LEGACY: ${ error } ` ,
115
+ )
116
+ } )
117
+ }
118
+ } )
119
+ . catch ( ( error ) => {
120
+ console . error ( `Error fetching MORPHO_LEGACY balance: ${ error } ` )
121
+ } )
100
122
}
123
+ }
124
+ }
125
+ if ( isEligibleForProtocolRewards ) {
126
+ let rewardsControllerAddress : string | undefined
127
+ let poolDataProviderAddress : string | undefined
128
+ const network = networkIdToLibraryNetwork ( networkId )
129
+ if (
130
+ protocol === LendingProtocol . AaveV3 &&
131
+ network !== Network . HARDHAT &&
132
+ network !== Network . LOCAL &&
133
+ network !== Network . TENDERLY
134
+ ) {
135
+ rewardsControllerAddress = ADDRESSES [ network ] . aave . v3 . RewardsController
136
+ poolDataProviderAddress = ADDRESSES [ network ] . aave . v3 . PoolDataProvider
137
+ } else if (
138
+ protocol === LendingProtocol . SparkV3 &&
139
+ network !== Network . HARDHAT &&
140
+ network !== Network . LOCAL &&
141
+ network !== Network . TENDERLY
142
+ ) {
143
+ rewardsControllerAddress = ADDRESSES [ network ] . spark . RewardsController
144
+ poolDataProviderAddress = ADDRESSES [ network ] . spark . PoolDataProvider
145
+ } else {
146
+ console . warn ( `Unsupported protocol or network for rewards: ${ protocol } on ${ network } ` )
147
+ throw new Error ( `Unsupported protocol or network for rewards: ${ protocol } on ${ network } ` )
148
+ }
101
149
102
- getAllUserRewards ( {
103
- networkId,
104
- token : quoteAddress ,
105
- account : dpmProxy ,
106
- rewardsController : rewardsControllerAddress as string ,
107
- poolDataProvider : poolDataProviderAddress as string ,
108
- } )
109
- . then ( async ( { rewardsList, unclaimedAmounts, assets } ) => {
110
- if ( unclaimedAmounts . some ( ( amount ) => amount . gt ( zero ) ) ) {
111
- const tx = encodeClaimAllRewards ( {
112
- networkId,
113
- assets : assets as string [ ] ,
114
- dpmAccount : dpmProxy ,
115
- rewardsController : rewardsControllerAddress as string ,
116
- } )
150
+ getAllUserRewards ( {
151
+ networkId,
152
+ token : quoteAddress ,
153
+ account : dpmProxy ,
154
+ rewardsController : rewardsControllerAddress as string ,
155
+ poolDataProvider : poolDataProviderAddress as string ,
156
+ } )
157
+ . then ( async ( { rewardsList, unclaimedAmounts, assets } ) => {
158
+ if ( unclaimedAmounts . some ( ( amount ) => amount . gt ( zero ) ) ) {
159
+ const tx = encodeClaimAllRewards ( {
160
+ networkId,
161
+ assets : assets as string [ ] ,
162
+ dpmAccount : dpmProxy ,
163
+ rewardsController : rewardsControllerAddress as string ,
164
+ } )
117
165
118
- rewardsList . forEach ( ( token , index ) => {
119
- if ( unclaimedAmounts [ index ] . gt ( zero ) ) {
120
- dispatchClaim ( {
121
- token : getTokenByAddress ( token , networkId ) . symbol ,
122
- claimable : unclaimedAmounts [ index ] ,
123
- tx,
124
- } )
125
- }
126
- } )
127
- }
128
- } )
129
- . catch ( ( error ) => {
130
- console . error ( `Error fetching ${ protocol } rewards:` , error )
131
- } )
132
- }
166
+ rewardsList . forEach ( ( token , index ) => {
167
+ if ( unclaimedAmounts [ index ] . gt ( zero ) ) {
168
+ dispatchClaim ( {
169
+ token : getTokenByAddress ( token , networkId ) . symbol ,
170
+ claimable : unclaimedAmounts [ index ] ,
171
+ tx,
172
+ } )
173
+ }
174
+ } )
175
+ }
176
+ } )
177
+ . catch ( ( error ) => {
178
+ console . error ( `Error fetching ${ protocol } rewards:` , error )
179
+ } )
133
180
}
134
181
} , [ dpmProxy , networkId , protocol , quoteAddress ] )
135
182
@@ -152,19 +199,33 @@ const OmniDetailSectionRewardsClaimsInternal: FC = () => {
152
199
153
200
export const OmniDetailSectionRewardsClaims : FC = ( ) => {
154
201
const {
155
- environment : { protocol, collateralToken, quoteToken } ,
202
+ environment : { protocol, collateralToken, quoteToken, networkId } ,
156
203
} = useOmniGeneralContext ( )
157
204
158
- const eligibleTokens = [ 'SUSDE' , 'USDE' , 'WETH' , 'ETH' ]
205
+ const rewardsEligibleTokens = [ 'SUSDE' , 'USDE' , 'WETH' , 'ETH' ]
206
+
207
+ // Regular ERC20 claims eligibility
208
+ const isEligibleForErc20Claims = claimableErc20ByNetwork [ networkId ] . length > 0
209
+
210
+ // Aave/Spark rewards eligibility
211
+ const isEligibleForProtocolRewards =
212
+ [ LendingProtocol . AaveV3 , LendingProtocol . SparkV3 ] . includes ( protocol ) &&
213
+ ( rewardsEligibleTokens . includes ( collateralToken ) || rewardsEligibleTokens . includes ( quoteToken ) )
159
214
160
- const isEligible =
161
- [
162
- LendingProtocol . MorphoBlue ,
163
- LendingProtocol . Ajna ,
164
- LendingProtocol . AaveV3 ,
165
- LendingProtocol . SparkV3 ,
166
- ] . includes ( protocol ) &&
167
- ( eligibleTokens . includes ( collateralToken ) || eligibleTokens . includes ( quoteToken ) )
215
+ // Legacy Morpho claims eligibility
216
+ const isEligibleForMorphoLegacy =
217
+ networkId === NetworkIds . MAINNET && protocol === LendingProtocol . MorphoBlue
168
218
169
- return isEligible ? < OmniDetailSectionRewardsClaimsInternal /> : < > </ >
219
+ const hasAnyEligibleClaims =
220
+ isEligibleForErc20Claims || isEligibleForProtocolRewards || isEligibleForMorphoLegacy
221
+
222
+ return hasAnyEligibleClaims ? (
223
+ < OmniDetailSectionRewardsClaimsInternal
224
+ isEligibleForErc20Claims = { isEligibleForErc20Claims }
225
+ isEligibleForProtocolRewards = { isEligibleForProtocolRewards }
226
+ isEligibleForMorphoLegacy = { isEligibleForMorphoLegacy }
227
+ />
228
+ ) : (
229
+ < > </ >
230
+ )
170
231
}
0 commit comments