@defisaver/automation-sdk 1.2.2 → 1.2.4

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.
@@ -2,20 +2,23 @@ import type Web3 from 'web3';
2
2
  import type { PastEventOptions } from 'web3-eth-contract';
3
3
  import type { Position, Interfaces, EthereumAddress, SubscriptionOptions, Contract, ParseData } from '../../types';
4
4
  import type { StrategyModel, SubStorage } from '../../types/contracts/generated/SubStorage';
5
- import Automation from './Automation';
6
5
  import type { ChainId } from '../../types/enums';
6
+ import Automation from './Automation';
7
7
  interface IStrategiesAutomation extends Interfaces.Automation {
8
8
  chainId: ChainId;
9
+ providerFork: Web3;
9
10
  }
10
11
  export default class StrategiesAutomation extends Automation {
11
12
  protected chainId: ChainId;
12
13
  protected web3: Web3;
14
+ protected web3Fork: Web3;
13
15
  protected subStorageContract: Contract.WithMeta<SubStorage>;
16
+ protected subStorageContractFork: Contract.WithMeta<SubStorage> | null;
14
17
  constructor(args: IStrategiesAutomation);
15
- protected getEventFromSubStorage(event: string, options?: PastEventOptions): Promise<import("web3-eth-contract").EventData[]>;
18
+ protected getEventFromSubStorage(event: string, options?: PastEventOptions): Promise<any[]>;
16
19
  protected getStrategiesSubs(subIds: number[]): Promise<StrategyModel.StoredSubDataStructOutputStruct[]>;
17
- protected getSubscriptionEventsFromSubStorage(options?: PastEventOptions): Promise<import("web3-eth-contract").EventData[]>;
18
- protected getUpdateDataEventsFromSubStorage(options?: PastEventOptions): Promise<import("web3-eth-contract").EventData[]>;
20
+ protected getSubscriptionEventsFromSubStorage(options?: PastEventOptions): Promise<any[]>;
21
+ protected getUpdateDataEventsFromSubStorage(options?: PastEventOptions): Promise<any[]>;
19
22
  protected getParsedSubscriptions(parseData: ParseData): Position.Automated | null;
20
23
  protected _getSubscriptions(addresses?: EthereumAddress[], options?: SubscriptionOptions): Promise<(Position.Automated | null)[]>;
21
24
  getSubscriptions(options?: SubscriptionOptions): Promise<(Position.Automated | null)[]>;
@@ -11,30 +11,43 @@ import Dec from 'decimal.js';
11
11
  import { addToObjectIf, isDefined } from '../../services/utils';
12
12
  import { getAbiItem, makeSubStorageContract } from '../../services/contractService';
13
13
  import { getEventsFromContract, multicall } from '../../services/ethereumService';
14
- import Automation from './Automation';
15
14
  import { parseStrategiesAutomatedPosition } from '../../services/strategiesService';
15
+ import Automation from './Automation';
16
16
  export default class StrategiesAutomation extends Automation {
17
17
  constructor(args) {
18
18
  super();
19
19
  this.web3 = args.provider;
20
+ this.web3Fork = args.providerFork;
20
21
  this.chainId = args.chainId;
21
22
  this.subStorageContract = makeSubStorageContract(this.web3, this.chainId);
23
+ this.subStorageContractFork = this.web3Fork ? makeSubStorageContract(this.web3Fork, this.chainId) : null;
22
24
  this.assert();
23
25
  }
24
26
  getEventFromSubStorage(event, options) {
25
27
  return __awaiter(this, void 0, void 0, function* () {
26
- return getEventsFromContract(this.subStorageContract, event, options);
28
+ return getEventsFromContract(this.subStorageContract, this.subStorageContractFork, event, options);
27
29
  });
28
30
  }
29
31
  getStrategiesSubs(subIds) {
30
32
  return __awaiter(this, void 0, void 0, function* () {
31
- const subStorageContract = this.subStorageContract;
32
- const defaultOptions = {
33
- target: subStorageContract.address,
34
- abiItem: getAbiItem(subStorageContract.abi, 'strategiesSubs'),
35
- };
36
- const multicallCalls = subIds.map((subId) => (Object.assign(Object.assign({}, defaultOptions), { params: [subId] })));
37
- return multicall(this.web3, this.chainId, multicallCalls);
33
+ let options;
34
+ let web3;
35
+ if (this.web3Fork && this.subStorageContractFork) {
36
+ options = {
37
+ target: this.subStorageContractFork.address,
38
+ abiItem: getAbiItem(this.subStorageContractFork.abi, 'strategiesSubs'),
39
+ };
40
+ web3 = this.web3Fork;
41
+ }
42
+ else {
43
+ options = {
44
+ target: this.subStorageContract.address,
45
+ abiItem: getAbiItem(this.subStorageContract.abi, 'strategiesSubs'),
46
+ };
47
+ web3 = this.web3;
48
+ }
49
+ const multicallCalls = subIds.map((subId) => (Object.assign(Object.assign({}, options), { params: [subId] })));
50
+ return multicall(web3, this.chainId, multicallCalls);
38
51
  });
39
52
  }
40
53
  getSubscriptionEventsFromSubStorage(options) {
@@ -140,6 +140,16 @@ export const MAINNET_BUNDLES_INFO = {
140
140
  strategyId: Strategies.Identifiers.Boost,
141
141
  protocol: PROTOCOLS.MakerDAO,
142
142
  },
143
+ [Bundles.MainnetIds.AAVE_V3_CLOSE_TO_DEBT]: {
144
+ strategyOrBundleId: Bundles.MainnetIds.AAVE_V3_CLOSE_TO_DEBT,
145
+ strategyId: Strategies.Identifiers.CloseOnPriceToDebt,
146
+ protocol: PROTOCOLS.AaveV3,
147
+ },
148
+ [Bundles.MainnetIds.AAVE_V3_CLOSE_TO_COLLATERAL]: {
149
+ strategyOrBundleId: Bundles.MainnetIds.AAVE_V3_CLOSE_TO_COLLATERAL,
150
+ strategyId: Strategies.Identifiers.CloseOnPriceToColl,
151
+ protocol: PROTOCOLS.AaveV3,
152
+ },
143
153
  };
144
154
  export const OPTIMISM_BUNDLES_INFO = {
145
155
  [Bundles.OptimismIds.AAVE_V3_REPAY]: {
@@ -4,4 +4,4 @@ import type { BlockNumber, Multicall, Contract } from '../types';
4
4
  import type { BaseContract } from '../types/contracts/generated/types';
5
5
  import type { ChainId } from '../types/enums';
6
6
  export declare function multicall(web3: Web3, chainId: ChainId, calls: Multicall.Calls[], block?: BlockNumber): Promise<Multicall.Payload>;
7
- export declare function getEventsFromContract<T extends BaseContract>(contractWithMeta: Contract.WithMeta<T>, event: string, options?: PastEventOptions): Promise<import("web3-eth-contract").EventData[]>;
7
+ export declare function getEventsFromContract<T extends BaseContract>(contractWithMeta: Contract.WithMeta<T>, contractWithMetaFork: Contract.WithMeta<T> | null, event: string, options?: PastEventOptions): Promise<any[]>;
@@ -29,6 +29,13 @@ export function multicall(web3, chainId, calls, block = 'latest') {
29
29
  return formattedResult;
30
30
  });
31
31
  }
32
- export function getEventsFromContract(contractWithMeta, event, options) {
33
- return contractWithMeta.contract.getPastEvents(event, Object.assign({ fromBlock: contractWithMeta.createdBlock }, addToObjectIf(isDefined(options), options)));
32
+ export function getEventsFromContract(contractWithMeta, contractWithMetaFork, event, options) {
33
+ return __awaiter(this, void 0, void 0, function* () {
34
+ const events = yield contractWithMeta.contract.getPastEvents(event, Object.assign({}, addToObjectIf(isDefined(options), Object.assign(Object.assign({}, options), { fromBlock: contractWithMeta.createdBlock }))));
35
+ let eventsFork = [];
36
+ if (contractWithMetaFork) {
37
+ eventsFork = yield contractWithMetaFork.contract.getPastEvents(event, Object.assign({}, addToObjectIf(isDefined(options), Object.assign(Object.assign({}, options), { toBlock: 'latest' }))));
38
+ }
39
+ return [...events, ...eventsFork];
40
+ });
34
41
  }
@@ -84,7 +84,9 @@ export declare namespace Bundles {
84
84
  AAVE_V3_REPAY = 8,
85
85
  AAVE_V3_BOOST = 9,
86
86
  MAKER_REPAY = 10,
87
- MAKER_BOOST = 11
87
+ MAKER_BOOST = 11,
88
+ AAVE_V3_CLOSE_TO_DEBT = 12,
89
+ AAVE_V3_CLOSE_TO_COLLATERAL = 13
88
90
  }
89
91
  enum OptimismIds {
90
92
  AAVE_V3_REPAY = 0,
@@ -99,6 +99,8 @@ export var Bundles;
99
99
  MainnetIds[MainnetIds["AAVE_V3_BOOST"] = 9] = "AAVE_V3_BOOST";
100
100
  MainnetIds[MainnetIds["MAKER_REPAY"] = 10] = "MAKER_REPAY";
101
101
  MainnetIds[MainnetIds["MAKER_BOOST"] = 11] = "MAKER_BOOST";
102
+ MainnetIds[MainnetIds["AAVE_V3_CLOSE_TO_DEBT"] = 12] = "AAVE_V3_CLOSE_TO_DEBT";
103
+ MainnetIds[MainnetIds["AAVE_V3_CLOSE_TO_COLLATERAL"] = 13] = "AAVE_V3_CLOSE_TO_COLLATERAL";
102
104
  })(MainnetIds = Bundles.MainnetIds || (Bundles.MainnetIds = {}));
103
105
  let OptimismIds;
104
106
  (function (OptimismIds) {
@@ -66,6 +66,7 @@ export declare namespace Interfaces {
66
66
  }
67
67
  interface Automation {
68
68
  provider: Web3;
69
+ providerFork: Web3;
69
70
  }
70
71
  interface LegacyAutomation<T extends BaseContract> {
71
72
  provider: Web3;
@@ -121,6 +122,7 @@ export declare namespace Position {
121
122
  closeToAssetAddr: EthereumAddress;
122
123
  }
123
124
  }
125
+ type SpecificAny = Specific.CloseOnPrice | Specific.TrailingStop | Specific.RatioProtection | Specific.CloseOnPriceAave;
124
126
  interface Automated {
125
127
  chainId: ChainId;
126
128
  owner: EthereumAddress;
@@ -141,7 +143,7 @@ export declare namespace Position {
141
143
  subData: PlaceholderType;
142
144
  };
143
145
  };
144
- specific: Specific.CloseOnPrice | Specific.TrailingStop | Specific.RatioProtection | Specific.CloseOnPriceAave;
146
+ specific: SpecificAny;
145
147
  }
146
148
  interface LegacyAutomated {
147
149
  chainId: ChainId;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defisaver/automation-sdk",
3
- "version": "1.2.2",
3
+ "version": "1.2.4",
4
4
  "description": "",
5
5
  "main": "./umd/index.js",
6
6
  "module": "./esm/index.js",
@@ -8,17 +8,18 @@ import type {
8
8
  import type {
9
9
  Subscribe, StrategyModel, SubStorage, UpdateData,
10
10
  } from '../../types/contracts/generated/SubStorage';
11
+ import type { ChainId } from '../../types/enums';
11
12
 
12
13
  import { addToObjectIf, isDefined } from '../../services/utils';
13
14
  import { getAbiItem, makeSubStorageContract } from '../../services/contractService';
14
15
  import { getEventsFromContract, multicall } from '../../services/ethereumService';
16
+ import { parseStrategiesAutomatedPosition } from '../../services/strategiesService';
15
17
 
16
18
  import Automation from './Automation';
17
- import { parseStrategiesAutomatedPosition } from '../../services/strategiesService';
18
- import type { ChainId } from '../../types/enums';
19
19
 
20
20
  interface IStrategiesAutomation extends Interfaces.Automation {
21
21
  chainId: ChainId,
22
+ providerFork: Web3,
22
23
  }
23
24
 
24
25
  export default class StrategiesAutomation extends Automation {
@@ -26,33 +27,48 @@ export default class StrategiesAutomation extends Automation {
26
27
 
27
28
  protected web3: Web3;
28
29
 
30
+ protected web3Fork: Web3;
31
+
29
32
  protected subStorageContract: Contract.WithMeta<SubStorage>;
30
33
 
34
+ protected subStorageContractFork: Contract.WithMeta<SubStorage> | null;
35
+
31
36
  constructor(args: IStrategiesAutomation) {
32
37
  super();
33
38
 
34
39
  this.web3 = args.provider;
40
+ this.web3Fork = args.providerFork;
35
41
  this.chainId = args.chainId;
36
42
  this.subStorageContract = makeSubStorageContract(this.web3, this.chainId);
43
+ this.subStorageContractFork = this.web3Fork ? makeSubStorageContract(this.web3Fork, this.chainId) : null;
37
44
 
38
45
  this.assert();
39
46
  }
40
47
 
41
48
  protected async getEventFromSubStorage(event: string, options?: PastEventOptions) {
42
- return getEventsFromContract<SubStorage>(this.subStorageContract, event, options);
49
+ return getEventsFromContract<SubStorage>(this.subStorageContract, this.subStorageContractFork, event, options);
43
50
  }
44
51
 
45
52
  protected async getStrategiesSubs(subIds: number[]): Promise<StrategyModel.StoredSubDataStructOutputStruct[]> {
46
- const subStorageContract = this.subStorageContract;
47
-
48
- const defaultOptions = {
49
- target: subStorageContract.address,
50
- abiItem: getAbiItem(subStorageContract.abi, 'strategiesSubs'),
51
- };
52
-
53
- const multicallCalls = subIds.map((subId) => ({ ...defaultOptions, params: [subId] }));
53
+ let options : any;
54
+ let web3: Web3;
55
+
56
+ if (this.web3Fork && this.subStorageContractFork) {
57
+ options = {
58
+ target: this.subStorageContractFork.address,
59
+ abiItem: getAbiItem(this.subStorageContractFork.abi, 'strategiesSubs'),
60
+ };
61
+ web3 = this.web3Fork;
62
+ } else {
63
+ options = {
64
+ target: this.subStorageContract.address,
65
+ abiItem: getAbiItem(this.subStorageContract.abi, 'strategiesSubs'),
66
+ };
67
+ web3 = this.web3;
68
+ }
54
69
 
55
- return multicall(this.web3, this.chainId, multicallCalls);
70
+ const multicallCalls = subIds.map((subId) => ({ ...options, params: [subId] }));
71
+ return multicall(web3, this.chainId, multicallCalls);
56
72
  }
57
73
 
58
74
  protected async getSubscriptionEventsFromSubStorage(options?: PastEventOptions) {
@@ -75,6 +91,7 @@ export default class StrategiesAutomation extends Automation {
75
91
  ...addToObjectIf(isDefined(addresses), { filter: { proxy: addresses } }),
76
92
  };
77
93
 
94
+
78
95
  const subscriptionEvents = (await this.getSubscriptionEventsFromSubStorage(_options)) as PlaceholderType; // TODO PlaceholderType
79
96
 
80
97
  let subscriptions: (Position.Automated | null)[] = [];
@@ -156,6 +156,16 @@ export const MAINNET_BUNDLES_INFO: MainnetBundleInfo = {
156
156
  strategyId: Strategies.Identifiers.Boost,
157
157
  protocol: PROTOCOLS.MakerDAO,
158
158
  },
159
+ [Bundles.MainnetIds.AAVE_V3_CLOSE_TO_DEBT]: {
160
+ strategyOrBundleId: Bundles.MainnetIds.AAVE_V3_CLOSE_TO_DEBT,
161
+ strategyId: Strategies.Identifiers.CloseOnPriceToDebt,
162
+ protocol: PROTOCOLS.AaveV3,
163
+ },
164
+ [Bundles.MainnetIds.AAVE_V3_CLOSE_TO_COLLATERAL]: {
165
+ strategyOrBundleId: Bundles.MainnetIds.AAVE_V3_CLOSE_TO_COLLATERAL,
166
+ strategyId: Strategies.Identifiers.CloseOnPriceToColl,
167
+ protocol: PROTOCOLS.AaveV3,
168
+ },
159
169
  };
160
170
 
161
171
  export const OPTIMISM_BUNDLES_INFO: OptimismBundleInfo = {
@@ -1,7 +1,7 @@
1
1
  import type Web3 from 'web3';
2
2
  import type { PastEventOptions } from 'web3-eth-contract';
3
3
  import type {
4
- BlockNumber, Multicall, Contract,
4
+ BlockNumber, Multicall, Contract, PlaceholderType,
5
5
  } from '../types';
6
6
 
7
7
  import { makeUniMulticallContract } from './contractService';
@@ -41,15 +41,28 @@ export async function multicall(
41
41
  return formattedResult;
42
42
  }
43
43
 
44
- export function getEventsFromContract<T extends BaseContract>(
44
+ export async function getEventsFromContract<T extends BaseContract>(
45
45
  contractWithMeta: Contract.WithMeta<T>,
46
+ contractWithMetaFork: Contract.WithMeta<T> | null,
46
47
  event: string, options?: PastEventOptions,
47
48
  ) {
48
- return contractWithMeta.contract.getPastEvents(
49
+ const events = await contractWithMeta.contract.getPastEvents(
49
50
  event,
50
51
  {
51
- fromBlock: contractWithMeta.createdBlock,
52
- ...addToObjectIf(isDefined(options), options),
52
+ ...addToObjectIf(isDefined(options), { ...options, fromBlock: contractWithMeta.createdBlock }),
53
53
  },
54
54
  );
55
+
56
+ let eventsFork : PlaceholderType = [];
57
+
58
+ if (contractWithMetaFork) {
59
+ eventsFork = await contractWithMetaFork.contract.getPastEvents(
60
+ event,
61
+ {
62
+ ...addToObjectIf(isDefined(options), { ...options, toBlock: 'latest' }),
63
+ },
64
+ );
65
+ }
66
+
67
+ return [...events, ...eventsFork];
55
68
  }
@@ -294,7 +294,7 @@ function parseLiquityBondProtection(position: Position.Automated, parseData: Par
294
294
 
295
295
  const parsingMethodsMapping: StrategiesToProtocolVersionMapping = {
296
296
  [ProtocolIdentifiers.StrategiesAutomation.MakerDAO]: {
297
- [Strategies.Identifiers.SavingsLiqProtection]: parseMakerSavingsLiqProtection, // TODO union type by protocol
297
+ [Strategies.Identifiers.SavingsLiqProtection]: parseMakerSavingsLiqProtection,
298
298
  [Strategies.Identifiers.CloseOnPriceToDebt]: parseMakerCloseOnPrice,
299
299
  [Strategies.Identifiers.CloseOnPriceToColl]: parseMakerCloseOnPrice,
300
300
  [Strategies.Identifiers.TrailingStopToColl]: parseMakerTrailingStop,
@@ -92,6 +92,8 @@ export namespace Bundles {
92
92
  AAVE_V3_BOOST = 9,
93
93
  MAKER_REPAY = 10,
94
94
  MAKER_BOOST = 11,
95
+ AAVE_V3_CLOSE_TO_DEBT = 12,
96
+ AAVE_V3_CLOSE_TO_COLLATERAL = 13,
95
97
  }
96
98
 
97
99
  export enum OptimismIds {
@@ -86,6 +86,7 @@ export declare namespace Interfaces {
86
86
 
87
87
  interface Automation {
88
88
  provider: Web3,
89
+ providerFork: Web3,
89
90
  }
90
91
  interface LegacyAutomation<T extends BaseContract> {
91
92
  provider: Web3,
@@ -146,6 +147,8 @@ export declare namespace Position {
146
147
  }
147
148
  }
148
149
 
150
+ type SpecificAny = Specific.CloseOnPrice | Specific.TrailingStop | Specific.RatioProtection | Specific.CloseOnPriceAave;
151
+
149
152
  export interface Automated {
150
153
  chainId: ChainId,
151
154
  owner: EthereumAddress,
@@ -166,7 +169,7 @@ export declare namespace Position {
166
169
  subData: PlaceholderType,
167
170
  },
168
171
  },
169
- specific: Specific.CloseOnPrice | Specific.TrailingStop | Specific.RatioProtection | Specific.CloseOnPriceAave,
172
+ specific: SpecificAny,
170
173
  }
171
174
 
172
175
  export interface LegacyAutomated {