@metamask-previews/assets-controllers 53.1.0-preview-b4d33c51 → 53.1.0-preview-9b02be34

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.
Files changed (41) hide show
  1. package/dist/DeFiPositionsController/DeFiPositionsController.cjs +82 -0
  2. package/dist/DeFiPositionsController/DeFiPositionsController.cjs.map +1 -0
  3. package/dist/DeFiPositionsController/DeFiPositionsController.d.cts +55 -0
  4. package/dist/DeFiPositionsController/DeFiPositionsController.d.cts.map +1 -0
  5. package/dist/DeFiPositionsController/DeFiPositionsController.d.mts +55 -0
  6. package/dist/DeFiPositionsController/DeFiPositionsController.d.mts.map +1 -0
  7. package/dist/DeFiPositionsController/DeFiPositionsController.mjs +77 -0
  8. package/dist/DeFiPositionsController/DeFiPositionsController.mjs.map +1 -0
  9. package/dist/DeFiPositionsController/fetch-positions.cjs +22 -0
  10. package/dist/DeFiPositionsController/fetch-positions.cjs.map +1 -0
  11. package/dist/DeFiPositionsController/fetch-positions.d.cts +59 -0
  12. package/dist/DeFiPositionsController/fetch-positions.d.cts.map +1 -0
  13. package/dist/DeFiPositionsController/fetch-positions.d.mts +59 -0
  14. package/dist/DeFiPositionsController/fetch-positions.d.mts.map +1 -0
  15. package/dist/DeFiPositionsController/fetch-positions.mjs +18 -0
  16. package/dist/DeFiPositionsController/fetch-positions.mjs.map +1 -0
  17. package/dist/DeFiPositionsController/group-positions.cjs +99 -0
  18. package/dist/DeFiPositionsController/group-positions.cjs.map +1 -0
  19. package/dist/DeFiPositionsController/group-positions.d.cts +36 -0
  20. package/dist/DeFiPositionsController/group-positions.d.cts.map +1 -0
  21. package/dist/DeFiPositionsController/group-positions.d.mts +36 -0
  22. package/dist/DeFiPositionsController/group-positions.d.mts.map +1 -0
  23. package/dist/DeFiPositionsController/group-positions.mjs +96 -0
  24. package/dist/DeFiPositionsController/group-positions.mjs.map +1 -0
  25. package/dist/DeFiPositionsController/mocks/mock-responses.cjs +600 -0
  26. package/dist/DeFiPositionsController/mocks/mock-responses.cjs.map +1 -0
  27. package/dist/DeFiPositionsController/mocks/mock-responses.d.cts +22 -0
  28. package/dist/DeFiPositionsController/mocks/mock-responses.d.cts.map +1 -0
  29. package/dist/DeFiPositionsController/mocks/mock-responses.d.mts +22 -0
  30. package/dist/DeFiPositionsController/mocks/mock-responses.d.mts.map +1 -0
  31. package/dist/DeFiPositionsController/mocks/mock-responses.mjs +597 -0
  32. package/dist/DeFiPositionsController/mocks/mock-responses.mjs.map +1 -0
  33. package/dist/index.cjs +3 -1
  34. package/dist/index.cjs.map +1 -1
  35. package/dist/index.d.cts +3 -0
  36. package/dist/index.d.cts.map +1 -1
  37. package/dist/index.d.mts +3 -0
  38. package/dist/index.d.mts.map +1 -1
  39. package/dist/index.mjs +1 -0
  40. package/dist/index.mjs.map +1 -1
  41. package/package.json +1 -1
