@defisaver/automation-sdk 3.1.10 → 3.2.1

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/.env ADDED
@@ -0,0 +1,5 @@
1
+ # Used for testing
2
+ RPC_1=https://eth-mainnet.g.alchemy.com/v2/4jm0fn2xvapd3pDpmdE8Y1rZKEp71U0Y
3
+ RPC_10=https://opt-mainnet.g.alchemy.com/v2/4jm0fn2xvapd3pDpmdE8Y1rZKEp71U0Y
4
+ RPC_42161=https://arb-mainnet.g.alchemy.com/v2/4jm0fn2xvapd3pDpmdE8Y1rZKEp71U0Y
5
+ RPC_8453=https://base-mainnet.g.alchemy.com/v2/4jm0fn2xvapd3pDpmdE8Y1rZKEp71U0Y
@@ -1,7 +1,7 @@
1
1
  import type Web3 from 'web3';
2
2
  import type { PastEventOptions } from 'web3-eth-contract';
3
3
  import type { Position, Interfaces, EthereumAddress, SubscriptionOptions, Contract, ParseData, BlockNumber } from '../../types';
4
- import type { StrategyModel, SubStorage } from '../../types/contracts/generated/SubStorage';
4
+ import type { StrategyModel, Subscribe, SubStorage, UpdateData } from '../../types/contracts/generated/SubStorage';
5
5
  import type { ChainId } from '../../types/enums';
6
6
  import Automation from './Automation';
