-
Notifications
You must be signed in to change notification settings - Fork 491
Expand file tree
/
Copy pathIncentivesCard.tsx
More file actions
216 lines (200 loc) · 6.69 KB
/
Copy pathIncentivesCard.tsx
File metadata and controls
216 lines (200 loc) · 6.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import { ProtocolAction } from '@aave/contract-helpers';
import { ReserveIncentiveResponse } from '@aave/math-utils/dist/esm/formatters/incentive/calculate-reserve-incentives';
import { Box, Typography } from '@mui/material';
import { useRouter } from 'next/router';
import { ReactNode } from 'react';
import { ENABLE_SELF_CAMPAIGN, useMeritIncentives } from 'src/hooks/useMeritIncentives';
import { useMerklIncentives } from 'src/hooks/useMerklIncentives';
import { useMerklPointsIncentives } from 'src/hooks/useMerklPointsIncentives';
import { convertAprToApy } from 'src/utils/utils';
import { FormattedNumber } from '../primitives/FormattedNumber';
import { NoData } from '../primitives/NoData';
import {
EthenaIncentivesButton,
EtherfiIncentivesButton,
IncentivesButton,
MeritIncentivesButton,
MerklIncentivesButton,
SonicIncentivesButton,
} from './IncentivesButton';
interface IncentivesCardProps {
symbol: string;
value: string | number;
incentives?: ReserveIncentiveResponse[];
/** aToken / vToken address (legacy; hook resolves underlying internally). */
address?: string;
variant?: 'main14' | 'main16' | 'secondary14';
symbolsVariant?: 'secondary14' | 'secondary16';
color?: string;
tooltip?: ReactNode;
market: string;
protocolAction?: ProtocolAction;
align?: 'center' | 'flex-end';
inlineIncentives?: boolean;
}
export const IncentivesCard = ({
symbol,
value,
incentives,
address,
variant = 'secondary14',
symbolsVariant,
align,
color,
tooltip,
market,
protocolAction,
inlineIncentives = false,
}: IncentivesCardProps) => {
const router = useRouter();
const protocolAPY = typeof value === 'string' ? parseFloat(value) : value;
const protocolIncentivesAPR =
incentives?.reduce((sum, inc) => {
if (inc.incentiveAPR === 'Infinity' || sum === 'Infinity') {
return 'Infinity';
}
return sum + +inc.incentiveAPR;
}, 0 as number | 'Infinity') || 0;
const protocolIncentivesAPY = convertAprToApy(
protocolIncentivesAPR === 'Infinity' ? 0 : protocolIncentivesAPR
);
const { data: meritIncentives } = useMeritIncentives({
symbol,
market,
protocolAction,
protocolAPY,
protocolIncentives: incentives || [],
});
const { data: merklIncentives } = useMerklIncentives({
market,
rewardedAsset: address,
protocolAction,
protocolAPY,
protocolIncentives: incentives || [],
});
const { data: merklPointsIncentives } = useMerklPointsIncentives({
market,
rewardedAsset: address,
protocolAction,
protocolAPY,
protocolIncentives: incentives || [],
});
const meritIncentivesAPR = meritIncentives?.breakdown?.meritIncentivesAPR || 0;
// TODO: This is a one-off for the Self campaign.
// Remove once the Self incentives are finished.
const selfAPY = ENABLE_SELF_CAMPAIGN ? meritIncentives?.variants?.selfAPY ?? 0 : 0;
const totalMeritAPY = meritIncentivesAPR + selfAPY;
const merklIncentivesAPR = merklPointsIncentives?.breakdown?.points
? merklPointsIncentives.breakdown.merklIncentivesAPR || 0
: merklIncentives?.breakdown?.merklIncentivesAPR || 0;
const isBorrow = protocolAction === ProtocolAction.borrow;
// If any incentive is infinite, the total should be infinite
const hasInfiniteIncentives = protocolIncentivesAPR === 'Infinity';
const displayAPY = hasInfiniteIncentives
? 'Infinity'
: isBorrow
? protocolAPY - (protocolIncentivesAPY as number) - totalMeritAPY - merklIncentivesAPR
: protocolAPY + (protocolIncentivesAPY as number) + totalMeritAPY + merklIncentivesAPR;
const isSghoPage =
typeof router?.asPath === 'string' && router.asPath.toLowerCase().startsWith('/sgho');
const hideMeritValue = symbol === 'GHO' && !isSghoPage;
const isMarketsOrDashboardPage =
typeof router?.pathname === 'string' &&
(router.pathname.startsWith('/dashboard') || router.pathname.startsWith('/markets'));
// Hide GHO Merkl value on dashboard/markets; show only the badge icon.
const hideMerklValue =
symbol === 'GHO' &&
(protocolAction === ProtocolAction.borrow || protocolAction === ProtocolAction.supply) &&
isMarketsOrDashboardPage;
const incentivesContent = (
<>
<IncentivesButton
incentives={incentives}
symbol={symbol}
market={market}
protocolAction={protocolAction}
protocolAPY={protocolAPY}
address={address}
/>
<MeritIncentivesButton
symbol={symbol}
market={market}
protocolAction={protocolAction}
protocolAPY={protocolAPY}
protocolIncentives={incentives || []}
hideValue={hideMeritValue}
/>
<MerklIncentivesButton
market={market}
rewardedAsset={address}
protocolAction={protocolAction}
protocolAPY={protocolAPY}
protocolIncentives={incentives || []}
hideValue={hideMerklValue}
/>
<EthenaIncentivesButton rewardedAsset={address} />
<EtherfiIncentivesButton symbol={symbol} market={market} protocolAction={protocolAction} />
<SonicIncentivesButton rewardedAsset={address} />
</>
);
return (
<Box
sx={{
display: 'flex',
flexDirection: inlineIncentives ? 'row' : 'column',
alignItems: inlineIncentives ? 'center' : align || { xs: 'flex-end', xsm: 'center' },
justifyContent: inlineIncentives ? 'flex-start' : 'center',
textAlign: inlineIncentives ? 'left' : 'center',
gap: inlineIncentives ? 1 : 1,
}}
>
{value.toString() !== '-1' ? (
<Box sx={{ display: 'flex', alignItems: 'center', gap: 1 }}>
{displayAPY === 'Infinity' ? (
<Typography variant={variant} color={color || 'text.secondary'}>
∞ %
</Typography>
) : (
<FormattedNumber
data-cy={`apy`}
value={displayAPY}
percent
variant={variant}
symbolsVariant={symbolsVariant}
color={color}
symbolsColor={color}
/>
)}
{tooltip}
{inlineIncentives && (
<Box
sx={{
display: 'flex',
gap: '4px',
flexWrap: 'wrap',
alignItems: 'center',
}}
>
{incentivesContent}
</Box>
)}
</Box>
) : (
<NoData variant={variant} color={color || 'text.secondary'} />
)}
{!inlineIncentives && (
<Box
sx={{
display: 'flex',
gap: '4px',
flexWrap: 'wrap',
alignItems: 'center',
justifyContent: 'center',
}}
>
{incentivesContent}
</Box>
)}
</Box>
);
};