@defisaver/positions-sdk 2.1.110 → 2.1.111-merkl-auth-api-1-dev
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/cjs/aaveV3/merkl.js +2 -2
- package/cjs/aaveV4/merkl.js +2 -2
- package/cjs/claiming/aaveV3.js +5 -1
- package/cjs/helpers/aaveHelpers/index.d.ts +17 -0
- package/cjs/helpers/aaveHelpers/index.js +45 -1
- package/cjs/types/aave.d.ts +3 -0
- package/esm/aaveV3/merkl.js +3 -3
- package/esm/aaveV4/merkl.js +3 -3
- package/esm/claiming/aaveV3.js +6 -2
- package/esm/helpers/aaveHelpers/index.d.ts +17 -0
- package/esm/helpers/aaveHelpers/index.js +43 -0
- package/esm/types/aave.d.ts +3 -0
- package/package.json +1 -1
- package/src/aaveV3/merkl.ts +3 -3
- package/src/aaveV4/merkl.ts +3 -3
- package/src/claiming/aaveV3.ts +7 -3
- package/src/helpers/aaveHelpers/index.ts +60 -0
- package/src/types/aave.ts +4 -0
package/cjs/aaveV3/merkl.js
CHANGED
|
@@ -40,8 +40,8 @@ const formatAaveAsset = (_symbol) => {
|
|
|
40
40
|
exports.formatAaveAsset = formatAaveAsset;
|
|
41
41
|
const getMerkleCampaigns = (chainId) => __awaiter(void 0, void 0, void 0, function* () {
|
|
42
42
|
try {
|
|
43
|
-
const res = yield fetch('https://
|
|
44
|
-
signal: AbortSignal.timeout(utils_1.
|
|
43
|
+
const res = yield fetch('https://fe.defisaver.com/api/merkl/opportunities?mainProtocolId=aave', {
|
|
44
|
+
signal: AbortSignal.timeout(utils_1.LONGER_TIMEOUT),
|
|
45
45
|
});
|
|
46
46
|
if (!res.ok)
|
|
47
47
|
throw new Error('Failed to fetch Merkle campaigns');
|
package/cjs/aaveV4/merkl.js
CHANGED
|
@@ -34,8 +34,8 @@ const buildIncentive = (opportunity) => {
|
|
|
34
34
|
const getAaveV4MerkleCampaigns = (chainId) => __awaiter(void 0, void 0, void 0, function* () {
|
|
35
35
|
const result = { hub: {}, spoke: {} };
|
|
36
36
|
try {
|
|
37
|
-
const res = yield fetch('https://
|
|
38
|
-
signal: AbortSignal.timeout(utils_1.
|
|
37
|
+
const res = yield fetch('https://fe.defisaver.com/api/merkl/opportunities?mainProtocolId=aave', {
|
|
38
|
+
signal: AbortSignal.timeout(utils_1.LONGER_TIMEOUT),
|
|
39
39
|
});
|
|
40
40
|
if (!res.ok)
|
|
41
41
|
throw new Error('Failed to fetch Aave V4 Merkle campaigns');
|
package/cjs/claiming/aaveV3.js
CHANGED
|
@@ -99,7 +99,7 @@ function getMeritUnclaimedRewards(account, network) {
|
|
|
99
99
|
return __awaiter(this, void 0, void 0, function* () {
|
|
100
100
|
let data;
|
|
101
101
|
try {
|
|
102
|
-
const res = yield fetch(`https://
|
|
102
|
+
const res = yield fetch(`https://fe.defisaver.com/api/merkl/get-user-rewards/${account}?chainId=${network}`, { signal: AbortSignal.timeout(utils_1.LONGER_TIMEOUT) });
|
|
103
103
|
data = yield res.json();
|
|
104
104
|
}
|
|
105
105
|
catch (error) {
|
|
@@ -107,6 +107,10 @@ function getMeritUnclaimedRewards(account, network) {
|
|
|
107
107
|
data = [];
|
|
108
108
|
}
|
|
109
109
|
const claimableTokens = [];
|
|
110
|
+
// Merkl (or our proxy, on a non-2xx) can return a non-array error body; `fetch` doesn't throw on
|
|
111
|
+
// HTTP errors, so guard before iterating. Mirrors the app-side getMeritUnclaimedRewards.
|
|
112
|
+
if (!Array.isArray(data))
|
|
113
|
+
return claimableTokens;
|
|
110
114
|
data.forEach((item) => {
|
|
111
115
|
item.rewards.forEach(reward => {
|
|
112
116
|
const { token, amount, claimed, proofs, } = reward;
|
|
@@ -30,6 +30,23 @@ export declare const aaveAnyGetEmodeMutableProps: ({ eModeCategory, eModeCategor
|
|
|
30
30
|
liquidationRatio: any;
|
|
31
31
|
collateralFactor: any;
|
|
32
32
|
};
|
|
33
|
+
/**
|
|
34
|
+
* @description Per-asset effective LTV and liquidation threshold (LLTV) for the user, eMode-aware.
|
|
35
|
+
* Mirrors AaveV3View._getUserReserveLtvAndLltv: an asset in the active eMode's ltv-zero set has
|
|
36
|
+
* ltv 0 but KEEPS the eMode liquidation threshold (liquidations only consider LLTV). The returned
|
|
37
|
+
* `ltv` is identical to aaveAnyGetEmodeMutableProps().collateralFactor in every branch, so a ratio
|
|
38
|
+
* built on this matches the regular safety ratio whenever no collateral is LTV-0.
|
|
39
|
+
*
|
|
40
|
+
* Intentionally NOT merged with aaveAnyGetEmodeMutableProps (which it otherwise duplicates): the two
|
|
41
|
+
* differ only for an eMode ltv-zero asset — that helper returns liquidationRatio '0', whereas this one
|
|
42
|
+
* keeps the eMode liquidation threshold (to match the contract). aaveAnyGetEmodeMutableProps also feeds
|
|
43
|
+
* liquidationLimitUsd, so unifying them would change healthRatio / liqRatio / liquidationPrice for
|
|
44
|
+
* eMode-ltv-zero positions — to be done as a separate, validated follow-up PR.
|
|
45
|
+
*/
|
|
46
|
+
export declare const aaveAnyGetUserReserveLtvAndLltv: ({ eModeCategory, eModeCategoriesData, assetsData, }: AaveHelperCommon, _asset: string) => {
|
|
47
|
+
ltv: string;
|
|
48
|
+
lltv: string;
|
|
49
|
+
};
|
|
33
50
|
export declare const aaveAnyGetAggregatedPositionData: ({ usedAssets, eModeCategory, assetsData, selectedMarket, network, ...rest }: AaveHelperCommon) => AaveV3AggregatedPositionData;
|
|
34
51
|
export declare const getApyAfterValuesEstimation: (selectedMarket: AaveMarketInfo, actions: [{
|
|
35
52
|
action: string;
|
|
@@ -23,7 +23,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
23
23
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
24
24
|
};
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.getAaveUnderlyingSymbol = exports.getApyAfterValuesEstimation = exports.aaveAnyGetAggregatedPositionData = exports.aaveAnyGetEmodeMutableProps = exports.aaveAnyGetSuppliableAsCollAssets = exports.aaveAnyGetSuppliableAssets = exports.aaveAnyGetCollSuppliedAssets = exports.aaveV3IsInSiloedMode = exports.aaveV3IsInIsolationMode = exports.isAaveV3 = exports.isAaveV2 = exports.AAVE_V3_MARKETS = void 0;
|
|
26
|
+
exports.getAaveUnderlyingSymbol = exports.getApyAfterValuesEstimation = exports.aaveAnyGetAggregatedPositionData = exports.aaveAnyGetUserReserveLtvAndLltv = exports.aaveAnyGetEmodeMutableProps = exports.aaveAnyGetSuppliableAsCollAssets = exports.aaveAnyGetSuppliableAssets = exports.aaveAnyGetCollSuppliedAssets = exports.aaveV3IsInSiloedMode = exports.aaveV3IsInIsolationMode = exports.isAaveV3 = exports.isAaveV2 = exports.AAVE_V3_MARKETS = void 0;
|
|
27
27
|
const decimal_js_1 = __importDefault(require("decimal.js"));
|
|
28
28
|
const tokens_1 = require("@defisaver/tokens");
|
|
29
29
|
const types_1 = require("../../types");
|
|
@@ -83,6 +83,39 @@ const aaveAnyGetEmodeMutableProps = ({ eModeCategory, eModeCategoriesData, asset
|
|
|
83
83
|
return ({ liquidationRatio, collateralFactor });
|
|
84
84
|
};
|
|
85
85
|
exports.aaveAnyGetEmodeMutableProps = aaveAnyGetEmodeMutableProps;
|
|
86
|
+
/**
|
|
87
|
+
* @description Offset subtracted from the liquidation threshold (LLTV) when crediting LTV-0 collateral
|
|
88
|
+
* in the safety-ratio fallback. Matches AaveV3View.getSafetyRatioWithLtvZeroFallback ('LLTV - 5%').
|
|
89
|
+
* Values are fractions (e.g. 0.8), so 5% === 0.05.
|
|
90
|
+
*/
|
|
91
|
+
const LTV_ZERO_FALLBACK_LLTV_OFFSET = '0.05';
|
|
92
|
+
/**
|
|
93
|
+
* @description Per-asset effective LTV and liquidation threshold (LLTV) for the user, eMode-aware.
|
|
94
|
+
* Mirrors AaveV3View._getUserReserveLtvAndLltv: an asset in the active eMode's ltv-zero set has
|
|
95
|
+
* ltv 0 but KEEPS the eMode liquidation threshold (liquidations only consider LLTV). The returned
|
|
96
|
+
* `ltv` is identical to aaveAnyGetEmodeMutableProps().collateralFactor in every branch, so a ratio
|
|
97
|
+
* built on this matches the regular safety ratio whenever no collateral is LTV-0.
|
|
98
|
+
*
|
|
99
|
+
* Intentionally NOT merged with aaveAnyGetEmodeMutableProps (which it otherwise duplicates): the two
|
|
100
|
+
* differ only for an eMode ltv-zero asset — that helper returns liquidationRatio '0', whereas this one
|
|
101
|
+
* keeps the eMode liquidation threshold (to match the contract). aaveAnyGetEmodeMutableProps also feeds
|
|
102
|
+
* liquidationLimitUsd, so unifying them would change healthRatio / liqRatio / liquidationPrice for
|
|
103
|
+
* eMode-ltv-zero positions — to be done as a separate, validated follow-up PR.
|
|
104
|
+
*/
|
|
105
|
+
const aaveAnyGetUserReserveLtvAndLltv = ({ eModeCategory, eModeCategoriesData, assetsData, }, _asset) => {
|
|
106
|
+
const asset = (0, utils_1.getNativeAssetFromWrapped)(_asset);
|
|
107
|
+
const assetData = assetsData[asset];
|
|
108
|
+
const eModeCategoryData = eModeCategoriesData === null || eModeCategoriesData === void 0 ? void 0 : eModeCategoriesData[eModeCategory];
|
|
109
|
+
if (eModeCategory === 0
|
|
110
|
+
|| !eModeCategoryData
|
|
111
|
+
|| !eModeCategoryData.collateralAssets.includes(asset)
|
|
112
|
+
|| new decimal_js_1.default(eModeCategoryData.collateralFactor || 0).eq(0)) {
|
|
113
|
+
return { ltv: assetData.collateralFactor, lltv: assetData.liquidationRatio };
|
|
114
|
+
}
|
|
115
|
+
const ltv = eModeCategoryData.ltvZeroAssets.includes(asset) ? '0' : eModeCategoryData.collateralFactor;
|
|
116
|
+
return { ltv, lltv: eModeCategoryData.liquidationRatio };
|
|
117
|
+
};
|
|
118
|
+
exports.aaveAnyGetUserReserveLtvAndLltv = aaveAnyGetUserReserveLtvAndLltv;
|
|
86
119
|
const aaveAnyGetAggregatedPositionData = (_a) => {
|
|
87
120
|
var { usedAssets, eModeCategory, assetsData, selectedMarket, network } = _a, rest = __rest(_a, ["usedAssets", "eModeCategory", "assetsData", "selectedMarket", "network"]);
|
|
88
121
|
const data = Object.assign({ usedAssets, eModeCategory, assetsData, selectedMarket, network }, rest);
|
|
@@ -96,6 +129,17 @@ const aaveAnyGetAggregatedPositionData = (_a) => {
|
|
|
96
129
|
payload.leftToBorrowUsd = leftToBorrowUsd.lte('0') ? '0' : leftToBorrowUsd.toString();
|
|
97
130
|
payload.ratio = +payload.suppliedUsd ? new decimal_js_1.default(payload.borrowLimitUsd).div(payload.borrowedUsd).mul(100).toString() : '0';
|
|
98
131
|
payload.collRatio = +payload.suppliedUsd ? new decimal_js_1.default(payload.suppliedCollateralUsd).div(payload.borrowedUsd).mul(100).toString() : '0';
|
|
132
|
+
// Safety ratio as evaluated by the automation bots: LTV-0 collateral is credited at (LLTV - 5%)
|
|
133
|
+
// instead of 0 (AaveV3View.getSafetyRatioWithLtvZeroFallback). Equals `ratio` when no collateral
|
|
134
|
+
// is LTV-0. Computed off-chain here so it is available for after-value simulations too.
|
|
135
|
+
payload.borrowLimitWithLtvZeroFallbackUsd = (0, moneymarket_1.getAssetsTotal)(usedAssets, ({ isSupplied, collateral }) => isSupplied && collateral, ({ symbol, suppliedUsd }) => {
|
|
136
|
+
const { ltv, lltv } = (0, exports.aaveAnyGetUserReserveLtvAndLltv)(data, symbol);
|
|
137
|
+
const effectiveLtv = new decimal_js_1.default(ltv).eq(0)
|
|
138
|
+
? decimal_js_1.default.max(0, new decimal_js_1.default(lltv).sub(LTV_ZERO_FALLBACK_LLTV_OFFSET))
|
|
139
|
+
: new decimal_js_1.default(ltv);
|
|
140
|
+
return new decimal_js_1.default(suppliedUsd).mul(effectiveLtv);
|
|
141
|
+
});
|
|
142
|
+
payload.safetyRatioWithLtvZeroFallback = +payload.suppliedUsd ? new decimal_js_1.default(payload.borrowLimitWithLtvZeroFallbackUsd).div(payload.borrowedUsd).mul(100).toString() : '0';
|
|
99
143
|
payload.liqRatio = new decimal_js_1.default(payload.borrowLimitUsd).div(payload.liquidationLimitUsd).toString();
|
|
100
144
|
payload.liqPercent = new decimal_js_1.default(payload.borrowLimitUsd).div(payload.liquidationLimitUsd).mul(100).toString();
|
|
101
145
|
const { leveragedType, leveragedAsset } = (0, moneymarket_1.isLeveragedPos)(usedAssets);
|
package/cjs/types/aave.d.ts
CHANGED
|
@@ -125,6 +125,7 @@ export interface AavePositionData extends MMPositionData {
|
|
|
125
125
|
ratio: string;
|
|
126
126
|
minRatio: string;
|
|
127
127
|
collRatio: string;
|
|
128
|
+
safetyRatioWithLtvZeroFallback?: string;
|
|
128
129
|
suppliedUsd: string;
|
|
129
130
|
borrowedUsd: string;
|
|
130
131
|
borrowLimitUsd: string;
|
|
@@ -156,6 +157,8 @@ export interface AaveV3AggregatedPositionData {
|
|
|
156
157
|
leftToBorrowUsd: string;
|
|
157
158
|
ratio: string;
|
|
158
159
|
collRatio: string;
|
|
160
|
+
borrowLimitWithLtvZeroFallbackUsd: string;
|
|
161
|
+
safetyRatioWithLtvZeroFallback: string;
|
|
159
162
|
netApy: string;
|
|
160
163
|
incentiveUsd: string;
|
|
161
164
|
totalInterestUsd: string;
|
package/esm/aaveV3/merkl.js
CHANGED
|
@@ -8,7 +8,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
10
|
import { aprToApy } from '../moneymarket';
|
|
11
|
-
import {
|
|
11
|
+
import { LONGER_TIMEOUT, wethToEth } from '../services/utils';
|
|
12
12
|
import { OpportunityAction, OpportunityStatus, } from '../types';
|
|
13
13
|
export const getAaveUnderlyingSymbol = (_symbol = '') => {
|
|
14
14
|
let symbol = _symbol
|
|
@@ -35,8 +35,8 @@ export const formatAaveAsset = (_symbol) => {
|
|
|
35
35
|
};
|
|
36
36
|
export const getMerkleCampaigns = (chainId) => __awaiter(void 0, void 0, void 0, function* () {
|
|
37
37
|
try {
|
|
38
|
-
const res = yield fetch('https://
|
|
39
|
-
signal: AbortSignal.timeout(
|
|
38
|
+
const res = yield fetch('https://fe.defisaver.com/api/merkl/opportunities?mainProtocolId=aave', {
|
|
39
|
+
signal: AbortSignal.timeout(LONGER_TIMEOUT),
|
|
40
40
|
});
|
|
41
41
|
if (!res.ok)
|
|
42
42
|
throw new Error('Failed to fetch Merkle campaigns');
|
package/esm/aaveV4/merkl.js
CHANGED
|
@@ -8,7 +8,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
10
|
import { aprToApy } from '../moneymarket';
|
|
11
|
-
import {
|
|
11
|
+
import { LONGER_TIMEOUT } from '../services/utils';
|
|
12
12
|
import { IncentiveKind, OpportunityAction, OpportunityStatus, } from '../types';
|
|
13
13
|
/**
|
|
14
14
|
* Merkl tags Aave V4 reward campaigns by scope via the `type` field:
|
|
@@ -31,8 +31,8 @@ const buildIncentive = (opportunity) => {
|
|
|
31
31
|
export const getAaveV4MerkleCampaigns = (chainId) => __awaiter(void 0, void 0, void 0, function* () {
|
|
32
32
|
const result = { hub: {}, spoke: {} };
|
|
33
33
|
try {
|
|
34
|
-
const res = yield fetch('https://
|
|
35
|
-
signal: AbortSignal.timeout(
|
|
34
|
+
const res = yield fetch('https://fe.defisaver.com/api/merkl/opportunities?mainProtocolId=aave', {
|
|
35
|
+
signal: AbortSignal.timeout(LONGER_TIMEOUT),
|
|
36
36
|
});
|
|
37
37
|
if (!res.ok)
|
|
38
38
|
throw new Error('Failed to fetch Aave V4 Merkle campaigns');
|
package/esm/claiming/aaveV3.js
CHANGED
|
@@ -10,7 +10,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
10
|
import Dec from 'decimal.js';
|
|
11
11
|
import { ClaimType } from '../types/claiming';
|
|
12
12
|
import { AaveIncentiveDataProviderV3ContractViem, AaveRewardsControllerViem, } from '../contracts';
|
|
13
|
-
import { compareAddresses, getEthAmountForDecimals } from '../services/utils';
|
|
13
|
+
import { compareAddresses, getEthAmountForDecimals, LONGER_TIMEOUT } from '../services/utils';
|
|
14
14
|
import { getAaveUnderlyingSymbol } from '../helpers/aaveHelpers';
|
|
15
15
|
const mapAaveRewardsToClaimableTokens = (aaveRewards, marketAddress, walletAddress) => aaveRewards.map(reward => ({
|
|
16
16
|
symbol: reward.symbol,
|
|
@@ -92,7 +92,7 @@ export function getMeritUnclaimedRewards(account, network) {
|
|
|
92
92
|
return __awaiter(this, void 0, void 0, function* () {
|
|
93
93
|
let data;
|
|
94
94
|
try {
|
|
95
|
-
const res = yield fetch(`https://
|
|
95
|
+
const res = yield fetch(`https://fe.defisaver.com/api/merkl/get-user-rewards/${account}?chainId=${network}`, { signal: AbortSignal.timeout(LONGER_TIMEOUT) });
|
|
96
96
|
data = yield res.json();
|
|
97
97
|
}
|
|
98
98
|
catch (error) {
|
|
@@ -100,6 +100,10 @@ export function getMeritUnclaimedRewards(account, network) {
|
|
|
100
100
|
data = [];
|
|
101
101
|
}
|
|
102
102
|
const claimableTokens = [];
|
|
103
|
+
// Merkl (or our proxy, on a non-2xx) can return a non-array error body; `fetch` doesn't throw on
|
|
104
|
+
// HTTP errors, so guard before iterating. Mirrors the app-side getMeritUnclaimedRewards.
|
|
105
|
+
if (!Array.isArray(data))
|
|
106
|
+
return claimableTokens;
|
|
103
107
|
data.forEach((item) => {
|
|
104
108
|
item.rewards.forEach(reward => {
|
|
105
109
|
const { token, amount, claimed, proofs, } = reward;
|
|
@@ -30,6 +30,23 @@ export declare const aaveAnyGetEmodeMutableProps: ({ eModeCategory, eModeCategor
|
|
|
30
30
|
liquidationRatio: any;
|
|
31
31
|
collateralFactor: any;
|
|
32
32
|
};
|
|
33
|
+
/**
|
|
34
|
+
* @description Per-asset effective LTV and liquidation threshold (LLTV) for the user, eMode-aware.
|
|
35
|
+
* Mirrors AaveV3View._getUserReserveLtvAndLltv: an asset in the active eMode's ltv-zero set has
|
|
36
|
+
* ltv 0 but KEEPS the eMode liquidation threshold (liquidations only consider LLTV). The returned
|
|
37
|
+
* `ltv` is identical to aaveAnyGetEmodeMutableProps().collateralFactor in every branch, so a ratio
|
|
38
|
+
* built on this matches the regular safety ratio whenever no collateral is LTV-0.
|
|
39
|
+
*
|
|
40
|
+
* Intentionally NOT merged with aaveAnyGetEmodeMutableProps (which it otherwise duplicates): the two
|
|
41
|
+
* differ only for an eMode ltv-zero asset — that helper returns liquidationRatio '0', whereas this one
|
|
42
|
+
* keeps the eMode liquidation threshold (to match the contract). aaveAnyGetEmodeMutableProps also feeds
|
|
43
|
+
* liquidationLimitUsd, so unifying them would change healthRatio / liqRatio / liquidationPrice for
|
|
44
|
+
* eMode-ltv-zero positions — to be done as a separate, validated follow-up PR.
|
|
45
|
+
*/
|
|
46
|
+
export declare const aaveAnyGetUserReserveLtvAndLltv: ({ eModeCategory, eModeCategoriesData, assetsData, }: AaveHelperCommon, _asset: string) => {
|
|
47
|
+
ltv: string;
|
|
48
|
+
lltv: string;
|
|
49
|
+
};
|
|
33
50
|
export declare const aaveAnyGetAggregatedPositionData: ({ usedAssets, eModeCategory, assetsData, selectedMarket, network, ...rest }: AaveHelperCommon) => AaveV3AggregatedPositionData;
|
|
34
51
|
export declare const getApyAfterValuesEstimation: (selectedMarket: AaveMarketInfo, actions: [{
|
|
35
52
|
action: string;
|
|
@@ -69,6 +69,38 @@ export const aaveAnyGetEmodeMutableProps = ({ eModeCategory, eModeCategoriesData
|
|
|
69
69
|
const { liquidationRatio, collateralFactor } = eModeCategoryData;
|
|
70
70
|
return ({ liquidationRatio, collateralFactor });
|
|
71
71
|
};
|
|
72
|
+
/**
|
|
73
|
+
* @description Offset subtracted from the liquidation threshold (LLTV) when crediting LTV-0 collateral
|
|
74
|
+
* in the safety-ratio fallback. Matches AaveV3View.getSafetyRatioWithLtvZeroFallback ('LLTV - 5%').
|
|
75
|
+
* Values are fractions (e.g. 0.8), so 5% === 0.05.
|
|
76
|
+
*/
|
|
77
|
+
const LTV_ZERO_FALLBACK_LLTV_OFFSET = '0.05';
|
|
78
|
+
/**
|
|
79
|
+
* @description Per-asset effective LTV and liquidation threshold (LLTV) for the user, eMode-aware.
|
|
80
|
+
* Mirrors AaveV3View._getUserReserveLtvAndLltv: an asset in the active eMode's ltv-zero set has
|
|
81
|
+
* ltv 0 but KEEPS the eMode liquidation threshold (liquidations only consider LLTV). The returned
|
|
82
|
+
* `ltv` is identical to aaveAnyGetEmodeMutableProps().collateralFactor in every branch, so a ratio
|
|
83
|
+
* built on this matches the regular safety ratio whenever no collateral is LTV-0.
|
|
84
|
+
*
|
|
85
|
+
* Intentionally NOT merged with aaveAnyGetEmodeMutableProps (which it otherwise duplicates): the two
|
|
86
|
+
* differ only for an eMode ltv-zero asset — that helper returns liquidationRatio '0', whereas this one
|
|
87
|
+
* keeps the eMode liquidation threshold (to match the contract). aaveAnyGetEmodeMutableProps also feeds
|
|
88
|
+
* liquidationLimitUsd, so unifying them would change healthRatio / liqRatio / liquidationPrice for
|
|
89
|
+
* eMode-ltv-zero positions — to be done as a separate, validated follow-up PR.
|
|
90
|
+
*/
|
|
91
|
+
export const aaveAnyGetUserReserveLtvAndLltv = ({ eModeCategory, eModeCategoriesData, assetsData, }, _asset) => {
|
|
92
|
+
const asset = getNativeAssetFromWrapped(_asset);
|
|
93
|
+
const assetData = assetsData[asset];
|
|
94
|
+
const eModeCategoryData = eModeCategoriesData === null || eModeCategoriesData === void 0 ? void 0 : eModeCategoriesData[eModeCategory];
|
|
95
|
+
if (eModeCategory === 0
|
|
96
|
+
|| !eModeCategoryData
|
|
97
|
+
|| !eModeCategoryData.collateralAssets.includes(asset)
|
|
98
|
+
|| new Dec(eModeCategoryData.collateralFactor || 0).eq(0)) {
|
|
99
|
+
return { ltv: assetData.collateralFactor, lltv: assetData.liquidationRatio };
|
|
100
|
+
}
|
|
101
|
+
const ltv = eModeCategoryData.ltvZeroAssets.includes(asset) ? '0' : eModeCategoryData.collateralFactor;
|
|
102
|
+
return { ltv, lltv: eModeCategoryData.liquidationRatio };
|
|
103
|
+
};
|
|
72
104
|
export const aaveAnyGetAggregatedPositionData = (_a) => {
|
|
73
105
|
var { usedAssets, eModeCategory, assetsData, selectedMarket, network } = _a, rest = __rest(_a, ["usedAssets", "eModeCategory", "assetsData", "selectedMarket", "network"]);
|
|
74
106
|
const data = Object.assign({ usedAssets, eModeCategory, assetsData, selectedMarket, network }, rest);
|
|
@@ -82,6 +114,17 @@ export const aaveAnyGetAggregatedPositionData = (_a) => {
|
|
|
82
114
|
payload.leftToBorrowUsd = leftToBorrowUsd.lte('0') ? '0' : leftToBorrowUsd.toString();
|
|
83
115
|
payload.ratio = +payload.suppliedUsd ? new Dec(payload.borrowLimitUsd).div(payload.borrowedUsd).mul(100).toString() : '0';
|
|
84
116
|
payload.collRatio = +payload.suppliedUsd ? new Dec(payload.suppliedCollateralUsd).div(payload.borrowedUsd).mul(100).toString() : '0';
|
|
117
|
+
// Safety ratio as evaluated by the automation bots: LTV-0 collateral is credited at (LLTV - 5%)
|
|
118
|
+
// instead of 0 (AaveV3View.getSafetyRatioWithLtvZeroFallback). Equals `ratio` when no collateral
|
|
119
|
+
// is LTV-0. Computed off-chain here so it is available for after-value simulations too.
|
|
120
|
+
payload.borrowLimitWithLtvZeroFallbackUsd = getAssetsTotal(usedAssets, ({ isSupplied, collateral }) => isSupplied && collateral, ({ symbol, suppliedUsd }) => {
|
|
121
|
+
const { ltv, lltv } = aaveAnyGetUserReserveLtvAndLltv(data, symbol);
|
|
122
|
+
const effectiveLtv = new Dec(ltv).eq(0)
|
|
123
|
+
? Dec.max(0, new Dec(lltv).sub(LTV_ZERO_FALLBACK_LLTV_OFFSET))
|
|
124
|
+
: new Dec(ltv);
|
|
125
|
+
return new Dec(suppliedUsd).mul(effectiveLtv);
|
|
126
|
+
});
|
|
127
|
+
payload.safetyRatioWithLtvZeroFallback = +payload.suppliedUsd ? new Dec(payload.borrowLimitWithLtvZeroFallbackUsd).div(payload.borrowedUsd).mul(100).toString() : '0';
|
|
85
128
|
payload.liqRatio = new Dec(payload.borrowLimitUsd).div(payload.liquidationLimitUsd).toString();
|
|
86
129
|
payload.liqPercent = new Dec(payload.borrowLimitUsd).div(payload.liquidationLimitUsd).mul(100).toString();
|
|
87
130
|
const { leveragedType, leveragedAsset } = isLeveragedPos(usedAssets);
|
package/esm/types/aave.d.ts
CHANGED
|
@@ -125,6 +125,7 @@ export interface AavePositionData extends MMPositionData {
|
|
|
125
125
|
ratio: string;
|
|
126
126
|
minRatio: string;
|
|
127
127
|
collRatio: string;
|
|
128
|
+
safetyRatioWithLtvZeroFallback?: string;
|
|
128
129
|
suppliedUsd: string;
|
|
129
130
|
borrowedUsd: string;
|
|
130
131
|
borrowLimitUsd: string;
|
|
@@ -156,6 +157,8 @@ export interface AaveV3AggregatedPositionData {
|
|
|
156
157
|
leftToBorrowUsd: string;
|
|
157
158
|
ratio: string;
|
|
158
159
|
collRatio: string;
|
|
160
|
+
borrowLimitWithLtvZeroFallbackUsd: string;
|
|
161
|
+
safetyRatioWithLtvZeroFallback: string;
|
|
159
162
|
netApy: string;
|
|
160
163
|
incentiveUsd: string;
|
|
161
164
|
totalInterestUsd: string;
|
package/package.json
CHANGED
package/src/aaveV3/merkl.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { aprToApy } from '../moneymarket';
|
|
2
|
-
import {
|
|
2
|
+
import { LONGER_TIMEOUT, wethToEth } from '../services/utils';
|
|
3
3
|
import {
|
|
4
4
|
MerkleRewardMap, MerklOpportunity, OpportunityAction, OpportunityStatus,
|
|
5
5
|
} from '../types';
|
|
@@ -31,8 +31,8 @@ export const formatAaveAsset = (_symbol: string) => {
|
|
|
31
31
|
|
|
32
32
|
export const getMerkleCampaigns = async (chainId: NetworkNumber): Promise<MerkleRewardMap> => {
|
|
33
33
|
try {
|
|
34
|
-
const res = await fetch('https://
|
|
35
|
-
signal: AbortSignal.timeout(
|
|
34
|
+
const res = await fetch('https://fe.defisaver.com/api/merkl/opportunities?mainProtocolId=aave', {
|
|
35
|
+
signal: AbortSignal.timeout(LONGER_TIMEOUT),
|
|
36
36
|
});
|
|
37
37
|
if (!res.ok) throw new Error('Failed to fetch Merkle campaigns');
|
|
38
38
|
const opportunities = await res.json() as MerklOpportunity[];
|
package/src/aaveV4/merkl.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { aprToApy } from '../moneymarket';
|
|
2
|
-
import {
|
|
2
|
+
import { LONGER_TIMEOUT } from '../services/utils';
|
|
3
3
|
import {
|
|
4
4
|
AaveV4MerklRewardMap,
|
|
5
5
|
AaveV4ReserveAssetData,
|
|
@@ -34,8 +34,8 @@ const buildIncentive = (opportunity: MerklOpportunity): IncentiveData => {
|
|
|
34
34
|
export const getAaveV4MerkleCampaigns = async (chainId: NetworkNumber): Promise<AaveV4MerklRewardMap> => {
|
|
35
35
|
const result: AaveV4MerklRewardMap = { hub: {}, spoke: {} };
|
|
36
36
|
try {
|
|
37
|
-
const res = await fetch('https://
|
|
38
|
-
signal: AbortSignal.timeout(
|
|
37
|
+
const res = await fetch('https://fe.defisaver.com/api/merkl/opportunities?mainProtocolId=aave', {
|
|
38
|
+
signal: AbortSignal.timeout(LONGER_TIMEOUT),
|
|
39
39
|
});
|
|
40
40
|
if (!res.ok) throw new Error('Failed to fetch Aave V4 Merkle campaigns');
|
|
41
41
|
const opportunities = await res.json() as MerklOpportunity[];
|
package/src/claiming/aaveV3.ts
CHANGED
|
@@ -6,7 +6,7 @@ import {
|
|
|
6
6
|
AaveIncentiveDataProviderV3ContractViem,
|
|
7
7
|
AaveRewardsControllerViem,
|
|
8
8
|
} from '../contracts';
|
|
9
|
-
import { compareAddresses, getEthAmountForDecimals } from '../services/utils';
|
|
9
|
+
import { compareAddresses, getEthAmountForDecimals, LONGER_TIMEOUT } from '../services/utils';
|
|
10
10
|
import { getAaveUnderlyingSymbol } from '../helpers/aaveHelpers';
|
|
11
11
|
|
|
12
12
|
type AaveReward = {
|
|
@@ -106,8 +106,8 @@ export async function getUnclaimedRewardsForAllMarkets(
|
|
|
106
106
|
export async function getMeritUnclaimedRewards(account: EthAddress, network: NetworkNumber): Promise<ClaimableToken[]> {
|
|
107
107
|
let data;
|
|
108
108
|
try {
|
|
109
|
-
const res = await fetch(`https://
|
|
110
|
-
{ signal: AbortSignal.timeout(
|
|
109
|
+
const res = await fetch(`https://fe.defisaver.com/api/merkl/get-user-rewards/${account}?chainId=${network}`,
|
|
110
|
+
{ signal: AbortSignal.timeout(LONGER_TIMEOUT) });
|
|
111
111
|
data = await res.json();
|
|
112
112
|
} catch (error) {
|
|
113
113
|
console.error('External API Failure: Aave Merit', error);
|
|
@@ -116,6 +116,10 @@ export async function getMeritUnclaimedRewards(account: EthAddress, network: Net
|
|
|
116
116
|
|
|
117
117
|
const claimableTokens: ClaimableToken[] = [];
|
|
118
118
|
|
|
119
|
+
// Merkl (or our proxy, on a non-2xx) can return a non-array error body; `fetch` doesn't throw on
|
|
120
|
+
// HTTP errors, so guard before iterating. Mirrors the app-side getMeritUnclaimedRewards.
|
|
121
|
+
if (!Array.isArray(data)) return claimableTokens;
|
|
122
|
+
|
|
119
123
|
data.forEach((item: { rewards: any[]; }) => {
|
|
120
124
|
item.rewards.forEach(reward => {
|
|
121
125
|
const {
|
|
@@ -80,6 +80,51 @@ export const aaveAnyGetEmodeMutableProps = (
|
|
|
80
80
|
return ({ liquidationRatio, collateralFactor });
|
|
81
81
|
};
|
|
82
82
|
|
|
83
|
+
/**
|
|
84
|
+
* @description Offset subtracted from the liquidation threshold (LLTV) when crediting LTV-0 collateral
|
|
85
|
+
* in the safety-ratio fallback. Matches AaveV3View.getSafetyRatioWithLtvZeroFallback ('LLTV - 5%').
|
|
86
|
+
* Values are fractions (e.g. 0.8), so 5% === 0.05.
|
|
87
|
+
*/
|
|
88
|
+
const LTV_ZERO_FALLBACK_LLTV_OFFSET = '0.05';
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* @description Per-asset effective LTV and liquidation threshold (LLTV) for the user, eMode-aware.
|
|
92
|
+
* Mirrors AaveV3View._getUserReserveLtvAndLltv: an asset in the active eMode's ltv-zero set has
|
|
93
|
+
* ltv 0 but KEEPS the eMode liquidation threshold (liquidations only consider LLTV). The returned
|
|
94
|
+
* `ltv` is identical to aaveAnyGetEmodeMutableProps().collateralFactor in every branch, so a ratio
|
|
95
|
+
* built on this matches the regular safety ratio whenever no collateral is LTV-0.
|
|
96
|
+
*
|
|
97
|
+
* Intentionally NOT merged with aaveAnyGetEmodeMutableProps (which it otherwise duplicates): the two
|
|
98
|
+
* differ only for an eMode ltv-zero asset — that helper returns liquidationRatio '0', whereas this one
|
|
99
|
+
* keeps the eMode liquidation threshold (to match the contract). aaveAnyGetEmodeMutableProps also feeds
|
|
100
|
+
* liquidationLimitUsd, so unifying them would change healthRatio / liqRatio / liquidationPrice for
|
|
101
|
+
* eMode-ltv-zero positions — to be done as a separate, validated follow-up PR.
|
|
102
|
+
*/
|
|
103
|
+
export const aaveAnyGetUserReserveLtvAndLltv = (
|
|
104
|
+
{
|
|
105
|
+
eModeCategory,
|
|
106
|
+
eModeCategoriesData,
|
|
107
|
+
assetsData,
|
|
108
|
+
}: AaveHelperCommon,
|
|
109
|
+
_asset: string,
|
|
110
|
+
): { ltv: string, lltv: string } => {
|
|
111
|
+
const asset = getNativeAssetFromWrapped(_asset);
|
|
112
|
+
const assetData = assetsData[asset];
|
|
113
|
+
const eModeCategoryData = eModeCategoriesData?.[eModeCategory];
|
|
114
|
+
|
|
115
|
+
if (
|
|
116
|
+
eModeCategory === 0
|
|
117
|
+
|| !eModeCategoryData
|
|
118
|
+
|| !eModeCategoryData.collateralAssets.includes(asset)
|
|
119
|
+
|| new Dec(eModeCategoryData.collateralFactor || 0).eq(0)
|
|
120
|
+
) {
|
|
121
|
+
return { ltv: assetData.collateralFactor, lltv: assetData.liquidationRatio };
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const ltv = eModeCategoryData.ltvZeroAssets.includes(asset) ? '0' : eModeCategoryData.collateralFactor;
|
|
125
|
+
return { ltv, lltv: eModeCategoryData.liquidationRatio };
|
|
126
|
+
};
|
|
127
|
+
|
|
83
128
|
export const aaveAnyGetAggregatedPositionData = ({
|
|
84
129
|
usedAssets,
|
|
85
130
|
eModeCategory,
|
|
@@ -109,6 +154,21 @@ export const aaveAnyGetAggregatedPositionData = ({
|
|
|
109
154
|
payload.leftToBorrowUsd = leftToBorrowUsd.lte('0') ? '0' : leftToBorrowUsd.toString();
|
|
110
155
|
payload.ratio = +payload.suppliedUsd ? new Dec(payload.borrowLimitUsd).div(payload.borrowedUsd).mul(100).toString() : '0';
|
|
111
156
|
payload.collRatio = +payload.suppliedUsd ? new Dec(payload.suppliedCollateralUsd).div(payload.borrowedUsd).mul(100).toString() : '0';
|
|
157
|
+
// Safety ratio as evaluated by the automation bots: LTV-0 collateral is credited at (LLTV - 5%)
|
|
158
|
+
// instead of 0 (AaveV3View.getSafetyRatioWithLtvZeroFallback). Equals `ratio` when no collateral
|
|
159
|
+
// is LTV-0. Computed off-chain here so it is available for after-value simulations too.
|
|
160
|
+
payload.borrowLimitWithLtvZeroFallbackUsd = getAssetsTotal(
|
|
161
|
+
usedAssets,
|
|
162
|
+
({ isSupplied, collateral }: { isSupplied: boolean, collateral: string }) => isSupplied && collateral,
|
|
163
|
+
({ symbol, suppliedUsd }: { symbol: string, suppliedUsd: string }) => {
|
|
164
|
+
const { ltv, lltv } = aaveAnyGetUserReserveLtvAndLltv(data, symbol);
|
|
165
|
+
const effectiveLtv = new Dec(ltv).eq(0)
|
|
166
|
+
? Dec.max(0, new Dec(lltv).sub(LTV_ZERO_FALLBACK_LLTV_OFFSET))
|
|
167
|
+
: new Dec(ltv);
|
|
168
|
+
return new Dec(suppliedUsd).mul(effectiveLtv);
|
|
169
|
+
},
|
|
170
|
+
);
|
|
171
|
+
payload.safetyRatioWithLtvZeroFallback = +payload.suppliedUsd ? new Dec(payload.borrowLimitWithLtvZeroFallbackUsd).div(payload.borrowedUsd).mul(100).toString() : '0';
|
|
112
172
|
payload.liqRatio = new Dec(payload.borrowLimitUsd).div(payload.liquidationLimitUsd).toString();
|
|
113
173
|
payload.liqPercent = new Dec(payload.borrowLimitUsd).div(payload.liquidationLimitUsd).mul(100).toString();
|
|
114
174
|
const { leveragedType, leveragedAsset } = isLeveragedPos(usedAssets);
|
package/src/types/aave.ts
CHANGED
|
@@ -141,6 +141,8 @@ export interface AavePositionData extends MMPositionData {
|
|
|
141
141
|
ratio: string,
|
|
142
142
|
minRatio: string,
|
|
143
143
|
collRatio: string,
|
|
144
|
+
// Safety ratio as evaluated by automation bots (LTV-0 collateral credited at LLTV - 5%). Aave v3 only.
|
|
145
|
+
safetyRatioWithLtvZeroFallback?: string,
|
|
144
146
|
suppliedUsd: string,
|
|
145
147
|
borrowedUsd: string,
|
|
146
148
|
borrowLimitUsd: string,
|
|
@@ -173,6 +175,8 @@ export interface AaveV3AggregatedPositionData {
|
|
|
173
175
|
leftToBorrowUsd: string,
|
|
174
176
|
ratio: string,
|
|
175
177
|
collRatio: string,
|
|
178
|
+
borrowLimitWithLtvZeroFallbackUsd: string,
|
|
179
|
+
safetyRatioWithLtvZeroFallback: string,
|
|
176
180
|
netApy: string,
|
|
177
181
|
incentiveUsd: string,
|
|
178
182
|
totalInterestUsd: string,
|