@defisaver/automation-sdk 2.0.8 → 2.0.9
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 +4 -0
- package/esm/automation/private/LegacyAutomation.js +9 -3
- package/esm/automation/private/StrategiesAutomation.d.ts +4 -0
- package/esm/automation/private/StrategiesAutomation.js +29 -4
- package/esm/types/index.d.ts +5 -2
- package/package.json +3 -3
- package/src/automation/private/LegacyAutomation.ts +14 -4
- package/src/automation/private/StrategiesAutomation.ts +40 -6
- package/src/types/index.ts +6 -2
- package/umd/index.js +772 -450
package/.env
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
RPC_1=https://mainnet.infura.io/v3/a3503e4501d040cd9ea2c913c68983a1
|
|
2
|
+
RPC_10=https://opt-mainnet.g.alchemy.com/v2/Pwh-hdbsP7Bf0CP_UMw4_Pw-NfHhSpIA
|
|
3
|
+
RPC_8453=https://base-mainnet.g.alchemy.com/v2/TERsAaPNcA7sgCVHHAMBvd95UtT4wCEf
|
|
4
|
+
RPC_42161=https://arb-mainnet.g.alchemy.com/v2/Pwh-hdbsP7Bf0CP_UMw4_Pw-NfHhSpIA
|
|
@@ -12,6 +12,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
12
12
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
13
|
};
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
const decimal_js_1 = __importDefault(require("decimal.js"));
|
|
15
16
|
const contractService_1 = require("../../services/contractService");
|
|
16
17
|
const ethereumService_1 = require("../../services/ethereumService");
|
|
17
18
|
const utils_1 = require("../../services/utils");
|
|
@@ -82,13 +83,18 @@ class LegacyAutomation extends Automation_1.default {
|
|
|
82
83
|
}
|
|
83
84
|
getParsedSubscriptions(addresses, options) {
|
|
84
85
|
return __awaiter(this, void 0, void 0, function* () {
|
|
86
|
+
// Legacy automation was disabled on block 18213086
|
|
87
|
+
if (!(options === null || options === void 0 ? void 0 : options.fromBlock)
|
|
88
|
+
|| (options === null || options === void 0 ? void 0 : options.fromBlock) === 'latest'
|
|
89
|
+
|| (options === null || options === void 0 ? void 0 : options.fromBlock) === 'pending'
|
|
90
|
+
|| ((options === null || options === void 0 ? void 0 : options.fromBlock) && new decimal_js_1.default(options === null || options === void 0 ? void 0 : options.fromBlock.toString()).lt(18213086))) {
|
|
91
|
+
return [];
|
|
92
|
+
}
|
|
85
93
|
const subscriptions = yield this._getSubscriptions(addresses, options);
|
|
86
|
-
// @ts-ignore
|
|
87
94
|
return subscriptions.map((sub) => ({
|
|
88
95
|
chainId: this.chainId,
|
|
89
96
|
owner: sub[this.getOwnerPropName()],
|
|
90
|
-
isEnabled:
|
|
91
|
-
? (options === null || options === void 0 ? void 0 : options.fromBlock) > 18213086 : false,
|
|
97
|
+
isEnabled: true,
|
|
92
98
|
protocol: this.protocol,
|
|
93
99
|
specific: Object.assign({}, sub),
|
|
94
100
|
strategy: {
|
|
@@ -20,6 +20,10 @@ export default class StrategiesAutomation extends Automation {
|
|
|
20
20
|
protected getSubscriptionEventsFromSubStorage(options?: PastEventOptions): Promise<any[]>;
|
|
21
21
|
protected getUpdateDataEventsFromSubStorage(options?: PastEventOptions): Promise<any[]>;
|
|
22
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)[];
|
|
23
27
|
protected _getSubscriptions(addresses?: EthereumAddress[], options?: SubscriptionOptions): Promise<(Position.Automated | null)[]>;
|
|
24
28
|
getSubscriptions(options?: SubscriptionOptions): Promise<(Position.Automated | null)[]>;
|
|
25
29
|
getSubscriptionsFor(addresses: EthereumAddress[], options?: SubscriptionOptions): Promise<(Position.Automated | null)[]>;
|
|
@@ -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 enums_1 = require("../../types/enums");
|
|
16
17
|
const utils_1 = require("../../services/utils");
|
|
17
18
|
const contractService_1 = require("../../services/contractService");
|
|
18
19
|
const ethereumService_1 = require("../../services/ethereumService");
|
|
@@ -70,14 +71,38 @@ class StrategiesAutomation extends Automation_1.default {
|
|
|
70
71
|
getParsedSubscriptions(parseData) {
|
|
71
72
|
return (0, strategiesService_1.parseStrategiesAutomatedPosition)(parseData);
|
|
72
73
|
}
|
|
74
|
+
/**
|
|
75
|
+
* @description Removes expired Limit Order subscriptions
|
|
76
|
+
*/
|
|
77
|
+
removeExpiredSubscriptions(subscriptions) {
|
|
78
|
+
return subscriptions.filter((subscription) => {
|
|
79
|
+
if (!subscription) {
|
|
80
|
+
return true;
|
|
81
|
+
}
|
|
82
|
+
const { protocol, strategy, strategyData } = subscription;
|
|
83
|
+
if (protocol.id === enums_1.ProtocolIdentifiers.StrategiesAutomation.Exchange
|
|
84
|
+
&& strategy.strategyId === enums_1.Strategies.Identifiers.LimitOrder) {
|
|
85
|
+
return new decimal_js_1.default(strategyData.decoded.triggerData.goodUntil).gt(new decimal_js_1.default(Date.now()).div(1000));
|
|
86
|
+
}
|
|
87
|
+
return true;
|
|
88
|
+
});
|
|
89
|
+
}
|
|
73
90
|
_getSubscriptions(addresses, options) {
|
|
74
91
|
return __awaiter(this, void 0, void 0, function* () {
|
|
75
92
|
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 } }));
|
|
76
|
-
|
|
93
|
+
let subscriptionEvents = (yield this.getSubscriptionEventsFromSubStorage(_options)); // TODO PlaceholderType
|
|
77
94
|
let subscriptions = [];
|
|
78
95
|
if (subscriptionEvents) {
|
|
79
|
-
|
|
80
|
-
|
|
96
|
+
let strategiesSubs = yield this.getStrategiesSubs(subscriptionEvents.map((e) => e.returnValues.subId), _options.toBlock);
|
|
97
|
+
if (_options.enabledOnly) {
|
|
98
|
+
const filteredSubscriptionEvents = [];
|
|
99
|
+
strategiesSubs = strategiesSubs.filter((sub, index) => {
|
|
100
|
+
if (sub === null || sub === void 0 ? void 0 : sub.isEnabled)
|
|
101
|
+
filteredSubscriptionEvents.push(subscriptionEvents[index]);
|
|
102
|
+
return sub === null || sub === void 0 ? void 0 : sub.isEnabled;
|
|
103
|
+
});
|
|
104
|
+
subscriptionEvents = filteredSubscriptionEvents;
|
|
105
|
+
}
|
|
81
106
|
subscriptions = yield Promise.all(strategiesSubs.map((sub, index) => __awaiter(this, void 0, void 0, function* () {
|
|
82
107
|
var _a;
|
|
83
108
|
let latestUpdate = subscriptionEvents[index].returnValues;
|
|
@@ -120,7 +145,7 @@ class StrategiesAutomation extends Automation_1.default {
|
|
|
120
145
|
}, []);
|
|
121
146
|
}
|
|
122
147
|
}
|
|
123
|
-
return subscriptions;
|
|
148
|
+
return _options.unexpiredOnly ? this.removeExpiredSubscriptions(subscriptions) : subscriptions;
|
|
124
149
|
});
|
|
125
150
|
}
|
|
126
151
|
getSubscriptions(options) {
|
package/esm/types/index.d.ts
CHANGED
|
@@ -45,11 +45,14 @@ export declare namespace Multicall {
|
|
|
45
45
|
}
|
|
46
46
|
type Payload = any[];
|
|
47
47
|
}
|
|
48
|
-
|
|
48
|
+
interface _SubscriptionOptions {
|
|
49
49
|
toBlock: BlockNumber;
|
|
50
50
|
fromBlock: BlockNumber;
|
|
51
|
-
mergeWithSameId
|
|
51
|
+
mergeWithSameId: boolean;
|
|
52
|
+
enabledOnly: boolean;
|
|
53
|
+
unexpiredOnly: boolean;
|
|
52
54
|
}
|
|
55
|
+
export type SubscriptionOptions = Partial<_SubscriptionOptions>;
|
|
53
56
|
export declare namespace Interfaces {
|
|
54
57
|
interface ProtocolBase {
|
|
55
58
|
id: ProtocolIdentifiers.StrategiesAutomation | ProtocolIdentifiers.LegacyAutomation;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@defisaver/automation-sdk",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.9",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "./umd/index.js",
|
|
6
6
|
"module": "./esm/index.js",
|
|
@@ -22,8 +22,8 @@
|
|
|
22
22
|
"license": "ISC",
|
|
23
23
|
"dependencies": {
|
|
24
24
|
"@defisaver/eslint-config": "^1.0.0",
|
|
25
|
-
"@defisaver/sdk": "^1.0.
|
|
26
|
-
"@defisaver/tokens": "^1.
|
|
25
|
+
"@defisaver/sdk": "^1.0.53",
|
|
26
|
+
"@defisaver/tokens": "^1.5.1",
|
|
27
27
|
"@ethersproject/address": "^5.0.10",
|
|
28
28
|
"@ethersproject/solidity": "^5.0.9",
|
|
29
29
|
"decimal.js": "^10.4.3",
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import Dec from 'decimal.js';
|
|
2
|
+
|
|
1
3
|
import type Web3 from 'web3';
|
|
2
4
|
import type {
|
|
3
5
|
EthereumAddress, Position, Interfaces, Contract, PlaceholderType, SubscriptionOptions,
|
|
@@ -99,14 +101,22 @@ export default class LegacyAutomation extends Automation {
|
|
|
99
101
|
}
|
|
100
102
|
|
|
101
103
|
protected async getParsedSubscriptions(addresses?: EthereumAddress[], options?: SubscriptionOptions): Promise<Position.LegacyAutomated[]> {
|
|
104
|
+
// Legacy automation was disabled on block 18213086
|
|
105
|
+
if (
|
|
106
|
+
!options?.fromBlock
|
|
107
|
+
|| options?.fromBlock === 'latest'
|
|
108
|
+
|| options?.fromBlock === 'pending'
|
|
109
|
+
|| (options?.fromBlock && new Dec(options?.fromBlock.toString()).lt(18213086))
|
|
110
|
+
) {
|
|
111
|
+
return [];
|
|
112
|
+
}
|
|
113
|
+
|
|
102
114
|
const subscriptions = await this._getSubscriptions(addresses, options);
|
|
103
115
|
|
|
104
|
-
|
|
105
|
-
return subscriptions.map((sub) => ({
|
|
116
|
+
return subscriptions.map((sub: any) => ({
|
|
106
117
|
chainId: this.chainId,
|
|
107
118
|
owner: sub[this.getOwnerPropName()],
|
|
108
|
-
isEnabled:
|
|
109
|
-
? options?.fromBlock > 18213086 : false, // Legacy automation was disabled on block 18213086
|
|
119
|
+
isEnabled: true,
|
|
110
120
|
protocol: this.protocol,
|
|
111
121
|
specific: { ...sub },
|
|
112
122
|
strategy: {
|
|
@@ -8,6 +8,7 @@ import type {
|
|
|
8
8
|
StrategyModel, SubStorage,
|
|
9
9
|
} from '../../types/contracts/generated/SubStorage';
|
|
10
10
|
import type { ChainId } from '../../types/enums';
|
|
11
|
+
import { Strategies, ProtocolIdentifiers } from '../../types/enums';
|
|
11
12
|
|
|
12
13
|
import { addToObjectIf, isDefined } from '../../services/utils';
|
|
13
14
|
import { getAbiItem, makeSubStorageContract } from '../../services/contractService';
|
|
@@ -84,20 +85,53 @@ export default class StrategiesAutomation extends Automation {
|
|
|
84
85
|
return parseStrategiesAutomatedPosition(parseData);
|
|
85
86
|
}
|
|
86
87
|
|
|
88
|
+
/**
|
|
89
|
+
* @description Removes expired Limit Order subscriptions
|
|
90
|
+
*/
|
|
91
|
+
protected removeExpiredSubscriptions(subscriptions: (Position.Automated | null)[]) {
|
|
92
|
+
return subscriptions.filter((subscription: Position.Automated | null) => {
|
|
93
|
+
if (!subscription) {
|
|
94
|
+
return true;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const { protocol, strategy, strategyData } = subscription;
|
|
98
|
+
|
|
99
|
+
if (
|
|
100
|
+
protocol.id === ProtocolIdentifiers.StrategiesAutomation.Exchange
|
|
101
|
+
&& strategy.strategyId === Strategies.Identifiers.LimitOrder
|
|
102
|
+
) {
|
|
103
|
+
return new Dec(strategyData.decoded.triggerData.goodUntil).gt(new Dec(Date.now()).div(1000));
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return true;
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
|
|
87
110
|
protected async _getSubscriptions(addresses?: EthereumAddress[], options?: SubscriptionOptions): Promise<(Position.Automated | null)[]> {
|
|
88
111
|
const _options = {
|
|
89
112
|
...addToObjectIf(isDefined(options), options),
|
|
90
113
|
...addToObjectIf(isDefined(addresses), { filter: { proxy: addresses } }),
|
|
91
|
-
};
|
|
114
|
+
} as SubscriptionOptions & PastEventOptions;
|
|
92
115
|
|
|
93
|
-
|
|
94
|
-
const subscriptionEvents = (await this.getSubscriptionEventsFromSubStorage(_options)) as PlaceholderType; // TODO PlaceholderType
|
|
116
|
+
let subscriptionEvents = (await this.getSubscriptionEventsFromSubStorage(_options)) as PlaceholderType; // TODO PlaceholderType
|
|
95
117
|
|
|
96
118
|
let subscriptions: (Position.Automated | null)[] = [];
|
|
97
119
|
|
|
98
120
|
if (subscriptionEvents) {
|
|
99
|
-
|
|
100
|
-
|
|
121
|
+
let strategiesSubs = await this.getStrategiesSubs(
|
|
122
|
+
subscriptionEvents.map((e: { returnValues: { subId: number } }) => e.returnValues.subId), _options.toBlock,
|
|
123
|
+
);
|
|
124
|
+
|
|
125
|
+
if (_options.enabledOnly) {
|
|
126
|
+
const filteredSubscriptionEvents = [] as PlaceholderType;
|
|
127
|
+
|
|
128
|
+
strategiesSubs = strategiesSubs.filter((sub, index) => {
|
|
129
|
+
if (sub?.isEnabled) filteredSubscriptionEvents.push(subscriptionEvents[index]);
|
|
130
|
+
return sub?.isEnabled;
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
subscriptionEvents = filteredSubscriptionEvents;
|
|
134
|
+
}
|
|
101
135
|
|
|
102
136
|
subscriptions = await Promise.all(strategiesSubs.map(async (sub, index: number) => {
|
|
103
137
|
let latestUpdate = subscriptionEvents[index].returnValues;
|
|
@@ -168,7 +202,7 @@ export default class StrategiesAutomation extends Automation {
|
|
|
168
202
|
}
|
|
169
203
|
}
|
|
170
204
|
|
|
171
|
-
return subscriptions;
|
|
205
|
+
return _options.unexpiredOnly ? this.removeExpiredSubscriptions(subscriptions) : subscriptions;
|
|
172
206
|
}
|
|
173
207
|
|
|
174
208
|
public async getSubscriptions(options?: SubscriptionOptions): Promise<(Position.Automated | null)[]> {
|
package/src/types/index.ts
CHANGED
|
@@ -61,12 +61,16 @@ export declare namespace Multicall {
|
|
|
61
61
|
type Payload = any[];
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
-
|
|
64
|
+
interface _SubscriptionOptions {
|
|
65
65
|
toBlock: BlockNumber,
|
|
66
66
|
fromBlock: BlockNumber,
|
|
67
|
-
mergeWithSameId
|
|
67
|
+
mergeWithSameId: boolean,
|
|
68
|
+
enabledOnly: boolean,
|
|
69
|
+
unexpiredOnly: boolean,
|
|
68
70
|
}
|
|
69
71
|
|
|
72
|
+
export type SubscriptionOptions = Partial<_SubscriptionOptions>;
|
|
73
|
+
|
|
70
74
|
export declare namespace Interfaces {
|
|
71
75
|
interface ProtocolBase {
|
|
72
76
|
id: ProtocolIdentifiers.StrategiesAutomation | ProtocolIdentifiers.LegacyAutomation,
|