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