@defisaver/automation-sdk 3.3.16-liq-prot-dev → 3.3.17-liq-prot-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.
@@ -1457,6 +1457,93 @@ describe('Feature: strategySubService.ts', () => {
1457
1457
  });
1458
1458
  });
1459
1459
 
1460
+ describe('leverageManagementOnPriceGeneric()', () => {
1461
+ const examples: Array<[
1462
+ [StrategyOrBundleIds, boolean, TriggerData, SubData],
1463
+ [
1464
+ loanToken: EthereumAddress,
1465
+ collToken: EthereumAddress,
1466
+ oracle: EthereumAddress,
1467
+ irm: EthereumAddress,
1468
+ lltv: string,
1469
+ user: EthereumAddress,
1470
+ targetRatio: number,
1471
+ price: number,
1472
+ priceState: RatioState,
1473
+ isBoost: boolean,
1474
+ isEOA: boolean,
1475
+ network: ChainId,
1476
+ ]
1477
+ ]> = [
1478
+ [
1479
+ [
1480
+ Bundles.MainnetIds.MORPHO_BLUE_REPAY_ON_PRICE,
1481
+ true,
1482
+ ['0x000000000000000000000000870ac11d48b15db9a138cf899d20f13f79ba00bc0000000000000000000000007f39c581f595b53c5cb19bd0b3f8da6c935e2ca0000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000000000000000000000000000000000003a352944000000000000000000000000000000000000000000000000000000000000000001'],
1483
+ [
1484
+ '0x000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
1485
+ '0x0000000000000000000000007f39c581f595b53c5cb19bd0b3f8da6c935e2ca0',
1486
+ '0x000000000000000000000000870ac11d48b15db9a138cf899d20f13f79ba00bc',
1487
+ '0x0000000000000000000000000000000000000000000000000000000000000000',
1488
+ '0x0000000000000000000000000000000000000000000000000d1d507e40be8000',
1489
+ '0x0000000000000000000000000000000000000000000000001bc16d674ec80000',
1490
+ '0x0000000000000000000000001031d218133afab8c2b819b1366c7e434ad91e9c',
1491
+ ],
1492
+ ],
1493
+ [
1494
+ web3Utils.toChecksumAddress('0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2'),
1495
+ web3Utils.toChecksumAddress('0x7f39C581F595B53c5cb19bD0b3f8dA6c935E2Ca0'),
1496
+ web3Utils.toChecksumAddress('0x870aC11D48B15DB9a138Cf899d20F13F79Ba00BC'),
1497
+ web3Utils.toChecksumAddress('0x0000000000000000000000000000000000000000'),
1498
+ '945000000000000000',
1499
+ web3Utils.toChecksumAddress('0x1031d218133AFaB8c2B819B1366c7E434Ad91E9c'),
1500
+ 200,
1501
+ 2500,
1502
+ RatioState.UNDER,
1503
+ false,
1504
+ false,
1505
+ ChainId.Ethereum,
1506
+ ],
1507
+ ],
1508
+ [
1509
+ [
1510
+ Bundles.MainnetIds.MORPHO_BLUE_BOOST_ON_PRICE,
1511
+ true,
1512
+ ['0x000000000000000000000000870ac11d48b15db9a138cf899d20f13f79ba00bc0000000000000000000000007f39c581f595b53c5cb19bd0b3f8da6c935e2ca0000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000000000000000000000000000000000005d21dba0000000000000000000000000000000000000000000000000000000000000000000'],
1513
+ [
1514
+ '0x000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
1515
+ '0x0000000000000000000000007f39c581f595b53c5cb19bd0b3f8da6c935e2ca0',
1516
+ '0x000000000000000000000000870ac11d48b15db9a138cf899d20f13f79ba00bc',
1517
+ '0x0000000000000000000000000000000000000000000000000000000000000000',
1518
+ '0x0000000000000000000000000000000000000000000000000d1d507e40be8000',
1519
+ '0x00000000000000000000000000000000000000000000000016345785d8a00000',
1520
+ '0x0000000000000000000000001031d218133afab8c2b819b1366c7e434ad91e9c',
1521
+ ],
1522
+ ],
1523
+ [
1524
+ web3Utils.toChecksumAddress('0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2'),
1525
+ web3Utils.toChecksumAddress('0x7f39C581F595B53c5cb19bD0b3f8dA6c935E2Ca0'),
1526
+ web3Utils.toChecksumAddress('0x870aC11D48B15DB9a138Cf899d20F13F79Ba00BC'),
1527
+ web3Utils.toChecksumAddress('0x0000000000000000000000000000000000000000'),
1528
+ '945000000000000000',
1529
+ web3Utils.toChecksumAddress('0x1031d218133AFaB8c2B819B1366c7E434Ad91E9c'),
1530
+ 160,
1531
+ 4000,
1532
+ RatioState.OVER,
1533
+ true,
1534
+ false,
1535
+ ChainId.Ethereum,
1536
+ ],
1537
+ ],
1538
+ ];
1539
+
1540
+ examples.forEach(([expected, actual]) => {
1541
+ it(`Given ${JSON.stringify(actual)} should return expected value: ${JSON.stringify(expected)}`, () => {
1542
+ expect(morphoBlueEncode.leverageManagementOnPriceGeneric(...actual)).to.eql(expected);
1543
+ });
1544
+ });
1545
+ });
1546
+
1460
1547
  describe('closeOnPrice()', () => {
1461
1548
  const examples: Array<[
1462
1549
  [StrategyOrBundleIds, boolean, TriggerData, SubData],
@@ -1500,7 +1587,7 @@ describe('Feature: strategySubService.ts', () => {
1500
1587
  1500,
1501
1588
  CloseToAssetType.DEBT,
1502
1589
  4000,
1503
- CloseToAssetType.COLLATERAL
1590
+ CloseToAssetType.COLLATERAL,
1504
1591
  ]
1505
1592
  ],
1506
1593
  ];
@@ -1511,6 +1598,63 @@ describe('Feature: strategySubService.ts', () => {
1511
1598
  });
1512
1599
  });
