@defisaver/automation-sdk 3.3.3 → 3.3.5-dev.0

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.
@@ -2,6 +2,7 @@ import Dec from 'decimal.js';
2
2
  import { expect } from 'chai';
3
3
  import { getAssetInfo } from '@defisaver/tokens';
4
4
  import * as web3Utils from 'web3-utils';
5
+ import { MAXUINT } from '@defisaver/tokens';
5
6
 
6
7
  import { ChainId, CloseStrategyType, CloseToAssetType, OrderType, RatioState } from '../types/enums';
7
8
  import type { EthereumAddress, SubData } from '../types';
@@ -364,6 +365,151 @@ describe('Feature: subDataService.ts', () => {
364
365
  });
365
366
  });
366
367
 
368
+ describe('When testing subDataService.aaveV3CollateralSwitchSubData', () => {
369
+ describe('encode()', () => {
370
+ const examples: Array<[SubData, [fromAsset: EthereumAddress, fromAssetId: number, toAsset: EthereumAddress, toAssetId: number, marketAddr: EthereumAddress, amountToSwitch: string, useOnBehalf?: boolean]]> = [
371
+ // WETH -> USDC
372
+ [
373
+ [
374
+ '0x000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
375
+ '0x0000000000000000000000000000000000000000000000000000000000000000',
376
+ '0x000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48',
377
+ '0x0000000000000000000000000000000000000000000000000000000000000001',
378
+ '0x00000000000000000000000087870bca3f3fd6335c3f4ce8392d69d0b4161d39',
379
+ '0x0000000000000000000000000000000000000000000000008ac7230489e80000',
380
+ '0x0000000000000000000000000000000000000000000000000000000000000000',
381
+ ],
382
+ [
383
+ web3Utils.toChecksumAddress(getAssetInfo('WETH', ChainId.Ethereum).address),
384
+ 0,
385
+ web3Utils.toChecksumAddress(getAssetInfo('USDC', ChainId.Ethereum).address),
386
+ 1,
387
+ web3Utils.toChecksumAddress('0x87870Bca3F3fD6335C3F4ce8392D69d0B4161d39'),
388
+ '10000000000000000000', // 10 WETH
389
+ false,
390
+ ]
391
+ ],
392
+ // USDC -> WETH (MaxUint256)
393
+ [
394
+ [
395
+ '0x000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48',
396
+ '0x0000000000000000000000000000000000000000000000000000000000000001',
397
+ '0x000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
398
+ '0x0000000000000000000000000000000000000000000000000000000000000000',
399
+ '0x00000000000000000000000087870bca3f3fd6335c3f4ce8392d69d0b4161d39',
400
+ '0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff',
401
+ '0x0000000000000000000000000000000000000000000000000000000000000000',
402
+ ],
403
+ [
404
+ web3Utils.toChecksumAddress(getAssetInfo('USDC', ChainId.Ethereum).address),
405
+ 1,
406
+ web3Utils.toChecksumAddress(getAssetInfo('WETH', ChainId.Ethereum).address),
407
+ 0,
408
+ web3Utils.toChecksumAddress('0x87870Bca3F3fD6335C3F4ce8392D69d0B4161d39'),
409
+ MAXUINT, // MaxUint256
410
+ ]
411
+ ],
412
+ // WETH -> WBTC
413
+ [
414
+ [
415
+ '0x000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
416
+ '0x0000000000000000000000000000000000000000000000000000000000000000',
417
+ '0x0000000000000000000000002260fac5e5542a773aa44fbcfedf7c193bc2c599',
418
+ '0x0000000000000000000000000000000000000000000000000000000000000002',
419
+ '0x00000000000000000000000087870bca3f3fd6335c3f4ce8392d69d0b4161d39',
420
+ '0x0000000000000000000000000000000000000000000000004563918244f40000',
421
+ '0x0000000000000000000000000000000000000000000000000000000000000000',
422
+ ],
423
+ [
424
+ web3Utils.toChecksumAddress(getAssetInfo('WETH', ChainId.Ethereum).address),
425
+ 0,
426
+ web3Utils.toChecksumAddress(getAssetInfo('WBTC', ChainId.Ethereum).address),
427
+ 2,
428
+ web3Utils.toChecksumAddress('0x87870Bca3F3fD6335C3F4ce8392D69d0B4161d39'),
429
+ '5000000000000000000', // 5 WETH
430
+ ]
431
+ ],
432
+ ];
433
+
434
+ examples.forEach(([expected, actual]) => {
435
+ it(`Given ${JSON.stringify(actual)} should return expected value: ${JSON.stringify(expected)}`, () => {
436
+ expect(subDataService.aaveV3CollateralSwitchSubData.encode(...actual)).to.eql(expected);
437
+ });
438
+ });
439
+ });
440
+
441
+ describe('decode()', () => {
442
+ const examples: Array<[{ fromAsset: EthereumAddress, fromAssetId: number, toAsset: EthereumAddress, toAssetId: number, marketAddr: EthereumAddress, amountToSwitch: string }, SubData]> = [
443
+ // WETH -> USDC
444
+ [
445
+ {
446
+ fromAsset: web3Utils.toChecksumAddress(getAssetInfo('WETH', ChainId.Ethereum).address),
447
+ fromAssetId: 0,
448
+ toAsset: web3Utils.toChecksumAddress(getAssetInfo('USDC', ChainId.Ethereum).address),
449
+ toAssetId: 1,
450
+ marketAddr: web3Utils.toChecksumAddress('0x87870Bca3F3fD6335C3F4ce8392D69d0B4161d39'),
451
+ amountToSwitch: '10000000000000000000',
452
+ },
453
+ [
454
+ '0x000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
455
+ '0x0000000000000000000000000000000000000000000000000000000000000000',
456
+ '0x000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48',
457
+ '0x0000000000000000000000000000000000000000000000000000000000000001',
458
+ '0x00000000000000000000000087870bca3f3fd6335c3f4ce8392d69d0b4161d39',
459
+ '0x0000000000000000000000000000000000000000000000008ac7230489e80000',
460
+ '0x0000000000000000000000000000000000000000000000000000000000000000',
461
+ ],
462
+ ],
463
+ // USDC -> WETH (MaxUint256)
464
+ [
465
+ {
466
+ fromAsset: web3Utils.toChecksumAddress(getAssetInfo('USDC', ChainId.Ethereum).address),
467
+ fromAssetId: 1,
468
+ toAsset: web3Utils.toChecksumAddress(getAssetInfo('WETH', ChainId.Ethereum).address),
469
+ toAssetId: 0,
470
+ marketAddr: web3Utils.toChecksumAddress('0x87870Bca3F3fD6335C3F4ce8392D69d0B4161d39'),
471
+ amountToSwitch: MAXUINT,
472
+ },
473
+ [
474
+ '0x000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48',
475
+ '0x0000000000000000000000000000000000000000000000000000000000000001',
476
+ '0x000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
477
+ '0x0000000000000000000000000000000000000000000000000000000000000000',
478
+ '0x00000000000000000000000087870bca3f3fd6335c3f4ce8392d69d0b4161d39',
479
+ '0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff',
480
+ '0x0000000000000000000000000000000000000000000000000000000000000000',
481
+ ],
482
+ ],
483
+ // WETH -> WBTC
484
+ [
485
+ {
486
+ fromAsset: web3Utils.toChecksumAddress(getAssetInfo('WETH', ChainId.Ethereum).address),
487
+ fromAssetId: 0,
488
+ toAsset: web3Utils.toChecksumAddress(getAssetInfo('WBTC', ChainId.Ethereum).address),
489
+ toAssetId: 2,
490
+ marketAddr: web3Utils.toChecksumAddress('0x87870Bca3F3fD6335C3F4ce8392D69d0B4161d39'),
491
+ amountToSwitch: '5000000000000000000',
492
+ },
493
+ [
494
+ '0x000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
495
+ '0x0000000000000000000000000000000000000000000000000000000000000000',
496
+ '0x0000000000000000000000002260fac5e5542a773aa44fbcfedf7c193bc2c599',
497
+ '0x0000000000000000000000000000000000000000000000000000000000000002',
498
+ '0x00000000000000000000000087870bca3f3fd6335c3f4ce8392d69d0b4161d39',
499
+ '0x0000000000000000000000000000000000000000000000004563918244f40000',
500
+ '0x0000000000000000000000000000000000000000000000000000000000000000',
501
+ ],
502
+ ],
503
+ ];
504
+
505
+ examples.forEach(([expected, actual]) => {
506
+ it(`Given ${JSON.stringify(actual)} should return expected value: ${JSON.stringify(expected)}`, () => {
507
+ expect(subDataService.aaveV3CollateralSwitchSubData.decode(actual)).to.eql(expected);
508
+ });
509
+ });
510
+ });
511
+ });
512
+
367
513
  describe('When testing subDataService.compoundV2LeverageManagementSubData', () => {
368
514
  describe('encode()', () => {
369
515
  const examples: Array<[[string, string, string, string, boolean], [triggerRepayRatio: number, triggerBoostRatio: number, targetBoostRatio: number, targetRepayRatio: number, boostEnabled: boolean]]> = [
@@ -313,6 +313,55 @@ export const aaveV3CloseGenericSubData = {
313
313
  },
314
314
  };
315
315
 
316
+ export const aaveV3CollateralSwitchSubData = {
317
+ encode(
318
+ fromAsset: EthereumAddress,
319
+ fromAssetId: number,
320
+ toAsset: EthereumAddress,
321
+ toAssetId: number,
322
+ marketAddr: EthereumAddress,
323
+ amountToSwitch: string,
324
+ useOnBehalf: boolean = false,
325
+ ): SubData {
326
+ const encodedFromAsset = AbiCoder.encodeParameter('address', fromAsset);
327
+ const encodedFromAssetId = AbiCoder.encodeParameter('uint8', fromAssetId);
328
+ const encodedToAsset = AbiCoder.encodeParameter('address', toAsset);
329
+ const encodedToAssetId = AbiCoder.encodeParameter('uint8', toAssetId);
330
+ const encodedMarketAddr = AbiCoder.encodeParameter('address', marketAddr);
331
+ const encodedAmountToSwitch = AbiCoder.encodeParameter('uint256', amountToSwitch);
332
+ const encodedUseOnBehalf = AbiCoder.encodeParameter('bool', useOnBehalf);
333
+
334
+ return [
335
+ encodedFromAsset,
336
+ encodedFromAssetId,
337
+ encodedToAsset,
338
+ encodedToAssetId,
339
+ encodedMarketAddr,
340
+ encodedAmountToSwitch,
341
+ encodedUseOnBehalf,
342
+ ];
343
+ },
344
+ decode(subData: SubData): {
345
+ fromAsset: EthereumAddress,
346
+ fromAssetId: number,
347
+ toAsset: EthereumAddress,
348
+ toAssetId: number,
349
+ marketAddr: EthereumAddress,
350
+ amountToSwitch: string,
351
+ } {
352
+ const fromAsset = AbiCoder.decodeParameter('address', subData[0]) as unknown as EthereumAddress;
353
+ const fromAssetId = Number(AbiCoder.decodeParameter('uint8', subData[1]));
354
+ const toAsset = AbiCoder.decodeParameter('address', subData[2]) as unknown as EthereumAddress;
355
+ const toAssetId = Number(AbiCoder.decodeParameter('uint8', subData[3]));
356
+ const marketAddr = AbiCoder.decodeParameter('address', subData[4]) as unknown as EthereumAddress;
357
+ const amountToSwitch = AbiCoder.decodeParameter('uint256', subData[5]) as unknown as string;
358
+
359
+ return {
360
+ fromAsset, fromAssetId, toAsset, toAssetId, marketAddr, amountToSwitch,
361
+ };
362
+ },
363
+ };
364
+
316
365
  export const aaveV3QuotePriceSubData = {
317
366
  encode(
318
367
  collAsset: EthereumAddress,
@@ -96,21 +96,25 @@ export namespace Strategies {
96
96
  LIQUITY_DEBT_IN_FRONT_REPAY = 75,
97
97
  CURVEUSD_PAYBACK = 92,
98
98
  LIQUITY_V2_PAYBACK = 113,
99
+ AAVE_V3_COLLATERAL_SWITCH = 135,
99
100
  }
100
101
 
101
102
  export enum OptimismIds {
102
103
  EXCHANGE_DCA = 8,
103
104
  EXCHANGE_LIMIT_ORDER = 9,
105
+ AAVE_V3_COLLATERAL_SWITCH = 24,
104
106
  }
105
107
 
106
108
  export enum BaseIds {
107
109
  EXCHANGE_DCA = 8,
108
110
  EXCHANGE_LIMIT_ORDER = 9,
111
+ AAVE_V3_COLLATERAL_SWITCH = 56,
109
112
  }
110
113
 
111
114
  export enum ArbitrumIds {
112
115
  EXCHANGE_DCA = 8,
113
116
  EXCHANGE_LIMIT_ORDER = 9,
117
+ AAVE_V3_COLLATERAL_SWITCH = 50,
114
118
  }
115
119
 
116
120
  export enum Identifiers {
@@ -143,6 +147,7 @@ export namespace Strategies {
143
147
  RepayOnPrice = 'repay-on-price',
144
148
  EoaBoostOnPrice = 'eoa-boost-on-price',
145
149
  EoaRepayOnPrice = 'eoa-repay-on-price',
150
+ CollateralSwitch = 'collateral-switch',
146
151
  }
147
152
  export enum IdOverrides {
148
153
  TakeProfit = 'take-profit',