@defisaver/automation-sdk 2.0.2 → 2.0.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.
Files changed (55) hide show
  1. package/.eslintrc.js +1 -0
  2. package/esm/abis/index.js +27 -15
  3. package/esm/automation/private/Automation.js +9 -6
  4. package/esm/automation/private/LegacyAutomation.d.ts +5 -5
  5. package/esm/automation/private/LegacyAutomation.js +30 -23
  6. package/esm/automation/private/LegacyProtocol.js +4 -1
  7. package/esm/automation/private/Protocol.js +4 -1
  8. package/esm/automation/private/StrategiesAutomation.d.ts +2 -2
  9. package/esm/automation/private/StrategiesAutomation.js +27 -21
  10. package/esm/automation/public/ArbitrumStrategies.js +10 -4
  11. package/esm/automation/public/EthereumStrategies.js +10 -4
  12. package/esm/automation/public/OptimismStrategies.js +10 -4
  13. package/esm/automation/public/legacy/LegacyAaveAutomation.js +13 -7
  14. package/esm/automation/public/legacy/LegacyCompoundAutomation.js +13 -7
  15. package/esm/automation/public/legacy/LegacyMakerAutomation.js +13 -7
  16. package/esm/configuration.js +8 -5
  17. package/esm/constants/index.js +281 -275
  18. package/esm/index.js +57 -17
  19. package/esm/services/contractService.js +22 -14
  20. package/esm/services/ethereumService.js +18 -10
  21. package/esm/services/strategiesService.js +132 -105
  22. package/esm/services/strategySubService.js +90 -61
  23. package/esm/services/subDataService.d.ts +5 -0
  24. package/esm/services/subDataService.js +158 -145
  25. package/esm/services/triggerService.d.ts +5 -5
  26. package/esm/services/triggerService.js +124 -94
  27. package/esm/services/utils.js +82 -33
  28. package/esm/services/utils.test.d.ts +1 -0
  29. package/esm/services/utils.test.js +362 -0
  30. package/esm/types/contracts/generated/Erc20.js +2 -1
  31. package/esm/types/contracts/generated/Legacy_AaveV2Subscriptions.js +2 -1
  32. package/esm/types/contracts/generated/Legacy_AuthCheck.js +2 -1
  33. package/esm/types/contracts/generated/Legacy_CompoundV2Subscriptions.js +2 -1
  34. package/esm/types/contracts/generated/Legacy_MakerSubscriptions.js +2 -1
  35. package/esm/types/contracts/generated/SubStorage.js +2 -1
  36. package/esm/types/contracts/generated/UniMulticall.js +2 -1
  37. package/esm/types/contracts/generated/index.js +2 -1
  38. package/esm/types/contracts/generated/types.js +2 -1
  39. package/esm/types/enums.js +17 -14
  40. package/esm/types/index.js +2 -1
  41. package/package.json +11 -7
  42. package/src/automation/private/LegacyAutomation.ts +11 -10
  43. package/src/automation/private/StrategiesAutomation.ts +5 -6
  44. package/src/configuration.ts +0 -3
  45. package/src/index.ts +0 -1
  46. package/src/services/ethereumService.ts +6 -6
  47. package/src/services/strategiesService.ts +2 -2
  48. package/src/services/subDataService.ts +84 -76
  49. package/src/services/triggerService.ts +83 -83
  50. package/src/services/utils.test.ts +414 -0
  51. package/src/services/utils.ts +6 -7
  52. package/tsconfig.json +1 -1
  53. package/umd/index.js +7045 -4124
  54. package/src/types/typings/process.d.ts +0 -9
  55. package/yarn-error.log +0 -7233
