@metamask-previews/earn-controller 0.14.0-preview-3c144344 → 0.14.0-preview-2723071f
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 -30
- package/dist/EarnController.cjs +91 -438
- package/dist/EarnController.cjs.map +1 -1
- package/dist/EarnController.d.cts +25 -224
- package/dist/EarnController.d.cts.map +1 -1
- package/dist/EarnController.d.mts +25 -224
- package/dist/EarnController.d.mts.map +1 -1
- package/dist/EarnController.mjs +92 -439
- package/dist/EarnController.mjs.map +1 -1
- package/dist/index.cjs +1 -23
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -3
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +1 -3
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +0 -2
- 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 -106
- package/dist/selectors.cjs.map +0 -1
- package/dist/selectors.d.cts +0 -1484
- package/dist/selectors.d.cts.map +0 -1
- package/dist/selectors.d.mts +0 -1484
- package/dist/selectors.d.mts.map +0 -1
- package/dist/selectors.mjs +0 -97
- 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_env, _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,40 +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
|
-
_EarnController_env.set(this, void 0);
|
151
|
-
__classPrivateFieldSet(this, _EarnController_env, env, "f");
|
152
|
-
__classPrivateFieldSet(this, _EarnController_earnApiService, new stake_sdk_1.EarnApiService(__classPrivateFieldGet(this, _EarnController_env, "f")), "f");
|
153
|
-
// temporary array of supported chains
|
154
|
-
// TODO: remove this once we export a supported chains list from the sdk
|
155
|
-
// from sdk or api to get lending and pooled staking chains
|
156
|
-
__classPrivateFieldSet(this, _EarnController_supportedPooledStakingChains, [1, 560048], "f");
|
157
|
-
__classPrivateFieldSet(this, _EarnController_addTransactionFn, addTransactionFn, "f");
|
158
|
-
__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);
|
159
116
|
this.refreshPooledStakingData().catch(console.error);
|
160
|
-
this.refreshLendingData().catch(console.error);
|
161
117
|
const { selectedNetworkClientId } = this.messagingSystem.call('NetworkController:getState');
|
162
118
|
__classPrivateFieldSet(this, _EarnController_selectedNetworkClientId, selectedNetworkClientId, "f");
|
163
119
|
// Listen for network changes
|
164
120
|
this.messagingSystem.subscribe('NetworkController:stateChange', (networkControllerState) => {
|
165
121
|
if (networkControllerState.selectedNetworkClientId !==
|
166
122
|
__classPrivateFieldGet(this, _EarnController_selectedNetworkClientId, "f")) {
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
this.refreshPooledStakingVaultDailyApys(chainId).catch(console.error);
|
173
|
-
this.refreshPooledStakingVaultApyAverages(chainId).catch(console.error);
|
174
|
-
this.refreshPooledStakes({ chainId }).catch(console.error);
|
175
|
-
}
|
176
|
-
// refresh lending data for all chains
|
177
|
-
this.refreshLendingMarkets().catch(console.error);
|
178
|
-
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);
|
179
128
|
}
|
180
129
|
__classPrivateFieldSet(this, _EarnController_selectedNetworkClientId, networkControllerState.selectedNetworkClientId, "f");
|
181
130
|
});
|
@@ -187,11 +136,8 @@ class EarnController extends base_controller_1.BaseController {
|
|
187
136
|
* Until this has been fixed, we rely on the event payload for the latest account instead of #getCurrentAccount().
|
188
137
|
* Issue: https://github.com/MetaMask/accounts-planning/issues/887
|
189
138
|
*/
|
190
|
-
// TODO: temp solution, this will refresh lending eligibility also
|
191
|
-
// we could have a more general check, as what is happening is a compliance address check
|
192
139
|
this.refreshStakingEligibility({ address }).catch(console.error);
|
193
140
|
this.refreshPooledStakes({ address }).catch(console.error);
|
194
|
-
this.refreshLendingPositions({ address }).catch(console.error);
|
195
141
|
});
|
196
142
|
// Listen for confirmed staking transactions
|
197
143
|
this.messagingSystem.subscribe('TransactionController:transactionConfirmed', (transactionMeta) => {
|
@@ -203,15 +149,10 @@ class EarnController extends base_controller_1.BaseController {
|
|
203
149
|
const { type, originalType } = transactionMeta;
|
204
150
|
const isStakingTransaction = stakingTransactionTypes.has(type) ||
|
205
151
|
stakingTransactionTypes.has(originalType);
|
206
|
-
const isLendingTransaction = lendingTransactionTypes.has(type) ||
|
207
|
-
lendingTransactionTypes.has(originalType);
|
208
|
-
const sender = transactionMeta.txParams.from;
|
209
152
|
if (isStakingTransaction) {
|
153
|
+
const sender = transactionMeta.txParams.from;
|
210
154
|
this.refreshPooledStakes({ resetCache: true, address: sender }).catch(console.error);
|
211
155
|
}
|
212
|
-
if (isLendingTransaction) {
|
213
|
-
this.refreshLendingPositions({ address: sender }).catch(console.error);
|
214
|
-
}
|
215
156
|
});
|
216
157
|
}
|
217
158
|
/**
|
@@ -222,24 +163,18 @@ class EarnController extends base_controller_1.BaseController {
|
|
222
163
|
* @param options - Optional arguments
|
223
164
|
* @param [options.resetCache] - Control whether the BE cache should be invalidated (optional).
|
224
165
|
* @param [options.address] - The address to refresh pooled stakes for (optional).
|
225
|
-
* @param [options.chainId] - The chain id to refresh pooled stakes for (optional).
|
226
166
|
* @returns A promise that resolves when the stakes data has been updated
|
227
167
|
*/
|
228
|
-
async refreshPooledStakes({ resetCache = false, address,
|
168
|
+
async refreshPooledStakes({ resetCache = false, address, } = {}) {
|
229
169
|
const addressToUse = address ?? __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentAccount).call(this)?.address;
|
230
170
|
if (!addressToUse) {
|
231
171
|
return;
|
232
172
|
}
|
233
|
-
const
|
234
|
-
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);
|
235
175
|
this.update((state) => {
|
236
|
-
|
237
|
-
|
238
|
-
state.pooled_staking[chainIdToUse] = {
|
239
|
-
...chainState,
|
240
|
-
pooledStakes: accounts[0],
|
241
|
-
exchangeRate,
|
242
|
-
};
|
176
|
+
state.pooled_staking.pooledStakes = accounts[0];
|
177
|
+
state.pooled_staking.exchangeRate = exchangeRate;
|
243
178
|
});
|
244
179
|
}
|
245
180
|
/**
|
@@ -255,12 +190,11 @@ class EarnController extends base_controller_1.BaseController {
|
|
255
190
|
if (!addressToCheck) {
|
256
191
|
return;
|
257
192
|
}
|
258
|
-
const { eligible: isEligible } = await __classPrivateFieldGet(this,
|
193
|
+
const { eligible: isEligible } = await __classPrivateFieldGet(this, _EarnController_stakingApiService, "f").getPooledStakingEligibility([
|
259
194
|
addressToCheck,
|
260
195
|
]);
|
261
196
|
this.update((state) => {
|
262
197
|
state.pooled_staking.isEligible = isEligible;
|
263
|
-
state.lending.isEligible = isEligible;
|
264
198
|
});
|
265
199
|
}
|
266
200
|
/**
|
@@ -268,59 +202,41 @@ class EarnController extends base_controller_1.BaseController {
|
|
268
202
|
* Updates the vault metadata in the controller state including APY, capacity,
|
269
203
|
* fee percentage, total assets, and vault address.
|
270
204
|
*
|
271
|
-
* @param chainId - The chain id to refresh pooled staking vault metadata for (optional).
|
272
205
|
* @returns A promise that resolves when the vault metadata has been updated
|
273
206
|
*/
|
274
|
-
async refreshPooledStakingVaultMetadata(
|
275
|
-
const
|
276
|
-
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);
|
277
210
|
this.update((state) => {
|
278
|
-
|
279
|
-
exports.DEFAULT_POOLED_STAKING_CHAIN_STATE;
|
280
|
-
state.pooled_staking[chainIdToUse] = {
|
281
|
-
...chainState,
|
282
|
-
vaultMetadata,
|
283
|
-
};
|
211
|
+
state.pooled_staking.vaultMetadata = vaultMetadata;
|
284
212
|
});
|
285
213
|
}
|
286
214
|
/**
|
287
215
|
* Refreshes pooled staking vault daily apys for the current chain.
|
288
216
|
* Updates the pooled staking vault daily apys controller state.
|
289
217
|
*
|
290
|
-
* @param chainId - The chain id to refresh pooled staking vault daily apys for (optional).
|
291
218
|
* @param days - The number of days to fetch pooled staking vault daily apys for (defaults to 365).
|
292
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').
|
293
220
|
* @returns A promise that resolves when the pooled staking vault daily apys have been updated.
|
294
221
|
*/
|
295
|
-
async refreshPooledStakingVaultDailyApys(
|
296
|
-
const
|
297
|
-
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);
|
298
225
|
this.update((state) => {
|
299
|
-
|
300
|
-
exports.DEFAULT_POOLED_STAKING_CHAIN_STATE;
|
301
|
-
state.pooled_staking[chainIdToUse] = {
|
302
|
-
...chainState,
|
303
|
-
vaultDailyApys,
|
304
|
-
};
|
226
|
+
state.pooled_staking.vaultDailyApys = vaultDailyApys;
|
305
227
|
});
|
306
228
|
}
|
307
229
|
/**
|
308
230
|
* Refreshes pooled staking vault apy averages for the current chain.
|
309
231
|
* Updates the pooled staking vault apy averages controller state.
|
310
232
|
*
|
311
|
-
* @param chainId - The chain id to refresh pooled staking vault apy averages for (optional).
|
312
233
|
* @returns A promise that resolves when the pooled staking vault apy averages have been updated.
|
313
234
|
*/
|
314
|
-
async refreshPooledStakingVaultApyAverages(
|
315
|
-
const
|
316
|
-
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);
|
317
238
|
this.update((state) => {
|
318
|
-
|
319
|
-
exports.DEFAULT_POOLED_STAKING_CHAIN_STATE;
|
320
|
-
state.pooled_staking[chainIdToUse] = {
|
321
|
-
...chainState,
|
322
|
-
vaultApyAverages,
|
323
|
-
};
|
239
|
+
state.pooled_staking.vaultApyAverages = vaultApyAverages;
|
324
240
|
});
|
325
241
|
}
|
326
242
|
/**
|
@@ -335,309 +251,39 @@ class EarnController extends base_controller_1.BaseController {
|
|
335
251
|
* @throws {Error} If any of the refresh operations fail, with concatenated error messages
|
336
252
|
*/
|
337
253
|
async refreshPooledStakingData({ resetCache, address, } = {}) {
|
338
|
-
const errors = [];
|
339
|
-
for (const chainId of __classPrivateFieldGet(this, _EarnController_supportedPooledStakingChains, "f")) {
|
340
|
-
await Promise.all([
|
341
|
-
this.refreshPooledStakes({ resetCache, address, chainId }).catch((error) => {
|
342
|
-
errors.push(error);
|
343
|
-
}),
|
344
|
-
this.refreshStakingEligibility({ address }).catch((error) => {
|
345
|
-
errors.push(error);
|
346
|
-
}),
|
347
|
-
this.refreshPooledStakingVaultMetadata(chainId).catch((error) => {
|
348
|
-
errors.push(error);
|
349
|
-
}),
|
350
|
-
this.refreshPooledStakingVaultDailyApys(chainId).catch((error) => {
|
351
|
-
errors.push(error);
|
352
|
-
}),
|
353
|
-
this.refreshPooledStakingVaultApyAverages(chainId).catch((error) => {
|
354
|
-
errors.push(error);
|
355
|
-
}),
|
356
|
-
]);
|
357
|
-
}
|
358
|
-
if (errors.length > 0) {
|
359
|
-
throw new Error(`Failed to refresh some staking data: ${errors
|
360
|
-
.map((e) => e.message)
|
361
|
-
.join(', ')}`);
|
362
|
-
}
|
363
|
-
}
|
364
|
-
/**
|
365
|
-
* Refreshes the lending markets data for all chains.
|
366
|
-
* Updates the lending markets in the controller state.
|
367
|
-
*
|
368
|
-
* @returns A promise that resolves when the lending markets have been updated
|
369
|
-
*/
|
370
|
-
async refreshLendingMarkets() {
|
371
|
-
const markets = await __classPrivateFieldGet(this, _EarnController_earnApiService, "f").lending.getMarkets();
|
372
|
-
this.update((state) => {
|
373
|
-
state.lending.markets = markets;
|
374
|
-
});
|
375
|
-
}
|
376
|
-
/**
|
377
|
-
* Refreshes the lending positions for the current account.
|
378
|
-
* Updates the lending positions in the controller state.
|
379
|
-
*
|
380
|
-
* @param options - Optional arguments
|
381
|
-
* @param [options.address] - The address to refresh lending positions for (optional).
|
382
|
-
* @returns A promise that resolves when the lending positions have been updated
|
383
|
-
*/
|
384
|
-
async refreshLendingPositions({ address, } = {}) {
|
385
|
-
const addressToUse = address ?? __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentAccount).call(this)?.address;
|
386
|
-
if (!addressToUse) {
|
387
|
-
return;
|
388
|
-
}
|
389
|
-
// linter complaining about this not being a promise, but it is
|
390
|
-
// TODO: figure out why this is not seen as a promise
|
391
|
-
const positions = await Promise.resolve(__classPrivateFieldGet(this, _EarnController_earnApiService, "f").lending.getPositions(addressToUse));
|
392
|
-
this.update((state) => {
|
393
|
-
state.lending.positions = positions.map((position) => ({
|
394
|
-
...position,
|
395
|
-
marketId: position.market.id,
|
396
|
-
marketAddress: position.market.address,
|
397
|
-
protocol: position.market.protocol,
|
398
|
-
}));
|
399
|
-
});
|
400
|
-
}
|
401
|
-
/**
|
402
|
-
* Refreshes the lending eligibility status for the current account.
|
403
|
-
* Updates the eligibility status in the controller state based on the location and address blocklist for compliance.
|
404
|
-
*
|
405
|
-
* @param options - Optional arguments
|
406
|
-
* @param [options.address] - The address to refresh lending eligibility for (optional).
|
407
|
-
* @returns A promise that resolves when the eligibility status has been updated
|
408
|
-
*/
|
409
|
-
async refreshLendingEligibility({ address, } = {}) {
|
410
|
-
const addressToUse = address ?? __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentAccount).call(this)?.address;
|
411
|
-
// TODO: this is a temporary solution to refresh lending eligibility as
|
412
|
-
// the eligibility check is not yet implemented for lending
|
413
|
-
// this check will check the address against the same blocklist as the
|
414
|
-
// staking eligibility check
|
415
|
-
if (!addressToUse) {
|
416
|
-
return;
|
417
|
-
}
|
418
|
-
const { eligible: isEligible } = await __classPrivateFieldGet(this, _EarnController_earnApiService, "f").pooledStaking.getPooledStakingEligibility([
|
419
|
-
addressToUse,
|
420
|
-
]);
|
421
|
-
this.update((state) => {
|
422
|
-
state.lending.isEligible = isEligible;
|
423
|
-
state.pooled_staking.isEligible = isEligible;
|
424
|
-
});
|
425
|
-
}
|
426
|
-
/**
|
427
|
-
* Refreshes all lending related data including markets, positions, and eligibility.
|
428
|
-
* This method allows partial success, meaning some data may update while other requests fail.
|
429
|
-
* All errors are collected and thrown as a single error message.
|
430
|
-
*
|
431
|
-
* @returns A promise that resolves when all possible data has been updated
|
432
|
-
* @throws {Error} If any of the refresh operations fail, with concatenated error messages
|
433
|
-
*/
|
434
|
-
async refreshLendingData() {
|
435
254
|
const errors = [];
|
436
255
|
await Promise.all([
|
437
|
-
this.
|
256
|
+
this.refreshPooledStakes({ resetCache, address }).catch((error) => {
|
438
257
|
errors.push(error);
|
439
258
|
}),
|
440
|
-
this.
|
259
|
+
this.refreshStakingEligibility({ address }).catch((error) => {
|
441
260
|
errors.push(error);
|
442
261
|
}),
|
443
|
-
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) => {
|
444
269
|
errors.push(error);
|
445
270
|
}),
|
446
271
|
]);
|
447
272
|
if (errors.length > 0) {
|
448
|
-
throw new Error(`Failed to refresh some
|
273
|
+
throw new Error(`Failed to refresh some staking data: ${errors
|
449
274
|
.map((e) => e.message)
|
450
275
|
.join(', ')}`);
|
451
276
|
}
|
452
277
|
}
|
453
|
-
/**
|
454
|
-
* Gets the lending position history for the current account.
|
455
|
-
*
|
456
|
-
* @param options - Optional arguments
|
457
|
-
* @param [options.address] - The address to get lending position history for (optional).
|
458
|
-
* @param [options.chainId] - The chain id to get lending position history for (optional).
|
459
|
-
* @param [options.positionId] - The position id to get lending position history for.
|
460
|
-
* @param [options.marketId] - The market id to get lending position history for.
|
461
|
-
* @param [options.marketAddress] - The market address to get lending position history for.
|
462
|
-
* @param [options.protocol] - The protocol to get lending position history for.
|
463
|
-
* @param [options.days] - The number of days to get lending position history for (optional).
|
464
|
-
* @returns A promise that resolves when the lending position history has been updated
|
465
|
-
*/
|
466
|
-
getLendingPositionHistory({ address, chainId, positionId, marketId, marketAddress, protocol, days = 730, }) {
|
467
|
-
const addressToUse = address ?? __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentAccount).call(this)?.address;
|
468
|
-
const chainIdToUse = chainId ?? __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentChainId).call(this);
|
469
|
-
if (!addressToUse || !(0, stake_sdk_1.isSupportedLendingChain)(chainIdToUse)) {
|
470
|
-
return [];
|
471
|
-
}
|
472
|
-
return __classPrivateFieldGet(this, _EarnController_earnApiService, "f").lending.getPositionHistory(addressToUse, chainIdToUse, protocol, marketId, marketAddress, positionId, days);
|
473
|
-
}
|
474
|
-
/**
|
475
|
-
* Gets the lending market daily apys and averages for the current chain.
|
476
|
-
*
|
477
|
-
* @param options - Optional arguments
|
478
|
-
* @param [options.chainId] - The chain id to get lending market daily apys and averages for (optional).
|
479
|
-
* @param [options.protocol] - The protocol to get lending market daily apys and averages for.
|
480
|
-
* @param [options.marketId] - The market id to get lending market daily apys and averages for.
|
481
|
-
* @param [options.days] - The number of days to get lending market daily apys and averages for (optional).
|
482
|
-
* @returns A promise that resolves when the lending market daily apys and averages have been updated
|
483
|
-
*/
|
484
|
-
getLendingMarketDailyApysAndAverages({ chainId, protocol, marketId, days = 365, }) {
|
485
|
-
const chainIdToUse = chainId ?? __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentChainId).call(this);
|
486
|
-
if (!(0, stake_sdk_1.isSupportedLendingChain)(chainIdToUse)) {
|
487
|
-
return undefined;
|
488
|
-
}
|
489
|
-
return __classPrivateFieldGet(this, _EarnController_earnApiService, "f").lending.getHistoricMarketApys(chainIdToUse, protocol, marketId, days);
|
490
|
-
}
|
491
|
-
/**
|
492
|
-
* Executes a lending deposit transaction.
|
493
|
-
*
|
494
|
-
* @param options - The options for the lending deposit transaction.
|
495
|
-
* @param options.amount - The amount to deposit.
|
496
|
-
* @param options.protocol - The protocol of the lending market.
|
497
|
-
* @param options.underlyingTokenAddress - The address of the underlying token.
|
498
|
-
* @param options.gasOptions - The gas options for the transaction.
|
499
|
-
* @param options.gasOptions.gasLimit - The gas limit for the transaction.
|
500
|
-
* @param options.gasOptions.gasBufferPct - The gas buffer percentage for the transaction.
|
501
|
-
* @param options.txOptions - The transaction options for the transaction.
|
502
|
-
* @returns A promise that resolves to the transaction hash.
|
503
|
-
*/
|
504
|
-
async executeLendingDeposit({ amount, protocol, underlyingTokenAddress, gasOptions, txOptions, }) {
|
505
|
-
const address = __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentAccount).call(this)?.address;
|
506
|
-
const transactionData = await __classPrivateFieldGet(this, _EarnController_earnSDK, "f")?.contracts?.lending?.[protocol]?.[underlyingTokenAddress]?.encodeDepositTransactionData(amount, address, gasOptions);
|
507
|
-
if (!transactionData) {
|
508
|
-
throw new Error('Transaction data not found');
|
509
|
-
}
|
510
|
-
if (!__classPrivateFieldGet(this, _EarnController_selectedNetworkClientId, "f")) {
|
511
|
-
throw new Error('Selected network client id not found');
|
512
|
-
}
|
513
|
-
const txHash = await __classPrivateFieldGet(this, _EarnController_addTransactionFn, "f").call(this, {
|
514
|
-
...transactionData,
|
515
|
-
value: transactionData.value.toString(),
|
516
|
-
chainId: (0, controller_utils_1.toHex)(__classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentChainId).call(this)),
|
517
|
-
gasLimit: String(transactionData.gasLimit),
|
518
|
-
}, {
|
519
|
-
...txOptions,
|
520
|
-
networkClientId: __classPrivateFieldGet(this, _EarnController_selectedNetworkClientId, "f"),
|
521
|
-
});
|
522
|
-
return txHash;
|
523
|
-
}
|
524
|
-
/**
|
525
|
-
* Executes a lending withdraw transaction.
|
526
|
-
*
|
527
|
-
* @param options - The options for the lending withdraw transaction.
|
528
|
-
* @param options.amount - The amount to withdraw.
|
529
|
-
* @param options.protocol - The protocol of the lending market.
|
530
|
-
* @param options.underlyingTokenAddress - The address of the underlying token.
|
531
|
-
* @param options.gasOptions - The gas options for the transaction.
|
532
|
-
* @param options.gasOptions.gasLimit - The gas limit for the transaction.
|
533
|
-
* @param options.gasOptions.gasBufferPct - The gas buffer percentage for the transaction.
|
534
|
-
* @param options.txOptions - The transaction options for the transaction.
|
535
|
-
* @returns A promise that resolves to the transaction hash.
|
536
|
-
*/
|
537
|
-
async executeLendingWithdraw({ amount, protocol, underlyingTokenAddress, gasOptions, txOptions, }) {
|
538
|
-
const address = __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentAccount).call(this)?.address;
|
539
|
-
const transactionData = await __classPrivateFieldGet(this, _EarnController_earnSDK, "f")?.contracts?.lending?.[protocol]?.[underlyingTokenAddress]?.encodeWithdrawTransactionData(amount, address, gasOptions);
|
540
|
-
if (!transactionData) {
|
541
|
-
throw new Error('Transaction data not found');
|
542
|
-
}
|
543
|
-
if (!__classPrivateFieldGet(this, _EarnController_selectedNetworkClientId, "f")) {
|
544
|
-
throw new Error('Selected network client id not found');
|
545
|
-
}
|
546
|
-
const txHash = await __classPrivateFieldGet(this, _EarnController_addTransactionFn, "f").call(this, {
|
547
|
-
...transactionData,
|
548
|
-
value: transactionData.value.toString(),
|
549
|
-
chainId: (0, controller_utils_1.toHex)(__classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentChainId).call(this)),
|
550
|
-
gasLimit: String(transactionData.gasLimit),
|
551
|
-
}, {
|
552
|
-
...txOptions,
|
553
|
-
networkClientId: __classPrivateFieldGet(this, _EarnController_selectedNetworkClientId, "f"),
|
554
|
-
});
|
555
|
-
return txHash;
|
556
|
-
}
|
557
|
-
/**
|
558
|
-
* Executes a lending token approve transaction.
|
559
|
-
*
|
560
|
-
* @param options - The options for the lending token approve transaction.
|
561
|
-
* @param options.amount - The amount to approve.
|
562
|
-
* @param options.protocol - The protocol of the lending market.
|
563
|
-
* @param options.underlyingTokenAddress - The address of the underlying token.
|
564
|
-
* @param options.gasOptions - The gas options for the transaction.
|
565
|
-
* @param options.gasOptions.gasLimit - The gas limit for the transaction.
|
566
|
-
* @param options.gasOptions.gasBufferPct - The gas buffer percentage for the transaction.
|
567
|
-
* @param options.txOptions - The transaction options for the transaction.
|
568
|
-
* @returns A promise that resolves to the transaction hash.
|
569
|
-
*/
|
570
|
-
async executeLendingTokenApprove({ protocol, amount, underlyingTokenAddress, gasOptions, txOptions, }) {
|
571
|
-
const address = __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentAccount).call(this)?.address;
|
572
|
-
const transactionData = await __classPrivateFieldGet(this, _EarnController_earnSDK, "f")?.contracts?.lending?.[protocol]?.[underlyingTokenAddress]?.encodeUnderlyingTokenApproveTransactionData(amount, address, gasOptions);
|
573
|
-
if (!transactionData) {
|
574
|
-
throw new Error('Transaction data not found');
|
575
|
-
}
|
576
|
-
if (!__classPrivateFieldGet(this, _EarnController_selectedNetworkClientId, "f")) {
|
577
|
-
throw new Error('Selected network client id not found');
|
578
|
-
}
|
579
|
-
const txHash = await __classPrivateFieldGet(this, _EarnController_addTransactionFn, "f").call(this, {
|
580
|
-
...transactionData,
|
581
|
-
value: transactionData.value.toString(),
|
582
|
-
chainId: (0, controller_utils_1.toHex)(__classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentChainId).call(this)),
|
583
|
-
gasLimit: String(transactionData.gasLimit),
|
584
|
-
}, {
|
585
|
-
...txOptions,
|
586
|
-
networkClientId: __classPrivateFieldGet(this, _EarnController_selectedNetworkClientId, "f"),
|
587
|
-
});
|
588
|
-
return txHash;
|
589
|
-
}
|
590
|
-
/**
|
591
|
-
* Gets the allowance for a lending token.
|
592
|
-
*
|
593
|
-
* @param protocol - The protocol of the lending market.
|
594
|
-
* @param underlyingTokenAddress - The address of the underlying token.
|
595
|
-
* @returns A promise that resolves to the allowance.
|
596
|
-
*/
|
597
|
-
async getLendingTokenAllowance(protocol, underlyingTokenAddress) {
|
598
|
-
const address = __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentAccount).call(this)?.address;
|
599
|
-
const allowance = await __classPrivateFieldGet(this, _EarnController_earnSDK, "f")?.contracts?.lending?.[protocol]?.[underlyingTokenAddress]?.underlyingTokenAllowance(address);
|
600
|
-
return allowance;
|
601
|
-
}
|
602
|
-
/**
|
603
|
-
* Gets the maximum withdraw amount for a lending token's output token or shares if no output token.
|
604
|
-
*
|
605
|
-
* @param protocol - The protocol of the lending market.
|
606
|
-
* @param underlyingTokenAddress - The address of the underlying token.
|
607
|
-
* @returns A promise that resolves to the maximum withdraw amount.
|
608
|
-
*/
|
609
|
-
async getLendingTokenMaxWithdraw(protocol, underlyingTokenAddress) {
|
610
|
-
const address = __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentAccount).call(this)?.address;
|
611
|
-
const maxWithdraw = await __classPrivateFieldGet(this, _EarnController_earnSDK, "f")?.contracts?.lending?.[protocol]?.[underlyingTokenAddress]?.maxWithdraw(address);
|
612
|
-
return maxWithdraw;
|
613
|
-
}
|
614
|
-
/**
|
615
|
-
* Gets the maximum deposit amount for a lending token.
|
616
|
-
*
|
617
|
-
* @param protocol - The protocol of the lending market.
|
618
|
-
* @param underlyingTokenAddress - The address of the underlying token.
|
619
|
-
* @returns A promise that resolves to the maximum deposit amount.
|
620
|
-
*/
|
621
|
-
async getLendingTokenMaxDeposit(protocol, underlyingTokenAddress) {
|
622
|
-
const address = __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getCurrentAccount).call(this)?.address;
|
623
|
-
const maxDeposit = await __classPrivateFieldGet(this, _EarnController_earnSDK, "f")?.contracts?.lending?.[protocol]?.[underlyingTokenAddress]?.maxDeposit(address);
|
624
|
-
return maxDeposit;
|
625
|
-
}
|
626
278
|
}
|
627
279
|
exports.EarnController = EarnController;
|
628
|
-
|
629
|
-
/**
|
630
|
-
* Initializes the Earn SDK.
|
631
|
-
*
|
632
|
-
* @param networkClientId - The network client id to initialize the Earn SDK for (optional).
|
633
|
-
*/
|
634
|
-
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) {
|
635
281
|
const { selectedNetworkClientId } = networkClientId
|
636
282
|
? { selectedNetworkClientId: networkClientId }
|
637
283
|
: this.messagingSystem.call('NetworkController:getState');
|
638
284
|
const networkClient = this.messagingSystem.call('NetworkController:getNetworkClientById', selectedNetworkClientId);
|
639
285
|
if (!networkClient?.provider) {
|
640
|
-
__classPrivateFieldSet(this,
|
286
|
+
__classPrivateFieldSet(this, _EarnController_stakeSDK, null, "f");
|
641
287
|
return;
|
642
288
|
}
|
643
289
|
const provider = new providers_1.Web3Provider(networkClient.provider);
|
@@ -645,26 +291,33 @@ async function _EarnController_initializeSDK(networkClientId) {
|
|
645
291
|
// Initialize appropriate contracts based on chainId
|
646
292
|
const config = {
|
647
293
|
chainId: (0, controller_utils_1.convertHexToDecimal)(chainId),
|
648
|
-
env: __classPrivateFieldGet(this, _EarnController_env, "f"),
|
649
294
|
};
|
650
295
|
try {
|
651
|
-
__classPrivateFieldSet(this,
|
296
|
+
__classPrivateFieldSet(this, _EarnController_stakeSDK, stake_sdk_1.StakeSdk.create(config), "f");
|
297
|
+
__classPrivateFieldGet(this, _EarnController_stakeSDK, "f").pooledStakingContract.connectSignerOrProvider(provider);
|
652
298
|
}
|
653
299
|
catch (error) {
|
654
|
-
__classPrivateFieldSet(this,
|
300
|
+
__classPrivateFieldSet(this, _EarnController_stakeSDK, null, "f");
|
655
301
|
// Only log unexpected errors, not unsupported chain errors
|
656
302
|
if (!(error instanceof Error &&
|
657
303
|
error.message.includes('Unsupported chainId'))) {
|
658
|
-
console.error('
|
304
|
+
console.error('Stake SDK initialization failed:', error);
|
659
305
|
}
|
660
306
|
}
|
661
307
|
}, _EarnController_getCurrentAccount = function _EarnController_getCurrentAccount() {
|
662
308
|
return this.messagingSystem.call('AccountsController:getSelectedAccount');
|
663
|
-
}, _EarnController_getCurrentChainId = function _EarnController_getCurrentChainId(
|
664
|
-
const
|
665
|
-
|
666
|
-
|
667
|
-
const {
|
668
|
-
|
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;
|
669
322
|
};
|
670
323
|
//# sourceMappingURL=EarnController.cjs.map
|