1513
1600
  });
1601
+
1602
+ describe('closeOnPriceGeneric()', () => {
1603
+ const examples: Array<[
1604
+ [StrategyOrBundleIds, boolean, TriggerData, SubData],
1605
+ [
1606
+ loanToken: EthereumAddress,
1607
+ collToken: EthereumAddress,
1608
+ oracle: EthereumAddress,
1609
+ irm: EthereumAddress,
1610
+ lltv: string,
1611
+ user: EthereumAddress,
1612
+ stopLossPrice: number,
1613
+ stopLossType: CloseToAssetType,
1614
+ takeProfitPrice: number,
1615
+ takeProfitType: CloseToAssetType,
1616
+ isEOA: boolean,
1617
+ network: ChainId,
1618
+ ]
1619
+ ]> = [
1620
+ [
1621
+ [
1622
+ Bundles.MainnetIds.MORPHO_BLUE_CLOSE,
1623
+ true,
1624
+ ['0x000000000000000000000000870ac11d48b15db9a138cf899d20f13f79ba00bc0000000000000000000000007f39c581f595b53c5cb19bd0b3f8da6c935e2ca0000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc200000000000000000000000000000000000000000000000000000022ecb25c000000000000000000000000000000000000000000000000000000005d21dba000'],
1625
+ [
1626
+ '0x000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
1627
+ '0x0000000000000000000000007f39c581f595b53c5cb19bd0b3f8da6c935e2ca0',
1628
+ '0x000000000000000000000000870ac11d48b15db9a138cf899d20f13f79ba00bc',
1629
+ '0x0000000000000000000000000000000000000000000000000000000000000000',
1630
+ '0x0000000000000000000000000000000000000000000000000d1d507e40be8000',
1631
+ '0x0000000000000000000000001031d218133afab8c2b819b1366c7e434ad91e9c',
1632
+ '0x0000000000000000000000000000000000000000000000000000000000000005',
1633
+ ],
1634
+ ],
1635
+ [
1636
+ web3Utils.toChecksumAddress('0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2'),
1637
+ web3Utils.toChecksumAddress('0x7f39C581F595B53c5cb19bD0b3f8dA6c935E2Ca0'),
1638
+ web3Utils.toChecksumAddress('0x870aC11D48B15DB9a138Cf899d20F13F79Ba00BC'),
1639
+ web3Utils.toChecksumAddress('0x0000000000000000000000000000000000000000'),
1640
+ '945000000000000000',
1641
+ web3Utils.toChecksumAddress('0x1031d218133AFaB8c2B819B1366c7E434Ad91E9c'),
1642
+ 1500,
1643
+ CloseToAssetType.DEBT,
1644
+ 4000,
1645
+ CloseToAssetType.COLLATERAL,
1646
+ false,
1647
+ ChainId.Ethereum,
1648
+ ]
1649
+ ],
1650
+ ];
1651
+
1652
+ examples.forEach(([expected, actual]) => {
1653
+ it(`Given ${JSON.stringify(actual)} should return expected value: ${JSON.stringify(expected)}`, () => {
1654
+ expect(morphoBlueEncode.closeOnPriceGeneric(...actual)).to.eql(expected);
1655
+ });
1656
+ });
1657
+ });
1514
1658
  });
