@metamask-previews/earn-controller 12.2.6-preview-3866c0ff1 → 12.2.6-preview-e3275932a
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 -4
- package/dist/EarnController-method-action-types.cjs +7 -0
- package/dist/EarnController-method-action-types.cjs.map +1 -0
- package/dist/{EarnController-method-action-types.d.ts → EarnController-method-action-types.d.cts} +2 -2
- package/dist/EarnController-method-action-types.d.cts.map +1 -0
- package/dist/EarnController-method-action-types.d.mts +263 -0
- package/dist/EarnController-method-action-types.d.mts.map +1 -0
- package/dist/{EarnController-method-action-types.js → EarnController-method-action-types.mjs} +1 -1
- package/dist/EarnController-method-action-types.mjs.map +1 -0
- package/dist/EarnController.cjs +778 -0
- package/dist/EarnController.cjs.map +1 -0
- package/dist/{EarnController.d.ts → EarnController.d.cts} +12 -12
- package/dist/EarnController.d.cts.map +1 -0
- package/dist/EarnController.d.mts +365 -0
- package/dist/EarnController.d.mts.map +1 -0
- package/dist/{EarnController.js → EarnController.mjs} +135 -150
- package/dist/EarnController.mjs.map +1 -0
- package/dist/index.cjs +33 -0
- package/dist/index.cjs.map +1 -0
- package/dist/{index.d.ts → index.d.cts} +6 -6
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +6 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/{index.js → index.mjs} +4 -4
- package/dist/index.mjs.map +1 -0
- package/dist/selectors.cjs +121 -0
- package/dist/selectors.cjs.map +1 -0
- package/dist/{selectors.d.ts → selectors.d.cts} +117 -117
- package/dist/selectors.d.cts.map +1 -0
- package/dist/selectors.d.mts +1518 -0
- package/dist/selectors.d.mts.map +1 -0
- package/dist/{selectors.js → selectors.mjs} +5 -4
- package/dist/selectors.mjs.map +1 -0
- package/dist/types.cjs +3 -0
- package/dist/types.cjs.map +1 -0
- package/dist/{types.d.ts → types.d.cts} +1 -1
- package/dist/types.d.cts.map +1 -0
- package/dist/types.d.mts +21 -0
- package/dist/types.d.mts.map +1 -0
- package/dist/types.mjs +2 -0
- package/dist/types.mjs.map +1 -0
- package/package.json +16 -12
- package/dist/EarnController-method-action-types.d.ts.map +0 -1
- package/dist/EarnController-method-action-types.js.map +0 -1
- package/dist/EarnController.d.ts.map +0 -1
- package/dist/EarnController.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/selectors.d.ts.map +0 -1
- package/dist/selectors.js.map +0 -1
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js +0 -2
- package/dist/types.js.map +0 -1
|
@@ -1,9 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
+
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
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
+
};
|
|
7
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
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_initPromise, _EarnController_lastRefreshedAddress, _EarnController_earnApiService, _EarnController_addTransactionFn, _EarnController_supportedPooledStakingChains, _EarnController_env, _EarnController_refreshEarnPortfolio, _EarnController_initializeSDK, _EarnController_getSelectedNetworkClientId, _EarnController_getSelectedEvmAccount, _EarnController_getSelectedEvmAccountAddress, _EarnController_refreshEarnPortfolioOnAccountReady;
|
|
13
|
+
import { Web3Provider } from "@ethersproject/providers";
|
|
14
|
+
import { BaseController } from "@metamask/base-controller";
|
|
15
|
+
import { convertHexToDecimal, toHex } from "@metamask/controller-utils";
|
|
16
|
+
import { isEvmAccountType } from "@metamask/keyring-api";
|
|
17
|
+
import { EarnSdk, EarnApiService, isSupportedLendingChain, EarnEnvironments, ChainId, isSupportedPooledStakingChain } from "@metamask/stake-sdk";
|
|
18
|
+
import { TransactionType } from "@metamask/transaction-controller";
|
|
7
19
|
export const controllerName = 'EarnController';
|
|
8
20
|
const stakingTransactionTypes = new Set([
|
|
9
21
|
TransactionType.stakingDeposit,
|
|
@@ -152,13 +164,6 @@ const MESSENGER_EXPOSED_METHODS = [
|
|
|
152
164
|
* EarnController manages DeFi earning opportunities across different protocols and chains.
|
|
153
165
|
*/
|
|
154
166
|
export class EarnController extends BaseController {
|
|
155
|
-
#earnSDK = null;
|
|
156
|
-
#initPromise = null;
|
|
157
|
-
#lastRefreshedAddress;
|
|
158
|
-
#earnApiService;
|
|
159
|
-
#addTransactionFn;
|
|
160
|
-
#supportedPooledStakingChains;
|
|
161
|
-
#env;
|
|
162
167
|
constructor({ messenger, state = {}, addTransactionFn, env = EarnEnvironments.PROD, }) {
|
|
163
168
|
super({
|
|
164
169
|
name: controllerName,
|
|
@@ -169,8 +174,16 @@ export class EarnController extends BaseController {
|
|
|
169
174
|
...state,
|
|
170
175
|
},
|
|
171
176
|
});
|
|
172
|
-
this
|
|
173
|
-
this
|
|
177
|
+
_EarnController_instances.add(this);
|
|
178
|
+
_EarnController_earnSDK.set(this, null);
|
|
179
|
+
_EarnController_initPromise.set(this, null);
|
|
180
|
+
_EarnController_lastRefreshedAddress.set(this, void 0);
|
|
181
|
+
_EarnController_earnApiService.set(this, void 0);
|
|
182
|
+
_EarnController_addTransactionFn.set(this, void 0);
|
|
183
|
+
_EarnController_supportedPooledStakingChains.set(this, void 0);
|
|
184
|
+
_EarnController_env.set(this, void 0);
|
|
185
|
+
__classPrivateFieldSet(this, _EarnController_env, env, "f");
|
|
186
|
+
__classPrivateFieldSet(this, _EarnController_earnApiService, new EarnApiService(__classPrivateFieldGet(this, _EarnController_env, "f")), "f");
|
|
174
187
|
// temporary array of supported chains
|
|
175
188
|
// TODO: remove this once we export a supported chains list from the sdk
|
|
176
189
|
// from sdk or api to get lending and pooled staking chains
|
|
@@ -179,11 +192,11 @@ export class EarnController extends BaseController {
|
|
|
179
192
|
// (testnet) is still fully supported on-demand via explicit chainId
|
|
180
193
|
// calls (e.g. refreshPooledStakes({ chainId: ChainId.HOODI })), we just
|
|
181
194
|
// don't unconditionally fetch it for every user on every unlock.
|
|
182
|
-
this
|
|
183
|
-
this
|
|
195
|
+
__classPrivateFieldSet(this, _EarnController_supportedPooledStakingChains, [ChainId.ETHEREUM], "f");
|
|
196
|
+
__classPrivateFieldSet(this, _EarnController_addTransactionFn, addTransactionFn, "f");
|
|
184
197
|
// Listen for network changes
|
|
185
198
|
this.messenger.subscribe('NetworkController:networkDidChange', (networkControllerState) => {
|
|
186
|
-
this
|
|
199
|
+
__classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_initializeSDK).call(this, networkControllerState.selectedNetworkClientId).catch(console.error);
|
|
187
200
|
// refresh pooled staking data
|
|
188
201
|
this.refreshPooledStakingVaultMetadata().catch(console.error);
|
|
189
202
|
this.refreshPooledStakingVaultDailyApys().catch(console.error);
|
|
@@ -196,14 +209,14 @@ export class EarnController extends BaseController {
|
|
|
196
209
|
this.messenger.registerMethodActionHandlers(this, MESSENGER_EXPOSED_METHODS);
|
|
197
210
|
// Listen for account group changes
|
|
198
211
|
this.messenger.subscribe('AccountTreeController:selectedAccountGroupChange', () => {
|
|
199
|
-
const address = this
|
|
212
|
+
const address = __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getSelectedEvmAccountAddress).call(this);
|
|
200
213
|
// Skip if this account group change didn't actually change the
|
|
201
214
|
// resolved address (e.g. it can fire again right after init()
|
|
202
215
|
// during startup hydration) - nothing new to fetch.
|
|
203
|
-
if (!address || address === this
|
|
216
|
+
if (!address || address === __classPrivateFieldGet(this, _EarnController_lastRefreshedAddress, "f")) {
|
|
204
217
|
return;
|
|
205
218
|
}
|
|
206
|
-
this
|
|
219
|
+
__classPrivateFieldSet(this, _EarnController_lastRefreshedAddress, address, "f");
|
|
207
220
|
// TODO: temp solution, this will refresh lending eligibility also
|
|
208
221
|
// we could have a more general check, as what is happening is a compliance address check
|
|
209
222
|
this.refreshEarnEligibility({ address }).catch(console.error);
|
|
@@ -231,111 +244,25 @@ export class EarnController extends BaseController {
|
|
|
231
244
|
}
|
|
232
245
|
});
|
|
233
246
|
}
|
|
234
|
-
#refreshEarnPortfolio(address) {
|
|
235
|
-
this.#lastRefreshedAddress = address;
|
|
236
|
-
this.refreshEarnEligibility({ address }).catch(console.error);
|
|
237
|
-
this.refreshPooledStakingData({ address }).catch(console.error);
|
|
238
|
-
this.refreshLendingData().catch(console.error);
|
|
239
|
-
}
|
|
240
247
|
async init() {
|
|
241
|
-
if (this
|
|
242
|
-
return this
|
|
248
|
+
if (__classPrivateFieldGet(this, _EarnController_initPromise, "f")) {
|
|
249
|
+
return __classPrivateFieldGet(this, _EarnController_initPromise, "f");
|
|
243
250
|
}
|
|
244
|
-
this
|
|
245
|
-
await this
|
|
246
|
-
const address = this
|
|
251
|
+
__classPrivateFieldSet(this, _EarnController_initPromise, (async () => {
|
|
252
|
+
await __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_initializeSDK).call(this, __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getSelectedNetworkClientId).call(this));
|
|
253
|
+
const address = __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getSelectedEvmAccountAddress).call(this);
|
|
247
254
|
if (address) {
|
|
248
|
-
this
|
|
255
|
+
__classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_refreshEarnPortfolio).call(this, address);
|
|
249
256
|
}
|
|
250
257
|
else {
|
|
251
258
|
// Account tree state is not yet available, so we defer the refresh to when it is.
|
|
252
|
-
this
|
|
259
|
+
__classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_refreshEarnPortfolioOnAccountReady).call(this);
|
|
253
260
|
}
|
|
254
261
|
})().catch((error) => {
|
|
255
|
-
this
|
|
262
|
+
__classPrivateFieldSet(this, _EarnController_initPromise, null, "f");
|
|
256
263
|
throw error;
|
|
257
|
-
});
|
|
258
|
-
return this
|
|
259
|
-
}
|
|
260
|
-
/**
|
|
261
|
-
* Initializes the Earn SDK.
|
|
262
|
-
*
|
|
263
|
-
* @param networkClientId - The network client id to initialize the Earn SDK for.
|
|
264
|
-
*/
|
|
265
|
-
async #initializeSDK(networkClientId) {
|
|
266
|
-
const networkClient = this.messenger.call('NetworkController:getNetworkClientById', networkClientId);
|
|
267
|
-
if (!networkClient?.provider) {
|
|
268
|
-
this.#earnSDK = null;
|
|
269
|
-
return;
|
|
270
|
-
}
|
|
271
|
-
const provider = new Web3Provider(networkClient.provider);
|
|
272
|
-
const { chainId } = networkClient.configuration;
|
|
273
|
-
// Initialize appropriate contracts based on chainId
|
|
274
|
-
const config = {
|
|
275
|
-
chainId: convertHexToDecimal(chainId),
|
|
276
|
-
env: this.#env,
|
|
277
|
-
};
|
|
278
|
-
try {
|
|
279
|
-
this.#earnSDK = await EarnSdk.create(provider, config);
|
|
280
|
-
}
|
|
281
|
-
catch (error) {
|
|
282
|
-
this.#earnSDK = null;
|
|
283
|
-
// Only log unexpected errors, not unsupported chain errors
|
|
284
|
-
if (!(error instanceof Error &&
|
|
285
|
-
error.message.includes('Unsupported chainId'))) {
|
|
286
|
-
console.error('Earn SDK initialization failed:', error);
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
/**
|
|
291
|
-
* Gets the selected network client ID from NetworkController's live state.
|
|
292
|
-
*
|
|
293
|
-
* @returns The selected network client ID.
|
|
294
|
-
*/
|
|
295
|
-
#getSelectedNetworkClientId() {
|
|
296
|
-
return this.messenger.call('NetworkController:getState')
|
|
297
|
-
.selectedNetworkClientId;
|
|
298
|
-
}
|
|
299
|
-
/**
|
|
300
|
-
* Gets the EVM account from the selected account group.
|
|
301
|
-
*
|
|
302
|
-
* @returns The EVM account or undefined if no EVM account is found.
|
|
303
|
-
*/
|
|
304
|
-
#getSelectedEvmAccount() {
|
|
305
|
-
return this.messenger
|
|
306
|
-
.call('AccountTreeController:getAccountsFromSelectedAccountGroup')
|
|
307
|
-
.find((account) => isEvmAccountType(account.type));
|
|
308
|
-
}
|
|
309
|
-
/**
|
|
310
|
-
* Gets the EVM account address from the selected account group.
|
|
311
|
-
*
|
|
312
|
-
* @returns The EVM account address or undefined if no EVM account is found.
|
|
313
|
-
*/
|
|
314
|
-
#getSelectedEvmAccountAddress() {
|
|
315
|
-
return this.#getSelectedEvmAccount()?.address;
|
|
316
|
-
}
|
|
317
|
-
/**
|
|
318
|
-
* Sets up a one-time subscription to AccountTreeController:stateChange that
|
|
319
|
-
* triggers address-dependent refreshes once both the selected account group
|
|
320
|
-
* is populated and an EVM account address is resolvable. Unsubscribes
|
|
321
|
-
* only after a refresh is triggered.
|
|
322
|
-
*
|
|
323
|
-
* This handles the case where EarnController.init() runs before
|
|
324
|
-
* AccountTreeController.init() has populated the selected account group.
|
|
325
|
-
*/
|
|
326
|
-
#refreshEarnPortfolioOnAccountReady() {
|
|
327
|
-
const handler = ({ selectedAccountGroup, }) => {
|
|
328
|
-
if (!selectedAccountGroup) {
|
|
329
|
-
return;
|
|
330
|
-
}
|
|
331
|
-
const address = this.#getSelectedEvmAccountAddress();
|
|
332
|
-
if (!address) {
|
|
333
|
-
return;
|
|
334
|
-
}
|
|
335
|
-
this.messenger.unsubscribe('AccountTreeController:stateChange', handler);
|
|
336
|
-
this.#refreshEarnPortfolio(address);
|
|
337
|
-
};
|
|
338
|
-
this.messenger.subscribe('AccountTreeController:stateChange', handler);
|
|
264
|
+
}), "f");
|
|
265
|
+
return __classPrivateFieldGet(this, _EarnController_initPromise, "f");
|
|
339
266
|
}
|
|
340
267
|
/**
|
|
341
268
|
* Refreshes the pooled stakes data for the current account.
|
|
@@ -349,14 +276,14 @@ export class EarnController extends BaseController {
|
|
|
349
276
|
* @returns A promise that resolves when the stakes data has been updated
|
|
350
277
|
*/
|
|
351
278
|
async refreshPooledStakes({ resetCache = false, address, chainId = ChainId.ETHEREUM, } = {}) {
|
|
352
|
-
const addressToUse = address ?? this
|
|
279
|
+
const addressToUse = address ?? __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getSelectedEvmAccountAddress).call(this);
|
|
353
280
|
if (!addressToUse) {
|
|
354
281
|
return;
|
|
355
282
|
}
|
|
356
283
|
const chainIdToUse = isSupportedPooledStakingChain(chainId)
|
|
357
284
|
? chainId
|
|
358
285
|
: ChainId.ETHEREUM;
|
|
359
|
-
const { accounts, exchangeRate } = await this
|
|
286
|
+
const { accounts, exchangeRate } = await __classPrivateFieldGet(this, _EarnController_earnApiService, "f").pooledStaking.getPooledStakes([addressToUse], chainIdToUse, resetCache);
|
|
360
287
|
this.update((state) => {
|
|
361
288
|
const chainState = state.pooled_staking[chainIdToUse] ??
|
|
362
289
|
DEFAULT_POOLED_STAKING_CHAIN_STATE;
|
|
@@ -378,11 +305,11 @@ export class EarnController extends BaseController {
|
|
|
378
305
|
* @returns A promise that resolves when the eligibility status has been updated
|
|
379
306
|
*/
|
|
380
307
|
async refreshEarnEligibility({ address, } = {}) {
|
|
381
|
-
const addressToCheck = address ?? this
|
|
308
|
+
const addressToCheck = address ?? __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getSelectedEvmAccountAddress).call(this);
|
|
382
309
|
if (!addressToCheck) {
|
|
383
310
|
return;
|
|
384
311
|
}
|
|
385
|
-
const { eligible: isEligible } = await this
|
|
312
|
+
const { eligible: isEligible } = await __classPrivateFieldGet(this, _EarnController_earnApiService, "f").pooledStaking.getPooledStakingEligibility([
|
|
386
313
|
addressToCheck,
|
|
387
314
|
]);
|
|
388
315
|
this.update((state) => {
|
|
@@ -402,7 +329,7 @@ export class EarnController extends BaseController {
|
|
|
402
329
|
const chainIdToUse = isSupportedPooledStakingChain(chainId)
|
|
403
330
|
? chainId
|
|
404
331
|
: ChainId.ETHEREUM;
|
|
405
|
-
const vaultMetadata = await this
|
|
332
|
+
const vaultMetadata = await __classPrivateFieldGet(this, _EarnController_earnApiService, "f").pooledStaking.getVaultData(chainIdToUse);
|
|
406
333
|
this.update((state) => {
|
|
407
334
|
const chainState = state.pooled_staking[chainIdToUse] ??
|
|
408
335
|
DEFAULT_POOLED_STAKING_CHAIN_STATE;
|
|
@@ -426,7 +353,7 @@ export class EarnController extends BaseController {
|
|
|
426
353
|
const chainIdToUse = isSupportedPooledStakingChain(chainId)
|
|
427
354
|
? chainId
|
|
428
355
|
: ChainId.ETHEREUM;
|
|
429
|
-
const vaultDailyApys = await this
|
|
356
|
+
const vaultDailyApys = await __classPrivateFieldGet(this, _EarnController_earnApiService, "f").pooledStaking.getVaultDailyApys(chainIdToUse, days, order);
|
|
430
357
|
this.update((state) => {
|
|
431
358
|
const chainState = state.pooled_staking[chainIdToUse] ??
|
|
432
359
|
DEFAULT_POOLED_STAKING_CHAIN_STATE;
|
|
@@ -447,7 +374,7 @@ export class EarnController extends BaseController {
|
|
|
447
374
|
const chainIdToUse = isSupportedPooledStakingChain(chainId)
|
|
448
375
|
? chainId
|
|
449
376
|
: ChainId.ETHEREUM;
|
|
450
|
-
const vaultApyAverages = await this
|
|
377
|
+
const vaultApyAverages = await __classPrivateFieldGet(this, _EarnController_earnApiService, "f").pooledStaking.getVaultApyAverages(chainIdToUse);
|
|
451
378
|
this.update((state) => {
|
|
452
379
|
const chainState = state.pooled_staking[chainIdToUse] ??
|
|
453
380
|
DEFAULT_POOLED_STAKING_CHAIN_STATE;
|
|
@@ -470,7 +397,7 @@ export class EarnController extends BaseController {
|
|
|
470
397
|
*/
|
|
471
398
|
async refreshPooledStakingData({ resetCache, address, } = {}) {
|
|
472
399
|
const errors = [];
|
|
473
|
-
for (const chainId of this
|
|
400
|
+
for (const chainId of __classPrivateFieldGet(this, _EarnController_supportedPooledStakingChains, "f")) {
|
|
474
401
|
await Promise.all([
|
|
475
402
|
this.refreshPooledStakes({ resetCache, address, chainId }).catch((error) => {
|
|
476
403
|
errors.push(error);
|
|
@@ -499,7 +426,7 @@ export class EarnController extends BaseController {
|
|
|
499
426
|
* @returns A promise that resolves when the lending markets have been updated
|
|
500
427
|
*/
|
|
501
428
|
async refreshLendingMarkets() {
|
|
502
|
-
const markets = await this
|
|
429
|
+
const markets = await __classPrivateFieldGet(this, _EarnController_earnApiService, "f").lending.getMarkets();
|
|
503
430
|
this.update((state) => {
|
|
504
431
|
state.lending.markets = markets;
|
|
505
432
|
});
|
|
@@ -513,13 +440,13 @@ export class EarnController extends BaseController {
|
|
|
513
440
|
* @returns A promise that resolves when the lending positions have been updated
|
|
514
441
|
*/
|
|
515
442
|
async refreshLendingPositions({ address, } = {}) {
|
|
516
|
-
const addressToUse = address ?? this
|
|
443
|
+
const addressToUse = address ?? __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getSelectedEvmAccountAddress).call(this);
|
|
517
444
|
if (!addressToUse) {
|
|
518
445
|
return;
|
|
519
446
|
}
|
|
520
447
|
// linter complaining about this not being a promise, but it is
|
|
521
448
|
// TODO: figure out why this is not seen as a promise
|
|
522
|
-
const positions = await Promise.resolve(this
|
|
449
|
+
const positions = await Promise.resolve(__classPrivateFieldGet(this, _EarnController_earnApiService, "f").lending.getPositions(addressToUse));
|
|
523
450
|
this.update((state) => {
|
|
524
451
|
state.lending.positions = positions.map((position) => ({
|
|
525
452
|
...position,
|
|
@@ -591,11 +518,11 @@ export class EarnController extends BaseController {
|
|
|
591
518
|
* @returns A promise that resolves when the lending position history has been updated
|
|
592
519
|
*/
|
|
593
520
|
getLendingPositionHistory({ address, chainId, positionId, marketId, marketAddress, protocol, days = 730, }) {
|
|
594
|
-
const addressToUse = address ?? this
|
|
521
|
+
const addressToUse = address ?? __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getSelectedEvmAccountAddress).call(this);
|
|
595
522
|
if (!addressToUse || !isSupportedLendingChain(chainId)) {
|
|
596
523
|
return [];
|
|
597
524
|
}
|
|
598
|
-
return this
|
|
525
|
+
return __classPrivateFieldGet(this, _EarnController_earnApiService, "f").lending.getPositionHistory(addressToUse, chainId, protocol, marketId, marketAddress, positionId, days);
|
|
599
526
|
}
|
|
600
527
|
/**
|
|
601
528
|
* Gets the lending market daily apys and averages for the current chain.
|
|
@@ -611,7 +538,7 @@ export class EarnController extends BaseController {
|
|
|
611
538
|
if (!isSupportedLendingChain(chainId)) {
|
|
612
539
|
return undefined;
|
|
613
540
|
}
|
|
614
|
-
return this
|
|
541
|
+
return __classPrivateFieldGet(this, _EarnController_earnApiService, "f").lending.getHistoricMarketApys(chainId, protocol, marketId, days);
|
|
615
542
|
}
|
|
616
543
|
/**
|
|
617
544
|
* Executes a lending deposit transaction.
|
|
@@ -628,22 +555,22 @@ export class EarnController extends BaseController {
|
|
|
628
555
|
* @returns A promise that resolves to the transaction hash.
|
|
629
556
|
*/
|
|
630
557
|
async executeLendingDeposit({ amount, chainId, protocol, underlyingTokenAddress, gasOptions, txOptions, }) {
|
|
631
|
-
const address = this
|
|
558
|
+
const address = __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getSelectedEvmAccountAddress).call(this);
|
|
632
559
|
if (!address) {
|
|
633
560
|
throw new Error('No EVM-compatible account address found');
|
|
634
561
|
}
|
|
635
|
-
const transactionData = await this
|
|
562
|
+
const transactionData = await __classPrivateFieldGet(this, _EarnController_earnSDK, "f")?.contracts?.lending?.[protocol]?.[underlyingTokenAddress]?.encodeDepositTransactionData(amount, address, gasOptions);
|
|
636
563
|
if (!transactionData) {
|
|
637
564
|
throw new Error('Transaction data not found');
|
|
638
565
|
}
|
|
639
|
-
const selectedNetworkClientId = this
|
|
566
|
+
const selectedNetworkClientId = __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getSelectedNetworkClientId).call(this);
|
|
640
567
|
if (!selectedNetworkClientId) {
|
|
641
568
|
throw new Error('Selected network client id not found');
|
|
642
569
|
}
|
|
643
570
|
const gasLimit = !transactionData.gasLimit
|
|
644
571
|
? undefined
|
|
645
572
|
: toHex(transactionData.gasLimit);
|
|
646
|
-
const txHash = await this
|
|
573
|
+
const txHash = await __classPrivateFieldGet(this, _EarnController_addTransactionFn, "f").call(this, {
|
|
647
574
|
...transactionData,
|
|
648
575
|
value: transactionData.value.toString(),
|
|
649
576
|
chainId: toHex(chainId),
|
|
@@ -670,22 +597,22 @@ export class EarnController extends BaseController {
|
|
|
670
597
|
* @returns A promise that resolves to the transaction hash.
|
|
671
598
|
*/
|
|
672
599
|
async executeLendingWithdraw({ amount, chainId, protocol, underlyingTokenAddress, gasOptions, txOptions, }) {
|
|
673
|
-
const address = this
|
|
600
|
+
const address = __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getSelectedEvmAccountAddress).call(this);
|
|
674
601
|
if (!address) {
|
|
675
602
|
throw new Error('No EVM-compatible account address found');
|
|
676
603
|
}
|
|
677
|
-
const transactionData = await this
|
|
604
|
+
const transactionData = await __classPrivateFieldGet(this, _EarnController_earnSDK, "f")?.contracts?.lending?.[protocol]?.[underlyingTokenAddress]?.encodeWithdrawTransactionData(amount, address, gasOptions);
|
|
678
605
|
if (!transactionData) {
|
|
679
606
|
throw new Error('Transaction data not found');
|
|
680
607
|
}
|
|
681
|
-
const selectedNetworkClientId = this
|
|
608
|
+
const selectedNetworkClientId = __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getSelectedNetworkClientId).call(this);
|
|
682
609
|
if (!selectedNetworkClientId) {
|
|
683
610
|
throw new Error('Selected network client id not found');
|
|
684
611
|
}
|
|
685
612
|
const gasLimit = !transactionData.gasLimit
|
|
686
613
|
? undefined
|
|
687
614
|
: toHex(transactionData.gasLimit);
|
|
688
|
-
const txHash = await this
|
|
615
|
+
const txHash = await __classPrivateFieldGet(this, _EarnController_addTransactionFn, "f").call(this, {
|
|
689
616
|
...transactionData,
|
|
690
617
|
value: transactionData.value.toString(),
|
|
691
618
|
chainId: toHex(chainId),
|
|
@@ -712,22 +639,22 @@ export class EarnController extends BaseController {
|
|
|
712
639
|
* @returns A promise that resolves to the transaction hash.
|
|
713
640
|
*/
|
|
714
641
|
async executeLendingTokenApprove({ protocol, amount, chainId, underlyingTokenAddress, gasOptions, txOptions, }) {
|
|
715
|
-
const address = this
|
|
642
|
+
const address = __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getSelectedEvmAccountAddress).call(this);
|
|
716
643
|
if (!address) {
|
|
717
644
|
throw new Error('No EVM-compatible account address found');
|
|
718
645
|
}
|
|
719
|
-
const transactionData = await this
|
|
646
|
+
const transactionData = await __classPrivateFieldGet(this, _EarnController_earnSDK, "f")?.contracts?.lending?.[protocol]?.[underlyingTokenAddress]?.encodeUnderlyingTokenApproveTransactionData(amount, address, gasOptions);
|
|
720
647
|
if (!transactionData) {
|
|
721
648
|
throw new Error('Transaction data not found');
|
|
722
649
|
}
|
|
723
|
-
const selectedNetworkClientId = this
|
|
650
|
+
const selectedNetworkClientId = __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getSelectedNetworkClientId).call(this);
|
|
724
651
|
if (!selectedNetworkClientId) {
|
|
725
652
|
throw new Error('Selected network client id not found');
|
|
726
653
|
}
|
|
727
654
|
const gasLimit = !transactionData.gasLimit
|
|
728
655
|
? undefined
|
|
729
656
|
: toHex(transactionData.gasLimit);
|
|
730
|
-
const txHash = await this
|
|
657
|
+
const txHash = await __classPrivateFieldGet(this, _EarnController_addTransactionFn, "f").call(this, {
|
|
731
658
|
...transactionData,
|
|
732
659
|
value: transactionData.value.toString(),
|
|
733
660
|
chainId: toHex(chainId),
|
|
@@ -747,11 +674,11 @@ export class EarnController extends BaseController {
|
|
|
747
674
|
* @returns A promise that resolves to the allowance.
|
|
748
675
|
*/
|
|
749
676
|
async getLendingTokenAllowance(protocol, underlyingTokenAddress) {
|
|
750
|
-
const address = this
|
|
677
|
+
const address = __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getSelectedEvmAccountAddress).call(this);
|
|
751
678
|
if (!address) {
|
|
752
679
|
return undefined;
|
|
753
680
|
}
|
|
754
|
-
const allowance = await this
|
|
681
|
+
const allowance = await __classPrivateFieldGet(this, _EarnController_earnSDK, "f")?.contracts?.lending?.[protocol]?.[underlyingTokenAddress]?.underlyingTokenAllowance(address);
|
|
755
682
|
return allowance;
|
|
756
683
|
}
|
|
757
684
|
/**
|
|
@@ -762,11 +689,11 @@ export class EarnController extends BaseController {
|
|
|
762
689
|
* @returns A promise that resolves to the maximum withdraw amount.
|
|
763
690
|
*/
|
|
764
691
|
async getLendingTokenMaxWithdraw(protocol, underlyingTokenAddress) {
|
|
765
|
-
const address = this
|
|
692
|
+
const address = __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getSelectedEvmAccountAddress).call(this);
|
|
766
693
|
if (!address) {
|
|
767
694
|
return undefined;
|
|
768
695
|
}
|
|
769
|
-
const maxWithdraw = await this
|
|
696
|
+
const maxWithdraw = await __classPrivateFieldGet(this, _EarnController_earnSDK, "f")?.contracts?.lending?.[protocol]?.[underlyingTokenAddress]?.maxWithdraw(address);
|
|
770
697
|
return maxWithdraw;
|
|
771
698
|
}
|
|
772
699
|
/**
|
|
@@ -777,12 +704,70 @@ export class EarnController extends BaseController {
|
|
|
777
704
|
* @returns A promise that resolves to the maximum deposit amount.
|
|
778
705
|
*/
|
|
779
706
|
async getLendingTokenMaxDeposit(protocol, underlyingTokenAddress) {
|
|
780
|
-
const address = this
|
|
707
|
+
const address = __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getSelectedEvmAccountAddress).call(this);
|
|
781
708
|
if (!address) {
|
|
782
709
|
return undefined;
|
|
783
710
|
}
|
|
784
|
-
const maxDeposit = await this
|
|
711
|
+
const maxDeposit = await __classPrivateFieldGet(this, _EarnController_earnSDK, "f")?.contracts?.lending?.[protocol]?.[underlyingTokenAddress]?.maxDeposit(address);
|
|
785
712
|
return maxDeposit;
|
|
786
713
|
}
|
|
787
714
|
}
|
|
788
|
-
|
|
715
|
+
_EarnController_earnSDK = new WeakMap(), _EarnController_initPromise = new WeakMap(), _EarnController_lastRefreshedAddress = new WeakMap(), _EarnController_earnApiService = new WeakMap(), _EarnController_addTransactionFn = new WeakMap(), _EarnController_supportedPooledStakingChains = new WeakMap(), _EarnController_env = new WeakMap(), _EarnController_instances = new WeakSet(), _EarnController_refreshEarnPortfolio = function _EarnController_refreshEarnPortfolio(address) {
|
|
716
|
+
__classPrivateFieldSet(this, _EarnController_lastRefreshedAddress, address, "f");
|
|
717
|
+
this.refreshEarnEligibility({ address }).catch(console.error);
|
|
718
|
+
this.refreshPooledStakingData({ address }).catch(console.error);
|
|
719
|
+
this.refreshLendingData().catch(console.error);
|
|
720
|
+
}, _EarnController_initializeSDK =
|
|
721
|
+
/**
|
|
722
|
+
* Initializes the Earn SDK.
|
|
723
|
+
*
|
|
724
|
+
* @param networkClientId - The network client id to initialize the Earn SDK for.
|
|
725
|
+
*/
|
|
726
|
+
async function _EarnController_initializeSDK(networkClientId) {
|
|
727
|
+
const networkClient = this.messenger.call('NetworkController:getNetworkClientById', networkClientId);
|
|
728
|
+
if (!networkClient?.provider) {
|
|
729
|
+
__classPrivateFieldSet(this, _EarnController_earnSDK, null, "f");
|
|
730
|
+
return;
|
|
731
|
+
}
|
|
732
|
+
const provider = new Web3Provider(networkClient.provider);
|
|
733
|
+
const { chainId } = networkClient.configuration;
|
|
734
|
+
// Initialize appropriate contracts based on chainId
|
|
735
|
+
const config = {
|
|
736
|
+
chainId: convertHexToDecimal(chainId),
|
|
737
|
+
env: __classPrivateFieldGet(this, _EarnController_env, "f"),
|
|
738
|
+
};
|
|
739
|
+
try {
|
|
740
|
+
__classPrivateFieldSet(this, _EarnController_earnSDK, await EarnSdk.create(provider, config), "f");
|
|
741
|
+
}
|
|
742
|
+
catch (error) {
|
|
743
|
+
__classPrivateFieldSet(this, _EarnController_earnSDK, null, "f");
|
|
744
|
+
// Only log unexpected errors, not unsupported chain errors
|
|
745
|
+
if (!(error instanceof Error &&
|
|
746
|
+
error.message.includes('Unsupported chainId'))) {
|
|
747
|
+
console.error('Earn SDK initialization failed:', error);
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
}, _EarnController_getSelectedNetworkClientId = function _EarnController_getSelectedNetworkClientId() {
|
|
751
|
+
return this.messenger.call('NetworkController:getState')
|
|
752
|
+
.selectedNetworkClientId;
|
|
753
|
+
}, _EarnController_getSelectedEvmAccount = function _EarnController_getSelectedEvmAccount() {
|
|
754
|
+
return this.messenger
|
|
755
|
+
.call('AccountTreeController:getAccountsFromSelectedAccountGroup')
|
|
756
|
+
.find((account) => isEvmAccountType(account.type));
|
|
757
|
+
}, _EarnController_getSelectedEvmAccountAddress = function _EarnController_getSelectedEvmAccountAddress() {
|
|
758
|
+
return __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getSelectedEvmAccount).call(this)?.address;
|
|
759
|
+
}, _EarnController_refreshEarnPortfolioOnAccountReady = function _EarnController_refreshEarnPortfolioOnAccountReady() {
|
|
760
|
+
const handler = ({ selectedAccountGroup, }) => {
|
|
761
|
+
if (!selectedAccountGroup) {
|
|
762
|
+
return;
|
|
763
|
+
}
|
|
764
|
+
const address = __classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_getSelectedEvmAccountAddress).call(this);
|
|
765
|
+
if (!address) {
|
|
766
|
+
return;
|
|
767
|
+
}
|
|
768
|
+
this.messenger.unsubscribe('AccountTreeController:stateChange', handler);
|
|
769
|
+
__classPrivateFieldGet(this, _EarnController_instances, "m", _EarnController_refreshEarnPortfolio).call(this, address);
|
|
770
|
+
};
|
|
771
|
+
this.messenger.subscribe('AccountTreeController:stateChange', handler);
|
|
772
|
+
};
|
|
773
|
+
//# sourceMappingURL=EarnController.mjs.map
|