@defisaver/automation-sdk 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.babelrc +3 -0
- package/.editorconfig +9 -0
- package/.eslintignore +7 -0
- package/.eslintrc.js +104 -0
- package/esm/abis/Erc20.json +223 -0
- package/esm/abis/SubStorage.json +17 -0
- package/esm/abis/UniMulticall.json +14 -0
- package/esm/abis/index.d.ts +9 -0
- package/esm/abis/index.js +18 -0
- package/esm/abis/legacy_AaveV2Subscriptions.json +8 -0
- package/esm/abis/legacy_AuthCheck.json +8 -0
- package/esm/abis/legacy_CompoundV2Subscriptions.json +9 -0
- package/esm/abis/legacy_MakerSubscriptions.json +9 -0
- package/esm/automation/private/Automation.d.ts +12 -0
- package/esm/automation/private/Automation.js +39 -0
- package/esm/automation/private/LegacyAutomation.d.ts +25 -0
- package/esm/automation/private/LegacyAutomation.js +102 -0
- package/esm/automation/private/LegacyProtocol.d.ts +20 -0
- package/esm/automation/private/LegacyProtocol.js +34 -0
- package/esm/automation/private/Protocol.d.ts +20 -0
- package/esm/automation/private/Protocol.js +34 -0
- package/esm/automation/private/StrategiesAutomation.d.ts +24 -0
- package/esm/automation/private/StrategiesAutomation.js +111 -0
- package/esm/automation/public/ArbitrumStrategies.d.ts +5 -0
- package/esm/automation/public/ArbitrumStrategies.js +7 -0
- package/esm/automation/public/EthereumStrategies.d.ts +5 -0
- package/esm/automation/public/EthereumStrategies.js +7 -0
- package/esm/automation/public/OptimismStrategies.d.ts +5 -0
- package/esm/automation/public/OptimismStrategies.js +7 -0
- package/esm/automation/public/legacy/LegacyAaveAutomation.d.ts +6 -0
- package/esm/automation/public/legacy/LegacyAaveAutomation.js +14 -0
- package/esm/automation/public/legacy/LegacyCompoundAutomation.d.ts +6 -0
- package/esm/automation/public/legacy/LegacyCompoundAutomation.js +14 -0
- package/esm/automation/public/legacy/LegacyMakerAutomation.d.ts +6 -0
- package/esm/automation/public/legacy/LegacyMakerAutomation.js +14 -0
- package/esm/configuration.d.ts +1 -0
- package/esm/configuration.js +9 -0
- package/esm/constants/index.d.ts +23 -0
- package/esm/constants/index.js +153 -0
- package/esm/index.d.ts +20 -0
- package/esm/index.js +22 -0
- package/esm/services/contractService.d.ts +11 -0
- package/esm/services/contractService.js +40 -0
- package/esm/services/ethereumService.d.ts +7 -0
- package/esm/services/ethereumService.js +34 -0
- package/esm/services/strategiesService.d.ts +2 -0
- package/esm/services/strategiesService.js +244 -0
- package/esm/services/strategySubService.d.ts +32 -0
- package/esm/services/strategySubService.js +91 -0
- package/esm/services/subDataService.d.ts +62 -0
- package/esm/services/subDataService.js +148 -0
- package/esm/services/triggerService.d.ts +89 -0
- package/esm/services/triggerService.js +141 -0
- package/esm/services/utils.d.ts +24 -0
- package/esm/services/utils.js +78 -0
- package/esm/types/contracts/generated/Erc20.d.ts +53 -0
- package/esm/types/contracts/generated/Erc20.js +4 -0
- package/esm/types/contracts/generated/Legacy_AaveV2Subscriptions.d.ts +129 -0
- package/esm/types/contracts/generated/Legacy_AaveV2Subscriptions.js +4 -0
- package/esm/types/contracts/generated/Legacy_AuthCheck.d.ts +20 -0
- package/esm/types/contracts/generated/Legacy_AuthCheck.js +4 -0
- package/esm/types/contracts/generated/Legacy_CompoundV2Subscriptions.d.ts +128 -0
- package/esm/types/contracts/generated/Legacy_CompoundV2Subscriptions.js +4 -0
- package/esm/types/contracts/generated/Legacy_MakerSubscriptions.d.ts +246 -0
- package/esm/types/contracts/generated/Legacy_MakerSubscriptions.js +4 -0
- package/esm/types/contracts/generated/SubStorage.d.ts +114 -0
- package/esm/types/contracts/generated/SubStorage.js +4 -0
- package/esm/types/contracts/generated/UniMulticall.d.ts +55 -0
- package/esm/types/contracts/generated/UniMulticall.js +4 -0
- package/esm/types/contracts/generated/index.d.ts +7 -0
- package/esm/types/contracts/generated/index.js +1 -0
- package/esm/types/contracts/generated/types.d.ts +54 -0
- package/esm/types/contracts/generated/types.js +1 -0
- package/esm/types/enums.d.ts +97 -0
- package/esm/types/enums.js +113 -0
- package/esm/types/index.d.ts +180 -0
- package/esm/types/index.js +1 -0
- package/package.json +66 -0
- package/scripts/generateContractTypes.js +39 -0
- package/src/abis/Erc20.json +223 -0
- package/src/abis/SubStorage.json +17 -0
- package/src/abis/UniMulticall.json +14 -0
- package/src/abis/index.ts +28 -0
- package/src/abis/legacy_AaveV2Subscriptions.json +8 -0
- package/src/abis/legacy_AuthCheck.json +8 -0
- package/src/abis/legacy_CompoundV2Subscriptions.json +9 -0
- package/src/abis/legacy_MakerSubscriptions.json +9 -0
- package/src/automation/private/Automation.ts +44 -0
- package/src/automation/private/LegacyAutomation.ts +124 -0
- package/src/automation/private/LegacyProtocol.ts +45 -0
- package/src/automation/private/Protocol.ts +45 -0
- package/src/automation/private/StrategiesAutomation.ts +157 -0
- package/src/automation/public/ArbitrumStrategies.ts +10 -0
- package/src/automation/public/EthereumStrategies.ts +10 -0
- package/src/automation/public/OptimismStrategies.ts +10 -0
- package/src/automation/public/legacy/LegacyAaveAutomation.ts +20 -0
- package/src/automation/public/legacy/LegacyCompoundAutomation.ts +20 -0
- package/src/automation/public/legacy/LegacyMakerAutomation.ts +20 -0
- package/src/configuration.ts +11 -0
- package/src/constants/index.ts +173 -0
- package/src/index.ts +38 -0
- package/src/services/contractService.ts +65 -0
- package/src/services/ethereumService.ts +55 -0
- package/src/services/strategiesService.ts +336 -0
- package/src/services/strategySubService.ts +202 -0
- package/src/services/subDataService.ts +212 -0
- package/src/services/triggerService.ts +173 -0
- package/src/services/utils.ts +101 -0
- package/src/types/contracts/generated/Erc20.ts +95 -0
- package/src/types/contracts/generated/Legacy_AaveV2Subscriptions.ts +207 -0
- package/src/types/contracts/generated/Legacy_AuthCheck.ts +41 -0
- package/src/types/contracts/generated/Legacy_CompoundV2Subscriptions.ts +205 -0
- package/src/types/contracts/generated/Legacy_MakerSubscriptions.ts +338 -0
- package/src/types/contracts/generated/SubStorage.ts +196 -0
- package/src/types/contracts/generated/UniMulticall.ts +83 -0
- package/src/types/contracts/generated/index.ts +10 -0
- package/src/types/contracts/generated/types.ts +73 -0
- package/src/types/enums.ts +108 -0
- package/src/types/index.ts +217 -0
- package/src/types/typings/process.d.ts +9 -0
- package/tsconfig.json +79 -0
- package/umd/web3.d.ts +28961 -0
- package/webpack.umd.js +52 -0
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import Dec from 'decimal.js';
|
|
2
|
+
import { getAssetInfo } from '@defisaver/tokens';
|
|
3
|
+
import { otherAddresses } from '@defisaver/sdk';
|
|
4
|
+
|
|
5
|
+
import type { EthereumAddress, SubData } from '../types';
|
|
6
|
+
import { ChainId } from '../types/enums';
|
|
7
|
+
|
|
8
|
+
import { ZERO_ADDRESS } from '../constants';
|
|
9
|
+
|
|
10
|
+
import { compareAddresses, ratioPercentageToWei, weiToRatioPercentage } from './utils';
|
|
11
|
+
|
|
12
|
+
const { mockedWeb3 } = process;
|
|
13
|
+
|
|
14
|
+
export const makerRepayFromSavingsSubData = {
|
|
15
|
+
encode(
|
|
16
|
+
vaultId: number,
|
|
17
|
+
targetRatioPercentage: number,
|
|
18
|
+
chainId: ChainId,
|
|
19
|
+
daiAddr?: EthereumAddress,
|
|
20
|
+
mcdCdpManagerAddr?: EthereumAddress,
|
|
21
|
+
): SubData {
|
|
22
|
+
const _daiAddr = daiAddr || getAssetInfo('DAI', chainId).address;
|
|
23
|
+
// @ts-ignore // TODO - this requires change in @defisaver/tokens
|
|
24
|
+
const _mcdCdpManagerAddr = mcdCdpManagerAddr || otherAddresses(chainId).McdCdpManager;
|
|
25
|
+
|
|
26
|
+
const vaultIdEncoded = mockedWeb3.eth.abi.encodeParameter('uint256', vaultId.toString());
|
|
27
|
+
const targetRatioWei = ratioPercentageToWei(targetRatioPercentage);
|
|
28
|
+
const targetRatioEncoded = mockedWeb3.eth.abi.encodeParameter('uint256', targetRatioWei);
|
|
29
|
+
const daiAddrEncoded = mockedWeb3.eth.abi.encodeParameter('address', _daiAddr);
|
|
30
|
+
const mcdManagerAddrEncoded = mockedWeb3.eth.abi.encodeParameter('address', _mcdCdpManagerAddr);
|
|
31
|
+
|
|
32
|
+
return [vaultIdEncoded, targetRatioEncoded, daiAddrEncoded, mcdManagerAddrEncoded];
|
|
33
|
+
},
|
|
34
|
+
decode(subData: SubData): { vaultId: number, daiAddr: string, mcdManagerAddr: string, targetRatio: number } {
|
|
35
|
+
const vaultId = +mockedWeb3.eth.abi.decodeParameter('uint256', subData[0]).toString();
|
|
36
|
+
|
|
37
|
+
const weiRatio = mockedWeb3.eth.abi.decodeParameter('uint256', subData[1]) as any as string;
|
|
38
|
+
const targetRatio = weiToRatioPercentage(weiRatio);
|
|
39
|
+
|
|
40
|
+
const daiAddr = mockedWeb3.eth.abi.decodeParameter('address', subData[2]).toString();
|
|
41
|
+
const mcdManagerAddr = mockedWeb3.eth.abi.decodeParameter('address', subData[3]).toString();
|
|
42
|
+
|
|
43
|
+
return {
|
|
44
|
+
vaultId, targetRatio, daiAddr, mcdManagerAddr,
|
|
45
|
+
};
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export const makerCloseSubData = {
|
|
50
|
+
encode(
|
|
51
|
+
vaultId: number,
|
|
52
|
+
closeToAssetAddr: EthereumAddress,
|
|
53
|
+
chainId: ChainId,
|
|
54
|
+
daiAddr?: EthereumAddress,
|
|
55
|
+
mcdCdpManagerAddr?: EthereumAddress,
|
|
56
|
+
): SubData {
|
|
57
|
+
const _daiAddr = daiAddr || getAssetInfo('DAI', chainId).address;
|
|
58
|
+
// @ts-ignore // TODO - this requires change in @defisaver/tokens
|
|
59
|
+
const _mcdCdpManagerAddr = mcdCdpManagerAddr || otherAddresses(chainId).McdCdpManager;
|
|
60
|
+
|
|
61
|
+
const vaultIdEncoded = mockedWeb3.eth.abi.encodeParameter('uint256', vaultId.toString());
|
|
62
|
+
const daiAddrEncoded = mockedWeb3.eth.abi.encodeParameter('address', _daiAddr);
|
|
63
|
+
const mcdManagerAddrEncoded = mockedWeb3.eth.abi.encodeParameter('address', _mcdCdpManagerAddr);
|
|
64
|
+
|
|
65
|
+
if (compareAddresses(closeToAssetAddr, _daiAddr)) {
|
|
66
|
+
// Close to DAI strategy
|
|
67
|
+
return [vaultIdEncoded, daiAddrEncoded, mcdManagerAddrEncoded];
|
|
68
|
+
}
|
|
69
|
+
// Close to collateral strategy
|
|
70
|
+
const collAddrEncoded = mockedWeb3.eth.abi.encodeParameter('address', closeToAssetAddr);
|
|
71
|
+
return [vaultIdEncoded, collAddrEncoded, daiAddrEncoded, mcdManagerAddrEncoded];
|
|
72
|
+
},
|
|
73
|
+
decode(subData: SubData): { vaultId: number, closeToAssetAddr: EthereumAddress } {
|
|
74
|
+
const vaultId = +mockedWeb3.eth.abi.decodeParameter('uint256', subData[0]);
|
|
75
|
+
// if closing to collateral, asset addr will be 2nd param out of 4
|
|
76
|
+
// if closing to DAI, will return 2nd param out of 3, which will be DAI addr
|
|
77
|
+
const closeToAssetAddr = mockedWeb3.eth.abi.decodeParameter('address', subData[1]).toString().toLowerCase();
|
|
78
|
+
|
|
79
|
+
return {
|
|
80
|
+
vaultId, closeToAssetAddr,
|
|
81
|
+
};
|
|
82
|
+
},
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
export const liquityCloseSubData = {
|
|
86
|
+
encode(
|
|
87
|
+
closeToAssetAddr: EthereumAddress,
|
|
88
|
+
chainId: ChainId = ChainId.Ethereum,
|
|
89
|
+
collAddr?: EthereumAddress,
|
|
90
|
+
debtAddr?: EthereumAddress,
|
|
91
|
+
): SubData {
|
|
92
|
+
const _collAddr = collAddr || getAssetInfo('WETH', chainId).address;
|
|
93
|
+
const _debtAddr = debtAddr || getAssetInfo('LUSD', chainId).address;
|
|
94
|
+
|
|
95
|
+
const collAddrEncoded = mockedWeb3.eth.abi.encodeParameter('address', _collAddr);
|
|
96
|
+
const debtAddrEncoded = mockedWeb3.eth.abi.encodeParameter('address', _debtAddr);
|
|
97
|
+
// if (compareAddresses(closeToAssetAddr, daiAddr)) { // TODO - Uhm, wth?
|
|
98
|
+
// // close to LUSD strategy
|
|
99
|
+
// return [daiAddrEncoded, mcdManagerAddrEncoded];
|
|
100
|
+
// }
|
|
101
|
+
// close to collateral strategy
|
|
102
|
+
return [collAddrEncoded, debtAddrEncoded];
|
|
103
|
+
},
|
|
104
|
+
decode(subData: SubData): { closeToAssetAddr: EthereumAddress, debtAddr: string } {
|
|
105
|
+
const closeToAssetAddr = mockedWeb3.eth.abi.decodeParameter('address', subData[0]).toString().toLowerCase();
|
|
106
|
+
const debtAddr = mockedWeb3.eth.abi.decodeParameter('address', subData[1]).toString().toLowerCase();
|
|
107
|
+
|
|
108
|
+
return { closeToAssetAddr, debtAddr };
|
|
109
|
+
},
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
export const aaveLeverageManagementSubData = { // TODO encode?
|
|
113
|
+
decode(subData: SubData): { targetRatio: number } {
|
|
114
|
+
const ratioWei = mockedWeb3.eth.abi.decodeParameter('uint256', subData[0]) as any as string;
|
|
115
|
+
const targetRatio = weiToRatioPercentage(ratioWei);
|
|
116
|
+
|
|
117
|
+
return { targetRatio };
|
|
118
|
+
},
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
export const aaveV3QuotePriceSubData = {
|
|
122
|
+
encode(
|
|
123
|
+
collAsset: EthereumAddress,
|
|
124
|
+
collAssetId: number,
|
|
125
|
+
debtAsset: EthereumAddress,
|
|
126
|
+
debtAssetId: number,
|
|
127
|
+
nullAddress: EthereumAddress = ZERO_ADDRESS,
|
|
128
|
+
): SubData {
|
|
129
|
+
const encodedColl = mockedWeb3.eth.abi.encodeParameter('address', collAsset);
|
|
130
|
+
const encodedCollId = mockedWeb3.eth.abi.encodeParameter('uint8', collAssetId);
|
|
131
|
+
|
|
132
|
+
const encodedDebt = mockedWeb3.eth.abi.encodeParameter('address', debtAsset);
|
|
133
|
+
const encodedDebtId = mockedWeb3.eth.abi.encodeParameter('uint8', debtAssetId);
|
|
134
|
+
|
|
135
|
+
const encodedNullAddress = mockedWeb3.eth.abi.encodeParameter('address', nullAddress);
|
|
136
|
+
|
|
137
|
+
return [encodedColl, encodedCollId, encodedDebt, encodedDebtId, encodedNullAddress];
|
|
138
|
+
},
|
|
139
|
+
decode(subData: SubData): { collAsset: EthereumAddress, collAssetId: number, debtAsset: EthereumAddress, debtAssetId: number } {
|
|
140
|
+
const collAsset = mockedWeb3.eth.abi.decodeParameter('address', subData[0]) as unknown as EthereumAddress;
|
|
141
|
+
const collAssetId = Number(mockedWeb3.eth.abi.decodeParameter('uint8', subData[1]));
|
|
142
|
+
|
|
143
|
+
const debtAsset = mockedWeb3.eth.abi.decodeParameter('address', subData[2]) as unknown as EthereumAddress;
|
|
144
|
+
const debtAssetId = Number(mockedWeb3.eth.abi.decodeParameter('uint8', subData[3]));
|
|
145
|
+
|
|
146
|
+
return {
|
|
147
|
+
collAsset, collAssetId, debtAsset, debtAssetId,
|
|
148
|
+
};
|
|
149
|
+
},
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
export const compoundV3LeverageManagementSubData = {
|
|
153
|
+
encode(
|
|
154
|
+
market: EthereumAddress,
|
|
155
|
+
baseToken: EthereumAddress,
|
|
156
|
+
repayFrom: number,
|
|
157
|
+
boostFrom: number,
|
|
158
|
+
boostTo: number,
|
|
159
|
+
repayTo: number,
|
|
160
|
+
boostEnabled: boolean,
|
|
161
|
+
isEOA: boolean,
|
|
162
|
+
): SubData {
|
|
163
|
+
return [
|
|
164
|
+
market,
|
|
165
|
+
baseToken,
|
|
166
|
+
new Dec(repayFrom).mul(1e16).toString(),
|
|
167
|
+
new Dec(boostFrom).mul(1e16).toString(),
|
|
168
|
+
new Dec(boostTo).mul(1e16).toString(),
|
|
169
|
+
new Dec(repayTo).mul(1e16).toString(),
|
|
170
|
+
// @ts-ignore // TODO
|
|
171
|
+
boostEnabled, isEOA,
|
|
172
|
+
];
|
|
173
|
+
},
|
|
174
|
+
decode(subData: SubData): { targetRatio: number } {
|
|
175
|
+
const weiRatio = mockedWeb3.eth.abi.decodeParameter('uint256', subData[3]) as any as string;
|
|
176
|
+
const targetRatio = weiToRatioPercentage(weiRatio);
|
|
177
|
+
|
|
178
|
+
return { targetRatio };
|
|
179
|
+
},
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
export const cBondsRebondSubData = {
|
|
183
|
+
encode(bondId: number | string): SubData {
|
|
184
|
+
const bondIdEncoded = mockedWeb3.eth.abi.encodeParameter('uint256', bondId);
|
|
185
|
+
return [bondIdEncoded];
|
|
186
|
+
},
|
|
187
|
+
decode(subData: SubData): { bondId: string } {
|
|
188
|
+
const bondId = mockedWeb3.eth.abi.decodeParameter('uint256', subData[1]).toString();
|
|
189
|
+
return { bondId };
|
|
190
|
+
},
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
export const liquityPaybackUsingChickenBondSubData = {
|
|
194
|
+
/**
|
|
195
|
+
* @param sourceId bondId or subId
|
|
196
|
+
* @param sourceType 0 for bond, 1 for subId
|
|
197
|
+
*/
|
|
198
|
+
encode: (sourceId: string, sourceType: number): SubData => {
|
|
199
|
+
const sourceIdEncoded = mockedWeb3.eth.abi.encodeParameter('uint256', sourceId);
|
|
200
|
+
const sourceTypeEncoded = mockedWeb3.eth.abi.encodeParameter('uint256', sourceType);
|
|
201
|
+
const lusdAddressEncoded = mockedWeb3.eth.abi.encodeParameter('address', getAssetInfo('LUSD').address);
|
|
202
|
+
const bLusdAddressEncoded = mockedWeb3.eth.abi.encodeParameter('address', getAssetInfo('bLUSD').address);
|
|
203
|
+
|
|
204
|
+
return [sourceIdEncoded, sourceTypeEncoded, lusdAddressEncoded, bLusdAddressEncoded];
|
|
205
|
+
},
|
|
206
|
+
decode: (subData: SubData) => {
|
|
207
|
+
const sourceId = mockedWeb3.eth.abi.decodeParameter('uint256', subData[0]).toString();
|
|
208
|
+
const sourceType = mockedWeb3.eth.abi.decodeParameter('uint256', subData[1]).toString();
|
|
209
|
+
|
|
210
|
+
return { sourceId, sourceType };
|
|
211
|
+
},
|
|
212
|
+
};
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import Dec from 'decimal.js';
|
|
2
|
+
|
|
3
|
+
import type {
|
|
4
|
+
EthereumAddress, TriggerData,
|
|
5
|
+
} from '../types';
|
|
6
|
+
import type { RatioState } from '../types/enums';
|
|
7
|
+
|
|
8
|
+
import { ratioPercentageToWei } from './utils';
|
|
9
|
+
|
|
10
|
+
const { mockedWeb3 } = process;
|
|
11
|
+
|
|
12
|
+
export const chainlinkPriceTrigger = {
|
|
13
|
+
encode(tokenAddr: EthereumAddress, price: string, state: RatioState) {
|
|
14
|
+
const _price = new Dec(price).mul(1e8).floor().toString();
|
|
15
|
+
return [mockedWeb3.eth.abi.encodeParameters(['address', 'uint256', 'uint8'], [tokenAddr, _price, state])];
|
|
16
|
+
},
|
|
17
|
+
decode(triggerData: TriggerData): { price: string, state: RatioState, tokenAddr: EthereumAddress } {
|
|
18
|
+
const decodedData = mockedWeb3.eth.abi.decodeParameters(['address', 'uint256', 'uint8'], triggerData[0]);
|
|
19
|
+
return { tokenAddr: decodedData[0], price: new Dec(decodedData[1]).div(1e8).toString(), state: +decodedData[2] };
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export const trailingStopTrigger = {
|
|
24
|
+
encode(tokenAddr: EthereumAddress, percentage: number, roundId: number) {
|
|
25
|
+
const _percentage = new Dec(percentage).mul(1e8).toString();
|
|
26
|
+
return [mockedWeb3.eth.abi.encodeParameters(['address', 'uint256', 'uint80'], [tokenAddr, _percentage, roundId])];
|
|
27
|
+
},
|
|
28
|
+
decode(triggerData: TriggerData):{ triggerPercentage: number, tokenAddr: EthereumAddress, roundId: string } {
|
|
29
|
+
const decodedData = mockedWeb3.eth.abi.decodeParameters(['address', 'uint256', 'uint80'], triggerData[0]);
|
|
30
|
+
const triggerPercentage = new Dec(decodedData[1]).div(1e8).toNumber();
|
|
31
|
+
return { tokenAddr: decodedData[0], triggerPercentage, roundId: decodedData[2] };
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export const makerRatioTrigger = {
|
|
36
|
+
encode(vaultId: number, ratioPercentage: number, ratioState: RatioState) {
|
|
37
|
+
const ratioWei = ratioPercentageToWei(ratioPercentage);
|
|
38
|
+
return [mockedWeb3.eth.abi.encodeParameters(['uint256', 'uint256', 'uint8'], [vaultId, ratioWei, ratioState])];
|
|
39
|
+
},
|
|
40
|
+
decode(triggerData: TriggerData): { vaultId: number, ratioState: number, ratio: number } {
|
|
41
|
+
const decodedData = mockedWeb3.eth.abi.decodeParameters(['uint256', 'uint256', 'uint8'], triggerData[0]);
|
|
42
|
+
return { vaultId: +decodedData[0], ratio: new Dec(mockedWeb3.utils.fromWei(decodedData[1])).mul(100).toNumber(), ratioState: +decodedData[2] };
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
export const aaveV3RatioTrigger = {
|
|
47
|
+
decode(triggerData: TriggerData) {
|
|
48
|
+
const decodedData = mockedWeb3.eth.abi.decodeParameters(['address', 'address', 'uint256', 'uint8'], triggerData[0]) as string[];
|
|
49
|
+
return {
|
|
50
|
+
market: decodedData[1],
|
|
51
|
+
ratio: new Dec(mockedWeb3.utils.fromWei(decodedData[2])).mul(100).toNumber(),
|
|
52
|
+
ratioState: Number(decodedData[3]),
|
|
53
|
+
};
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
export const aaveV3QuotePriceTrigger = {
|
|
58
|
+
encode(
|
|
59
|
+
baseTokenAddress: EthereumAddress,
|
|
60
|
+
quoteTokenAddress: EthereumAddress,
|
|
61
|
+
price: number,
|
|
62
|
+
ratioState: RatioState,
|
|
63
|
+
) {
|
|
64
|
+
// Price is always in 8 decimals
|
|
65
|
+
const _price = new Dec(price.toString()).mul(10 ** 8).floor().toString();
|
|
66
|
+
return [mockedWeb3.eth.abi.encodeParameters(['address', 'address', 'uint256', 'uint8'], [baseTokenAddress, quoteTokenAddress, _price, ratioState])];
|
|
67
|
+
},
|
|
68
|
+
decode(
|
|
69
|
+
triggerData: TriggerData,
|
|
70
|
+
): { baseTokenAddress: EthereumAddress, quoteTokenAddress: EthereumAddress, price: string, ratioState: RatioState } {
|
|
71
|
+
const decodedData = mockedWeb3.eth.abi.decodeParameters(['address', 'address', 'uint256', 'uint8'], triggerData[0]) as Array<string>;
|
|
72
|
+
// Price is always in 8 decimals
|
|
73
|
+
const price = new Dec(decodedData[2]).div(10 ** 8).toDP(8).toString();
|
|
74
|
+
return {
|
|
75
|
+
price,
|
|
76
|
+
baseTokenAddress: decodedData[0],
|
|
77
|
+
quoteTokenAddress: decodedData[1],
|
|
78
|
+
ratioState: +decodedData[3],
|
|
79
|
+
};
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
export const compoundV2RatioTrigger = {
|
|
84
|
+
encode(owner: EthereumAddress, ratioPercentage: number, ratioState: RatioState) {
|
|
85
|
+
const ratioWei = ratioPercentageToWei(ratioPercentage);
|
|
86
|
+
return [mockedWeb3.eth.abi.encodeParameters(['address', 'uint256', 'uint8'], [owner, ratioWei, ratioState])];
|
|
87
|
+
},
|
|
88
|
+
decode(triggerData: TriggerData): { owner: EthereumAddress, ratioState: RatioState, ratio: string } {
|
|
89
|
+
const decodedData = mockedWeb3.eth.abi.decodeParameters(['address', 'uint256', 'uint8'], triggerData[0]);
|
|
90
|
+
return {
|
|
91
|
+
owner: decodedData[0],
|
|
92
|
+
ratio: new Dec(mockedWeb3.utils.fromWei(decodedData[1])).mul(100).toString(),
|
|
93
|
+
ratioState: +decodedData[2],
|
|
94
|
+
};
|
|
95
|
+
},
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
export const liquityRatioTrigger = {
|
|
99
|
+
encode(owner: EthereumAddress, ratioPercentage: number, ratioState: RatioState) {
|
|
100
|
+
const ratioWei = ratioPercentageToWei(ratioPercentage);
|
|
101
|
+
return [mockedWeb3.eth.abi.encodeParameters(['address', 'uint256', 'uint8'], [owner, ratioWei, ratioState])];
|
|
102
|
+
},
|
|
103
|
+
decode(triggerData: TriggerData): { owner: EthereumAddress, ratioState: RatioState, ratio: string } {
|
|
104
|
+
const decodedData = mockedWeb3.eth.abi.decodeParameters(['address', 'uint256', 'uint8'], triggerData[0]);
|
|
105
|
+
return {
|
|
106
|
+
owner: decodedData[0],
|
|
107
|
+
ratio: new Dec(mockedWeb3.utils.fromWei(decodedData[1])).mul(100).toString(),
|
|
108
|
+
ratioState: +decodedData[2],
|
|
109
|
+
};
|
|
110
|
+
},
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
export const liquityDebtInFrontTrigger = {
|
|
114
|
+
encode(owner: EthereumAddress, debtInFrontMin: string) {
|
|
115
|
+
return [mockedWeb3.eth.abi.encodeParameters(['address', 'uint256'], [owner, debtInFrontMin])];
|
|
116
|
+
},
|
|
117
|
+
decode(triggerData: TriggerData): { owner: EthereumAddress, debtInFrontMin: string } {
|
|
118
|
+
const decodedData = mockedWeb3.eth.abi.decodeParameters(['address', 'uint256'], triggerData[0]);
|
|
119
|
+
return {
|
|
120
|
+
owner: decodedData[0],
|
|
121
|
+
debtInFrontMin: decodedData[1],
|
|
122
|
+
};
|
|
123
|
+
},
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
export const aaveV2RatioTrigger = {
|
|
127
|
+
encode(owner: EthereumAddress, market: EthereumAddress, ratioPercentage: number, ratioState: RatioState) {
|
|
128
|
+
const ratioWei = ratioPercentageToWei(ratioPercentage);
|
|
129
|
+
return [mockedWeb3.eth.abi.encodeParameters(['address', 'address', 'uint256', 'uint8'], [owner, market, ratioWei, ratioState])];
|
|
130
|
+
},
|
|
131
|
+
decode(triggerData: TriggerData): { owner: EthereumAddress, market:EthereumAddress, ratioState: RatioState, ratio: string } {
|
|
132
|
+
const decodedData = mockedWeb3.eth.abi.decodeParameters(['address', 'address', 'uint256', 'uint8'], triggerData[0]);
|
|
133
|
+
return {
|
|
134
|
+
owner: decodedData[0],
|
|
135
|
+
market: decodedData[1],
|
|
136
|
+
ratio: new Dec(mockedWeb3.utils.fromWei(decodedData[2])).mul(100).toString(),
|
|
137
|
+
ratioState: +decodedData[3],
|
|
138
|
+
};
|
|
139
|
+
},
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
export const cBondsRebondTrigger = {
|
|
143
|
+
encode(bondId: number | string) {
|
|
144
|
+
return [mockedWeb3.eth.abi.encodeParameters(['uint256'], [bondId])];
|
|
145
|
+
},
|
|
146
|
+
decode(triggerData: TriggerData): { bondId: string } {
|
|
147
|
+
const decodedData = mockedWeb3.eth.abi.decodeParameters(['uint256'], triggerData[0]);
|
|
148
|
+
return { bondId: decodedData[0] };
|
|
149
|
+
},
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
export const compoundV3RatioTrigger = {
|
|
153
|
+
encode(
|
|
154
|
+
owner: EthereumAddress,
|
|
155
|
+
market: EthereumAddress,
|
|
156
|
+
ratioPercentage: number,
|
|
157
|
+
ratioState: RatioState,
|
|
158
|
+
) {
|
|
159
|
+
const ratioWei = ratioPercentageToWei(ratioPercentage);
|
|
160
|
+
return [mockedWeb3.eth.abi.encodeParameters(['address', 'address', 'uint256', 'uint8'], [owner, market, ratioWei, ratioState])];
|
|
161
|
+
},
|
|
162
|
+
decode(
|
|
163
|
+
triggerData: TriggerData,
|
|
164
|
+
): { owner: EthereumAddress, market: EthereumAddress, ratioState: RatioState, ratio: number } {
|
|
165
|
+
const decodedData = mockedWeb3.eth.abi.decodeParameters(['address', 'address', 'uint256', 'uint8'], triggerData[0]);
|
|
166
|
+
return {
|
|
167
|
+
owner: decodedData[0],
|
|
168
|
+
market: decodedData[1],
|
|
169
|
+
ratio: new Dec(mockedWeb3.utils.fromWei(decodedData[2])).mul(100).toNumber(),
|
|
170
|
+
ratioState: +decodedData[3],
|
|
171
|
+
};
|
|
172
|
+
},
|
|
173
|
+
};
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { getAssetInfo, getAssetInfoByAddress } from '@defisaver/tokens';
|
|
2
|
+
import Dec from 'decimal.js';
|
|
3
|
+
|
|
4
|
+
import type { EthereumAddress } from '../types';
|
|
5
|
+
|
|
6
|
+
import { ChainId, RatioState } from '../types/enums';
|
|
7
|
+
|
|
8
|
+
const { mockedWeb3 } = process;
|
|
9
|
+
|
|
10
|
+
export function isDefined<T>(value: T): value is NonNullable<T> {
|
|
11
|
+
return value !== undefined && value !== null;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function isUndefined(value: unknown): boolean {
|
|
15
|
+
return !isDefined(value);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function compareAddresses(firstAddress: EthereumAddress, secondAddress: EthereumAddress): boolean {
|
|
19
|
+
return firstAddress.toLowerCase() === secondAddress.toLowerCase();
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function isAddress(address: EthereumAddress) {
|
|
23
|
+
return new RegExp(/0x[0-9a-fA-F]{40}/).test(address);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function addToArrayIf(condition: boolean, ...items: any): Array<any> {
|
|
27
|
+
return (condition ? items : []);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function addToObjectIf(condition: boolean, item: any): object {
|
|
31
|
+
return (condition ? item : {});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function ethToWeth(maybeEth: string) {
|
|
35
|
+
return maybeEth?.replace(/^ETH$/, 'WETH');
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function wethToEth(maybeWeth: string) {
|
|
39
|
+
return maybeWeth?.replace(/^WETH$/, 'ETH');
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function wethToEthByAddress(maybeWethAddr: EthereumAddress, chainId: ChainId = ChainId.Ethereum): EthereumAddress {
|
|
43
|
+
return getAssetInfo(wethToEth(getAssetInfoByAddress(maybeWethAddr, chainId).symbol), chainId).address;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function compareSubHashes(currentSubHash: string, newSubStructDecoded: object): boolean {
|
|
47
|
+
return currentSubHash === mockedWeb3.utils.keccak256(mockedWeb3.eth.abi.encodeParameter('(uint64,bool,bytes[],bytes32[])', newSubStructDecoded));
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function encodeSubId(subIdDec: string = '0') {
|
|
51
|
+
return new Dec(subIdDec).toHex().slice(2).padStart(8, '0');
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function ratioPercentageToWei(ratioPercentage: number) {
|
|
55
|
+
return mockedWeb3.utils.toWei(new Dec(ratioPercentage).div(100).toString());
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function weiToRatioPercentage(ratioWei: string) {
|
|
59
|
+
return new Dec(mockedWeb3.utils.fromWei(new Dec(ratioWei).mul(100).toString())).toNumber();
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function isRatioStateOver(ratioState: RatioState): boolean {
|
|
63
|
+
return ratioState === RatioState.OVER;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function isRatioStateUnder(ratioState: RatioState): boolean {
|
|
67
|
+
return ratioState === RatioState.UNDER;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function isEmptyBytes(string: string) {
|
|
71
|
+
return string === '0x0000000000000000000000000000000000000000';
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function requireAddress(address: EthereumAddress): void | never {
|
|
75
|
+
if (typeof address !== 'string') throw new Error('Address is not a string');
|
|
76
|
+
if (address === '') throw new Error('Address is empty string');
|
|
77
|
+
if (address.length < 42) throw new Error(`Address too short (${address.length} instead of 42)`);
|
|
78
|
+
if (isEmptyBytes(address)) throw new Error('Address is empty bytes');
|
|
79
|
+
if (!(new RegExp(/0x[0-9a-fA-F]{40}/).test(address))) throw new Error('Address invalid');
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function requireAddresses(addresses: EthereumAddress[]) {
|
|
83
|
+
addresses.forEach((address) => requireAddress(address));
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function getRatioStateInfoForAaveCloseStrategy(
|
|
87
|
+
currentRatioState: RatioState,
|
|
88
|
+
collAsset: EthereumAddress,
|
|
89
|
+
debtAsset: EthereumAddress,
|
|
90
|
+
chainId: ChainId,
|
|
91
|
+
): { shouldFlip: boolean, ratioState: RatioState } {
|
|
92
|
+
// Flip only if stable/volatile to keep human-readable trigger price setting
|
|
93
|
+
const shouldFlip = getAssetInfoByAddress(collAsset, chainId).isStable && !getAssetInfoByAddress(debtAsset, chainId).isStable;
|
|
94
|
+
let ratioState = currentRatioState;
|
|
95
|
+
if (shouldFlip) {
|
|
96
|
+
ratioState = isRatioStateOver(currentRatioState)
|
|
97
|
+
? ratioState = RatioState.UNDER
|
|
98
|
+
: ratioState = RatioState.OVER;
|
|
99
|
+
}
|
|
100
|
+
return { shouldFlip, ratioState };
|
|
101
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/* Autogenerated file. Do not edit manually. */
|
|
2
|
+
/* tslint:disable */
|
|
3
|
+
/* eslint-disable */
|
|
4
|
+
|
|
5
|
+
import type BN from "bn.js";
|
|
6
|
+
import type { ContractOptions } from "web3-eth-contract";
|
|
7
|
+
import type { EventLog } from "web3-core";
|
|
8
|
+
import type { EventEmitter } from "events";
|
|
9
|
+
import type {
|
|
10
|
+
Callback,
|
|
11
|
+
PayableTransactionObject,
|
|
12
|
+
NonPayableTransactionObject,
|
|
13
|
+
BlockType,
|
|
14
|
+
ContractEventLog,
|
|
15
|
+
BaseContract,
|
|
16
|
+
} from "./types";
|
|
17
|
+
|
|
18
|
+
export interface EventOptions {
|
|
19
|
+
filter?: object;
|
|
20
|
+
fromBlock?: BlockType;
|
|
21
|
+
topics?: string[];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export type Approval = ContractEventLog<{
|
|
25
|
+
owner: string;
|
|
26
|
+
spender: string;
|
|
27
|
+
value: string;
|
|
28
|
+
0: string;
|
|
29
|
+
1: string;
|
|
30
|
+
2: string;
|
|
31
|
+
}>;
|
|
32
|
+
export type Transfer = ContractEventLog<{
|
|
33
|
+
from: string;
|
|
34
|
+
to: string;
|
|
35
|
+
value: string;
|
|
36
|
+
0: string;
|
|
37
|
+
1: string;
|
|
38
|
+
2: string;
|
|
39
|
+
}>;
|
|
40
|
+
|
|
41
|
+
export interface Erc20 extends BaseContract {
|
|
42
|
+
constructor(
|
|
43
|
+
jsonInterface: any[],
|
|
44
|
+
address?: string,
|
|
45
|
+
options?: ContractOptions
|
|
46
|
+
): Erc20;
|
|
47
|
+
clone(): Erc20;
|
|
48
|
+
methods: {
|
|
49
|
+
name(): NonPayableTransactionObject<string>;
|
|
50
|
+
|
|
51
|
+
approve(
|
|
52
|
+
_spender: string,
|
|
53
|
+
_value: number | string | BN
|
|
54
|
+
): NonPayableTransactionObject<boolean>;
|
|
55
|
+
|
|
56
|
+
totalSupply(): NonPayableTransactionObject<string>;
|
|
57
|
+
|
|
58
|
+
transferFrom(
|
|
59
|
+
_from: string,
|
|
60
|
+
_to: string,
|
|
61
|
+
_value: number | string | BN
|
|
62
|
+
): NonPayableTransactionObject<boolean>;
|
|
63
|
+
|
|
64
|
+
decimals(): NonPayableTransactionObject<string>;
|
|
65
|
+
|
|
66
|
+
balanceOf(_owner: string): NonPayableTransactionObject<string>;
|
|
67
|
+
|
|
68
|
+
symbol(): NonPayableTransactionObject<string>;
|
|
69
|
+
|
|
70
|
+
transfer(
|
|
71
|
+
_to: string,
|
|
72
|
+
_value: number | string | BN
|
|
73
|
+
): NonPayableTransactionObject<boolean>;
|
|
74
|
+
|
|
75
|
+
allowance(
|
|
76
|
+
_owner: string,
|
|
77
|
+
_spender: string
|
|
78
|
+
): NonPayableTransactionObject<string>;
|
|
79
|
+
};
|
|
80
|
+
events: {
|
|
81
|
+
Approval(cb?: Callback<Approval>): EventEmitter;
|
|
82
|
+
Approval(options?: EventOptions, cb?: Callback<Approval>): EventEmitter;
|
|
83
|
+
|
|
84
|
+
Transfer(cb?: Callback<Transfer>): EventEmitter;
|
|
85
|
+
Transfer(options?: EventOptions, cb?: Callback<Transfer>): EventEmitter;
|
|
86
|
+
|
|
87
|
+
allEvents(options?: EventOptions, cb?: Callback<EventLog>): EventEmitter;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
once(event: "Approval", cb: Callback<Approval>): void;
|
|
91
|
+
once(event: "Approval", options: EventOptions, cb: Callback<Approval>): void;
|
|
92
|
+
|
|
93
|
+
once(event: "Transfer", cb: Callback<Transfer>): void;
|
|
94
|
+
once(event: "Transfer", options: EventOptions, cb: Callback<Transfer>): void;
|
|
95
|
+
}
|