@@ -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,97 @@ 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
+ console . log ( 'morphoLegacyToken' , morphoLegacyToken )
97
+ if ( morphoLegacyToken ) {
98
+ console . log ( 'morphoLegacyToken' , morphoLegacyToken )
80
99
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 } ` )
100
+ console . log ( 'network' , network )
101
+ if ( network === Network . MAINNET ) {
102
+ tokenBalance ( { token : morphoLegacyToken , account : dpmProxy , networkId } )
103
+ . then ( ( balance ) => {
104
+ console . log ( 'balance mmm' , balance )
105
+ if ( balance . gt ( zero ) ) {
106
+ encodeApproveAndWrapProxyAction ( {
107
+ oldToken : morphoLegacyToken ,
108
+ newToken : 'MORPHO' ,
109
+ wrapper : ADDRESSES [ network ] . morphoblue . Wrapper ,
110
+ amount : balance ,
111
+ networkId,
112
+ } )
113
+ . then ( ( tx ) => {
114
+ dispatchClaim ( { token : 'MORPHO' , claimable : balance , tx } )
115
+ } )
116
+ . catch ( ( error ) => {
117
+ console . error (
118
+ `Error encoding approve and wrap action for MORPHO_LEGACY: ${ error } ` ,
119
+ )
120
+ } )
121
+ }
122
+ } )
123
+ . catch ( ( error ) => {
124
+ console . error ( `Error fetching MORPHO_LEGACY balance: ${ error } ` )
125
+ } )
100
126
}
127
+ }
128
+ }
129
+ if ( isEligibleForProtocolRewards ) {
130
+ let rewardsControllerAddress : string | undefined
131
+ let poolDataProviderAddress : string | undefined
132
+ const network = networkIdToLibraryNetwork ( networkId )
133
+ if (
134
+ protocol === LendingProtocol . AaveV3 &&
135
+ network !== Network . HARDHAT &&
136
+ network !== Network . LOCAL &&
137
+ network !== Network . TENDERLY
138
+ ) {
139
+ rewardsControllerAddress = ADDRESSES [ network ] . aave . v3 . RewardsController
140
+ poolDataProviderAddress = ADDRESSES [ network ] . aave . v3 . PoolDataProvider
141
+ } else if (
142
+ protocol === LendingProtocol . SparkV3 &&
143
+ network !== Network . HARDHAT &&
144
+ network !== Network . LOCAL &&
145
+ network !== Network . TENDERLY
146
+ ) {
147
+ rewardsControllerAddress = ADDRESSES [ network ] . spark . RewardsController
148
+ poolDataProviderAddress = ADDRESSES [ network ] . spark . PoolDataProvider
149
+ } else {
150
+ console . warn ( `Unsupported protocol or network for rewards: ${ protocol } on ${ network } ` )
151
+ throw new Error ( `Unsupported protocol or network for rewards: ${ protocol } on ${ network } ` )
152
+ }
101
153
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
- } )
154
+ getAllUserRewards ( {
155
+ networkId,
156
+ token : quoteAddress ,
157
+ account : dpmProxy ,
158
+ rewardsController : rewardsControllerAddress as string ,
159
+ poolDataProvider : poolDataProviderAddress as string ,
160
+ } )
161
+ . then ( async ( { rewardsList, unclaimedAmounts, assets } ) => {
162
+ if ( unclaimedAmounts . some ( ( amount ) => amount . gt ( zero ) ) ) {
163
+ const tx = encodeClaimAllRewards ( {
164
+ networkId,
165
+ assets : assets as string [ ] ,
166
+ dpmAccount : dpmProxy ,
167
+ rewardsController : rewardsControllerAddress as string ,
168
+ } )
117
169
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
- }
170
+ rewardsList . forEach ( ( token , index ) => {
171
+ if ( unclaimedAmounts [ index ] . gt ( zero ) ) {
172
+ dispatchClaim ( {
173
+ token : getTokenByAddress ( token , networkId ) . symbol ,
174
+ claimable : unclaimedAmounts [ index ] ,
175
+ tx,
176
+ } )
177
+ }
178
+ } )
179
+ }
180
+ } )
181
+ . catch ( ( error ) => {
182
+ console . error ( `Error fetching ${ protocol } rewards:` , error )
183
+ } )
133
184
}
134
185
} , [ dpmProxy , networkId , protocol , quoteAddress ] )
135
186
@@ -152,19 +203,33 @@ const OmniDetailSectionRewardsClaimsInternal: FC = () => {
152
203
153
204
export const OmniDetailSectionRewardsClaims : FC = ( ) => {
154
205
const {
155
- environment : { protocol, collateralToken, quoteToken } ,
206
+ environment : { protocol, collateralToken, quoteToken, networkId } ,
156
207
} = useOmniGeneralContext ( )
157
208
158
- const eligibleTokens = [ 'SUSDE' , 'USDE' , 'WETH' , 'ETH' ]
209
+ const rewardsEligibleTokens = [ 'SUSDE' , 'USDE' , 'WETH' , 'ETH' ]
210
+
211
+ // Regular ERC20 claims eligibility
212
+ const isEligibleForErc20Claims = claimableErc20ByNetwork [ networkId ] . length > 0
213
+
214
+ // Aave/Spark rewards eligibility
215
+ const isEligibleForProtocolRewards =
216
+ [ LendingProtocol . AaveV3 , LendingProtocol . SparkV3 ] . includes ( protocol ) &&
217
+ ( rewardsEligibleTokens . includes ( collateralToken ) || rewardsEligibleTokens . includes ( quoteToken ) )
159
218
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 ) )
219
+ // Legacy Morpho claims eligibility
220
+ const isEligibleForMorphoLegacy =
221
+ networkId === NetworkIds . MAINNET && protocol === LendingProtocol . MorphoBlue
168
222
169
- return isEligible ? < OmniDetailSectionRewardsClaimsInternal /> : < > </ >
223
+ const hasAnyEligibleClaims =
224
+ isEligibleForErc20Claims || isEligibleForProtocolRewards || isEligibleForMorphoLegacy
225
+
226
+ return hasAnyEligibleClaims ? (
227
+ < OmniDetailSectionRewardsClaimsInternal
228
+ isEligibleForErc20Claims = { isEligibleForErc20Claims }
229
+ isEligibleForProtocolRewards = { isEligibleForProtocolRewards }
230
+ isEligibleForMorphoLegacy = { isEligibleForMorphoLegacy }
231
+ />
232
+ ) : (
233
+ < > </ >
234
+ )
170
235
}
0 commit comments