@defisaver/automation-sdk 3.0.7 → 3.0.8-dev

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 (143) hide show
  1. package/.babelrc +3 -3
  2. package/.editorconfig +9 -9
  3. package/.env.dev +4 -4
  4. package/.eslintignore +6 -6
  5. package/.eslintrc.js +41 -41
  6. package/.tests.sh +2 -2
  7. package/README.md +41 -41
  8. package/esm/abis/Erc20.json +223 -223
  9. package/esm/abis/SubStorage.json +17 -17
  10. package/esm/abis/UniMulticall.json +14 -14
  11. package/esm/abis/index.d.ts +9 -9
  12. package/esm/abis/index.js +30 -30
  13. package/esm/abis/legacy_AaveV2Subscriptions.json +8 -8
  14. package/esm/abis/legacy_AuthCheck.json +8 -8
  15. package/esm/abis/legacy_CompoundV2Subscriptions.json +9 -9
  16. package/esm/abis/legacy_MakerSubscriptions.json +9 -9
  17. package/esm/automation/private/Automation.d.ts +12 -12
  18. package/esm/automation/private/Automation.js +42 -42
  19. package/esm/automation/private/LegacyAutomation.d.ts +25 -25
  20. package/esm/automation/private/LegacyAutomation.js +118 -118
  21. package/esm/automation/private/LegacyProtocol.d.ts +22 -22
  22. package/esm/automation/private/LegacyProtocol.js +41 -41
  23. package/esm/automation/private/LegacyProtocol.test.d.ts +1 -1
  24. package/esm/automation/private/LegacyProtocol.test.js +25 -25
  25. package/esm/automation/private/Protocol.d.ts +22 -22
  26. package/esm/automation/private/Protocol.js +41 -41
  27. package/esm/automation/private/Protocol.test.d.ts +1 -1
  28. package/esm/automation/private/Protocol.test.js +25 -25
  29. package/esm/automation/private/StrategiesAutomation.d.ts +33 -33
  30. package/esm/automation/private/StrategiesAutomation.js +181 -181
  31. package/esm/automation/private/StrategiesAutomation.test.d.ts +1 -1
  32. package/esm/automation/private/StrategiesAutomation.test.js +671 -671
  33. package/esm/automation/public/ArbitrumStrategies.d.ts +5 -5
  34. package/esm/automation/public/ArbitrumStrategies.js +13 -13
  35. package/esm/automation/public/EthereumStrategies.d.ts +5 -5
  36. package/esm/automation/public/EthereumStrategies.js +13 -13
  37. package/esm/automation/public/OptimismStrategies.d.ts +5 -5
  38. package/esm/automation/public/OptimismStrategies.js +13 -13
  39. package/esm/automation/public/legacy/LegacyAaveAutomation.d.ts +6 -6
  40. package/esm/automation/public/legacy/LegacyAaveAutomation.js +20 -20
  41. package/esm/automation/public/legacy/LegacyCompoundAutomation.d.ts +6 -6
  42. package/esm/automation/public/legacy/LegacyCompoundAutomation.js +20 -20
  43. package/esm/automation/public/legacy/LegacyMakerAutomation.d.ts +6 -6
  44. package/esm/automation/public/legacy/LegacyMakerAutomation.js +20 -20
  45. package/esm/configuration.d.ts +1 -1
  46. package/esm/configuration.js +12 -12
  47. package/esm/constants/index.d.ts +24 -45
  48. package/esm/constants/index.js +426 -395
  49. package/esm/index.d.ts +22 -22
  50. package/esm/index.js +63 -63
  51. package/esm/services/contractService.d.ts +12 -12
  52. package/esm/services/contractService.js +54 -54
  53. package/esm/services/ethereumService.d.ts +7 -7
  54. package/esm/services/ethereumService.js +49 -49
  55. package/esm/services/ethereumService.test.d.ts +1 -1
  56. package/esm/services/ethereumService.test.js +242 -242
  57. package/esm/services/strategiesService.d.ts +2 -2
  58. package/esm/services/strategiesService.js +700 -700
  59. package/esm/services/strategiesService.test.d.ts +1 -1
  60. package/esm/services/strategiesService.test.js +110 -110
  61. package/esm/services/strategySubService.d.ts +100 -87
  62. package/esm/services/strategySubService.js +264 -257
  63. package/esm/services/strategySubService.test.d.ts +1 -1
  64. package/esm/services/strategySubService.test.js +934 -894
  65. package/esm/services/subDataService.d.ts +192 -181
  66. package/esm/services/subDataService.js +475 -443
  67. package/esm/services/subDataService.test.d.ts +1 -1
  68. package/esm/services/subDataService.test.js +1282 -1225
  69. package/esm/services/triggerService.d.ts +191 -191
  70. package/esm/services/triggerService.js +367 -367
  71. package/esm/services/triggerService.test.d.ts +1 -1
  72. package/esm/services/triggerService.test.js +926 -926
  73. package/esm/services/utils.d.ts +25 -25
  74. package/esm/services/utils.js +131 -131
  75. package/esm/services/utils.test.d.ts +1 -1
  76. package/esm/services/utils.test.js +376 -376
  77. package/esm/types/contracts/generated/Erc20.d.ts +53 -53
  78. package/esm/types/contracts/generated/Erc20.js +5 -5
  79. package/esm/types/contracts/generated/Legacy_AaveV2Subscriptions.d.ts +129 -129
  80. package/esm/types/contracts/generated/Legacy_AaveV2Subscriptions.js +5 -5
  81. package/esm/types/contracts/generated/Legacy_AuthCheck.d.ts +20 -20
  82. package/esm/types/contracts/generated/Legacy_AuthCheck.js +5 -5
  83. package/esm/types/contracts/generated/Legacy_CompoundV2Subscriptions.d.ts +128 -128
  84. package/esm/types/contracts/generated/Legacy_CompoundV2Subscriptions.js +5 -5
  85. package/esm/types/contracts/generated/Legacy_MakerSubscriptions.d.ts +246 -246
  86. package/esm/types/contracts/generated/Legacy_MakerSubscriptions.js +5 -5
  87. package/esm/types/contracts/generated/SubStorage.d.ts +114 -114
  88. package/esm/types/contracts/generated/SubStorage.js +5 -5
  89. package/esm/types/contracts/generated/UniMulticall.d.ts +55 -55
  90. package/esm/types/contracts/generated/UniMulticall.js +5 -5
  91. package/esm/types/contracts/generated/index.d.ts +7 -7
  92. package/esm/types/contracts/generated/index.js +2 -2
  93. package/esm/types/contracts/generated/types.d.ts +54 -54
  94. package/esm/types/contracts/generated/types.js +2 -2
  95. package/esm/types/enums.d.ts +168 -160
  96. package/esm/types/enums.js +188 -180
  97. package/esm/types/index.d.ts +217 -217
  98. package/esm/types/index.js +2 -2
  99. package/package.json +71 -71
  100. package/scripts/generateContractTypes.js +39 -39
  101. package/src/abis/Erc20.json +222 -222
  102. package/src/abis/SubStorage.json +17 -17
  103. package/src/abis/UniMulticall.json +14 -14
  104. package/src/abis/index.ts +28 -28
  105. package/src/abis/legacy_AaveV2Subscriptions.json +7 -7
  106. package/src/abis/legacy_AuthCheck.json +7 -7
  107. package/src/abis/legacy_CompoundV2Subscriptions.json +8 -8
  108. package/src/abis/legacy_MakerSubscriptions.json +8 -8
  109. package/src/automation/private/Automation.ts +44 -44
  110. package/src/automation/private/LegacyAutomation.ts +135 -135
  111. package/src/automation/private/LegacyProtocol.test.ts +23 -23
  112. package/src/automation/private/LegacyProtocol.ts +51 -51
  113. package/src/automation/private/Protocol.test.ts +23 -23
  114. package/src/automation/private/Protocol.ts +51 -51
  115. package/src/automation/private/StrategiesAutomation.test.ts +663 -663
  116. package/src/automation/private/StrategiesAutomation.ts +242 -242
  117. package/src/automation/public/ArbitrumStrategies.ts +10 -10
  118. package/src/automation/public/EthereumStrategies.ts +10 -10
  119. package/src/automation/public/OptimismStrategies.ts +10 -10
  120. package/src/automation/public/legacy/LegacyAaveAutomation.ts +20 -20
  121. package/src/automation/public/legacy/LegacyCompoundAutomation.ts +20 -20
  122. package/src/automation/public/legacy/LegacyMakerAutomation.ts +20 -20
  123. package/src/configuration.ts +8 -8
  124. package/src/constants/index.ts +441 -409
  125. package/src/index.ts +38 -38
  126. package/src/services/contractService.ts +77 -77
  127. package/src/services/ethereumService.test.ts +257 -257
  128. package/src/services/ethereumService.ts +69 -69
  129. package/src/services/strategiesService.test.ts +105 -105
  130. package/src/services/strategiesService.ts +904 -904
  131. package/src/services/strategySubService.test.ts +1119 -1065
  132. package/src/services/strategySubService.ts +518 -496
  133. package/src/services/subDataService.test.ts +1387 -1326
  134. package/src/services/subDataService.ts +645 -595
  135. package/src/services/triggerService.test.ts +1004 -1004
  136. package/src/services/triggerService.ts +449 -449
  137. package/src/services/utils.test.ts +430 -430
  138. package/src/services/utils.ts +103 -103
  139. package/src/types/enums.ts +182 -174
  140. package/src/types/index.ts +262 -262
  141. package/tsconfig.json +79 -79
  142. package/umd/index.js +108 -16
  143. package/webpack.umd.js +52 -52
