@defisaver/positions-sdk 2.1.52-dev-3 → 2.1.53-aave-v4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.mocharc.json +4 -4
- package/.nvmrc +1 -1
- package/README.md +64 -64
- package/cjs/aaveV4/index.d.ts +7 -0
- package/cjs/aaveV4/index.js +174 -0
- package/cjs/config/contracts.d.ts +1277 -0
- package/cjs/config/contracts.js +9 -0
- package/cjs/contracts.d.ts +23120 -0
- package/cjs/contracts.js +2 -1
- package/cjs/fluid/index.d.ts +3 -3
- package/cjs/helpers/aaveV4Helpers/index.d.ts +13 -0
- package/cjs/helpers/aaveV4Helpers/index.js +117 -0
- package/cjs/helpers/index.d.ts +1 -0
- package/cjs/helpers/index.js +2 -1
- package/cjs/helpers/morphoBlueHelpers/index.js +66 -66
- package/cjs/index.d.ts +2 -1
- package/cjs/index.js +3 -1
- package/cjs/markets/aaveV4/index.d.ts +13 -0
- package/cjs/markets/aaveV4/index.js +39 -0
- package/cjs/markets/index.d.ts +1 -0
- package/cjs/markets/index.js +3 -1
- package/cjs/portfolio/index.js +20 -0
- package/cjs/savings/morphoVaults/index.js +17 -17
- package/cjs/types/aaveV4.d.ts +137 -0
- package/cjs/types/aaveV4.js +11 -0
- package/cjs/types/index.d.ts +2 -0
- package/cjs/types/index.js +2 -0
- package/cjs/types/portfolio.d.ts +4 -0
- package/esm/aaveV4/index.d.ts +7 -0
- package/esm/aaveV4/index.js +165 -0
- package/esm/config/contracts.d.ts +1277 -0
- package/esm/config/contracts.js +8 -0
- package/esm/contracts.d.ts +23120 -0
- package/esm/contracts.js +1 -0
- package/esm/fluid/index.d.ts +3 -3
- package/esm/helpers/aaveV4Helpers/index.d.ts +13 -0
- package/esm/helpers/aaveV4Helpers/index.js +108 -0
- package/esm/helpers/index.d.ts +1 -0
- package/esm/helpers/index.js +1 -0
- package/esm/helpers/morphoBlueHelpers/index.js +66 -66
- package/esm/index.d.ts +2 -1
- package/esm/index.js +2 -1
- package/esm/markets/aaveV4/index.d.ts +13 -0
- package/esm/markets/aaveV4/index.js +29 -0
- package/esm/markets/index.d.ts +1 -0
- package/esm/markets/index.js +1 -0
- package/esm/portfolio/index.js +21 -1
- package/esm/savings/morphoVaults/index.js +17 -17
- package/esm/types/aaveV4.d.ts +137 -0
- package/esm/types/aaveV4.js +8 -0
- package/esm/types/index.d.ts +2 -0
- package/esm/types/index.js +2 -0
- package/esm/types/portfolio.d.ts +4 -0
- package/package.json +48 -48
- package/src/aaveV2/index.ts +240 -240
- package/src/aaveV3/index.ts +635 -635
- package/src/aaveV3/merit.ts +97 -97
- package/src/aaveV3/merkl.ts +74 -74
- package/src/aaveV4/index.ts +176 -0
- package/src/claiming/aaveV3.ts +154 -154
- package/src/claiming/compV3.ts +22 -22
- package/src/claiming/ethena.ts +61 -61
- package/src/claiming/index.ts +12 -12
- package/src/claiming/king.ts +66 -66
- package/src/claiming/morphoBlue.ts +118 -118
- package/src/claiming/spark.ts +225 -225
- package/src/compoundV2/index.ts +244 -244
- package/src/compoundV3/index.ts +274 -274
- package/src/config/contracts.ts +1328 -1320
- package/src/constants/index.ts +10 -10
- package/src/contracts.ts +174 -172
- package/src/curveUsd/index.ts +254 -254
- package/src/eulerV2/index.ts +324 -324
- package/src/exchange/index.ts +25 -25
- package/src/fluid/index.ts +1800 -1800
- package/src/helpers/aaveHelpers/index.ts +202 -202
- package/src/helpers/aaveV4Helpers/index.ts +128 -0
- package/src/helpers/compoundHelpers/index.ts +276 -276
- package/src/helpers/curveUsdHelpers/index.ts +40 -40
- package/src/helpers/eulerHelpers/index.ts +229 -229
- package/src/helpers/fluidHelpers/index.ts +335 -335
- package/src/helpers/index.ts +11 -10
- package/src/helpers/liquityV2Helpers/index.ts +82 -82
- package/src/helpers/llamaLendHelpers/index.ts +53 -53
- package/src/helpers/makerHelpers/index.ts +52 -52
- package/src/helpers/morphoBlueHelpers/index.ts +405 -405
- package/src/helpers/sparkHelpers/index.ts +169 -169
- package/src/index.ts +51 -49
- package/src/liquity/index.ts +159 -159
- package/src/liquityV2/index.ts +703 -703
- package/src/llamaLend/index.ts +305 -305
- package/src/maker/index.ts +223 -223
- package/src/markets/aave/index.ts +118 -118
- package/src/markets/aave/marketAssets.ts +54 -54
- package/src/markets/aaveV4/index.ts +42 -0
- package/src/markets/compound/index.ts +243 -243
- package/src/markets/compound/marketsAssets.ts +97 -97
- package/src/markets/curveUsd/index.ts +69 -69
- package/src/markets/euler/index.ts +26 -26
- package/src/markets/fluid/index.ts +2900 -2900
- package/src/markets/index.ts +26 -25
- package/src/markets/liquityV2/index.ts +102 -102
- package/src/markets/llamaLend/contractAddresses.ts +141 -141
- package/src/markets/llamaLend/index.ts +235 -235
- package/src/markets/morphoBlue/index.ts +988 -988
- package/src/markets/spark/index.ts +29 -29
- package/src/markets/spark/marketAssets.ts +12 -12
- package/src/moneymarket/moneymarketCommonService.ts +84 -84
- package/src/morphoBlue/index.ts +274 -274
- package/src/portfolio/index.ts +606 -586
- package/src/savings/index.ts +95 -95
- package/src/savings/makerDsr/index.ts +53 -53
- package/src/savings/makerDsr/options.ts +9 -9
- package/src/savings/morphoVaults/index.ts +80 -80
- package/src/savings/morphoVaults/options.ts +193 -193
- package/src/savings/skyOptions/index.ts +95 -95
- package/src/savings/skyOptions/options.ts +10 -10
- package/src/savings/sparkSavingsVaults/index.ts +60 -60
- package/src/savings/sparkSavingsVaults/options.ts +35 -35
- package/src/savings/yearnV3Vaults/index.ts +61 -61
- package/src/savings/yearnV3Vaults/options.ts +55 -55
- package/src/savings/yearnVaults/index.ts +73 -73
- package/src/savings/yearnVaults/options.ts +32 -32
- package/src/services/priceService.ts +278 -278
- package/src/services/utils.ts +115 -115
- package/src/services/viem.ts +57 -57
- package/src/setup.ts +8 -8
- package/src/spark/index.ts +459 -459
- package/src/staking/eligibility.ts +53 -53
- package/src/staking/index.ts +1 -1
- package/src/staking/staking.ts +192 -192
- package/src/types/aave.ts +199 -199
- package/src/types/aaveV4.ts +153 -0
- package/src/types/claiming.ts +114 -114
- package/src/types/common.ts +115 -115
- package/src/types/compound.ts +145 -145
- package/src/types/curveUsd.ts +123 -123
- package/src/types/euler.ts +176 -176
- package/src/types/fluid.ts +485 -485
- package/src/types/index.ts +17 -15
- package/src/types/liquity.ts +30 -30
- package/src/types/liquityV2.ts +128 -128
- package/src/types/llamaLend.ts +161 -161
- package/src/types/maker.ts +63 -63
- package/src/types/merit.ts +1 -1
- package/src/types/merkl.ts +70 -70
- package/src/types/morphoBlue.ts +202 -202
- package/src/types/portfolio.ts +64 -60
- package/src/types/savings/index.ts +23 -23
- package/src/types/savings/makerDsr.ts +13 -13
- package/src/types/savings/morphoVaults.ts +32 -32
- package/src/types/savings/sky.ts +14 -14
- package/src/types/savings/sparkSavingsVaults.ts +15 -15
- package/src/types/savings/yearnV3Vaults.ts +17 -17
- package/src/types/savings/yearnVaults.ts +14 -14
- package/src/types/spark.ts +135 -135
- package/src/umbrella/index.ts +69 -69
- package/src/umbrella/umbrellaUtils.ts +29 -29
package/src/aaveV3/merit.ts
CHANGED
|
@@ -1,97 +1,97 @@
|
|
|
1
|
-
|
|
2
|
-
import { DEFAULT_TIMEOUT } from '../services/utils';
|
|
3
|
-
import { NetworkNumber } from '../types/common';
|
|
4
|
-
import { aprToApy } from '../moneymarket';
|
|
5
|
-
import { AaveVersions, MeritTokenRewardMap } from '../types';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Maps API keys to reward data & actions - hardcoded and needs to be maintained actively.
|
|
9
|
-
* Mapping based on Aave interface implementation: https://github.com/aave/interface/blob/main/src/hooks/useMeritIncentives.ts
|
|
10
|
-
* Active campaigns found here: https://apps.aavechan.com/merit
|
|
11
|
-
*/
|
|
12
|
-
const MERIT_DATA_MAP:
|
|
13
|
-
Record<
|
|
14
|
-
NetworkNumber,
|
|
15
|
-
Partial<Record<
|
|
16
|
-
AaveVersions,
|
|
17
|
-
Record<string, {
|
|
18
|
-
rewardTokenSymbol: string;
|
|
19
|
-
action: 'supply' | 'borrow' | 'stake';
|
|
20
|
-
message?: string;
|
|
21
|
-
supplyTokens?: string[],
|
|
22
|
-
borrowTokens?: string[],
|
|
23
|
-
}>
|
|
24
|
-
>>
|
|
25
|
-
> = {
|
|
26
|
-
[NetworkNumber.Eth]: {
|
|
27
|
-
[AaveVersions.AaveV3]: {
|
|
28
|
-
'ethereum-supply-ethx': { rewardTokenSymbol: 'SD', action: 'supply', supplyTokens: ['ETHx'] },
|
|
29
|
-
'ethereum-supply-rlusd': { rewardTokenSymbol: 'aEthRLUSD', action: 'supply', supplyTokens: ['RLUSD'] },
|
|
30
|
-
// Campaign disabled here as it's present on Merkl API:
|
|
31
|
-
// 'ethereum-borrow-eurc': { rewardTokenSymbol: 'aEthEURC', action: 'borrow', borrowTokens: ['EURC'] },
|
|
32
|
-
},
|
|
33
|
-
[AaveVersions.AaveV3Lido]: {},
|
|
34
|
-
[AaveVersions.AaveV3Etherfi]: {},
|
|
35
|
-
},
|
|
36
|
-
[NetworkNumber.Arb]: {
|
|
37
|
-
[AaveVersions.AaveV3]: {},
|
|
38
|
-
},
|
|
39
|
-
[NetworkNumber.Opt]: {
|
|
40
|
-
[AaveVersions.AaveV3]: {},
|
|
41
|
-
},
|
|
42
|
-
[NetworkNumber.Base]: {
|
|
43
|
-
[AaveVersions.AaveV3]: {
|
|
44
|
-
'base-borrow-usdc': { rewardTokenSymbol: 'USDC', action: 'borrow', borrowTokens: ['USDC'] },
|
|
45
|
-
'base-borrow-gho': { rewardTokenSymbol: 'GHO', action: 'borrow', borrowTokens: ['GHO'] },
|
|
46
|
-
'base-borrow-eurc': { rewardTokenSymbol: 'EURC', action: 'borrow', borrowTokens: ['EURC'] },
|
|
47
|
-
},
|
|
48
|
-
},
|
|
49
|
-
[NetworkNumber.Linea]: {
|
|
50
|
-
[AaveVersions.AaveV3]: {},
|
|
51
|
-
},
|
|
52
|
-
[NetworkNumber.Plasma]: {
|
|
53
|
-
[AaveVersions.AaveV3]: {},
|
|
54
|
-
},
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Fetches merit rewards data from Aave API
|
|
59
|
-
*/
|
|
60
|
-
export const fetchMeritRewardsData = async (): Promise<Record<string, number | null>> => {
|
|
61
|
-
try {
|
|
62
|
-
const response = await fetch('https://apps.aavechan.com/api/merit/aprs', {
|
|
63
|
-
signal: AbortSignal.timeout(5000),
|
|
64
|
-
});
|
|
65
|
-
const data = await response.json();
|
|
66
|
-
return data.currentAPR.actionsAPR as Record<string, number | null>;
|
|
67
|
-
} catch (error) {
|
|
68
|
-
console.error('External API Failure: Failed to fetch merit rewards data:', error);
|
|
69
|
-
return {};
|
|
70
|
-
}
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
export const getMeritCampaigns = async (chainId: NetworkNumber, market: AaveVersions): Promise<MeritTokenRewardMap> => {
|
|
74
|
-
const meritData = await fetchMeritRewardsData();
|
|
75
|
-
const relevantCampaigns = {
|
|
76
|
-
supply: {},
|
|
77
|
-
borrow: {},
|
|
78
|
-
} as MeritTokenRewardMap;
|
|
79
|
-
Object.entries(MERIT_DATA_MAP[chainId]?.[market] || {})
|
|
80
|
-
.filter(([key, rewardData]) => !!meritData[key])
|
|
81
|
-
.forEach(([key, rewardData]) => {
|
|
82
|
-
const apr = meritData[key]!;
|
|
83
|
-
if (!apr) return;
|
|
84
|
-
const reward = {
|
|
85
|
-
rewardTokenSymbol: rewardData.rewardTokenSymbol,
|
|
86
|
-
apy: aprToApy(apr).toString(),
|
|
87
|
-
description: `Eligible for Merit rewards in ${rewardData.rewardTokenSymbol}. ${rewardData.message ? `\n${rewardData.message}` : ''}`,
|
|
88
|
-
};
|
|
89
|
-
rewardData.supplyTokens?.forEach(token => {
|
|
90
|
-
relevantCampaigns.supply[token] = reward;
|
|
91
|
-
});
|
|
92
|
-
rewardData.borrowTokens?.forEach(token => {
|
|
93
|
-
relevantCampaigns.borrow[token] = reward;
|
|
94
|
-
});
|
|
95
|
-
});
|
|
96
|
-
return relevantCampaigns;
|
|
97
|
-
};
|
|
1
|
+
|
|
2
|
+
import { DEFAULT_TIMEOUT } from '../services/utils';
|
|
3
|
+
import { NetworkNumber } from '../types/common';
|
|
4
|
+
import { aprToApy } from '../moneymarket';
|
|
5
|
+
import { AaveVersions, MeritTokenRewardMap } from '../types';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Maps API keys to reward data & actions - hardcoded and needs to be maintained actively.
|
|
9
|
+
* Mapping based on Aave interface implementation: https://github.com/aave/interface/blob/main/src/hooks/useMeritIncentives.ts
|
|
10
|
+
* Active campaigns found here: https://apps.aavechan.com/merit
|
|
11
|
+
*/
|
|
12
|
+
const MERIT_DATA_MAP:
|
|
13
|
+
Record<
|
|
14
|
+
NetworkNumber,
|
|
15
|
+
Partial<Record<
|
|
16
|
+
AaveVersions,
|
|
17
|
+
Record<string, {
|
|
18
|
+
rewardTokenSymbol: string;
|
|
19
|
+
action: 'supply' | 'borrow' | 'stake';
|
|
20
|
+
message?: string;
|
|
21
|
+
supplyTokens?: string[],
|
|
22
|
+
borrowTokens?: string[],
|
|
23
|
+
}>
|
|
24
|
+
>>
|
|
25
|
+
> = {
|
|
26
|
+
[NetworkNumber.Eth]: {
|
|
27
|
+
[AaveVersions.AaveV3]: {
|
|
28
|
+
'ethereum-supply-ethx': { rewardTokenSymbol: 'SD', action: 'supply', supplyTokens: ['ETHx'] },
|
|
29
|
+
'ethereum-supply-rlusd': { rewardTokenSymbol: 'aEthRLUSD', action: 'supply', supplyTokens: ['RLUSD'] },
|
|
30
|
+
// Campaign disabled here as it's present on Merkl API:
|
|
31
|
+
// 'ethereum-borrow-eurc': { rewardTokenSymbol: 'aEthEURC', action: 'borrow', borrowTokens: ['EURC'] },
|
|
32
|
+
},
|
|
33
|
+
[AaveVersions.AaveV3Lido]: {},
|
|
34
|
+
[AaveVersions.AaveV3Etherfi]: {},
|
|
35
|
+
},
|
|
36
|
+
[NetworkNumber.Arb]: {
|
|
37
|
+
[AaveVersions.AaveV3]: {},
|
|
38
|
+
},
|
|
39
|
+
[NetworkNumber.Opt]: {
|
|
40
|
+
[AaveVersions.AaveV3]: {},
|
|
41
|
+
},
|
|
42
|
+
[NetworkNumber.Base]: {
|
|
43
|
+
[AaveVersions.AaveV3]: {
|
|
44
|
+
'base-borrow-usdc': { rewardTokenSymbol: 'USDC', action: 'borrow', borrowTokens: ['USDC'] },
|
|
45
|
+
'base-borrow-gho': { rewardTokenSymbol: 'GHO', action: 'borrow', borrowTokens: ['GHO'] },
|
|
46
|
+
'base-borrow-eurc': { rewardTokenSymbol: 'EURC', action: 'borrow', borrowTokens: ['EURC'] },
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
[NetworkNumber.Linea]: {
|
|
50
|
+
[AaveVersions.AaveV3]: {},
|
|
51
|
+
},
|
|
52
|
+
[NetworkNumber.Plasma]: {
|
|
53
|
+
[AaveVersions.AaveV3]: {},
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Fetches merit rewards data from Aave API
|
|
59
|
+
*/
|
|
60
|
+
export const fetchMeritRewardsData = async (): Promise<Record<string, number | null>> => {
|
|
61
|
+
try {
|
|
62
|
+
const response = await fetch('https://apps.aavechan.com/api/merit/aprs', {
|
|
63
|
+
signal: AbortSignal.timeout(5000),
|
|
64
|
+
});
|
|
65
|
+
const data = await response.json();
|
|
66
|
+
return data.currentAPR.actionsAPR as Record<string, number | null>;
|
|
67
|
+
} catch (error) {
|
|
68
|
+
console.error('External API Failure: Failed to fetch merit rewards data:', error);
|
|
69
|
+
return {};
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
export const getMeritCampaigns = async (chainId: NetworkNumber, market: AaveVersions): Promise<MeritTokenRewardMap> => {
|
|
74
|
+
const meritData = await fetchMeritRewardsData();
|
|
75
|
+
const relevantCampaigns = {
|
|
76
|
+
supply: {},
|
|
77
|
+
borrow: {},
|
|
78
|
+
} as MeritTokenRewardMap;
|
|
79
|
+
Object.entries(MERIT_DATA_MAP[chainId]?.[market] || {})
|
|
80
|
+
.filter(([key, rewardData]) => !!meritData[key])
|
|
81
|
+
.forEach(([key, rewardData]) => {
|
|
82
|
+
const apr = meritData[key]!;
|
|
83
|
+
if (!apr) return;
|
|
84
|
+
const reward = {
|
|
85
|
+
rewardTokenSymbol: rewardData.rewardTokenSymbol,
|
|
86
|
+
apy: aprToApy(apr).toString(),
|
|
87
|
+
description: `Eligible for Merit rewards in ${rewardData.rewardTokenSymbol}. ${rewardData.message ? `\n${rewardData.message}` : ''}`,
|
|
88
|
+
};
|
|
89
|
+
rewardData.supplyTokens?.forEach(token => {
|
|
90
|
+
relevantCampaigns.supply[token] = reward;
|
|
91
|
+
});
|
|
92
|
+
rewardData.borrowTokens?.forEach(token => {
|
|
93
|
+
relevantCampaigns.borrow[token] = reward;
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
return relevantCampaigns;
|
|
97
|
+
};
|
package/src/aaveV3/merkl.ts
CHANGED
|
@@ -1,74 +1,74 @@
|
|
|
1
|
-
import { aprToApy } from '../moneymarket';
|
|
2
|
-
import { DEFAULT_TIMEOUT, wethToEth } from '../services/utils';
|
|
3
|
-
import {
|
|
4
|
-
MerkleRewardMap, MerklOpportunity, OpportunityAction, OpportunityStatus,
|
|
5
|
-
} from '../types';
|
|
6
|
-
import { EthAddress, NetworkNumber } from '../types/common';
|
|
7
|
-
|
|
8
|
-
export const getAaveUnderlyingSymbol = (_symbol = '') => {
|
|
9
|
-
let symbol = _symbol
|
|
10
|
-
.replace(/^aEthLido/, '')
|
|
11
|
-
.replace(/^aEthEtherFi/, '')
|
|
12
|
-
.replace(/^aEth/, '')
|
|
13
|
-
.replace(/^aArb/, '')
|
|
14
|
-
.replace(/^aOpt/, '')
|
|
15
|
-
.replace(/^aBas/, '')
|
|
16
|
-
.replace(/^aLin/, '');
|
|
17
|
-
if (symbol.startsWith('a')) symbol = symbol.slice(1);
|
|
18
|
-
return wethToEth(symbol);
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* aEthLidoUSDC -> aUSDC
|
|
23
|
-
* USDC -> USDC
|
|
24
|
-
*/
|
|
25
|
-
export const formatAaveAsset = (_symbol: string) => {
|
|
26
|
-
if (_symbol.startsWith('a')) {
|
|
27
|
-
return `a${getAaveUnderlyingSymbol(_symbol)}`;
|
|
28
|
-
}
|
|
29
|
-
return _symbol;
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
export const getMerkleCampaigns = async (chainId: NetworkNumber): Promise<MerkleRewardMap> => {
|
|
33
|
-
try {
|
|
34
|
-
const res = await fetch('https://api.merkl.xyz/v4/opportunities?mainProtocolId=aave', {
|
|
35
|
-
signal: AbortSignal.timeout(DEFAULT_TIMEOUT),
|
|
36
|
-
});
|
|
37
|
-
if (!res.ok) throw new Error('Failed to fetch Merkle campaigns');
|
|
38
|
-
const opportunities = await res.json() as MerklOpportunity[];
|
|
39
|
-
const relevantOpportunities = opportunities
|
|
40
|
-
.filter((o: MerklOpportunity) => o.chainId === chainId)
|
|
41
|
-
.filter((o: MerklOpportunity) => o.status === OpportunityStatus.LIVE);
|
|
42
|
-
return relevantOpportunities.reduce((acc, opportunity) => {
|
|
43
|
-
const rewardToken = opportunity.rewardsRecord.breakdowns[0].token;
|
|
44
|
-
const description = `Eligible for ${formatAaveAsset(rewardToken.symbol)} rewards through Merkl. ${opportunity.description ? `\n${opportunity.description}` : ''}`;
|
|
45
|
-
if (opportunity.action === OpportunityAction.LEND && opportunity.explorerAddress) {
|
|
46
|
-
const supplyAToken = opportunity.explorerAddress?.toLowerCase() as EthAddress;
|
|
47
|
-
if (!acc[supplyAToken]) acc[supplyAToken] = {};
|
|
48
|
-
acc[supplyAToken].supply = {
|
|
49
|
-
apy: aprToApy(opportunity.apr),
|
|
50
|
-
// rewardToken: rewardToken.address,
|
|
51
|
-
rewardTokenSymbol: rewardToken.symbol,
|
|
52
|
-
description,
|
|
53
|
-
identifier: opportunity.identifier,
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
if (opportunity.action === OpportunityAction.BORROW && opportunity.explorerAddress) {
|
|
57
|
-
const borrowAToken = opportunity.explorerAddress?.toLowerCase() as EthAddress;
|
|
58
|
-
if (!acc[borrowAToken]) acc[borrowAToken] = {};
|
|
59
|
-
acc[borrowAToken].borrow = {
|
|
60
|
-
apy: aprToApy(opportunity.apr),
|
|
61
|
-
// rewardToken: rewardToken.address,
|
|
62
|
-
rewardTokenSymbol: rewardToken.symbol,
|
|
63
|
-
description,
|
|
64
|
-
identifier: opportunity.identifier,
|
|
65
|
-
};
|
|
66
|
-
}
|
|
67
|
-
return acc;
|
|
68
|
-
}, {} as MerkleRewardMap);
|
|
69
|
-
} catch (e) {
|
|
70
|
-
console.error('Failed to fetch Merkle campaigns', e);
|
|
71
|
-
return {};
|
|
72
|
-
}
|
|
73
|
-
};
|
|
74
|
-
|
|
1
|
+
import { aprToApy } from '../moneymarket';
|
|
2
|
+
import { DEFAULT_TIMEOUT, wethToEth } from '../services/utils';
|
|
3
|
+
import {
|
|
4
|
+
MerkleRewardMap, MerklOpportunity, OpportunityAction, OpportunityStatus,
|
|
5
|
+
} from '../types';
|
|
6
|
+
import { EthAddress, NetworkNumber } from '../types/common';
|
|
7
|
+
|
|
8
|
+
export const getAaveUnderlyingSymbol = (_symbol = '') => {
|
|
9
|
+
let symbol = _symbol
|
|
10
|
+
.replace(/^aEthLido/, '')
|
|
11
|
+
.replace(/^aEthEtherFi/, '')
|
|
12
|
+
.replace(/^aEth/, '')
|
|
13
|
+
.replace(/^aArb/, '')
|
|
14
|
+
.replace(/^aOpt/, '')
|
|
15
|
+
.replace(/^aBas/, '')
|
|
16
|
+
.replace(/^aLin/, '');
|
|
17
|
+
if (symbol.startsWith('a')) symbol = symbol.slice(1);
|
|
18
|
+
return wethToEth(symbol);
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* aEthLidoUSDC -> aUSDC
|
|
23
|
+
* USDC -> USDC
|
|
24
|
+
*/
|
|
25
|
+
export const formatAaveAsset = (_symbol: string) => {
|
|
26
|
+
if (_symbol.startsWith('a')) {
|
|
27
|
+
return `a${getAaveUnderlyingSymbol(_symbol)}`;
|
|
28
|
+
}
|
|
29
|
+
return _symbol;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export const getMerkleCampaigns = async (chainId: NetworkNumber): Promise<MerkleRewardMap> => {
|
|
33
|
+
try {
|
|
34
|
+
const res = await fetch('https://api.merkl.xyz/v4/opportunities?mainProtocolId=aave', {
|
|
35
|
+
signal: AbortSignal.timeout(DEFAULT_TIMEOUT),
|
|
36
|
+
});
|
|
37
|
+
if (!res.ok) throw new Error('Failed to fetch Merkle campaigns');
|
|
38
|
+
const opportunities = await res.json() as MerklOpportunity[];
|
|
39
|
+
const relevantOpportunities = opportunities
|
|
40
|
+
.filter((o: MerklOpportunity) => o.chainId === chainId)
|
|
41
|
+
.filter((o: MerklOpportunity) => o.status === OpportunityStatus.LIVE);
|
|
42
|
+
return relevantOpportunities.reduce((acc, opportunity) => {
|
|
43
|
+
const rewardToken = opportunity.rewardsRecord.breakdowns[0].token;
|
|
44
|
+
const description = `Eligible for ${formatAaveAsset(rewardToken.symbol)} rewards through Merkl. ${opportunity.description ? `\n${opportunity.description}` : ''}`;
|
|
45
|
+
if (opportunity.action === OpportunityAction.LEND && opportunity.explorerAddress) {
|
|
46
|
+
const supplyAToken = opportunity.explorerAddress?.toLowerCase() as EthAddress;
|
|
47
|
+
if (!acc[supplyAToken]) acc[supplyAToken] = {};
|
|
48
|
+
acc[supplyAToken].supply = {
|
|
49
|
+
apy: aprToApy(opportunity.apr),
|
|
50
|
+
// rewardToken: rewardToken.address,
|
|
51
|
+
rewardTokenSymbol: rewardToken.symbol,
|
|
52
|
+
description,
|
|
53
|
+
identifier: opportunity.identifier,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
if (opportunity.action === OpportunityAction.BORROW && opportunity.explorerAddress) {
|
|
57
|
+
const borrowAToken = opportunity.explorerAddress?.toLowerCase() as EthAddress;
|
|
58
|
+
if (!acc[borrowAToken]) acc[borrowAToken] = {};
|
|
59
|
+
acc[borrowAToken].borrow = {
|
|
60
|
+
apy: aprToApy(opportunity.apr),
|
|
61
|
+
// rewardToken: rewardToken.address,
|
|
62
|
+
rewardTokenSymbol: rewardToken.symbol,
|
|
63
|
+
description,
|
|
64
|
+
identifier: opportunity.identifier,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
return acc;
|
|
68
|
+
}, {} as MerkleRewardMap);
|
|
69
|
+
} catch (e) {
|
|
70
|
+
console.error('Failed to fetch Merkle campaigns', e);
|
|
71
|
+
return {};
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import { Client } from 'viem';
|
|
2
|
+
import Dec from 'decimal.js';
|
|
3
|
+
import { assetAmountInEth, getAssetInfoByAddress } from '@defisaver/tokens';
|
|
4
|
+
import { getViemProvider } from '../services/viem';
|
|
5
|
+
import {
|
|
6
|
+
AaveV4AccountData,
|
|
7
|
+
AaveV4HubAssetOnChainData,
|
|
8
|
+
AaveV4HubOnChainData,
|
|
9
|
+
AaveV4ReserveAssetData, AaveV4ReserveAssetOnChain, AaveV4SpokeData, AaveV4SpokeInfo,
|
|
10
|
+
AaveV4UsedReserveAssets,
|
|
11
|
+
} from '../types';
|
|
12
|
+
import {
|
|
13
|
+
EthAddress, EthereumProvider, IncentiveData, IncentiveKind, NetworkNumber,
|
|
14
|
+
} from '../types/common';
|
|
15
|
+
import { AaveV4ViewContractViem } from '../contracts';
|
|
16
|
+
import { getStakingApy, STAKING_ASSETS } from '../staking';
|
|
17
|
+
import { wethToEth } from '../services/utils';
|
|
18
|
+
import { aaveV4GetAggregatedPositionData } from '../helpers/aaveV4Helpers';
|
|
19
|
+
import { getAaveV4HubByAddress } from '../markets/aaveV4';
|
|
20
|
+
|
|
21
|
+
const fetchHubData = async (viewContract: ReturnType<typeof AaveV4ViewContractViem>, hubAddress: EthAddress): Promise<AaveV4HubOnChainData> => {
|
|
22
|
+
const hubData = await viewContract.read.getHubAllAssetsData([hubAddress]);
|
|
23
|
+
return {
|
|
24
|
+
assets: hubData.reduce((acc: Record<number, AaveV4HubAssetOnChainData>, assetOnChainData) => {
|
|
25
|
+
acc[assetOnChainData.assetId] = {
|
|
26
|
+
assetId: assetOnChainData.assetId,
|
|
27
|
+
drawnRate: assetOnChainData.drawnRate,
|
|
28
|
+
};
|
|
29
|
+
return acc;
|
|
30
|
+
}, {}),
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const formatReserveAsset = async (reserveAsset: AaveV4ReserveAssetOnChain, hubAsset: AaveV4HubAssetOnChainData, reserveId: number, oracleDecimals: number, network: NetworkNumber): Promise<AaveV4ReserveAssetData> => {
|
|
35
|
+
const assetInfo = getAssetInfoByAddress(reserveAsset.underlying, network);
|
|
36
|
+
const symbol = wethToEth(assetInfo.symbol);
|
|
37
|
+
const hubInfo = getAaveV4HubByAddress(network, reserveAsset.hub);
|
|
38
|
+
if (!hubInfo) {
|
|
39
|
+
throw new Error(`Hub not found with address: ${reserveAsset.hub}`);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const isStakingAsset = STAKING_ASSETS.includes(symbol);
|
|
43
|
+
const supplyIncentives: IncentiveData[] = [];
|
|
44
|
+
const borrowIncentives: IncentiveData[] = [];
|
|
45
|
+
|
|
46
|
+
if (isStakingAsset) {
|
|
47
|
+
const yieldApy = await getStakingApy(symbol, network as NetworkNumber);
|
|
48
|
+
supplyIncentives.push({
|
|
49
|
+
apy: yieldApy,
|
|
50
|
+
token: symbol,
|
|
51
|
+
incentiveKind: IncentiveKind.Staking,
|
|
52
|
+
description: `Native ${symbol} yield.`,
|
|
53
|
+
});
|
|
54
|
+
if (reserveAsset.borrowable) {
|
|
55
|
+
// when borrowing assets whose value increases over time
|
|
56
|
+
borrowIncentives.push({
|
|
57
|
+
apy: new Dec(yieldApy).mul(-1).toString(),
|
|
58
|
+
token: symbol,
|
|
59
|
+
incentiveKind: IncentiveKind.Reward,
|
|
60
|
+
description: `Due to the native yield of ${symbol}, the value of the debt would increase over time.`,
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return ({
|
|
66
|
+
symbol,
|
|
67
|
+
underlying: reserveAsset.underlying,
|
|
68
|
+
hub: hubInfo.address,
|
|
69
|
+
hubName: hubInfo?.label,
|
|
70
|
+
assetId: reserveAsset.assetId,
|
|
71
|
+
reserveId,
|
|
72
|
+
paused: reserveAsset.paused,
|
|
73
|
+
frozen: reserveAsset.frozen,
|
|
74
|
+
borrowable: reserveAsset.borrowable,
|
|
75
|
+
collateralRisk: new Dec(reserveAsset.collateralRisk).div(10000).toNumber(),
|
|
76
|
+
collateralFactor: new Dec(reserveAsset.collateralFactor).div(10000).toNumber(),
|
|
77
|
+
liquidationFee: new Dec(reserveAsset.liquidationFee).div(10000).toNumber(),
|
|
78
|
+
price: new Dec(reserveAsset.price).div(new Dec(10).pow(oracleDecimals)).toString(),
|
|
79
|
+
totalSupplied: assetAmountInEth(reserveAsset.totalSupplied.toString(), symbol),
|
|
80
|
+
totalDrawn: assetAmountInEth(reserveAsset.totalDrawn.toString(), symbol),
|
|
81
|
+
totalPremium: assetAmountInEth(reserveAsset.totalPremium.toString(), symbol),
|
|
82
|
+
totalDebt: assetAmountInEth(reserveAsset.totalDebt.toString(), symbol),
|
|
83
|
+
supplyCap: assetAmountInEth(reserveAsset.supplyCap.toString(), symbol),
|
|
84
|
+
borrowCap: assetAmountInEth(reserveAsset.borrowCap.toString(), symbol),
|
|
85
|
+
spokeActive: reserveAsset.spokeActive,
|
|
86
|
+
spokePaused: reserveAsset.spokePaused,
|
|
87
|
+
drawnRate: new Dec(hubAsset.drawnRate).div(new Dec(10).pow(27)).toString(),
|
|
88
|
+
supplyRate: '0', // To be implemented
|
|
89
|
+
supplyIncentives,
|
|
90
|
+
borrowIncentives,
|
|
91
|
+
canBeBorrowed: reserveAsset.spokeActive && !reserveAsset.spokePaused && !reserveAsset.paused && !reserveAsset.frozen,
|
|
92
|
+
canBeSupplied: reserveAsset.spokeActive && !reserveAsset.spokePaused && !reserveAsset.paused && !reserveAsset.frozen,
|
|
93
|
+
canBeWithdrawn: reserveAsset.spokeActive && !reserveAsset.spokePaused && !reserveAsset.paused,
|
|
94
|
+
canBePayBacked: reserveAsset.spokeActive && !reserveAsset.spokePaused && !reserveAsset.paused,
|
|
95
|
+
utilization: new Dec(reserveAsset.totalDrawn.toString()).times(100).div(new Dec(reserveAsset.totalSupplied.toString())).toString(),
|
|
96
|
+
});
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
export async function _getAaveV4SpokeData(provider: Client, network: NetworkNumber, market: AaveV4SpokeInfo, blockNumber: 'latest' | number = 'latest'): Promise<AaveV4SpokeData> {
|
|
100
|
+
const viewContract = AaveV4ViewContractViem(provider, network, blockNumber);
|
|
101
|
+
|
|
102
|
+
const hubsData: Record<EthAddress, AaveV4HubOnChainData> = {};
|
|
103
|
+
const [spokeData] = await Promise.all([
|
|
104
|
+
viewContract.read.getSpokeDataFull([market.address]),
|
|
105
|
+
...market.hubs.map(async (hubAddress) => {
|
|
106
|
+
hubsData[hubAddress] = await fetchHubData(viewContract, hubAddress);
|
|
107
|
+
}),
|
|
108
|
+
]);
|
|
109
|
+
|
|
110
|
+
const reserveAssetsArray = await Promise.all(spokeData[1].map(async (reserveAssetOnChain: AaveV4ReserveAssetOnChain, index: number) => formatReserveAsset(reserveAssetOnChain, hubsData[reserveAssetOnChain.hub].assets[reserveAssetOnChain.assetId], index, +spokeData[0].oracleDecimals.toString(), network)));
|
|
111
|
+
|
|
112
|
+
return {
|
|
113
|
+
assetsData: reserveAssetsArray.reduce((acc: Record<string, AaveV4ReserveAssetData>, reserveAsset: AaveV4ReserveAssetData) => {
|
|
114
|
+
acc[`${reserveAsset.symbol}-${reserveAsset.reserveId}`] = reserveAsset;
|
|
115
|
+
return acc;
|
|
116
|
+
}, {}),
|
|
117
|
+
oracle: spokeData[0].oracle,
|
|
118
|
+
oracleDecimals: +spokeData[0].oracleDecimals.toString(),
|
|
119
|
+
address: market.address,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export async function getAaveV4SpokeData(provider: EthereumProvider, network: NetworkNumber, spoke: AaveV4SpokeInfo, blockNumber: 'latest' | number = 'latest'): Promise<AaveV4SpokeData> {
|
|
124
|
+
return _getAaveV4SpokeData(getViemProvider(provider, network), network, spoke, blockNumber);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export async function _getAaveV4AccountData(provider: Client, network: NetworkNumber, spokeData: AaveV4SpokeData, address: EthAddress, blockNumber: 'latest' | number = 'latest'): Promise<AaveV4AccountData> {
|
|
128
|
+
const viewContract = AaveV4ViewContractViem(provider, network, blockNumber);
|
|
129
|
+
|
|
130
|
+
const loanData = await viewContract.read.getLoanData([spokeData.address, address]);
|
|
131
|
+
|
|
132
|
+
const healthFactor = new Dec(loanData.healthFactor).div(1e18).toString();
|
|
133
|
+
const usedAssets = loanData.reserves.reduce((acc: AaveV4UsedReserveAssets, usedReserveAsset) => {
|
|
134
|
+
const identifier = `${wethToEth(getAssetInfoByAddress(usedReserveAsset.underlying, network).symbol)}-${+usedReserveAsset.reserveId.toString()}`;
|
|
135
|
+
const reserveData = spokeData.assetsData[identifier];
|
|
136
|
+
const price = reserveData.price;
|
|
137
|
+
const supplied = assetAmountInEth(usedReserveAsset.supplied.toString(), reserveData.symbol);
|
|
138
|
+
const drawn = assetAmountInEth(usedReserveAsset.drawn.toString(), reserveData.symbol);
|
|
139
|
+
const premium = assetAmountInEth(usedReserveAsset.premium.toString(), reserveData.symbol);
|
|
140
|
+
const borrowed = assetAmountInEth(usedReserveAsset.totalDebt.toString(), reserveData.symbol);
|
|
141
|
+
acc[identifier] = {
|
|
142
|
+
symbol: reserveData.symbol,
|
|
143
|
+
hubName: reserveData.hubName,
|
|
144
|
+
assetId: reserveData.assetId,
|
|
145
|
+
reserveId: +usedReserveAsset.reserveId.toString(),
|
|
146
|
+
supplied,
|
|
147
|
+
suppliedUsd: new Dec(supplied).mul(price).toString(),
|
|
148
|
+
drawn,
|
|
149
|
+
drawnUsd: new Dec(drawn).mul(price).toString(),
|
|
150
|
+
premium,
|
|
151
|
+
premiumUsd: new Dec(premium).mul(price).toString(),
|
|
152
|
+
borrowed,
|
|
153
|
+
borrowedUsd: new Dec(borrowed).mul(price).toString(),
|
|
154
|
+
isSupplied: !new Dec(supplied).eq(0),
|
|
155
|
+
isBorrowed: usedReserveAsset.isBorrowing,
|
|
156
|
+
collateral: usedReserveAsset.isUsingAsCollateral,
|
|
157
|
+
collateralFactor: new Dec(usedReserveAsset.collateralFactor).div(10000).toNumber(),
|
|
158
|
+
};
|
|
159
|
+
return acc;
|
|
160
|
+
}, {});
|
|
161
|
+
|
|
162
|
+
return {
|
|
163
|
+
usedAssets,
|
|
164
|
+
healthFactor,
|
|
165
|
+
...aaveV4GetAggregatedPositionData({
|
|
166
|
+
usedAssets,
|
|
167
|
+
assetsData: spokeData.assetsData,
|
|
168
|
+
network,
|
|
169
|
+
useUserCollateralFactor: true,
|
|
170
|
+
}),
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
export async function getAaveV4AccountData(provider: EthereumProvider, network: NetworkNumber, marketData: AaveV4SpokeData, address: EthAddress, blockNumber: 'latest' | number = 'latest'): Promise<any> {
|
|
175
|
+
return _getAaveV4AccountData(getViemProvider(provider, network), network, marketData, address, blockNumber);
|
|
176
|
+
}
|