7
7
  interface IStrategiesAutomation extends Interfaces.Automation {
@@ -17,8 +17,8 @@ export default class StrategiesAutomation extends Automation {
17
17
  constructor(args: IStrategiesAutomation);
18
18
  protected getEventFromSubStorage(event: string, options?: PastEventOptions): Promise<any[]>;
19
19
  protected getStrategiesSubs(subIds: number[], fromBlock?: BlockNumber): Promise<StrategyModel.StoredSubDataStructOutputStruct[]>;
20
- protected getSubscriptionEventsFromSubStorage(options?: PastEventOptions): Promise<any[]>;
21
- protected getUpdateDataEventsFromSubStorage(options?: PastEventOptions): Promise<any[]>;
20
+ protected getSubscriptionEventsFromSubStorage(options?: PastEventOptions): Promise<Subscribe[]>;
21
+ protected getUpdateDataEventsFromSubStorage(options?: PastEventOptions): Promise<UpdateData[]>;
22
22
  protected getParsedSubscriptions(parseData: ParseData): Position.Automated | null;
23
23
  /**
24
24
  * @description Removes expired Limit Order subscriptions
@@ -13,6 +13,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  const decimal_js_1 = __importDefault(require("decimal.js"));
16
+ const es6_promise_pool_1 = __importDefault(require("es6-promise-pool"));
16
17
  const enums_1 = require("../../types/enums");
17
18
  const utils_1 = require("../../services/utils");
18
19
  const contractService_1 = require("../../services/contractService");
@@ -133,10 +134,10 @@ class StrategiesAutomation extends Automation_1.default {
133
134
  _getSubscriptions(addresses, options) {
134
135
  return __awaiter(this, void 0, void 0, function* () {
135
136
  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 subscriptionEvents = yield this.getSubscriptionEventsFromSubStorage(_options);
137
138
  let subscriptions = [];
138
139
  if (subscriptionEvents) {
139
- let strategiesSubs = yield this.getStrategiesSubs(subscriptionEvents.map((e) => e.returnValues.subId), _options.toBlock);
140
+ let strategiesSubs = yield this.getStrategiesSubs(subscriptionEvents.map((e) => +e.returnValues.subId), _options.toBlock);
140
141
  if (_options.enabledOnly) {
141
142
  const filteredSubscriptionEvents = [];
142
143
  strategiesSubs = strategiesSubs.filter((sub, index) => {
@@ -146,20 +147,28 @@ class StrategiesAutomation extends Automation_1.default {
146
147
  });
147
148
  subscriptionEvents = filteredSubscriptionEvents;
148
149
  }
149
- subscriptions = yield Promise.all(strategiesSubs.map((sub, index) => __awaiter(this, void 0, void 0, function* () {
150
+ const replaceSubWithUpdate = (index) => __awaiter(this, void 0, void 0, function* () {
150
151
  var _a;
151
- let latestUpdate = subscriptionEvents[index].returnValues;
152
+ const sub = strategiesSubs[index];
153
+ let latestUpdate = Object.assign({}, subscriptionEvents[index].returnValues);
152
154
  if (latestUpdate.subHash !== (sub === null || sub === void 0 ? void 0 : sub.strategySubHash)) {
153
155
  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);
156
+ latestUpdate = Object.assign(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), { 2: latestUpdate[2] });
155
157
  }
156
- return this.getParsedSubscriptions({
158
+ subscriptions.push(this.getParsedSubscriptions({
157
159
  chainId: this.chainId,
158
160
  blockNumber: subscriptionEvents[index].blockNumber,
159
161
  subscriptionEventData: latestUpdate,
160
162
  strategiesSubsData: sub,
161
- });
162
- })));
163
+ }));
164
+ });
165
+ // eslint-disable-next-line func-names
166
+ const pool = new es6_promise_pool_1.default(function* () {
167
+ for (let index = 0; index < strategiesSubs.length; index++) {
168
+ yield replaceSubWithUpdate(index);
169
+ }
170
+ }, 50);
171
+ yield pool.start();
163
172
  if (options === null || options === void 0 ? void 0 : options.mergeSubs) {
164
173
  subscriptions = this.mergeSubs(subscriptions);
165
174
  }
@@ -1,7 +1,7 @@
1
1
  import type Web3 from 'web3';
2
2
  import type { PastEventOptions } from 'web3-eth-contract';
3
3
  import type { Position, Interfaces, EthereumAddress, SubscriptionOptions, Contract, ParseData, BlockNumber } from '../../types';
4
- import type { StrategyModel, SubStorage } from '../../types/contracts/generated/SubStorage';
4
+ import type { StrategyModel, Subscribe, SubStorage, UpdateData } from '../../types/contracts/generated/SubStorage';
5
5
  import type { ChainId } from '../../types/enums';
6
6
  import Automation from './Automation';
7
7
  interface IStrategiesAutomation extends Interfaces.Automation {
@@ -17,8 +17,8 @@ export default class StrategiesAutomation extends Automation {
17
17
  constructor(args: IStrategiesAutomation);
18
18
  protected getEventFromSubStorage(event: string, options?: PastEventOptions): Promise<any[]>;
19
19
  protected getStrategiesSubs(subIds: number[], fromBlock?: BlockNumber): Promise<StrategyModel.StoredSubDataStructOutputStruct[]>;
20
- protected getSubscriptionEventsFromSubStorage(options?: PastEventOptions): Promise<any[]>;
21
- protected getUpdateDataEventsFromSubStorage(options?: PastEventOptions): Promise<any[]>;
20
+ protected getSubscriptionEventsFromSubStorage(options?: PastEventOptions): Promise<Subscribe[]>;
21
+ protected getUpdateDataEventsFromSubStorage(options?: PastEventOptions): Promise<UpdateData[]>;
22
22
  protected getParsedSubscriptions(parseData: ParseData): Position.Automated | null;
23
23
  /**
24
24
  * @description Removes expired Limit Order subscriptions
@@ -8,6 +8,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  });
9
9
  };
10
10
  import Dec from 'decimal.js';
11
+ import PromisePool from 'es6-promise-pool';
11
12
  import { Strategies, ProtocolIdentifiers } from '../../types/enums';
12
13
  import { addToObjectIf, isDefined, isUndefined } from '../../services/utils';
13
14
  import { getAbiItem, makeSubStorageContract } from '../../services/contractService';
@@ -128,10 +129,10 @@ export default class StrategiesAutomation extends Automation {
128
129
  _getSubscriptions(addresses, options) {
129
130
  return __awaiter(this, void 0, void 0, function* () {
130
131
  const _options = Object.assign(Object.assign({}, addToObjectIf(isDefined(options), options)), addToObjectIf(isDefined(addresses), { filter: { proxy: addresses } }));
131
- let subscriptionEvents = (yield this.getSubscriptionEventsFromSubStorage(_options)); // TODO PlaceholderType
132
+ let subscriptionEvents = yield this.getSubscriptionEventsFromSubStorage(_options);
132
133
  let subscriptions = [];
133
134
  if (subscriptionEvents) {
134
- let strategiesSubs = yield this.getStrategiesSubs(subscriptionEvents.map((e) => e.returnValues.subId), _options.toBlock);
135
+ let strategiesSubs = yield this.getStrategiesSubs(subscriptionEvents.map((e) => +e.returnValues.subId), _options.toBlock);
135
136
  if (_options.enabledOnly) {
136
137
  const filteredSubscriptionEvents = [];
137
138
  strategiesSubs = strategiesSubs.filter((sub, index) => {
@@ -141,20 +142,28 @@ export default class StrategiesAutomation extends Automation {
141
142
  });
142
143
  subscriptionEvents = filteredSubscriptionEvents;
143
144
  }
144
- subscriptions = yield Promise.all(strategiesSubs.map((sub, index) => __awaiter(this, void 0, void 0, function* () {
145
+ const replaceSubWithUpdate = (index) => __awaiter(this, void 0, void 0, function* () {
145
146
  var _a;
146
- let latestUpdate = subscriptionEvents[index].returnValues;
147
+ const sub = strategiesSubs[index];
148
+ let latestUpdate = Object.assign({}, subscriptionEvents[index].returnValues);
147
149
  if (latestUpdate.subHash !== (sub === null || sub === void 0 ? void 0 : sub.strategySubHash)) {
148
150
  const updates = yield this.getUpdateDataEventsFromSubStorage(Object.assign(Object.assign({}, addToObjectIf(!!_options, _options)), { filter: { subId: latestUpdate.subId } }));
149
- 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);
151
+ latestUpdate = Object.assign(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), { 2: latestUpdate[2] });
150
152
  }
151
- return this.getParsedSubscriptions({
153
+ subscriptions.push(this.getParsedSubscriptions({
152
154
  chainId: this.chainId,
153
155
  blockNumber: subscriptionEvents[index].blockNumber,
154
156
  subscriptionEventData: latestUpdate,
155
157
  strategiesSubsData: sub,
156
- });
157
- })));
158
+ }));
159
+ });
160
+ // eslint-disable-next-line func-names
161
+ const pool = new PromisePool(function* () {
162
+ for (let index = 0; index < strategiesSubs.length; index++) {
163
+ yield replaceSubWithUpdate(index);
164
+ }
165
+ }, 50);
166
+ yield pool.start();
158
167
  if (options === null || options === void 0 ? void 0 : options.mergeSubs) {
159
168
  subscriptions = this.mergeSubs(subscriptions);
160
169
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defisaver/automation-sdk",
3
- "version": "3.1.10",
3
+ "version": "3.2.1",
4
4
  "description": "",
5
5
  "main": "./cjs/index.js",
6
6
  "module": "./esm/index.js",
@@ -23,12 +23,12 @@
23
23
  "repository": "https://github.com/defisaver/automation-sdk",
24
24
  "license": "ISC",
25
25
  "dependencies": {
26
- "@defisaver/eslint-config": "^1.0.1",
27
- "@defisaver/sdk": "^1.2.17",
28
- "@defisaver/tokens": "^1.5.54",
26
+ "@defisaver/sdk": "^1.2.20",
27
+ "@defisaver/tokens": "^1.5.56",
29
28
  "@ethersproject/address": "^5.0.10",
30
29
  "@ethersproject/solidity": "^5.0.9",
31
30
  "decimal.js": "^10.4.3",
31
+ "es6-promise-pool": "^2.5.0",
32
32
  "lodash": "^4.17.21",
33
33
  "web3-eth-abi": "^1.10.2",
34
34
  "web3-utils": "^1.10.2"
@@ -46,7 +46,6 @@
46
46
  "husky": "^8.0.0",
47
47
  "jsdoc-to-markdown": "^6.0.1",
48
48
  "mocha": "^10.2.0",
49
- "react": "^18.2.0",
50
49
  "ts-node": "^10.9.1",
51
50
  "typechain": "^8.1.0",
52
51
  "typechain-target-web3-v1-3mihai3": "^6.0.1",
@@ -1,11 +1,12 @@
1
1
  import Dec from 'decimal.js';
2
2
  import type Web3 from 'web3';
3
3
  import type { PastEventOptions } from 'web3-eth-contract';
4
+ import PromisePool from 'es6-promise-pool';
4
5
  import type {
5
6
  Position, Interfaces, EthereumAddress, SubscriptionOptions, Contract, ParseData, PlaceholderType, BlockNumber,
6
7
  } from '../../types';
7
8
  import type {
8
- StrategyModel, SubStorage,
9
+ StrategyModel, Subscribe, SubStorage, UpdateData,
9
10
  } from '../../types/contracts/generated/SubStorage';
10
11
  import type { ChainId } from '../../types/enums';
11
12
  import { Strategies, ProtocolIdentifiers } from '../../types/enums';
@@ -77,13 +78,13 @@ export default class StrategiesAutomation extends Automation {
77
78
  }
78
79
 
79
80
  protected async getSubscriptionEventsFromSubStorage(options?: PastEventOptions) {
80
- return this.getEventFromSubStorage('Subscribe', options);
81
+ return this.getEventFromSubStorage('Subscribe', options) as Promise<Subscribe[]>;
81
82
  }
82
83
 
83
84
  protected async getUpdateDataEventsFromSubStorage(options?: PastEventOptions) {
84
85
  const events = await this.getEventFromSubStorage('UpdateData', options);
85
86
  /** @dev - Some RPCs sort events differently */
86
- return events.sort((a, b) => a.blockNumber - b.blockNumber);
87
+ return events.sort((a, b) => a.blockNumber - b.blockNumber) as UpdateData[];
87
88
  }
88
89
 
89
90
  protected getParsedSubscriptions(parseData: ParseData) {
@@ -183,13 +184,13 @@ export default class StrategiesAutomation extends Automation {
183
184
  ...addToObjectIf(isDefined(addresses), { filter: { proxy: addresses } }),
184
185
  } as SubscriptionOptions & PastEventOptions;
185
186
 
186
- let subscriptionEvents = (await this.getSubscriptionEventsFromSubStorage(_options)) as PlaceholderType; // TODO PlaceholderType
187
+ let subscriptionEvents = await this.getSubscriptionEventsFromSubStorage(_options);
187
188
 
188
189
  let subscriptions: (Position.Automated | null)[] = [];
189
190
 
190
191
  if (subscriptionEvents) {
191
192
  let strategiesSubs = await this.getStrategiesSubs(
192
- subscriptionEvents.map((e: { returnValues: { subId: number } }) => e.returnValues.subId), _options.toBlock,
193
+ subscriptionEvents.map((e) => +e.returnValues.subId), _options.toBlock,
193
194
  );
194
195
 
195
196
  if (_options.enabledOnly) {
@@ -203,8 +204,9 @@ export default class StrategiesAutomation extends Automation {
203
204
  subscriptionEvents = filteredSubscriptionEvents;
204
205
  }
205
206
 
206
- subscriptions = await Promise.all(strategiesSubs.map(async (sub, index: number) => {
207
- let latestUpdate = subscriptionEvents[index].returnValues;
207
+ const replaceSubWithUpdate = async (index: number) => {
208
+ const sub = strategiesSubs[index];
209
+ let latestUpdate = { ...subscriptionEvents[index].returnValues };
208
210
 
209
211
  if (latestUpdate.subHash !== sub?.strategySubHash) {
210
212
  const updates = await this.getUpdateDataEventsFromSubStorage({
@@ -214,15 +216,26 @@ export default class StrategiesAutomation extends Automation {
214
216
  latestUpdate = {
215
217
  ...latestUpdate, // Update is missing proxy, hence this
216
218
  ...updates?.[updates.length - 1]?.returnValues,
219
+ 2: latestUpdate[2], // type issue
217
220
  };
218
221
  }
219
- return this.getParsedSubscriptions({
220
- chainId: this.chainId,
221
- blockNumber: subscriptionEvents[index].blockNumber,
222
- subscriptionEventData: latestUpdate,
223
- strategiesSubsData: sub,
224
- });
225
- }));
222
+ subscriptions.push(
223
+ this.getParsedSubscriptions({
224
+ chainId: this.chainId,
225
+ blockNumber: subscriptionEvents[index].blockNumber,
226
+ subscriptionEventData: latestUpdate,
227
+ strategiesSubsData: sub,
228
+ }),
229
+ );
230
+ };
231
+
232
+ // eslint-disable-next-line func-names
233
+ const pool = new PromisePool(function* () {
234
+ for (let index = 0; index < strategiesSubs.length; index++) {
235
+ yield replaceSubWithUpdate(index);
236
+ }
237
+ } as any, 50);
238
+ await pool.start();
226
239
 
227
240
  if (options?.mergeSubs) {
228
241
  subscriptions = this.mergeSubs(subscriptions);