@metamask/assets-controllers 56.0.0 → 58.0.0
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 +37 -1
- package/README.md +1 -0
- package/dist/DeFiPositionsController/DeFiPositionsController.cjs +126 -0
- package/dist/DeFiPositionsController/DeFiPositionsController.cjs.map +1 -0
- package/dist/DeFiPositionsController/DeFiPositionsController.d.cts +72 -0
- package/dist/DeFiPositionsController/DeFiPositionsController.d.cts.map +1 -0
- package/dist/DeFiPositionsController/DeFiPositionsController.d.mts +72 -0
- package/dist/DeFiPositionsController/DeFiPositionsController.d.mts.map +1 -0
- package/dist/DeFiPositionsController/DeFiPositionsController.mjs +121 -0
- package/dist/DeFiPositionsController/DeFiPositionsController.mjs.map +1 -0
- package/dist/DeFiPositionsController/fetch-positions.cjs +21 -0
- package/dist/DeFiPositionsController/fetch-positions.cjs.map +1 -0
- package/dist/DeFiPositionsController/fetch-positions.d.cts +59 -0
- package/dist/DeFiPositionsController/fetch-positions.d.cts.map +1 -0
- package/dist/DeFiPositionsController/fetch-positions.d.mts +59 -0
- package/dist/DeFiPositionsController/fetch-positions.d.mts.map +1 -0
- package/dist/DeFiPositionsController/fetch-positions.mjs +17 -0
- package/dist/DeFiPositionsController/fetch-positions.mjs.map +1 -0
- package/dist/DeFiPositionsController/group-defi-positions.cjs +96 -0
- package/dist/DeFiPositionsController/group-defi-positions.cjs.map +1 -0
- package/dist/DeFiPositionsController/group-defi-positions.d.cts +36 -0
- package/dist/DeFiPositionsController/group-defi-positions.d.cts.map +1 -0
- package/dist/DeFiPositionsController/group-defi-positions.d.mts +36 -0
- package/dist/DeFiPositionsController/group-defi-positions.d.mts.map +1 -0
- package/dist/DeFiPositionsController/group-defi-positions.mjs +92 -0
- package/dist/DeFiPositionsController/group-defi-positions.mjs.map +1 -0
- package/dist/MultichainAssetsRatesController/MultichainAssetsRatesController.cjs +57 -4
- package/dist/MultichainAssetsRatesController/MultichainAssetsRatesController.cjs.map +1 -1
- package/dist/MultichainAssetsRatesController/MultichainAssetsRatesController.d.cts +16 -3
- package/dist/MultichainAssetsRatesController/MultichainAssetsRatesController.d.cts.map +1 -1
- package/dist/MultichainAssetsRatesController/MultichainAssetsRatesController.d.mts +16 -3
- package/dist/MultichainAssetsRatesController/MultichainAssetsRatesController.d.mts.map +1 -1
- package/dist/MultichainAssetsRatesController/MultichainAssetsRatesController.mjs +57 -4
- package/dist/MultichainAssetsRatesController/MultichainAssetsRatesController.mjs.map +1 -1
- package/dist/index.cjs +3 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -0
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +3 -0
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +1 -0
- package/dist/index.mjs.map +1 -1
- package/package.json +10 -8
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,40 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [58.0.0]
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
|
|
14
|
+
- Added `includeMarketData` to the params of the `OnAssetsConversion` handler ([#5639](https://github.com/MetaMask/core/pull/5639))
|
|
15
|
+
- Added `fetchHistoricalPricesForAsset` method to `MultichainAssetsRatesController` ([#5639](https://github.com/MetaMask/core/pull/5639))
|
|
16
|
+
- Added `getSelectedMultichainAccount` action to `multichainAssetsRatesController` ([#5639](https://github.com/MetaMask/core/pull/5639))
|
|
17
|
+
- Added new state field `historicalPrices` to `MultichainAssetsRatesController` ([#5639](https://github.com/MetaMask/core/pull/5639))
|
|
18
|
+
|
|
19
|
+
### Changed
|
|
20
|
+
|
|
21
|
+
- **BREAKING:** Bump `@metamask/snaps-controllers` peer dependency from ^9.19.0 to ^11.0.0 ([#5639](https://github.com/MetaMask/core/pull/5639))
|
|
22
|
+
- **BREAKING:** Bump `@metamask/providers` peer dependency from ^18.1.0 to ^21.0.0 ([#5639](https://github.com/MetaMask/core/pull/5639))
|
|
23
|
+
- Bump `@metamask/snaps-utils` from ^8.10.0 to ^9.2.0 ([#5639](https://github.com/MetaMask/core/pull/5639))
|
|
24
|
+
|
|
25
|
+
## [57.0.0]
|
|
26
|
+
|
|
27
|
+
### Added
|
|
28
|
+
|
|
29
|
+
- Add a new `DeFiPositionsController` that maintains an updated list of DeFi positions for EVM accounts ([#5400](https://github.com/MetaMask/core/pull/5400))
|
|
30
|
+
- Export `DeFiPositionsController`
|
|
31
|
+
- Export the following types
|
|
32
|
+
- `DeFiPositionsControllerState`
|
|
33
|
+
- `DeFiPositionsControllerActions`
|
|
34
|
+
- `DeFiPositionsControllerEvents`
|
|
35
|
+
- `DeFiPositionsControllerGetStateAction`
|
|
36
|
+
- `DeFiPositionsControllerStateChangeEvent`
|
|
37
|
+
- `DeFiPositionsControllerMessenger`
|
|
38
|
+
- `GroupedDeFiPositions`
|
|
39
|
+
|
|
40
|
+
### Changed
|
|
41
|
+
|
|
42
|
+
- **BREAKING** Add `@metamask/transaction-controller` as a peer dependency at `^54.0.0` ([#5400](https://github.com/MetaMask/core/pull/5400))
|
|
43
|
+
|
|
10
44
|
## [56.0.0]
|
|
11
45
|
|
|
12
46
|
### Changed
|
|
@@ -1520,7 +1554,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
1520
1554
|
|
|
1521
1555
|
- Use Ethers for AssetsContractController ([#845](https://github.com/MetaMask/core/pull/845))
|
|
1522
1556
|
|
|
1523
|
-
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@
|
|
1557
|
+
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@58.0.0...HEAD
|
|
1558
|
+
[58.0.0]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@57.0.0...@metamask/assets-controllers@58.0.0
|
|
1559
|
+
[57.0.0]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@56.0.0...@metamask/assets-controllers@57.0.0
|
|
1524
1560
|
[56.0.0]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@55.0.1...@metamask/assets-controllers@56.0.0
|
|
1525
1561
|
[55.0.1]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@55.0.0...@metamask/assets-controllers@55.0.1
|
|
1526
1562
|
[55.0.0]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@54.0.0...@metamask/assets-controllers@55.0.0
|
package/README.md
CHANGED
|
@@ -19,6 +19,7 @@ This package features the following controllers:
|
|
|
19
19
|
- [**CollectibleDetectionController**](src/CollectibleDetectionController.ts) keeps a periodically updated list of ERC-721 tokens assigned to the currently selected address.
|
|
20
20
|
- [**CollectiblesController**](src/CollectiblesController.ts) tracks ERC-721 and ERC-1155 tokens assigned to the currently selected address, using OpenSea to retrieve token information.
|
|
21
21
|
- [**CurrencyRateController**](src/CurrencyRateController.ts) keeps a periodically updated value of the exchange rate from the currently selected "native" currency to another (handling testnet tokens specially).
|
|
22
|
+
- [**DeFiPositionsController**](src/DeFiPositionsController/DeFiPositionsController.ts.ts) keeps a periodically updated value of the DeFi positions for the owner EVM addresses.
|
|
22
23
|
- [**RatesController**](src/RatesController/RatesController.ts) keeps a periodically updated value for the exchange rates for different cryptocurrencies. The difference between the `RatesController` and `CurrencyRateController` is that the second one is coupled to the `NetworksController` and is EVM specific, whilst the first one can handle different blockchain currencies like BTC and SOL.
|
|
23
24
|
- [**TokenBalancesController**](src/TokenBalancesController.ts) keeps a periodically updated set of balances for the current set of ERC-20 tokens.
|
|
24
25
|
- [**TokenDetectionController**](src/TokenDetectionController.ts) keeps a periodically updated list of ERC-20 tokens assigned to the currently selected address.
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
3
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
4
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
5
|
+
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
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
7
|
+
};
|
|
8
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
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 _DeFiPositionsController_instances, _DeFiPositionsController_fetchPositions, _DeFiPositionsController_isEnabled, _DeFiPositionsController_updateAccountPositions, _DeFiPositionsController_fetchAccountPositions;
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.DeFiPositionsController = exports.getDefaultDefiPositionsControllerState = void 0;
|
|
16
|
+
const polling_controller_1 = require("@metamask/polling-controller");
|
|
17
|
+
const fetch_positions_1 = require("./fetch-positions.cjs");
|
|
18
|
+
const group_defi_positions_1 = require("./group-defi-positions.cjs");
|
|
19
|
+
const assetsUtil_1 = require("../assetsUtil.cjs");
|
|
20
|
+
const TEN_MINUTES_IN_MS = 60000;
|
|
21
|
+
const FETCH_POSITIONS_BATCH_SIZE = 10;
|
|
22
|
+
const controllerName = 'DeFiPositionsController';
|
|
23
|
+
const controllerMetadata = {
|
|
24
|
+
allDeFiPositions: {
|
|
25
|
+
persist: false,
|
|
26
|
+
anonymous: false,
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
const getDefaultDefiPositionsControllerState = () => {
|
|
30
|
+
return {
|
|
31
|
+
allDeFiPositions: {},
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
exports.getDefaultDefiPositionsControllerState = getDefaultDefiPositionsControllerState;
|
|
35
|
+
/**
|
|
36
|
+
* Controller that stores assets and exposes convenience methods
|
|
37
|
+
*/
|
|
38
|
+
class DeFiPositionsController extends (0, polling_controller_1.StaticIntervalPollingController)() {
|
|
39
|
+
/**
|
|
40
|
+
* DeFiPositionsController constuctor
|
|
41
|
+
*
|
|
42
|
+
* @param options - Constructor options.
|
|
43
|
+
* @param options.messenger - The controller messenger.
|
|
44
|
+
* @param options.isEnabled - Function that returns whether the controller is enabled. (default: () => true)
|
|
45
|
+
*/
|
|
46
|
+
constructor({ messenger, isEnabled = () => true, }) {
|
|
47
|
+
super({
|
|
48
|
+
name: controllerName,
|
|
49
|
+
metadata: controllerMetadata,
|
|
50
|
+
messenger,
|
|
51
|
+
state: (0, exports.getDefaultDefiPositionsControllerState)(),
|
|
52
|
+
});
|
|
53
|
+
_DeFiPositionsController_instances.add(this);
|
|
54
|
+
_DeFiPositionsController_fetchPositions.set(this, void 0);
|
|
55
|
+
_DeFiPositionsController_isEnabled.set(this, void 0);
|
|
56
|
+
this.setIntervalLength(TEN_MINUTES_IN_MS);
|
|
57
|
+
__classPrivateFieldSet(this, _DeFiPositionsController_fetchPositions, (0, fetch_positions_1.buildPositionFetcher)(), "f");
|
|
58
|
+
__classPrivateFieldSet(this, _DeFiPositionsController_isEnabled, isEnabled, "f");
|
|
59
|
+
this.messagingSystem.subscribe('KeyringController:unlock', () => {
|
|
60
|
+
this.startPolling(null);
|
|
61
|
+
});
|
|
62
|
+
this.messagingSystem.subscribe('KeyringController:lock', () => {
|
|
63
|
+
this.stopAllPolling();
|
|
64
|
+
});
|
|
65
|
+
this.messagingSystem.subscribe('TransactionController:transactionConfirmed', async (transactionMeta) => {
|
|
66
|
+
if (!__classPrivateFieldGet(this, _DeFiPositionsController_isEnabled, "f").call(this)) {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
await __classPrivateFieldGet(this, _DeFiPositionsController_instances, "m", _DeFiPositionsController_updateAccountPositions).call(this, transactionMeta.txParams.from);
|
|
70
|
+
});
|
|
71
|
+
this.messagingSystem.subscribe('AccountsController:accountAdded', async (account) => {
|
|
72
|
+
if (!__classPrivateFieldGet(this, _DeFiPositionsController_isEnabled, "f").call(this) || !account.type.startsWith('eip155:')) {
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
await __classPrivateFieldGet(this, _DeFiPositionsController_instances, "m", _DeFiPositionsController_updateAccountPositions).call(this, account.address);
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
async _executePoll() {
|
|
79
|
+
if (!__classPrivateFieldGet(this, _DeFiPositionsController_isEnabled, "f").call(this)) {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
const accounts = this.messagingSystem.call('AccountsController:listAccounts');
|
|
83
|
+
const initialResult = [];
|
|
84
|
+
const results = await (0, assetsUtil_1.reduceInBatchesSerially)({
|
|
85
|
+
initialResult,
|
|
86
|
+
values: accounts,
|
|
87
|
+
batchSize: FETCH_POSITIONS_BATCH_SIZE,
|
|
88
|
+
eachBatch: async (workingResult, batch) => {
|
|
89
|
+
const batchResults = (await Promise.all(batch.map(async ({ address: accountAddress, type }) => {
|
|
90
|
+
if (type.startsWith('eip155:')) {
|
|
91
|
+
const positions = await __classPrivateFieldGet(this, _DeFiPositionsController_instances, "m", _DeFiPositionsController_fetchAccountPositions).call(this, accountAddress);
|
|
92
|
+
return {
|
|
93
|
+
accountAddress,
|
|
94
|
+
positions,
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
return undefined;
|
|
98
|
+
}))).filter(Boolean);
|
|
99
|
+
return [...workingResult, ...batchResults];
|
|
100
|
+
},
|
|
101
|
+
});
|
|
102
|
+
const allDefiPositions = results.reduce((acc, { accountAddress, positions }) => {
|
|
103
|
+
acc[accountAddress] = positions;
|
|
104
|
+
return acc;
|
|
105
|
+
}, {});
|
|
106
|
+
this.update((state) => {
|
|
107
|
+
state.allDeFiPositions = allDefiPositions;
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
exports.DeFiPositionsController = DeFiPositionsController;
|
|
112
|
+
_DeFiPositionsController_fetchPositions = new WeakMap(), _DeFiPositionsController_isEnabled = new WeakMap(), _DeFiPositionsController_instances = new WeakSet(), _DeFiPositionsController_updateAccountPositions = async function _DeFiPositionsController_updateAccountPositions(accountAddress) {
|
|
113
|
+
const accountPositionsPerChain = await __classPrivateFieldGet(this, _DeFiPositionsController_instances, "m", _DeFiPositionsController_fetchAccountPositions).call(this, accountAddress);
|
|
114
|
+
this.update((state) => {
|
|
115
|
+
state.allDeFiPositions[accountAddress] = accountPositionsPerChain;
|
|
116
|
+
});
|
|
117
|
+
}, _DeFiPositionsController_fetchAccountPositions = async function _DeFiPositionsController_fetchAccountPositions(accountAddress) {
|
|
118
|
+
try {
|
|
119
|
+
const defiPositionsResponse = await __classPrivateFieldGet(this, _DeFiPositionsController_fetchPositions, "f").call(this, accountAddress);
|
|
120
|
+
return (0, group_defi_positions_1.groupDeFiPositions)(defiPositionsResponse);
|
|
121
|
+
}
|
|
122
|
+
catch {
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
//# sourceMappingURL=DeFiPositionsController.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"DeFiPositionsController.cjs","sourceRoot":"","sources":["../../src/DeFiPositionsController/DeFiPositionsController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAYA,qEAA+E;AAK/E,2DAAyD;AACzD,qEAGgC;AAChC,kDAAwD;AAExD,MAAM,iBAAiB,GAAG,KAAM,CAAC;AAEjC,MAAM,0BAA0B,GAAG,EAAE,CAAC;AAEtC,MAAM,cAAc,GAAG,yBAAyB,CAAC;AAejD,MAAM,kBAAkB,GAAgD;IACtE,gBAAgB,EAAE;QAChB,OAAO,EAAE,KAAK;QACd,SAAS,EAAE,KAAK;KACjB;CACF,CAAC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MAAM,gBAAgB,GAAG,OAAO,CAAC,MAAM,CACrC,CAAC,GAAG,EAAE,EAAE,cAAc,EAAE,SAAS,EAAE,EAAE,EAAE;YACrC,GAAG,CAAC,cAAc,CAAC,GAAG,SAAS,CAAC;YAChC,OAAO,GAAG,CAAC;QACb,CAAC,EACD,EAAsD,CACvD,CAAC;QAEF,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,KAAK,CAAC,gBAAgB,GAAG,gBAAgB,CAAC;QAC5C,CAAC,CAAC,CAAC;IACL,CAAC;CAsBF;AAjJD,0DAiJC;mNApBC,KAAK,0DAAyB,cAAsB;IAClD,MAAM,wBAAwB,GAC5B,MAAM,uBAAA,IAAI,0FAAuB,MAA3B,IAAI,EAAwB,cAAc,CAAC,CAAC;IAEpD,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;QACpB,KAAK,CAAC,gBAAgB,CAAC,cAAc,CAAC,GAAG,wBAAwB,CAAC;IACpE,CAAC,CAAC,CAAC;AACL,CAAC,mDAED,KAAK,yDACH,cAAsB;IAEtB,IAAI;QACF,MAAM,qBAAqB,GAAG,MAAM,uBAAA,IAAI,+CAAgB,MAApB,IAAI,EAAiB,cAAc,CAAC,CAAC;QAEzE,OAAO,IAAA,yCAAkB,EAAC,qBAAqB,CAAC,CAAC;KAClD;IAAC,MAAM;QACN,OAAO,IAAI,CAAC;KACb;AACH,CAAC","sourcesContent":["import type {\n AccountsControllerAccountAddedEvent,\n AccountsControllerListAccountsAction,\n} from '@metamask/accounts-controller';\nimport type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n RestrictedMessenger,\n StateMetadata,\n} from '@metamask/base-controller';\nimport type { KeyringControllerUnlockEvent } from '@metamask/keyring-controller';\nimport type { KeyringControllerLockEvent } from '@metamask/keyring-controller';\nimport { StaticIntervalPollingController } from '@metamask/polling-controller';\nimport type { TransactionControllerTransactionConfirmedEvent } from '@metamask/transaction-controller';\nimport type { Hex } from '@metamask/utils';\n\nimport type { DefiPositionResponse } from './fetch-positions';\nimport { buildPositionFetcher } from './fetch-positions';\nimport {\n groupDeFiPositions,\n type GroupedDeFiPositions,\n} from './group-defi-positions';\nimport { reduceInBatchesSerially } from '../assetsUtil';\n\nconst TEN_MINUTES_IN_MS = 60_000;\n\nconst FETCH_POSITIONS_BATCH_SIZE = 10;\n\nconst controllerName = 'DeFiPositionsController';\n\ntype GroupedDeFiPositionsPerChain = {\n [chain: Hex]: GroupedDeFiPositions;\n};\n\nexport type DeFiPositionsControllerState = {\n /**\n * Object containing DeFi positions per account and network\n */\n allDeFiPositions: {\n [accountAddress: string]: GroupedDeFiPositionsPerChain | null;\n };\n};\n\nconst controllerMetadata: StateMetadata<DeFiPositionsControllerState> = {\n allDeFiPositions: {\n persist: false,\n anonymous: false,\n },\n};\n\nexport const getDefaultDefiPositionsControllerState =\n (): DeFiPositionsControllerState => {\n return {\n allDeFiPositions: {},\n };\n };\n\nexport type DeFiPositionsControllerActions =\n DeFiPositionsControllerGetStateAction;\n\nexport type DeFiPositionsControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n DeFiPositionsControllerState\n>;\n\nexport type DeFiPositionsControllerEvents =\n DeFiPositionsControllerStateChangeEvent;\n\nexport type DeFiPositionsControllerStateChangeEvent =\n ControllerStateChangeEvent<\n typeof controllerName,\n DeFiPositionsControllerState\n >;\n\n/**\n * The external actions available to the {@link DeFiPositionsController}.\n */\nexport type AllowedActions = AccountsControllerListAccountsAction;\n\n/**\n * The external events available to the {@link DeFiPositionsController}.\n */\nexport type AllowedEvents =\n | KeyringControllerUnlockEvent\n | KeyringControllerLockEvent\n | TransactionControllerTransactionConfirmedEvent\n | AccountsControllerAccountAddedEvent;\n\n/**\n * The messenger of the {@link DeFiPositionsController}.\n */\nexport type DeFiPositionsControllerMessenger = RestrictedMessenger<\n typeof controllerName,\n DeFiPositionsControllerActions | AllowedActions,\n DeFiPositionsControllerEvents | AllowedEvents,\n AllowedActions['type'],\n AllowedEvents['type']\n>;\n\n/**\n * Controller that stores assets and exposes convenience methods\n */\nexport class DeFiPositionsController extends StaticIntervalPollingController()<\n typeof controllerName,\n DeFiPositionsControllerState,\n DeFiPositionsControllerMessenger\n> {\n readonly #fetchPositions: (\n accountAddress: string,\n ) => Promise<DefiPositionResponse[]>;\n\n readonly #isEnabled: () => boolean;\n\n /**\n * DeFiPositionsController constuctor\n *\n * @param options - Constructor options.\n * @param options.messenger - The controller messenger.\n * @param options.isEnabled - Function that returns whether the controller is enabled. (default: () => true)\n */\n constructor({\n messenger,\n isEnabled = () => true,\n }: {\n messenger: DeFiPositionsControllerMessenger;\n isEnabled?: () => boolean;\n }) {\n super({\n name: controllerName,\n metadata: controllerMetadata,\n messenger,\n state: getDefaultDefiPositionsControllerState(),\n });\n\n this.setIntervalLength(TEN_MINUTES_IN_MS);\n\n this.#fetchPositions = buildPositionFetcher();\n this.#isEnabled = isEnabled;\n\n this.messagingSystem.subscribe('KeyringController:unlock', () => {\n this.startPolling(null);\n });\n\n this.messagingSystem.subscribe('KeyringController:lock', () => {\n this.stopAllPolling();\n });\n\n this.messagingSystem.subscribe(\n 'TransactionController:transactionConfirmed',\n async (transactionMeta) => {\n if (!this.#isEnabled()) {\n return;\n }\n\n await this.#updateAccountPositions(transactionMeta.txParams.from);\n },\n );\n\n this.messagingSystem.subscribe(\n 'AccountsController:accountAdded',\n async (account) => {\n if (!this.#isEnabled() || !account.type.startsWith('eip155:')) {\n return;\n }\n\n await this.#updateAccountPositions(account.address);\n },\n );\n }\n\n async _executePoll(): Promise<void> {\n if (!this.#isEnabled()) {\n return;\n }\n\n const accounts = this.messagingSystem.call(\n 'AccountsController:listAccounts',\n );\n\n const initialResult: {\n accountAddress: string;\n positions: GroupedDeFiPositionsPerChain | null;\n }[] = [];\n\n const results = await reduceInBatchesSerially({\n initialResult,\n values: accounts,\n batchSize: FETCH_POSITIONS_BATCH_SIZE,\n eachBatch: async (workingResult, batch) => {\n const batchResults = (\n await Promise.all(\n batch.map(async ({ address: accountAddress, type }) => {\n if (type.startsWith('eip155:')) {\n const positions =\n await this.#fetchAccountPositions(accountAddress);\n\n return {\n accountAddress,\n positions,\n };\n }\n\n return undefined;\n }),\n )\n ).filter(Boolean) as {\n accountAddress: string;\n positions: GroupedDeFiPositionsPerChain | null;\n }[];\n\n return [...workingResult, ...batchResults];\n },\n });\n\n const allDefiPositions = results.reduce(\n (acc, { accountAddress, positions }) => {\n acc[accountAddress] = positions;\n return acc;\n },\n {} as DeFiPositionsControllerState['allDeFiPositions'],\n );\n\n this.update((state) => {\n state.allDeFiPositions = allDefiPositions;\n });\n }\n\n async #updateAccountPositions(accountAddress: string): Promise<void> {\n const accountPositionsPerChain =\n await this.#fetchAccountPositions(accountAddress);\n\n this.update((state) => {\n state.allDeFiPositions[accountAddress] = accountPositionsPerChain;\n });\n }\n\n async #fetchAccountPositions(\n accountAddress: string,\n ): Promise<GroupedDeFiPositionsPerChain | null> {\n try {\n const defiPositionsResponse = await this.#fetchPositions(accountAddress);\n\n return groupDeFiPositions(defiPositionsResponse);\n } catch {\n return null;\n }\n }\n}\n"]}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import type { AccountsControllerAccountAddedEvent, AccountsControllerListAccountsAction } from "@metamask/accounts-controller";
|
|
3
|
+
import type { ControllerGetStateAction, ControllerStateChangeEvent, RestrictedMessenger } from "@metamask/base-controller";
|
|
4
|
+
import type { KeyringControllerUnlockEvent } from "@metamask/keyring-controller";
|
|
5
|
+
import type { KeyringControllerLockEvent } from "@metamask/keyring-controller";
|
|
6
|
+
import type { TransactionControllerTransactionConfirmedEvent } from "@metamask/transaction-controller";
|
|
7
|
+
import type { Hex } from "@metamask/utils";
|
|
8
|
+
import { type GroupedDeFiPositions } from "./group-defi-positions.cjs";
|
|
9
|
+
declare const controllerName = "DeFiPositionsController";
|
|
10
|
+
type GroupedDeFiPositionsPerChain = {
|
|
11
|
+
[chain: Hex]: GroupedDeFiPositions;
|
|
12
|
+
};
|
|
13
|
+
export type DeFiPositionsControllerState = {
|
|
14
|
+
/**
|
|
15
|
+
* Object containing DeFi positions per account and network
|
|
16
|
+
*/
|
|
17
|
+
allDeFiPositions: {
|
|
18
|
+
[accountAddress: string]: GroupedDeFiPositionsPerChain | null;
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
export declare const getDefaultDefiPositionsControllerState: () => DeFiPositionsControllerState;
|
|
22
|
+
export type DeFiPositionsControllerActions = DeFiPositionsControllerGetStateAction;
|
|
23
|
+
export type DeFiPositionsControllerGetStateAction = ControllerGetStateAction<typeof controllerName, DeFiPositionsControllerState>;
|
|
24
|
+
export type DeFiPositionsControllerEvents = DeFiPositionsControllerStateChangeEvent;
|
|
25
|
+
export type DeFiPositionsControllerStateChangeEvent = ControllerStateChangeEvent<typeof controllerName, DeFiPositionsControllerState>;
|
|
26
|
+
/**
|
|
27
|
+
* The external actions available to the {@link DeFiPositionsController}.
|
|
28
|
+
*/
|
|
29
|
+
export type AllowedActions = AccountsControllerListAccountsAction;
|
|
30
|
+
/**
|
|
31
|
+
* The external events available to the {@link DeFiPositionsController}.
|
|
32
|
+
*/
|
|
33
|
+
export type AllowedEvents = KeyringControllerUnlockEvent | KeyringControllerLockEvent | TransactionControllerTransactionConfirmedEvent | AccountsControllerAccountAddedEvent;
|
|
34
|
+
/**
|
|
35
|
+
* The messenger of the {@link DeFiPositionsController}.
|
|
36
|
+
*/
|
|
37
|
+
export type DeFiPositionsControllerMessenger = RestrictedMessenger<typeof controllerName, DeFiPositionsControllerActions | AllowedActions, DeFiPositionsControllerEvents | AllowedEvents, AllowedActions['type'], AllowedEvents['type']>;
|
|
38
|
+
declare const DeFiPositionsController_base: (abstract new (...args: any[]) => {
|
|
39
|
+
readonly "__#14@#intervalIds": Record<string, NodeJS.Timeout>;
|
|
40
|
+
"__#14@#intervalLength": number | undefined;
|
|
41
|
+
setIntervalLength(intervalLength: number): void;
|
|
42
|
+
getIntervalLength(): number | undefined;
|
|
43
|
+
_startPolling(input: import("@metamask/utils").Json): void;
|
|
44
|
+
_stopPollingByPollingTokenSetId(key: string): void;
|
|
45
|
+
readonly "__#3@#pollingTokenSets": Map<string, Set<string>>;
|
|
46
|
+
"__#3@#callbacks": Map<string, Set<(input: import("@metamask/utils").Json) => void>>;
|
|
47
|
+
_executePoll(input: import("@metamask/utils").Json): Promise<void>;
|
|
48
|
+
startPolling(input: import("@metamask/utils").Json): string;
|
|
49
|
+
stopAllPolling(): void;
|
|
50
|
+
stopPollingByPollingToken(pollingToken: string): void;
|
|
51
|
+
onPollingComplete(input: import("@metamask/utils").Json, callback: (input: import("@metamask/utils").Json) => void): void;
|
|
52
|
+
}) & typeof import("@metamask/base-controller").BaseController;
|
|
53
|
+
/**
|
|
54
|
+
* Controller that stores assets and exposes convenience methods
|
|
55
|
+
*/
|
|
56
|
+
export declare class DeFiPositionsController extends DeFiPositionsController_base<typeof controllerName, DeFiPositionsControllerState, DeFiPositionsControllerMessenger> {
|
|
57
|
+
#private;
|
|
58
|
+
/**
|
|
59
|
+
* DeFiPositionsController constuctor
|
|
60
|
+
*
|
|
61
|
+
* @param options - Constructor options.
|
|
62
|
+
* @param options.messenger - The controller messenger.
|
|
63
|
+
* @param options.isEnabled - Function that returns whether the controller is enabled. (default: () => true)
|
|
64
|
+
*/
|
|
65
|
+
constructor({ messenger, isEnabled, }: {
|
|
66
|
+
messenger: DeFiPositionsControllerMessenger;
|
|
67
|
+
isEnabled?: () => boolean;
|
|
68
|
+
});
|
|
69
|
+
_executePoll(): Promise<void>;
|
|
70
|
+
}
|
|
71
|
+
export {};
|
|
72
|
+
//# sourceMappingURL=DeFiPositionsController.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"DeFiPositionsController.d.cts","sourceRoot":"","sources":["../../src/DeFiPositionsController/DeFiPositionsController.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,EACV,mCAAmC,EACnC,oCAAoC,EACrC,sC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C,EACtB,aAAa,CAAC,MAAM,CAAC,CACtB,CAAC;;;;;;;;;;;;;;;;AAEF;;GAEG;AACH,qBAAa,uBAAwB,SAAQ,6BAC3C,OAAO,cAAc,EACrB,4BAA4B,EAC5B,gCAAgC,CACjC;;IAOC;;;;;;OAMG;gBACS,EACV,SAAS,EACT,SAAsB,GACvB,EAAE;QACD,SAAS,EAAE,gCAAgC,CAAC;QAC5C,SAAS,CAAC,EAAE,MAAM,OAAO,CAAC;KAC3B;IA4CK,YAAY,IAAI,OAAO,CAAC,IAAI,CAAC;CA6EpC"}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import type { AccountsControllerAccountAddedEvent, AccountsControllerListAccountsAction } from "@metamask/accounts-controller";
|
|
3
|
+
import type { ControllerGetStateAction, ControllerStateChangeEvent, RestrictedMessenger } from "@metamask/base-controller";
|
|
4
|
+
import type { KeyringControllerUnlockEvent } from "@metamask/keyring-controller";
|
|
5
|
+
import type { KeyringControllerLockEvent } from "@metamask/keyring-controller";
|
|
6
|
+
import type { TransactionControllerTransactionConfirmedEvent } from "@metamask/transaction-controller";
|
|
7
|
+
import type { Hex } from "@metamask/utils";
|
|
8
|
+
import { type GroupedDeFiPositions } from "./group-defi-positions.mjs";
|
|
9
|
+
declare const controllerName = "DeFiPositionsController";
|
|
10
|
+
type GroupedDeFiPositionsPerChain = {
|
|
11
|
+
[chain: Hex]: GroupedDeFiPositions;
|
|
12
|
+
};
|
|
13
|
+
export type DeFiPositionsControllerState = {
|
|
14
|
+
/**
|
|
15
|
+
* Object containing DeFi positions per account and network
|
|
16
|
+
*/
|
|
17
|
+
allDeFiPositions: {
|
|
18
|
+
[accountAddress: string]: GroupedDeFiPositionsPerChain | null;
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
export declare const getDefaultDefiPositionsControllerState: () => DeFiPositionsControllerState;
|
|
22
|
+
export type DeFiPositionsControllerActions = DeFiPositionsControllerGetStateAction;
|
|
23
|
+
export type DeFiPositionsControllerGetStateAction = ControllerGetStateAction<typeof controllerName, DeFiPositionsControllerState>;
|
|
24
|
+
export type DeFiPositionsControllerEvents = DeFiPositionsControllerStateChangeEvent;
|
|
25
|
+
export type DeFiPositionsControllerStateChangeEvent = ControllerStateChangeEvent<typeof controllerName, DeFiPositionsControllerState>;
|
|
26
|
+
/**
|
|
27
|
+
* The external actions available to the {@link DeFiPositionsController}.
|
|
28
|
+
*/
|
|
29
|
+
export type AllowedActions = AccountsControllerListAccountsAction;
|
|
30
|
+
/**
|
|
31
|
+
* The external events available to the {@link DeFiPositionsController}.
|
|
32
|
+
*/
|
|
33
|
+
export type AllowedEvents = KeyringControllerUnlockEvent | KeyringControllerLockEvent | TransactionControllerTransactionConfirmedEvent | AccountsControllerAccountAddedEvent;
|
|
34
|
+
/**
|
|
35
|
+
* The messenger of the {@link DeFiPositionsController}.
|
|
36
|
+
*/
|
|
37
|
+
export type DeFiPositionsControllerMessenger = RestrictedMessenger<typeof controllerName, DeFiPositionsControllerActions | AllowedActions, DeFiPositionsControllerEvents | AllowedEvents, AllowedActions['type'], AllowedEvents['type']>;
|
|
38
|
+
declare const DeFiPositionsController_base: (abstract new (...args: any[]) => {
|
|
39
|
+
readonly "__#14@#intervalIds": Record<string, NodeJS.Timeout>;
|
|
40
|
+
"__#14@#intervalLength": number | undefined;
|
|
41
|
+
setIntervalLength(intervalLength: number): void;
|
|
42
|
+
getIntervalLength(): number | undefined;
|
|
43
|
+
_startPolling(input: import("@metamask/utils").Json): void;
|
|
44
|
+
_stopPollingByPollingTokenSetId(key: string): void;
|
|
45
|
+
readonly "__#3@#pollingTokenSets": Map<string, Set<string>>;
|
|
46
|
+
"__#3@#callbacks": Map<string, Set<(input: import("@metamask/utils").Json) => void>>;
|
|
47
|
+
_executePoll(input: import("@metamask/utils").Json): Promise<void>;
|
|
48
|
+
startPolling(input: import("@metamask/utils").Json): string;
|
|
49
|
+
stopAllPolling(): void;
|
|
50
|
+
stopPollingByPollingToken(pollingToken: string): void;
|
|
51
|
+
onPollingComplete(input: import("@metamask/utils").Json, callback: (input: import("@metamask/utils").Json) => void): void;
|
|
52
|
+
}) & typeof import("@metamask/base-controller").BaseController;
|
|
53
|
+
/**
|
|
54
|
+
* Controller that stores assets and exposes convenience methods
|
|
55
|
+
*/
|
|
56
|
+
export declare class DeFiPositionsController extends DeFiPositionsController_base<typeof controllerName, DeFiPositionsControllerState, DeFiPositionsControllerMessenger> {
|
|
57
|
+
#private;
|
|
58
|
+
/**
|
|
59
|
+
* DeFiPositionsController constuctor
|
|
60
|
+
*
|
|
61
|
+
* @param options - Constructor options.
|
|
62
|
+
* @param options.messenger - The controller messenger.
|
|
63
|
+
* @param options.isEnabled - Function that returns whether the controller is enabled. (default: () => true)
|
|
64
|
+
*/
|
|
65
|
+
constructor({ messenger, isEnabled, }: {
|
|
66
|
+
messenger: DeFiPositionsControllerMessenger;
|
|
67
|
+
isEnabled?: () => boolean;
|
|
68
|
+
});
|
|
69
|
+
_executePoll(): Promise<void>;
|
|
70
|
+
}
|
|
71
|
+
export {};
|
|
72
|
+
//# sourceMappingURL=DeFiPositionsController.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"DeFiPositionsController.d.mts","sourceRoot":"","sources":["../../src/DeFiPositionsController/DeFiPositionsController.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,EACV,mCAAmC,EACnC,oCAAoC,EACrC,sCAAsC;AACvC,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAC1B,mBAAmB,EAEpB,kCAAkC;AACnC,OAAO,KAAK,EAAE,4BAA4B,EAAE,qCAAqC;AACjF,OAAO,KAAK,EAAE,0BAA0B,EAAE,qCAAqC;AAE/E,OAAO,KAAK,EAAE,8CAA8C,EAAE,yCAAyC;AACvG,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAI3C,OAAO,EAEL,KAAK,oBAAoB,EAC1B,mCAA+B;AAOhC,QAAA,MA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AAI,CAAC;CA6EpC"}
|
|
@@ -0,0 +1,121 @@
|
|
|
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 _DeFiPositionsController_instances, _DeFiPositionsController_fetchPositions, _DeFiPositionsController_isEnabled, _DeFiPositionsController_updateAccountPositions, _DeFiPositionsController_fetchAccountPositions;
|
|
13
|
+
import { StaticIntervalPollingController } from "@metamask/polling-controller";
|
|
14
|
+
import { buildPositionFetcher } from "./fetch-positions.mjs";
|
|
15
|
+
import { groupDeFiPositions } from "./group-defi-positions.mjs";
|
|
16
|
+
import { reduceInBatchesSerially } from "../assetsUtil.mjs";
|
|
17
|
+
const TEN_MINUTES_IN_MS = 60000;
|
|
18
|
+
const FETCH_POSITIONS_BATCH_SIZE = 10;
|
|
19
|
+
const controllerName = 'DeFiPositionsController';
|
|
20
|
+
const controllerMetadata = {
|
|
21
|
+
allDeFiPositions: {
|
|
22
|
+
persist: false,
|
|
23
|
+
anonymous: false,
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
export const getDefaultDefiPositionsControllerState = () => {
|
|
27
|
+
return {
|
|
28
|
+
allDeFiPositions: {},
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* Controller that stores assets and exposes convenience methods
|
|
33
|
+
*/
|
|
34
|
+
export class DeFiPositionsController extends StaticIntervalPollingController() {
|
|
35
|
+
/**
|
|
36
|
+
* DeFiPositionsController constuctor
|
|
37
|
+
*
|
|
38
|
+
* @param options - Constructor options.
|
|
39
|
+
* @param options.messenger - The controller messenger.
|
|
40
|
+
* @param options.isEnabled - Function that returns whether the controller is enabled. (default: () => true)
|
|
41
|
+
*/
|
|
42
|
+
constructor({ messenger, isEnabled = () => true, }) {
|
|
43
|
+
super({
|
|
44
|
+
name: controllerName,
|
|
45
|
+
metadata: controllerMetadata,
|
|
46
|
+
messenger,
|
|
47
|
+
state: getDefaultDefiPositionsControllerState(),
|
|
48
|
+
});
|
|
49
|
+
_DeFiPositionsController_instances.add(this);
|
|
50
|
+
_DeFiPositionsController_fetchPositions.set(this, void 0);
|
|
51
|
+
_DeFiPositionsController_isEnabled.set(this, void 0);
|
|
52
|
+
this.setIntervalLength(TEN_MINUTES_IN_MS);
|
|
53
|
+
__classPrivateFieldSet(this, _DeFiPositionsController_fetchPositions, buildPositionFetcher(), "f");
|
|
54
|
+
__classPrivateFieldSet(this, _DeFiPositionsController_isEnabled, isEnabled, "f");
|
|
55
|
+
this.messagingSystem.subscribe('KeyringController:unlock', () => {
|
|
56
|
+
this.startPolling(null);
|
|
57
|
+
});
|
|
58
|
+
this.messagingSystem.subscribe('KeyringController:lock', () => {
|
|
59
|
+
this.stopAllPolling();
|
|
60
|
+
});
|
|
61
|
+
this.messagingSystem.subscribe('TransactionController:transactionConfirmed', async (transactionMeta) => {
|
|
62
|
+
if (!__classPrivateFieldGet(this, _DeFiPositionsController_isEnabled, "f").call(this)) {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
await __classPrivateFieldGet(this, _DeFiPositionsController_instances, "m", _DeFiPositionsController_updateAccountPositions).call(this, transactionMeta.txParams.from);
|
|
66
|
+
});
|
|
67
|
+
this.messagingSystem.subscribe('AccountsController:accountAdded', async (account) => {
|
|
68
|
+
if (!__classPrivateFieldGet(this, _DeFiPositionsController_isEnabled, "f").call(this) || !account.type.startsWith('eip155:')) {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
await __classPrivateFieldGet(this, _DeFiPositionsController_instances, "m", _DeFiPositionsController_updateAccountPositions).call(this, account.address);
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
async _executePoll() {
|
|
75
|
+
if (!__classPrivateFieldGet(this, _DeFiPositionsController_isEnabled, "f").call(this)) {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
const accounts = this.messagingSystem.call('AccountsController:listAccounts');
|
|
79
|
+
const initialResult = [];
|
|
80
|
+
const results = await reduceInBatchesSerially({
|
|
81
|
+
initialResult,
|
|
82
|
+
values: accounts,
|
|
83
|
+
batchSize: FETCH_POSITIONS_BATCH_SIZE,
|
|
84
|
+
eachBatch: async (workingResult, batch) => {
|
|
85
|
+
const batchResults = (await Promise.all(batch.map(async ({ address: accountAddress, type }) => {
|
|
86
|
+
if (type.startsWith('eip155:')) {
|
|
87
|
+
const positions = await __classPrivateFieldGet(this, _DeFiPositionsController_instances, "m", _DeFiPositionsController_fetchAccountPositions).call(this, accountAddress);
|
|
88
|
+
return {
|
|
89
|
+
accountAddress,
|
|
90
|
+
positions,
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
return undefined;
|
|
94
|
+
}))).filter(Boolean);
|
|
95
|
+
return [...workingResult, ...batchResults];
|
|
96
|
+
},
|
|
97
|
+
});
|
|
98
|
+
const allDefiPositions = results.reduce((acc, { accountAddress, positions }) => {
|
|
99
|
+
acc[accountAddress] = positions;
|
|
100
|
+
return acc;
|
|
101
|
+
}, {});
|
|
102
|
+
this.update((state) => {
|
|
103
|
+
state.allDeFiPositions = allDefiPositions;
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
_DeFiPositionsController_fetchPositions = new WeakMap(), _DeFiPositionsController_isEnabled = new WeakMap(), _DeFiPositionsController_instances = new WeakSet(), _DeFiPositionsController_updateAccountPositions = async function _DeFiPositionsController_updateAccountPositions(accountAddress) {
|
|
108
|
+
const accountPositionsPerChain = await __classPrivateFieldGet(this, _DeFiPositionsController_instances, "m", _DeFiPositionsController_fetchAccountPositions).call(this, accountAddress);
|
|
109
|
+
this.update((state) => {
|
|
110
|
+
state.allDeFiPositions[accountAddress] = accountPositionsPerChain;
|
|
111
|
+
});
|
|
112
|
+
}, _DeFiPositionsController_fetchAccountPositions = async function _DeFiPositionsController_fetchAccountPositions(accountAddress) {
|
|
113
|
+
try {
|
|
114
|
+
const defiPositionsResponse = await __classPrivateFieldGet(this, _DeFiPositionsController_fetchPositions, "f").call(this, accountAddress);
|
|
115
|
+
return groupDeFiPositions(defiPositionsResponse);
|
|
116
|
+
}
|
|
117
|
+
catch {
|
|
118
|
+
return null;
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
//# sourceMappingURL=DeFiPositionsController.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"DeFiPositionsController.mjs","sourceRoot":"","sources":["../../src/DeFiPositionsController/DeFiPositionsController.ts"],"names":[],"mappings":";;;;;;;;;;;;AAYA,OAAO,EAAE,+BAA+B,EAAE,qCAAqC;AAK/E,OAAO,EAAE,oBAAoB,EAAE,8BAA0B;AACzD,OAAO,EACL,kBAAkB,EAEnB,mCAA+B;AAChC,OAAO,EAAE,uBAAuB,EAAE,0BAAsB;AAExD,MAAM,iBAAiB,GAAG,KAAM,CAAC;AAEjC,MAAM,0BAA0B,GAAG,EAAE,CAAC;AAEtC,MAAM,cAAc,GAAG,yBAAyB,CAAC;AAejD,MAAM,kBAAkB,GAAgD;IACtE,gBAAgB,EAAE;QAChB,OAAO,EAAE,KAAK;QACd,SAAS,EAAE,KA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GAAG,OAAO,CAAC,MAAM,CACrC,CAAC,GAAG,EAAE,EAAE,cAAc,EAAE,SAAS,EAAE,EAAE,EAAE;YACrC,GAAG,CAAC,cAAc,CAAC,GAAG,SAAS,CAAC;YAChC,OAAO,GAAG,CAAC;QACb,CAAC,EACD,EAAsD,CACvD,CAAC;QAEF,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,KAAK,CAAC,gBAAgB,GAAG,gBAAgB,CAAC;QAC5C,CAAC,CAAC,CAAC;IACL,CAAC;CAsBF;mNApBC,KAAK,0DAAyB,cAAsB;IAClD,MAAM,wBAAwB,GAC5B,MAAM,uBAAA,IAAI,0FAAuB,MAA3B,IAAI,EAAwB,cAAc,CAAC,CAAC;IAEpD,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;QACpB,KAAK,CAAC,gBAAgB,CAAC,cAAc,CAAC,GAAG,wBAAwB,CAAC;IACpE,CAAC,CAAC,CAAC;AACL,CAAC,mDAED,KAAK,yDACH,cAAsB;IAEtB,IAAI;QACF,MAAM,qBAAqB,GAAG,MAAM,uBAAA,IAAI,+CAAgB,MAApB,IAAI,EAAiB,cAAc,CAAC,CAAC;QAEzE,OAAO,kBAAkB,CAAC,qBAAqB,CAAC,CAAC;KAClD;IAAC,MAAM;QACN,OAAO,IAAI,CAAC;KACb;AACH,CAAC","sourcesContent":["import type {\n AccountsControllerAccountAddedEvent,\n AccountsControllerListAccountsAction,\n} from '@metamask/accounts-controller';\nimport type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n RestrictedMessenger,\n StateMetadata,\n} from '@metamask/base-controller';\nimport type { KeyringControllerUnlockEvent } from '@metamask/keyring-controller';\nimport type { KeyringControllerLockEvent } from '@metamask/keyring-controller';\nimport { StaticIntervalPollingController } from '@metamask/polling-controller';\nimport type { TransactionControllerTransactionConfirmedEvent } from '@metamask/transaction-controller';\nimport type { Hex } from '@metamask/utils';\n\nimport type { DefiPositionResponse } from './fetch-positions';\nimport { buildPositionFetcher } from './fetch-positions';\nimport {\n groupDeFiPositions,\n type GroupedDeFiPositions,\n} from './group-defi-positions';\nimport { reduceInBatchesSerially } from '../assetsUtil';\n\nconst TEN_MINUTES_IN_MS = 60_000;\n\nconst FETCH_POSITIONS_BATCH_SIZE = 10;\n\nconst controllerName = 'DeFiPositionsController';\n\ntype GroupedDeFiPositionsPerChain = {\n [chain: Hex]: GroupedDeFiPositions;\n};\n\nexport type DeFiPositionsControllerState = {\n /**\n * Object containing DeFi positions per account and network\n */\n allDeFiPositions: {\n [accountAddress: string]: GroupedDeFiPositionsPerChain | null;\n };\n};\n\nconst controllerMetadata: StateMetadata<DeFiPositionsControllerState> = {\n allDeFiPositions: {\n persist: false,\n anonymous: false,\n },\n};\n\nexport const getDefaultDefiPositionsControllerState =\n (): DeFiPositionsControllerState => {\n return {\n allDeFiPositions: {},\n };\n };\n\nexport type DeFiPositionsControllerActions =\n DeFiPositionsControllerGetStateAction;\n\nexport type DeFiPositionsControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n DeFiPositionsControllerState\n>;\n\nexport type DeFiPositionsControllerEvents =\n DeFiPositionsControllerStateChangeEvent;\n\nexport type DeFiPositionsControllerStateChangeEvent =\n ControllerStateChangeEvent<\n typeof controllerName,\n DeFiPositionsControllerState\n >;\n\n/**\n * The external actions available to the {@link DeFiPositionsController}.\n */\nexport type AllowedActions = AccountsControllerListAccountsAction;\n\n/**\n * The external events available to the {@link DeFiPositionsController}.\n */\nexport type AllowedEvents =\n | KeyringControllerUnlockEvent\n | KeyringControllerLockEvent\n | TransactionControllerTransactionConfirmedEvent\n | AccountsControllerAccountAddedEvent;\n\n/**\n * The messenger of the {@link DeFiPositionsController}.\n */\nexport type DeFiPositionsControllerMessenger = RestrictedMessenger<\n typeof controllerName,\n DeFiPositionsControllerActions | AllowedActions,\n DeFiPositionsControllerEvents | AllowedEvents,\n AllowedActions['type'],\n AllowedEvents['type']\n>;\n\n/**\n * Controller that stores assets and exposes convenience methods\n */\nexport class DeFiPositionsController extends StaticIntervalPollingController()<\n typeof controllerName,\n DeFiPositionsControllerState,\n DeFiPositionsControllerMessenger\n> {\n readonly #fetchPositions: (\n accountAddress: string,\n ) => Promise<DefiPositionResponse[]>;\n\n readonly #isEnabled: () => boolean;\n\n /**\n * DeFiPositionsController constuctor\n *\n * @param options - Constructor options.\n * @param options.messenger - The controller messenger.\n * @param options.isEnabled - Function that returns whether the controller is enabled. (default: () => true)\n */\n constructor({\n messenger,\n isEnabled = () => true,\n }: {\n messenger: DeFiPositionsControllerMessenger;\n isEnabled?: () => boolean;\n }) {\n super({\n name: controllerName,\n metadata: controllerMetadata,\n messenger,\n state: getDefaultDefiPositionsControllerState(),\n });\n\n this.setIntervalLength(TEN_MINUTES_IN_MS);\n\n this.#fetchPositions = buildPositionFetcher();\n this.#isEnabled = isEnabled;\n\n this.messagingSystem.subscribe('KeyringController:unlock', () => {\n this.startPolling(null);\n });\n\n this.messagingSystem.subscribe('KeyringController:lock', () => {\n this.stopAllPolling();\n });\n\n this.messagingSystem.subscribe(\n 'TransactionController:transactionConfirmed',\n async (transactionMeta) => {\n if (!this.#isEnabled()) {\n return;\n }\n\n await this.#updateAccountPositions(transactionMeta.txParams.from);\n },\n );\n\n this.messagingSystem.subscribe(\n 'AccountsController:accountAdded',\n async (account) => {\n if (!this.#isEnabled() || !account.type.startsWith('eip155:')) {\n return;\n }\n\n await this.#updateAccountPositions(account.address);\n },\n );\n }\n\n async _executePoll(): Promise<void> {\n if (!this.#isEnabled()) {\n return;\n }\n\n const accounts = this.messagingSystem.call(\n 'AccountsController:listAccounts',\n );\n\n const initialResult: {\n accountAddress: string;\n positions: GroupedDeFiPositionsPerChain | null;\n }[] = [];\n\n const results = await reduceInBatchesSerially({\n initialResult,\n values: accounts,\n batchSize: FETCH_POSITIONS_BATCH_SIZE,\n eachBatch: async (workingResult, batch) => {\n const batchResults = (\n await Promise.all(\n batch.map(async ({ address: accountAddress, type }) => {\n if (type.startsWith('eip155:')) {\n const positions =\n await this.#fetchAccountPositions(accountAddress);\n\n return {\n accountAddress,\n positions,\n };\n }\n\n return undefined;\n }),\n )\n ).filter(Boolean) as {\n accountAddress: string;\n positions: GroupedDeFiPositionsPerChain | null;\n }[];\n\n return [...workingResult, ...batchResults];\n },\n });\n\n const allDefiPositions = results.reduce(\n (acc, { accountAddress, positions }) => {\n acc[accountAddress] = positions;\n return acc;\n },\n {} as DeFiPositionsControllerState['allDeFiPositions'],\n );\n\n this.update((state) => {\n state.allDeFiPositions = allDefiPositions;\n });\n }\n\n async #updateAccountPositions(accountAddress: string): Promise<void> {\n const accountPositionsPerChain =\n await this.#fetchAccountPositions(accountAddress);\n\n this.update((state) => {\n state.allDeFiPositions[accountAddress] = accountPositionsPerChain;\n });\n }\n\n async #fetchAccountPositions(\n accountAddress: string,\n ): Promise<GroupedDeFiPositionsPerChain | null> {\n try {\n const defiPositionsResponse = await this.#fetchPositions(accountAddress);\n\n return groupDeFiPositions(defiPositionsResponse);\n } catch {\n return null;\n }\n }\n}\n"]}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.buildPositionFetcher = exports.DEFI_POSITIONS_API_URL = void 0;
|
|
4
|
+
// TODO: Update with prod API URL when available
|
|
5
|
+
exports.DEFI_POSITIONS_API_URL = 'https://defiadapters.dev-api.cx.metamask.io';
|
|
6
|
+
/**
|
|
7
|
+
* Builds a function that fetches DeFi positions for a given account address
|
|
8
|
+
*
|
|
9
|
+
* @returns A function that fetches DeFi positions for a given account address
|
|
10
|
+
*/
|
|
11
|
+
function buildPositionFetcher() {
|
|
12
|
+
return async (accountAddress) => {
|
|
13
|
+
const defiPositionsResponse = await fetch(`${exports.DEFI_POSITIONS_API_URL}/positions/${accountAddress}`);
|
|
14
|
+
if (defiPositionsResponse.status !== 200) {
|
|
15
|
+
throw new Error(`Unable to fetch defi positions - HTTP ${defiPositionsResponse.status}`);
|
|
16
|
+
}
|
|
17
|
+
return (await defiPositionsResponse.json()).data;
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
exports.buildPositionFetcher = buildPositionFetcher;
|
|
21
|
+
//# sourceMappingURL=fetch-positions.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fetch-positions.cjs","sourceRoot":"","sources":["../../src/DeFiPositionsController/fetch-positions.ts"],"names":[],"mappings":";;;AAyDA,gDAAgD;AACnC,QAAA,sBAAsB,GACjC,6CAA6C,CAAC;AAEhD;;;;GAIG;AACH,SAAgB,oBAAoB;IAClC,OAAO,KAAK,EAAE,cAAsB,EAAmC,EAAE;QACvE,MAAM,qBAAqB,GAAG,MAAM,KAAK,CACvC,GAAG,8BAAsB,cAAc,cAAc,EAAE,CACxD,CAAC;QAEF,IAAI,qBAAqB,CAAC,MAAM,KAAK,GAAG,EAAE;YACxC,MAAM,IAAI,KAAK,CACb,yCAAyC,qBAAqB,CAAC,MAAM,EAAE,CACxE,CAAC;SACH;QAED,OAAO,CAAC,MAAM,qBAAqB,CAAC,IAAI,EAAE,CAAC,CAAC,IAAI,CAAC;IACnD,CAAC,CAAC;AACJ,CAAC;AAdD,oDAcC","sourcesContent":["export type DefiPositionResponse = AdapterResponse<{\n tokens: ProtocolToken[];\n}>;\n\ntype ProtocolDetails = {\n chainId: number;\n protocolId: string;\n productId: string;\n protocolDisplayName: string;\n name: string;\n description: string;\n iconUrl: string;\n siteUrl: string;\n positionType: PositionType;\n metadata?: {\n groupPositions?: boolean;\n };\n};\n\ntype AdapterResponse<ProtocolResponse> =\n | (ProtocolDetails & {\n chainName: string;\n } & (\n | (ProtocolResponse & { success: true })\n | (AdapterErrorResponse & { success: false })\n ))\n | (AdapterErrorResponse & { success: false });\n\ntype AdapterErrorResponse = {\n error: {\n message: string;\n };\n};\n\nexport type PositionType = 'supply' | 'borrow' | 'stake' | 'reward';\n\nexport type ProtocolToken = Balance & {\n type: 'protocol';\n tokenId?: string;\n};\n\nexport type Underlying = Balance & {\n type: 'underlying' | 'underlying-claimable';\n iconUrl: string;\n};\n\nexport type Balance = {\n address: string;\n name: string;\n symbol: string;\n decimals: number;\n balanceRaw: string;\n balance: number;\n price?: number;\n tokens?: Underlying[];\n};\n\n// TODO: Update with prod API URL when available\nexport const DEFI_POSITIONS_API_URL =\n 'https://defiadapters.dev-api.cx.metamask.io';\n\n/**\n * Builds a function that fetches DeFi positions for a given account address\n *\n * @returns A function that fetches DeFi positions for a given account address\n */\nexport function buildPositionFetcher() {\n return async (accountAddress: string): Promise<DefiPositionResponse[]> => {\n const defiPositionsResponse = await fetch(\n `${DEFI_POSITIONS_API_URL}/positions/${accountAddress}`,\n );\n\n if (defiPositionsResponse.status !== 200) {\n throw new Error(\n `Unable to fetch defi positions - HTTP ${defiPositionsResponse.status}`,\n );\n }\n\n return (await defiPositionsResponse.json()).data;\n };\n}\n"]}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
export type DefiPositionResponse = AdapterResponse<{
|
|
2
|
+
tokens: ProtocolToken[];
|
|
3
|
+
}>;
|
|
4
|
+
type ProtocolDetails = {
|
|
5
|
+
chainId: number;
|
|
6
|
+
protocolId: string;
|
|
7
|
+
productId: string;
|
|
8
|
+
protocolDisplayName: string;
|
|
9
|
+
name: string;
|
|
10
|
+
description: string;
|
|
11
|
+
iconUrl: string;
|
|
12
|
+
siteUrl: string;
|
|
13
|
+
positionType: PositionType;
|
|
14
|
+
metadata?: {
|
|
15
|
+
groupPositions?: boolean;
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
type AdapterResponse<ProtocolResponse> = (ProtocolDetails & {
|
|
19
|
+
chainName: string;
|
|
20
|
+
} & ((ProtocolResponse & {
|
|
21
|
+
success: true;
|
|
22
|
+
}) | (AdapterErrorResponse & {
|
|
23
|
+
success: false;
|
|
24
|
+
}))) | (AdapterErrorResponse & {
|
|
25
|
+
success: false;
|
|
26
|
+
});
|
|
27
|
+
type AdapterErrorResponse = {
|
|
28
|
+
error: {
|
|
29
|
+
message: string;
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
export type PositionType = 'supply' | 'borrow' | 'stake' | 'reward';
|
|
33
|
+
export type ProtocolToken = Balance & {
|
|
34
|
+
type: 'protocol';
|
|
35
|
+
tokenId?: string;
|
|
36
|
+
};
|
|
37
|
+
export type Underlying = Balance & {
|
|
38
|
+
type: 'underlying' | 'underlying-claimable';
|
|
39
|
+
iconUrl: string;
|
|
40
|
+
};
|
|
41
|
+
export type Balance = {
|
|
42
|
+
address: string;
|
|
43
|
+
name: string;
|
|
44
|
+
symbol: string;
|
|
45
|
+
decimals: number;
|
|
46
|
+
balanceRaw: string;
|
|
47
|
+
balance: number;
|
|
48
|
+
price?: number;
|
|
49
|
+
tokens?: Underlying[];
|
|
50
|
+
};
|
|
51
|
+
export declare const DEFI_POSITIONS_API_URL = "https://defiadapters.dev-api.cx.metamask.io";
|
|
52
|
+
/**
|
|
53
|
+
* Builds a function that fetches DeFi positions for a given account address
|
|
54
|
+
*
|
|
55
|
+
* @returns A function that fetches DeFi positions for a given account address
|
|
56
|
+
*/
|
|
57
|
+
export declare function buildPositionFetcher(): (accountAddress: string) => Promise<DefiPositionResponse[]>;
|
|
58
|
+
export {};
|
|
59
|
+
//# sourceMappingURL=fetch-positions.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fetch-positions.d.cts","sourceRoot":"","sources":["../../src/DeFiPositionsController/fetch-positions.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,oBAAoB,GAAG,eAAe,CAAC;IACjD,MAAM,EAAE,aAAa,EAAE,CAAC;CACzB,CAAC,CAAC;AAEH,KAAK,eAAe,GAAG;IACrB,OAAO,EAAE,MAAM,CAAC;IAChB,UAAU,EAAE,MAAM,CAAC;IACnB,SAAS,EAAE,MAAM,CAAC;IAClB,mBAAmB,EAAE,MAAM,CAAC;IAC5B,IAAI,EAAE,MAAM,CAA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