@@ -1,33 +1,33 @@
1
- import type Web3 from 'web3';
2
- import type { PastEventOptions } from 'web3-eth-contract';
3
- import type { Position, Interfaces, EthereumAddress, SubscriptionOptions, Contract, ParseData, BlockNumber } from '../../types';
4
- import type { StrategyModel, SubStorage } from '../../types/contracts/generated/SubStorage';
5
- import type { ChainId } from '../../types/enums';
6
- import Automation from './Automation';
7
- interface IStrategiesAutomation extends Interfaces.Automation {
8
- chainId: ChainId;
9
- providerFork: Web3;
10
- }
11
- export default class StrategiesAutomation extends Automation {
12
- protected chainId: ChainId;
13
- protected web3: Web3;
14
- protected web3Fork: Web3;
15
- protected subStorageContract: Contract.WithMeta<SubStorage>;
16
- protected subStorageContractFork: Contract.WithMeta<SubStorage> | null;
17
- constructor(args: IStrategiesAutomation);
18
- protected getEventFromSubStorage(event: string, options?: PastEventOptions): Promise<any[]>;
19
- protected getStrategiesSubs(subIds: number[], fromBlock?: BlockNumber): Promise<StrategyModel.StoredSubDataStructOutputStruct[]>;
20
- protected getSubscriptionEventsFromSubStorage(options?: PastEventOptions): Promise<any[]>;
21
- protected getUpdateDataEventsFromSubStorage(options?: PastEventOptions): Promise<any[]>;
22
- protected getParsedSubscriptions(parseData: ParseData): Position.Automated | null;
23
- /**
24
- * @description Removes expired Limit Order subscriptions
25
- */
26
- protected removeExpiredSubscriptions(subscriptions: (Position.Automated | null)[]): (Position.Automated | null)[];
27
- private _mergeCheck;
28
- protected mergeSubs(_subscriptions: (Position.Automated | null)[]): Position.Automated[];
29
- protected _getSubscriptions(addresses?: EthereumAddress[], options?: SubscriptionOptions): Promise<(Position.Automated | null)[]>;
30
- getSubscriptions(options?: SubscriptionOptions): Promise<(Position.Automated | null)[]>;
31
- getSubscriptionsFor(addresses: EthereumAddress[], options?: SubscriptionOptions): Promise<(Position.Automated | null)[]>;
32
- }
33
- export {};
1
+ import type Web3 from 'web3';
2
+ import type { PastEventOptions } from 'web3-eth-contract';
3
+ import type { Position, Interfaces, EthereumAddress, SubscriptionOptions, Contract, ParseData, BlockNumber } from '../../types';
4
+ import type { StrategyModel, SubStorage } from '../../types/contracts/generated/SubStorage';
5
+ import type { ChainId } from '../../types/enums';
6
+ import Automation from './Automation';
7
+ interface IStrategiesAutomation extends Interfaces.Automation {
8
+ chainId: ChainId;
9
+ providerFork: Web3;
10
+ }
11
+ export default class StrategiesAutomation extends Automation {
12
+ protected chainId: ChainId;
13
+ protected web3: Web3;
14
+ protected web3Fork: Web3;
15
+ protected subStorageContract: Contract.WithMeta<SubStorage>;
16
+ protected subStorageContractFork: Contract.WithMeta<SubStorage> | null;
17
+ constructor(args: IStrategiesAutomation);
18
+ protected getEventFromSubStorage(event: string, options?: PastEventOptions): Promise<any[]>;
19
+ protected getStrategiesSubs(subIds: number[], fromBlock?: BlockNumber): Promise<StrategyModel.StoredSubDataStructOutputStruct[]>;
20
+ protected getSubscriptionEventsFromSubStorage(options?: PastEventOptions): Promise<any[]>;
21
+ protected getUpdateDataEventsFromSubStorage(options?: PastEventOptions): Promise<any[]>;
22
+ protected getParsedSubscriptions(parseData: ParseData): Position.Automated | null;
23
+ /**
24
+ * @description Removes expired Limit Order subscriptions
25
+ */
26
+ protected removeExpiredSubscriptions(subscriptions: (Position.Automated | null)[]): (Position.Automated | null)[];
27
+ private _mergeCheck;
28
+ protected mergeSubs(_subscriptions: (Position.Automated | null)[]): Position.Automated[];
29
+ protected _getSubscriptions(addresses?: EthereumAddress[], options?: SubscriptionOptions): Promise<(Position.Automated | null)[]>;
30
+ getSubscriptions(options?: SubscriptionOptions): Promise<(Position.Automated | null)[]>;
31
+ getSubscriptionsFor(addresses: EthereumAddress[], options?: SubscriptionOptions): Promise<(Position.Automated | null)[]>;
32
+ }
33
+ export {};
@@ -1,181 +1,181 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- const decimal_js_1 = __importDefault(require("decimal.js"));
16
- const enums_1 = require("../../types/enums");
17
- const utils_1 = require("../../services/utils");
18
- const contractService_1 = require("../../services/contractService");
19
- const ethereumService_1 = require("../../services/ethereumService");
20
- const strategiesService_1 = require("../../services/strategiesService");
21
- const Automation_1 = __importDefault(require("./Automation"));
22
- class StrategiesAutomation extends Automation_1.default {
23
- constructor(args) {
24
- super();
25
- this.web3 = args.provider;
26
- this.web3Fork = args.providerFork;
27
- this.chainId = args.chainId;
28
- this.subStorageContract = (0, contractService_1.makeSubStorageContract)(this.web3, this.chainId);
29
- this.subStorageContractFork = this.web3Fork ? (0, contractService_1.makeSubStorageContract)(this.web3Fork, this.chainId) : null;
30
- this.assert();
31
- }
32
- getEventFromSubStorage(event, options) {
33
- var _a;
34
- return __awaiter(this, void 0, void 0, function* () {
35
- // only used for backfilling, so in case options?.fromBlock in undefined
36
- // (just like we omit fromBlock when we call from app when not on fork), we still want to fetch events
37
- if (new decimal_js_1.default(this.subStorageContract.createdBlock.toString()).gt(((_a = options === null || options === void 0 ? void 0 : options.fromBlock) === null || _a === void 0 ? void 0 : _a.toString()) || this.subStorageContract.createdBlock.toString())) {
38
- return [];
39
- }
40
- return (0, ethereumService_1.getEventsFromContract)(this.subStorageContract, this.subStorageContractFork, event, options);
41
- });
42
- }
43
- getStrategiesSubs(subIds, fromBlock = 'latest') {
44
- return __awaiter(this, void 0, void 0, function* () {
45
- let options;
46
- let web3;
47
- if (this.web3Fork && this.subStorageContractFork) {
48
- options = {
49
- target: this.subStorageContractFork.address,
50
- abiItem: (0, contractService_1.getAbiItem)(this.subStorageContractFork.abi, 'strategiesSubs'),
51
- };
52
- web3 = this.web3Fork;
53
- }
54
- else {
55
- options = {
56
- target: this.subStorageContract.address,
57
- abiItem: (0, contractService_1.getAbiItem)(this.subStorageContract.abi, 'strategiesSubs'),
58
- };
59
- web3 = this.web3;
60
- }
61
- const multicallCalls = subIds.map((subId) => (Object.assign(Object.assign({}, options), { params: [subId] })));
62
- return (0, ethereumService_1.multicall)(web3, this.chainId, multicallCalls, fromBlock);
63
- });
64
- }
65
- getSubscriptionEventsFromSubStorage(options) {
66
- return __awaiter(this, void 0, void 0, function* () {
67
- return this.getEventFromSubStorage('Subscribe', options);
68
- });
69
- }
70
- getUpdateDataEventsFromSubStorage(options) {
71
- return __awaiter(this, void 0, void 0, function* () {
72
- const events = yield this.getEventFromSubStorage('UpdateData', options);
73
- /** @dev - Some RPCs sort events differently */
74
- return events.sort((a, b) => a.blockNumber - b.blockNumber);
75
- });
76
- }
77
- getParsedSubscriptions(parseData) {
78
- return (0, strategiesService_1.parseStrategiesAutomatedPosition)(parseData);
79
- }
80
- /**
81
- * @description Removes expired Limit Order subscriptions
82
- */
83
- removeExpiredSubscriptions(subscriptions) {
84
- return subscriptions.filter((subscription) => {
85
- if (!subscription) {
86
- return true;
87
- }
88
- const { protocol, strategy, strategyData } = subscription;
89
- if (protocol.id === enums_1.ProtocolIdentifiers.StrategiesAutomation.Exchange
90
- && strategy.strategyId === enums_1.Strategies.Identifiers.LimitOrder) {
91
- return new decimal_js_1.default(strategyData.decoded.triggerData.goodUntil).gt(new decimal_js_1.default(Date.now()).div(1000));
92
- }
93
- return true;
94
- });
95
- }
96
- _mergeCheck(s, current) {
97
- return s.owner === current.owner
98
- && s.strategy.strategyId === current.strategy.strategyId
99
- && s.protocol.id === current.protocol.id
100
- && s.specific.mergeId === current.specific.mergeWithId
101
- && (s.protocol.id !== enums_1.ProtocolIdentifiers.StrategiesAutomation.MakerDAO // reflexer needs to get added if we have it
102
- || s.strategyData.decoded.subData.vaultId === current.strategyData.decoded.triggerData.vaultId)
103
- && (s.protocol.id !== enums_1.ProtocolIdentifiers.StrategiesAutomation.CrvUSD // merge only crvUSD leverage management for the same market
104
- || s.strategyData.decoded.subData.controller.toLowerCase() === current.strategyData.decoded.triggerData.controller.toLowerCase())
105
- && (s.protocol.id !== enums_1.ProtocolIdentifiers.StrategiesAutomation.MorphoBlue // merge morpho blue with the same marketId
106
- || s.strategyData.decoded.triggerData.marketId.toLowerCase() === current.strategyData.decoded.triggerData.marketId.toLowerCase());
107
- }
108
- mergeSubs(_subscriptions) {
109
- const mergeBase = _subscriptions.filter(s => (0, utils_1.isDefined)(s) && (0, utils_1.isDefined)(s.specific.mergeWithId));
110
- const mergeExtension = _subscriptions.filter(s => (0, utils_1.isDefined)(s) && (0, utils_1.isDefined)(s.specific.mergeId));
111
- let subscriptions = _subscriptions.filter(s => (0, utils_1.isDefined)(s) && (0, utils_1.isUndefined)(s.specific.mergeWithId) && (0, utils_1.isUndefined)(s.specific.mergeId)).map((s) => (Object.assign(Object.assign({}, s), { subIds: [s.subId] })));
112
- mergeBase.forEach((current) => {
113
- const mergePairIndexSubEnabled = mergeExtension.findIndex(s => this._mergeCheck(s, current) && s.isEnabled === current.isEnabled);
114
- const mergePairIndexSubDisabled = mergeExtension.findIndex(s => this._mergeCheck(s, current));
115
- const mergePairIndex = mergePairIndexSubEnabled !== -1 ? mergePairIndexSubEnabled : mergePairIndexSubDisabled;
116
- if (mergePairIndex !== -1) {
117
- const mergePair = mergeExtension[mergePairIndex];
118
- mergeExtension.splice(mergePairIndex, 1);
119
- subscriptions.push(Object.assign(Object.assign(Object.assign({}, mergePair), current), {
120
- // @ts-ignore
121
- blockNumber: decimal_js_1.default.max(mergePair.blockNumber, current.blockNumber).toNumber(), subIds: [current.subId, mergePair.subId], isEnabled: mergePair.isEnabled || current.isEnabled, specific: Object.assign(Object.assign({}, mergePair.specific), current.specific) }));
122
- }
123
- else {
124
- subscriptions.push(Object.assign(Object.assign({}, current), { subIds: [current.subId] }));
125
- }
126
- });
127
- if (mergeExtension.length > 0) {
128
- console.error('Not all merge-able extensions were used', mergeExtension);
129
- subscriptions = [...subscriptions, ...mergeExtension.map((s) => (Object.assign(Object.assign({}, s), { subIds: [s.subId] })))];
130
- }
131
- return subscriptions;
132
- }
133
- _getSubscriptions(addresses, options) {
134
- return __awaiter(this, void 0, void 0, function* () {
135
- const _options = Object.assign(Object.assign({}, (0, utils_1.addToObjectIf)((0, utils_1.isDefined)(options), options)), (0, utils_1.addToObjectIf)((0, utils_1.isDefined)(addresses), { filter: { proxy: addresses } }));
136
- let subscriptionEvents = (yield this.getSubscriptionEventsFromSubStorage(_options)); // TODO PlaceholderType
137
- let subscriptions = [];
138
- if (subscriptionEvents) {
139
- let strategiesSubs = yield this.getStrategiesSubs(subscriptionEvents.map((e) => e.returnValues.subId), _options.toBlock);
140
- if (_options.enabledOnly) {
141
- const filteredSubscriptionEvents = [];
142
- strategiesSubs = strategiesSubs.filter((sub, index) => {
143
- if (sub === null || sub === void 0 ? void 0 : sub.isEnabled)
144
- filteredSubscriptionEvents.push(subscriptionEvents[index]);
145
- return sub === null || sub === void 0 ? void 0 : sub.isEnabled;
146
- });
147
- subscriptionEvents = filteredSubscriptionEvents;
148
- }
149
- subscriptions = yield Promise.all(strategiesSubs.map((sub, index) => __awaiter(this, void 0, void 0, function* () {
150
- var _a;
151
- let latestUpdate = subscriptionEvents[index].returnValues;
152
- if (latestUpdate.subHash !== (sub === null || sub === void 0 ? void 0 : sub.strategySubHash)) {
153
- const updates = yield this.getUpdateDataEventsFromSubStorage(Object.assign(Object.assign({}, (0, utils_1.addToObjectIf)(!!_options, _options)), { filter: { subId: latestUpdate.subId } }));
154
- latestUpdate = Object.assign(Object.assign({}, latestUpdate), (_a = updates === null || updates === void 0 ? void 0 : updates[updates.length - 1]) === null || _a === void 0 ? void 0 : _a.returnValues);
155
- }
156
- return this.getParsedSubscriptions({
157
- chainId: this.chainId,
158
- blockNumber: subscriptionEvents[index].blockNumber,
159
- subscriptionEventData: latestUpdate,
160
- strategiesSubsData: sub,
161
- });
162
- })));
163
- if (options === null || options === void 0 ? void 0 : options.mergeSubs) {
164
- subscriptions = this.mergeSubs(subscriptions);
165
- }
166
- }
167
- return _options.unexpiredOnly ? this.removeExpiredSubscriptions(subscriptions) : subscriptions;
168
- });
169
- }
170
- getSubscriptions(options) {
171
- return __awaiter(this, void 0, void 0, function* () {
172
- return this._getSubscriptions(undefined, options);
173
- });
174
- }
175
- getSubscriptionsFor(addresses, options) {
176
- return __awaiter(this, void 0, void 0, function* () {
177
- return this._getSubscriptions(addresses, options);
178
- });
179
- }
180
- }
181
- exports.default = StrategiesAutomation;
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ const decimal_js_1 = __importDefault(require("decimal.js"));
16
+ const enums_1 = require("../../types/enums");
17
+ const utils_1 = require("../../services/utils");
18
+ const contractService_1 = require("../../services/contractService");
19
+ const ethereumService_1 = require("../../services/ethereumService");
20
+ const strategiesService_1 = require("../../services/strategiesService");
21
+ const Automation_1 = __importDefault(require("./Automation"));
22
+ class StrategiesAutomation extends Automation_1.default {
23
+ constructor(args) {
24
+ super();
25
+ this.web3 = args.provider;
26
+ this.web3Fork = args.providerFork;
27
+ this.chainId = args.chainId;
28
+ this.subStorageContract = (0, contractService_1.makeSubStorageContract)(this.web3, this.chainId);
29
+ this.subStorageContractFork = this.web3Fork ? (0, contractService_1.makeSubStorageContract)(this.web3Fork, this.chainId) : null;
30
+ this.assert();
31
+ }
32
+ getEventFromSubStorage(event, options) {
33
+ var _a;
34
+ return __awaiter(this, void 0, void 0, function* () {
35
+ // only used for backfilling, so in case options?.fromBlock in undefined
36
+ // (just like we omit fromBlock when we call from app when not on fork), we still want to fetch events
37
+ if (new decimal_js_1.default(this.subStorageContract.createdBlock.toString()).gt(((_a = options === null || options === void 0 ? void 0 : options.fromBlock) === null || _a === void 0 ? void 0 : _a.toString()) || this.subStorageContract.createdBlock.toString())) {
38
+ return [];
39
+ }
40
+ return (0, ethereumService_1.getEventsFromContract)(this.subStorageContract, this.subStorageContractFork, event, options);
41
+ });
42
+ }
43
+ getStrategiesSubs(subIds, fromBlock = 'latest') {
44
+ return __awaiter(this, void 0, void 0, function* () {
45
+ let options;
46
+ let web3;
47
+ if (this.web3Fork && this.subStorageContractFork) {
48
+ options = {
49
+ target: this.subStorageContractFork.address,
50
+ abiItem: (0, contractService_1.getAbiItem)(this.subStorageContractFork.abi, 'strategiesSubs'),
51
+ };
52
+ web3 = this.web3Fork;
53
+ }
54
+ else {
55
+ options = {
56
+ target: this.subStorageContract.address,
57
+ abiItem: (0, contractService_1.getAbiItem)(this.subStorageContract.abi, 'strategiesSubs'),
58
+ };
59
+ web3 = this.web3;
60
+ }
61
+ const multicallCalls = subIds.map((subId) => (Object.assign(Object.assign({}, options), { params: [subId] })));
62
+ return (0, ethereumService_1.multicall)(web3, this.chainId, multicallCalls, fromBlock);
63
+ });
64
+ }
65
+ getSubscriptionEventsFromSubStorage(options) {
66
+ return __awaiter(this, void 0, void 0, function* () {
67
+ return this.getEventFromSubStorage('Subscribe', options);
68
+ });
69
+ }
70
+ getUpdateDataEventsFromSubStorage(options) {
71
+ return __awaiter(this, void 0, void 0, function* () {
72
+ const events = yield this.getEventFromSubStorage('UpdateData', options);
73
+ /** @dev - Some RPCs sort events differently */
74
+ return events.sort((a, b) => a.blockNumber - b.blockNumber);
75
+ });
76
+ }
77
+ getParsedSubscriptions(parseData) {
78
+ return (0, strategiesService_1.parseStrategiesAutomatedPosition)(parseData);
79
+ }
80
+ /**
81
+ * @description Removes expired Limit Order subscriptions
82
+ */
83
+ removeExpiredSubscriptions(subscriptions) {
84
+ return subscriptions.filter((subscription) => {
85
+ if (!subscription) {
86
+ return true;
87
+ }
88
+ const { protocol, strategy, strategyData } = subscription;
89
+ if (protocol.id === enums_1.ProtocolIdentifiers.StrategiesAutomation.Exchange
90
+ && strategy.strategyId === enums_1.Strategies.Identifiers.LimitOrder) {
91
+ return new decimal_js_1.default(strategyData.decoded.triggerData.goodUntil).gt(new decimal_js_1.default(Date.now()).div(1000));
92
+ }
93
+ return true;
94
+ });
95
+ }
96
+ _mergeCheck(s, current) {
97
+ return s.owner === current.owner
98
+ && s.strategy.strategyId === current.strategy.strategyId
99
+ && s.protocol.id === current.protocol.id
100
+ && s.specific.mergeId === current.specific.mergeWithId
101
+ && (s.protocol.id !== enums_1.ProtocolIdentifiers.StrategiesAutomation.MakerDAO // reflexer needs to get added if we have it
102
+ || s.strategyData.decoded.subData.vaultId === current.strategyData.decoded.triggerData.vaultId)
103
+ && (s.protocol.id !== enums_1.ProtocolIdentifiers.StrategiesAutomation.CrvUSD // merge only crvUSD leverage management for the same market
104
+ || s.strategyData.decoded.subData.controller.toLowerCase() === current.strategyData.decoded.triggerData.controller.toLowerCase())
105
+ && (s.protocol.id !== enums_1.ProtocolIdentifiers.StrategiesAutomation.MorphoBlue // merge morpho blue with the same marketId
106
+ || s.strategyData.decoded.triggerData.marketId.toLowerCase() === current.strategyData.decoded.triggerData.marketId.toLowerCase());
107
+ }
108
+ mergeSubs(_subscriptions) {
109
+ const mergeBase = _subscriptions.filter(s => (0, utils_1.isDefined)(s) && (0, utils_1.isDefined)(s.specific.mergeWithId));
110
+ const mergeExtension = _subscriptions.filter(s => (0, utils_1.isDefined)(s) && (0, utils_1.isDefined)(s.specific.mergeId));
111
+ let subscriptions = _subscriptions.filter(s => (0, utils_1.isDefined)(s) && (0, utils_1.isUndefined)(s.specific.mergeWithId) && (0, utils_1.isUndefined)(s.specific.mergeId)).map((s) => (Object.assign(Object.assign({}, s), { subIds: [s.subId] })));
112
+ mergeBase.forEach((current) => {
113
+ const mergePairIndexSubEnabled = mergeExtension.findIndex(s => this._mergeCheck(s, current) && s.isEnabled === current.isEnabled);
114
+ const mergePairIndexSubDisabled = mergeExtension.findIndex(s => this._mergeCheck(s, current));
115
+ const mergePairIndex = mergePairIndexSubEnabled !== -1 ? mergePairIndexSubEnabled : mergePairIndexSubDisabled;
116
+ if (mergePairIndex !== -1) {
117
+ const mergePair = mergeExtension[mergePairIndex];
118
+ mergeExtension.splice(mergePairIndex, 1);
119
+ subscriptions.push(Object.assign(Object.assign(Object.assign({}, mergePair), current), {
120
+ // @ts-ignore
121
+ blockNumber: decimal_js_1.default.max(mergePair.blockNumber, current.blockNumber).toNumber(), subIds: [current.subId, mergePair.subId], isEnabled: mergePair.isEnabled || current.isEnabled, specific: Object.assign(Object.assign({}, mergePair.specific), current.specific) }));
122
+ }
123
+ else {
124
+ subscriptions.push(Object.assign(Object.assign({}, current), { subIds: [current.subId] }));
125
+ }
126
+ });
127
+ if (mergeExtension.length > 0) {
128
+ console.error('Not all merge-able extensions were used', mergeExtension);
129
+ subscriptions = [...subscriptions, ...mergeExtension.map((s) => (Object.assign(Object.assign({}, s), { subIds: [s.subId] })))];
130
+ }
131
+ return subscriptions;
132
+ }
133
+ _getSubscriptions(addresses, options) {
134
+ return __awaiter(this, void 0, void 0, function* () {
135
+ const _options = Object.assign(Object.assign({}, (0, utils_1.addToObjectIf)((0, utils_1.isDefined)(options), options)), (0, utils_1.addToObjectIf)((0, utils_1.isDefined)(addresses), { filter: { proxy: addresses } }));
136
+ let subscriptionEvents = (yield this.getSubscriptionEventsFromSubStorage(_options)); // TODO PlaceholderType
137
+ let subscriptions = [];
138
+ if (subscriptionEvents) {
139
+ let strategiesSubs = yield this.getStrategiesSubs(subscriptionEvents.map((e) => e.returnValues.subId), _options.toBlock);
140
+ if (_options.enabledOnly) {
141
+ const filteredSubscriptionEvents = [];
142
+ strategiesSubs = strategiesSubs.filter((sub, index) => {
143
+ if (sub === null || sub === void 0 ? void 0 : sub.isEnabled)
144
+ filteredSubscriptionEvents.push(subscriptionEvents[index]);
145
+ return sub === null || sub === void 0 ? void 0 : sub.isEnabled;
146
+ });
147
+ subscriptionEvents = filteredSubscriptionEvents;
148
+ }
149
+ subscriptions = yield Promise.all(strategiesSubs.map((sub, index) => __awaiter(this, void 0, void 0, function* () {
150
+ var _a;
151
+ let latestUpdate = subscriptionEvents[index].returnValues;
152
+ if (latestUpdate.subHash !== (sub === null || sub === void 0 ? void 0 : sub.strategySubHash)) {
153
+ const updates = yield this.getUpdateDataEventsFromSubStorage(Object.assign(Object.assign({}, (0, utils_1.addToObjectIf)(!!_options, _options)), { filter: { subId: latestUpdate.subId } }));
154
+ latestUpdate = Object.assign(Object.assign({}, latestUpdate), (_a = updates === null || updates === void 0 ? void 0 : updates[updates.length - 1]) === null || _a === void 0 ? void 0 : _a.returnValues);
155
+ }
156
+ return this.getParsedSubscriptions({
157
+ chainId: this.chainId,
158
+ blockNumber: subscriptionEvents[index].blockNumber,
159
+ subscriptionEventData: latestUpdate,
160
+ strategiesSubsData: sub,
161
+ });
162
+ })));
163
+ if (options === null || options === void 0 ? void 0 : options.mergeSubs) {
164
+ subscriptions = this.mergeSubs(subscriptions);
165
+ }
166
+ }
167
+ return _options.unexpiredOnly ? this.removeExpiredSubscriptions(subscriptions) : subscriptions;
168
+ });
169
+ }
170
+ getSubscriptions(options) {
171
+ return __awaiter(this, void 0, void 0, function* () {
172
+ return this._getSubscriptions(undefined, options);
173
+ });
174
+ }
175
+ getSubscriptionsFor(addresses, options) {
176
+ return __awaiter(this, void 0, void 0, function* () {
177
+ return this._getSubscriptions(addresses, options);
178
+ });
179
+ }
180
+ }
181
+ exports.default = StrategiesAutomation;
@@ -1 +1 @@
1
- import '../../configuration';
1
+ import '../../configuration';