1515
1659
 
1516
1660
  describe('When testing strategySubService.sparkEncode', () => {
@@ -17,7 +17,6 @@ import * as subDataService from './subDataService';
17
17
  import * as triggerService from './triggerService';
18
18
  import {
19
19
  compareAddresses,
20
- getBundleIdsByNetwork,
21
20
  getCloseStrategyType,
22
21
  requireAddress,
23
22
  requireAddresses,
@@ -718,6 +717,46 @@ export const crvUSDEncode = {
718
717
  },
719
718
  };
720
719
 
720
+ export type MorphoBlueBundleStrategy = 'repay' | 'boost' | 'repayOnPrice' | 'boostOnPrice' | 'close' | 'liquidationProtection';
721
+
722
+ function getMorphoBlueBundlesIds(network: ChainId) {
723
+ switch (network) {
724
+ case ChainId.Ethereum:
725
+ return Bundles.MainnetIds;
726
+ case ChainId.Base:
727
+ return Bundles.BaseIds;
728
+ case ChainId.Arbitrum:
729
+ return Bundles.ArbitrumIds;
730
+ default:
731
+ throw new Error(`Morpho Blue strategies are not supported on chain ${network}`);
732
+ }
733
+ }
734
+
735
+ export function getMorphoBlueBundleId(
736
+ network: ChainId,
737
+ strategy: MorphoBlueBundleStrategy,
738
+ isEOA: boolean,
739
+ ): number {
740
+ const bundlesIds = getMorphoBlueBundlesIds(network);
741
+
742
+ switch (strategy) {
743
+ case 'repay':
744
+ return isEOA ? bundlesIds.MORPHO_BLUE_EOA_REPAY : bundlesIds.MORPHO_BLUE_REPAY;
745
+ case 'boost':
746
+ return isEOA ? bundlesIds.MORPHO_BLUE_EOA_BOOST : bundlesIds.MORPHO_BLUE_BOOST;
747
+ case 'repayOnPrice':
748
+ return isEOA ? bundlesIds.MORPHO_BLUE_EOA_REPAY_ON_PRICE : bundlesIds.MORPHO_BLUE_REPAY_ON_PRICE;
749
+ case 'boostOnPrice':
750
+ return isEOA ? bundlesIds.MORPHO_BLUE_EOA_BOOST_ON_PRICE : bundlesIds.MORPHO_BLUE_BOOST_ON_PRICE;
751
+ case 'close':
752
+ return isEOA ? bundlesIds.MORPHO_BLUE_EOA_CLOSE : bundlesIds.MORPHO_BLUE_CLOSE;
753
+ case 'liquidationProtection':
754
+ return isEOA ? bundlesIds.MORPHO_BLUE_EOA_LIQUIDATION_PROTECTION : bundlesIds.MORPHO_BLUE_SW_LIQUIDATION_PROTECTION;
755
+ default:
756
+ throw new Error(`Unknown Morpho Blue strategy: ${strategy}`);
757
+ }
758
+ }
759
+
721
760
  export const morphoBlueEncode = {
722
761
  leverageManagement(
723
762
  marketId: string,
@@ -739,19 +778,10 @@ export const morphoBlueEncode = {
739
778
 
740
779
  // over is boost, under is repay
741
780
  const isBoost = ratioState === RatioState.OVER;
742
- let strategyOrBundleId;
743
-
744
- if (network === ChainId.Base) {
745
- return [isBoost ? Bundles.BaseIds.MORPHO_BLUE_BOOST : Bundles.BaseIds.MORPHO_BLUE_REPAY, true, triggerData, subData];
746
- }
747
-
748
- const bundlesIds = network === ChainId.Arbitrum ? Bundles.ArbitrumIds : Bundles.MainnetIds;
749
-
750
- if (isBoost) strategyOrBundleId = isEOA ? bundlesIds.MORPHO_BLUE_EOA_BOOST : bundlesIds.MORPHO_BLUE_BOOST;
751
- else strategyOrBundleId = isEOA ? bundlesIds.MORPHO_BLUE_EOA_REPAY : bundlesIds.MORPHO_BLUE_REPAY;
781
+ const bundleId = getMorphoBlueBundleId(network, isBoost ? 'boost' : 'repay', isEOA);
752
782
  const isBundle = true;
753
783
 
754
- return [strategyOrBundleId, isBundle, triggerData, subData];
784
+ return [bundleId, isBundle, triggerData, subData];
755
785
  },
756
786
 
757
787
  liquidationProtection(
@@ -772,11 +802,7 @@ export const morphoBlueEncode = {
772
802
 
773
803
  const triggerData = triggerService.morphoBlueRatioTrigger.encode(marketId, user, triggerRatio, ratioState);
774
804
 
775
-
776
- // Type casting because there is no MORPHO_BLUE_EOA_LIQUIDATION_PROTECTION for Base chain.
777
- // That is fine because we will just always send isEOA == false for Base chain.
778
- const bundleNetwork = getBundleIdsByNetwork(network) as typeof Bundles.MainnetIds;
779
- const bundleId = isEOA ? bundleNetwork.MORPHO_BLUE_EOA_LIQUIDATION_PROTECTION : bundleNetwork.MORPHO_BLUE_SW_LIQUIDATION_PROTECTION;
805
+ const bundleId = getMorphoBlueBundleId(network, 'liquidationProtection', isEOA);
780
806
  const isBundle = true;
781
807
 
782
808
  return [bundleId, isBundle, triggerData, subData];
@@ -799,6 +825,28 @@ export const morphoBlueEncode = {
799
825
  const triggerData = triggerService.morphoBluePriceTrigger.encode(oracle, collToken, loanToken, price, priceState);
800
826
  return [strategyOrBundleId, isBundle, triggerData, subData];
801
827
  },
828
+ leverageManagementOnPriceGeneric(
829
+ loanToken: EthereumAddress,
830
+ collToken: EthereumAddress,
831
+ oracle: EthereumAddress,
832
+ irm: EthereumAddress,
833
+ lltv: string,
834
+ user: EthereumAddress,
835
+ targetRatio: number,
836
+ price: number,
837
+ priceState: RatioState,
838
+ isBoost: boolean,
839
+ isEOA: boolean,
840
+ network: ChainId,
841
+ ) {
842
+ const subData = subDataService.morphoBlueLeverageManagementOnPriceSubData.encode(loanToken, collToken, oracle, irm, lltv, targetRatio, user);
843
+ const triggerData = triggerService.morphoBluePriceTrigger.encode(oracle, collToken, loanToken, price, priceState);
844
+
845
+ const bundleId = getMorphoBlueBundleId(network, isBoost ? 'boostOnPrice' : 'repayOnPrice', isEOA);
846
+ const isBundle = true;
847
+
848
+ return [bundleId, isBundle, triggerData, subData];
849
+ },
802
850
  closeOnPrice(
803
851
  strategyOrBundleId: number,
804
852
  loanToken: EthereumAddress,
@@ -820,6 +868,30 @@ export const morphoBlueEncode = {
820
868
 
821
869
  return [strategyOrBundleId, isBundle, triggerDataEncoded, subDataEncoded];
822
870
  },
871
+ closeOnPriceGeneric(
872
+ loanToken: EthereumAddress,
873
+ collToken: EthereumAddress,
874
+ oracle: EthereumAddress,
875
+ irm: EthereumAddress,
876
+ lltv: string,
877
+ user: EthereumAddress,
878
+ stopLossPrice: number = 0,
879
+ stopLossType: CloseToAssetType = CloseToAssetType.DEBT,
880
+ takeProfitPrice: number = 0,
881
+ takeProfitType: CloseToAssetType = CloseToAssetType.COLLATERAL,
882
+ isEOA: boolean,
883
+ network: ChainId,
884
+ ) {
885
+ const isBundle = true;
886
+ const closeType = getCloseStrategyType(stopLossPrice, stopLossType, takeProfitPrice, takeProfitType);
887
+
888
+ const subDataEncoded = subDataService.morphoBlueCloseOnPriceSubData.encode(loanToken, collToken, oracle, irm, lltv, user, closeType);
889
+ const triggerDataEncoded = triggerService.morphoBluePriceRangeTrigger.encode(oracle, collToken, loanToken, stopLossPrice, takeProfitPrice);
890
+
891
+ const bundleId = getMorphoBlueBundleId(network, 'close', isEOA);
892
+
893
+ return [bundleId, isBundle, triggerDataEncoded, subDataEncoded];
894
+ },
823
895
  };
824
896
 
825
897
  export const liquityV2Encode = {
@@ -174,15 +174,3 @@ export function getStopLossAndTakeProfitTypeByCloseStrategyType(
174
174
  }
175
175
  }
176
176
 
177
- export function getBundleIdsByNetwork(network: ChainId) {
178
- switch (Number(network)) {
179
- case ChainId.Ethereum:
180
- return Bundles.MainnetIds;
181
- case ChainId.Arbitrum:
182
- return Bundles.ArbitrumIds;
183
- case ChainId.Base:
184
- return Bundles.BaseIds;
185
- default:
186
- throw new Error(`Unsupported network ${network}`);
187
- }
188
- }
@@ -256,6 +256,9 @@ export namespace Bundles {
256
256
  COMP_V3_SW_LIQUIDATION_PROTECTION = 82,
257
257
  COMP_V3_EOA_LIQUIDATION_PROTECTION = 83,
258
258
  MORPHO_BLUE_REPAY_ON_PRICE = 84,
259
+ MORPHO_BLUE_EOA_BOOST_ON_PRICE = 85,
260
+ MORPHO_BLUE_EOA_REPAY_ON_PRICE = 86,
261
+ MORPHO_BLUE_EOA_CLOSE = 87,
259
262
  }
260
263
 
261
264
  export enum OptimismIds {
@@ -309,6 +312,12 @@ export namespace Bundles {
309
312
  AAVE_V3_EOA_LIQUIDATION_PROTECTION = 34,
310
313
  MORPHO_BLUE_SW_LIQUIDATION_PROTECTION = 35,
311
314
  MORPHO_BLUE_REPAY_ON_PRICE = 36,
315
+ MORPHO_BLUE_EOA_REPAY = 37,
316
+ MORPHO_BLUE_EOA_BOOST = 38,
317
+ MORPHO_BLUE_EOA_BOOST_ON_PRICE = 39,
318
+ MORPHO_BLUE_EOA_REPAY_ON_PRICE = 40,
319
+ MORPHO_BLUE_EOA_CLOSE = 41,
320
+ MORPHO_BLUE_EOA_LIQUIDATION_PROTECTION = 42,
312
321
  }
313
322
 
314
323
  export enum ArbitrumIds {
@@ -349,5 +358,8 @@ export namespace Bundles {
349
358
  AAVE_V3_EOA_LIQUIDATION_PROTECTION = 34,
350
359
  MORPHO_BLUE_EOA_LIQUIDATION_PROTECTION = 35,
351
360
  MORPHO_BLUE_REPAY_ON_PRICE = 36,
361
+ MORPHO_BLUE_EOA_BOOST_ON_PRICE = 37,
362
+ MORPHO_BLUE_EOA_REPAY_ON_PRICE = 38,
363
+ MORPHO_BLUE_EOA_CLOSE = 39,
352
364
  }
353
365
  }