@@ -0,0 +1,82 @@
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_updateAccountPositions;
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.DeFiPositionsController = exports.getDefaultDefiPositionsControllerState = void 0;
16
+ const base_controller_1 = require("@metamask/base-controller");
17
+ const fetch_positions_1 = require("./fetch-positions.cjs");
18
+ const group_positions_1 = require("./group-positions.cjs");
19
+ const controllerName = 'DeFiPositionsController';
20
+ const controllerMetadata = {
21
+ allDeFiPositions: {
22
+ persist: false,
23
+ anonymous: false,
24
+ },
25
+ };
26
+ const getDefaultDefiPositionsControllerState = () => {
27
+ return {
28
+ allDeFiPositions: {},
29
+ };
30
+ };
31
+ exports.getDefaultDefiPositionsControllerState = getDefaultDefiPositionsControllerState;
32
+ /**
33
+ * Controller that stores assets and exposes convenience methods
34
+ */
35
+ class DeFiPositionsController extends base_controller_1.BaseController {
36
+ /**
37
+ * Tokens controller options
38
+ *
39
+ * @param options - Constructor options.
40
+ * @param options.messenger - The controller messenger.
41
+ * @param options.state - Initial state to set on this controller.
42
+ * @param options.apiUrl - Override for theAPI URL to use for fetching DeFi positions.
43
+ */
44
+ constructor({ messenger, state, apiUrl, }) {
45
+ super({
46
+ name: controllerName,
47
+ metadata: controllerMetadata,
48
+ messenger,
49
+ state: {
50
+ ...(0, exports.getDefaultDefiPositionsControllerState)(),
51
+ ...state,
52
+ },
53
+ });
54
+ _DeFiPositionsController_instances.add(this);
55
+ _DeFiPositionsController_fetchPositions.set(this, void 0);
56
+ __classPrivateFieldSet(this, _DeFiPositionsController_fetchPositions, (0, fetch_positions_1.buildPositionFetcher)(apiUrl), "f");
57
+ this.messagingSystem.subscribe('AccountsController:selectedAccountChange', async (selectedAccount) => {
58
+ console.log('DEFI POSITIONS CONTROLLER EVENT: AccountsController:selectedAccountChange', selectedAccount.address);
59
+ await __classPrivateFieldGet(this, _DeFiPositionsController_instances, "m", _DeFiPositionsController_updateAccountPositions).call(this, selectedAccount.address);
60
+ });
61
+ this.messagingSystem.subscribe('NetworkController:stateChange', async () => {
62
+ const { address } = this.messagingSystem.call('AccountsController:getSelectedAccount');
63
+ console.log('DEFI POSITIONS CONTROLLER EVENT: NetworkController:stateChange', address);
64
+ if (address) {
65
+ await __classPrivateFieldGet(this, _DeFiPositionsController_instances, "m", _DeFiPositionsController_updateAccountPositions).call(this, address);
66
+ }
67
+ });
68
+ }
69
+ }
70
+ exports.DeFiPositionsController = DeFiPositionsController;
71
+ _DeFiPositionsController_fetchPositions = new WeakMap(), _DeFiPositionsController_instances = new WeakSet(), _DeFiPositionsController_updateAccountPositions = async function _DeFiPositionsController_updateAccountPositions(accountAddress) {
72
+ // TODO: This is done to give the UI a loading effect. Probably not the best way to do this
73
+ this.update((state) => {
74
+ state.allDeFiPositions[accountAddress] = null;
75
+ });
76
+ const defiPositionsResponse = await __classPrivateFieldGet(this, _DeFiPositionsController_fetchPositions, "f").call(this, accountAddress);
77
+ const accountPositionsPerChain = (0, group_positions_1.groupPositions)(defiPositionsResponse);
78
+ this.update((state) => {
79
+ state.allDeFiPositions[accountAddress] = accountPositionsPerChain;
80
+ });
81
+ };
82
+ //# sourceMappingURL=DeFiPositionsController.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"DeFiPositionsController.cjs","sourceRoot":"","sources":["../../src/DeFiPositionsController/DeFiPositionsController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAUA,+DAA2D;AAK3D,2DAAyD;AACzD,2DAA0E;AAE1E,MAAM,cAAc,GAAG,yBAAyB,CAAC;AAWjD,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;AA0CJ;;GAEG;AACH,MAAa,uBAAwB,SAAQ,gCAI5C;IAKC;;;;;;;OAOG;IACH,YAAY,EACV,SAAS,EACT,KAAK,EACL,MAAM,GAKP;QACC,KAAK,CAAC;YACJ,IAAI,EAAE,c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;IAEvE,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;QACpB,KAAK,CAAC,gBAAgB,CAAC,cAAc,CAAC,GAAG,wBAAwB,CAAC;IACpE,CAAC,CAAC,CAAC;AACL,CAAC","sourcesContent":["import type {\n AccountsControllerGetSelectedAccountAction,\n AccountsControllerSelectedAccountChangeEvent,\n} from '@metamask/accounts-controller';\nimport type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n RestrictedMessenger,\n StateMetadata,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport type { NetworkControllerStateChangeEvent } from '@metamask/network-controller';\nimport type { Hex } from '@metamask/utils';\n\nimport type { DefiPositionResponse } from './fetch-positions';\nimport { buildPositionFetcher } from './fetch-positions';\nimport { groupPositions, type GroupedPositions } from './group-positions';\n\nconst controllerName = 'DeFiPositionsController';\n\nexport type DeFiPositionsControllerState = {\n /**\n * Object containing DeFi positions per account and network\n */\n allDeFiPositions: {\n [accountAddress: string]: { [chain: Hex]: GroupedPositions } | 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 = AccountsControllerGetSelectedAccountAction;\n\n/**\n * The external events available to the {@link DeFiPositionsController}.\n */\nexport type AllowedEvents =\n | NetworkControllerStateChangeEvent\n | AccountsControllerSelectedAccountChangeEvent;\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 BaseController<\n typeof controllerName,\n DeFiPositionsControllerState,\n DeFiPositionsControllerMessenger\n> {\n readonly #fetchPositions: (\n accountAddress: string,\n ) => Promise<DefiPositionResponse[]>;\n\n /**\n * Tokens controller options\n *\n * @param options - Constructor options.\n * @param options.messenger - The controller messenger.\n * @param options.state - Initial state to set on this controller.\n * @param options.apiUrl - Override for theAPI URL to use for fetching DeFi positions.\n */\n constructor({\n messenger,\n state,\n apiUrl,\n }: {\n messenger: DeFiPositionsControllerMessenger;\n state?: Partial<DeFiPositionsControllerState>;\n apiUrl?: string;\n }) {\n super({\n name: controllerName,\n metadata: controllerMetadata,\n messenger,\n state: {\n ...getDefaultDefiPositionsControllerState(),\n ...state,\n },\n });\n\n this.#fetchPositions = buildPositionFetcher(apiUrl);\n\n this.messagingSystem.subscribe(\n 'AccountsController:selectedAccountChange',\n async (selectedAccount) => {\n console.log(\n 'DEFI POSITIONS CONTROLLER EVENT: AccountsController:selectedAccountChange',\n selectedAccount.address,\n );\n await this.#updateAccountPositions(selectedAccount.address);\n },\n );\n\n this.messagingSystem.subscribe(\n 'NetworkController:stateChange',\n async () => {\n const { address } = this.messagingSystem.call(\n 'AccountsController:getSelectedAccount',\n );\n console.log(\n 'DEFI POSITIONS CONTROLLER EVENT: NetworkController:stateChange',\n address,\n );\n\n if (address) {\n await this.#updateAccountPositions(address);\n }\n },\n );\n }\n\n async #updateAccountPositions(accountAddress: string) {\n // TODO: This is done to give the UI a loading effect. Probably not the best way to do this\n this.update((state) => {\n state.allDeFiPositions[accountAddress] = null;\n });\n\n const defiPositionsResponse = await this.#fetchPositions(accountAddress);\n\n const accountPositionsPerChain = groupPositions(defiPositionsResponse);\n\n this.update((state) => {\n state.allDeFiPositions[accountAddress] = accountPositionsPerChain;\n });\n }\n}\n"]}
@@ -0,0 +1,55 @@
1
+ import type { AccountsControllerGetSelectedAccountAction, AccountsControllerSelectedAccountChangeEvent } from "@metamask/accounts-controller";
2
+ import type { ControllerGetStateAction, ControllerStateChangeEvent, RestrictedMessenger } from "@metamask/base-controller";
3
+ import { BaseController } from "@metamask/base-controller";
4
+ import type { NetworkControllerStateChangeEvent } from "@metamask/network-controller";
5
+ import type { Hex } from "@metamask/utils";
6
+ import { type GroupedPositions } from "./group-positions.cjs";
7
+ declare const controllerName = "DeFiPositionsController";
8
+ export type DeFiPositionsControllerState = {
9
+ /**
10
+ * Object containing DeFi positions per account and network
11
+ */
12
+ allDeFiPositions: {
13
+ [accountAddress: string]: {
14
+ [chain: Hex]: GroupedPositions;
15
+ } | null;
16
+ };
17
+ };
18
+ export declare const getDefaultDefiPositionsControllerState: () => DeFiPositionsControllerState;
19
+ export type DeFiPositionsControllerActions = DeFiPositionsControllerGetStateAction;
20
+ export type DeFiPositionsControllerGetStateAction = ControllerGetStateAction<typeof controllerName, DeFiPositionsControllerState>;
21
+ export type DeFiPositionsControllerEvents = DeFiPositionsControllerStateChangeEvent;
22
+ export type DeFiPositionsControllerStateChangeEvent = ControllerStateChangeEvent<typeof controllerName, DeFiPositionsControllerState>;
23
+ /**
24
+ * The external actions available to the {@link DeFiPositionsController}.
25
+ */
26
+ export type AllowedActions = AccountsControllerGetSelectedAccountAction;
27
+ /**
28
+ * The external events available to the {@link DeFiPositionsController}.
29
+ */
30
+ export type AllowedEvents = NetworkControllerStateChangeEvent | AccountsControllerSelectedAccountChangeEvent;
31
+ /**
32
+ * The messenger of the {@link DeFiPositionsController}.
33
+ */
34
+ export type DeFiPositionsControllerMessenger = RestrictedMessenger<typeof controllerName, DeFiPositionsControllerActions | AllowedActions, DeFiPositionsControllerEvents | AllowedEvents, AllowedActions['type'], AllowedEvents['type']>;
35
+ /**
36
+ * Controller that stores assets and exposes convenience methods
37
+ */
38
+ export declare class DeFiPositionsController extends BaseController<typeof controllerName, DeFiPositionsControllerState, DeFiPositionsControllerMessenger> {
39
+ #private;
40
+ /**
41
+ * Tokens controller options
42
+ *
43
+ * @param options - Constructor options.
44
+ * @param options.messenger - The controller messenger.
45
+ * @param options.state - Initial state to set on this controller.
46
+ * @param options.apiUrl - Override for theAPI URL to use for fetching DeFi positions.
47
+ */
48
+ constructor({ messenger, state, apiUrl, }: {
49
+ messenger: DeFiPositionsControllerMessenger;
50
+ state?: Partial<DeFiPositionsControllerState>;
51
+ apiUrl?: string;
52
+ });
53
+ }
54
+ export {};
55
+ //# 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,0CAA0C,EAC1C,4CAA4C,EAC7C,sCAAsC;AACvC,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAC1B,mBAAmB,EAEpB,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EAAE,iCAAiC,EAAE,qCAAqC;AACtF,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAI3C,OAAO,EAAkB,KAAK,gBAAgB,EAAE,8BAA0B;AAE1E,QAAA,MAAM,cAAc,4BAA4B,CAAC;AAEjD,MAAM,MAAM,4BAA4B,GAAG;IACzC;;OAEG;IACH,gBAAgB,EAAE;QAChB,CAAC,cAAc,EAAE,MAAM,GAAG;YAAE,CAAC,KAAK,EAAE,GAAG,GAAG,gBAAgB,CAAA;SAAE,GAAG,IAAI,CAAC;KACrE,CAAC;CACH,CAAC;AASF,eAAO,MAAM,sCAAsC,QAC7C,4BAIH,CAAC;AAEJ,MAAM,MAAM,8BAA8B,GACxC,qCAAqC,CAAC;AAExC,MAAM,MAAM,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,0CAA0C,CAAC;AAExE;;GAEG;AACH,MAAM,MAAM,aAAa,GACrB,iCAAiC,GACjC,4CAA4C,CAAC;AAEjD;;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,cAAc,CACzD,OAAO,cAAc,EACrB,4BAA4B,EAC5B,gCAAgC,CACjC;;IAKC;;;;;;;OAOG;gBACS,EACV,SAAS,EACT,KAAK,EACL,MAAM,GACP,EAAE;QACD,SAAS,EAAE,gCAAgC,CAAC;QAC5C,KAAK,CAAC,EAAE,OAAO,CAAC,4BAA4B,CAAC,CAAC;QAC9C,MAAM,CAAC,EAAE,MAAM,CAAC;KACjB;CAwDF"}
@@ -0,0 +1,55 @@
1
+ import type { AccountsControllerGetSelectedAccountAction, AccountsControllerSelectedAccountChangeEvent } from "@metamask/accounts-controller";
2
+ import type { ControllerGetStateAction, ControllerStateChangeEvent, RestrictedMessenger } from "@metamask/base-controller";
3
+ import { BaseController } from "@metamask/base-controller";
4
+ import type { NetworkControllerStateChangeEvent } from "@metamask/network-controller";
5
+ import type { Hex } from "@metamask/utils";
6
+ import { type GroupedPositions } from "./group-positions.mjs";
7
+ declare const controllerName = "DeFiPositionsController";
8
+ export type DeFiPositionsControllerState = {
9
+ /**
10
+ * Object containing DeFi positions per account and network
11
+ */
12
+ allDeFiPositions: {
13
+ [accountAddress: string]: {
14
+ [chain: Hex]: GroupedPositions;
15
+ } | null;
16
+ };
17
+ };
18
+ export declare const getDefaultDefiPositionsControllerState: () => DeFiPositionsControllerState;
19
+ export type DeFiPositionsControllerActions = DeFiPositionsControllerGetStateAction;
20
+ export type DeFiPositionsControllerGetStateAction = ControllerGetStateAction<typeof controllerName, DeFiPositionsControllerState>;
21
+ export type DeFiPositionsControllerEvents = DeFiPositionsControllerStateChangeEvent;
22
+ export type DeFiPositionsControllerStateChangeEvent = ControllerStateChangeEvent<typeof controllerName, DeFiPositionsControllerState>;
23
+ /**
24
+ * The external actions available to the {@link DeFiPositionsController}.
25
+ */
26
+ export type AllowedActions = AccountsControllerGetSelectedAccountAction;
27
+ /**
28
+ * The external events available to the {@link DeFiPositionsController}.
29
+ */
30
+ export type AllowedEvents = NetworkControllerStateChangeEvent | AccountsControllerSelectedAccountChangeEvent;
31
+ /**
32
+ * The messenger of the {@link DeFiPositionsController}.
33
+ */
34
+ export type DeFiPositionsControllerMessenger = RestrictedMessenger<typeof controllerName, DeFiPositionsControllerActions | AllowedActions, DeFiPositionsControllerEvents | AllowedEvents, AllowedActions['type'], AllowedEvents['type']>;
35
+ /**
36
+ * Controller that stores assets and exposes convenience methods
37
+ */
38
+ export declare class DeFiPositionsController extends BaseController<typeof controllerName, DeFiPositionsControllerState, DeFiPositionsControllerMessenger> {
39
+ #private;
40
+ /**
41
+ * Tokens controller options
42
+ *
43
+ * @param options - Constructor options.
44
+ * @param options.messenger - The controller messenger.
45
+ * @param options.state - Initial state to set on this controller.
46
+ * @param options.apiUrl - Override for theAPI URL to use for fetching DeFi positions.
47
+ */
48
+ constructor({ messenger, state, apiUrl, }: {
49
+ messenger: DeFiPositionsControllerMessenger;
50
+ state?: Partial<DeFiPositionsControllerState>;
51
+ apiUrl?: string;
52
+ });
53
+ }
54
+ export {};
55
+ //# 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,0CAA0C,EAC1C,4CAA4C,EAC7C,sCAAsC;AACvC,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAC1B,mBAAmB,EAEpB,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EAAE,iCAAiC,EAAE,qCAAqC;AACtF,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAI3C,OAAO,EAAkB,KAAK,gBAAgB,EAAE,8BAA0B;AAE1E,QAAA,MAAM,cAAc,4BAA4B,CAAC;AAEjD,MAAM,MAAM,4BAA4B,GAAG;IACzC;;OAEG;IACH,gBAAgB,EAAE;QAChB,CAAC,cAAc,EAAE,MAAM,GAAG;YAAE,CAAC,KAAK,EAAE,GAAG,GAAG,gBAAgB,CAAA;SAAE,GAAG,IAAI,CAAC;KACrE,CAAC;CACH,CAAC;AASF,eAAO,MAAM,sCAAsC,QAC7C,4BAIH,CAAC;AAEJ,MAAM,MAAM,8BAA8B,GACxC,qCAAqC,CAAC;AAExC,MAAM,MAAM,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,0CAA0C,CAAC;AAExE;;GAEG;AACH,MAAM,MAAM,aAAa,GACrB,iCAAiC,GACjC,4CAA4C,CAAC;AAEjD;;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,cAAc,CACzD,OAAO,cAAc,EACrB,4BAA4B,EAC5B,gCAAgC,CACjC;;IAKC;;;;;;;OAOG;gBACS,EACV,SAAS,EACT,KAAK,EACL,MAAM,GACP,EAAE;QACD,SAAS,EAAE,gCAAgC,CAAC;QAC5C,KAAK,CAAC,EAAE,OAAO,CAAC,4BAA4B,CAAC,CAAC;QAC9C,MAAM,CAAC,EAAE,MAAM,CAAC;KACjB;CAwDF"}
@@ -0,0 +1,77 @@
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_updateAccountPositions;
13
+ import { BaseController } from "@metamask/base-controller";
14
+ import { buildPositionFetcher } from "./fetch-positions.mjs";
15
+ import { groupPositions } from "./group-positions.mjs";
16
+ const controllerName = 'DeFiPositionsController';
17
+ const controllerMetadata = {
18
+ allDeFiPositions: {
19
+ persist: false,
20
+ anonymous: false,
21
+ },
22
+ };
23
+ export const getDefaultDefiPositionsControllerState = () => {
24
+ return {
25
+ allDeFiPositions: {},
26
+ };
27
+ };
28
+ /**
29
+ * Controller that stores assets and exposes convenience methods
30
+ */
31
+ export class DeFiPositionsController extends BaseController {
32
+ /**
33
+ * Tokens controller options
34
+ *
35
+ * @param options - Constructor options.
36
+ * @param options.messenger - The controller messenger.
37
+ * @param options.state - Initial state to set on this controller.
38
+ * @param options.apiUrl - Override for theAPI URL to use for fetching DeFi positions.
39
+ */
40
+ constructor({ messenger, state, apiUrl, }) {
41
+ super({
42
+ name: controllerName,
43
+ metadata: controllerMetadata,
44
+ messenger,
45
+ state: {
46
+ ...getDefaultDefiPositionsControllerState(),
47
+ ...state,
48
+ },
49
+ });
50
+ _DeFiPositionsController_instances.add(this);
51
+ _DeFiPositionsController_fetchPositions.set(this, void 0);
52
+ __classPrivateFieldSet(this, _DeFiPositionsController_fetchPositions, buildPositionFetcher(apiUrl), "f");
53
+ this.messagingSystem.subscribe('AccountsController:selectedAccountChange', async (selectedAccount) => {
54
+ console.log('DEFI POSITIONS CONTROLLER EVENT: AccountsController:selectedAccountChange', selectedAccount.address);
55
+ await __classPrivateFieldGet(this, _DeFiPositionsController_instances, "m", _DeFiPositionsController_updateAccountPositions).call(this, selectedAccount.address);
56
+ });
57
+ this.messagingSystem.subscribe('NetworkController:stateChange', async () => {
58
+ const { address } = this.messagingSystem.call('AccountsController:getSelectedAccount');
59
+ console.log('DEFI POSITIONS CONTROLLER EVENT: NetworkController:stateChange', address);
60
+ if (address) {
61
+ await __classPrivateFieldGet(this, _DeFiPositionsController_instances, "m", _DeFiPositionsController_updateAccountPositions).call(this, address);
62
+ }
63
+ });
64
+ }
65
+ }
66
+ _DeFiPositionsController_fetchPositions = new WeakMap(), _DeFiPositionsController_instances = new WeakSet(), _DeFiPositionsController_updateAccountPositions = async function _DeFiPositionsController_updateAccountPositions(accountAddress) {
67
+ // TODO: This is done to give the UI a loading effect. Probably not the best way to do this
68
+ this.update((state) => {
69
+ state.allDeFiPositions[accountAddress] = null;
70
+ });
71
+ const defiPositionsResponse = await __classPrivateFieldGet(this, _DeFiPositionsController_fetchPositions, "f").call(this, accountAddress);
72
+ const accountPositionsPerChain = groupPositions(defiPositionsResponse);
73
+ this.update((state) => {
74
+ state.allDeFiPositions[accountAddress] = accountPositionsPerChain;
75
+ });
76
+ };
77
+ //# sourceMappingURL=DeFiPositionsController.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"DeFiPositionsController.mjs","sourceRoot":"","sources":["../../src/DeFiPositionsController/DeFiPositionsController.ts"],"names":[],"mappings":";;;;;;;;;;;;AAUA,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAK3D,OAAO,EAAE,oBAAoB,EAAE,8BAA0B;AACzD,OAAO,EAAE,cAAc,EAAyB,8BAA0B;AAE1E,MAAM,cAAc,GAAG,yBAAyB,CAAC;AAWjD,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;AA0CJ;;GAEG;AACH,MAAM,OAAO,uBAAwB,SAAQ,cAI5C;IAKC;;;;;;;OAOG;IACH,YAAY,EACV,SAAS,EACT,KAAK,EACL,MAAM,GAKP;QACC,KAAK,CAAC;YACJ,IAAI,EAAE,cAAc;YACpB,QAAQ,EAAE,kBAAkB;YAC5B,SAAS;YACT,KAAK,EAAE;gBACL,GAAG,sCAAsC,EAAE;gBAC3C,GAAG,KAAK;aACT;SACF,CAAC,CAAC;;QA7BI,0DAE4B;QA6BnC,uBAAA,IAAI,2CAAmB,oBAAoB,CAAC,MAAM,CAAC,MAAA,CAAC;QAEpD,IAAI,CAAC,eAAe,CAAC,SAAS,CAC5B,0CAA0C,EAC1C,KAAK,EAAE,eAAe,EAAE,EAAE;YACxB,OAAO,CAAC,GAAG,CACT,2EAA2E,EAC3E,eAAe,CAAC,OAAO,CACxB,CAAC;YACF,MAAM,uBAAA,IAAI,2FAAwB,MAA5B,IAAI,EAAyB,eAAe,CAAC,OAAO,CAAC,CAAC;QAC9D,CAAC,CACF,CAAC;QAEF,IAAI,CAAC,eAAe,CAAC,SAAS,CAC5B,+BAA+B,EAC/B,KAAK,IAAI,EAAE;YACT,MAAM,EAAE,OAAO,EAAE,GAAG,IAAI,CAAC,eAAe,CAAC,IAAI,CAC3C,uCAAuC,CACxC,CAAC;YACF,OAAO,CAAC,GAAG,CACT,gEAAgE,EAChE,OAAO,CACR,CAAC;YAEF,IAAI,OAAO,EAAE;gBACX,MAAM,uBAAA,IAAI,2FAAwB,MAA5B,IAAI,EAAyB,OAAO,CAAC,CAAC;aAC7C;QACH,CAAC,CACF,CAAC;IACJ,CAAC;CAgBF;+JAdC,KAAK,0DAAyB,cAAsB;IAClD,2FAA2F;IAC3F,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;QACpB,KAAK,CAAC,gBAAgB,CAAC,cAAc,CAAC,GAAG,IAAI,CAAC;IAChD,CAAC,CAAC,CAAC;IAEH,MAAM,qBAAqB,GAAG,MAAM,uBAAA,IAAI,+CAAgB,MAApB,IAAI,EAAiB,cAAc,CAAC,CAAC;IAEzE,MAAM,wBAAwB,GAAG,cAAc,CAAC,qBAAqB,CAAC,CAAC;IAEvE,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;QACpB,KAAK,CAAC,gBAAgB,CAAC,cAAc,CAAC,GAAG,wBAAwB,CAAC;IACpE,CAAC,CAAC,CAAC;AACL,CAAC","sourcesContent":["import type {\n AccountsControllerGetSelectedAccountAction,\n AccountsControllerSelectedAccountChangeEvent,\n} from '@metamask/accounts-controller';\nimport type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n RestrictedMessenger,\n StateMetadata,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport type { NetworkControllerStateChangeEvent } from '@metamask/network-controller';\nimport type { Hex } from '@metamask/utils';\n\nimport type { DefiPositionResponse } from './fetch-positions';\nimport { buildPositionFetcher } from './fetch-positions';\nimport { groupPositions, type GroupedPositions } from './group-positions';\n\nconst controllerName = 'DeFiPositionsController';\n\nexport type DeFiPositionsControllerState = {\n /**\n * Object containing DeFi positions per account and network\n */\n allDeFiPositions: {\n [accountAddress: string]: { [chain: Hex]: GroupedPositions } | 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 = AccountsControllerGetSelectedAccountAction;\n\n/**\n * The external events available to the {@link DeFiPositionsController}.\n */\nexport type AllowedEvents =\n | NetworkControllerStateChangeEvent\n | AccountsControllerSelectedAccountChangeEvent;\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 BaseController<\n typeof controllerName,\n DeFiPositionsControllerState,\n DeFiPositionsControllerMessenger\n> {\n readonly #fetchPositions: (\n accountAddress: string,\n ) => Promise<DefiPositionResponse[]>;\n\n /**\n * Tokens controller options\n *\n * @param options - Constructor options.\n * @param options.messenger - The controller messenger.\n * @param options.state - Initial state to set on this controller.\n * @param options.apiUrl - Override for theAPI URL to use for fetching DeFi positions.\n */\n constructor({\n messenger,\n state,\n apiUrl,\n }: {\n messenger: DeFiPositionsControllerMessenger;\n state?: Partial<DeFiPositionsControllerState>;\n apiUrl?: string;\n }) {\n super({\n name: controllerName,\n metadata: controllerMetadata,\n messenger,\n state: {\n ...getDefaultDefiPositionsControllerState(),\n ...state,\n },\n });\n\n this.#fetchPositions = buildPositionFetcher(apiUrl);\n\n this.messagingSystem.subscribe(\n 'AccountsController:selectedAccountChange',\n async (selectedAccount) => {\n console.log(\n 'DEFI POSITIONS CONTROLLER EVENT: AccountsController:selectedAccountChange',\n selectedAccount.address,\n );\n await this.#updateAccountPositions(selectedAccount.address);\n },\n );\n\n this.messagingSystem.subscribe(\n 'NetworkController:stateChange',\n async () => {\n const { address } = this.messagingSystem.call(\n 'AccountsController:getSelectedAccount',\n );\n console.log(\n 'DEFI POSITIONS CONTROLLER EVENT: NetworkController:stateChange',\n address,\n );\n\n if (address) {\n await this.#updateAccountPositions(address);\n }\n },\n );\n }\n\n async #updateAccountPositions(accountAddress: string) {\n // TODO: This is done to give the UI a loading effect. Probably not the best way to do this\n this.update((state) => {\n state.allDeFiPositions[accountAddress] = null;\n });\n\n const defiPositionsResponse = await this.#fetchPositions(accountAddress);\n\n const accountPositionsPerChain = groupPositions(defiPositionsResponse);\n\n this.update((state) => {\n state.allDeFiPositions[accountAddress] = accountPositionsPerChain;\n });\n }\n}\n"]}
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.buildPositionFetcher = exports.DEFAULT_DEFI_POSITIONS_API_URL = void 0;
4
+ // TODO: Update with new API URL
5
+ exports.DEFAULT_DEFI_POSITIONS_API_URL = 'https://defi-services.metamask-institutional.io/defi-data/positions';
6
+ /**
7
+ * Builds a function that fetches DeFi positions for a given account address
8
+ *
9
+ * @param apiUrl - The URL of the API to fetch the DeFi positions from
10
+ * @returns A function that fetches DeFi positions for a given account address
11
+ */
12
+ function buildPositionFetcher(apiUrl = exports.DEFAULT_DEFI_POSITIONS_API_URL) {
13
+ return async (accountAddress) => {
14
+ const defiPositionsResponse = await fetch(`${apiUrl}/${accountAddress}`);
15
+ if (defiPositionsResponse.status !== 200) {
16
+ throw new Error(`Unable to fetch defi positions - HTTP ${defiPositionsResponse.status}`);
17
+ }
18
+ return (await defiPositionsResponse.json()).data;
19
+ };
20
+ }
21
+ exports.buildPositionFetcher = buildPositionFetcher;
22
+ //# sourceMappingURL=fetch-positions.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fetch-positions.cjs","sourceRoot":"","sources":["../../src/DeFiPositionsController/fetch-positions.ts"],"names":[],"mappings":";;;AAwDA,gCAAgC;AACnB,QAAA,8BAA8B,GACzC,qEAAqE,CAAC;AAExE;;;;;GAKG;AACH,SAAgB,oBAAoB,CAClC,SAAiB,sCAA8B;IAE/C,OAAO,KAAK,EAAE,cAAsB,EAAmC,EAAE;QACvE,MAAM,qBAAqB,GAAG,MAAM,KAAK,CAAC,GAAG,MAAM,IAAI,cAAc,EAAE,CAAC,CAAC;QAEzE,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 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; // TODO: Confirm this is the case\n tokens?: Underlying[];\n};\n\n// TODO: Update with new API URL\nexport const DEFAULT_DEFI_POSITIONS_API_URL =\n 'https://defi-services.metamask-institutional.io/defi-data/positions';\n\n/**\n * Builds a function that fetches DeFi positions for a given account address\n *\n * @param apiUrl - The URL of the API to fetch the DeFi positions from\n * @returns A function that fetches DeFi positions for a given account address\n */\nexport function buildPositionFetcher(\n apiUrl: string = DEFAULT_DEFI_POSITIONS_API_URL,\n) {\n return async (accountAddress: string): Promise<DefiPositionResponse[]> => {\n const defiPositionsResponse = await fetch(`${apiUrl}/${accountAddress}`);\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
+ name: string;
9
+ description: string;
10
+ iconUrl: string;
11
+ siteUrl: string;
12
+ positionType: PositionType;
13
+ metadata?: {
14
+ groupPositions?: boolean;
15
+ };
16
+ };
17
+ type AdapterResponse<ProtocolResponse> = (ProtocolDetails & {
18
+ chainName: string;
19
+ } & ((ProtocolResponse & {
20
+ success: true;
21
+ }) | (AdapterErrorResponse & {
22
+ success: false;
23
+ }))) | (AdapterErrorResponse & {
24
+ success: false;
25
+ });
26
+ type AdapterErrorResponse = {
27
+ error: {
28
+ message: string;
29
+ };
30
+ };
31
+ export type PositionType = 'supply' | 'borrow' | 'stake' | 'reward';
32
+ export type ProtocolToken = Balance & {
33
+ type: 'protocol';
34
+ tokenId?: string;
35
+ };
36
+ export type Underlying = Balance & {
37
+ type: 'underlying' | 'underlying-claimable';
38
+ iconUrl: string;
39
+ };
40
+ export type Balance = {
41
+ address: string;
42
+ name: string;
43
+ symbol: string;
44
+ decimals: number;
45
+ balanceRaw: string;
46
+ balance: number;
47
+ price?: number;
48
+ tokens?: Underlying[];
49
+ };
50
+ export declare const DEFAULT_DEFI_POSITIONS_API_URL = "https://defi-services.metamask-institutional.io/defi-data/positions";
51
+ /**
52
+ * Builds a function that fetches DeFi positions for a given account address
53
+ *
54
+ * @param apiUrl - The URL of the API to fetch the DeFi positions from
55
+ * @returns A function that fetches DeFi positions for a given account address
56
+ */
57
+ export declare function buildPositionFetcher(apiUrl?: string): (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,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,OAAO,EAAE,MAAM,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EAC4B,CAAC;AAExE;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAClC,MAAM,GAAE,MAAuC,oBAEjB,MAAM,KAAG,QAAQ,oBAAoB,EAAE,CAAC,CAWvE"}
@@ -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
+ name: string;
9
+ description: string;
10
+ iconUrl: string;
11
+ siteUrl: string;
12
+ positionType: PositionType;
13
+ metadata?: {
14
+ groupPositions?: boolean;
15
+ };
16
+ };
17
+ type AdapterResponse<ProtocolResponse> = (ProtocolDetails & {
18
+ chainName: string;
19
+ } & ((ProtocolResponse & {
20
+ success: true;
21
+ }) | (AdapterErrorResponse & {
22
+ success: false;
23
+ }))) | (AdapterErrorResponse & {
24
+ success: false;
25
+ });
26
+ type AdapterErrorResponse = {
27
+ error: {
28
+ message: string;
29
+ };
30
+ };
31
+ export type PositionType = 'supply' | 'borrow' | 'stake' | 'reward';
32
+ export type ProtocolToken = Balance & {
33
+ type: 'protocol';
34
+ tokenId?: string;
35
+ };
36
+ export type Underlying = Balance & {
37
+ type: 'underlying' | 'underlying-claimable';
38
+ iconUrl: string;
39
+ };
40
+ export type Balance = {
41
+ address: string;
42
+ name: string;
43
+ symbol: string;
44
+ decimals: number;
45
+ balanceRaw: string;
46
+ balance: number;
47
+ price?: number;
48
+ tokens?: Underlying[];
49
+ };
50
+ export declare const DEFAULT_DEFI_POSITIONS_API_URL = "https://defi-services.metamask-institutional.io/defi-data/positions";
51
+ /**
52
+ * Builds a function that fetches DeFi positions for a given account address
53
+ *
54
+ * @param apiUrl - The URL of the API to fetch the DeFi positions from
55
+ * @returns A function that fetches DeFi positions for a given account address
56
+ */
57
+ export declare function buildPositionFetcher(apiUrl?: string): (accountAddress: string) => Promise<DefiPositionResponse[]>;
58
+ export {};
59
+ //# sourceMappingURL=fetch-positions.d.mts.map
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,EAAE,YAAY,GAAG,sBAAsB,CAAC;IAC5C,OAAO,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF,MAAM,MAAM,OAAO,GAAG;IACpB,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,MAAM,CAAC;IACjB,UAAU,EAAE,MAAM,CAAC;IACnB,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,UAAU,EAAE,CAAC;CACvB,CAAC;AAGF,eAAO,MAAM,8BAA8B,wEAC4B,CAAC;AAExE;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAClC,MAAM,GAAE,MAAuC,oBAEjB,MAAM,KAAG,QAAQ,oBAAoB,EAAE,CAAC,CAWvE"}
@@ -0,0 +1,18 @@
1
+ // TODO: Update with new API URL
2
+ export const DEFAULT_DEFI_POSITIONS_API_URL = 'https://defi-services.metamask-institutional.io/defi-data/positions';
3
+ /**
4
+ * Builds a function that fetches DeFi positions for a given account address
5
+ *
6
+ * @param apiUrl - The URL of the API to fetch the DeFi positions from
7
+ * @returns A function that fetches DeFi positions for a given account address
8
+ */
9
+ export function buildPositionFetcher(apiUrl = DEFAULT_DEFI_POSITIONS_API_URL) {
10
+ return async (accountAddress) => {
11
+ const defiPositionsResponse = await fetch(`${apiUrl}/${accountAddress}`);
12
+ if (defiPositionsResponse.status !== 200) {
13
+ throw new Error(`Unable to fetch defi positions - HTTP ${defiPositionsResponse.status}`);
14
+ }
15
+ return (await defiPositionsResponse.json()).data;
16
+ };
17
+ }
18
+ //# sourceMappingURL=fetch-positions.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fetch-positions.mjs","sourceRoot":"","sources":["../../src/DeFiPositionsController/fetch-positions.ts"],"names":[],"mappings":"AAwDA,gCAAgC;AAChC,MAAM,CAAC,MAAM,8BAA8B,GACzC,qEAAqE,CAAC;AAExE;;;;;GAKG;AACH,MAAM,UAAU,oBAAoB,CAClC,SAAiB,8BAA8B;IAE/C,OAAO,KAAK,EAAE,cAAsB,EAAmC,EAAE;QACvE,MAAM,qBAAqB,GAAG,MAAM,KAAK,CAAC,GAAG,MAAM,IAAI,cAAc,EAAE,CAAC,CAAC;QAEzE,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","sourcesContent":["export type DefiPositionResponse = AdapterResponse<{\n tokens: ProtocolToken[];\n}>;\n\ntype ProtocolDetails = {\n chainId: number;\n protocolId: string;\n productId: 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; // TODO: Confirm this is the case\n tokens?: Underlying[];\n};\n\n// TODO: Update with new API URL\nexport const DEFAULT_DEFI_POSITIONS_API_URL =\n 'https://defi-services.metamask-institutional.io/defi-data/positions';\n\n/**\n * Builds a function that fetches DeFi positions for a given account address\n *\n * @param apiUrl - The URL of the API to fetch the DeFi positions from\n * @returns A function that fetches DeFi positions for a given account address\n */\nexport function buildPositionFetcher(\n apiUrl: string = DEFAULT_DEFI_POSITIONS_API_URL,\n) {\n return async (accountAddress: string): Promise<DefiPositionResponse[]> => {\n const defiPositionsResponse = await fetch(`${apiUrl}/${accountAddress}`);\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,99 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.groupPositions = void 0;
4
+ const controller_utils_1 = require("@metamask/controller-utils");
5
+ const lodash_1 = require("lodash");
6
+ /**
7
+ *
8
+ * @param defiPositionsResponse - The response from the defi positions API
9
+ * @returns The grouped positions that get assigned to the state
10
+ */
11
+ function groupPositions(defiPositionsResponse) {
12
+ const groupedPositions = {};
13
+ for (const position of defiPositionsResponse) {
14
+ if (!position.success) {
15
+ continue;
16
+ }
17
+ const { chainId, protocolId, iconUrl, positionType } = position;
18
+ const chain = (0, controller_utils_1.toHex)(chainId);
19
+ if (!groupedPositions[chain]) {
20
+ groupedPositions[chain] = {
21
+ aggregatedMarketValue: 0,
22
+ protocols: {},
23
+ };
24
+ }
25
+ const chainData = groupedPositions[chain];
26
+ if (!chainData.protocols[protocolId]) {
27
+ chainData.protocols[protocolId] = {
28
+ protocolDetails: {
29
+ // TODO: Prepare better source for protocol name
30
+ name: (0, lodash_1.upperFirst)((0, lodash_1.camelCase)(protocolId)),
31
+ // TODO: Picking icon url from the first product position might not be consistent
32
+ iconUrl,
33
+ },
34
+ aggregatedMarketValue: 0,
35
+ positionTypes: {},
36
+ };
37
+ }
38
+ const protocolData = chainData.protocols[protocolId];
39
+ let positionTypeData = protocolData.positionTypes[positionType];
40
+ if (!positionTypeData) {
41
+ positionTypeData = {
42
+ aggregatedMarketValue: 0,
43
+ positions: [],
44
+ };
45
+ protocolData.positionTypes[positionType] = positionTypeData;
46
+ }
47
+ for (const protocolToken of position.tokens) {
48
+ const token = processToken(protocolToken);
49
+ // If groupPositions is true, we group all positions of the same type
50
+ if (position.metadata?.groupPositions) {
51
+ if (positionTypeData.positions.length === 0) {
52
+ positionTypeData.positions.push([token]);
53
+ }
54
+ else {
55
+ positionTypeData.positions[0].push(token);
56
+ }
57
+ }
58
+ else {
59
+ positionTypeData.positions.push([token]);
60
+ }
61
+ if (token.marketValue) {
62
+ const multiplier = position.positionType === 'borrow' ? -1 : 1;
63
+ positionTypeData.aggregatedMarketValue += token.marketValue;
64
+ protocolData.aggregatedMarketValue += token.marketValue * multiplier;
65
+ chainData.aggregatedMarketValue += token.marketValue * multiplier;
66
+ }
67
+ }
68
+ }
69
+ return groupedPositions;
70
+ }
71
+ exports.groupPositions = groupPositions;
72
+ /**
73
+ *
74
+ * @param tokenBalance - The token balance that is going to be processed
75
+ * @returns The processed token balance
76
+ */
77
+ function processToken(tokenBalance) {
78
+ if (!tokenBalance.tokens) {
79
+ return {
80
+ ...tokenBalance,
81
+ marketValue: tokenBalance.price
82
+ ? tokenBalance.balance * tokenBalance.price
83
+ : undefined,
84
+ };
85
+ }
86
+ const processedTokens = tokenBalance.tokens.map((t) => {
87
+ const { tokens, ...tokenWithoutUnderlyings } = processToken(t);
88
+ return tokenWithoutUnderlyings;
89
+ });
90
+ const marketValue = processedTokens.reduce((acc, t) => acc === undefined || t.marketValue === undefined
91
+ ? undefined
92
+ : acc + t.marketValue, 0);
93
+ return {
94
+ ...tokenBalance,
95
+ marketValue,
96
+ tokens: processedTokens,
97
+ };
98
+ }
99
+ //# sourceMappingURL=group-positions.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"group-positions.cjs","sourceRoot":"","sources":["../../src/DeFiPositionsController/group-positions.ts"],"names":[],"mappings":";;;AAAA,iEAAmD;AAEnD,mCAA+C;AAmC/C;;;;GAIG;AACH,SAAgB,cAAc,CAAC,qBAA6C;IAG1E,MAAM,gBAAg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G,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';\nimport { upperFirst, camelCase } from 'lodash';\n\nimport type {\n DefiPositionResponse,\n PositionType,\n ProtocolToken,\n Underlying,\n Balance,\n} from './fetch-positions';\n\nexport type GroupedPositions = {\n aggregatedMarketValue: number;\n protocols: {\n [protocolId: string]: {\n protocolDetails: { name: string; iconUrl: string };\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 groupPositions(defiPositionsResponse: DefiPositionResponse[]): {\n [key: Hex]: GroupedPositions;\n} {\n const groupedPositions: { [key: Hex]: GroupedPositions } = {};\n\n for (const position of defiPositionsResponse) {\n if (!position.success) {\n continue;\n }\n\n const { chainId, protocolId, iconUrl, positionType } = position;\n\n const chain = toHex(chainId);\n\n if (!groupedPositions[chain]) {\n groupedPositions[chain] = {\n aggregatedMarketValue: 0,\n protocols: {},\n };\n }\n\n const chainData = groupedPositions[chain];\n\n if (!chainData.protocols[protocolId]) {\n chainData.protocols[protocolId] = {\n protocolDetails: {\n // TODO: Prepare better source for protocol name\n name: upperFirst(camelCase(protocolId)),\n // TODO: Picking icon url from the first product position might not be consistent\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 groupedPositions;\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"]}