@defisaver/automation-sdk 3.3.8 → 3.3.10-aave-v4-dev
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/cjs/constants/index.js +55 -0
- package/cjs/services/ethereumService.js +21 -14
- package/cjs/services/strategiesService.js +101 -0
- package/cjs/services/strategySubService.d.ts +8 -0
- package/cjs/services/strategySubService.js +47 -1
- package/cjs/services/strategySubService.test.js +143 -0
- package/cjs/services/subDataService.d.ts +302 -129
- package/cjs/services/subDataService.js +629 -372
- package/cjs/services/subDataService.test.js +213 -0
- package/cjs/services/triggerService.d.ts +29 -0
- package/cjs/services/triggerService.js +55 -1
- package/cjs/services/triggerService.test.js +84 -0
- package/cjs/types/enums.d.ts +14 -2
- package/cjs/types/enums.js +12 -0
- package/cjs/types/index.d.ts +22 -1
- package/esm/constants/index.js +55 -0
- package/esm/services/ethereumService.js +21 -14
- package/esm/services/strategiesService.js +102 -1
- package/esm/services/strategySubService.d.ts +8 -0
- package/esm/services/strategySubService.js +46 -0
- package/esm/services/strategySubService.test.js +144 -1
- package/esm/services/subDataService.d.ts +302 -129
- package/esm/services/subDataService.js +628 -371
- package/esm/services/subDataService.test.js +213 -0
- package/esm/services/triggerService.d.ts +29 -0
- package/esm/services/triggerService.js +54 -0
- package/esm/services/triggerService.test.js +85 -1
- package/esm/types/enums.d.ts +14 -2
- package/esm/types/enums.js +12 -0
- package/esm/types/index.d.ts +22 -1
- package/package.json +1 -1
- package/src/constants/index.ts +55 -0
- package/src/services/ethereumService.ts +23 -16
- package/src/services/strategiesService.ts +116 -1
- package/src/services/strategySubService.test.ts +180 -0
- package/src/services/strategySubService.ts +162 -2
- package/src/services/subDataService.test.ts +233 -0
- package/src/services/subDataService.ts +977 -703
- package/src/services/triggerService.test.ts +99 -0
- package/src/services/triggerService.ts +76 -1
- package/src/types/enums.ts +12 -0
- package/src/types/index.ts +27 -1
|
@@ -7,7 +7,9 @@ import type {
|
|
|
7
7
|
Position, ParseData, StrategiesToProtocolVersionMapping, BundleOrStrategy, StrategyOrBundleIds,
|
|
8
8
|
BundleInfoUnion, StrategyInfoUnion,
|
|
9
9
|
} from '../types';
|
|
10
|
-
import {
|
|
10
|
+
import {
|
|
11
|
+
ChainId, ProtocolIdentifiers, RatioState, Strategies,
|
|
12
|
+
} from '../types/enums';
|
|
11
13
|
|
|
12
14
|
import {
|
|
13
15
|
getPositionId, getRatioStateInfoForAaveCloseStrategy, getStopLossAndTakeProfitTypeByCloseStrategyType, isRatioStateOver, wethToEthByAddress,
|
|
@@ -383,6 +385,106 @@ function parseAaveV3CollateralSwitch(position: Position.Automated, parseData: Pa
|
|
|
383
385
|
return _position;
|
|
384
386
|
}
|
|
385
387
|
|
|
388
|
+
function parseAaveV4LeverageManagement(position: Position.Automated, parseData: ParseData): Position.Automated {
|
|
389
|
+
const _position = cloneDeep(position);
|
|
390
|
+
const { subStruct, subId, subHash } = parseData.subscriptionEventData;
|
|
391
|
+
const { isEnabled } = parseData.strategiesSubsData;
|
|
392
|
+
const triggerData = triggerService.aaveV4RatioTrigger.decode(subStruct.triggerData);
|
|
393
|
+
const subData = subDataService.aaveV4LeverageManagementSubData.decode(subStruct.subData);
|
|
394
|
+
const isEOA = _position.strategy.strategyId.includes('eoa');
|
|
395
|
+
const isRepay = [Strategies.Identifiers.Repay, Strategies.Identifiers.EoaRepay].includes(_position.strategy.strategyId as Strategies.Identifiers);
|
|
396
|
+
|
|
397
|
+
_position.strategyData.decoded.triggerData = triggerData;
|
|
398
|
+
_position.strategyData.decoded.subData = subData;
|
|
399
|
+
_position.positionId = getPositionId(_position.chainId, _position.protocol.id, _position.owner, triggerData.spoke);
|
|
400
|
+
_position.strategy.strategyId = isEOA ? Strategies.IdOverrides.EoaLeverageManagement : Strategies.IdOverrides.LeverageManagement;
|
|
401
|
+
|
|
402
|
+
if (isRepay) {
|
|
403
|
+
_position.specific = {
|
|
404
|
+
triggerRepayRatio: triggerData.ratio,
|
|
405
|
+
targetRepayRatio: subData.targetRatio,
|
|
406
|
+
repayEnabled: isEnabled,
|
|
407
|
+
subId1: Number(subId),
|
|
408
|
+
mergeWithId: isEOA ? Strategies.Identifiers.EoaBoost : Strategies.Identifiers.Boost,
|
|
409
|
+
subHashRepay: subHash,
|
|
410
|
+
};
|
|
411
|
+
} else {
|
|
412
|
+
_position.specific = {
|
|
413
|
+
triggerBoostRatio: triggerData.ratio,
|
|
414
|
+
targetBoostRatio: subData.targetRatio,
|
|
415
|
+
boostEnabled: isEnabled,
|
|
416
|
+
subId2: Number(subId),
|
|
417
|
+
mergeId: isEOA ? Strategies.Identifiers.EoaBoost : Strategies.Identifiers.Boost,
|
|
418
|
+
subHashBoost: subHash,
|
|
419
|
+
};
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
return _position;
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
function parseAaveV4LeverageManagementOnPrice(position: Position.Automated, parseData: ParseData): Position.Automated {
|
|
426
|
+
const _position = cloneDeep(position);
|
|
427
|
+
const { subStruct } = parseData.subscriptionEventData;
|
|
428
|
+
const triggerData = triggerService.aaveV4QuotePriceTrigger.decode(subStruct.triggerData);
|
|
429
|
+
const subData = subDataService.aaveV4LeverageManagementOnPriceSubData.decode(subStruct.subData);
|
|
430
|
+
const isEOA = _position.strategy.strategyId.includes('eoa');
|
|
431
|
+
|
|
432
|
+
_position.strategyData.decoded.triggerData = triggerData;
|
|
433
|
+
_position.strategyData.decoded.subData = subData;
|
|
434
|
+
_position.positionId = getPositionId(_position.chainId, _position.protocol.id, _position.owner, triggerData.spoke);
|
|
435
|
+
_position.strategy.strategyId = isEOA ? Strategies.IdOverrides.EoaLeverageManagementOnPrice : Strategies.IdOverrides.LeverageManagementOnPrice;
|
|
436
|
+
|
|
437
|
+
_position.specific = {
|
|
438
|
+
collAsset: subData.collAsset,
|
|
439
|
+
collAssetId: subData.collAssetId,
|
|
440
|
+
debtAsset: subData.debtAsset,
|
|
441
|
+
debtAssetId: subData.debtAssetId,
|
|
442
|
+
price: triggerData.price,
|
|
443
|
+
ratioState: triggerData.ratioState,
|
|
444
|
+
ratio: subData.targetRatio,
|
|
445
|
+
};
|
|
446
|
+
|
|
447
|
+
return _position;
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
function parseAaveV4CloseOnPrice(position: Position.Automated, parseData: ParseData): Position.Automated {
|
|
451
|
+
const _position = cloneDeep(position);
|
|
452
|
+
const { subStruct } = parseData.subscriptionEventData;
|
|
453
|
+
const triggerData = triggerService.aaveV4QuotePriceRangeTrigger.decode(subStruct.triggerData);
|
|
454
|
+
const subData = subDataService.aaveV4CloseSubData.decode(subStruct.subData);
|
|
455
|
+
const { takeProfitType, stopLossType } = getStopLossAndTakeProfitTypeByCloseStrategyType(+subData.closeType);
|
|
456
|
+
const isEOA = _position.strategy.strategyId.includes('eoa');
|
|
457
|
+
|
|
458
|
+
_position.strategyData.decoded.triggerData = triggerData;
|
|
459
|
+
_position.strategyData.decoded.subData = subData;
|
|
460
|
+
_position.positionId = getPositionId(_position.chainId, _position.protocol.id, _position.owner, triggerData.spoke);
|
|
461
|
+
_position.strategy.strategyId = isEOA ? Strategies.Identifiers.EoaCloseOnPrice : Strategies.Identifiers.CloseOnPrice;
|
|
462
|
+
|
|
463
|
+
_position.specific = {
|
|
464
|
+
collAsset: subData.collAsset,
|
|
465
|
+
collAssetId: subData.collAssetId,
|
|
466
|
+
debtAsset: subData.debtAsset,
|
|
467
|
+
debtAssetId: subData.debtAssetId,
|
|
468
|
+
stopLossPrice: triggerData.lowerPrice,
|
|
469
|
+
takeProfitPrice: triggerData.upperPrice,
|
|
470
|
+
stopLossType,
|
|
471
|
+
takeProfitType,
|
|
472
|
+
};
|
|
473
|
+
|
|
474
|
+
return _position;
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
function parseAaveV4CollateralSwitch(position: Position.Automated, parseData: ParseData): Position.Automated {
|
|
478
|
+
const _position = cloneDeep(position);
|
|
479
|
+
const { subStruct } = parseData.subscriptionEventData;
|
|
480
|
+
const triggerData = triggerService.aaveV4QuotePriceTrigger.decode(subStruct.triggerData);
|
|
481
|
+
const subData = subDataService.aaveV4CollateralSwitchSubData.decode(subStruct.subData);
|
|
482
|
+
_position.strategyData.decoded.triggerData = triggerData;
|
|
483
|
+
_position.strategyData.decoded.subData = subData;
|
|
484
|
+
_position.positionId = getPositionId(_position.chainId, _position.protocol.id, _position.owner, triggerData.spoke);
|
|
485
|
+
return _position;
|
|
486
|
+
}
|
|
487
|
+
|
|
386
488
|
function parseMorphoAaveV2LeverageManagement(position: Position.Automated, parseData: ParseData): Position.Automated {
|
|
387
489
|
const _position = cloneDeep(position);
|
|
388
490
|
|
|
@@ -1207,6 +1309,19 @@ const parsingMethodsMapping: StrategiesToProtocolVersionMapping = {
|
|
|
1207
1309
|
[Strategies.Identifiers.EoaCloseOnPrice]: parseAaveV3CloseOnPrice,
|
|
1208
1310
|
[Strategies.Identifiers.CollateralSwitch]: parseAaveV3CollateralSwitch,
|
|
1209
1311
|
},
|
|
1312
|
+
[ProtocolIdentifiers.StrategiesAutomation.AaveV4]: {
|
|
1313
|
+
[Strategies.Identifiers.Repay]: parseAaveV4LeverageManagement,
|
|
1314
|
+
[Strategies.Identifiers.Boost]: parseAaveV4LeverageManagement,
|
|
1315
|
+
[Strategies.Identifiers.RepayOnPrice]: parseAaveV4LeverageManagementOnPrice,
|
|
1316
|
+
[Strategies.Identifiers.BoostOnPrice]: parseAaveV4LeverageManagementOnPrice,
|
|
1317
|
+
[Strategies.Identifiers.CloseOnPrice]: parseAaveV4CloseOnPrice,
|
|
1318
|
+
[Strategies.Identifiers.EoaRepay]: parseAaveV4LeverageManagement,
|
|
1319
|
+
[Strategies.Identifiers.EoaBoost]: parseAaveV4LeverageManagement,
|
|
1320
|
+
[Strategies.Identifiers.EoaRepayOnPrice]: parseAaveV4LeverageManagementOnPrice,
|
|
1321
|
+
[Strategies.Identifiers.EoaBoostOnPrice]: parseAaveV4LeverageManagementOnPrice,
|
|
1322
|
+
[Strategies.Identifiers.EoaCloseOnPrice]: parseAaveV4CloseOnPrice,
|
|
1323
|
+
[Strategies.Identifiers.CollateralSwitch]: parseAaveV4CollateralSwitch,
|
|
1324
|
+
},
|
|
1210
1325
|
[ProtocolIdentifiers.StrategiesAutomation.CompoundV2]: {
|
|
1211
1326
|
[Strategies.Identifiers.Repay]: parseCompoundV2LeverageManagement,
|
|
1212
1327
|
[Strategies.Identifiers.Boost]: parseCompoundV2LeverageManagement,
|
|
@@ -21,6 +21,7 @@ import {
|
|
|
21
21
|
crvUSDEncode,
|
|
22
22
|
compoundV3L2Encode,
|
|
23
23
|
morphoBlueEncode,
|
|
24
|
+
aaveV4Encode,
|
|
24
25
|
} from './strategySubService';
|
|
25
26
|
|
|
26
27
|
describe('Feature: strategySubService.ts', () => {
|
|
@@ -170,6 +171,61 @@ describe('Feature: strategySubService.ts', () => {
|
|
|
170
171
|
});
|
|
171
172
|
});
|
|
172
173
|
});
|
|
174
|
+
|
|
175
|
+
describe('leverageManagementWithoutSubProxy()', () => {
|
|
176
|
+
const examples: Array<[
|
|
177
|
+
[StrategyOrBundleIds, boolean, TriggerData, SubData],
|
|
178
|
+
[
|
|
179
|
+
vaultId: number, triggerRatio: number, targetRatio: number, ratioState: RatioState,
|
|
180
|
+
isBoost: boolean, daiAddr?: EthereumAddress,
|
|
181
|
+
]
|
|
182
|
+
]> = [
|
|
183
|
+
// Repay scenario (isBoost=false, RatioState.UNDER)
|
|
184
|
+
[
|
|
185
|
+
[
|
|
186
|
+
Bundles.MainnetIds.MAKER_REPAY,
|
|
187
|
+
true,
|
|
188
|
+
['0x00000000000000000000000000000000000000000000000000000000000000de000000000000000000000000000000000000000000000000136dcc951d8c00000000000000000000000000000000000000000000000000000000000000000001'],
|
|
189
|
+
[
|
|
190
|
+
'0x00000000000000000000000000000000000000000000000000000000000000de', '0x00000000000000000000000000000000000000000000000018fae27693b40000',
|
|
191
|
+
'0x0000000000000000000000006b175474e89094c44da98b954eedeac495271d0f',
|
|
192
|
+
],
|
|
193
|
+
],
|
|
194
|
+
[
|
|
195
|
+
222, // vaultId
|
|
196
|
+
140, // triggerRatio
|
|
197
|
+
180, // targetRatio
|
|
198
|
+
RatioState.UNDER, // ratioState
|
|
199
|
+
false, // isBoost
|
|
200
|
+
]
|
|
201
|
+
],
|
|
202
|
+
// Boost scenario (isBoost=true, RatioState.OVER)
|
|
203
|
+
[
|
|
204
|
+
[
|
|
205
|
+
Bundles.MainnetIds.MAKER_BOOST,
|
|
206
|
+
true,
|
|
207
|
+
['0x000000000000000000000000000000000000000000000000000000000000014d00000000000000000000000000000000000000000000000026db992a3b1800000000000000000000000000000000000000000000000000000000000000000000'],
|
|
208
|
+
[
|
|
209
|
+
'0x000000000000000000000000000000000000000000000000000000000000014d', '0x00000000000000000000000000000000000000000000000022b1c8c1227a0000',
|
|
210
|
+
'0x0000000000000000000000006b175474e89094c44da98b954eedeac495271d0f',
|
|
211
|
+
],
|
|
212
|
+
],
|
|
213
|
+
[
|
|
214
|
+
333, // vaultId
|
|
215
|
+
280, // triggerRatio
|
|
216
|
+
250, // targetRatio
|
|
217
|
+
RatioState.OVER, // ratioState
|
|
218
|
+
true, // isBoost
|
|
219
|
+
]
|
|
220
|
+
],
|
|
221
|
+
];
|
|
222
|
+
|
|
223
|
+
examples.forEach(([expected, actual]) => {
|
|
224
|
+
it(`Given ${actual} should return expected value: ${JSON.stringify(expected)}`, () => {
|
|
225
|
+
expect(makerEncode.leverageManagementWithoutSubProxy(...actual)).to.eql(expected);
|
|
226
|
+
});
|
|
227
|
+
});
|
|
228
|
+
});
|
|
173
229
|
});
|
|
174
230
|
|
|
175
231
|
describe('When testing strategySubService.liquityEncode', () => {
|
|
@@ -1675,4 +1731,128 @@ describe('Feature: strategySubService.ts', () => {
|
|
|
1675
1731
|
});
|
|
1676
1732
|
});
|
|
1677
1733
|
});
|
|
1734
|
+
|
|
1735
|
+
describe('When testing strategySubService.aaveV4Encode', () => {
|
|
1736
|
+
describe('leverageManagement()', () => {
|
|
1737
|
+
const examples: Array<[
|
|
1738
|
+
[StrategyOrBundleIds, boolean, TriggerData, SubData],
|
|
1739
|
+
[
|
|
1740
|
+
strategyOrBundleId: number, owner: EthereumAddress, spoke: EthereumAddress, ratioState: RatioState, targetRatio: number, triggerRatio: number
|
|
1741
|
+
]
|
|
1742
|
+
]> = [
|
|
1743
|
+
[
|
|
1744
|
+
[1,true,["0x0000000000000000000000001031d218133afab8c2b819b1366c7e434ad91e9c0000000000000000000000002f39d218133afab8f2b819b1066c7e434ad94e9e00000000000000000000000000000000000000000000000010a741a4627800000000000000000000000000000000000000000000000000000000000000000001"],["0x0000000000000000000000002f39d218133afab8f2b819b1066c7e434ad94e9e","0x0000000000000000000000001031d218133afab8c2b819b1366c7e434ad91e9c","0x0000000000000000000000000000000000000000000000000000000000000001","0x00000000000000000000000000000000000000000000000014d1120d7b160000"]],
|
|
1745
|
+
[
|
|
1746
|
+
1,
|
|
1747
|
+
web3Utils.toChecksumAddress('0x1031d218133AFaB8c2B819B1366c7E434Ad91E9c'),
|
|
1748
|
+
web3Utils.toChecksumAddress('0x2f39d218133AFaB8F2B819B1066c7E434Ad94E9e'),
|
|
1749
|
+
RatioState.UNDER,
|
|
1750
|
+
150,
|
|
1751
|
+
120,
|
|
1752
|
+
]
|
|
1753
|
+
]
|
|
1754
|
+
];
|
|
1755
|
+
|
|
1756
|
+
examples.forEach(([expected, actual]) => {
|
|
1757
|
+
it(`Given ${JSON.stringify(actual)} should return expected value: ${JSON.stringify(expected)}`, () => {
|
|
1758
|
+
expect(aaveV4Encode.leverageManagement(...actual)).to.eql(expected);
|
|
1759
|
+
});
|
|
1760
|
+
});
|
|
1761
|
+
});
|
|
1762
|
+
|
|
1763
|
+
describe('leverageManagementOnPrice()', () => {
|
|
1764
|
+
const examples: Array<[
|
|
1765
|
+
[StrategyOrBundleIds, boolean, TriggerData, SubData],
|
|
1766
|
+
[
|
|
1767
|
+
strategyOrBundleId: number, owner: EthereumAddress, spoke: EthereumAddress, collAsset: EthereumAddress, collAssetId: number, debtAsset: EthereumAddress, debtAssetId: number, targetRatio: number, price: number, priceState: RatioState, ratioState: RatioState
|
|
1768
|
+
]
|
|
1769
|
+
]> = [
|
|
1770
|
+
[
|
|
1771
|
+
[2,true,["0x0000000000000000000000002f39d218133afab8f2b819b1066c7e434ad94e9e000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000001400000000000000000000000000000000000000000000000000000022ecb25c000000000000000000000000000000000000000000000000000000000000000000"],["0x0000000000000000000000002f39d218133afab8f2b819b1066c7e434ad94e9e","0x0000000000000000000000001031d218133afab8c2b819b1366c7e434ad91e9c","0x000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2","0x000000000000000000000000000000000000000000000000000000000000000a","0x000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48","0x0000000000000000000000000000000000000000000000000000000000000014","0x0000000000000000000000000000000000000000000000000000000000000000","0x00000000000000000000000000000000000000000000000016345785d8a00000"]],
|
|
1772
|
+
[
|
|
1773
|
+
2,
|
|
1774
|
+
web3Utils.toChecksumAddress('0x1031d218133AFaB8c2B819B1366c7E434Ad91E9c'),
|
|
1775
|
+
web3Utils.toChecksumAddress('0x2f39d218133AFaB8F2B819B1066c7E434Ad94E9e'),
|
|
1776
|
+
web3Utils.toChecksumAddress('0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'),
|
|
1777
|
+
10,
|
|
1778
|
+
web3Utils.toChecksumAddress('0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48'),
|
|
1779
|
+
20,
|
|
1780
|
+
160,
|
|
1781
|
+
1500,
|
|
1782
|
+
RatioState.OVER, // priceState - goes to trigger
|
|
1783
|
+
RatioState.OVER, // ratioState - UNDER for repay, OVER for boost
|
|
1784
|
+
]
|
|
1785
|
+
]
|
|
1786
|
+
];
|
|
1787
|
+
|
|
1788
|
+
examples.forEach(([expected, actual]) => {
|
|
1789
|
+
it(`Given ${JSON.stringify(actual)} should return expected value: ${JSON.stringify(expected)}`, () => {
|
|
1790
|
+
expect(aaveV4Encode.leverageManagementOnPrice(...actual)).to.eql(expected);
|
|
1791
|
+
});
|
|
1792
|
+
});
|
|
1793
|
+
});
|
|
1794
|
+
|
|
1795
|
+
describe('closeOnPrice()', () => {
|
|
1796
|
+
const examples: Array<[
|
|
1797
|
+
[StrategyOrBundleIds, boolean, TriggerData, SubData],
|
|
1798
|
+
[
|
|
1799
|
+
strategyOrBundleId: number, owner: EthereumAddress, spoke: EthereumAddress, collAsset: EthereumAddress, collAssetId: number, debtAsset: EthereumAddress, debtAssetId: number, stopLossPrice: number, stopLossType: CloseToAssetType, takeProfitPrice: number, takeProfitType: CloseToAssetType
|
|
1800
|
+
]
|
|
1801
|
+
]> = [
|
|
1802
|
+
[
|
|
1803
|
+
[3,true,["0x0000000000000000000000002f39d218133afab8f2b819b1066c7e434ad94e9e000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000002098a678000000000000000000000000000000000000000000000000000000000000000000"],["0x0000000000000000000000002f39d218133afab8f2b819b1066c7e434ad94e9e","0x0000000000000000000000001031d218133afab8c2b819b1366c7e434ad91e9c","0x000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2","0x000000000000000000000000000000000000000000000000000000000000000a","0x000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48","0x0000000000000000000000000000000000000000000000000000000000000014","0x0000000000000000000000000000000000000000000000000000000000000003"]],
|
|
1804
|
+
[
|
|
1805
|
+
3,
|
|
1806
|
+
web3Utils.toChecksumAddress('0x1031d218133AFaB8c2B819B1366c7E434Ad91E9c'),
|
|
1807
|
+
web3Utils.toChecksumAddress('0x2f39d218133AFaB8F2B819B1066c7E434Ad94E9e'),
|
|
1808
|
+
web3Utils.toChecksumAddress('0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'),
|
|
1809
|
+
10,
|
|
1810
|
+
web3Utils.toChecksumAddress('0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48'),
|
|
1811
|
+
20,
|
|
1812
|
+
1400,
|
|
1813
|
+
CloseToAssetType.DEBT,
|
|
1814
|
+
0,
|
|
1815
|
+
CloseToAssetType.COLLATERAL
|
|
1816
|
+
]
|
|
1817
|
+
]
|
|
1818
|
+
];
|
|
1819
|
+
|
|
1820
|
+
examples.forEach(([expected, actual]) => {
|
|
1821
|
+
it(`Given ${JSON.stringify(actual)} should return expected value: ${JSON.stringify(expected)}`, () => {
|
|
1822
|
+
expect(aaveV4Encode.closeOnPrice(...actual)).to.eql(expected);
|
|
1823
|
+
});
|
|
1824
|
+
});
|
|
1825
|
+
});
|
|
1826
|
+
|
|
1827
|
+
describe('collateralSwitch()', () => {
|
|
1828
|
+
const examples: Array<[
|
|
1829
|
+
[StrategyOrBundleIds, boolean, TriggerData, SubData],
|
|
1830
|
+
[
|
|
1831
|
+
strategyOrBundleId: number, owner: EthereumAddress, spoke: EthereumAddress, fromAsset: EthereumAddress, fromAssetId: number, toAsset: EthereumAddress, toAssetId: number, amountToSwitch: string, price: number, ratioState: RatioState
|
|
1832
|
+
]
|
|
1833
|
+
]> = [
|
|
1834
|
+
[
|
|
1835
|
+
[4,false,["0x0000000000000000000000002f39d218133afab8f2b819b1066c7e434ad94e9e000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000001400000000000000000000000000000000000000000000000000000022ecb25c000000000000000000000000000000000000000000000000000000000000000001"],["0x0000000000000000000000002f39d218133afab8f2b819b1066c7e434ad94e9e","0x0000000000000000000000001031d218133afab8c2b819b1366c7e434ad91e9c","0x000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2","0x000000000000000000000000000000000000000000000000000000000000000a","0x000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48","0x0000000000000000000000000000000000000000000000000000000000000014","0x0000000000000000000000000000000000000000000000000de0b6b3a7640000"]],
|
|
1836
|
+
[
|
|
1837
|
+
4,
|
|
1838
|
+
web3Utils.toChecksumAddress('0x1031d218133AFaB8c2B819B1366c7E434Ad91E9c'),
|
|
1839
|
+
web3Utils.toChecksumAddress('0x2f39d218133AFaB8F2B819B1066c7E434Ad94E9e'),
|
|
1840
|
+
web3Utils.toChecksumAddress('0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'),
|
|
1841
|
+
10,
|
|
1842
|
+
web3Utils.toChecksumAddress('0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48'),
|
|
1843
|
+
20,
|
|
1844
|
+
'1000000000000000000',
|
|
1845
|
+
1500,
|
|
1846
|
+
RatioState.UNDER,
|
|
1847
|
+
]
|
|
1848
|
+
]
|
|
1849
|
+
];
|
|
1850
|
+
|
|
1851
|
+
examples.forEach(([expected, actual]) => {
|
|
1852
|
+
it(`Given ${JSON.stringify(actual)} should return expected value: ${JSON.stringify(expected)}`, () => {
|
|
1853
|
+
expect(aaveV4Encode.collateralSwitch(...actual)).to.eql(expected);
|
|
1854
|
+
});
|
|
1855
|
+
});
|
|
1856
|
+
});
|
|
1857
|
+
});
|
|
1678
1858
|
});
|
|
@@ -3,8 +3,11 @@ import { getAssetInfo } from '@defisaver/tokens';
|
|
|
3
3
|
|
|
4
4
|
import type { OrderType } from '../types/enums';
|
|
5
5
|
import {
|
|
6
|
-
|
|
7
|
-
Bundles,
|
|
6
|
+
CloseToAssetType,
|
|
7
|
+
Bundles,
|
|
8
|
+
ChainId,
|
|
9
|
+
RatioState,
|
|
10
|
+
Strategies,
|
|
8
11
|
} from '../types/enums';
|
|
9
12
|
import type { EthereumAddress, StrategyOrBundleIds } from '../types';
|
|
10
13
|
|
|
@@ -95,6 +98,27 @@ export const makerEncode = {
|
|
|
95
98
|
boostEnabled,
|
|
96
99
|
];
|
|
97
100
|
},
|
|
101
|
+
leverageManagementWithoutSubProxy(
|
|
102
|
+
vaultId: number,
|
|
103
|
+
triggerRatio: number,
|
|
104
|
+
targetRatio: number,
|
|
105
|
+
ratioState: RatioState,
|
|
106
|
+
isBoost: boolean,
|
|
107
|
+
daiAddr?: EthereumAddress,
|
|
108
|
+
) {
|
|
109
|
+
const bundleId = isBoost ? Bundles.MainnetIds.MAKER_BOOST : Bundles.MainnetIds.MAKER_REPAY;
|
|
110
|
+
|
|
111
|
+
const triggerData = triggerService.makerRatioTrigger.encode(vaultId, triggerRatio, ratioState);
|
|
112
|
+
|
|
113
|
+
const subData = subDataService.makerLeverageManagementWithoutSubProxy.encode(vaultId, targetRatio, daiAddr);
|
|
114
|
+
|
|
115
|
+
return [
|
|
116
|
+
bundleId,
|
|
117
|
+
true,
|
|
118
|
+
triggerData,
|
|
119
|
+
subData,
|
|
120
|
+
];
|
|
121
|
+
},
|
|
98
122
|
};
|
|
99
123
|
|
|
100
124
|
export const liquityEncode = {
|
|
@@ -578,6 +602,24 @@ export const sparkEncode = {
|
|
|
578
602
|
|
|
579
603
|
return [strategyOrBundleId, isBundle, triggerDataEncoded, subDataEncoded];
|
|
580
604
|
},
|
|
605
|
+
leverageManagementWithoutSubProxy(
|
|
606
|
+
strategyOrBundleId: number,
|
|
607
|
+
market: EthereumAddress,
|
|
608
|
+
user: EthereumAddress,
|
|
609
|
+
ratioState: RatioState,
|
|
610
|
+
targetRatio: number,
|
|
611
|
+
triggerRatio: number,
|
|
612
|
+
) {
|
|
613
|
+
const isBundle = true;
|
|
614
|
+
|
|
615
|
+
const subData = subDataService.sparkLeverageManagementSubDataWithoutSubProxy.encode(
|
|
616
|
+
targetRatio,
|
|
617
|
+
ratioState,
|
|
618
|
+
);
|
|
619
|
+
const triggerData = triggerService.sparkRatioTrigger.encode(user, market, triggerRatio, ratioState);
|
|
620
|
+
|
|
621
|
+
return [strategyOrBundleId, isBundle, triggerData, subData];
|
|
622
|
+
},
|
|
581
623
|
};
|
|
582
624
|
|
|
583
625
|
export const crvUSDEncode = {
|
|
@@ -782,3 +824,121 @@ export const fluidEncode = {
|
|
|
782
824
|
return [strategyOrBundleId, isBundle, triggerData, subData];
|
|
783
825
|
},
|
|
784
826
|
};
|
|
827
|
+
|
|
828
|
+
export const aaveV4Encode = {
|
|
829
|
+
leverageManagement(
|
|
830
|
+
strategyOrBundleId: number,
|
|
831
|
+
owner: EthereumAddress,
|
|
832
|
+
spoke: EthereumAddress,
|
|
833
|
+
ratioState: RatioState,
|
|
834
|
+
targetRatio: number,
|
|
835
|
+
triggerRatio: number,
|
|
836
|
+
) {
|
|
837
|
+
const isBundle = true;
|
|
838
|
+
const subData = subDataService.aaveV4LeverageManagementSubData.encode(spoke, owner, ratioState, targetRatio);
|
|
839
|
+
const triggerData = triggerService.aaveV4RatioTrigger.encode(owner, spoke, triggerRatio, ratioState);
|
|
840
|
+
|
|
841
|
+
return [strategyOrBundleId, isBundle, triggerData, subData];
|
|
842
|
+
},
|
|
843
|
+
leverageManagementOnPrice(
|
|
844
|
+
strategyOrBundleId: number,
|
|
845
|
+
owner: EthereumAddress,
|
|
846
|
+
spoke: EthereumAddress,
|
|
847
|
+
collAsset: EthereumAddress,
|
|
848
|
+
collAssetId: number,
|
|
849
|
+
debtAsset: EthereumAddress,
|
|
850
|
+
debtAssetId: number,
|
|
851
|
+
targetRatio: number,
|
|
852
|
+
price: number,
|
|
853
|
+
priceState: RatioState,
|
|
854
|
+
ratioState: RatioState, // UNDER for repay, OVER for boost
|
|
855
|
+
) {
|
|
856
|
+
const isBundle = true;
|
|
857
|
+
const subData = subDataService.aaveV4LeverageManagementOnPriceSubData.encode(
|
|
858
|
+
spoke,
|
|
859
|
+
owner,
|
|
860
|
+
collAsset,
|
|
861
|
+
collAssetId,
|
|
862
|
+
debtAsset,
|
|
863
|
+
debtAssetId,
|
|
864
|
+
ratioState,
|
|
865
|
+
targetRatio,
|
|
866
|
+
);
|
|
867
|
+
const triggerData = triggerService.aaveV4QuotePriceTrigger.encode(
|
|
868
|
+
spoke,
|
|
869
|
+
collAssetId,
|
|
870
|
+
debtAssetId,
|
|
871
|
+
price,
|
|
872
|
+
priceState,
|
|
873
|
+
);
|
|
874
|
+
|
|
875
|
+
return [strategyOrBundleId, isBundle, triggerData, subData];
|
|
876
|
+
},
|
|
877
|
+
closeOnPrice(
|
|
878
|
+
strategyOrBundleId: number,
|
|
879
|
+
owner: EthereumAddress,
|
|
880
|
+
spoke: EthereumAddress,
|
|
881
|
+
collAsset: EthereumAddress,
|
|
882
|
+
collAssetId: number,
|
|
883
|
+
debtAsset: EthereumAddress,
|
|
884
|
+
debtAssetId: number,
|
|
885
|
+
stopLossPrice: number = 0,
|
|
886
|
+
stopLossType: CloseToAssetType = CloseToAssetType.DEBT,
|
|
887
|
+
takeProfitPrice: number = 0,
|
|
888
|
+
takeProfitType: CloseToAssetType = CloseToAssetType.COLLATERAL,
|
|
889
|
+
) {
|
|
890
|
+
const isBundle = true;
|
|
891
|
+
const closeType = getCloseStrategyType(stopLossPrice, stopLossType, takeProfitPrice, takeProfitType);
|
|
892
|
+
|
|
893
|
+
const subData = subDataService.aaveV4CloseSubData.encode(
|
|
894
|
+
spoke,
|
|
895
|
+
owner,
|
|
896
|
+
collAsset,
|
|
897
|
+
collAssetId,
|
|
898
|
+
debtAsset,
|
|
899
|
+
debtAssetId,
|
|
900
|
+
closeType,
|
|
901
|
+
);
|
|
902
|
+
const triggerData = triggerService.aaveV4QuotePriceRangeTrigger.encode(
|
|
903
|
+
spoke,
|
|
904
|
+
collAssetId,
|
|
905
|
+
debtAssetId,
|
|
906
|
+
stopLossPrice,
|
|
907
|
+
takeProfitPrice,
|
|
908
|
+
);
|
|
909
|
+
|
|
910
|
+
return [strategyOrBundleId, isBundle, triggerData, subData];
|
|
911
|
+
},
|
|
912
|
+
collateralSwitch(
|
|
913
|
+
strategyOrBundleId: number,
|
|
914
|
+
owner: EthereumAddress,
|
|
915
|
+
spoke: EthereumAddress,
|
|
916
|
+
fromAsset: EthereumAddress,
|
|
917
|
+
fromAssetId: number,
|
|
918
|
+
toAsset: EthereumAddress,
|
|
919
|
+
toAssetId: number,
|
|
920
|
+
amountToSwitch: string,
|
|
921
|
+
price: number,
|
|
922
|
+
ratioState: RatioState,
|
|
923
|
+
) {
|
|
924
|
+
const isBundle = false;
|
|
925
|
+
const subData = subDataService.aaveV4CollateralSwitchSubData.encode(
|
|
926
|
+
spoke,
|
|
927
|
+
owner,
|
|
928
|
+
fromAsset,
|
|
929
|
+
fromAssetId,
|
|
930
|
+
toAsset,
|
|
931
|
+
toAssetId,
|
|
932
|
+
amountToSwitch,
|
|
933
|
+
);
|
|
934
|
+
const triggerData = triggerService.aaveV4QuotePriceTrigger.encode(
|
|
935
|
+
spoke,
|
|
936
|
+
fromAssetId, // baseTokenId
|
|
937
|
+
toAssetId, // quoteTokenId
|
|
938
|
+
price,
|
|
939
|
+
ratioState,
|
|
940
|
+
);
|
|
941
|
+
|
|
942
|
+
return [strategyOrBundleId, isBundle, triggerData, subData];
|
|
943
|
+
},
|
|
944
|
+
};
|