@metamask-previews/assets-controllers 65.0.0-preview-de21a3fe → 66.0.0-preview-4ef4efd7
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 +5 -1
- package/dist/DeFiPositionsController/DeFiPositionsController.cjs +33 -4
- package/dist/DeFiPositionsController/DeFiPositionsController.cjs.map +1 -1
- package/dist/DeFiPositionsController/DeFiPositionsController.d.cts +25 -2
- package/dist/DeFiPositionsController/DeFiPositionsController.d.cts.map +1 -1
- package/dist/DeFiPositionsController/DeFiPositionsController.d.mts +25 -2
- package/dist/DeFiPositionsController/DeFiPositionsController.d.mts.map +1 -1
- package/dist/DeFiPositionsController/DeFiPositionsController.mjs +33 -4
- package/dist/DeFiPositionsController/DeFiPositionsController.mjs.map +1 -1
- package/dist/DeFiPositionsController/calculate-defi-metrics.cjs +41 -0
- package/dist/DeFiPositionsController/calculate-defi-metrics.cjs.map +1 -0
- package/dist/DeFiPositionsController/calculate-defi-metrics.d.cts +10 -0
- package/dist/DeFiPositionsController/calculate-defi-metrics.d.cts.map +1 -0
- package/dist/DeFiPositionsController/calculate-defi-metrics.d.mts +10 -0
- package/dist/DeFiPositionsController/calculate-defi-metrics.d.mts.map +1 -0
- package/dist/DeFiPositionsController/calculate-defi-metrics.mjs +37 -0
- package/dist/DeFiPositionsController/calculate-defi-metrics.mjs.map +1 -0
- package/dist/DeFiPositionsController/group-defi-positions.cjs.map +1 -1
- package/dist/DeFiPositionsController/group-defi-positions.d.cts.map +1 -1
- package/dist/DeFiPositionsController/group-defi-positions.d.mts.map +1 -1
- package/dist/DeFiPositionsController/group-defi-positions.mjs.map +1 -1
- package/package.json +4 -4
package/CHANGELOG.md
CHANGED
@@ -7,8 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
7
7
|
|
8
8
|
## [Unreleased]
|
9
9
|
|
10
|
+
## [66.0.0]
|
11
|
+
|
10
12
|
### Added
|
11
13
|
|
14
|
+
- Add optional parameter to track DeFi metrics when positions are being fetched ([#5868](https://github.com/MetaMask/core/pull/5868))
|
12
15
|
- Add phishing protection for NFT metadata URLs in `NftController` ([#5598](https://github.com/MetaMask/core/pull/5598))
|
13
16
|
- NFT metadata URLs are now scanned for malicious content using the `PhishingController`
|
14
17
|
- Malicious URLs in NFT metadata fields (image, externalLink, etc.) are automatically sanitized
|
@@ -1674,7 +1677,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
1674
1677
|
|
1675
1678
|
- Use Ethers for AssetsContractController ([#845](https://github.com/MetaMask/core/pull/845))
|
1676
1679
|
|
1677
|
-
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@
|
1680
|
+
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@66.0.0...HEAD
|
1681
|
+
[66.0.0]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@65.0.0...@metamask/assets-controllers@66.0.0
|
1678
1682
|
[65.0.0]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@64.0.0...@metamask/assets-controllers@65.0.0
|
1679
1683
|
[64.0.0]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@63.1.0...@metamask/assets-controllers@64.0.0
|
1680
1684
|
[63.1.0]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@63.0.0...@metamask/assets-controllers@63.1.0
|
@@ -10,10 +10,11 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
10
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
11
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
12
12
|
};
|
13
|
-
var _DeFiPositionsController_instances, _DeFiPositionsController_fetchPositions, _DeFiPositionsController_isEnabled, _DeFiPositionsController_updateAccountPositions, _DeFiPositionsController_fetchAccountPositions;
|
13
|
+
var _DeFiPositionsController_instances, _DeFiPositionsController_fetchPositions, _DeFiPositionsController_isEnabled, _DeFiPositionsController_trackEvent, _DeFiPositionsController_updateAccountPositions, _DeFiPositionsController_fetchAccountPositions, _DeFiPositionsController_updatePositionsCountMetrics;
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
15
15
|
exports.DeFiPositionsController = exports.getDefaultDefiPositionsControllerState = void 0;
|
16
16
|
const polling_controller_1 = require("@metamask/polling-controller");
|
17
|
+
const calculate_defi_metrics_1 = require("./calculate-defi-metrics.cjs");
|
17
18
|
const fetch_positions_1 = require("./fetch-positions.cjs");
|
18
19
|
const group_defi_positions_1 = require("./group-defi-positions.cjs");
|
19
20
|
const assetsUtil_1 = require("../assetsUtil.cjs");
|
@@ -25,10 +26,15 @@ const controllerMetadata = {
|
|
25
26
|
persist: false,
|
26
27
|
anonymous: false,
|
27
28
|
},
|
29
|
+
allDeFiPositionsCount: {
|
30
|
+
persist: false,
|
31
|
+
anonymous: false,
|
32
|
+
},
|
28
33
|
};
|
29
34
|
const getDefaultDefiPositionsControllerState = () => {
|
30
35
|
return {
|
31
36
|
allDeFiPositions: {},
|
37
|
+
allDeFiPositionsCount: {},
|
32
38
|
};
|
33
39
|
};
|
34
40
|
exports.getDefaultDefiPositionsControllerState = getDefaultDefiPositionsControllerState;
|
@@ -42,8 +48,9 @@ class DeFiPositionsController extends (0, polling_controller_1.StaticIntervalPol
|
|
42
48
|
* @param options - Constructor options.
|
43
49
|
* @param options.messenger - The controller messenger.
|
44
50
|
* @param options.isEnabled - Function that returns whether the controller is enabled. (default: () => true)
|
51
|
+
* @param options.trackEvent - Function to track events. (default: undefined)
|
45
52
|
*/
|
46
|
-
constructor({ messenger, isEnabled = () => true, }) {
|
53
|
+
constructor({ messenger, isEnabled = () => true, trackEvent, }) {
|
47
54
|
super({
|
48
55
|
name: controllerName,
|
49
56
|
metadata: controllerMetadata,
|
@@ -53,6 +60,7 @@ class DeFiPositionsController extends (0, polling_controller_1.StaticIntervalPol
|
|
53
60
|
_DeFiPositionsController_instances.add(this);
|
54
61
|
_DeFiPositionsController_fetchPositions.set(this, void 0);
|
55
62
|
_DeFiPositionsController_isEnabled.set(this, void 0);
|
63
|
+
_DeFiPositionsController_trackEvent.set(this, void 0);
|
56
64
|
this.setIntervalLength(TEN_MINUTES_IN_MS);
|
57
65
|
__classPrivateFieldSet(this, _DeFiPositionsController_fetchPositions, (0, fetch_positions_1.buildPositionFetcher)(), "f");
|
58
66
|
__classPrivateFieldSet(this, _DeFiPositionsController_isEnabled, isEnabled, "f");
|
@@ -74,6 +82,7 @@ class DeFiPositionsController extends (0, polling_controller_1.StaticIntervalPol
|
|
74
82
|
}
|
75
83
|
await __classPrivateFieldGet(this, _DeFiPositionsController_instances, "m", _DeFiPositionsController_updateAccountPositions).call(this, account.address);
|
76
84
|
});
|
85
|
+
__classPrivateFieldSet(this, _DeFiPositionsController_trackEvent, trackEvent, "f");
|
77
86
|
}
|
78
87
|
async _executePoll() {
|
79
88
|
if (!__classPrivateFieldGet(this, _DeFiPositionsController_isEnabled, "f").call(this)) {
|
@@ -109,7 +118,7 @@ class DeFiPositionsController extends (0, polling_controller_1.StaticIntervalPol
|
|
109
118
|
}
|
110
119
|
}
|
111
120
|
exports.DeFiPositionsController = DeFiPositionsController;
|
112
|
-
_DeFiPositionsController_fetchPositions = new WeakMap(), _DeFiPositionsController_isEnabled = new WeakMap(), _DeFiPositionsController_instances = new WeakSet(), _DeFiPositionsController_updateAccountPositions = async function _DeFiPositionsController_updateAccountPositions(accountAddress) {
|
121
|
+
_DeFiPositionsController_fetchPositions = new WeakMap(), _DeFiPositionsController_isEnabled = new WeakMap(), _DeFiPositionsController_trackEvent = new WeakMap(), _DeFiPositionsController_instances = new WeakSet(), _DeFiPositionsController_updateAccountPositions = async function _DeFiPositionsController_updateAccountPositions(accountAddress) {
|
113
122
|
const accountPositionsPerChain = await __classPrivateFieldGet(this, _DeFiPositionsController_instances, "m", _DeFiPositionsController_fetchAccountPositions).call(this, accountAddress);
|
114
123
|
this.update((state) => {
|
115
124
|
state.allDeFiPositions[accountAddress] = accountPositionsPerChain;
|
@@ -117,10 +126,30 @@ _DeFiPositionsController_fetchPositions = new WeakMap(), _DeFiPositionsControlle
|
|
117
126
|
}, _DeFiPositionsController_fetchAccountPositions = async function _DeFiPositionsController_fetchAccountPositions(accountAddress) {
|
118
127
|
try {
|
119
128
|
const defiPositionsResponse = await __classPrivateFieldGet(this, _DeFiPositionsController_fetchPositions, "f").call(this, accountAddress);
|
120
|
-
|
129
|
+
const groupedDeFiPositions = (0, group_defi_positions_1.groupDeFiPositions)(defiPositionsResponse);
|
130
|
+
try {
|
131
|
+
__classPrivateFieldGet(this, _DeFiPositionsController_instances, "m", _DeFiPositionsController_updatePositionsCountMetrics).call(this, groupedDeFiPositions, accountAddress);
|
132
|
+
}
|
133
|
+
catch (error) {
|
134
|
+
console.error(`Failed to update positions count for account ${accountAddress}:`, error);
|
135
|
+
}
|
136
|
+
return groupedDeFiPositions;
|
121
137
|
}
|
122
138
|
catch {
|
123
139
|
return null;
|
124
140
|
}
|
141
|
+
}, _DeFiPositionsController_updatePositionsCountMetrics = function _DeFiPositionsController_updatePositionsCountMetrics(groupedDeFiPositions, accountAddress) {
|
142
|
+
// If no track event passed then skip the metrics update
|
143
|
+
if (!__classPrivateFieldGet(this, _DeFiPositionsController_trackEvent, "f")) {
|
144
|
+
return;
|
145
|
+
}
|
146
|
+
const defiMetrics = (0, calculate_defi_metrics_1.calculateDeFiPositionMetrics)(groupedDeFiPositions);
|
147
|
+
const { totalPositions } = defiMetrics.properties;
|
148
|
+
if (totalPositions !== this.state.allDeFiPositionsCount[accountAddress]) {
|
149
|
+
this.update((state) => {
|
150
|
+
state.allDeFiPositionsCount[accountAddress] = totalPositions;
|
151
|
+
});
|
152
|
+
__classPrivateFieldGet(this, _DeFiPositionsController_trackEvent, "f")?.call(this, defiMetrics);
|
153
|
+
}
|
125
154
|
};
|
126
155
|
//# sourceMappingURL=DeFiPositionsController.cjs.map
|
@@ -1 +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;AAEK,MAAM,sCAAsC,GACjD,GAAiC,EAAE;IACjC,OAAO;QACL,gBAAgB,EAAE,EAAE;KACrB,CAAC;AACJ,CAAC,CAAC;AALS,QAAA,sCAAsC,0CAK/C;AA4CJ;;GAEG;AACH,MAAa,uBAAwB,SAAQ,IAAA,oDAA+B,GAI3E;IAOC;;;;;;OAMG;IACH,YAAY,EACV,SAAS,EACT,SAAS,GAAG,GAAG,EAAE,CAAC,IAAI,GAIvB;QACC,KAAK,CAAC;YACJ,IAAI,EAAE,cAAc;YACpB,QAAQ,EAAE,kBAAkB;YAC5B,SAAS;YACT,KAAK,EAAE,IAAA,8CAAsC,GAAE;SAChD,CAAC,CAAC;;QAzBI,0DAE4B;QAE5B,qDAA0B;QAuBjC,IAAI,CAAC,iBAAiB,CAAC,iBAAiB,CAAC,CAAC;QAE1C,uBAAA,IAAI,2CAAmB,IAAA,sCAAoB,GAAE,MAAA,CAAC;QAC9C,uBAAA,IAAI,sCAAc,SAAS,MAAA,CAAC;QAE5B,IAAI,CAAC,eAAe,CAAC,SAAS,CAAC,0BAA0B,EAAE,GAAG,EAAE;YAC9D,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC;QAC1B,CAAC,CAAC,CAAC;QAEH,IAAI,CAAC,eAAe,CAAC,SAAS,C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AkB,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"]}
|
1
|
+
{"version":3,"file":"DeFiPositionsController.cjs","sourceRoot":"","sources":["../../src/DeFiPositionsController/DeFiPositionsController.ts"],"names":[],"mappings":";;;;;;;;;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AI,EAAiB,cAAc,CAAC,CAAC;QAEzE,MAAM,oBAAoB,GAAG,IAAA,yCAAkB,EAAC,qBAAqB,CAAC,CAAC;QAEvE,IAAI;YACF,uBAAA,IAAI,gGAA6B,MAAjC,IAAI,EAA8B,oBAAoB,EAAE,cAAc,CAAC,CAAC;SACzE;QAAC,OAAO,KAAK,EAAE;YACd,OAAO,CAAC,KAAK,CACX,gDAAgD,cAAc,GAAG,EACjE,KAAK,CACN,CAAC;SACH;QAED,OAAO,oBAAoB,CAAC;KAC7B;IAAC,MAAM;QACN,OAAO,IAAI,CAAC;KACb;AACH,CAAC,uHAGC,oBAAkD,EAClD,cAAsB;IAEtB,wDAAwD;IACxD,IAAI,CAAC,uBAAA,IAAI,2CAAY,EAAE;QACrB,OAAO;KACR;IAED,MAAM,WAAW,GAAG,IAAA,qDAA4B,EAAC,oBAAoB,CAAC,CAAC;IACvE,MAAM,EAAE,cAAc,EAAE,GAAG,WAAW,CAAC,UAAU,CAAC;IAElD,IAAI,cAAc,KAAK,IAAI,CAAC,KAAK,CAAC,qBAAqB,CAAC,cAAc,CAAC,EAAE;QACvE,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,KAAK,CAAC,qBAAqB,CAAC,cAAc,CAAC,GAAG,cAAc,CAAC;QAC/D,CAAC,CAAC,CAAC;QAEH,uBAAA,IAAI,2CAAY,EAAE,KAAlB,IAAI,EAAe,WAAW,CAAC,CAAC;KACjC;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 { calculateDeFiPositionMetrics } from './calculate-defi-metrics';\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\nexport type GroupedDeFiPositionsPerChain = {\n [chain: Hex]: GroupedDeFiPositions;\n};\n\nexport type TrackingEventPayload = {\n event: string;\n category: string;\n properties: {\n totalPositions: number;\n totalMarketValueUSD: number;\n breakdown?: {\n protocolId: string;\n marketValueUSD: number;\n chainId: Hex;\n count: number;\n }[];\n };\n};\n\ntype TrackEventHook = (event: TrackingEventPayload) => void;\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 /**\n * Object containing DeFi positions count per account\n */\n allDeFiPositionsCount: {\n [accountAddress: string]: number;\n };\n};\n\nconst controllerMetadata: StateMetadata<DeFiPositionsControllerState> = {\n allDeFiPositions: {\n persist: false,\n anonymous: false,\n },\n allDeFiPositionsCount: {\n persist: false,\n anonymous: false,\n },\n};\n\nexport const getDefaultDefiPositionsControllerState =\n (): DeFiPositionsControllerState => {\n return {\n allDeFiPositions: {},\n allDeFiPositionsCount: {},\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 readonly #trackEvent?: TrackEventHook;\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 * @param options.trackEvent - Function to track events. (default: undefined)\n */\n constructor({\n messenger,\n isEnabled = () => true,\n trackEvent,\n }: {\n messenger: DeFiPositionsControllerMessenger;\n isEnabled?: () => boolean;\n trackEvent?: TrackEventHook;\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 this.#trackEvent = trackEvent;\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 const groupedDeFiPositions = groupDeFiPositions(defiPositionsResponse);\n\n try {\n this.#updatePositionsCountMetrics(groupedDeFiPositions, accountAddress);\n } catch (error) {\n console.error(\n `Failed to update positions count for account ${accountAddress}:`,\n error,\n );\n }\n\n return groupedDeFiPositions;\n } catch {\n return null;\n }\n }\n\n #updatePositionsCountMetrics(\n groupedDeFiPositions: GroupedDeFiPositionsPerChain,\n accountAddress: string,\n ) {\n // If no track event passed then skip the metrics update\n if (!this.#trackEvent) {\n return;\n }\n\n const defiMetrics = calculateDeFiPositionMetrics(groupedDeFiPositions);\n const { totalPositions } = defiMetrics.properties;\n\n if (totalPositions !== this.state.allDeFiPositionsCount[accountAddress]) {\n this.update((state) => {\n state.allDeFiPositionsCount[accountAddress] = totalPositions;\n });\n\n this.#trackEvent?.(defiMetrics);\n }\n }\n}\n"]}
|
@@ -7,9 +7,24 @@ import type { TransactionControllerTransactionConfirmedEvent } from "@metamask/t
|
|
7
7
|
import type { Hex } from "@metamask/utils";
|
8
8
|
import { type GroupedDeFiPositions } from "./group-defi-positions.cjs";
|
9
9
|
declare const controllerName = "DeFiPositionsController";
|
10
|
-
type GroupedDeFiPositionsPerChain = {
|
10
|
+
export type GroupedDeFiPositionsPerChain = {
|
11
11
|
[chain: Hex]: GroupedDeFiPositions;
|
12
12
|
};
|
13
|
+
export type TrackingEventPayload = {
|
14
|
+
event: string;
|
15
|
+
category: string;
|
16
|
+
properties: {
|
17
|
+
totalPositions: number;
|
18
|
+
totalMarketValueUSD: number;
|
19
|
+
breakdown?: {
|
20
|
+
protocolId: string;
|
21
|
+
marketValueUSD: number;
|
22
|
+
chainId: Hex;
|
23
|
+
count: number;
|
24
|
+
}[];
|
25
|
+
};
|
26
|
+
};
|
27
|
+
type TrackEventHook = (event: TrackingEventPayload) => void;
|
13
28
|
export type DeFiPositionsControllerState = {
|
14
29
|
/**
|
15
30
|
* Object containing DeFi positions per account and network
|
@@ -17,6 +32,12 @@ export type DeFiPositionsControllerState = {
|
|
17
32
|
allDeFiPositions: {
|
18
33
|
[accountAddress: string]: GroupedDeFiPositionsPerChain | null;
|
19
34
|
};
|
35
|
+
/**
|
36
|
+
* Object containing DeFi positions count per account
|
37
|
+
*/
|
38
|
+
allDeFiPositionsCount: {
|
39
|
+
[accountAddress: string]: number;
|
40
|
+
};
|
20
41
|
};
|
21
42
|
export declare const getDefaultDefiPositionsControllerState: () => DeFiPositionsControllerState;
|
22
43
|
export type DeFiPositionsControllerActions = DeFiPositionsControllerGetStateAction;
|
@@ -61,10 +82,12 @@ export declare class DeFiPositionsController extends DeFiPositionsController_bas
|
|
61
82
|
* @param options - Constructor options.
|
62
83
|
* @param options.messenger - The controller messenger.
|
63
84
|
* @param options.isEnabled - Function that returns whether the controller is enabled. (default: () => true)
|
85
|
+
* @param options.trackEvent - Function to track events. (default: undefined)
|
64
86
|
*/
|
65
|
-
constructor({ messenger, isEnabled, }: {
|
87
|
+
constructor({ messenger, isEnabled, trackEvent, }: {
|
66
88
|
messenger: DeFiPositionsControllerMessenger;
|
67
89
|
isEnabled?: () => boolean;
|
90
|
+
trackEvent?: TrackEventHook;
|
68
91
|
});
|
69
92
|
_executePoll(): Promise<void>;
|
70
93
|
}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"DeFiPositionsController.d.cts","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;
|
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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AM,qCAAqC,GAAG,wBAAwB,CAC1E,OAAO,cAAc,EACrB,4BAA4B,CAC7B,CAAC;AAEF,MAAM,MAAM,6BAA6B,GACvC,uCAAuC,CAAC;AAE1C,MAAM,MAAM,uCAAuC,GACjD,0BAA0B,CACxB,OAAO,cAAc,EACrB,4BAA4B,CAC7B,CAAC;AAEJ;;GAEG;AACH,MAAM,MAAM,cAAc,GAAG,oCAAoC,CAAC;AAElE;;GAEG;AACH,MAAM,MAAM,aAAa,GACrB,4BAA4B,GAC5B,0BAA0B,GAC1B,8CAA8C,GAC9C,mCAAmC,CAAC;AAExC;;GAEG;AACH,MAAM,MAAM,gCAAgC,GAAG,mBAAmB,CAChE,OAAO,cAAc,EACrB,8BAA8B,GAAG,cAAc,EAC/C,6BAA6B,GAAG,aAAa,EAC7C,cAAc,CAAC,MAAM,CAAC,EACtB,aAAa,CAAC,MAAM,CAAC,CACtB,CAAC;;;;;;;;;;;;;;;;AAEF;;GAEG;AACH,qBAAa,uBAAwB,SAAQ,6BAC3C,OAAO,cAAc,EACrB,4BAA4B,EAC5B,gCAAgC,CACjC;;IASC;;;;;;;OAOG;gBACS,EACV,SAAS,EACT,SAAsB,EACtB,UAAU,GACX,EAAE;QACD,SAAS,EAAE,gCAAgC,CAAC;QAC5C,SAAS,CAAC,EAAE,MAAM,OAAO,CAAC;QAC1B,UAAU,CAAC,EAAE,cAAc,CAAC;KAC7B;IA8CK,YAAY,IAAI,OAAO,CAAC,IAAI,CAAC;CA6GpC"}
|
@@ -7,9 +7,24 @@ import type { TransactionControllerTransactionConfirmedEvent } from "@metamask/t
|
|
7
7
|
import type { Hex } from "@metamask/utils";
|
8
8
|
import { type GroupedDeFiPositions } from "./group-defi-positions.mjs";
|
9
9
|
declare const controllerName = "DeFiPositionsController";
|
10
|
-
type GroupedDeFiPositionsPerChain = {
|
10
|
+
export type GroupedDeFiPositionsPerChain = {
|
11
11
|
[chain: Hex]: GroupedDeFiPositions;
|
12
12
|
};
|
13
|
+
export type TrackingEventPayload = {
|
14
|
+
event: string;
|
15
|
+
category: string;
|
16
|
+
properties: {
|
17
|
+
totalPositions: number;
|
18
|
+
totalMarketValueUSD: number;
|
19
|
+
breakdown?: {
|
20
|
+
protocolId: string;
|
21
|
+
marketValueUSD: number;
|
22
|
+
chainId: Hex;
|
23
|
+
count: number;
|
24
|
+
}[];
|
25
|
+
};
|
26
|
+
};
|
27
|
+
type TrackEventHook = (event: TrackingEventPayload) => void;
|
13
28
|
export type DeFiPositionsControllerState = {
|
14
29
|
/**
|
15
30
|
* Object containing DeFi positions per account and network
|
@@ -17,6 +32,12 @@ export type DeFiPositionsControllerState = {
|
|
17
32
|
allDeFiPositions: {
|
18
33
|
[accountAddress: string]: GroupedDeFiPositionsPerChain | null;
|
19
34
|
};
|
35
|
+
/**
|
36
|
+
* Object containing DeFi positions count per account
|
37
|
+
*/
|
38
|
+
allDeFiPositionsCount: {
|
39
|
+
[accountAddress: string]: number;
|
40
|
+
};
|
20
41
|
};
|
21
42
|
export declare const getDefaultDefiPositionsControllerState: () => DeFiPositionsControllerState;
|
22
43
|
export type DeFiPositionsControllerActions = DeFiPositionsControllerGetStateAction;
|
@@ -61,10 +82,12 @@ export declare class DeFiPositionsController extends DeFiPositionsController_bas
|
|
61
82
|
* @param options - Constructor options.
|
62
83
|
* @param options.messenger - The controller messenger.
|
63
84
|
* @param options.isEnabled - Function that returns whether the controller is enabled. (default: () => true)
|
85
|
+
* @param options.trackEvent - Function to track events. (default: undefined)
|
64
86
|
*/
|
65
|
-
constructor({ messenger, isEnabled, }: {
|
87
|
+
constructor({ messenger, isEnabled, trackEvent, }: {
|
66
88
|
messenger: DeFiPositionsControllerMessenger;
|
67
89
|
isEnabled?: () => boolean;
|
90
|
+
trackEvent?: TrackEventHook;
|
68
91
|
});
|
69
92
|
_executePoll(): Promise<void>;
|
70
93
|
}
|
@@ -1 +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;
|
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;AAK3C,OAAO,EAEL,KAAK,oBAAoB,EAC1B,mCAA+B;AAOhC,QAAA,MAAM,cAAc,4BAA4B,CAAC;AAEjD,MAAM,MAAM,4BAA4B,GAAG;IACzC,CAAC,KAAK,EAAE,GAAG,GAAG,oBAAoB,CAAC;CACpC,CAAC;AAEF,MAAM,MAAM,oBAAoB,GAAG;IACjC,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,MAAM,CAAC;IACjB,UAAU,EAAE;QACV,cAAc,EAAE,MAAM,CAAC;QACvB,mBAAmB,EAAE,MAAM,CAAC;QAC5B,SAAS,CAAC,EAAE;YACV,UAAU,EAAE,MAAM,CAAC;YACnB,cAAc,EAAE,MAAM,CAAC;YACv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AAS,EACT,SAAsB,EACtB,UAAU,GACX,EAAE;QACD,SAAS,EAAE,gCAAgC,CAAC;QAC5C,SAAS,CAAC,EAAE,MAAM,OAAO,CAAC;QAC1B,UAAU,CAAC,EAAE,cAAc,CAAC;KAC7B;IA8CK,YAAY,IAAI,OAAO,CAAC,IAAI,CAAC;CA6GpC"}
|
@@ -9,8 +9,9 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
9
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
10
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
11
11
|
};
|
12
|
-
var _DeFiPositionsController_instances, _DeFiPositionsController_fetchPositions, _DeFiPositionsController_isEnabled, _DeFiPositionsController_updateAccountPositions, _DeFiPositionsController_fetchAccountPositions;
|
12
|
+
var _DeFiPositionsController_instances, _DeFiPositionsController_fetchPositions, _DeFiPositionsController_isEnabled, _DeFiPositionsController_trackEvent, _DeFiPositionsController_updateAccountPositions, _DeFiPositionsController_fetchAccountPositions, _DeFiPositionsController_updatePositionsCountMetrics;
|
13
13
|
import { StaticIntervalPollingController } from "@metamask/polling-controller";
|
14
|
+
import { calculateDeFiPositionMetrics } from "./calculate-defi-metrics.mjs";
|
14
15
|
import { buildPositionFetcher } from "./fetch-positions.mjs";
|
15
16
|
import { groupDeFiPositions } from "./group-defi-positions.mjs";
|
16
17
|
import { reduceInBatchesSerially } from "../assetsUtil.mjs";
|
@@ -22,10 +23,15 @@ const controllerMetadata = {
|
|
22
23
|
persist: false,
|
23
24
|
anonymous: false,
|
24
25
|
},
|
26
|
+
allDeFiPositionsCount: {
|
27
|
+
persist: false,
|
28
|
+
anonymous: false,
|
29
|
+
},
|
25
30
|
};
|
26
31
|
export const getDefaultDefiPositionsControllerState = () => {
|
27
32
|
return {
|
28
33
|
allDeFiPositions: {},
|
34
|
+
allDeFiPositionsCount: {},
|
29
35
|
};
|
30
36
|
};
|
31
37
|
/**
|
@@ -38,8 +44,9 @@ export class DeFiPositionsController extends StaticIntervalPollingController() {
|
|
38
44
|
* @param options - Constructor options.
|
39
45
|
* @param options.messenger - The controller messenger.
|
40
46
|
* @param options.isEnabled - Function that returns whether the controller is enabled. (default: () => true)
|
47
|
+
* @param options.trackEvent - Function to track events. (default: undefined)
|
41
48
|
*/
|
42
|
-
constructor({ messenger, isEnabled = () => true, }) {
|
49
|
+
constructor({ messenger, isEnabled = () => true, trackEvent, }) {
|
43
50
|
super({
|
44
51
|
name: controllerName,
|
45
52
|
metadata: controllerMetadata,
|
@@ -49,6 +56,7 @@ export class DeFiPositionsController extends StaticIntervalPollingController() {
|
|
49
56
|
_DeFiPositionsController_instances.add(this);
|
50
57
|
_DeFiPositionsController_fetchPositions.set(this, void 0);
|
51
58
|
_DeFiPositionsController_isEnabled.set(this, void 0);
|
59
|
+
_DeFiPositionsController_trackEvent.set(this, void 0);
|
52
60
|
this.setIntervalLength(TEN_MINUTES_IN_MS);
|
53
61
|
__classPrivateFieldSet(this, _DeFiPositionsController_fetchPositions, buildPositionFetcher(), "f");
|
54
62
|
__classPrivateFieldSet(this, _DeFiPositionsController_isEnabled, isEnabled, "f");
|
@@ -70,6 +78,7 @@ export class DeFiPositionsController extends StaticIntervalPollingController() {
|
|
70
78
|
}
|
71
79
|
await __classPrivateFieldGet(this, _DeFiPositionsController_instances, "m", _DeFiPositionsController_updateAccountPositions).call(this, account.address);
|
72
80
|
});
|
81
|
+
__classPrivateFieldSet(this, _DeFiPositionsController_trackEvent, trackEvent, "f");
|
73
82
|
}
|
74
83
|
async _executePoll() {
|
75
84
|
if (!__classPrivateFieldGet(this, _DeFiPositionsController_isEnabled, "f").call(this)) {
|
@@ -104,7 +113,7 @@ export class DeFiPositionsController extends StaticIntervalPollingController() {
|
|
104
113
|
});
|
105
114
|
}
|
106
115
|
}
|
107
|
-
_DeFiPositionsController_fetchPositions = new WeakMap(), _DeFiPositionsController_isEnabled = new WeakMap(), _DeFiPositionsController_instances = new WeakSet(), _DeFiPositionsController_updateAccountPositions = async function _DeFiPositionsController_updateAccountPositions(accountAddress) {
|
116
|
+
_DeFiPositionsController_fetchPositions = new WeakMap(), _DeFiPositionsController_isEnabled = new WeakMap(), _DeFiPositionsController_trackEvent = new WeakMap(), _DeFiPositionsController_instances = new WeakSet(), _DeFiPositionsController_updateAccountPositions = async function _DeFiPositionsController_updateAccountPositions(accountAddress) {
|
108
117
|
const accountPositionsPerChain = await __classPrivateFieldGet(this, _DeFiPositionsController_instances, "m", _DeFiPositionsController_fetchAccountPositions).call(this, accountAddress);
|
109
118
|
this.update((state) => {
|
110
119
|
state.allDeFiPositions[accountAddress] = accountPositionsPerChain;
|
@@ -112,10 +121,30 @@ _DeFiPositionsController_fetchPositions = new WeakMap(), _DeFiPositionsControlle
|
|
112
121
|
}, _DeFiPositionsController_fetchAccountPositions = async function _DeFiPositionsController_fetchAccountPositions(accountAddress) {
|
113
122
|
try {
|
114
123
|
const defiPositionsResponse = await __classPrivateFieldGet(this, _DeFiPositionsController_fetchPositions, "f").call(this, accountAddress);
|
115
|
-
|
124
|
+
const groupedDeFiPositions = groupDeFiPositions(defiPositionsResponse);
|
125
|
+
try {
|
126
|
+
__classPrivateFieldGet(this, _DeFiPositionsController_instances, "m", _DeFiPositionsController_updatePositionsCountMetrics).call(this, groupedDeFiPositions, accountAddress);
|
127
|
+
}
|
128
|
+
catch (error) {
|
129
|
+
console.error(`Failed to update positions count for account ${accountAddress}:`, error);
|
130
|
+
}
|
131
|
+
return groupedDeFiPositions;
|
116
132
|
}
|
117
133
|
catch {
|
118
134
|
return null;
|
119
135
|
}
|
136
|
+
}, _DeFiPositionsController_updatePositionsCountMetrics = function _DeFiPositionsController_updatePositionsCountMetrics(groupedDeFiPositions, accountAddress) {
|
137
|
+
// If no track event passed then skip the metrics update
|
138
|
+
if (!__classPrivateFieldGet(this, _DeFiPositionsController_trackEvent, "f")) {
|
139
|
+
return;
|
140
|
+
}
|
141
|
+
const defiMetrics = calculateDeFiPositionMetrics(groupedDeFiPositions);
|
142
|
+
const { totalPositions } = defiMetrics.properties;
|
143
|
+
if (totalPositions !== this.state.allDeFiPositionsCount[accountAddress]) {
|
144
|
+
this.update((state) => {
|
145
|
+
state.allDeFiPositionsCount[accountAddress] = totalPositions;
|
146
|
+
});
|
147
|
+
__classPrivateFieldGet(this, _DeFiPositionsController_trackEvent, "f")?.call(this, defiMetrics);
|
148
|
+
}
|
120
149
|
};
|
121
150
|
//# sourceMappingURL=DeFiPositionsController.mjs.map
|
@@ -1 +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,KAAK;KACjB;CACF,CAAC;AAEF,MAAM,CAAC,MAAM,sCAAsC,GACjD,GAAiC,EAAE;IACjC,OAAO;QACL,gBAAgB,EAAE,EAAE;KACrB,CAAC;AACJ,CAAC,CAAC;AA4CJ;;GAEG;AACH,MAAM,OAAO,uBAAwB,SAAQ,+BAA+B,EAI3E;IAOC;;;;;;OAMG;IACH,YAAY,EACV,SAAS,EACT,SAAS,GAAG,GAAG,EAAE,CAAC,IAAI,GAIvB;QACC,KAAK,CAAC;YACJ,IAAI,EAAE,cAAc;YACpB,QAAQ,EAAE,kBAAkB;YAC5B,SAAS;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ClD,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"]}
|
1
|
+
{"version":3,"file":"DeFiPositionsController.mjs","sourceRoot":"","sources":["../../src/DeFiPositionsController/DeFiPositionsController.ts"],"names":[],"mappings":";;;;;;;;;;;;AAYA,OAAO,EAAE,+BAA+B,EAAE,qCAAqC;AAI/E,OAAO,EAAE,4BAA4B,EAAE,qCAAiC;AAExE,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;AAuCjD,MAAM,kBAAkB,GAAgD;IACtE,gBAAgB,EAAE;QAChB,OAAO,EAAE,KAAK;QACd,SAAS,EAAE,KAAK;KACjB;IACD,qBAAqB,EAAE;QACrB,OAAO,EAAE,KAAK;QACd,SAAS,EAAE,KAAK;KACjB;CACF,CAAC;AAEF,MAAM,CAAC,MAAM,sCAAsC,GACjD,GAAiC,EAAE;IACjC,OAAO;QACL,gBAAgB,EAAE,EAAE;QACpB,qBAAqB,EAAE,EAAE;KAC1B,CAAC;AACJ,CAAC,CAAC;AA4CJ;;GAEG;AACH,MAAM,OAAO,uBAAwB,SAAQ,+BAA+B,EAI3E;IASC;;;;;;;OAOG;IACH,YAAY,EACV,SAAS,EACT,SAAS,GAAG,GAAG,EAAE,CAAC,IAAI,EACtB,UAAU,GAKX;QACC,KAAK,CAAC;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AC,cAAc,CAAC,GAAG,cAAc,CAAC;QAC/D,CAAC,CAAC,CAAC;QAEH,uBAAA,IAAI,2CAAY,EAAE,KAAlB,IAAI,EAAe,WAAW,CAAC,CAAC;KACjC;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 { calculateDeFiPositionMetrics } from './calculate-defi-metrics';\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\nexport type GroupedDeFiPositionsPerChain = {\n [chain: Hex]: GroupedDeFiPositions;\n};\n\nexport type TrackingEventPayload = {\n event: string;\n category: string;\n properties: {\n totalPositions: number;\n totalMarketValueUSD: number;\n breakdown?: {\n protocolId: string;\n marketValueUSD: number;\n chainId: Hex;\n count: number;\n }[];\n };\n};\n\ntype TrackEventHook = (event: TrackingEventPayload) => void;\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 /**\n * Object containing DeFi positions count per account\n */\n allDeFiPositionsCount: {\n [accountAddress: string]: number;\n };\n};\n\nconst controllerMetadata: StateMetadata<DeFiPositionsControllerState> = {\n allDeFiPositions: {\n persist: false,\n anonymous: false,\n },\n allDeFiPositionsCount: {\n persist: false,\n anonymous: false,\n },\n};\n\nexport const getDefaultDefiPositionsControllerState =\n (): DeFiPositionsControllerState => {\n return {\n allDeFiPositions: {},\n allDeFiPositionsCount: {},\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 readonly #trackEvent?: TrackEventHook;\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 * @param options.trackEvent - Function to track events. (default: undefined)\n */\n constructor({\n messenger,\n isEnabled = () => true,\n trackEvent,\n }: {\n messenger: DeFiPositionsControllerMessenger;\n isEnabled?: () => boolean;\n trackEvent?: TrackEventHook;\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 this.#trackEvent = trackEvent;\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 const groupedDeFiPositions = groupDeFiPositions(defiPositionsResponse);\n\n try {\n this.#updatePositionsCountMetrics(groupedDeFiPositions, accountAddress);\n } catch (error) {\n console.error(\n `Failed to update positions count for account ${accountAddress}:`,\n error,\n );\n }\n\n return groupedDeFiPositions;\n } catch {\n return null;\n }\n }\n\n #updatePositionsCountMetrics(\n groupedDeFiPositions: GroupedDeFiPositionsPerChain,\n accountAddress: string,\n ) {\n // If no track event passed then skip the metrics update\n if (!this.#trackEvent) {\n return;\n }\n\n const defiMetrics = calculateDeFiPositionMetrics(groupedDeFiPositions);\n const { totalPositions } = defiMetrics.properties;\n\n if (totalPositions !== this.state.allDeFiPositionsCount[accountAddress]) {\n this.update((state) => {\n state.allDeFiPositionsCount[accountAddress] = totalPositions;\n });\n\n this.#trackEvent?.(defiMetrics);\n }\n }\n}\n"]}
|
@@ -0,0 +1,41 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.calculateDeFiPositionMetrics = void 0;
|
4
|
+
/**
|
5
|
+
* Calculates the total market value and total positions for a given account
|
6
|
+
* and returns a breakdown of the market value per protocol.
|
7
|
+
*
|
8
|
+
* @param accountPositionsPerChain - The account positions per chain.
|
9
|
+
* @returns An object containing the total market value, total positions, and a breakdown of the market value per protocol.
|
10
|
+
*/
|
11
|
+
function calculateDeFiPositionMetrics(accountPositionsPerChain) {
|
12
|
+
let totalMarketValueUSD = 0;
|
13
|
+
let totalPositions = 0;
|
14
|
+
const breakdown = [];
|
15
|
+
Object.entries(accountPositionsPerChain).forEach(([chainId, chainPositions]) => {
|
16
|
+
const chainTotalMarketValueUSD = chainPositions.aggregatedMarketValue;
|
17
|
+
totalMarketValueUSD += chainTotalMarketValueUSD;
|
18
|
+
Object.entries(chainPositions.protocols).forEach(([protocolId, protocol]) => {
|
19
|
+
const protocolTotalMarketValueUSD = protocol.aggregatedMarketValue;
|
20
|
+
const protocolCount = Object.values(protocol.positionTypes).reduce((acc, positionType) => acc + (positionType?.positions?.flat().length || 0), 0);
|
21
|
+
totalPositions += protocolCount;
|
22
|
+
breakdown.push({
|
23
|
+
protocolId,
|
24
|
+
marketValueUSD: protocolTotalMarketValueUSD,
|
25
|
+
chainId: chainId,
|
26
|
+
count: protocolCount,
|
27
|
+
});
|
28
|
+
});
|
29
|
+
});
|
30
|
+
return {
|
31
|
+
category: 'DeFi',
|
32
|
+
event: 'DeFi Stats',
|
33
|
+
properties: {
|
34
|
+
totalMarketValueUSD,
|
35
|
+
totalPositions,
|
36
|
+
breakdown,
|
37
|
+
},
|
38
|
+
};
|
39
|
+
}
|
40
|
+
exports.calculateDeFiPositionMetrics = calculateDeFiPositionMetrics;
|
41
|
+
//# sourceMappingURL=calculate-defi-metrics.cjs.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"calculate-defi-metrics.cjs","sourceRoot":"","sources":["../../src/DeFiPositionsController/calculate-defi-metrics.ts"],"names":[],"mappings":";;;AAOA;;;;;;GAMG;AACH,SAAgB,4BAA4B,CAC1C,wBAAsD;IAEtD,IAAI,mBAAmB,GAAG,CAAC,CAAC;IAC5B,IAAI,cAAc,GAAG,CAAC,CAAC;IACvB,MAAM,SAAS,GAKT,EAAE,CAAC;IAET,MAAM,CAAC,OAAO,CAAC,wBAAwB,CAAC,CAAC,OAAO,CAC9C,CAAC,CAAC,OAAO,EAAE,cAAc,CAAC,EAAE,EAAE;QAC5B,MAAM,wBAAwB,GAAG,cAAc,CAAC,qBAAqB,CAAC;QACtE,mBAAmB,IAAI,wBAAwB,CAAC;QAEhD,MAAM,CAAC,OAAO,CAAC,cAAc,CAAC,SAAS,CAAC,CAAC,OAAO,CAC9C,CAAC,CAAC,UAAU,EAAE,QAAQ,CAAC,EAAE,EAAE;YACzB,MAAM,2BAA2B,GAAG,QAAQ,CAAC,qBAAqB,CAAC;YAEnE,MAAM,aAAa,GAAG,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,aAAa,CAAC,CAAC,MAAM,CAChE,CAAC,GAAG,EAAE,YAAY,EAAE,EAAE,CACpB,GAAG,GAAG,CAAC,YAAY,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,MAAM,IAAI,CAAC,CAAC,EAErD,CAAC,CACF,CAAC;YAEF,cAAc,IAAI,aAAa,CAAC;YAEhC,SAAS,CAAC,IAAI,CAAC;gBACb,UAAU;gBACV,cAAc,EAAE,2BAA2B;gBAC3C,OAAO,EAAE,OAAc;gBACvB,KAAK,EAAE,aAAa;aACrB,CAAC,CAAC;QACL,CAAC,CACF,CAAC;IACJ,CAAC,CACF,CAAC;IACF,OAAO;QACL,QAAQ,EAAE,MAAM;QAChB,KAAK,EAAE,YAAY;QACnB,UAAU,EAAE;YACV,mBAAmB;YACnB,cAAc;YACd,SAAS;SACV;KACF,CAAC;AACJ,CAAC;AAjDD,oEAiDC","sourcesContent":["import type { Hex } from '@metamask/utils';\n\nimport type {\n GroupedDeFiPositionsPerChain,\n TrackingEventPayload,\n} from './DeFiPositionsController';\n\n/**\n * Calculates the total market value and total positions for a given account\n * and returns a breakdown of the market value per protocol.\n *\n * @param accountPositionsPerChain - The account positions per chain.\n * @returns An object containing the total market value, total positions, and a breakdown of the market value per protocol.\n */\nexport function calculateDeFiPositionMetrics(\n accountPositionsPerChain: GroupedDeFiPositionsPerChain,\n): TrackingEventPayload {\n let totalMarketValueUSD = 0;\n let totalPositions = 0;\n const breakdown: {\n protocolId: string;\n marketValueUSD: number;\n chainId: Hex;\n count: number;\n }[] = [];\n\n Object.entries(accountPositionsPerChain).forEach(\n ([chainId, chainPositions]) => {\n const chainTotalMarketValueUSD = chainPositions.aggregatedMarketValue;\n totalMarketValueUSD += chainTotalMarketValueUSD;\n\n Object.entries(chainPositions.protocols).forEach(\n ([protocolId, protocol]) => {\n const protocolTotalMarketValueUSD = protocol.aggregatedMarketValue;\n\n const protocolCount = Object.values(protocol.positionTypes).reduce(\n (acc, positionType) =>\n acc + (positionType?.positions?.flat().length || 0),\n\n 0,\n );\n\n totalPositions += protocolCount;\n\n breakdown.push({\n protocolId,\n marketValueUSD: protocolTotalMarketValueUSD,\n chainId: chainId as Hex,\n count: protocolCount,\n });\n },\n );\n },\n );\n return {\n category: 'DeFi',\n event: 'DeFi Stats',\n properties: {\n totalMarketValueUSD,\n totalPositions,\n breakdown,\n },\n };\n}\n"]}
|
@@ -0,0 +1,10 @@
|
|
1
|
+
import type { GroupedDeFiPositionsPerChain, TrackingEventPayload } from "./DeFiPositionsController.cjs";
|
2
|
+
/**
|
3
|
+
* Calculates the total market value and total positions for a given account
|
4
|
+
* and returns a breakdown of the market value per protocol.
|
5
|
+
*
|
6
|
+
* @param accountPositionsPerChain - The account positions per chain.
|
7
|
+
* @returns An object containing the total market value, total positions, and a breakdown of the market value per protocol.
|
8
|
+
*/
|
9
|
+
export declare function calculateDeFiPositionMetrics(accountPositionsPerChain: GroupedDeFiPositionsPerChain): TrackingEventPayload;
|
10
|
+
//# sourceMappingURL=calculate-defi-metrics.d.cts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"calculate-defi-metrics.d.cts","sourceRoot":"","sources":["../../src/DeFiPositionsController/calculate-defi-metrics.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACV,4BAA4B,EAC5B,oBAAoB,EACrB,sCAAkC;AAEnC;;;;;;GAMG;AACH,wBAAgB,4BAA4B,CAC1C,wBAAwB,EAAE,4BAA4B,GACrD,oBAAoB,CA+CtB"}
|
@@ -0,0 +1,10 @@
|
|
1
|
+
import type { GroupedDeFiPositionsPerChain, TrackingEventPayload } from "./DeFiPositionsController.mjs";
|
2
|
+
/**
|
3
|
+
* Calculates the total market value and total positions for a given account
|
4
|
+
* and returns a breakdown of the market value per protocol.
|
5
|
+
*
|
6
|
+
* @param accountPositionsPerChain - The account positions per chain.
|
7
|
+
* @returns An object containing the total market value, total positions, and a breakdown of the market value per protocol.
|
8
|
+
*/
|
9
|
+
export declare function calculateDeFiPositionMetrics(accountPositionsPerChain: GroupedDeFiPositionsPerChain): TrackingEventPayload;
|
10
|
+
//# sourceMappingURL=calculate-defi-metrics.d.mts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"calculate-defi-metrics.d.mts","sourceRoot":"","sources":["../../src/DeFiPositionsController/calculate-defi-metrics.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACV,4BAA4B,EAC5B,oBAAoB,EACrB,sCAAkC;AAEnC;;;;;;GAMG;AACH,wBAAgB,4BAA4B,CAC1C,wBAAwB,EAAE,4BAA4B,GACrD,oBAAoB,CA+CtB"}
|
@@ -0,0 +1,37 @@
|
|
1
|
+
/**
|
2
|
+
* Calculates the total market value and total positions for a given account
|
3
|
+
* and returns a breakdown of the market value per protocol.
|
4
|
+
*
|
5
|
+
* @param accountPositionsPerChain - The account positions per chain.
|
6
|
+
* @returns An object containing the total market value, total positions, and a breakdown of the market value per protocol.
|
7
|
+
*/
|
8
|
+
export function calculateDeFiPositionMetrics(accountPositionsPerChain) {
|
9
|
+
let totalMarketValueUSD = 0;
|
10
|
+
let totalPositions = 0;
|
11
|
+
const breakdown = [];
|
12
|
+
Object.entries(accountPositionsPerChain).forEach(([chainId, chainPositions]) => {
|
13
|
+
const chainTotalMarketValueUSD = chainPositions.aggregatedMarketValue;
|
14
|
+
totalMarketValueUSD += chainTotalMarketValueUSD;
|
15
|
+
Object.entries(chainPositions.protocols).forEach(([protocolId, protocol]) => {
|
16
|
+
const protocolTotalMarketValueUSD = protocol.aggregatedMarketValue;
|
17
|
+
const protocolCount = Object.values(protocol.positionTypes).reduce((acc, positionType) => acc + (positionType?.positions?.flat().length || 0), 0);
|
18
|
+
totalPositions += protocolCount;
|
19
|
+
breakdown.push({
|
20
|
+
protocolId,
|
21
|
+
marketValueUSD: protocolTotalMarketValueUSD,
|
22
|
+
chainId: chainId,
|
23
|
+
count: protocolCount,
|
24
|
+
});
|
25
|
+
});
|
26
|
+
});
|
27
|
+
return {
|
28
|
+
category: 'DeFi',
|
29
|
+
event: 'DeFi Stats',
|
30
|
+
properties: {
|
31
|
+
totalMarketValueUSD,
|
32
|
+
totalPositions,
|
33
|
+
breakdown,
|
34
|
+
},
|
35
|
+
};
|
36
|
+
}
|
37
|
+
//# sourceMappingURL=calculate-defi-metrics.mjs.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"calculate-defi-metrics.mjs","sourceRoot":"","sources":["../../src/DeFiPositionsController/calculate-defi-metrics.ts"],"names":[],"mappings":"AAOA;;;;;;GAMG;AACH,MAAM,UAAU,4BAA4B,CAC1C,wBAAsD;IAEtD,IAAI,mBAAmB,GAAG,CAAC,CAAC;IAC5B,IAAI,cAAc,GAAG,CAAC,CAAC;IACvB,MAAM,SAAS,GAKT,EAAE,CAAC;IAET,MAAM,CAAC,OAAO,CAAC,wBAAwB,CAAC,CAAC,OAAO,CAC9C,CAAC,CAAC,OAAO,EAAE,cAAc,CAAC,EAAE,EAAE;QAC5B,MAAM,wBAAwB,GAAG,cAAc,CAAC,qBAAqB,CAAC;QACtE,mBAAmB,IAAI,wBAAwB,CAAC;QAEhD,MAAM,CAAC,OAAO,CAAC,cAAc,CAAC,SAAS,CAAC,CAAC,OAAO,CAC9C,CAAC,CAAC,UAAU,EAAE,QAAQ,CAAC,EAAE,EAAE;YACzB,MAAM,2BAA2B,GAAG,QAAQ,CAAC,qBAAqB,CAAC;YAEnE,MAAM,aAAa,GAAG,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,aAAa,CAAC,CAAC,MAAM,CAChE,CAAC,GAAG,EAAE,YAAY,EAAE,EAAE,CACpB,GAAG,GAAG,CAAC,YAAY,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,MAAM,IAAI,CAAC,CAAC,EAErD,CAAC,CACF,CAAC;YAEF,cAAc,IAAI,aAAa,CAAC;YAEhC,SAAS,CAAC,IAAI,CAAC;gBACb,UAAU;gBACV,cAAc,EAAE,2BAA2B;gBAC3C,OAAO,EAAE,OAAc;gBACvB,KAAK,EAAE,aAAa;aACrB,CAAC,CAAC;QACL,CAAC,CACF,CAAC;IACJ,CAAC,CACF,CAAC;IACF,OAAO;QACL,QAAQ,EAAE,MAAM;QAChB,KAAK,EAAE,YAAY;QACnB,UAAU,EAAE;YACV,mBAAmB;YACnB,cAAc;YACd,SAAS;SACV;KACF,CAAC;AACJ,CAAC","sourcesContent":["import type { Hex } from '@metamask/utils';\n\nimport type {\n GroupedDeFiPositionsPerChain,\n TrackingEventPayload,\n} from './DeFiPositionsController';\n\n/**\n * Calculates the total market value and total positions for a given account\n * and returns a breakdown of the market value per protocol.\n *\n * @param accountPositionsPerChain - The account positions per chain.\n * @returns An object containing the total market value, total positions, and a breakdown of the market value per protocol.\n */\nexport function calculateDeFiPositionMetrics(\n accountPositionsPerChain: GroupedDeFiPositionsPerChain,\n): TrackingEventPayload {\n let totalMarketValueUSD = 0;\n let totalPositions = 0;\n const breakdown: {\n protocolId: string;\n marketValueUSD: number;\n chainId: Hex;\n count: number;\n }[] = [];\n\n Object.entries(accountPositionsPerChain).forEach(\n ([chainId, chainPositions]) => {\n const chainTotalMarketValueUSD = chainPositions.aggregatedMarketValue;\n totalMarketValueUSD += chainTotalMarketValueUSD;\n\n Object.entries(chainPositions.protocols).forEach(\n ([protocolId, protocol]) => {\n const protocolTotalMarketValueUSD = protocol.aggregatedMarketValue;\n\n const protocolCount = Object.values(protocol.positionTypes).reduce(\n (acc, positionType) =>\n acc + (positionType?.positions?.flat().length || 0),\n\n 0,\n );\n\n totalPositions += protocolCount;\n\n breakdown.push({\n protocolId,\n marketValueUSD: protocolTotalMarketValueUSD,\n chainId: chainId as Hex,\n count: protocolCount,\n });\n },\n );\n },\n );\n return {\n category: 'DeFi',\n event: 'DeFi Stats',\n properties: {\n totalMarketValueUSD,\n totalPositions,\n breakdown,\n },\n };\n}\n"]}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"group-defi-positions.cjs","sourceRoot":"","sources":["../../src/DeFiPositionsController/group-defi-positions.ts"],"names":[],"mappings":";;;AAAA,iEAAmD;
|
1
|
+
{"version":3,"file":"group-defi-positions.cjs","sourceRoot":"","sources":["../../src/DeFiPositionsController/group-defi-positions.ts"],"names":[],"mappings":";;;AAAA,iEAAmD;AAuCnD;;;;GAIG;AACH,SAAgB,kBAAkB,CAChC,qBAA6C;IAI7C,MAAM,oBAAoB,GAAyC,EAAE,CAAC;IAEtE,KAAK,MAAM,QAAQ,IAAI,qBAAqB,EAAE;QAC5C,IAAI,CAAC,QAAQ,CAAC,OAAO,EAAE;YACrB,SAAS;SACV;QAED,MAAM,EAAE,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,YAAY,EAAE,mBAAmB,EAAE,GACvE,QAAQ,CAAC;QAEX,MAAM,KAAK,GAAG,IAAA,wBAAK,EAAC,OAAO,CAAC,CAAC;QAE7B,IAAI,CAAC,oBAAoB,CAAC,KAAK,CAAC,EAAE;YAChC,oBAAoB,CAAC,KAAK,CAAC,GAAG;gBAC5B,qBAAqB,EAAE,CAAC;gBACxB,SAAS,EAAE,EAAE;aACd,CAAC;SACH;QAED,MAAM,SAAS,GAAG,oBAAoB,CAAC,KAAK,CAAC,CAAC;QAE9C,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,UAAU,CAAC,EAAE;YACpC,SAAS,CAAC,SAAS,CAAC,UAAU,CAAC,GAAG;gBAChC,eAAe,EAAE;oBACf,IAAI,EAAE,mBAAmB;oBACzB,OAAO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AK,CAAC,WAAW,GAAG,UAAU,CAAC;aACnE;SACF;KACF;IAED,OAAO,oBAAoB,CAAC;AAC9B,CAAC;AAzED,gDAyEC;AAED;;;;GAIG;AACH,SAAS,YAAY,CACnB,YAAe;IAKf,IAAI,CAAC,YAAY,CAAC,MAAM,EAAE;QACxB,OAAO;YACL,GAAG,YAAY;YACf,WAAW,EAAE,YAAY,CAAC,KAAK;gBAC7B,CAAC,CAAC,YAAY,CAAC,OAAO,GAAG,YAAY,CAAC,KAAK;gBAC3C,CAAC,CAAC,SAAS;SACd,CAAC;KACH;IAED,MAAM,eAAe,GAAG,YAAY,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE;QACpD,MAAM,EAAE,MAAM,EAAE,GAAG,uBAAuB,EAAE,GAAG,YAAY,CAAC,CAAC,CAAC,CAAC;QAE/D,OAAO,uBAAuB,CAAC;IACjC,CAAC,CAAC,CAAC;IAEH,MAAM,WAAW,GAAG,eAAe,CAAC,MAAM,CACxC,CAAC,GAAG,EAAE,CAAC,EAAE,EAAE,CACT,GAAG,KAAK,SAAS,IAAI,CAAC,CAAC,WAAW,KAAK,SAAS;QAC9C,CAAC,CAAC,SAAS;QACX,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,WAAW,EACzB,CAAuB,CACxB,CAAC;IAEF,OAAO;QACL,GAAG,YAAY;QACf,WAAW;QACX,MAAM,EAAE,eAAe;KACxB,CAAC;AACJ,CAAC","sourcesContent":["import { toHex } from '@metamask/controller-utils';\nimport type { Hex } from '@metamask/utils';\n\nimport type {\n DefiPositionResponse,\n PositionType,\n ProtocolToken,\n Underlying,\n Balance,\n} from './fetch-positions';\n\nexport type GroupedDeFiPositions = {\n aggregatedMarketValue: number;\n protocols: {\n [protocolId: string]: {\n protocolDetails: {\n name: string;\n iconUrl: string;\n };\n aggregatedMarketValue: number;\n positionTypes: {\n [key in PositionType]?: {\n aggregatedMarketValue: number;\n positions: ProtocolTokenWithMarketValue[][];\n };\n };\n };\n };\n};\n\nexport type ProtocolTokenWithMarketValue = Omit<ProtocolToken, 'tokens'> & {\n marketValue?: number;\n tokens: UnderlyingWithMarketValue[];\n};\n\nexport type UnderlyingWithMarketValue = Omit<Underlying, 'tokens'> & {\n marketValue?: number;\n};\n\n/**\n *\n * @param defiPositionsResponse - The response from the defi positions API\n * @returns The grouped positions that get assigned to the state\n */\nexport function groupDeFiPositions(\n defiPositionsResponse: DefiPositionResponse[],\n): {\n [key: Hex]: GroupedDeFiPositions;\n} {\n const groupedDeFiPositions: { [key: Hex]: GroupedDeFiPositions } = {};\n\n for (const position of defiPositionsResponse) {\n if (!position.success) {\n continue;\n }\n\n const { chainId, protocolId, iconUrl, positionType, protocolDisplayName } =\n position;\n\n const chain = toHex(chainId);\n\n if (!groupedDeFiPositions[chain]) {\n groupedDeFiPositions[chain] = {\n aggregatedMarketValue: 0,\n protocols: {},\n };\n }\n\n const chainData = groupedDeFiPositions[chain];\n\n if (!chainData.protocols[protocolId]) {\n chainData.protocols[protocolId] = {\n protocolDetails: {\n name: protocolDisplayName,\n iconUrl,\n },\n aggregatedMarketValue: 0,\n positionTypes: {},\n };\n }\n\n const protocolData = chainData.protocols[protocolId];\n\n let positionTypeData = protocolData.positionTypes[positionType];\n if (!positionTypeData) {\n positionTypeData = {\n aggregatedMarketValue: 0,\n positions: [],\n };\n protocolData.positionTypes[positionType] = positionTypeData;\n }\n\n for (const protocolToken of position.tokens) {\n const token = processToken(protocolToken) as ProtocolTokenWithMarketValue;\n\n // If groupPositions is true, we group all positions of the same type\n if (position.metadata?.groupPositions) {\n if (positionTypeData.positions.length === 0) {\n positionTypeData.positions.push([token]);\n } else {\n positionTypeData.positions[0].push(token);\n }\n } else {\n positionTypeData.positions.push([token]);\n }\n\n if (token.marketValue) {\n const multiplier = position.positionType === 'borrow' ? -1 : 1;\n\n positionTypeData.aggregatedMarketValue += token.marketValue;\n protocolData.aggregatedMarketValue += token.marketValue * multiplier;\n chainData.aggregatedMarketValue += token.marketValue * multiplier;\n }\n }\n }\n\n return groupedDeFiPositions;\n}\n\n/**\n *\n * @param tokenBalance - The token balance that is going to be processed\n * @returns The processed token balance\n */\nfunction processToken<T extends Balance>(\n tokenBalance: T,\n): T & {\n marketValue?: number;\n tokens?: UnderlyingWithMarketValue[];\n} {\n if (!tokenBalance.tokens) {\n return {\n ...tokenBalance,\n marketValue: tokenBalance.price\n ? tokenBalance.balance * tokenBalance.price\n : undefined,\n };\n }\n\n const processedTokens = tokenBalance.tokens.map((t) => {\n const { tokens, ...tokenWithoutUnderlyings } = processToken(t);\n\n return tokenWithoutUnderlyings;\n });\n\n const marketValue = processedTokens.reduce(\n (acc, t) =>\n acc === undefined || t.marketValue === undefined\n ? undefined\n : acc + t.marketValue,\n 0 as number | undefined,\n );\n\n return {\n ...tokenBalance,\n marketValue,\n tokens: processedTokens,\n };\n}\n"]}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"group-defi-positions.d.cts","sourceRoot":"","sources":["../../src/DeFiPositionsController/group-defi-positions.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAE3C,OAAO,KAAK,EACV,oBAAoB,EACpB,YAAY,EACZ,aAAa,EACb,UAAU,EAEX,8BAA0B;AAE3B,MAAM,MAAM,oBAAoB,GAAG;IACjC,qBAAqB,EAAE,MAAM,CAAC;IAC9B,SAAS,EAAE;QACT,CAAC,UAAU,EAAE,MAAM,GAAG;YACpB,eAAe,EAAE;
|
1
|
+
{"version":3,"file":"group-defi-positions.d.cts","sourceRoot":"","sources":["../../src/DeFiPositionsController/group-defi-positions.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAE3C,OAAO,KAAK,EACV,oBAAoB,EACpB,YAAY,EACZ,aAAa,EACb,UAAU,EAEX,8BAA0B;AAE3B,MAAM,MAAM,oBAAoB,GAAG;IACjC,qBAAqB,EAAE,MAAM,CAAC;IAC9B,SAAS,EAAE;QACT,CAAC,UAAU,EAAE,MAAM,GAAG;YACpB,eAAe,EAAE;gBACf,IAAI,EAAE,MAAM,CAAC;gBACb,OAAO,EAAE,MAAM,CAAC;aACjB,CAAC;YACF,qBAAqB,EAAE,MAAM,CAAC;YAC9B,aAAa,EAAE;iBACZ,GAAG,IAAI,YAAY,CAAC,CAAC,EAAE;oBACtB,qBAAqB,EAAE,MAAM,CAAC;oBAC9B,SAAS,EAAE,4BAA4B,EAAE,EAAE,CAAC;iBAC7C;aACF,CAAC;SACH,CAAC;KACH,CAAC;CACH,CAAC;AAEF,MAAM,MAAM,4BAA4B,GAAG,IAAI,CAAC,aAAa,EAAE,QAAQ,CAAC,GAAG;IACzE,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,MAAM,EAAE,yBAAyB,EAAE,CAAC;CACrC,CAAC;AAEF,MAAM,MAAM,yBAAyB,GAAG,IAAI,CAAC,UAAU,EAAE,QAAQ,CAAC,GAAG;IACnE,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,CAAC;AAEF;;;;GAIG;AACH,wBAAgB,kBAAkB,CAChC,qBAAqB,EAAE,oBAAoB,EAAE,GAC5C;IACD,CAAC,GAAG,EAAE,GAAG,GAAG,oBAAoB,CAAC;CAClC,CAqEA"}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"group-defi-positions.d.mts","sourceRoot":"","sources":["../../src/DeFiPositionsController/group-defi-positions.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAE3C,OAAO,KAAK,EACV,oBAAoB,EACpB,YAAY,EACZ,aAAa,EACb,UAAU,EAEX,8BAA0B;AAE3B,MAAM,MAAM,oBAAoB,GAAG;IACjC,qBAAqB,EAAE,MAAM,CAAC;IAC9B,SAAS,EAAE;QACT,CAAC,UAAU,EAAE,MAAM,GAAG;YACpB,eAAe,EAAE;
|
1
|
+
{"version":3,"file":"group-defi-positions.d.mts","sourceRoot":"","sources":["../../src/DeFiPositionsController/group-defi-positions.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAE3C,OAAO,KAAK,EACV,oBAAoB,EACpB,YAAY,EACZ,aAAa,EACb,UAAU,EAEX,8BAA0B;AAE3B,MAAM,MAAM,oBAAoB,GAAG;IACjC,qBAAqB,EAAE,MAAM,CAAC;IAC9B,SAAS,EAAE;QACT,CAAC,UAAU,EAAE,MAAM,GAAG;YACpB,eAAe,EAAE;gBACf,IAAI,EAAE,MAAM,CAAC;gBACb,OAAO,EAAE,MAAM,CAAC;aACjB,CAAC;YACF,qBAAqB,EAAE,MAAM,CAAC;YAC9B,aAAa,EAAE;iBACZ,GAAG,IAAI,YAAY,CAAC,CAAC,EAAE;oBACtB,qBAAqB,EAAE,MAAM,CAAC;oBAC9B,SAAS,EAAE,4BAA4B,EAAE,EAAE,CAAC;iBAC7C;aACF,CAAC;SACH,CAAC;KACH,CAAC;CACH,CAAC;AAEF,MAAM,MAAM,4BAA4B,GAAG,IAAI,CAAC,aAAa,EAAE,QAAQ,CAAC,GAAG;IACzE,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,MAAM,EAAE,yBAAyB,EAAE,CAAC;CACrC,CAAC;AAEF,MAAM,MAAM,yBAAyB,GAAG,IAAI,CAAC,UAAU,EAAE,QAAQ,CAAC,GAAG;IACnE,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,CAAC;AAEF;;;;GAIG;AACH,wBAAgB,kBAAkB,CAChC,qBAAqB,EAAE,oBAAoB,EAAE,GAC5C;IACD,CAAC,GAAG,EAAE,GAAG,GAAG,oBAAoB,CAAC;CAClC,CAqEA"}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"group-defi-positions.mjs","sourceRoot":"","sources":["../../src/DeFiPositionsController/group-defi-positions.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,mCAAmC;
|
1
|
+
{"version":3,"file":"group-defi-positions.mjs","sourceRoot":"","sources":["../../src/D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EAAE,EAAE;QACpD,MAAM,EAAE,MAAM,EAAE,GAAG,uBAAuB,EAAE,GAAG,YAAY,CAAC,CAAC,CAAC,CAAC;QAE/D,OAAO,uBAAuB,CAAC;IACjC,CAAC,CAAC,CAAC;IAEH,MAAM,WAAW,GAAG,eAAe,CAAC,MAAM,CACxC,CAAC,GAAG,EAAE,CAAC,EAAE,EAAE,CACT,GAAG,KAAK,SAAS,IAAI,CAAC,CAAC,WAAW,KAAK,SAAS;QAC9C,CAAC,CAAC,SAAS;QACX,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,WAAW,EACzB,CAAuB,CACxB,CAAC;IAEF,OAAO;QACL,GAAG,YAAY;QACf,WAAW;QACX,MAAM,EAAE,eAAe;KACxB,CAAC;AACJ,CAAC","sourcesContent":["import { toHex } from '@metamask/controller-utils';\nimport type { Hex } from '@metamask/utils';\n\nimport type {\n DefiPositionResponse,\n PositionType,\n ProtocolToken,\n Underlying,\n Balance,\n} from './fetch-positions';\n\nexport type GroupedDeFiPositions = {\n aggregatedMarketValue: number;\n protocols: {\n [protocolId: string]: {\n protocolDetails: {\n name: string;\n iconUrl: string;\n };\n aggregatedMarketValue: number;\n positionTypes: {\n [key in PositionType]?: {\n aggregatedMarketValue: number;\n positions: ProtocolTokenWithMarketValue[][];\n };\n };\n };\n };\n};\n\nexport type ProtocolTokenWithMarketValue = Omit<ProtocolToken, 'tokens'> & {\n marketValue?: number;\n tokens: UnderlyingWithMarketValue[];\n};\n\nexport type UnderlyingWithMarketValue = Omit<Underlying, 'tokens'> & {\n marketValue?: number;\n};\n\n/**\n *\n * @param defiPositionsResponse - The response from the defi positions API\n * @returns The grouped positions that get assigned to the state\n */\nexport function groupDeFiPositions(\n defiPositionsResponse: DefiPositionResponse[],\n): {\n [key: Hex]: GroupedDeFiPositions;\n} {\n const groupedDeFiPositions: { [key: Hex]: GroupedDeFiPositions } = {};\n\n for (const position of defiPositionsResponse) {\n if (!position.success) {\n continue;\n }\n\n const { chainId, protocolId, iconUrl, positionType, protocolDisplayName } =\n position;\n\n const chain = toHex(chainId);\n\n if (!groupedDeFiPositions[chain]) {\n groupedDeFiPositions[chain] = {\n aggregatedMarketValue: 0,\n protocols: {},\n };\n }\n\n const chainData = groupedDeFiPositions[chain];\n\n if (!chainData.protocols[protocolId]) {\n chainData.protocols[protocolId] = {\n protocolDetails: {\n name: protocolDisplayName,\n iconUrl,\n },\n aggregatedMarketValue: 0,\n positionTypes: {},\n };\n }\n\n const protocolData = chainData.protocols[protocolId];\n\n let positionTypeData = protocolData.positionTypes[positionType];\n if (!positionTypeData) {\n positionTypeData = {\n aggregatedMarketValue: 0,\n positions: [],\n };\n protocolData.positionTypes[positionType] = positionTypeData;\n }\n\n for (const protocolToken of position.tokens) {\n const token = processToken(protocolToken) as ProtocolTokenWithMarketValue;\n\n // If groupPositions is true, we group all positions of the same type\n if (position.metadata?.groupPositions) {\n if (positionTypeData.positions.length === 0) {\n positionTypeData.positions.push([token]);\n } else {\n positionTypeData.positions[0].push(token);\n }\n } else {\n positionTypeData.positions.push([token]);\n }\n\n if (token.marketValue) {\n const multiplier = position.positionType === 'borrow' ? -1 : 1;\n\n positionTypeData.aggregatedMarketValue += token.marketValue;\n protocolData.aggregatedMarketValue += token.marketValue * multiplier;\n chainData.aggregatedMarketValue += token.marketValue * multiplier;\n }\n }\n }\n\n return groupedDeFiPositions;\n}\n\n/**\n *\n * @param tokenBalance - The token balance that is going to be processed\n * @returns The processed token balance\n */\nfunction processToken<T extends Balance>(\n tokenBalance: T,\n): T & {\n marketValue?: number;\n tokens?: UnderlyingWithMarketValue[];\n} {\n if (!tokenBalance.tokens) {\n return {\n ...tokenBalance,\n marketValue: tokenBalance.price\n ? tokenBalance.balance * tokenBalance.price\n : undefined,\n };\n }\n\n const processedTokens = tokenBalance.tokens.map((t) => {\n const { tokens, ...tokenWithoutUnderlyings } = processToken(t);\n\n return tokenWithoutUnderlyings;\n });\n\n const marketValue = processedTokens.reduce(\n (acc, t) =>\n acc === undefined || t.marketValue === undefined\n ? undefined\n : acc + t.marketValue,\n 0 as number | undefined,\n );\n\n return {\n ...tokenBalance,\n marketValue,\n tokens: processedTokens,\n };\n}\n"]}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@metamask-previews/assets-controllers",
|
3
|
-
"version": "
|
3
|
+
"version": "66.0.0-preview-4ef4efd7",
|
4
4
|
"description": "Controllers which manage interactions involving ERC-20, ERC-721, and ERC-1155 tokens (including NFTs)",
|
5
5
|
"keywords": [
|
6
6
|
"MetaMask",
|
@@ -84,14 +84,14 @@
|
|
84
84
|
"@metamask/keyring-controller": "^22.0.0",
|
85
85
|
"@metamask/keyring-internal-api": "^6.0.1",
|
86
86
|
"@metamask/keyring-snap-client": "^4.1.0",
|
87
|
-
"@metamask/network-controller": "^23.5.
|
87
|
+
"@metamask/network-controller": "^23.5.1",
|
88
88
|
"@metamask/permission-controller": "^11.0.6",
|
89
89
|
"@metamask/phishing-controller": "^12.5.0",
|
90
|
-
"@metamask/preferences-controller": "^18.
|
90
|
+
"@metamask/preferences-controller": "^18.1.0",
|
91
91
|
"@metamask/providers": "^21.0.0",
|
92
92
|
"@metamask/snaps-controllers": "^11.2.1",
|
93
93
|
"@metamask/snaps-sdk": "^6.22.0",
|
94
|
-
"@metamask/transaction-controller": "^56.
|
94
|
+
"@metamask/transaction-controller": "^56.3.0",
|
95
95
|
"@types/jest": "^27.4.1",
|
96
96
|
"@types/lodash": "^4.14.191",
|
97
97
|
"@types/node": "^16.18.54",
|