-
Notifications
You must be signed in to change notification settings - Fork 480
Expand file tree
/
Copy pathuseIncentivizedApy.ts
More file actions
92 lines (78 loc) · 3.12 KB
/
useIncentivizedApy.ts
File metadata and controls
92 lines (78 loc) · 3.12 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
import { ProtocolAction } from '@aave/contract-helpers';
import { ReserveIncentiveResponse } from '@aave/math-utils/dist/esm/formatters/incentive/calculate-reserve-incentives';
import { ENABLE_SELF_CAMPAIGN, useMeritIncentives } from 'src/hooks/useMeritIncentives';
import { convertAprToApy } from 'src/utils/utils';
import { useMerklIncentives } from './useMerklIncentives';
import { useMerklPointsIncentives } from './useMerklPointsIncentives';
interface IncentivizedApyParams {
symbol: string;
market: string;
rewardedAsset: string;
protocolAction?: ProtocolAction;
protocolAPY: number | string;
protocolIncentives?: ReserveIncentiveResponse[];
}
type UseIncentivizedApyResult = {
displayAPY: number | 'Infinity';
hasInfiniteIncentives: boolean;
isLoading: boolean;
};
export const useIncentivizedApy = ({
symbol,
market,
rewardedAsset: address,
protocolAction,
protocolAPY: value,
protocolIncentives: incentives = [],
}: IncentivizedApyParams): UseIncentivizedApyResult => {
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, isLoading: meritLoading } = useMeritIncentives({
symbol,
market,
protocolAction,
protocolAPY,
protocolIncentives: incentives || [],
});
const { data: merklIncentives, isLoading: merklLoading } = useMerklIncentives({
market,
rewardedAsset: address,
protocolAction,
protocolAPY,
protocolIncentives: incentives || [],
});
const { data: merklPointsIncentives, isLoading: merklPointsLoading } = useMerklPointsIncentives({
market,
rewardedAsset: address,
protocolAction,
protocolAPY,
protocolIncentives: incentives || [],
});
const isLoading = meritLoading || merklLoading || merklPointsLoading;
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;
return { displayAPY, hasInfiniteIncentives, isLoading };
};