@defisaver/positions-sdk 2.1.157 → 2.1.158-morpho-batch-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/aaveV4/lend.js +1 -0
- package/cjs/aaveV4/merkl.js +50 -10
- package/cjs/config/contracts.d.ts +516 -4
- package/cjs/config/contracts.js +17 -5
- package/cjs/contracts.d.ts +120 -0
- package/cjs/savings/index.js +6 -6
- package/cjs/savings/morphoVaults/index.d.ts +15 -3
- package/cjs/savings/morphoVaults/index.js +97 -26
- package/cjs/types/common.d.ts +2 -0
- package/cjs/types/merkl.d.ts +37 -2
- package/esm/aaveV4/lend.js +1 -0
- package/esm/aaveV4/merkl.js +51 -11
- package/esm/config/contracts.d.ts +516 -4
- package/esm/config/contracts.js +17 -5
- package/esm/contracts.d.ts +120 -0
- package/esm/savings/index.js +6 -6
- package/esm/savings/morphoVaults/index.d.ts +15 -3
- package/esm/savings/morphoVaults/index.js +93 -24
- package/esm/types/common.d.ts +2 -0
- package/esm/types/merkl.d.ts +37 -2
- package/package.json +1 -1
- package/src/aaveV4/lend.ts +1 -0
- package/src/aaveV4/merkl.ts +45 -6
- package/src/config/contracts.ts +17 -5
- package/src/savings/index.ts +6 -6
- package/src/savings/morphoVaults/index.ts +108 -27
- package/src/types/common.ts +2 -0
- package/src/types/merkl.ts +20 -2
package/src/aaveV4/merkl.ts
CHANGED
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
IncentiveData,
|
|
9
9
|
IncentiveKind,
|
|
10
10
|
IncentiveSide,
|
|
11
|
+
IncentiveSource,
|
|
11
12
|
MerklCampaign,
|
|
12
13
|
MerklOpportunity,
|
|
13
14
|
OpportunityAction,
|
|
@@ -17,8 +18,12 @@ import {
|
|
|
17
18
|
|
|
18
19
|
/**
|
|
19
20
|
* Merkl tags Aave V4 reward campaigns by scope via the `type` field:
|
|
20
|
-
* - AAVE_V4_HUB_SUPPLY / AAVE_V4_HUB_BORROW
|
|
21
|
+
* - AAVE_V4_HUB_SUPPLY / AAVE_V4_HUB_BORROW → reward tied to a hub asset
|
|
21
22
|
* - AAVE_V4_SPOKE_SUPPLY / AAVE_V4_SPOKE_BORROW → reward tied to a spoke reserve
|
|
23
|
+
* - AAVE_V4_HUB_NET_LENDING / AAVE_V4_SPOKE_NET_BORROWING → reward on the NET position
|
|
24
|
+
* (supply minus same-token borrows, or vice versa); params list scopes as `hubs[]`/`spokes[]`
|
|
25
|
+
* arrays instead of a flat address+id pair, and only the campaign's own side maps to a reward
|
|
26
|
+
* (the opposite-side ids describe what reduces the accrual)
|
|
22
27
|
* Embedded campaign params provide the exact on-chain identifiers. Token addresses cannot safely
|
|
23
28
|
* identify Aave V4 rewards because one spoke can expose the same underlying from multiple hubs.
|
|
24
29
|
* Each stored reward is also tagged with campaign identity (`campaignIds`/`parentCampaignIds`) so
|
|
@@ -35,13 +40,24 @@ const buildIncentive = (opportunity: MerklOpportunity): IncentiveData => {
|
|
|
35
40
|
apy: aprToApy(opportunity.apr),
|
|
36
41
|
token,
|
|
37
42
|
incentiveKind: IncentiveKind.Reward,
|
|
38
|
-
|
|
43
|
+
source: IncentiveSource.Merkl,
|
|
44
|
+
name: opportunity.name,
|
|
45
|
+
description: opportunity.description || `Eligible for ${token} rewards through Merkl.`,
|
|
39
46
|
};
|
|
40
47
|
};
|
|
41
48
|
|
|
42
49
|
export const buildAaveV4MerklRewardMap = (opportunities: MerklOpportunity[], chainId: NetworkNumber): AaveV4MerklRewardMap => {
|
|
43
50
|
const result: AaveV4MerklRewardMap = { hub: {}, spoke: {} };
|
|
44
51
|
|
|
52
|
+
const endByCampaignId: Record<string, number> = {};
|
|
53
|
+
opportunities.forEach((o) => o.campaigns?.forEach((c) => {
|
|
54
|
+
if (c.id && c.endTimestamp) endByCampaignId[c.id] = +c.endTimestamp;
|
|
55
|
+
}));
|
|
56
|
+
const effectiveEndTimestamp = (campaign: MerklCampaign): number => Math.max(
|
|
57
|
+
campaign.endTimestamp ? +campaign.endTimestamp : 0,
|
|
58
|
+
...(campaign.childCampaignIds || []).map((id) => endByCampaignId[id] || 0),
|
|
59
|
+
);
|
|
60
|
+
|
|
45
61
|
opportunities
|
|
46
62
|
.filter((o) => o.chainId === chainId)
|
|
47
63
|
.filter((o) => o.status === OpportunityStatus.LIVE)
|
|
@@ -64,15 +80,25 @@ export const buildAaveV4MerklRewardMap = (opportunities: MerklOpportunity[], cha
|
|
|
64
80
|
};
|
|
65
81
|
// one opportunity can span several campaigns (e.g. renewed periods), so campaign identity is
|
|
66
82
|
// collected per scope key before the reward entries are written
|
|
67
|
-
const idsByKey: Record<string, { campaignIds: Set<string>, parentCampaignIds: Set<string
|
|
83
|
+
const idsByKey: Record<string, { campaignIds: Set<string>, parentCampaignIds: Set<string>, endTimestamp?: number }> = {};
|
|
68
84
|
const collect = (key: string, campaign: MerklCampaign) => {
|
|
69
85
|
if (!idsByKey[key]) idsByKey[key] = { campaignIds: new Set(), parentCampaignIds: new Set() };
|
|
70
86
|
if (campaign.id) idsByKey[key].campaignIds.add(campaign.id);
|
|
71
87
|
if (campaign.parentCampaignId) idsByKey[key].parentCampaignIds.add(campaign.parentCampaignId);
|
|
88
|
+
const endTimestamp = effectiveEndTimestamp(campaign);
|
|
89
|
+
if (endTimestamp) idsByKey[key].endTimestamp = Math.max(idsByKey[key].endTimestamp || 0, endTimestamp);
|
|
72
90
|
};
|
|
73
91
|
|
|
74
92
|
if (o.type.includes('HUB')) {
|
|
75
93
|
o.campaigns?.forEach((c) => {
|
|
94
|
+
if (c.params?.hubs?.length) {
|
|
95
|
+
c.params.hubs.forEach((hub) => {
|
|
96
|
+
if (!hub.hubAddress) return;
|
|
97
|
+
const rewardedIds = (side === IncentiveSide.Borrow ? hub.borrowAssetIds : hub.lendingAssetIds) || [];
|
|
98
|
+
rewardedIds.forEach((assetId) => collect(scopeKey(hub.hubAddress, assetId), c));
|
|
99
|
+
});
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
76
102
|
const hubAddress = c.params?.hubAddress ?? c.params?.distributionMethodParameters?.distributionSettings?.hubAddress;
|
|
77
103
|
const assetId = c.params?.assetId ?? c.params?.distributionMethodParameters?.distributionSettings?.assetId;
|
|
78
104
|
if (!hubAddress || assetId === undefined || assetId === null) return;
|
|
@@ -80,16 +106,29 @@ export const buildAaveV4MerklRewardMap = (opportunities: MerklOpportunity[], cha
|
|
|
80
106
|
});
|
|
81
107
|
Object.entries(idsByKey).forEach(([key, ids]) => {
|
|
82
108
|
if (!result.hub[key]) result.hub[key] = {};
|
|
83
|
-
result.hub[key][side] = [...(result.hub[key][side] || []), { ...incentive, campaignIds: [...ids.campaignIds] }];
|
|
109
|
+
result.hub[key][side] = [...(result.hub[key][side] || []), { ...incentive, endTimestamp: ids.endTimestamp, campaignIds: [...ids.campaignIds] }];
|
|
84
110
|
});
|
|
85
111
|
} else if (o.type.includes('SPOKE')) {
|
|
86
112
|
o.campaigns?.forEach((c) => {
|
|
113
|
+
if (c.params?.spokes?.length) {
|
|
114
|
+
c.params.spokes.forEach((spoke) => {
|
|
115
|
+
if (!spoke.spokeAddress) return;
|
|
116
|
+
const rewardedTokens = (side === IncentiveSide.Borrow ? spoke.borrowTokens : spoke.supplyTokens) || [];
|
|
117
|
+
rewardedTokens.forEach((tokenScope) => {
|
|
118
|
+
if (tokenScope.reserveId === undefined || tokenScope.reserveId === null) return;
|
|
119
|
+
collect(scopeKey(spoke.spokeAddress, tokenScope.reserveId), c);
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
87
124
|
if (!c.params?.spokeAddress || c.params.reserveId === undefined || c.params.reserveId === null) return;
|
|
88
125
|
collect(scopeKey(c.params.spokeAddress, c.params.reserveId), c);
|
|
89
126
|
});
|
|
90
127
|
Object.entries(idsByKey).forEach(([key, ids]) => {
|
|
91
128
|
if (!result.spoke[key]) result.spoke[key] = {};
|
|
92
|
-
result.spoke[key][side] = [...(result.spoke[key][side] || []), {
|
|
129
|
+
result.spoke[key][side] = [...(result.spoke[key][side] || []), {
|
|
130
|
+
...incentive, endTimestamp: ids.endTimestamp, campaignIds: [...ids.campaignIds], parentCampaignIds: [...ids.parentCampaignIds],
|
|
131
|
+
}];
|
|
93
132
|
});
|
|
94
133
|
}
|
|
95
134
|
});
|
|
@@ -101,7 +140,7 @@ export const getAaveV4MerkleCampaigns = async (chainId: NetworkNumber): Promise<
|
|
|
101
140
|
try {
|
|
102
141
|
const opportunities = await fetchAllMerklOpportunities({
|
|
103
142
|
mainProtocolId: 'aave',
|
|
104
|
-
type: 'AAVE_V4_HUB_SUPPLY,AAVE_V4_HUB_BORROW,AAVE_V4_HUB_NET_LENDING,
|
|
143
|
+
type: 'AAVE_V4_HUB_SUPPLY,AAVE_V4_HUB_BORROW,AAVE_V4_SPOKE_SUPPLY,AAVE_V4_SPOKE_BORROW,AAVE_V4_HUB_NET_LENDING,AAVE_V4_HUB_NET_BORROWING,AAVE_V4_SPOKE_NET_LENDING,AAVE_V4_SPOKE_NET_BORROWING',
|
|
105
144
|
status: OpportunityStatus.LIVE,
|
|
106
145
|
campaigns: 'true',
|
|
107
146
|
});
|
package/src/config/contracts.ts
CHANGED
|
@@ -1126,15 +1126,27 @@ export const MorphoBlueView = {
|
|
|
1126
1126
|
},
|
|
1127
1127
|
} as const;
|
|
1128
1128
|
export const MidnightView = {
|
|
1129
|
-
"abi": [{"inputs":[{"internalType":"bytes32","name":"_id","type":"bytes32"}],"name":"getMarketInfo","outputs":[{"components":[{"internalType":"bytes32","name":"id","type":"bytes32"},{"internalType":"uint128","name":"totalUnits","type":"uint128"},{"internalType":"uint128","name":"lossFactor","type":"uint128"},{"internalType":"uint128","name":"withdrawable","type":"uint128"},{"internalType":"uint128","name":"continuousFeeCredit","type":"uint128"},{"internalType":"uint16[7]","name":"settlementFees","type":"uint16[7]"},{"internalType":"uint32","name":"continuousFee","type":"uint32"},{"internalType":"uint8","name":"tickSpacing","type":"uint8"},{"internalType":"uint256[]","name":"prices","type":"uint256[]"}],"internalType":"struct MidnightView.MarketInfo","name":"info","type":"tuple"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"_id","type":"bytes32"},{"internalType":"address","name":"_user","type":"address"}],"name":"getPositionInfo","outputs":[{"components":[{"internalType":"uint128","name":"credit","type":"uint128"},{"internalType":"uint128","name":"pendingFee","type":"uint128"},{"internalType":"uint128","name":"debt","type":"uint128"},{"internalType":"uint128","name":"collateralBitmap","type":"uint128"},{"internalType":"uint128[]","name":"collateral","type":"uint128[]"},{"internalType":"uint256","name":"ratio","type":"uint256"}],"internalType":"struct MidnightView.PositionInfo","name":"pos","type":"tuple"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"_id","type":"bytes32"},{"internalType":"address","name":"_user","type":"address"}],"name":"getRatio","outputs":[{"internalType":"uint256","name":"ratio","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"components":[{"internalType":"uint256","name":"chainId","type":"uint256"},{"internalType":"address","name":"midnight","type":"address"},{"internalType":"address","name":"loanToken","type":"address"},{"components":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"lltv","type":"uint256"},{"internalType":"uint256","name":"liquidationCursor","type":"uint256"},{"internalType":"address","name":"oracle","type":"address"}],"internalType":"struct CollateralParams[]","name":"collateralParams","type":"tuple[]"},{"internalType":"uint256","name":"maturity","type":"uint256"},{"internalType":"uint256","name":"rcfThreshold","type":"uint256"},{"internalType":"address","name":"enterGate","type":"address"},{"internalType":"address","name":"liquidatorGate","type":"address"}],"internalType":"struct Market","name":"_market","type":"tuple"}],"name":"toId","outputs":[{"internalType":"bytes32","name":"id","type":"bytes32"}],"stateMutability":"pure","type":"function"},{"inputs":[{"internalType":"bytes32","name":"_id","type":"bytes32"}],"name":"toMarket","outputs":[{"components":[{"internalType":"uint256","name":"chainId","type":"uint256"},{"internalType":"address","name":"midnight","type":"address"},{"internalType":"address","name":"loanToken","type":"address"},{"components":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"lltv","type":"uint256"},{"internalType":"uint256","name":"liquidationCursor","type":"uint256"},{"internalType":"address","name":"oracle","type":"address"}],"internalType":"struct CollateralParams[]","name":"collateralParams","type":"tuple[]"},{"internalType":"uint256","name":"maturity","type":"uint256"},{"internalType":"uint256","name":"rcfThreshold","type":"uint256"},{"internalType":"address","name":"enterGate","type":"address"},{"internalType":"address","name":"liquidatorGate","type":"address"}],"internalType":"struct Market","name":"market","type":"tuple"}],"stateMutability":"view","type":"function"}],
|
|
1129
|
+
"abi": [{"inputs":[],"name":"InvalidCollateralIndex","type":"error"},{"inputs":[],"name":"InvalidOfferMarketId","type":"error"},{"inputs":[],"name":"InvalidOfferType","type":"error"},{"inputs":[],"name":"NoOrdersProvided","type":"error"},{"inputs":[],"name":"ZeroAmountRequested","type":"error"},{"inputs":[{"internalType":"bytes32","name":"_id","type":"bytes32"}],"name":"getMarketInfo","outputs":[{"components":[{"internalType":"bytes32","name":"id","type":"bytes32"},{"internalType":"uint128","name":"totalUnits","type":"uint128"},{"internalType":"uint128","name":"lossFactor","type":"uint128"},{"internalType":"uint128","name":"withdrawable","type":"uint128"},{"internalType":"uint128","name":"continuousFeeCredit","type":"uint128"},{"internalType":"uint16[7]","name":"settlementFees","type":"uint16[7]"},{"internalType":"uint32","name":"continuousFee","type":"uint32"},{"internalType":"uint8","name":"tickSpacing","type":"uint8"},{"internalType":"uint256[]","name":"prices","type":"uint256[]"}],"internalType":"struct MidnightView.MarketInfo","name":"info","type":"tuple"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"_id","type":"bytes32"},{"internalType":"address","name":"_user","type":"address"}],"name":"getPositionInfo","outputs":[{"components":[{"internalType":"uint128","name":"credit","type":"uint128"},{"internalType":"uint128","name":"pendingFee","type":"uint128"},{"internalType":"uint128","name":"debt","type":"uint128"},{"internalType":"uint128","name":"collateralBitmap","type":"uint128"},{"internalType":"uint128[]","name":"collateral","type":"uint128[]"},{"internalType":"uint256","name":"ratio","type":"uint256"}],"internalType":"struct MidnightView.PositionInfo","name":"pos","type":"tuple"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"_id","type":"bytes32"},{"internalType":"address","name":"_user","type":"address"}],"name":"getRatio","outputs":[{"internalType":"uint256","name":"ratio","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"components":[{"internalType":"uint256","name":"chainId","type":"uint256"},{"internalType":"address","name":"midnight","type":"address"},{"internalType":"address","name":"loanToken","type":"address"},{"components":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"lltv","type":"uint256"},{"internalType":"uint256","name":"liquidationCursor","type":"uint256"},{"internalType":"address","name":"oracle","type":"address"}],"internalType":"struct CollateralParams[]","name":"collateralParams","type":"tuple[]"},{"internalType":"uint256","name":"maturity","type":"uint256"},{"internalType":"uint256","name":"rcfThreshold","type":"uint256"},{"internalType":"address","name":"enterGate","type":"address"},{"internalType":"address","name":"liquidatorGate","type":"address"}],"internalType":"struct Market","name":"_market","type":"tuple"}],"name":"toId","outputs":[{"internalType":"bytes32","name":"id","type":"bytes32"}],"stateMutability":"pure","type":"function"},{"inputs":[{"internalType":"bytes32","name":"_id","type":"bytes32"}],"name":"toMarket","outputs":[{"components":[{"internalType":"uint256","name":"chainId","type":"uint256"},{"internalType":"address","name":"midnight","type":"address"},{"internalType":"address","name":"loanToken","type":"address"},{"components":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"lltv","type":"uint256"},{"internalType":"uint256","name":"liquidationCursor","type":"uint256"},{"internalType":"address","name":"oracle","type":"address"}],"internalType":"struct CollateralParams[]","name":"collateralParams","type":"tuple[]"},{"internalType":"uint256","name":"maturity","type":"uint256"},{"internalType":"uint256","name":"rcfThreshold","type":"uint256"},{"internalType":"address","name":"enterGate","type":"address"},{"internalType":"address","name":"liquidatorGate","type":"address"}],"internalType":"struct Market","name":"market","type":"tuple"}],"stateMutability":"view","type":"function"}],
|
|
1130
1130
|
"networks": {
|
|
1131
1131
|
"1": {
|
|
1132
|
-
"address": "
|
|
1133
|
-
"createdBlock":
|
|
1132
|
+
"address": "0xb8e1dAAd42e18203cfCED7193FE0e2E19d713A56",
|
|
1133
|
+
"createdBlock": 25983494,
|
|
1134
|
+
"oldVersions": {
|
|
1135
|
+
"25938908": {
|
|
1136
|
+
"abi": [{"inputs":[{"internalType":"bytes32","name":"_id","type":"bytes32"}],"name":"getMarketInfo","outputs":[{"components":[{"internalType":"bytes32","name":"id","type":"bytes32"},{"internalType":"uint128","name":"totalUnits","type":"uint128"},{"internalType":"uint128","name":"lossFactor","type":"uint128"},{"internalType":"uint128","name":"withdrawable","type":"uint128"},{"internalType":"uint128","name":"continuousFeeCredit","type":"uint128"},{"internalType":"uint16[7]","name":"settlementFees","type":"uint16[7]"},{"internalType":"uint32","name":"continuousFee","type":"uint32"},{"internalType":"uint8","name":"tickSpacing","type":"uint8"},{"internalType":"uint256[]","name":"prices","type":"uint256[]"}],"internalType":"struct MidnightView.MarketInfo","name":"info","type":"tuple"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"_id","type":"bytes32"},{"internalType":"address","name":"_user","type":"address"}],"name":"getPositionInfo","outputs":[{"components":[{"internalType":"uint128","name":"credit","type":"uint128"},{"internalType":"uint128","name":"pendingFee","type":"uint128"},{"internalType":"uint128","name":"debt","type":"uint128"},{"internalType":"uint128","name":"collateralBitmap","type":"uint128"},{"internalType":"uint128[]","name":"collateral","type":"uint128[]"},{"internalType":"uint256","name":"ratio","type":"uint256"}],"internalType":"struct MidnightView.PositionInfo","name":"pos","type":"tuple"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"_id","type":"bytes32"},{"internalType":"address","name":"_user","type":"address"}],"name":"getRatio","outputs":[{"internalType":"uint256","name":"ratio","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"components":[{"internalType":"uint256","name":"chainId","type":"uint256"},{"internalType":"address","name":"midnight","type":"address"},{"internalType":"address","name":"loanToken","type":"address"},{"components":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"lltv","type":"uint256"},{"internalType":"uint256","name":"liquidationCursor","type":"uint256"},{"internalType":"address","name":"oracle","type":"address"}],"internalType":"struct CollateralParams[]","name":"collateralParams","type":"tuple[]"},{"internalType":"uint256","name":"maturity","type":"uint256"},{"internalType":"uint256","name":"rcfThreshold","type":"uint256"},{"internalType":"address","name":"enterGate","type":"address"},{"internalType":"address","name":"liquidatorGate","type":"address"}],"internalType":"struct Market","name":"_market","type":"tuple"}],"name":"toId","outputs":[{"internalType":"bytes32","name":"id","type":"bytes32"}],"stateMutability":"pure","type":"function"},{"inputs":[{"internalType":"bytes32","name":"_id","type":"bytes32"}],"name":"toMarket","outputs":[{"components":[{"internalType":"uint256","name":"chainId","type":"uint256"},{"internalType":"address","name":"midnight","type":"address"},{"internalType":"address","name":"loanToken","type":"address"},{"components":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"lltv","type":"uint256"},{"internalType":"uint256","name":"liquidationCursor","type":"uint256"},{"internalType":"address","name":"oracle","type":"address"}],"internalType":"struct CollateralParams[]","name":"collateralParams","type":"tuple[]"},{"internalType":"uint256","name":"maturity","type":"uint256"},{"internalType":"uint256","name":"rcfThreshold","type":"uint256"},{"internalType":"address","name":"enterGate","type":"address"},{"internalType":"address","name":"liquidatorGate","type":"address"}],"internalType":"struct Market","name":"market","type":"tuple"}],"stateMutability":"view","type":"function"}],
|
|
1137
|
+
"address": "0xB64FBf011343961D9AC4A04b714414E35ebE6BE0",
|
|
1138
|
+
}
|
|
1139
|
+
},
|
|
1134
1140
|
},
|
|
1135
1141
|
"8453": {
|
|
1136
|
-
"address": "
|
|
1137
|
-
"createdBlock":
|
|
1142
|
+
"address": "0xdEab43eb1519bD4314bc99c4019866734103D3Ae",
|
|
1143
|
+
"createdBlock": 51346932,
|
|
1144
|
+
"oldVersions": {
|
|
1145
|
+
"48932293": {
|
|
1146
|
+
"abi": [{"inputs":[{"internalType":"bytes32","name":"_id","type":"bytes32"}],"name":"getMarketInfo","outputs":[{"components":[{"internalType":"bytes32","name":"id","type":"bytes32"},{"internalType":"uint128","name":"totalUnits","type":"uint128"},{"internalType":"uint128","name":"lossFactor","type":"uint128"},{"internalType":"uint128","name":"withdrawable","type":"uint128"},{"internalType":"uint128","name":"continuousFeeCredit","type":"uint128"},{"internalType":"uint16[7]","name":"settlementFees","type":"uint16[7]"},{"internalType":"uint32","name":"continuousFee","type":"uint32"},{"internalType":"uint8","name":"tickSpacing","type":"uint8"},{"internalType":"uint256[]","name":"prices","type":"uint256[]"}],"internalType":"struct MidnightView.MarketInfo","name":"info","type":"tuple"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"_id","type":"bytes32"},{"internalType":"address","name":"_user","type":"address"}],"name":"getPositionInfo","outputs":[{"components":[{"internalType":"uint128","name":"credit","type":"uint128"},{"internalType":"uint128","name":"pendingFee","type":"uint128"},{"internalType":"uint128","name":"debt","type":"uint128"},{"internalType":"uint128","name":"collateralBitmap","type":"uint128"},{"internalType":"uint128[]","name":"collateral","type":"uint128[]"},{"internalType":"uint256","name":"ratio","type":"uint256"}],"internalType":"struct MidnightView.PositionInfo","name":"pos","type":"tuple"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"_id","type":"bytes32"},{"internalType":"address","name":"_user","type":"address"}],"name":"getRatio","outputs":[{"internalType":"uint256","name":"ratio","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"components":[{"internalType":"uint256","name":"chainId","type":"uint256"},{"internalType":"address","name":"midnight","type":"address"},{"internalType":"address","name":"loanToken","type":"address"},{"components":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"lltv","type":"uint256"},{"internalType":"uint256","name":"liquidationCursor","type":"uint256"},{"internalType":"address","name":"oracle","type":"address"}],"internalType":"struct CollateralParams[]","name":"collateralParams","type":"tuple[]"},{"internalType":"uint256","name":"maturity","type":"uint256"},{"internalType":"uint256","name":"rcfThreshold","type":"uint256"},{"internalType":"address","name":"enterGate","type":"address"},{"internalType":"address","name":"liquidatorGate","type":"address"}],"internalType":"struct Market","name":"_market","type":"tuple"}],"name":"toId","outputs":[{"internalType":"bytes32","name":"id","type":"bytes32"}],"stateMutability":"pure","type":"function"},{"inputs":[{"internalType":"bytes32","name":"_id","type":"bytes32"}],"name":"toMarket","outputs":[{"components":[{"internalType":"uint256","name":"chainId","type":"uint256"},{"internalType":"address","name":"midnight","type":"address"},{"internalType":"address","name":"loanToken","type":"address"},{"components":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"lltv","type":"uint256"},{"internalType":"uint256","name":"liquidationCursor","type":"uint256"},{"internalType":"address","name":"oracle","type":"address"}],"internalType":"struct CollateralParams[]","name":"collateralParams","type":"tuple[]"},{"internalType":"uint256","name":"maturity","type":"uint256"},{"internalType":"uint256","name":"rcfThreshold","type":"uint256"},{"internalType":"address","name":"enterGate","type":"address"},{"internalType":"address","name":"liquidatorGate","type":"address"}],"internalType":"struct Market","name":"market","type":"tuple"}],"stateMutability":"view","type":"function"}],
|
|
1147
|
+
"address": "0x3aa272f329E8B562A3bA56Bb6979a44D23A28839",
|
|
1148
|
+
}
|
|
1149
|
+
},
|
|
1138
1150
|
}
|
|
1139
1151
|
},
|
|
1140
1152
|
} as const;
|
package/src/savings/index.ts
CHANGED
|
@@ -43,15 +43,15 @@ export const getSavingsData = async (
|
|
|
43
43
|
|
|
44
44
|
await Promise.all([
|
|
45
45
|
...(network === NetworkNumber.Eth ? [
|
|
46
|
-
|
|
46
|
+
(async () => {
|
|
47
47
|
try {
|
|
48
|
-
const
|
|
49
|
-
const data = await morphoVaults.
|
|
50
|
-
savingsData
|
|
48
|
+
const vaults = morphoVaultsList.map((vaultKey) => morphoVaults.morphoVaultsOptions.getMorphoVault(vaultKey));
|
|
49
|
+
const data = await morphoVaults.getMorphoVaultsData(provider, network, vaults, accounts);
|
|
50
|
+
Object.assign(savingsData, data);
|
|
51
51
|
} catch (err) {
|
|
52
|
-
console.error(
|
|
52
|
+
console.error('[getSavingsData] Error fetching morpho vaults:', err);
|
|
53
53
|
}
|
|
54
|
-
}),
|
|
54
|
+
})(),
|
|
55
55
|
...yearnVaultsList.map(async (vaultKey) => {
|
|
56
56
|
try {
|
|
57
57
|
const vault = yearnVaults.yearnVaultsOptions.getYearnVault(vaultKey);
|
|
@@ -3,52 +3,100 @@ import Dec from 'decimal.js';
|
|
|
3
3
|
import { request as graphqlRequest } from 'graphql-request';
|
|
4
4
|
import { assetAmountInEth } from '@defisaver/tokens';
|
|
5
5
|
import * as morphoVaultsOptions from './options';
|
|
6
|
-
import {
|
|
6
|
+
import {
|
|
7
|
+
EthAddress, EthereumProvider, NetworkNumber, MorphoVault, MorphoVaultType, SavingsVaultData,
|
|
8
|
+
} from '../../types';
|
|
7
9
|
import { getViemProvider } from '../../services/viem';
|
|
8
10
|
import { getMorphoVaultContractViem } from '../../contracts';
|
|
9
|
-
import { MorphoVault, SavingsVaultData } from '../../types';
|
|
10
11
|
|
|
11
12
|
export {
|
|
12
13
|
morphoVaultsOptions,
|
|
13
14
|
};
|
|
14
15
|
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
16
|
+
const MORPHO_BLUE_API = 'https://api.morpho.org/graphql';
|
|
17
|
+
|
|
18
|
+
// Morpho API caps list page size at 100 items
|
|
19
|
+
const MORPHO_API_PAGE_SIZE = 100;
|
|
20
|
+
|
|
21
|
+
const vaultsLiquidityQuery = `
|
|
22
|
+
query VaultsLiquidity($addresses: [String!], $chainIds: [Int!]) {
|
|
23
|
+
vaults(first: ${MORPHO_API_PAGE_SIZE}, where: { address_in: $addresses, chainId_in: $chainIds }) {
|
|
24
|
+
items {
|
|
25
|
+
address
|
|
26
|
+
liquidity {
|
|
27
|
+
underlying
|
|
28
|
+
}
|
|
27
29
|
}
|
|
28
30
|
}
|
|
29
31
|
}`;
|
|
30
32
|
|
|
31
|
-
|
|
33
|
+
/**
|
|
34
|
+
* Fetches liquidity for all given vaults in a single API request (chunked if over the page cap),
|
|
35
|
+
* returned as a map keyed by lowercased vault address. A vault missing from the API response is
|
|
36
|
+
* absent from the map.
|
|
37
|
+
*/
|
|
38
|
+
export const fetchMorphoVaultsLiquidity = async (network: NetworkNumber, vaults: MorphoVault[]): Promise<Record<string, string>> => {
|
|
39
|
+
const chunks: MorphoVault[][] = [];
|
|
40
|
+
for (let i = 0; i < vaults.length; i += MORPHO_API_PAGE_SIZE) chunks.push(vaults.slice(i, i + MORPHO_API_PAGE_SIZE));
|
|
41
|
+
|
|
42
|
+
const liquidityByAddress: Record<string, string> = {};
|
|
43
|
+
await Promise.all(chunks.map(async (chunk) => {
|
|
44
|
+
const data = await graphqlRequest(MORPHO_BLUE_API, vaultsLiquidityQuery, {
|
|
45
|
+
addresses: chunk.map((vault) => vault.address),
|
|
46
|
+
chainIds: [network],
|
|
47
|
+
// the BigInt scalar serializes as a JSON number when it fits in Number.MAX_SAFE_INTEGER, a string otherwise
|
|
48
|
+
}) as { vaults: { items: { address: string, liquidity: { underlying: string | number | null } | null }[] | null } };
|
|
32
49
|
|
|
33
|
-
|
|
50
|
+
(data.vaults.items || []).forEach((item) => {
|
|
51
|
+
if (item?.liquidity?.underlying !== undefined && item.liquidity?.underlying !== null) {
|
|
52
|
+
liquidityByAddress[item.address.toLowerCase()] = String(item.liquidity.underlying);
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
}));
|
|
56
|
+
return liquidityByAddress;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
const getBatchedViemProvider = (provider: EthereumProvider, network: NetworkNumber) => getViemProvider(provider, network, {
|
|
60
|
+
batch: {
|
|
61
|
+
multicall: {
|
|
62
|
+
batchSize: 2500000,
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
const getMorphoVaultChainData = async (provider: Client, morphoVault: MorphoVault, accounts: EthAddress[]) => {
|
|
34
68
|
const morphoVaultContract = getMorphoVaultContractViem(provider, morphoVault.address);
|
|
35
69
|
|
|
36
70
|
const shares: Record<EthAddress, bigint> = {};
|
|
37
71
|
|
|
38
|
-
const [totalAssets, totalSupply, decimals, decimalsOffset
|
|
72
|
+
const [totalAssets, totalSupply, decimals, decimalsOffset] = await Promise.all([
|
|
39
73
|
morphoVaultContract.read.totalAssets(),
|
|
40
74
|
morphoVaultContract.read.totalSupply(),
|
|
41
75
|
morphoVaultContract.read.decimals(),
|
|
42
76
|
morphoVaultContract.read.DECIMALS_OFFSET(),
|
|
43
|
-
graphqlRequest(MORPHO_BLUE_API, vaultDataQuery, { address: morphoVault.address, chainId: network }),
|
|
44
77
|
...accounts.map(async (account) => {
|
|
45
78
|
const share = await morphoVaultContract.read.balanceOf([account]);
|
|
46
79
|
shares[account] = share;
|
|
47
80
|
}),
|
|
48
81
|
]);
|
|
49
82
|
|
|
83
|
+
return {
|
|
84
|
+
totalAssets, totalSupply, decimals, decimalsOffset, shares,
|
|
85
|
+
};
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
const formatMorphoVaultData = (
|
|
89
|
+
morphoVault: MorphoVault,
|
|
90
|
+
chainData: Awaited<ReturnType<typeof getMorphoVaultChainData>>,
|
|
91
|
+
liquidityUnderlying: string,
|
|
92
|
+
accounts: EthAddress[],
|
|
93
|
+
): SavingsVaultData => {
|
|
94
|
+
const {
|
|
95
|
+
totalAssets, totalSupply, decimals, decimalsOffset, shares,
|
|
96
|
+
} = chainData;
|
|
97
|
+
|
|
50
98
|
const poolSize = assetAmountInEth(totalAssets.toString(), morphoVault.asset);
|
|
51
|
-
const liquidity = assetAmountInEth(
|
|
99
|
+
const liquidity = assetAmountInEth(liquidityUnderlying, morphoVault.asset);
|
|
52
100
|
|
|
53
101
|
const supplied: Record<EthAddress, string> = {};
|
|
54
102
|
accounts.forEach((account) => {
|
|
@@ -67,12 +115,45 @@ export const _getMorphoVaultData = async (provider: Client, network: NetworkNumb
|
|
|
67
115
|
};
|
|
68
116
|
};
|
|
69
117
|
|
|
118
|
+
/**
|
|
119
|
+
* Fetches data for all given vaults with a single Morpho API request for their liquidity,
|
|
120
|
+
* instead of one request per vault. The on-chain reads run in parallel with the API request.
|
|
121
|
+
* A vault whose on-chain reads fail or that is missing from the API response is left out of
|
|
122
|
+
* the result; a failed API request rejects, leaving all vaults out.
|
|
123
|
+
*/
|
|
124
|
+
export const _getMorphoVaultsData = async (provider: Client, network: NetworkNumber, vaults: MorphoVault[], accounts: EthAddress[]): Promise<Partial<Record<MorphoVaultType, SavingsVaultData>>> => {
|
|
125
|
+
const [liquidityByAddress, chainData] = await Promise.all([
|
|
126
|
+
fetchMorphoVaultsLiquidity(network, vaults),
|
|
127
|
+
Promise.all(vaults.map((vault) => getMorphoVaultChainData(provider, vault, accounts).catch((err) => {
|
|
128
|
+
console.error(`[getMorphoVaultsData] Error fetching on-chain data for morpho vault ${vault.type}:`, err);
|
|
129
|
+
return null;
|
|
130
|
+
}))),
|
|
131
|
+
]);
|
|
132
|
+
|
|
133
|
+
const savingsData: Partial<Record<MorphoVaultType, SavingsVaultData>> = {};
|
|
134
|
+
vaults.forEach((vault, i) => {
|
|
135
|
+
const vaultChainData = chainData[i];
|
|
136
|
+
if (!vaultChainData) return;
|
|
137
|
+
const liquidityUnderlying = liquidityByAddress[vault.address.toLowerCase()];
|
|
138
|
+
if (liquidityUnderlying === undefined) {
|
|
139
|
+
console.error(`[getMorphoVaultsData] Vault ${vault.type} (${vault.address}) missing from Morpho API response`);
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
savingsData[vault.type] = formatMorphoVaultData(vault, vaultChainData, liquidityUnderlying, accounts);
|
|
143
|
+
});
|
|
144
|
+
return savingsData;
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
export async function getMorphoVaultsData(provider: EthereumProvider, network: NetworkNumber, vaults: MorphoVault[], accounts: EthAddress[]): Promise<Partial<Record<MorphoVaultType, SavingsVaultData>>> {
|
|
148
|
+
return _getMorphoVaultsData(getBatchedViemProvider(provider, network), network, vaults, accounts);
|
|
149
|
+
}
|
|
150
|
+
|
|
70
151
|
export async function getMorphoVaultData(provider: EthereumProvider, network: NetworkNumber, morphoVault: MorphoVault, accounts: EthAddress[]): Promise<SavingsVaultData> {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
}
|
|
152
|
+
const [liquidityByAddress, chainData] = await Promise.all([
|
|
153
|
+
fetchMorphoVaultsLiquidity(network, [morphoVault]),
|
|
154
|
+
getMorphoVaultChainData(getBatchedViemProvider(provider, network), morphoVault, accounts),
|
|
155
|
+
]);
|
|
156
|
+
const liquidityUnderlying = liquidityByAddress[morphoVault.address.toLowerCase()];
|
|
157
|
+
if (liquidityUnderlying === undefined) throw new Error(`Vault ${morphoVault.address} missing from Morpho API response`);
|
|
158
|
+
return formatMorphoVaultData(morphoVault, chainData, liquidityUnderlying, accounts);
|
|
159
|
+
}
|
package/src/types/common.ts
CHANGED
package/src/types/merkl.ts
CHANGED
|
@@ -14,8 +14,6 @@ export enum OpportunityStatus {
|
|
|
14
14
|
export type MerklCampaign = {
|
|
15
15
|
id: string;
|
|
16
16
|
campaignId: string;
|
|
17
|
-
startTimestamp?: number;
|
|
18
|
-
endTimestamp?: number;
|
|
19
17
|
/**
|
|
20
18
|
* Merkl-internal `id` of the parent campaign — set on child campaigns, which re-publish a hub
|
|
21
19
|
* (parent) campaign's reward scoped to a single spoke reserve. Absent on standalone campaigns,
|
|
@@ -23,12 +21,32 @@ export type MerklCampaign = {
|
|
|
23
21
|
*/
|
|
24
22
|
parentCampaignId?: string;
|
|
25
23
|
childCampaignIds?: string[];
|
|
24
|
+
startTimestamp?: number;
|
|
25
|
+
endTimestamp?: number;
|
|
26
26
|
params?: {
|
|
27
27
|
reserveId?: string | number;
|
|
28
28
|
spokeAddress?: EthAddress;
|
|
29
29
|
hubAddress?: EthAddress;
|
|
30
30
|
hubAssetId?: string | number;
|
|
31
31
|
assetId?: string | number;
|
|
32
|
+
/**
|
|
33
|
+
* AAVE_V4_HUB_NET_* campaigns scope per hub: the reward applies to the ids on the campaign's
|
|
34
|
+
* own side (`lendingAssetIds` for LEND, `borrowAssetIds` for BORROW) — the opposite-side ids
|
|
35
|
+
* only describe positions that reduce the net accrual, so they never map to a reward.
|
|
36
|
+
*/
|
|
37
|
+
hubs?: {
|
|
38
|
+
hubAddress: EthAddress;
|
|
39
|
+
assets?: { symbol: string; assetId: string | number; decimals?: number; underlyingToken?: EthAddress }[];
|
|
40
|
+
lendingAssetIds?: (string | number)[];
|
|
41
|
+
borrowAssetIds?: (string | number)[];
|
|
42
|
+
}[];
|
|
43
|
+
/** AAVE_V4_SPOKE_NET_* campaigns scope per spoke, with the same own-side rule as `hubs`. */
|
|
44
|
+
spokes?: {
|
|
45
|
+
spokeAddress: EthAddress;
|
|
46
|
+
spokeName?: string;
|
|
47
|
+
supplyTokens?: { symbol: string; reserveId: string | number; hubAddress?: EthAddress; hubAssetId?: string | number; underlyingToken?: EthAddress }[];
|
|
48
|
+
borrowTokens?: { symbol: string; reserveId: string | number; hubAddress?: EthAddress; hubAssetId?: string | number; underlyingToken?: EthAddress }[];
|
|
49
|
+
}[];
|
|
32
50
|
distributionMethodParameters?: {
|
|
33
51
|
distributionMethod?: string;
|
|
34
52
|
distributionSettings?: {
|