@@ -1,76 +1,106 @@
1
- import Dec from 'decimal.js';
2
- import { ratioPercentageToWei } from './utils';
3
- const { mockedWeb3 } = process;
4
- export const chainlinkPriceTrigger = {
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 Dec(price).mul(1e8).floor().toString();
7
- return [mockedWeb3.eth.abi.encodeParameters(['address', 'uint256', 'uint8'], [tokenAddr, _price, state])];
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 = mockedWeb3.eth.abi.decodeParameters(['address', 'uint256', 'uint8'], triggerData[0]);
11
- return { tokenAddr: decodedData[0], price: new Dec(decodedData[1]).div(1e8).toString(), state: +decodedData[2] };
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
- export const trailingStopTrigger = {
44
+ exports.trailingStopTrigger = {
15
45
  encode(tokenAddr, percentage, roundId) {
16
- const _percentage = new Dec(percentage).mul(1e8).toString();
17
- return [mockedWeb3.eth.abi.encodeParameters(['address', 'uint256', 'uint80'], [tokenAddr, _percentage, roundId])];
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 = mockedWeb3.eth.abi.decodeParameters(['address', 'uint256', 'uint80'], triggerData[0]);
21
- const triggerPercentage = new Dec(decodedData[1]).div(1e8).toNumber();
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
- export const makerRatioTrigger = {
55
+ exports.makerRatioTrigger = {
26
56
  encode(vaultId, ratioPercentage, ratioState) {
27
- const ratioWei = ratioPercentageToWei(ratioPercentage);
28
- return [mockedWeb3.eth.abi.encodeParameters(['uint256', 'uint256', 'uint8'], [vaultId, ratioWei, ratioState])];
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 = mockedWeb3.eth.abi.decodeParameters(['uint256', 'uint256', 'uint8'], triggerData[0]);
32
- return { vaultId: +decodedData[0], ratio: new Dec(mockedWeb3.utils.fromWei(decodedData[1])).mul(100).toNumber(), ratioState: +decodedData[2] };
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
- export const aaveV3RatioTrigger = {
65
+ exports.aaveV3RatioTrigger = {
36
66
  encode(owner, market, ratioPercentage, ratioState) {
37
- const ratioWei = ratioPercentageToWei(ratioPercentage);
38
- return [mockedWeb3.eth.abi.encodeParameters(['address', 'address', 'uint256', 'uint8'], [owner, market, ratioWei, ratioState])];
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 = mockedWeb3.eth.abi.decodeParameters(['address', 'address', 'uint256', 'uint8'], triggerData[0]);
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: new Dec(mockedWeb3.utils.fromWei(decodedData[2])).mul(100).toNumber(),
75
+ ratio: (0, utils_1.weiToRatioPercentage)(decodedData[2]),
46
76
  ratioState: Number(decodedData[3]),
47
77
  };
48
78
  },
49
79
  };
50
- export const morphoAaveV2RatioTrigger = {
80
+ exports.morphoAaveV2RatioTrigger = {
51
81
  encode(owner, ratioPercentage, ratioState) {
52
- const ratioWei = new Dec(ratioPercentage).mul(1e16).toString();
53
- return [mockedWeb3.eth.abi.encodeParameters(['address', 'uint128', 'uint8'], [owner, ratioWei, ratioState])];
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 = mockedWeb3.eth.abi.decodeParameters(['address', 'uint128', 'uint8'], triggerData[0]);
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 Dec(decodedData[1]).div(1e16).toNumber(),
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
- export const aaveV3QuotePriceTrigger = {
94
+ exports.aaveV3QuotePriceTrigger = {
65
95
  encode(baseTokenAddress, quoteTokenAddress, price, ratioState) {
66
96
  // Price is always in 8 decimals
67
- const _price = new Dec(price.toString()).mul(Math.pow(10, 8)).floor().toString();
68
- return [mockedWeb3.eth.abi.encodeParameters(['address', 'address', 'uint256', 'uint8'], [baseTokenAddress, quoteTokenAddress, _price, ratioState])];
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 = mockedWeb3.eth.abi.decodeParameters(['address', 'address', 'uint256', 'uint8'], triggerData[0]);
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 Dec(decodedData[2]).div(Math.pow(10, 8)).toDP(8).toString();
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
- export const compoundV2RatioTrigger = {
112
+ exports.compoundV2RatioTrigger = {
83
113
  encode(owner, ratioPercentage, ratioState) {
84
- const ratioWei = ratioPercentageToWei(ratioPercentage);
85
- return [mockedWeb3.eth.abi.encodeParameters(['address', 'uint256', 'uint8'], [owner, ratioWei, ratioState])];
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 = mockedWeb3.eth.abi.decodeParameters(['address', 'uint256', 'uint8'], triggerData[0]);
118
+ const decodedData = web3_eth_abi_1.default.decodeParameters(['address', 'uint256', 'uint8'], triggerData[0]);
89
119
  return {
90
120
  owner: decodedData[0],
91
- ratio: new Dec(mockedWeb3.utils.fromWei(decodedData[1])).mul(100).toNumber(),
121
+ ratio: (0, utils_1.weiToRatioPercentage)(decodedData[1]),
92
122
  ratioState: +decodedData[2],
93
123
  };
94
124
  },
95
125
  };
96
- export const liquityRatioTrigger = {
126
+ exports.liquityRatioTrigger = {
97
127
  encode(owner, ratioPercentage, ratioState) {
98
- const ratioWei = ratioPercentageToWei(ratioPercentage);
99
- return [mockedWeb3.eth.abi.encodeParameters(['address', 'uint256', 'uint8'], [owner, ratioWei, ratioState])];
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 = mockedWeb3.eth.abi.decodeParameters(['address', 'uint256', 'uint8'], triggerData[0]);
132
+ const decodedData = web3_eth_abi_1.default.decodeParameters(['address', 'uint256', 'uint8'], triggerData[0]);
103
133
  return {
104
134
  owner: decodedData[0],
105
- ratio: new Dec(mockedWeb3.utils.fromWei(decodedData[1])).mul(100).toNumber(),
135
+ ratio: (0, utils_1.weiToRatioPercentage)(decodedData[1]),
106
136
  ratioState: +decodedData[2],
107
137
  };
108
138
  },
109
139
  };
110
- export const liquityDebtInFrontTrigger = {
140
+ exports.liquityDebtInFrontTrigger = {
111
141
  encode(owner, debtInFrontMin) {
112
- return [mockedWeb3.eth.abi.encodeParameters(['address', 'uint256'], [owner, debtInFrontMin])];
142
+ return [web3_eth_abi_1.default.encodeParameters(['address', 'uint256'], [owner, debtInFrontMin])];
113
143
  },
114
144
  decode(triggerData) {
115
- const decodedData = mockedWeb3.eth.abi.decodeParameters(['address', 'uint256'], triggerData[0]);
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
- export const aaveV2RatioTrigger = {
152
+ exports.aaveV2RatioTrigger = {
123
153
  encode(owner, market, ratioPercentage, ratioState) {
124
- const ratioWei = ratioPercentageToWei(ratioPercentage);
125
- return [mockedWeb3.eth.abi.encodeParameters(['address', 'address', 'uint256', 'uint8'], [owner, market, ratioWei, ratioState])];
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 = mockedWeb3.eth.abi.decodeParameters(['address', 'address', 'uint256', 'uint8'], triggerData[0]);
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: new Dec(mockedWeb3.utils.fromWei(decodedData[2])).mul(100).toNumber(),
162
+ ratio: (0, utils_1.weiToRatioPercentage)(decodedData[2]),
133
163
  ratioState: +decodedData[3],
134
164
  };
135
165
  },
136
166
  };
137
- export const cBondsRebondTrigger = {
167
+ exports.cBondsRebondTrigger = {
138
168
  encode(bondId) {
139
- return [mockedWeb3.eth.abi.encodeParameters(['uint256'], [bondId])];
169
+ return [web3_eth_abi_1.default.encodeParameters(['uint256'], [bondId])];
140
170
  },
141
171
  decode(triggerData) {
142
- const decodedData = mockedWeb3.eth.abi.decodeParameters(['uint256'], triggerData[0]);
172
+ const decodedData = web3_eth_abi_1.default.decodeParameters(['uint256'], triggerData[0]);
143
173
  return { bondId: decodedData[0] };
144
174
  },
145
175
  };
146
- export const compoundV3RatioTrigger = {
176
+ exports.compoundV3RatioTrigger = {
147
177
  encode(owner, market, ratioPercentage, ratioState) {
148
- const ratioWei = ratioPercentageToWei(ratioPercentage);
149
- return [mockedWeb3.eth.abi.encodeParameters(['address', 'address', 'uint256', 'uint8'], [owner, market, ratioWei, ratioState])];
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 = mockedWeb3.eth.abi.decodeParameters(['address', 'address', 'uint256', 'uint8'], triggerData[0]);
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: new Dec(mockedWeb3.utils.fromWei(decodedData[2])).mul(100).toNumber(),
186
+ ratio: (0, utils_1.weiToRatioPercentage)(decodedData[2]),
157
187
  ratioState: +decodedData[3],
158
188
  };
159
189
  },
160
190
  };
161
- export const exchangeTimestampTrigger = {
191
+ exports.exchangeTimestampTrigger = {
162
192
  encode(timestamp, interval) {
163
- const timestampWei = new Dec(timestamp).toString();
164
- const intervalWei = new Dec(interval).toString();
165
- return [mockedWeb3.eth.abi.encodeParameters(['uint256', 'uint256'], [timestampWei, intervalWei])];
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 = mockedWeb3.eth.abi.decodeParameters(['uint256', 'uint256'], triggerData[0]);
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
- export const exchangeOffchainPriceTrigger = {
205
+ exports.exchangeOffchainPriceTrigger = {
176
206
  encode(targetPrice, goodUntil, orderType, fromTokenDecimals) {
177
- const price = new Dec(targetPrice.toString()).mul(Math.pow(10, fromTokenDecimals)).floor().toString();
178
- const goodUntilWei = mockedWeb3.utils.toWei(new Dec(goodUntil).toString());
179
- return [mockedWeb3.eth.abi.encodeParameters(['uint256', 'uint256'], [price, goodUntilWei, orderType])];
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 = mockedWeb3.eth.abi.decodeParameters(['uint256', 'uint256', 'uint8'], triggerData[0]);
183
- const decimals = new Dec(toTokenDecimals).plus(18).minus(fromTokenDecimals).toString();
184
- const price = new Dec(decodedData[0]).div(new Dec(10).pow(decimals)).toDP(fromTokenDecimals).toString();
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
- export const sparkRatioTrigger = {
222
+ exports.sparkRatioTrigger = {
193
223
  encode(owner, market, ratioPercentage, ratioState) {
194
- const ratioWei = ratioPercentageToWei(ratioPercentage);
195
- return [mockedWeb3.eth.abi.encodeParameters(['address', 'address', 'uint256', 'uint8'], [owner, market, ratioWei, ratioState])];
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 = mockedWeb3.eth.abi.decodeParameters(['address', 'address', 'uint256', 'uint8'], triggerData[0]);
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: new Dec(mockedWeb3.utils.fromWei(decodedData[2])).mul(100).toNumber(),
232
+ ratio: (0, utils_1.weiToRatioPercentage)(decodedData[2]),
203
233
  ratioState: Number(decodedData[3]),
204
234
  };
205
235
  },
206
236
  };
207
- export const sparkQuotePriceTrigger = {
237
+ exports.sparkQuotePriceTrigger = {
208
238
  encode(baseTokenAddress, quoteTokenAddress, price, ratioState) {
209
239
  // Price is always in 8 decimals
210
- const _price = new Dec(price.toString()).mul(Math.pow(10, 8)).floor().toString();
211
- return [mockedWeb3.eth.abi.encodeParameters(['address', 'address', 'uint256', 'uint8'], [baseTokenAddress, quoteTokenAddress, _price, ratioState])];
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 = mockedWeb3.eth.abi.decodeParameters(['address', 'address', 'uint256', 'uint8'], triggerData[0]);
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 Dec(decodedData[2]).div(Math.pow(10, 8)).toDP(8).toString();
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
- export const curveUsdBorrowRateTrigger = {
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 Dec(new Dec(new Dec(targetRate).div(100)).plus(1)).ln().div(365).div(86400)
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 Dec(rate).mul(Math.pow(10, 18)).floor().toString(); // 18 decimals
232
- return [mockedWeb3.eth.abi.encodeParameters(['address', 'uint256', 'uint8'], [market, rateWei, rateState])];
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 = mockedWeb3.eth.abi.decodeParameters(['address', 'uint256', 'uint8'], triggerData[0]);
236
- const rateEth = mockedWeb3.utils.fromWei(decodedData[1]);
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 Dec(rateEth).mul(365).mul(86400);
239
- const targetRate = new Dec(new Dec(2.718281828459).pow(exponentRate).minus(1)).mul(100)
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
- export const curveUsdSoftLiquidationTrigger = {
278
+ exports.curveUsdSoftLiquidationTrigger = {
249
279
  encode(market, owner, percentage) {
250
280
  // 100% = 1e18 => 1% = 1e16
251
- const _percentage = new Dec(percentage).mul(Math.pow(10, 16)).floor().toString();
252
- return [mockedWeb3.eth.abi.encodeParameters(['address', 'address', 'uint256'], [market, owner, _percentage])];
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 = mockedWeb3.eth.abi.decodeParameters(['address', 'address', 'uint256'], triggerData[0]);
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 Dec(decodedData[2]).div(Math.pow(10, 16)).toString(),
289
+ percentage: new decimal_js_1.default(decodedData[2]).div(Math.pow(10, 16)).toString(),
260
290
  };
261
291
  },
262
292
  };
@@ -1,56 +1,102 @@
1
- import { getAssetInfo, getAssetInfoByAddress } from '@defisaver/tokens';
2
- import Dec from 'decimal.js';
3
- import { ChainId, RatioState } from '../types/enums';
4
- const { mockedWeb3 } = process;
5
- export function isDefined(value) {
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
- export function isUndefined(value) {
38
+ exports.isDefined = isDefined;
39
+ function isUndefined(value) {
9
40
  return !isDefined(value);
10
41
  }
11
- export function compareAddresses(firstAddress, secondAddress) {
42
+ exports.isUndefined = isUndefined;
43
+ function compareAddresses(firstAddress, secondAddress) {
12
44
  return firstAddress.toLowerCase() === secondAddress.toLowerCase();
13
45
  }
14
- export function isAddress(address) {
46
+ exports.compareAddresses = compareAddresses;
47
+ function isAddress(address) {
15
48
  return new RegExp(/0x[0-9a-fA-F]{40}/).test(address);
16
49
  }
17
- export function addToArrayIf(condition, ...items) {
50
+ exports.isAddress = isAddress;
51
+ function addToArrayIf(condition, ...items) {
18
52
  return (condition ? items : []);
19
53
  }
20
- export function addToObjectIf(condition, item) {
54
+ exports.addToArrayIf = addToArrayIf;
55
+ function addToObjectIf(condition, item) {
21
56
  return (condition ? item : {});
22
57
  }
23
- export function ethToWeth(maybeEth) {
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
- export function wethToEth(maybeWeth) {
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
- export function wethToEthByAddress(maybeWethAddr, chainId = ChainId.Ethereum) {
30
- return getAssetInfo(wethToEth(getAssetInfoByAddress(maybeWethAddr, chainId).symbol), chainId).address;
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
- export function compareSubHashes(currentSubHash, newSubStructDecoded) {
33
- return currentSubHash === mockedWeb3.utils.keccak256(mockedWeb3.eth.abi.encodeParameter('(uint64,bool,bytes[],bytes32[])', newSubStructDecoded));
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
- export function encodeSubId(subIdDec = '0') {
36
- return new Dec(subIdDec).toHex().slice(2).padStart(8, '0');
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
- export function ratioPercentageToWei(ratioPercentage) {
39
- return mockedWeb3.utils.toWei(new Dec(ratioPercentage).div(100).toString());
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
- export function weiToRatioPercentage(ratioWei) {
42
- return new Dec(mockedWeb3.utils.fromWei(new Dec(ratioWei).mul(100).toString())).toNumber();
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
- export function isRatioStateOver(ratioState) {
45
- return ratioState === RatioState.OVER;
86
+ exports.weiToRatioPercentage = weiToRatioPercentage;
87
+ function isRatioStateOver(ratioState) {
88
+ return ratioState === enums_1.RatioState.OVER;
46
89
  }
47
- export function isRatioStateUnder(ratioState) {
48
- return ratioState === RatioState.UNDER;
90
+ exports.isRatioStateOver = isRatioStateOver;
91
+ function isRatioStateUnder(ratioState) {
92
+ return ratioState === enums_1.RatioState.UNDER;
49
93
  }
50
- export function isEmptyBytes(string) {
94
+ exports.isRatioStateUnder = isRatioStateUnder;
95
+ function isEmptyBytes(string) {
51
96
  return string === '0x0000000000000000000000000000000000000000';
52
97
  }
53
- export function requireAddress(address) {
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
- export function requireAddresses(addresses) {
111
+ exports.requireAddress = requireAddress;
112
+ function requireAddresses(addresses) {
66
113
  addresses.forEach((address) => requireAddress(address));
67
114
  }
68
- export function getRatioStateInfoForAaveCloseStrategy(currentRatioState, collAsset, debtAsset, chainId) {
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 {};