@defisaver/positions-sdk 2.1.104 → 2.1.106
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/aaveV4/index.js +53 -20
- package/cjs/claiming/aaveV3.d.ts +1 -1
- package/cjs/claiming/aaveV3.js +3 -4
- package/cjs/claiming/index.d.ts +1 -2
- package/cjs/claiming/index.js +1 -3
- package/cjs/config/contracts.d.ts +0 -214
- package/cjs/config/contracts.js +1 -6
- package/cjs/helpers/morphoBlueHelpers/index.d.ts +0 -1
- package/cjs/helpers/morphoBlueHelpers/index.js +27 -32
- package/cjs/portfolio/index.js +0 -27
- package/cjs/types/aaveV4.d.ts +9 -0
- package/cjs/types/claiming.d.ts +1 -13
- package/cjs/types/claiming.js +0 -2
- package/cjs/types/morphoBlue.d.ts +2 -3
- package/esm/aaveV4/index.js +53 -20
- package/esm/claiming/aaveV3.d.ts +1 -1
- package/esm/claiming/aaveV3.js +3 -4
- package/esm/claiming/index.d.ts +1 -2
- package/esm/claiming/index.js +1 -2
- package/esm/config/contracts.d.ts +0 -214
- package/esm/config/contracts.js +0 -4
- package/esm/helpers/morphoBlueHelpers/index.d.ts +0 -1
- package/esm/helpers/morphoBlueHelpers/index.js +27 -31
- package/esm/portfolio/index.js +0 -27
- package/esm/types/aaveV4.d.ts +9 -0
- package/esm/types/claiming.d.ts +1 -13
- package/esm/types/claiming.js +0 -2
- package/esm/types/morphoBlue.d.ts +2 -3
- package/package.json +1 -1
- package/src/aaveV4/index.ts +48 -20
- package/src/claiming/aaveV3.ts +2 -3
- package/src/claiming/index.ts +0 -2
- package/src/config/contracts.ts +0 -4
- package/src/helpers/morphoBlueHelpers/index.ts +29 -32
- package/src/portfolio/index.ts +0 -25
- package/src/types/aaveV4.ts +9 -0
- package/src/types/claiming.ts +0 -15
- package/src/types/morphoBlue.ts +2 -3
- package/cjs/claiming/morphoBlue.d.ts +0 -5
- package/cjs/claiming/morphoBlue.js +0 -113
- package/esm/claiming/morphoBlue.d.ts +0 -5
- package/esm/claiming/morphoBlue.js +0 -105
- package/src/claiming/morphoBlue.ts +0 -119
package/src/aaveV4/index.ts
CHANGED
|
@@ -50,6 +50,9 @@ const fetchHubData = async (viewContract: ReturnType<typeof AaveV4ViewContractVi
|
|
|
50
50
|
|
|
51
51
|
const formatReserveAsset = async (reserveAsset: AaveV4ReserveAssetOnChain, hubAsset: AaveV4HubAssetOnChainData, reserveId: number, oracleDecimals: number, network: NetworkNumber): Promise<AaveV4ReserveAssetData> => {
|
|
52
52
|
const assetInfo = getAssetInfoByAddress(reserveAsset.underlying, network);
|
|
53
|
+
// `@defisaver/tokens` returns a placeholder ('?', decimals NaN) when the underlying is not in the
|
|
54
|
+
// tokens package. Flag it so consumers can render it read-only instead of feeding NaN into amounts.
|
|
55
|
+
const isUnsupported = assetInfo.symbol === '?';
|
|
53
56
|
const symbol = wethToEth(assetInfo.symbol);
|
|
54
57
|
const hubInfo = getAaveV4HubByAddress(network, reserveAsset.hub);
|
|
55
58
|
if (!hubInfo) {
|
|
@@ -101,11 +104,23 @@ const formatReserveAsset = async (reserveAsset: AaveV4ReserveAssetOnChain, hubAs
|
|
|
101
104
|
const supplyApr = borrowApr.mul(hubUtilization).mul(premiumMultiplier).mul(new Dec(1).minus(liquidityFee));
|
|
102
105
|
const utilization = hubUtilization.times(100).toString();
|
|
103
106
|
|
|
107
|
+
// For unsupported assets `symbol` is '?' (decimals NaN in `@defisaver/tokens`), so the
|
|
108
|
+
// symbol-based conversion would produce NaN. Fall back to the on-chain `decimals` so the reserve
|
|
109
|
+
// still shows correct amounts (and feeds correct USD/ratio/liquidation math) in read-only mode.
|
|
110
|
+
const toEth = (raw: string | number | bigint) => {
|
|
111
|
+
const rawStr = raw.toString();
|
|
112
|
+
if (isMaxUint(rawStr)) return rawStr;
|
|
113
|
+
if (isUnsupported) return new Dec(rawStr || 0).div(new Dec(10).pow(reserveAsset.decimals)).toString();
|
|
114
|
+
return assetAmountInEth(rawStr, symbol);
|
|
115
|
+
};
|
|
116
|
+
|
|
104
117
|
const hubLiquidityRaw = hubAsset.liquidity;
|
|
105
|
-
const hubLiquidity =
|
|
118
|
+
const hubLiquidity = toEth(hubLiquidityRaw.toString());
|
|
106
119
|
|
|
107
120
|
return ({
|
|
108
121
|
symbol,
|
|
122
|
+
decimals: reserveAsset.decimals,
|
|
123
|
+
isUnsupported,
|
|
109
124
|
underlying: reserveAsset.underlying,
|
|
110
125
|
hub: hubInfo.address,
|
|
111
126
|
hubName: hubInfo?.label,
|
|
@@ -119,12 +134,12 @@ const formatReserveAsset = async (reserveAsset: AaveV4ReserveAssetOnChain, hubAs
|
|
|
119
134
|
liquidationFee: new Dec(reserveAsset.liquidationFee).div(10000).toNumber(),
|
|
120
135
|
maxLiquidationBonus: new Dec(reserveAsset.maxLiquidationBonus).div(10000).toNumber(),
|
|
121
136
|
price: new Dec(reserveAsset.price).div(new Dec(10).pow(oracleDecimals)).toString(),
|
|
122
|
-
totalSupplied:
|
|
123
|
-
totalDrawn:
|
|
124
|
-
totalPremium:
|
|
125
|
-
totalDebt:
|
|
126
|
-
supplyCap:
|
|
127
|
-
borrowCap:
|
|
137
|
+
totalSupplied: toEth(totalSuppliedRaw.toString()),
|
|
138
|
+
totalDrawn: toEth(totalDrawnRaw.toString()),
|
|
139
|
+
totalPremium: toEth(totalPremiumRaw.toString()),
|
|
140
|
+
totalDebt: toEth(totalDebtRaw.toString()),
|
|
141
|
+
supplyCap: toEth(supplyCapRaw.toString()),
|
|
142
|
+
borrowCap: toEth(borrowCapRaw.toString()),
|
|
128
143
|
spokeActive: reserveAsset.spokeActive,
|
|
129
144
|
spokeHalted: reserveAsset.spokeHalted,
|
|
130
145
|
drawnRate: drawnRate.toString(),
|
|
@@ -132,10 +147,10 @@ const formatReserveAsset = async (reserveAsset: AaveV4ReserveAssetOnChain, hubAs
|
|
|
132
147
|
supplyRate: aprToApy(supplyApr.toString()),
|
|
133
148
|
supplyIncentives,
|
|
134
149
|
borrowIncentives,
|
|
135
|
-
canBeBorrowed: reserveAsset.spokeActive && !reserveAsset.spokeHalted && !reserveAsset.paused && !reserveAsset.frozen && reserveAsset.borrowable,
|
|
136
|
-
canBeSupplied: reserveAsset.spokeActive && !reserveAsset.spokeHalted && !reserveAsset.paused && !reserveAsset.frozen,
|
|
137
|
-
canBeWithdrawn: reserveAsset.spokeActive && !reserveAsset.spokeHalted && !reserveAsset.paused,
|
|
138
|
-
canBePayBacked: reserveAsset.spokeActive && !reserveAsset.spokeHalted && !reserveAsset.paused,
|
|
150
|
+
canBeBorrowed: !isUnsupported && reserveAsset.spokeActive && !reserveAsset.spokeHalted && !reserveAsset.paused && !reserveAsset.frozen && reserveAsset.borrowable,
|
|
151
|
+
canBeSupplied: !isUnsupported && reserveAsset.spokeActive && !reserveAsset.spokeHalted && !reserveAsset.paused && !reserveAsset.frozen,
|
|
152
|
+
canBeWithdrawn: !isUnsupported && reserveAsset.spokeActive && !reserveAsset.spokeHalted && !reserveAsset.paused,
|
|
153
|
+
canBePayBacked: !isUnsupported && reserveAsset.spokeActive && !reserveAsset.spokeHalted && !reserveAsset.paused,
|
|
139
154
|
utilization,
|
|
140
155
|
hubLiquidity,
|
|
141
156
|
premiumMultiplier: premiumMultiplier.toString(),
|
|
@@ -182,17 +197,29 @@ export async function _getAaveV4AccountData(provider: Client, network: NetworkNu
|
|
|
182
197
|
const healthFactorFromContract = new Dec(loanData.healthFactor.toString());
|
|
183
198
|
const healthFactor = isMaxUint(healthFactorFromContract.toString()) ? 'Infinity' : healthFactorFromContract.div(1e18).toString();
|
|
184
199
|
const usedAssets = loanData.reserves.reduce((acc: AaveV4UsedReserveAssets, usedReserveAsset) => {
|
|
185
|
-
const
|
|
200
|
+
const assetInfo = getAssetInfoByAddress(usedReserveAsset.underlying, network);
|
|
201
|
+
const isUnsupported = assetInfo.symbol === '?';
|
|
202
|
+
const symbol = wethToEth(assetInfo.symbol);
|
|
203
|
+
const identifier = `${symbol}-${+usedReserveAsset.reserveId.toString()}`;
|
|
186
204
|
const reserveData = spokeData.assetsData[identifier];
|
|
187
|
-
const price = reserveData
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
const
|
|
205
|
+
const price = reserveData?.price ?? '0';
|
|
206
|
+
// For unsupported assets the symbol-based conversion yields NaN, so use the on-chain decimals
|
|
207
|
+
// from the reserve data instead. If the reserve is missing entirely we can't convert, so fall
|
|
208
|
+
// back to '0' and keep the entry read-only.
|
|
209
|
+
const toEth = (raw: string) => {
|
|
210
|
+
if (isMaxUint(raw)) return raw;
|
|
211
|
+
if (!reserveData) return '0';
|
|
212
|
+
if (isUnsupported) return new Dec(raw || 0).div(new Dec(10).pow(reserveData.decimals)).toString();
|
|
213
|
+
return assetAmountInEth(raw, reserveData.symbol);
|
|
214
|
+
};
|
|
215
|
+
const supplied = toEth(usedReserveAsset.supplied.toString());
|
|
216
|
+
const drawn = toEth(usedReserveAsset.drawn.toString());
|
|
217
|
+
const premium = toEth(usedReserveAsset.premium.toString());
|
|
218
|
+
const borrowed = toEth(usedReserveAsset.totalDebt.toString());
|
|
192
219
|
acc[identifier] = {
|
|
193
|
-
symbol: reserveData
|
|
194
|
-
hubName: reserveData
|
|
195
|
-
assetId: reserveData
|
|
220
|
+
symbol: reserveData?.symbol ?? symbol,
|
|
221
|
+
hubName: reserveData?.hubName ?? '',
|
|
222
|
+
assetId: reserveData?.assetId ?? 0,
|
|
196
223
|
reserveId: +usedReserveAsset.reserveId.toString(),
|
|
197
224
|
supplied,
|
|
198
225
|
suppliedUsd: new Dec(supplied).mul(price).toString(),
|
|
@@ -206,6 +233,7 @@ export async function _getAaveV4AccountData(provider: Client, network: NetworkNu
|
|
|
206
233
|
isBorrowed: usedReserveAsset.isBorrowing,
|
|
207
234
|
collateral: usedReserveAsset.isUsingAsCollateral,
|
|
208
235
|
collateralFactor: new Dec(usedReserveAsset.collateralFactor).div(10000).toNumber(),
|
|
236
|
+
isUnsupported: isUnsupported || !reserveData,
|
|
209
237
|
};
|
|
210
238
|
return acc;
|
|
211
239
|
}, {});
|
package/src/claiming/aaveV3.ts
CHANGED
|
@@ -103,7 +103,7 @@ export async function getUnclaimedRewardsForAllMarkets(
|
|
|
103
103
|
return mapAaveRewardsToClaimableTokens(Object.values(totalUnclaimedPerRewardToken), marketAddress, walletAddress);
|
|
104
104
|
}
|
|
105
105
|
|
|
106
|
-
export async function getMeritUnclaimedRewards(account: EthAddress, network: NetworkNumber
|
|
106
|
+
export async function getMeritUnclaimedRewards(account: EthAddress, network: NetworkNumber): Promise<ClaimableToken[]> {
|
|
107
107
|
let data;
|
|
108
108
|
try {
|
|
109
109
|
const res = await fetch(`https://api-merkl.angle.money/v4/users/${account}/rewards?chainId=${network}`,
|
|
@@ -125,8 +125,7 @@ export async function getMeritUnclaimedRewards(account: EthAddress, network: Net
|
|
|
125
125
|
proofs,
|
|
126
126
|
} = reward;
|
|
127
127
|
|
|
128
|
-
|
|
129
|
-
if (!token || !token.symbol || amount === '0' || (isTokenMorpho && !acceptMorpho)) return;
|
|
128
|
+
if (!token || !token.symbol || amount === '0') return;
|
|
130
129
|
|
|
131
130
|
const unclaimedAmount = new Dec(amount).minus(claimed || 0).toString();
|
|
132
131
|
if (unclaimedAmount === '0') return;
|
package/src/claiming/index.ts
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
1
|
import * as aaveV3Claim from './aaveV3';
|
|
2
2
|
import * as compV3Claim from './compV3';
|
|
3
3
|
import * as kingV3Claim from './king';
|
|
4
|
-
import * as morphoBlueClaim from './morphoBlue';
|
|
5
4
|
import * as sparkClaim from './spark';
|
|
6
5
|
|
|
7
6
|
export {
|
|
8
7
|
aaveV3Claim,
|
|
9
8
|
compV3Claim,
|
|
10
9
|
kingV3Claim,
|
|
11
|
-
morphoBlueClaim,
|
|
12
10
|
sparkClaim,
|
|
13
11
|
};
|
package/src/config/contracts.ts
CHANGED
|
@@ -1264,10 +1264,6 @@ export const SparkRewardsController = {
|
|
|
1264
1264
|
}
|
|
1265
1265
|
}
|
|
1266
1266
|
} as const;
|
|
1267
|
-
export const MorphoDistributor = {
|
|
1268
|
-
"abi": [{"inputs":[{"internalType":"address","name":"initialOwner","type":"address"},{"internalType":"uint256","name":"initialTimelock","type":"uint256"},{"internalType":"bytes32","name":"initialRoot","type":"bytes32"},{"internalType":"bytes32","name":"initialIpfsHash","type":"bytes32"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"acceptRoot","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"account","type":"address"},{"internalType":"address","name":"reward","type":"address"},{"internalType":"uint256","name":"claimable","type":"uint256"},{"internalType":"bytes32[]","name":"proof","type":"bytes32[]"}],"name":"claim","outputs":[{"internalType":"uint256","name":"amount","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"account","type":"address"},{"internalType":"address","name":"reward","type":"address"}],"name":"claimed","outputs":[{"internalType":"uint256","name":"amount","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"ipfsHash","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"isUpdater","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"pendingRoot","outputs":[{"internalType":"bytes32","name":"root","type":"bytes32"},{"internalType":"bytes32","name":"ipfsHash","type":"bytes32"},{"internalType":"uint256","name":"validAt","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"revokePendingRoot","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"root","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"setOwner","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes32","name":"newRoot","type":"bytes32"},{"internalType":"bytes32","name":"newIpfsHash","type":"bytes32"}],"name":"setRoot","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"updater","type":"address"},{"internalType":"bool","name":"active","type":"bool"}],"name":"setRootUpdater","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"newTimelock","type":"uint256"}],"name":"setTimelock","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes32","name":"newRoot","type":"bytes32"},{"internalType":"bytes32","name":"newIpfsHash","type":"bytes32"}],"name":"submitRoot","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"timelock","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"}],
|
|
1269
|
-
"networks": {}
|
|
1270
|
-
} as const;
|
|
1271
1267
|
export const AaveRewardsController = {
|
|
1272
1268
|
"abi": [{"inputs":[{"internalType":"address[]","name":"assets","type":"address[]"},{"internalType":"address","name":"user","type":"address"}],"name":"getAllUserRewards","outputs":[{"internalType":"address[]","name":"rewardsList","type":"address[]"},{"internalType":"uint256[]","name":"unclaimedAmounts","type":"uint256[]"}],"stateMutability":"view","type":"function"}],
|
|
1273
1269
|
"networks": {
|
|
@@ -146,12 +146,14 @@ export const getApyAfterValuesEstimation = async (selectedMarket: MorphoBlueMark
|
|
|
146
146
|
return { borrowRate, supplyRate };
|
|
147
147
|
};
|
|
148
148
|
|
|
149
|
-
const API_URL = 'https://
|
|
149
|
+
const API_URL = 'https://api.morpho.org/graphql';
|
|
150
|
+
// Morpho Blue ACRM (Adaptive Curve IRM) always targets 90% utilization — protocol constant
|
|
151
|
+
const ACRM_TARGET_UTILIZATION = '900000000000000000';
|
|
152
|
+
|
|
150
153
|
const MARKET_QUERY = `
|
|
151
|
-
query MarketByUniqueKey($
|
|
152
|
-
|
|
154
|
+
query MarketByUniqueKey($marketId: String!, $chainId: Int!) {
|
|
155
|
+
marketById(marketId: $marketId, chainId: $chainId) {
|
|
153
156
|
reallocatableLiquidityAssets
|
|
154
|
-
targetBorrowUtilization
|
|
155
157
|
loanAsset {
|
|
156
158
|
address
|
|
157
159
|
decimals
|
|
@@ -168,8 +170,8 @@ const MARKET_QUERY = `
|
|
|
168
170
|
address
|
|
169
171
|
name
|
|
170
172
|
}
|
|
171
|
-
|
|
172
|
-
|
|
173
|
+
withdrawMarket {
|
|
174
|
+
marketId
|
|
173
175
|
loanAsset {
|
|
174
176
|
address
|
|
175
177
|
}
|
|
@@ -193,15 +195,15 @@ const MARKET_QUERY = `
|
|
|
193
195
|
address
|
|
194
196
|
}
|
|
195
197
|
irmAddress
|
|
196
|
-
lltv
|
|
198
|
+
lltv
|
|
197
199
|
}
|
|
198
200
|
}
|
|
199
201
|
`;
|
|
200
202
|
|
|
201
203
|
const REWARDS_QUERY = `
|
|
202
|
-
query MarketByUniqueKey($
|
|
203
|
-
|
|
204
|
-
|
|
204
|
+
query MarketByUniqueKey($marketId: String!, $chainId: Int!) {
|
|
205
|
+
marketById(marketId: $marketId, chainId: $chainId) {
|
|
206
|
+
marketId
|
|
205
207
|
state {
|
|
206
208
|
rewards {
|
|
207
209
|
amountPerSuppliedToken
|
|
@@ -230,19 +232,19 @@ export const getReallocatableLiquidity = async (marketId: string, network: Netwo
|
|
|
230
232
|
headers: { 'Content-Type': 'application/json' },
|
|
231
233
|
body: JSON.stringify({
|
|
232
234
|
query: MARKET_QUERY,
|
|
233
|
-
variables: {
|
|
235
|
+
variables: { marketId, chainId: network },
|
|
234
236
|
}),
|
|
235
237
|
signal: AbortSignal.timeout(LONGER_TIMEOUT),
|
|
236
238
|
});
|
|
237
239
|
|
|
238
|
-
const data: { data: {
|
|
239
|
-
const marketData: MorphoBlueRealloactionMarketData = data?.data?.
|
|
240
|
+
const data: { data: { marketById: MorphoBlueRealloactionMarketData } } = await response.json();
|
|
241
|
+
const marketData: MorphoBlueRealloactionMarketData = data?.data?.marketById;
|
|
240
242
|
|
|
241
243
|
if (!marketData) throw new Error('Market data not found');
|
|
242
244
|
|
|
243
245
|
return {
|
|
244
246
|
reallocatableLiquidity: marketData.reallocatableLiquidityAssets,
|
|
245
|
-
targetBorrowUtilization:
|
|
247
|
+
targetBorrowUtilization: ACRM_TARGET_UTILIZATION,
|
|
246
248
|
};
|
|
247
249
|
} catch (error) {
|
|
248
250
|
console.error('External API Failure: Morpho blue reallocatable liquidity', error);
|
|
@@ -295,13 +297,13 @@ export const getReallocation = async (market: MorphoBlueMarketData, assetsData:
|
|
|
295
297
|
headers: { 'Content-Type': 'application/json' },
|
|
296
298
|
body: JSON.stringify({
|
|
297
299
|
query: MARKET_QUERY,
|
|
298
|
-
variables: {
|
|
300
|
+
variables: { marketId, chainId: network },
|
|
299
301
|
}),
|
|
300
302
|
signal: AbortSignal.timeout(LONGER_TIMEOUT),
|
|
301
303
|
});
|
|
302
304
|
|
|
303
|
-
const data: { data: {
|
|
304
|
-
const marketData: MorphoBlueRealloactionMarketData = data?.data?.
|
|
305
|
+
const data: { data: { marketById: MorphoBlueRealloactionMarketData } } = await response.json();
|
|
306
|
+
const marketData: MorphoBlueRealloactionMarketData = data?.data?.marketById;
|
|
305
307
|
|
|
306
308
|
if (!marketData) throw new Error('Market data not found');
|
|
307
309
|
|
|
@@ -315,9 +317,9 @@ export const getReallocation = async (market: MorphoBlueMarketData, assetsData:
|
|
|
315
317
|
const newUtil = new Dec(newTotalBorrowAssets).div(totalSupplyWei).toString();
|
|
316
318
|
const newUtilScaled = new Dec(newUtil).mul(1e18).toString();
|
|
317
319
|
|
|
318
|
-
if (new Dec(newUtilScaled).lt(
|
|
320
|
+
if (new Dec(newUtilScaled).lt(ACRM_TARGET_UTILIZATION)) return { vaults: [], withdrawals: [] };
|
|
319
321
|
|
|
320
|
-
const liquidityToAllocate = getLiquidityToAllocate(amountToBorrow, totalBorrowWei, totalSupplyWei,
|
|
322
|
+
const liquidityToAllocate = getLiquidityToAllocate(amountToBorrow, totalBorrowWei, totalSupplyWei, ACRM_TARGET_UTILIZATION, marketData.reallocatableLiquidityAssets);
|
|
321
323
|
|
|
322
324
|
const vaultTotalAssets = marketData.publicAllocatorSharedLiquidity.reduce(
|
|
323
325
|
(acc: Record<string, string>, item: MorphoBluePublicAllocatorItem) => {
|
|
@@ -348,14 +350,14 @@ export const getReallocation = async (market: MorphoBlueMarketData, assetsData:
|
|
|
348
350
|
totalReallocated = new Dec(totalReallocated).add(amountToTake).toString();
|
|
349
351
|
const withdrawal: [string[], string, string] = [
|
|
350
352
|
[
|
|
351
|
-
item.
|
|
352
|
-
item.
|
|
353
|
-
item.
|
|
354
|
-
item.
|
|
355
|
-
item.
|
|
353
|
+
item.withdrawMarket.loanAsset.address,
|
|
354
|
+
item.withdrawMarket.collateralAsset?.address,
|
|
355
|
+
item.withdrawMarket.oracle?.address,
|
|
356
|
+
item.withdrawMarket.irmAddress,
|
|
357
|
+
item.withdrawMarket.lltv,
|
|
356
358
|
],
|
|
357
359
|
amountToTake.toString(),
|
|
358
|
-
item.
|
|
360
|
+
item.withdrawMarket.marketId,
|
|
359
361
|
];
|
|
360
362
|
if (!withdrawalsPerVault[vaultAddress]) {
|
|
361
363
|
withdrawalsPerVault[vaultAddress] = [];
|
|
@@ -386,12 +388,12 @@ export const getRewardsForMarket = async (marketId: string, network: NetworkNumb
|
|
|
386
388
|
headers: { 'Content-Type': 'application/json' },
|
|
387
389
|
body: JSON.stringify({
|
|
388
390
|
query: REWARDS_QUERY,
|
|
389
|
-
variables: {
|
|
391
|
+
variables: { marketId, chainId: network },
|
|
390
392
|
}),
|
|
391
393
|
});
|
|
392
394
|
|
|
393
395
|
const data = await response.json();
|
|
394
|
-
const marketData = data?.data?.
|
|
396
|
+
const marketData = data?.data?.marketById;
|
|
395
397
|
if (!marketData) throw new Error('Market data not found');
|
|
396
398
|
const morphoAssetInfo = getAssetInfo('MORPHO');
|
|
397
399
|
const { supplyApr, borrowApr } = marketData.state.rewards.find((reward: any) => compareAddresses(reward.asset.address, morphoAssetInfo.addresses[network])) || { supplyApr: '0', borrowApr: '0' };
|
|
@@ -399,8 +401,3 @@ export const getRewardsForMarket = async (marketId: string, network: NetworkNumb
|
|
|
399
401
|
const borrowAprPercent = new Dec(borrowApr).mul(100).toString();
|
|
400
402
|
return { supplyApy: aprToApy(supplyAprPercent), borrowApy: aprToApy(borrowAprPercent) };
|
|
401
403
|
};
|
|
402
|
-
|
|
403
|
-
export const getMorphoUnderlyingSymbol = (_symbol: string) => {
|
|
404
|
-
if (_symbol === 'MORPHO Legacy') return 'MORPHO';
|
|
405
|
-
return wethToEth(_symbol);
|
|
406
|
-
};
|
package/src/portfolio/index.ts
CHANGED
|
@@ -48,7 +48,6 @@ import { getUmbrellaData } from '../umbrella';
|
|
|
48
48
|
import { getMeritUnclaimedRewards, getUnclaimedRewardsForAllMarkets } from '../claiming/aaveV3';
|
|
49
49
|
import { getCompoundV3Rewards } from '../claiming/compV3';
|
|
50
50
|
import { fetchSparkAirdropRewards, fetchSparkRewards } from '../claiming/spark';
|
|
51
|
-
import { fetchMorphoBlueRewards } from '../claiming/morphoBlue';
|
|
52
51
|
import { getKingRewards } from '../claiming/king';
|
|
53
52
|
import { fetchEthenaAirdropRewards } from '../claiming/ethena';
|
|
54
53
|
import { _getAaveV4AccountData, _getAaveV4SpokeData } from '../aaveV4';
|
|
@@ -61,7 +60,6 @@ export async function getPortfolioData(provider: EthereumProvider, network: Netw
|
|
|
61
60
|
}> {
|
|
62
61
|
const isMainnet = network === NetworkNumber.Eth;
|
|
63
62
|
const isFluidSupported = [NetworkNumber.Eth, NetworkNumber.Arb, NetworkNumber.Base, NetworkNumber.Plasma].includes(network);
|
|
64
|
-
const isMorphoRewardsSupported = [NetworkNumber.Eth, NetworkNumber.Base].includes(network);
|
|
65
63
|
|
|
66
64
|
const morphoMarkets = Object.values(MorphoBlueMarkets(network)).filter((market) => market.chainIds.includes(network));
|
|
67
65
|
const compoundV3Markets = Object.values(CompoundMarkets(network)).filter((market) => market.chainIds.includes(network) && market.value !== CompoundVersions.CompoundV2);
|
|
@@ -157,7 +155,6 @@ export async function getPortfolioData(provider: EthereumProvider, network: Netw
|
|
|
157
155
|
spark: {},
|
|
158
156
|
spk: {},
|
|
159
157
|
king: {},
|
|
160
|
-
morpho: {},
|
|
161
158
|
ethena: {},
|
|
162
159
|
};
|
|
163
160
|
}
|
|
@@ -354,28 +351,6 @@ export async function getPortfolioData(provider: EthereumProvider, network: Netw
|
|
|
354
351
|
rewardsData[address.toLowerCase() as EthAddress].aaveV3 = { error: `Error fetching Aave V3 rewards data for address ${address}`, data: null };
|
|
355
352
|
}
|
|
356
353
|
})).flat(),
|
|
357
|
-
// Batch Morpho Blue rewards
|
|
358
|
-
(async () => {
|
|
359
|
-
if (!isMorphoRewardsSupported) return;
|
|
360
|
-
try {
|
|
361
|
-
const morphoRewards = await fetchMorphoBlueRewards(client, network, addresses);
|
|
362
|
-
for (const address of addresses) {
|
|
363
|
-
const lowerAddress = address.toLowerCase() as EthAddress;
|
|
364
|
-
rewardsData[lowerAddress].morpho = {
|
|
365
|
-
error: '',
|
|
366
|
-
data: morphoRewards[lowerAddress] || [],
|
|
367
|
-
};
|
|
368
|
-
}
|
|
369
|
-
} catch (error) {
|
|
370
|
-
console.error('Error fetching Morpho Blue rewards data in batch:', error);
|
|
371
|
-
for (const address of addresses) {
|
|
372
|
-
rewardsData[address.toLowerCase() as EthAddress].morpho = {
|
|
373
|
-
error: 'Error fetching Morpho Blue rewards data in batch',
|
|
374
|
-
data: null,
|
|
375
|
-
};
|
|
376
|
-
}
|
|
377
|
-
}
|
|
378
|
-
})(),
|
|
379
354
|
// Batch Spark Airdrop rewards
|
|
380
355
|
(async () => {
|
|
381
356
|
try {
|
package/src/types/aaveV4.ts
CHANGED
|
@@ -85,6 +85,8 @@ export interface AaveV4ReserveAssetOnChain {
|
|
|
85
85
|
|
|
86
86
|
export interface AaveV4ReserveAssetData {
|
|
87
87
|
symbol: string,
|
|
88
|
+
/** Underlying token decimals as reported on-chain (independent of `@defisaver/tokens`). */
|
|
89
|
+
decimals: number,
|
|
88
90
|
underlying: EthAddress,
|
|
89
91
|
hub: EthAddress,
|
|
90
92
|
hubName: string,
|
|
@@ -130,6 +132,11 @@ export interface AaveV4ReserveAssetData {
|
|
|
130
132
|
hubLiquidity: string,
|
|
131
133
|
premiumMultiplier: string;
|
|
132
134
|
liquidityFee: string;
|
|
135
|
+
/**
|
|
136
|
+
* True when the underlying token is missing from `@defisaver/tokens` (placeholder `?` asset).
|
|
137
|
+
* The reserve is kept for read-only display, but amounts are zeroed and all actions are disabled.
|
|
138
|
+
*/
|
|
139
|
+
isUnsupported?: boolean;
|
|
133
140
|
}
|
|
134
141
|
|
|
135
142
|
export type AaveV4AssetsData = Record<string, AaveV4ReserveAssetData>;
|
|
@@ -151,6 +158,8 @@ export interface AaveV4UsedReserveAsset {
|
|
|
151
158
|
isBorrowed: boolean,
|
|
152
159
|
collateral: boolean,
|
|
153
160
|
collateralFactor: number,
|
|
161
|
+
/** True when the underlying token is missing from `@defisaver/tokens` (placeholder `?` asset). */
|
|
162
|
+
isUnsupported?: boolean,
|
|
154
163
|
}
|
|
155
164
|
|
|
156
165
|
export interface AaveV4AggregatedPositionData {
|
package/src/types/claiming.ts
CHANGED
|
@@ -9,8 +9,6 @@ export enum ClaimType {
|
|
|
9
9
|
COMPOUND_V3_COMP = 'COMPOUND_V3_COMP',
|
|
10
10
|
/** Rewards from Spark (wstETH only for now) */
|
|
11
11
|
SPARK_REWARDS = 'SPARK_REWARDS',
|
|
12
|
-
/** Rewards from Morpho */
|
|
13
|
-
MORPHO = 'MORPHO',
|
|
14
12
|
/** Rewards from King (prev LTR^2 - received for weETH holding) */
|
|
15
13
|
KING_REWARDS = 'KING_REWARDS',
|
|
16
14
|
/** Spark Airdrop */
|
|
@@ -63,18 +61,6 @@ export type KingRewardsClaimableToken = _ClaimableTokenPartial & {
|
|
|
63
61
|
};
|
|
64
62
|
};
|
|
65
63
|
|
|
66
|
-
export type MorphoClaimableToken = _ClaimableTokenPartial & {
|
|
67
|
-
claimType: ClaimType.MORPHO,
|
|
68
|
-
additionalClaimFields: {
|
|
69
|
-
originalAmount: string,
|
|
70
|
-
merkleProofs: string[];
|
|
71
|
-
distributor: EthAddress;
|
|
72
|
-
isLegacy: boolean;
|
|
73
|
-
txData: string;
|
|
74
|
-
}
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
|
|
78
64
|
export type CompoundV3CompClaimableToken = _ClaimableTokenPartial & {
|
|
79
65
|
claimType: ClaimType.COMPOUND_V3_COMP,
|
|
80
66
|
additionalClaimFields: {
|
|
@@ -106,7 +92,6 @@ export type ClaimableToken =
|
|
|
106
92
|
AaveRewardsClaimableToken
|
|
107
93
|
| AaveMeritRewardsClaimableToken
|
|
108
94
|
| CompoundV3CompClaimableToken
|
|
109
|
-
| MorphoClaimableToken
|
|
110
95
|
| SparkRewardsClaimableToken
|
|
111
96
|
| KingRewardsClaimableToken
|
|
112
97
|
| SparkAirdropClaimableToken
|
package/src/types/morphoBlue.ts
CHANGED
|
@@ -206,13 +206,13 @@ export interface MorphoBlueAllocationMarket {
|
|
|
206
206
|
oracle: { address: string },
|
|
207
207
|
irmAddress: string,
|
|
208
208
|
lltv: string,
|
|
209
|
-
|
|
209
|
+
marketId: string,
|
|
210
210
|
}
|
|
211
211
|
|
|
212
212
|
export interface MorphoBluePublicAllocatorItem {
|
|
213
213
|
vault: MorphoBlueVault,
|
|
214
214
|
assets: string,
|
|
215
|
-
|
|
215
|
+
withdrawMarket: MorphoBlueAllocationMarket,
|
|
216
216
|
}
|
|
217
217
|
|
|
218
218
|
export interface MorphoBlueAllocatorMarketState {
|
|
@@ -222,7 +222,6 @@ export interface MorphoBlueAllocatorMarketState {
|
|
|
222
222
|
|
|
223
223
|
export interface MorphoBlueRealloactionMarketData {
|
|
224
224
|
reallocatableLiquidityAssets: string,
|
|
225
|
-
targetBorrowUtilization: string,
|
|
226
225
|
publicAllocatorSharedLiquidity: MorphoBluePublicAllocatorItem[],
|
|
227
226
|
state: MorphoBlueAllocatorMarketState,
|
|
228
227
|
}
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import { Client } from 'viem';
|
|
2
|
-
import { ClaimableToken } from '../types/claiming';
|
|
3
|
-
import { EthAddress, NetworkNumber } from '../types/common';
|
|
4
|
-
export declare const getMorphoBlueRewardsInfo: (address: EthAddress) => Promise<any>;
|
|
5
|
-
export declare const fetchMorphoBlueRewards: (provider: Client, network: NetworkNumber, walletAddresses: EthAddress[]) => Promise<Record<string, ClaimableToken[]>>;
|
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
-
};
|
|
14
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.fetchMorphoBlueRewards = exports.getMorphoBlueRewardsInfo = void 0;
|
|
16
|
-
const tokens_1 = require("@defisaver/tokens");
|
|
17
|
-
const decimal_js_1 = __importDefault(require("decimal.js"));
|
|
18
|
-
const claiming_1 = require("../types/claiming");
|
|
19
|
-
const contracts_1 = require("../contracts");
|
|
20
|
-
const morphoBlueHelpers_1 = require("../helpers/morphoBlueHelpers");
|
|
21
|
-
const MORPHO_ALLOWED_CONTRACTS = [
|
|
22
|
-
'0x330eefa8a787552DC5cAd3C3cA644844B1E61Ddb',
|
|
23
|
-
'0x678dDC1d07eaa166521325394cDEb1E4c086DF43',
|
|
24
|
-
'0x2efd4625d0c149ebadf118ec5446c6de24d916a4',
|
|
25
|
-
];
|
|
26
|
-
const MORPHO_ALLOWED_TOKENS = [
|
|
27
|
-
'MORPHO',
|
|
28
|
-
'MORPHO Legacy',
|
|
29
|
-
];
|
|
30
|
-
const getMorphoBlueRewardsInfo = (address) => __awaiter(void 0, void 0, void 0, function* () {
|
|
31
|
-
if (!address)
|
|
32
|
-
return { claimable: '0' };
|
|
33
|
-
try {
|
|
34
|
-
const res = yield fetch(`https://rewards.morpho.org/v1/users/${address}/distributions`, { signal: AbortSignal.timeout(3000) });
|
|
35
|
-
if (!res.ok)
|
|
36
|
-
throw new Error(yield res.text());
|
|
37
|
-
return yield res.json();
|
|
38
|
-
}
|
|
39
|
-
catch (error) {
|
|
40
|
-
console.error('External API Failure: Morpho Merit', error);
|
|
41
|
-
return { claimable: '0' };
|
|
42
|
-
}
|
|
43
|
-
});
|
|
44
|
-
exports.getMorphoBlueRewardsInfo = getMorphoBlueRewardsInfo;
|
|
45
|
-
const fetchMorphoBlueRewards = (provider, network, walletAddresses) => __awaiter(void 0, void 0, void 0, function* () {
|
|
46
|
-
var _a;
|
|
47
|
-
// Fetch all API data in parallel (these are external API calls, can't be batched with multicall)
|
|
48
|
-
const apiDataPromises = walletAddresses.map(address => (0, exports.getMorphoBlueRewardsInfo)(address));
|
|
49
|
-
const apiDataArray = yield Promise.all(apiDataPromises);
|
|
50
|
-
// Process API data to get claimable tokens for each wallet
|
|
51
|
-
const allClaimableTokens = [];
|
|
52
|
-
const contractCallsData = [];
|
|
53
|
-
for (let i = 0; i < walletAddresses.length; i++) {
|
|
54
|
-
const walletAddress = walletAddresses[i];
|
|
55
|
-
const data = apiDataArray[i];
|
|
56
|
-
const claimableTokens = ((_a = data === null || data === void 0 ? void 0 : data.data) === null || _a === void 0 ? void 0 : _a.reduce((acc, reward) => {
|
|
57
|
-
var _a, _b, _c;
|
|
58
|
-
const token = (0, tokens_1.getAssetInfoByAddress)(reward.asset.address);
|
|
59
|
-
if (!MORPHO_ALLOWED_CONTRACTS.includes((_a = reward === null || reward === void 0 ? void 0 : reward.distributor) === null || _a === void 0 ? void 0 : _a.address) || !MORPHO_ALLOWED_TOKENS.includes(token.symbol)) {
|
|
60
|
-
return acc;
|
|
61
|
-
}
|
|
62
|
-
// Store contract call data for batching
|
|
63
|
-
contractCallsData.push({
|
|
64
|
-
walletAddress,
|
|
65
|
-
tokenAddress: reward.asset.address,
|
|
66
|
-
distributor: (_b = reward === null || reward === void 0 ? void 0 : reward.distributor) === null || _b === void 0 ? void 0 : _b.address,
|
|
67
|
-
});
|
|
68
|
-
return [
|
|
69
|
-
...acc,
|
|
70
|
-
{
|
|
71
|
-
symbol: token.symbol,
|
|
72
|
-
underlyingSymbol: (0, morphoBlueHelpers_1.getMorphoUnderlyingSymbol)(token.symbol),
|
|
73
|
-
tokenAddress: reward.asset.address,
|
|
74
|
-
amount: (0, tokens_1.assetAmountInEth)(reward.claimable, token.symbol),
|
|
75
|
-
walletAddress,
|
|
76
|
-
label: token.symbol,
|
|
77
|
-
claimType: claiming_1.ClaimType.MORPHO,
|
|
78
|
-
additionalClaimFields: {
|
|
79
|
-
originalAmount: reward.claimable,
|
|
80
|
-
distributor: (_c = reward === null || reward === void 0 ? void 0 : reward.distributor) === null || _c === void 0 ? void 0 : _c.address,
|
|
81
|
-
merkleProofs: reward === null || reward === void 0 ? void 0 : reward.proof,
|
|
82
|
-
isLegacy: token.symbol === 'MORPHO Legacy',
|
|
83
|
-
txData: reward === null || reward === void 0 ? void 0 : reward.tx_data,
|
|
84
|
-
},
|
|
85
|
-
}
|
|
86
|
-
];
|
|
87
|
-
}, [])) || [];
|
|
88
|
-
allClaimableTokens.push({ walletAddress, tokens: claimableTokens });
|
|
89
|
-
}
|
|
90
|
-
// Batch all contract calls using multicall
|
|
91
|
-
const contractPromises = contractCallsData.map(({ walletAddress, tokenAddress, distributor }) => {
|
|
92
|
-
const distributorContract = (0, contracts_1.createViemContractFromConfigFunc)('MorphoDistributor', distributor)(provider, network);
|
|
93
|
-
return distributorContract.read.claimed([walletAddress, tokenAddress]);
|
|
94
|
-
});
|
|
95
|
-
const contractResults = yield Promise.all(contractPromises);
|
|
96
|
-
// Process results
|
|
97
|
-
const results = {};
|
|
98
|
-
let contractCallIndex = 0;
|
|
99
|
-
for (const { walletAddress, tokens } of allClaimableTokens) {
|
|
100
|
-
const updatedTokens = [];
|
|
101
|
-
for (let i = 0; i < tokens.length; i++) {
|
|
102
|
-
const claimableToken = tokens[i];
|
|
103
|
-
const claimed = (0, tokens_1.assetAmountInEth)(contractResults[contractCallIndex].toString(), claimableToken.underlyingSymbol);
|
|
104
|
-
contractCallIndex++;
|
|
105
|
-
const updatedToken = Object.assign({}, claimableToken);
|
|
106
|
-
updatedToken.amount = new decimal_js_1.default(claimableToken.amount).sub(claimed).toString();
|
|
107
|
-
updatedTokens.push(updatedToken);
|
|
108
|
-
}
|
|
109
|
-
results[walletAddress.toLowerCase()] = updatedTokens.filter((claimableToken) => new decimal_js_1.default(claimableToken.amount).gt(0));
|
|
110
|
-
}
|
|
111
|
-
return results;
|
|
112
|
-
});
|
|
113
|
-
exports.fetchMorphoBlueRewards = fetchMorphoBlueRewards;
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import { Client } from 'viem';
|
|
2
|
-
import { ClaimableToken } from '../types/claiming';
|
|
3
|
-
import { EthAddress, NetworkNumber } from '../types/common';
|
|
4
|
-
export declare const getMorphoBlueRewardsInfo: (address: EthAddress) => Promise<any>;
|
|
5
|
-
export declare const fetchMorphoBlueRewards: (provider: Client, network: NetworkNumber, walletAddresses: EthAddress[]) => Promise<Record<string, ClaimableToken[]>>;
|