@metamask-previews/earn-controller 0.14.0-preview-7bb49dcb → 0.14.0-preview-c47fa49d
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/CHANGELOG.md +0 -1
- package/dist/EarnController.cjs +91 -431
- package/dist/EarnController.cjs.map +1 -1
- package/dist/EarnController.d.cts +25 -223
- package/dist/EarnController.d.cts.map +1 -1
- package/dist/EarnController.d.mts +25 -223
- package/dist/EarnController.d.mts.map +1 -1
- package/dist/EarnController.mjs +92 -432
- package/dist/EarnController.mjs.map +1 -1
- package/dist/index.cjs +1 -14
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -2
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +1 -2
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +0 -1
- package/dist/index.mjs.map +1 -1
- package/dist/types.cjs.map +1 -1
- package/dist/types.d.cts +0 -8
- package/dist/types.d.cts.map +1 -1
- package/dist/types.d.mts +0 -8
- package/dist/types.d.mts.map +1 -1
- package/dist/types.mjs.map +1 -1
- package/package.json +3 -5
- package/dist/selectors.cjs +0 -76
- package/dist/selectors.cjs.map +0 -1
- package/dist/selectors.d.cts +0 -336
- package/dist/selectors.d.cts.map +0 -1
- package/dist/selectors.d.mts +0 -336
- package/dist/selectors.d.mts.map +0 -1
- package/dist/selectors.mjs +0 -68
- package/dist/selectors.mjs.map +0 -1
package/dist/EarnController.cjs
CHANGED
@@ -1,18 +1,18 @@
|
|
1
1
|
"use strict";
|
2
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
4
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
5
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
6
|
+
};
|
2
7
|
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
3
8
|
if (kind === "m") throw new TypeError("Private method is not writable");
|
4
9
|
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
5
10
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
6
11
|
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
7
12
|
};
|
8
|
-
var
|
9
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
10
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
11
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
12
|
-
};
|
13
|
-
var _EarnController_instances, _EarnController_earnSDK, _EarnController_selectedNetworkClientId, _EarnController_earnApiService, _EarnController_addTransactionFn, _EarnController_supportedPooledStakingChains, _EarnController_initializeSDK, _EarnController_getCurrentAccount, _EarnController_getCurrentChainId;
|
13
|
+
var _EarnController_instances, _EarnController_stakeSDK, _EarnController_selectedNetworkClientId, _EarnController_stakingApiService, _EarnController_initializeSDK, _EarnController_getCurrentAccount, _EarnController_getCurrentChainId;
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
15
|
-
exports.EarnController = exports.getDefaultEarnControllerState = exports.
|
15
|
+
exports.EarnController = exports.getDefaultEarnControllerState = exports.controllerName = void 0;
|
16
16
|
const providers_1 = require("@ethersproject/providers");
|
17
17
|
const base_controller_1 = require("@metamask/base-controller");
|
18
18
|
const controller_utils_1 = require("@metamask/controller-utils");
|
@@ -24,10 +24,6 @@ const stakingTransactionTypes = new Set([
|
|
24
24
|
transaction_controller_1.TransactionType.stakingUnstake,
|
25
25
|
transaction_controller_1.TransactionType.stakingClaim,
|
26
26
|
]);
|
27
|
-
const lendingTransactionTypes = new Set([
|
28
|
-
transaction_controller_1.TransactionType.lendingDeposit,
|
29
|
-
'lendingWithdraw',
|
30
|
-
]);
|
31
27
|
/**
|
32
28
|
* Metadata for the EarnController.
|
33
29
|
*/
|
@@ -36,7 +32,7 @@ const earnControllerMetadata = {
|
|
36
32
|
persist: true,
|
37
33
|
anonymous: false,
|
38
34
|
},
|
39
|
-
|
35
|
+
stablecoin_lending: {
|
40
36
|
persist: true,
|
41
37
|
anonymous: false,
|
42
38
|
},
|
@@ -46,42 +42,17 @@ const earnControllerMetadata = {
|
|
46
42
|
},
|
47
43
|
};
|
48
44
|
// === Default State ===
|
49
|
-
|
50
|
-
|
51
|
-
chainId: 0,
|
52
|
-
protocol: '',
|
45
|
+
const DEFAULT_STABLECOIN_VAULT = {
|
46
|
+
symbol: '',
|
53
47
|
name: '',
|
54
|
-
address: '',
|
55
|
-
tvlUnderlying: '0',
|
56
|
-
netSupplyRate: 0,
|
57
|
-
totalSupplyRate: 0,
|
58
|
-
underlying: {
|
59
|
-
address: '',
|
60
|
-
chainId: 0,
|
61
|
-
},
|
62
|
-
outputToken: {
|
63
|
-
address: '',
|
64
|
-
chainId: 0,
|
65
|
-
},
|
66
|
-
rewards: [
|
67
|
-
{
|
68
|
-
token: {
|
69
|
-
address: '',
|
70
|
-
chainId: 0,
|
71
|
-
},
|
72
|
-
rate: 0,
|
73
|
-
},
|
74
|
-
],
|
75
|
-
};
|
76
|
-
exports.DEFAULT_LENDING_POSITION = {
|
77
|
-
id: '',
|
78
48
|
chainId: 0,
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
49
|
+
tokenAddress: '',
|
50
|
+
vaultAddress: '',
|
51
|
+
currentAPY: '0',
|
52
|
+
supply: '0',
|
53
|
+
liquidity: '0',
|
83
54
|
};
|
84
|
-
|
55
|
+
const DEFAULT_POOLED_STAKING_VAULT_APY_AVERAGES = {
|
85
56
|
oneDay: '0',
|
86
57
|
oneWeek: '0',
|
87
58
|
oneMonth: '0',
|
@@ -89,24 +60,6 @@ exports.DEFAULT_POOLED_STAKING_VAULT_APY_AVERAGES = {
|
|
89
60
|
sixMonths: '0',
|
90
61
|
oneYear: '0',
|
91
62
|
};
|
92
|
-
exports.DEFAULT_POOLED_STAKING_CHAIN_STATE = {
|
93
|
-
pooledStakes: {
|
94
|
-
account: '',
|
95
|
-
lifetimeRewards: '0',
|
96
|
-
assets: '0',
|
97
|
-
exitRequests: [],
|
98
|
-
},
|
99
|
-
exchangeRate: '1',
|
100
|
-
vaultMetadata: {
|
101
|
-
apy: '0',
|
102
|
-
capacity: '0',
|
103
|
-
feePercent: 0,
|
104
|
-
totalAssets: '0',
|
105
|
-
vaultAddress: '0x0000000000000000000000000000000000000000',
|
106
|
-
},
|
107
|
-
vaultDailyApys: [],
|
108
|
-
vaultApyAverages: exports.DEFAULT_POOLED_STAKING_VAULT_APY_AVERAGES,
|
109
|
-
};
|
110
63
|
/**
|
111
64
|
* Gets the default state for the EarnController.
|
112
65
|
*
|
@@ -115,12 +68,26 @@ exports.DEFAULT_POOLED_STAKING_CHAIN_STATE = {
|
|
115
68
|
function getDefaultEarnControllerState() {
|
116
69
|
return {
|
117
70
|
pooled_staking: {
|
71
|
+
pooledStakes: {
|
72
|
+
account: '',
|
73
|
+
lifetimeRewards: '0',
|
74
|
+
assets: '0',
|
75
|
+
exitRequests: [],
|
76
|
+
},
|
77
|
+
exchangeRate: '1',
|
78
|
+
vaultMetadata: {
|
79
|
+
apy: '0',
|
80
|
+
capacity: '0',
|
81
|
+
feePercent: 0,
|
82
|
+
totalAssets: '0',
|
83
|
+
vaultAddress: '0x0000000000000000000000000000000000000000',
|
84
|
+
},
|
85
|
+
vaultDailyApys: [],
|
86
|
+
vaultApyAverages: DEFAULT_POOLED_STAKING_VAULT_APY_AVERAGES,
|
118
87
|
isEligible: false,
|
119
88
|
},
|
120
|
-
|
121
|
-
|
122
|
-
positions: [exports.DEFAULT_LENDING_POSITION],
|
123
|
-
isEligible: false,
|
89
|
+
stablecoin_lending: {
|
90
|
+
vaults: [DEFAULT_STABLECOIN_VAULT],
|
124
91
|
},
|
125
92
|
lastUpdated: 0,
|
126
93
|
};
|
@@ -131,7 +98,7 @@ exports.getDefaultEarnControllerState = getDefaultEarnControllerState;
|
|
131
98
|
* EarnController manages DeFi earning opportunities across different protocols and chains.
|
132
99
|
*/
|
133
100
|
class EarnController extends base_controller_1.BaseController {
|
134
|
-
constructor({ messenger, state = {},
|
101
|
+
constructor({ messenger, state = {}, }) {
|
135
102
|
super({
|
136
103
|
name: exports.controllerName,
|
137
104
|
metadata: earnControllerMetadata,
|
@@ -142,37 +109,22 @@ class EarnController extends base_controller_1.BaseController {
|
|
142
109
|
},
|
143
110
|
});
|
144
111
|
_EarnController_instances.add(this);
|
145
|
-
|
112
|
+
_EarnController_stakeSDK.set(this, null);
|
146
113
|
_EarnController_selectedNetworkClientId.set(this, void 0);
|
147
|
-
|
148
|
-
|
149
|
-
_EarnController_supportedPooledStakingChains.set(this, void 0);
|
150
|
-
// temporary array of supported chains
|
151
|
-
// TODO: remove this once we have a more permanent solution
|
152
|
-
// from sdk or api to get lending and pooled staking chains
|
153
|
-
__classPrivateFieldSet(this, _EarnController_supportedPooledStakingChains, [1, 560048], "f");
|
154
|
-
__classPrivateFieldSet(this, _EarnController_addTransactionFn, addTransactionFn, "f");
|
155
|
-
__classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_initializeSDK).call(this).catch(console.error);
|
114
|
+
_EarnController_stakingApiService.set(this, new stake_sdk_1.StakingApiService());
|
115
|
+
__classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_initializeSDK).call(this);
|
156
116
|
this.refreshPooledStakingData().catch(console.error);
|
157
|
-
this.refreshLendingData().catch(console.error);
|
158
117
|
const { selectedNetworkClientId } = this.messagingSystem.call('NetworkController:getState');
|
159
118
|
__classPrivateFieldSet(this, _EarnController_selectedNetworkClientId, selectedNetworkClientId, "f");
|
160
119
|
// Listen for network changes
|
161
120
|
this.messagingSystem.subscribe('NetworkController:stateChange', (networkControllerState) => {
|
162
121
|
if (networkControllerState.selectedNetworkClientId !==
|
163
122
|
__classPrivateFieldGet(this, _EarnController_selectedNetworkClientId, "f")) {
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
this.refreshPooledStakingVaultDailyApys(chainId).catch(console.error);
|
170
|
-
this.refreshPooledStakingVaultApyAverages(chainId).catch(console.error);
|
171
|
-
this.refreshPooledStakes({ chainId }).catch(console.error);
|
172
|
-
}
|
173
|
-
// refresh lending data for all chains
|
174
|
-
this.refreshLendingMarkets().catch(console.error);
|
175
|
-
this.refreshLendingPositions().catch(console.error);
|
123
|
+
__classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_initializeSDK).call(this, networkControllerState.selectedNetworkClientId);
|
124
|
+
this.refreshPooledStakingVaultMetadata().catch(console.error);
|
125
|
+
this.refreshPooledStakingVaultDailyApys().catch(console.error);
|
126
|
+
this.refreshPooledStakingVaultApyAverages().catch(console.error);
|
127
|
+
this.refreshPooledStakes().catch(console.error);
|
176
128
|
}
|
177
129
|
__classPrivateFieldSet(this, _EarnController_selectedNetworkClientId, networkControllerState.selectedNetworkClientId, "f");
|
178
130
|
});
|
@@ -184,11 +136,8 @@ class EarnController extends base_controller_1.BaseController {
|
|
184
136
|
* Until this has been fixed, we rely on the event payload for the latest account instead of #getCurrentAccount().
|
185
137
|
* Issue: https://github.com/MetaMask/accounts-planning/issues/887
|
186
138
|
*/
|
187
|
-
// TODO: temp solution, this will refresh lending eligibility also
|
188
|
-
// we could have a more general check, as what is happening is a compliance address check
|
189
139
|
this.refreshStakingEligibility({ address }).catch(console.error);
|
190
140
|
this.refreshPooledStakes({ address }).catch(console.error);
|
191
|
-
this.refreshLendingPositions({ address }).catch(console.error);
|
192
141
|
});
|
193
142
|
// Listen for confirmed staking transactions
|
194
143
|
this.messagingSystem.subscribe('TransactionController:transactionConfirmed', (transactionMeta) => {
|
@@ -200,15 +149,10 @@ class EarnController extends base_controller_1.BaseController {
|
|
200
149
|
const { type, originalType } = transactionMeta;
|
201
150
|
const isStakingTransaction = stakingTransactionTypes.has(type) ||
|
202
151
|
stakingTransactionTypes.has(originalType);
|
203
|
-
const isLendingTransaction = lendingTransactionTypes.has(type) ||
|
204
|
-
lendingTransactionTypes.has(originalType);
|
205
|
-
const sender = transactionMeta.txParams.from;
|
206
152
|
if (isStakingTransaction) {
|
153
|
+
const sender = transactionMeta.txParams.from;
|
207
154
|
this.refreshPooledStakes({ resetCache: true, address: sender }).catch(console.error);
|
208
155
|
}
|
209
|
-
if (isLendingTransaction) {
|
210
|
-
this.refreshLendingPositions({ address: sender }).catch(console.error);
|
211
|
-
}
|
212
156
|
});
|
213
157
|
}
|
214
158
|
/**
|
@@ -219,24 +163,18 @@ class EarnController extends base_controller_1.BaseController {
|
|
219
163
|
* @param options - Optional arguments
|
220
164
|
* @param [options.resetCache] - Control whether the BE cache should be invalidated (optional).
|
221
165
|
* @param [options.address] - The address to refresh pooled stakes for (optional).
|
222
|
-
* @param [options.chainId] - The chain id to refresh pooled stakes for (optional).
|
223
166
|
* @returns A promise that resolves when the stakes data has been updated
|
224
167
|
*/
|
225
|
-
async refreshPooledStakes({ resetCache = false, address,
|
168
|
+
async refreshPooledStakes({ resetCache = false, address, } = {}) {
|
226
169
|
const addressToUse = address ?? __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentAccount).call(this)?.address;
|
227
170
|
if (!addressToUse) {
|
228
171
|
return;
|
229
172
|
}
|
230
|
-
const
|
231
|
-
const { accounts, exchangeRate } = await __classPrivateFieldGet(this,
|
173
|
+
const chainId = __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentChainId).call(this);
|
174
|
+
const { accounts, exchangeRate } = await __classPrivateFieldGet(this, _EarnController_stakingApiService, "f").getPooledStakes([addressToUse], chainId, resetCache);
|
232
175
|
this.update((state) => {
|
233
|
-
|
234
|
-
|
235
|
-
state.pooled_staking[chainIdToUse] = {
|
236
|
-
...chainState,
|
237
|
-
pooledStakes: accounts[0],
|
238
|
-
exchangeRate,
|
239
|
-
};
|
176
|
+
state.pooled_staking.pooledStakes = accounts[0];
|
177
|
+
state.pooled_staking.exchangeRate = exchangeRate;
|
240
178
|
});
|
241
179
|
}
|
242
180
|
/**
|
@@ -252,12 +190,11 @@ class EarnController extends base_controller_1.BaseController {
|
|
252
190
|
if (!addressToCheck) {
|
253
191
|
return;
|
254
192
|
}
|
255
|
-
const { eligible: isEligible } = await __classPrivateFieldGet(this,
|
193
|
+
const { eligible: isEligible } = await __classPrivateFieldGet(this, _EarnController_stakingApiService, "f").getPooledStakingEligibility([
|
256
194
|
addressToCheck,
|
257
195
|
]);
|
258
196
|
this.update((state) => {
|
259
197
|
state.pooled_staking.isEligible = isEligible;
|
260
|
-
state.lending.isEligible = isEligible;
|
261
198
|
});
|
262
199
|
}
|
263
200
|
/**
|
@@ -265,59 +202,41 @@ class EarnController extends base_controller_1.BaseController {
|
|
265
202
|
* Updates the vault metadata in the controller state including APY, capacity,
|
266
203
|
* fee percentage, total assets, and vault address.
|
267
204
|
*
|
268
|
-
* @param chainId - The chain id to refresh pooled staking vault metadata for (optional).
|
269
205
|
* @returns A promise that resolves when the vault metadata has been updated
|
270
206
|
*/
|
271
|
-
async refreshPooledStakingVaultMetadata(
|
272
|
-
const
|
273
|
-
const vaultMetadata = await __classPrivateFieldGet(this,
|
207
|
+
async refreshPooledStakingVaultMetadata() {
|
208
|
+
const chainId = __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentChainId).call(this);
|
209
|
+
const vaultMetadata = await __classPrivateFieldGet(this, _EarnController_stakingApiService, "f").getVaultData(chainId);
|
274
210
|
this.update((state) => {
|
275
|
-
|
276
|
-
exports.DEFAULT_POOLED_STAKING_CHAIN_STATE;
|
277
|
-
state.pooled_staking[chainIdToUse] = {
|
278
|
-
...chainState,
|
279
|
-
vaultMetadata,
|
280
|
-
};
|
211
|
+
state.pooled_staking.vaultMetadata = vaultMetadata;
|
281
212
|
});
|
282
213
|
}
|
283
214
|
/**
|
284
215
|
* Refreshes pooled staking vault daily apys for the current chain.
|
285
216
|
* Updates the pooled staking vault daily apys controller state.
|
286
217
|
*
|
287
|
-
* @param chainId - The chain id to refresh pooled staking vault daily apys for (optional).
|
288
218
|
* @param days - The number of days to fetch pooled staking vault daily apys for (defaults to 365).
|
289
219
|
* @param order - The order in which to fetch pooled staking vault daily apys. Descending order fetches the latest N days (latest working backwards). Ascending order fetches the oldest N days (oldest working forwards) (defaults to 'desc').
|
290
220
|
* @returns A promise that resolves when the pooled staking vault daily apys have been updated.
|
291
221
|
*/
|
292
|
-
async refreshPooledStakingVaultDailyApys(
|
293
|
-
const
|
294
|
-
const vaultDailyApys = await __classPrivateFieldGet(this,
|
222
|
+
async refreshPooledStakingVaultDailyApys(days = 365, order = 'desc') {
|
223
|
+
const chainId = __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentChainId).call(this);
|
224
|
+
const vaultDailyApys = await __classPrivateFieldGet(this, _EarnController_stakingApiService, "f").getVaultDailyApys(chainId, days, order);
|
295
225
|
this.update((state) => {
|
296
|
-
|
297
|
-
exports.DEFAULT_POOLED_STAKING_CHAIN_STATE;
|
298
|
-
state.pooled_staking[chainIdToUse] = {
|
299
|
-
...chainState,
|
300
|
-
vaultDailyApys,
|
301
|
-
};
|
226
|
+
state.pooled_staking.vaultDailyApys = vaultDailyApys;
|
302
227
|
});
|
303
228
|
}
|
304
229
|
/**
|
305
230
|
* Refreshes pooled staking vault apy averages for the current chain.
|
306
231
|
* Updates the pooled staking vault apy averages controller state.
|
307
232
|
*
|
308
|
-
* @param chainId - The chain id to refresh pooled staking vault apy averages for (optional).
|
309
233
|
* @returns A promise that resolves when the pooled staking vault apy averages have been updated.
|
310
234
|
*/
|
311
|
-
async refreshPooledStakingVaultApyAverages(
|
312
|
-
const
|
313
|
-
const vaultApyAverages = await __classPrivateFieldGet(this,
|
235
|
+
async refreshPooledStakingVaultApyAverages() {
|
236
|
+
const chainId = __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentChainId).call(this);
|
237
|
+
const vaultApyAverages = await __classPrivateFieldGet(this, _EarnController_stakingApiService, "f").getVaultApyAverages(chainId);
|
314
238
|
this.update((state) => {
|
315
|
-
|
316
|
-
exports.DEFAULT_POOLED_STAKING_CHAIN_STATE;
|
317
|
-
state.pooled_staking[chainIdToUse] = {
|
318
|
-
...chainState,
|
319
|
-
vaultApyAverages,
|
320
|
-
};
|
239
|
+
state.pooled_staking.vaultApyAverages = vaultApyAverages;
|
321
240
|
});
|
322
241
|
}
|
323
242
|
/**
|
@@ -332,306 +251,39 @@ class EarnController extends base_controller_1.BaseController {
|
|
332
251
|
* @throws {Error} If any of the refresh operations fail, with concatenated error messages
|
333
252
|
*/
|
334
253
|
async refreshPooledStakingData({ resetCache, address, } = {}) {
|
335
|
-
const errors = [];
|
336
|
-
for (const chainId of __classPrivateFieldGet(this, _EarnController_supportedPooledStakingChains, "f")) {
|
337
|
-
await Promise.all([
|
338
|
-
this.refreshPooledStakes({ resetCache, address, chainId }).catch((error) => {
|
339
|
-
errors.push(error);
|
340
|
-
}),
|
341
|
-
this.refreshStakingEligibility({ address }).catch((error) => {
|
342
|
-
errors.push(error);
|
343
|
-
}),
|
344
|
-
this.refreshPooledStakingVaultMetadata(chainId).catch((error) => {
|
345
|
-
errors.push(error);
|
346
|
-
}),
|
347
|
-
this.refreshPooledStakingVaultDailyApys(chainId).catch((error) => {
|
348
|
-
errors.push(error);
|
349
|
-
}),
|
350
|
-
this.refreshPooledStakingVaultApyAverages(chainId).catch((error) => {
|
351
|
-
errors.push(error);
|
352
|
-
}),
|
353
|
-
]);
|
354
|
-
}
|
355
|
-
if (errors.length > 0) {
|
356
|
-
throw new Error(`Failed to refresh some staking data: ${errors
|
357
|
-
.map((e) => e.message)
|
358
|
-
.join(', ')}`);
|
359
|
-
}
|
360
|
-
}
|
361
|
-
/**
|
362
|
-
* Refreshes the lending markets data for all chains.
|
363
|
-
* Updates the lending markets in the controller state.
|
364
|
-
*
|
365
|
-
* @returns A promise that resolves when the lending markets have been updated
|
366
|
-
*/
|
367
|
-
async refreshLendingMarkets() {
|
368
|
-
const markets = await __classPrivateFieldGet(this, _EarnController_earnApiService, "f").lending.getMarkets();
|
369
|
-
this.update((state) => {
|
370
|
-
state.lending.markets = markets;
|
371
|
-
});
|
372
|
-
}
|
373
|
-
/**
|
374
|
-
* Refreshes the lending positions for the current account.
|
375
|
-
* Updates the lending positions in the controller state.
|
376
|
-
*
|
377
|
-
* @param options - Optional arguments
|
378
|
-
* @param [options.address] - The address to refresh lending positions for (optional).
|
379
|
-
* @returns A promise that resolves when the lending positions have been updated
|
380
|
-
*/
|
381
|
-
async refreshLendingPositions({ address, } = {}) {
|
382
|
-
const addressToUse = address ?? __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentAccount).call(this)?.address;
|
383
|
-
if (!addressToUse) {
|
384
|
-
return;
|
385
|
-
}
|
386
|
-
// linter complaining about this not being a promise, but it is
|
387
|
-
// TODO: figure out why this is not seen as a promise
|
388
|
-
const positions = await Promise.resolve(__classPrivateFieldGet(this, _EarnController_earnApiService, "f").lending.getPositions(addressToUse));
|
389
|
-
this.update((state) => {
|
390
|
-
state.lending.positions = positions.map((position) => ({
|
391
|
-
...position,
|
392
|
-
marketId: position.market.id,
|
393
|
-
marketAddress: position.market.address,
|
394
|
-
protocol: position.market.protocol,
|
395
|
-
}));
|
396
|
-
});
|
397
|
-
}
|
398
|
-
/**
|
399
|
-
* Refreshes the lending eligibility status for the current account.
|
400
|
-
* Updates the eligibility status in the controller state based on the location and address blocklist for compliance.
|
401
|
-
*
|
402
|
-
* @param options - Optional arguments
|
403
|
-
* @param [options.address] - The address to refresh lending eligibility for (optional).
|
404
|
-
* @returns A promise that resolves when the eligibility status has been updated
|
405
|
-
*/
|
406
|
-
async refreshLendingEligibility({ address, } = {}) {
|
407
|
-
const addressToUse = address ?? __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentAccount).call(this)?.address;
|
408
|
-
// TODO: this is a temporary solution to refresh lending eligibility as
|
409
|
-
// the eligibility check is not yet implemented for lending
|
410
|
-
// this check will check the address against the same blocklist as the
|
411
|
-
// staking eligibility check
|
412
|
-
if (!addressToUse) {
|
413
|
-
return;
|
414
|
-
}
|
415
|
-
const { eligible: isEligible } = await __classPrivateFieldGet(this, _EarnController_earnApiService, "f").pooledStaking.getPooledStakingEligibility([
|
416
|
-
addressToUse,
|
417
|
-
]);
|
418
|
-
this.update((state) => {
|
419
|
-
state.lending.isEligible = isEligible;
|
420
|
-
state.pooled_staking.isEligible = isEligible;
|
421
|
-
});
|
422
|
-
}
|
423
|
-
/**
|
424
|
-
* Refreshes all lending related data including markets, positions, and eligibility.
|
425
|
-
* This method allows partial success, meaning some data may update while other requests fail.
|
426
|
-
* All errors are collected and thrown as a single error message.
|
427
|
-
*
|
428
|
-
* @returns A promise that resolves when all possible data has been updated
|
429
|
-
* @throws {Error} If any of the refresh operations fail, with concatenated error messages
|
430
|
-
*/
|
431
|
-
async refreshLendingData() {
|
432
254
|
const errors = [];
|
433
255
|
await Promise.all([
|
434
|
-
this.
|
256
|
+
this.refreshPooledStakes({ resetCache, address }).catch((error) => {
|
435
257
|
errors.push(error);
|
436
258
|
}),
|
437
|
-
this.
|
259
|
+
this.refreshStakingEligibility({ address }).catch((error) => {
|
438
260
|
errors.push(error);
|
439
261
|
}),
|
440
|
-
this.
|
262
|
+
this.refreshPooledStakingVaultMetadata().catch((error) => {
|
263
|
+
errors.push(error);
|
264
|
+
}),
|
265
|
+
this.refreshPooledStakingVaultDailyApys().catch((error) => {
|
266
|
+
errors.push(error);
|
267
|
+
}),
|
268
|
+
this.refreshPooledStakingVaultApyAverages().catch((error) => {
|
441
269
|
errors.push(error);
|
442
270
|
}),
|
443
271
|
]);
|
444
272
|
if (errors.length > 0) {
|
445
|
-
throw new Error(`Failed to refresh some
|
273
|
+
throw new Error(`Failed to refresh some staking data: ${errors
|
446
274
|
.map((e) => e.message)
|
447
275
|
.join(', ')}`);
|
448
276
|
}
|
449
277
|
}
|
450
|
-
/**
|
451
|
-
* Gets the lending position history for the current account.
|
452
|
-
*
|
453
|
-
* @param options - Optional arguments
|
454
|
-
* @param [options.address] - The address to get lending position history for (optional).
|
455
|
-
* @param [options.chainId] - The chain id to get lending position history for (optional).
|
456
|
-
* @param [options.positionId] - The position id to get lending position history for.
|
457
|
-
* @param [options.marketId] - The market id to get lending position history for.
|
458
|
-
* @param [options.marketAddress] - The market address to get lending position history for.
|
459
|
-
* @param [options.protocol] - The protocol to get lending position history for.
|
460
|
-
* @param [options.days] - The number of days to get lending position history for (optional).
|
461
|
-
* @returns A promise that resolves when the lending position history has been updated
|
462
|
-
*/
|
463
|
-
getLendingPositionHistory({ address, chainId, positionId, marketId, marketAddress, protocol, days = 730, }) {
|
464
|
-
const addressToUse = address ?? __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentAccount).call(this)?.address;
|
465
|
-
const chainIdToUse = chainId ?? __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentChainId).call(this);
|
466
|
-
if (!addressToUse) {
|
467
|
-
return [];
|
468
|
-
}
|
469
|
-
return __classPrivateFieldGet(this, _EarnController_earnApiService, "f").lending.getPositionHistory(addressToUse, chainIdToUse, protocol, marketId, marketAddress, positionId, days);
|
470
|
-
}
|
471
|
-
/**
|
472
|
-
* Gets the lending market daily apys and averages for the current chain.
|
473
|
-
*
|
474
|
-
* @param options - Optional arguments
|
475
|
-
* @param [options.chainId] - The chain id to get lending market daily apys and averages for (optional).
|
476
|
-
* @param [options.protocol] - The protocol to get lending market daily apys and averages for.
|
477
|
-
* @param [options.marketId] - The market id to get lending market daily apys and averages for.
|
478
|
-
* @param [options.days] - The number of days to get lending market daily apys and averages for (optional).
|
479
|
-
* @returns A promise that resolves when the lending market daily apys and averages have been updated
|
480
|
-
*/
|
481
|
-
getLendingMarketDailyApysAndAverages({ chainId, protocol, marketId, days = 365, }) {
|
482
|
-
const chainIdToUse = chainId ?? __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentChainId).call(this);
|
483
|
-
return __classPrivateFieldGet(this, _EarnController_earnApiService, "f").lending.getHistoricMarketApys(chainIdToUse, protocol, marketId, days);
|
484
|
-
}
|
485
|
-
/**
|
486
|
-
* Executes a lending deposit transaction.
|
487
|
-
*
|
488
|
-
* @param options - The options for the lending deposit transaction.
|
489
|
-
* @param options.amount - The amount to deposit.
|
490
|
-
* @param options.protocol - The protocol of the lending market.
|
491
|
-
* @param options.underlyingTokenAddress - The address of the underlying token.
|
492
|
-
* @param options.gasOptions - The gas options for the transaction.
|
493
|
-
* @param options.gasOptions.gasLimit - The gas limit for the transaction.
|
494
|
-
* @param options.gasOptions.gasBufferPct - The gas buffer percentage for the transaction.
|
495
|
-
* @param options.txOptions - The transaction options for the transaction.
|
496
|
-
* @returns A promise that resolves to the transaction hash.
|
497
|
-
*/
|
498
|
-
async executeLendingDeposit({ amount, protocol, underlyingTokenAddress, gasOptions, txOptions, }) {
|
499
|
-
const address = __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentAccount).call(this)?.address;
|
500
|
-
const transactionData = await __classPrivateFieldGet(this, _EarnController_earnSDK, "f")?.contracts?.lending?.[protocol]?.[underlyingTokenAddress]?.encodeDepositTransactionData(amount, address, gasOptions);
|
501
|
-
if (!transactionData) {
|
502
|
-
throw new Error('Transaction data not found');
|
503
|
-
}
|
504
|
-
if (!__classPrivateFieldGet(this, _EarnController_selectedNetworkClientId, "f")) {
|
505
|
-
throw new Error('Selected network client id not found');
|
506
|
-
}
|
507
|
-
const txHash = await __classPrivateFieldGet(this, _EarnController_addTransactionFn, "f").call(this, {
|
508
|
-
...transactionData,
|
509
|
-
value: transactionData.value.toString(),
|
510
|
-
chainId: (0, controller_utils_1.toHex)(__classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentChainId).call(this)),
|
511
|
-
gasLimit: String(transactionData.gasLimit),
|
512
|
-
}, {
|
513
|
-
...txOptions,
|
514
|
-
networkClientId: __classPrivateFieldGet(this, _EarnController_selectedNetworkClientId, "f"),
|
515
|
-
});
|
516
|
-
return txHash;
|
517
|
-
}
|
518
|
-
/**
|
519
|
-
* Executes a lending withdraw transaction.
|
520
|
-
*
|
521
|
-
* @param options - The options for the lending withdraw transaction.
|
522
|
-
* @param options.amount - The amount to withdraw.
|
523
|
-
* @param options.protocol - The protocol of the lending market.
|
524
|
-
* @param options.underlyingTokenAddress - The address of the underlying token.
|
525
|
-
* @param options.gasOptions - The gas options for the transaction.
|
526
|
-
* @param options.gasOptions.gasLimit - The gas limit for the transaction.
|
527
|
-
* @param options.gasOptions.gasBufferPct - The gas buffer percentage for the transaction.
|
528
|
-
* @param options.txOptions - The transaction options for the transaction.
|
529
|
-
* @returns A promise that resolves to the transaction hash.
|
530
|
-
*/
|
531
|
-
async executeLendingWithdraw({ amount, protocol, underlyingTokenAddress, gasOptions, txOptions, }) {
|
532
|
-
const address = __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentAccount).call(this)?.address;
|
533
|
-
const transactionData = await __classPrivateFieldGet(this, _EarnController_earnSDK, "f")?.contracts?.lending?.[protocol]?.[underlyingTokenAddress]?.encodeWithdrawTransactionData(amount, address, gasOptions);
|
534
|
-
if (!transactionData) {
|
535
|
-
throw new Error('Transaction data not found');
|
536
|
-
}
|
537
|
-
if (!__classPrivateFieldGet(this, _EarnController_selectedNetworkClientId, "f")) {
|
538
|
-
throw new Error('Selected network client id not found');
|
539
|
-
}
|
540
|
-
const txHash = await __classPrivateFieldGet(this, _EarnController_addTransactionFn, "f").call(this, {
|
541
|
-
...transactionData,
|
542
|
-
value: transactionData.value.toString(),
|
543
|
-
chainId: (0, controller_utils_1.toHex)(__classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentChainId).call(this)),
|
544
|
-
gasLimit: String(transactionData.gasLimit),
|
545
|
-
}, {
|
546
|
-
...txOptions,
|
547
|
-
networkClientId: __classPrivateFieldGet(this, _EarnController_selectedNetworkClientId, "f"),
|
548
|
-
});
|
549
|
-
return txHash;
|
550
|
-
}
|
551
|
-
/**
|
552
|
-
* Executes a lending token approve transaction.
|
553
|
-
*
|
554
|
-
* @param options - The options for the lending token approve transaction.
|
555
|
-
* @param options.amount - The amount to approve.
|
556
|
-
* @param options.protocol - The protocol of the lending market.
|
557
|
-
* @param options.underlyingTokenAddress - The address of the underlying token.
|
558
|
-
* @param options.gasOptions - The gas options for the transaction.
|
559
|
-
* @param options.gasOptions.gasLimit - The gas limit for the transaction.
|
560
|
-
* @param options.gasOptions.gasBufferPct - The gas buffer percentage for the transaction.
|
561
|
-
* @param options.txOptions - The transaction options for the transaction.
|
562
|
-
* @returns A promise that resolves to the transaction hash.
|
563
|
-
*/
|
564
|
-
async executeLendingTokenApprove({ protocol, amount, underlyingTokenAddress, gasOptions, txOptions, }) {
|
565
|
-
const address = __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentAccount).call(this)?.address;
|
566
|
-
const transactionData = await __classPrivateFieldGet(this, _EarnController_earnSDK, "f")?.contracts?.lending?.[protocol]?.[underlyingTokenAddress]?.encodeUnderlyingTokenApproveTransactionData(amount, address, gasOptions);
|
567
|
-
if (!transactionData) {
|
568
|
-
throw new Error('Transaction data not found');
|
569
|
-
}
|
570
|
-
if (!__classPrivateFieldGet(this, _EarnController_selectedNetworkClientId, "f")) {
|
571
|
-
throw new Error('Selected network client id not found');
|
572
|
-
}
|
573
|
-
const txHash = await __classPrivateFieldGet(this, _EarnController_addTransactionFn, "f").call(this, {
|
574
|
-
...transactionData,
|
575
|
-
value: transactionData.value.toString(),
|
576
|
-
chainId: (0, controller_utils_1.toHex)(__classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentChainId).call(this)),
|
577
|
-
gasLimit: String(transactionData.gasLimit),
|
578
|
-
}, {
|
579
|
-
...txOptions,
|
580
|
-
networkClientId: __classPrivateFieldGet(this, _EarnController_selectedNetworkClientId, "f"),
|
581
|
-
});
|
582
|
-
return txHash;
|
583
|
-
}
|
584
|
-
/**
|
585
|
-
* Gets the allowance for a lending token.
|
586
|
-
*
|
587
|
-
* @param protocol - The protocol of the lending market.
|
588
|
-
* @param underlyingTokenAddress - The address of the underlying token.
|
589
|
-
* @returns A promise that resolves to the allowance.
|
590
|
-
*/
|
591
|
-
async getLendingTokenAllowance(protocol, underlyingTokenAddress) {
|
592
|
-
const address = __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentAccount).call(this)?.address;
|
593
|
-
const allowance = await __classPrivateFieldGet(this, _EarnController_earnSDK, "f")?.contracts?.lending?.[protocol]?.[underlyingTokenAddress]?.underlyingTokenAllowance(address);
|
594
|
-
return allowance;
|
595
|
-
}
|
596
|
-
/**
|
597
|
-
* Gets the maximum withdraw amount for a lending token's output token or shares if no output token.
|
598
|
-
*
|
599
|
-
* @param protocol - The protocol of the lending market.
|
600
|
-
* @param underlyingTokenAddress - The address of the underlying token.
|
601
|
-
* @returns A promise that resolves to the maximum withdraw amount.
|
602
|
-
*/
|
603
|
-
async getLendingTokenMaxWithdraw(protocol, underlyingTokenAddress) {
|
604
|
-
const address = __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentAccount).call(this)?.address;
|
605
|
-
const maxWithdraw = await __classPrivateFieldGet(this, _EarnController_earnSDK, "f")?.contracts?.lending?.[protocol]?.[underlyingTokenAddress]?.maxWithdraw(address);
|
606
|
-
return maxWithdraw;
|
607
|
-
}
|
608
|
-
/**
|
609
|
-
* Gets the maximum deposit amount for a lending token.
|
610
|
-
*
|
611
|
-
* @param protocol - The protocol of the lending market.
|
612
|
-
* @param underlyingTokenAddress - The address of the underlying token.
|
613
|
-
* @returns A promise that resolves to the maximum deposit amount.
|
614
|
-
*/
|
615
|
-
async getLendingTokenMaxDeposit(protocol, underlyingTokenAddress) {
|
616
|
-
const address = __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentAccount).call(this)?.address;
|
617
|
-
const maxDeposit = await __classPrivateFieldGet(this, _EarnController_earnSDK, "f")?.contracts?.lending?.[protocol]?.[underlyingTokenAddress]?.maxDeposit(address);
|
618
|
-
return maxDeposit;
|
619
|
-
}
|
620
278
|
}
|
621
279
|
exports.EarnController = EarnController;
|
622
|
-
|
623
|
-
/**
|
624
|
-
* Initializes the Earn SDK.
|
625
|
-
*
|
626
|
-
* @param networkClientId - The network client id to initialize the Earn SDK for (optional).
|
627
|
-
*/
|
628
|
-
async function _EarnController_initializeSDK(networkClientId) {
|
280
|
+
_EarnController_stakeSDK = new WeakMap(), _EarnController_selectedNetworkClientId = new WeakMap(), _EarnController_stakingApiService = new WeakMap(), _EarnController_instances = new WeakSet(), _EarnController_initializeSDK = function _EarnController_initializeSDK(networkClientId) {
|
629
281
|
const { selectedNetworkClientId } = networkClientId
|
630
282
|
? { selectedNetworkClientId: networkClientId }
|
631
283
|
: this.messagingSystem.call('NetworkController:getState');
|
632
284
|
const networkClient = this.messagingSystem.call('NetworkController:getNetworkClientById', selectedNetworkClientId);
|
633
285
|
if (!networkClient?.provider) {
|
634
|
-
__classPrivateFieldSet(this,
|
286
|
+
__classPrivateFieldSet(this, _EarnController_stakeSDK, null, "f");
|
635
287
|
return;
|
636
288
|
}
|
637
289
|
const provider = new providers_1.Web3Provider(networkClient.provider);
|
@@ -641,23 +293,31 @@ async function _EarnController_initializeSDK(networkClientId) {
|
|
641
293
|
chainId: (0, controller_utils_1.convertHexToDecimal)(chainId),
|
642
294
|
};
|
643
295
|
try {
|
644
|
-
__classPrivateFieldSet(this,
|
296
|
+
__classPrivateFieldSet(this, _EarnController_stakeSDK, stake_sdk_1.StakeSdk.create(config), "f");
|
297
|
+
__classPrivateFieldGet(this, _EarnController_stakeSDK, "f").pooledStakingContract.connectSignerOrProvider(provider);
|
645
298
|
}
|
646
299
|
catch (error) {
|
647
|
-
__classPrivateFieldSet(this,
|
300
|
+
__classPrivateFieldSet(this, _EarnController_stakeSDK, null, "f");
|
648
301
|
// Only log unexpected errors, not unsupported chain errors
|
649
302
|
if (!(error instanceof Error &&
|
650
303
|
error.message.includes('Unsupported chainId'))) {
|
651
|
-
console.error('
|
304
|
+
console.error('Stake SDK initialization failed:', error);
|
652
305
|
}
|
653
306
|
}
|
654
307
|
}, _EarnController_getCurrentAccount = function _EarnController_getCurrentAccount() {
|
655
308
|
return this.messagingSystem.call('AccountsController:getSelectedAccount');
|
656
|
-
}, _EarnController_getCurrentChainId = function _EarnController_getCurrentChainId(
|
657
|
-
const
|
658
|
-
|
659
|
-
|
660
|
-
const {
|
661
|
-
|
309
|
+
}, _EarnController_getCurrentChainId = function _EarnController_getCurrentChainId() {
|
310
|
+
// const { selectedNetworkClientId } = this.messagingSystem.call(
|
311
|
+
// 'NetworkController:getState',
|
312
|
+
// );
|
313
|
+
// const {
|
314
|
+
// configuration: { chainId },
|
315
|
+
// } = this.messagingSystem.call(
|
316
|
+
// 'NetworkController:getNetworkClientById',
|
317
|
+
// selectedNetworkClientId,
|
318
|
+
// );
|
319
|
+
// return convertHexToDecimal(chainId);
|
320
|
+
// TEMP: Until we update our data-fetching and storage solution to not depend on single selected network.
|
321
|
+
return stake_sdk_1.ChainId.ETHEREUM;
|
662
322
|
};
|
663
323
|
//# sourceMappingURL=EarnController.cjs.map
|