@defisaver/automation-sdk 2.0.2 → 2.0.3
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/.eslintrc.js +1 -0
- package/esm/abis/index.js +27 -15
- package/esm/automation/private/Automation.js +9 -6
- package/esm/automation/private/LegacyAutomation.d.ts +5 -5
- package/esm/automation/private/LegacyAutomation.js +30 -23
- package/esm/automation/private/LegacyProtocol.js +4 -1
- package/esm/automation/private/Protocol.js +4 -1
- package/esm/automation/private/StrategiesAutomation.d.ts +2 -2
- package/esm/automation/private/StrategiesAutomation.js +27 -21
- package/esm/automation/public/ArbitrumStrategies.js +10 -4
- package/esm/automation/public/EthereumStrategies.js +10 -4
- package/esm/automation/public/OptimismStrategies.js +10 -4
- package/esm/automation/public/legacy/LegacyAaveAutomation.js +13 -7
- package/esm/automation/public/legacy/LegacyCompoundAutomation.js +13 -7
- package/esm/automation/public/legacy/LegacyMakerAutomation.js +13 -7
- package/esm/configuration.js +8 -5
- package/esm/constants/index.js +281 -275
- package/esm/index.js +57 -17
- package/esm/services/contractService.js +22 -14
- package/esm/services/ethereumService.js +18 -10
- package/esm/services/strategiesService.js +130 -103
- package/esm/services/strategySubService.js +90 -61
- package/esm/services/subDataService.js +151 -145
- package/esm/services/triggerService.d.ts +5 -5
- package/esm/services/triggerService.js +124 -94
- package/esm/services/utils.js +82 -33
- package/esm/services/utils.test.d.ts +1 -0
- package/esm/services/utils.test.js +362 -0
- package/esm/types/contracts/generated/Erc20.js +2 -1
- package/esm/types/contracts/generated/Legacy_AaveV2Subscriptions.js +2 -1
- package/esm/types/contracts/generated/Legacy_AuthCheck.js +2 -1
- package/esm/types/contracts/generated/Legacy_CompoundV2Subscriptions.js +2 -1
- package/esm/types/contracts/generated/Legacy_MakerSubscriptions.js +2 -1
- package/esm/types/contracts/generated/SubStorage.js +2 -1
- package/esm/types/contracts/generated/UniMulticall.js +2 -1
- package/esm/types/contracts/generated/index.js +2 -1
- package/esm/types/contracts/generated/types.js +2 -1
- package/esm/types/enums.js +17 -14
- package/esm/types/index.js +2 -1
- package/package.json +11 -7
- package/src/automation/private/LegacyAutomation.ts +11 -10
- package/src/automation/private/StrategiesAutomation.ts +5 -6
- package/src/configuration.ts +0 -3
- package/src/index.ts +0 -1
- package/src/services/ethereumService.ts +6 -6
- package/src/services/subDataService.ts +75 -76
- package/src/services/triggerService.ts +83 -83
- package/src/services/utils.test.ts +414 -0
- package/src/services/utils.ts +6 -7
- package/tsconfig.json +1 -1
- package/umd/index.js +7016 -4105
- package/src/types/typings/process.d.ts +0 -9
- package/yarn-error.log +0 -7233
|
@@ -1,76 +1,106 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.curveUsdSoftLiquidationTrigger = exports.curveUsdBorrowRateTrigger = exports.sparkQuotePriceTrigger = exports.sparkRatioTrigger = exports.exchangeOffchainPriceTrigger = exports.exchangeTimestampTrigger = exports.compoundV3RatioTrigger = exports.cBondsRebondTrigger = exports.aaveV2RatioTrigger = exports.liquityDebtInFrontTrigger = exports.liquityRatioTrigger = exports.compoundV2RatioTrigger = exports.aaveV3QuotePriceTrigger = exports.morphoAaveV2RatioTrigger = exports.aaveV3RatioTrigger = exports.makerRatioTrigger = exports.trailingStopTrigger = exports.chainlinkPriceTrigger = void 0;
|
|
30
|
+
const decimal_js_1 = __importDefault(require("decimal.js"));
|
|
31
|
+
const web3_eth_abi_1 = __importDefault(require("web3-eth-abi"));
|
|
32
|
+
const web3Utils = __importStar(require("web3-utils"));
|
|
33
|
+
const utils_1 = require("./utils");
|
|
34
|
+
exports.chainlinkPriceTrigger = {
|
|
5
35
|
encode(tokenAddr, price, state) {
|
|
6
|
-
const _price = new
|
|
7
|
-
return [
|
|
36
|
+
const _price = new decimal_js_1.default(price).mul(1e8).floor().toString();
|
|
37
|
+
return [web3_eth_abi_1.default.encodeParameters(['address', 'uint256', 'uint8'], [tokenAddr, _price, state])];
|
|
8
38
|
},
|
|
9
39
|
decode(triggerData) {
|
|
10
|
-
const decodedData =
|
|
11
|
-
return { tokenAddr: decodedData[0], price: new
|
|
40
|
+
const decodedData = web3_eth_abi_1.default.decodeParameters(['address', 'uint256', 'uint8'], triggerData[0]);
|
|
41
|
+
return { tokenAddr: decodedData[0], price: new decimal_js_1.default(decodedData[1]).div(1e8).toString(), state: +decodedData[2] };
|
|
12
42
|
},
|
|
13
43
|
};
|
|
14
|
-
|
|
44
|
+
exports.trailingStopTrigger = {
|
|
15
45
|
encode(tokenAddr, percentage, roundId) {
|
|
16
|
-
const _percentage = new
|
|
17
|
-
return [
|
|
46
|
+
const _percentage = new decimal_js_1.default(percentage).mul(1e8).toString();
|
|
47
|
+
return [web3_eth_abi_1.default.encodeParameters(['address', 'uint256', 'uint80'], [tokenAddr, _percentage, roundId])];
|
|
18
48
|
},
|
|
19
49
|
decode(triggerData) {
|
|
20
|
-
const decodedData =
|
|
21
|
-
const triggerPercentage = new
|
|
50
|
+
const decodedData = web3_eth_abi_1.default.decodeParameters(['address', 'uint256', 'uint80'], triggerData[0]);
|
|
51
|
+
const triggerPercentage = new decimal_js_1.default(decodedData[1]).div(1e8).toNumber();
|
|
22
52
|
return { tokenAddr: decodedData[0], triggerPercentage, roundId: decodedData[2] };
|
|
23
53
|
},
|
|
24
54
|
};
|
|
25
|
-
|
|
55
|
+
exports.makerRatioTrigger = {
|
|
26
56
|
encode(vaultId, ratioPercentage, ratioState) {
|
|
27
|
-
const ratioWei = ratioPercentageToWei(ratioPercentage);
|
|
28
|
-
return [
|
|
57
|
+
const ratioWei = (0, utils_1.ratioPercentageToWei)(ratioPercentage);
|
|
58
|
+
return [web3_eth_abi_1.default.encodeParameters(['uint256', 'uint256', 'uint8'], [vaultId, ratioWei, ratioState])];
|
|
29
59
|
},
|
|
30
60
|
decode(triggerData) {
|
|
31
|
-
const decodedData =
|
|
32
|
-
return { vaultId: +decodedData[0], ratio:
|
|
61
|
+
const decodedData = web3_eth_abi_1.default.decodeParameters(['uint256', 'uint256', 'uint8'], triggerData[0]);
|
|
62
|
+
return { vaultId: +decodedData[0], ratio: (0, utils_1.weiToRatioPercentage)(decodedData[1]), ratioState: +decodedData[2] };
|
|
33
63
|
},
|
|
34
64
|
};
|
|
35
|
-
|
|
65
|
+
exports.aaveV3RatioTrigger = {
|
|
36
66
|
encode(owner, market, ratioPercentage, ratioState) {
|
|
37
|
-
const ratioWei = ratioPercentageToWei(ratioPercentage);
|
|
38
|
-
return [
|
|
67
|
+
const ratioWei = (0, utils_1.ratioPercentageToWei)(ratioPercentage);
|
|
68
|
+
return [web3_eth_abi_1.default.encodeParameters(['address', 'address', 'uint256', 'uint8'], [owner, market, ratioWei, ratioState])];
|
|
39
69
|
},
|
|
40
70
|
decode(triggerData) {
|
|
41
|
-
const decodedData =
|
|
71
|
+
const decodedData = web3_eth_abi_1.default.decodeParameters(['address', 'address', 'uint256', 'uint8'], triggerData[0]);
|
|
42
72
|
return {
|
|
43
73
|
owner: decodedData[0],
|
|
44
74
|
market: decodedData[1],
|
|
45
|
-
ratio:
|
|
75
|
+
ratio: (0, utils_1.weiToRatioPercentage)(decodedData[2]),
|
|
46
76
|
ratioState: Number(decodedData[3]),
|
|
47
77
|
};
|
|
48
78
|
},
|
|
49
79
|
};
|
|
50
|
-
|
|
80
|
+
exports.morphoAaveV2RatioTrigger = {
|
|
51
81
|
encode(owner, ratioPercentage, ratioState) {
|
|
52
|
-
const ratioWei = new
|
|
53
|
-
return [
|
|
82
|
+
const ratioWei = new decimal_js_1.default(ratioPercentage).mul(1e16).toString();
|
|
83
|
+
return [web3_eth_abi_1.default.encodeParameters(['address', 'uint128', 'uint8'], [owner, ratioWei, ratioState])];
|
|
54
84
|
},
|
|
55
85
|
decode(triggerData) {
|
|
56
|
-
const decodedData =
|
|
86
|
+
const decodedData = web3_eth_abi_1.default.decodeParameters(['address', 'uint128', 'uint8'], triggerData[0]);
|
|
57
87
|
return {
|
|
58
88
|
owner: decodedData[0],
|
|
59
|
-
ratio: new
|
|
89
|
+
ratio: new decimal_js_1.default(decodedData[1]).div(1e16).toNumber(),
|
|
60
90
|
ratioState: Number(decodedData[2]),
|
|
61
91
|
};
|
|
62
92
|
},
|
|
63
93
|
};
|
|
64
|
-
|
|
94
|
+
exports.aaveV3QuotePriceTrigger = {
|
|
65
95
|
encode(baseTokenAddress, quoteTokenAddress, price, ratioState) {
|
|
66
96
|
// Price is always in 8 decimals
|
|
67
|
-
const _price = new
|
|
68
|
-
return [
|
|
97
|
+
const _price = new decimal_js_1.default(price.toString()).mul(Math.pow(10, 8)).floor().toString();
|
|
98
|
+
return [web3_eth_abi_1.default.encodeParameters(['address', 'address', 'uint256', 'uint8'], [baseTokenAddress, quoteTokenAddress, _price, ratioState])];
|
|
69
99
|
},
|
|
70
100
|
decode(triggerData) {
|
|
71
|
-
const decodedData =
|
|
101
|
+
const decodedData = web3_eth_abi_1.default.decodeParameters(['address', 'address', 'uint256', 'uint8'], triggerData[0]);
|
|
72
102
|
// Price is always in 8 decimals
|
|
73
|
-
const price = new
|
|
103
|
+
const price = new decimal_js_1.default(decodedData[2]).div(Math.pow(10, 8)).toDP(8).toString();
|
|
74
104
|
return {
|
|
75
105
|
price,
|
|
76
106
|
baseTokenAddress: decodedData[0],
|
|
@@ -79,109 +109,109 @@ export const aaveV3QuotePriceTrigger = {
|
|
|
79
109
|
};
|
|
80
110
|
},
|
|
81
111
|
};
|
|
82
|
-
|
|
112
|
+
exports.compoundV2RatioTrigger = {
|
|
83
113
|
encode(owner, ratioPercentage, ratioState) {
|
|
84
|
-
const ratioWei = ratioPercentageToWei(ratioPercentage);
|
|
85
|
-
return [
|
|
114
|
+
const ratioWei = (0, utils_1.ratioPercentageToWei)(ratioPercentage);
|
|
115
|
+
return [web3_eth_abi_1.default.encodeParameters(['address', 'uint256', 'uint8'], [owner, ratioWei, ratioState])];
|
|
86
116
|
},
|
|
87
117
|
decode(triggerData) {
|
|
88
|
-
const decodedData =
|
|
118
|
+
const decodedData = web3_eth_abi_1.default.decodeParameters(['address', 'uint256', 'uint8'], triggerData[0]);
|
|
89
119
|
return {
|
|
90
120
|
owner: decodedData[0],
|
|
91
|
-
ratio:
|
|
121
|
+
ratio: (0, utils_1.weiToRatioPercentage)(decodedData[1]),
|
|
92
122
|
ratioState: +decodedData[2],
|
|
93
123
|
};
|
|
94
124
|
},
|
|
95
125
|
};
|
|
96
|
-
|
|
126
|
+
exports.liquityRatioTrigger = {
|
|
97
127
|
encode(owner, ratioPercentage, ratioState) {
|
|
98
|
-
const ratioWei = ratioPercentageToWei(ratioPercentage);
|
|
99
|
-
return [
|
|
128
|
+
const ratioWei = (0, utils_1.ratioPercentageToWei)(ratioPercentage);
|
|
129
|
+
return [web3_eth_abi_1.default.encodeParameters(['address', 'uint256', 'uint8'], [owner, ratioWei, ratioState])];
|
|
100
130
|
},
|
|
101
131
|
decode(triggerData) {
|
|
102
|
-
const decodedData =
|
|
132
|
+
const decodedData = web3_eth_abi_1.default.decodeParameters(['address', 'uint256', 'uint8'], triggerData[0]);
|
|
103
133
|
return {
|
|
104
134
|
owner: decodedData[0],
|
|
105
|
-
ratio:
|
|
135
|
+
ratio: (0, utils_1.weiToRatioPercentage)(decodedData[1]),
|
|
106
136
|
ratioState: +decodedData[2],
|
|
107
137
|
};
|
|
108
138
|
},
|
|
109
139
|
};
|
|
110
|
-
|
|
140
|
+
exports.liquityDebtInFrontTrigger = {
|
|
111
141
|
encode(owner, debtInFrontMin) {
|
|
112
|
-
return [
|
|
142
|
+
return [web3_eth_abi_1.default.encodeParameters(['address', 'uint256'], [owner, debtInFrontMin])];
|
|
113
143
|
},
|
|
114
144
|
decode(triggerData) {
|
|
115
|
-
const decodedData =
|
|
145
|
+
const decodedData = web3_eth_abi_1.default.decodeParameters(['address', 'uint256'], triggerData[0]);
|
|
116
146
|
return {
|
|
117
147
|
owner: decodedData[0],
|
|
118
148
|
debtInFrontMin: decodedData[1],
|
|
119
149
|
};
|
|
120
150
|
},
|
|
121
151
|
};
|
|
122
|
-
|
|
152
|
+
exports.aaveV2RatioTrigger = {
|
|
123
153
|
encode(owner, market, ratioPercentage, ratioState) {
|
|
124
|
-
const ratioWei = ratioPercentageToWei(ratioPercentage);
|
|
125
|
-
return [
|
|
154
|
+
const ratioWei = (0, utils_1.ratioPercentageToWei)(ratioPercentage);
|
|
155
|
+
return [web3_eth_abi_1.default.encodeParameters(['address', 'address', 'uint256', 'uint8'], [owner, market, ratioWei, ratioState])];
|
|
126
156
|
},
|
|
127
157
|
decode(triggerData) {
|
|
128
|
-
const decodedData =
|
|
158
|
+
const decodedData = web3_eth_abi_1.default.decodeParameters(['address', 'address', 'uint256', 'uint8'], triggerData[0]);
|
|
129
159
|
return {
|
|
130
160
|
owner: decodedData[0],
|
|
131
161
|
market: decodedData[1],
|
|
132
|
-
ratio:
|
|
162
|
+
ratio: (0, utils_1.weiToRatioPercentage)(decodedData[1]),
|
|
133
163
|
ratioState: +decodedData[3],
|
|
134
164
|
};
|
|
135
165
|
},
|
|
136
166
|
};
|
|
137
|
-
|
|
167
|
+
exports.cBondsRebondTrigger = {
|
|
138
168
|
encode(bondId) {
|
|
139
|
-
return [
|
|
169
|
+
return [web3_eth_abi_1.default.encodeParameters(['uint256'], [bondId])];
|
|
140
170
|
},
|
|
141
171
|
decode(triggerData) {
|
|
142
|
-
const decodedData =
|
|
172
|
+
const decodedData = web3_eth_abi_1.default.decodeParameters(['uint256'], triggerData[0]);
|
|
143
173
|
return { bondId: decodedData[0] };
|
|
144
174
|
},
|
|
145
175
|
};
|
|
146
|
-
|
|
176
|
+
exports.compoundV3RatioTrigger = {
|
|
147
177
|
encode(owner, market, ratioPercentage, ratioState) {
|
|
148
|
-
const ratioWei = ratioPercentageToWei(ratioPercentage);
|
|
149
|
-
return [
|
|
178
|
+
const ratioWei = (0, utils_1.ratioPercentageToWei)(ratioPercentage);
|
|
179
|
+
return [web3_eth_abi_1.default.encodeParameters(['address', 'address', 'uint256', 'uint8'], [owner, market, ratioWei, ratioState])];
|
|
150
180
|
},
|
|
151
181
|
decode(triggerData) {
|
|
152
|
-
const decodedData =
|
|
182
|
+
const decodedData = web3_eth_abi_1.default.decodeParameters(['address', 'address', 'uint256', 'uint8'], triggerData[0]);
|
|
153
183
|
return {
|
|
154
184
|
owner: decodedData[0],
|
|
155
185
|
market: decodedData[1],
|
|
156
|
-
ratio:
|
|
186
|
+
ratio: (0, utils_1.weiToRatioPercentage)(decodedData[1]),
|
|
157
187
|
ratioState: +decodedData[3],
|
|
158
188
|
};
|
|
159
189
|
},
|
|
160
190
|
};
|
|
161
|
-
|
|
191
|
+
exports.exchangeTimestampTrigger = {
|
|
162
192
|
encode(timestamp, interval) {
|
|
163
|
-
const timestampWei = new
|
|
164
|
-
const intervalWei = new
|
|
165
|
-
return [
|
|
193
|
+
const timestampWei = new decimal_js_1.default(timestamp).toString();
|
|
194
|
+
const intervalWei = new decimal_js_1.default(interval).toString();
|
|
195
|
+
return [web3_eth_abi_1.default.encodeParameters(['uint256', 'uint256'], [timestampWei, intervalWei])];
|
|
166
196
|
},
|
|
167
197
|
decode(triggerData) {
|
|
168
|
-
const decodedData =
|
|
198
|
+
const decodedData = web3_eth_abi_1.default.decodeParameters(['uint256', 'uint256'], triggerData[0]);
|
|
169
199
|
return {
|
|
170
200
|
timestamp: decodedData[0],
|
|
171
201
|
interval: decodedData[1],
|
|
172
202
|
};
|
|
173
203
|
},
|
|
174
204
|
};
|
|
175
|
-
|
|
205
|
+
exports.exchangeOffchainPriceTrigger = {
|
|
176
206
|
encode(targetPrice, goodUntil, orderType, fromTokenDecimals) {
|
|
177
|
-
const price = new
|
|
178
|
-
const goodUntilWei =
|
|
179
|
-
return [
|
|
207
|
+
const price = new decimal_js_1.default(targetPrice.toString()).mul(Math.pow(10, fromTokenDecimals)).floor().toString();
|
|
208
|
+
const goodUntilWei = web3Utils.toWei(new decimal_js_1.default(goodUntil).toString(), 'ether');
|
|
209
|
+
return [web3_eth_abi_1.default.encodeParameters(['uint256', 'uint256'], [price, goodUntilWei, orderType])];
|
|
180
210
|
},
|
|
181
211
|
decode(triggerData, fromTokenDecimals, toTokenDecimals) {
|
|
182
|
-
const decodedData =
|
|
183
|
-
const decimals = new
|
|
184
|
-
const price = new
|
|
212
|
+
const decodedData = web3_eth_abi_1.default.decodeParameters(['uint256', 'uint256', 'uint8'], triggerData[0]);
|
|
213
|
+
const decimals = new decimal_js_1.default(toTokenDecimals).plus(18).minus(fromTokenDecimals).toString();
|
|
214
|
+
const price = new decimal_js_1.default(decodedData[0]).div(new decimal_js_1.default(10).pow(decimals)).toDP(fromTokenDecimals).toString();
|
|
185
215
|
return {
|
|
186
216
|
targetPrice: price,
|
|
187
217
|
goodUntil: decodedData[1],
|
|
@@ -189,31 +219,31 @@ export const exchangeOffchainPriceTrigger = {
|
|
|
189
219
|
};
|
|
190
220
|
},
|
|
191
221
|
};
|
|
192
|
-
|
|
222
|
+
exports.sparkRatioTrigger = {
|
|
193
223
|
encode(owner, market, ratioPercentage, ratioState) {
|
|
194
|
-
const ratioWei = ratioPercentageToWei(ratioPercentage);
|
|
195
|
-
return [
|
|
224
|
+
const ratioWei = (0, utils_1.ratioPercentageToWei)(ratioPercentage);
|
|
225
|
+
return [web3_eth_abi_1.default.encodeParameters(['address', 'address', 'uint256', 'uint8'], [owner, market, ratioWei, ratioState])];
|
|
196
226
|
},
|
|
197
227
|
decode(triggerData) {
|
|
198
|
-
const decodedData =
|
|
228
|
+
const decodedData = web3_eth_abi_1.default.decodeParameters(['address', 'address', 'uint256', 'uint8'], triggerData[0]);
|
|
199
229
|
return {
|
|
200
230
|
owner: decodedData[0],
|
|
201
231
|
market: decodedData[1],
|
|
202
|
-
ratio:
|
|
232
|
+
ratio: (0, utils_1.weiToRatioPercentage)(decodedData[2]),
|
|
203
233
|
ratioState: Number(decodedData[3]),
|
|
204
234
|
};
|
|
205
235
|
},
|
|
206
236
|
};
|
|
207
|
-
|
|
237
|
+
exports.sparkQuotePriceTrigger = {
|
|
208
238
|
encode(baseTokenAddress, quoteTokenAddress, price, ratioState) {
|
|
209
239
|
// Price is always in 8 decimals
|
|
210
|
-
const _price = new
|
|
211
|
-
return [
|
|
240
|
+
const _price = new decimal_js_1.default(price.toString()).mul(Math.pow(10, 8)).floor().toString();
|
|
241
|
+
return [web3_eth_abi_1.default.encodeParameters(['address', 'address', 'uint256', 'uint8'], [baseTokenAddress, quoteTokenAddress, _price, ratioState])];
|
|
212
242
|
},
|
|
213
243
|
decode(triggerData) {
|
|
214
|
-
const decodedData =
|
|
244
|
+
const decodedData = web3_eth_abi_1.default.decodeParameters(['address', 'address', 'uint256', 'uint8'], triggerData[0]);
|
|
215
245
|
// Price is always in 8 decimals
|
|
216
|
-
const price = new
|
|
246
|
+
const price = new decimal_js_1.default(decodedData[2]).div(Math.pow(10, 8)).toDP(8).toString();
|
|
217
247
|
return {
|
|
218
248
|
price,
|
|
219
249
|
baseTokenAddress: decodedData[0],
|
|
@@ -222,21 +252,21 @@ export const sparkQuotePriceTrigger = {
|
|
|
222
252
|
};
|
|
223
253
|
},
|
|
224
254
|
};
|
|
225
|
-
|
|
255
|
+
exports.curveUsdBorrowRateTrigger = {
|
|
226
256
|
encode(market, targetRate, rateState) {
|
|
227
257
|
// the form is x = (e**(rate*365*86400))-1 where x*100 is number in %
|
|
228
258
|
// we reverse engineer that so we can calculate rate = ln(y/100 + 1) / 365*86400 where y is input in %
|
|
229
|
-
const rate = new
|
|
259
|
+
const rate = new decimal_js_1.default(new decimal_js_1.default(new decimal_js_1.default(targetRate).div(100)).plus(1)).ln().div(365).div(86400)
|
|
230
260
|
.toString();
|
|
231
|
-
const rateWei = new
|
|
232
|
-
return [
|
|
261
|
+
const rateWei = new decimal_js_1.default(rate).mul(Math.pow(10, 18)).floor().toString(); // 18 decimals
|
|
262
|
+
return [web3_eth_abi_1.default.encodeParameters(['address', 'uint256', 'uint8'], [market, rateWei, rateState])];
|
|
233
263
|
},
|
|
234
264
|
decode(triggerData) {
|
|
235
|
-
const decodedData =
|
|
236
|
-
const rateEth =
|
|
265
|
+
const decodedData = web3_eth_abi_1.default.decodeParameters(['address', 'uint256', 'uint8'], triggerData[0]);
|
|
266
|
+
const rateEth = (0, utils_1.weiToRatioPercentage)(decodedData[1]);
|
|
237
267
|
// the form is x = (e**(rate*365*86400))-1 where x*100 is number in %
|
|
238
|
-
const exponentRate = new
|
|
239
|
-
const targetRate = new
|
|
268
|
+
const exponentRate = new decimal_js_1.default(rateEth).mul(365).mul(86400);
|
|
269
|
+
const targetRate = new decimal_js_1.default(new decimal_js_1.default(2.718281828459).pow(exponentRate).minus(1)).mul(100)
|
|
240
270
|
.toString();
|
|
241
271
|
return {
|
|
242
272
|
market: decodedData[0],
|
|
@@ -245,18 +275,18 @@ export const curveUsdBorrowRateTrigger = {
|
|
|
245
275
|
};
|
|
246
276
|
},
|
|
247
277
|
};
|
|
248
|
-
|
|
278
|
+
exports.curveUsdSoftLiquidationTrigger = {
|
|
249
279
|
encode(market, owner, percentage) {
|
|
250
280
|
// 100% = 1e18 => 1% = 1e16
|
|
251
|
-
const _percentage = new
|
|
252
|
-
return [
|
|
281
|
+
const _percentage = new decimal_js_1.default(percentage).mul(Math.pow(10, 16)).floor().toString();
|
|
282
|
+
return [web3_eth_abi_1.default.encodeParameters(['address', 'address', 'uint256'], [market, owner, _percentage])];
|
|
253
283
|
},
|
|
254
284
|
decode(triggerData) {
|
|
255
|
-
const decodedData =
|
|
285
|
+
const decodedData = web3_eth_abi_1.default.decodeParameters(['address', 'address', 'uint256'], triggerData[0]);
|
|
256
286
|
return {
|
|
257
287
|
market: decodedData[0],
|
|
258
288
|
owner: decodedData[1],
|
|
259
|
-
percentage: new
|
|
289
|
+
percentage: new decimal_js_1.default(decodedData[2]).div(Math.pow(10, 16)).toString(),
|
|
260
290
|
};
|
|
261
291
|
},
|
|
262
292
|
};
|
package/esm/services/utils.js
CHANGED
|
@@ -1,56 +1,102 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.getRatioStateInfoForAaveCloseStrategy = exports.requireAddresses = exports.requireAddress = exports.isEmptyBytes = exports.isRatioStateUnder = exports.isRatioStateOver = exports.weiToRatioPercentage = exports.ratioPercentageToWei = exports.encodeSubId = exports.compareSubHashes = exports.wethToEthByAddress = exports.wethToEth = exports.ethToWeth = exports.addToObjectIf = exports.addToArrayIf = exports.isAddress = exports.compareAddresses = exports.isUndefined = exports.isDefined = void 0;
|
|
30
|
+
const decimal_js_1 = __importDefault(require("decimal.js"));
|
|
31
|
+
const web3Utils = __importStar(require("web3-utils"));
|
|
32
|
+
const web3_eth_abi_1 = __importDefault(require("web3-eth-abi"));
|
|
33
|
+
const tokens_1 = require("@defisaver/tokens");
|
|
34
|
+
const enums_1 = require("../types/enums");
|
|
35
|
+
function isDefined(value) {
|
|
6
36
|
return value !== undefined && value !== null;
|
|
7
37
|
}
|
|
8
|
-
|
|
38
|
+
exports.isDefined = isDefined;
|
|
39
|
+
function isUndefined(value) {
|
|
9
40
|
return !isDefined(value);
|
|
10
41
|
}
|
|
11
|
-
|
|
42
|
+
exports.isUndefined = isUndefined;
|
|
43
|
+
function compareAddresses(firstAddress, secondAddress) {
|
|
12
44
|
return firstAddress.toLowerCase() === secondAddress.toLowerCase();
|
|
13
45
|
}
|
|
14
|
-
|
|
46
|
+
exports.compareAddresses = compareAddresses;
|
|
47
|
+
function isAddress(address) {
|
|
15
48
|
return new RegExp(/0x[0-9a-fA-F]{40}/).test(address);
|
|
16
49
|
}
|
|
17
|
-
|
|
50
|
+
exports.isAddress = isAddress;
|
|
51
|
+
function addToArrayIf(condition, ...items) {
|
|
18
52
|
return (condition ? items : []);
|
|
19
53
|
}
|
|
20
|
-
|
|
54
|
+
exports.addToArrayIf = addToArrayIf;
|
|
55
|
+
function addToObjectIf(condition, item) {
|
|
21
56
|
return (condition ? item : {});
|
|
22
57
|
}
|
|
23
|
-
|
|
58
|
+
exports.addToObjectIf = addToObjectIf;
|
|
59
|
+
function ethToWeth(maybeEth) {
|
|
24
60
|
return maybeEth === null || maybeEth === void 0 ? void 0 : maybeEth.replace(/^ETH$/, 'WETH');
|
|
25
61
|
}
|
|
26
|
-
|
|
62
|
+
exports.ethToWeth = ethToWeth;
|
|
63
|
+
function wethToEth(maybeWeth) {
|
|
27
64
|
return maybeWeth === null || maybeWeth === void 0 ? void 0 : maybeWeth.replace(/^WETH$/, 'ETH');
|
|
28
65
|
}
|
|
29
|
-
|
|
30
|
-
|
|
66
|
+
exports.wethToEth = wethToEth;
|
|
67
|
+
function wethToEthByAddress(maybeWethAddr, chainId = enums_1.ChainId.Ethereum) {
|
|
68
|
+
return (0, tokens_1.getAssetInfo)(wethToEth((0, tokens_1.getAssetInfoByAddress)(maybeWethAddr, chainId).symbol), chainId).address;
|
|
31
69
|
}
|
|
32
|
-
|
|
33
|
-
|
|
70
|
+
exports.wethToEthByAddress = wethToEthByAddress;
|
|
71
|
+
function compareSubHashes(currentSubHash, newSubStructDecoded) {
|
|
72
|
+
return currentSubHash === web3Utils.keccak256(web3_eth_abi_1.default.encodeParameter('(uint64,bool,bytes[],bytes32[])', newSubStructDecoded));
|
|
34
73
|
}
|
|
35
|
-
|
|
36
|
-
|
|
74
|
+
exports.compareSubHashes = compareSubHashes;
|
|
75
|
+
function encodeSubId(subIdDec = '0') {
|
|
76
|
+
return new decimal_js_1.default(subIdDec).toHex().slice(2).padStart(8, '0');
|
|
37
77
|
}
|
|
38
|
-
|
|
39
|
-
|
|
78
|
+
exports.encodeSubId = encodeSubId;
|
|
79
|
+
function ratioPercentageToWei(ratioPercentage) {
|
|
80
|
+
return web3Utils.toWei(new decimal_js_1.default(ratioPercentage).div(100).toString(), 'ether');
|
|
40
81
|
}
|
|
41
|
-
|
|
42
|
-
|
|
82
|
+
exports.ratioPercentageToWei = ratioPercentageToWei;
|
|
83
|
+
function weiToRatioPercentage(ratioWei) {
|
|
84
|
+
return new decimal_js_1.default(web3Utils.fromWei(new decimal_js_1.default(ratioWei).mul(100).toString(), 'ether')).toNumber();
|
|
43
85
|
}
|
|
44
|
-
|
|
45
|
-
|
|
86
|
+
exports.weiToRatioPercentage = weiToRatioPercentage;
|
|
87
|
+
function isRatioStateOver(ratioState) {
|
|
88
|
+
return ratioState === enums_1.RatioState.OVER;
|
|
46
89
|
}
|
|
47
|
-
|
|
48
|
-
|
|
90
|
+
exports.isRatioStateOver = isRatioStateOver;
|
|
91
|
+
function isRatioStateUnder(ratioState) {
|
|
92
|
+
return ratioState === enums_1.RatioState.UNDER;
|
|
49
93
|
}
|
|
50
|
-
|
|
94
|
+
exports.isRatioStateUnder = isRatioStateUnder;
|
|
95
|
+
function isEmptyBytes(string) {
|
|
51
96
|
return string === '0x0000000000000000000000000000000000000000';
|
|
52
97
|
}
|
|
53
|
-
|
|
98
|
+
exports.isEmptyBytes = isEmptyBytes;
|
|
99
|
+
function requireAddress(address) {
|
|
54
100
|
if (typeof address !== 'string')
|
|
55
101
|
throw new Error('Address is not a string');
|
|
56
102
|
if (address === '')
|
|
@@ -62,17 +108,20 @@ export function requireAddress(address) {
|
|
|
62
108
|
if (!(new RegExp(/0x[0-9a-fA-F]{40}/).test(address)))
|
|
63
109
|
throw new Error('Address invalid');
|
|
64
110
|
}
|
|
65
|
-
|
|
111
|
+
exports.requireAddress = requireAddress;
|
|
112
|
+
function requireAddresses(addresses) {
|
|
66
113
|
addresses.forEach((address) => requireAddress(address));
|
|
67
114
|
}
|
|
68
|
-
|
|
115
|
+
exports.requireAddresses = requireAddresses;
|
|
116
|
+
function getRatioStateInfoForAaveCloseStrategy(currentRatioState, collAsset, debtAsset, chainId) {
|
|
69
117
|
// Flip only if stable/volatile to keep human-readable trigger price setting
|
|
70
|
-
const shouldFlip = getAssetInfoByAddress(collAsset, chainId).isStable && !getAssetInfoByAddress(debtAsset, chainId).isStable;
|
|
118
|
+
const shouldFlip = (0, tokens_1.getAssetInfoByAddress)(collAsset, chainId).isStable && !(0, tokens_1.getAssetInfoByAddress)(debtAsset, chainId).isStable;
|
|
71
119
|
let ratioState = currentRatioState;
|
|
72
120
|
if (shouldFlip) {
|
|
73
121
|
ratioState = isRatioStateOver(currentRatioState)
|
|
74
|
-
? ratioState = RatioState.UNDER
|
|
75
|
-
: ratioState = RatioState.OVER;
|
|
122
|
+
? ratioState = enums_1.RatioState.UNDER
|
|
123
|
+
: ratioState = enums_1.RatioState.OVER;
|
|
76
124
|
}
|
|
77
125
|
return { shouldFlip, ratioState };
|
|
78
126
|
}
|
|
127
|
+
exports.getRatioStateInfoForAaveCloseStrategy = getRatioStateInfoForAaveCloseStrategy;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|