1
1
import "module-alias/register" ;
2
2
import { BigNumber } from "@ethersproject/bignumber" ;
3
+ import { defaultAbiCoder } from "ethers/lib/utils" ;
3
4
4
5
import {
5
6
Account ,
@@ -14,6 +15,7 @@ import { CompoundLeverageModule, DebtIssuanceModule, SetToken } from "@utils/con
14
15
import { CEther , CERc20 } from "@utils/contracts/compound" ;
15
16
import DeployHelper from "@utils/deploys" ;
16
17
import {
18
+ bitcoin ,
17
19
cacheBeforeEach ,
18
20
ether ,
19
21
getAccounts ,
@@ -103,6 +105,7 @@ describe("FLIRebalanceViewer", () => {
103
105
104
106
await setV2Setup . weth . connect ( owner . wallet ) . approve ( uniswapSetup . router . address , MAX_UINT_256 ) ;
105
107
await setV2Setup . usdc . connect ( owner . wallet ) . approve ( uniswapSetup . router . address , MAX_UINT_256 ) ;
108
+ await setV2Setup . wbtc . connect ( owner . wallet ) . approve ( uniswapSetup . router . address , MAX_UINT_256 ) ;
106
109
await uniswapSetup . router . addLiquidity (
107
110
setV2Setup . weth . address ,
108
111
setV2Setup . usdc . address ,
@@ -113,6 +116,26 @@ describe("FLIRebalanceViewer", () => {
113
116
owner . address ,
114
117
MAX_UINT_256
115
118
) ;
119
+ await uniswapSetup . router . addLiquidity (
120
+ setV2Setup . wbtc . address ,
121
+ setV2Setup . usdc . address ,
122
+ bitcoin ( 100 ) ,
123
+ usdc ( 5000000 ) ,
124
+ bitcoin ( 99 ) ,
125
+ usdc ( 4000000 ) ,
126
+ owner . address ,
127
+ MAX_UINT_256
128
+ ) ;
129
+ await uniswapSetup . router . addLiquidity (
130
+ setV2Setup . wbtc . address ,
131
+ setV2Setup . weth . address ,
132
+ bitcoin ( 100 ) ,
133
+ ether ( 5000 ) ,
134
+ bitcoin ( 99 ) ,
135
+ ether ( 4000 ) ,
136
+ owner . address ,
137
+ MAX_UINT_256
138
+ ) ;
116
139
117
140
// Mint cTokens
118
141
await setV2Setup . usdc . approve ( cUSDC . address , ether ( 100000 ) ) ;
@@ -581,5 +604,96 @@ describe("FLIRebalanceViewer", () => {
581
604
} ) ;
582
605
} ) ;
583
606
} ) ;
607
+
608
+ context . only ( "when there is a multihop trade" , async ( ) => {
609
+ describe ( "when above max leverage ratio but below incentivized leverage ratio" , async ( ) => {
610
+ beforeEach ( async ( ) => {
611
+ await compoundSetup . priceOracle . setUnderlyingPrice ( cEther . address , ether ( 850 ) ) ;
612
+
613
+ await flexibleLeverageStrategyAdapter . setExecutionSettings (
614
+ {
615
+ unutilizedLeveragePercentage : execution . unutilizedLeveragePercentage ,
616
+ twapMaxTradeSize : execution . twapMaxTradeSize ,
617
+ twapCooldownPeriod : execution . twapCooldownPeriod ,
618
+ slippageTolerance : execution . slippageTolerance ,
619
+ exchangeName : "UniswapTradeAdapter" ,
620
+ leverExchangeData : EMPTY_BYTES ,
621
+ deleverExchangeData : defaultAbiCoder . encode (
622
+ [ "address[]" ] ,
623
+ [ [ setV2Setup . weth . address , setV2Setup . wbtc . address , setV2Setup . usdc . address ] ]
624
+ ) ,
625
+ }
626
+ ) ;
627
+ } ) ;
628
+
629
+ it ( "should return rebalance" , async ( ) => {
630
+ const shouldRebalance = await subject ( ) ;
631
+
632
+ expect ( shouldRebalance ) . to . eq ( ONE ) ;
633
+ } ) ;
634
+
635
+ describe ( "but Uniswap slippage would exceed bounds" , async ( ) => {
636
+ beforeEach ( async ( ) => {
637
+ await setUniswapPoolToPrice (
638
+ uniswapSetup . router ,
639
+ uniswapSetup . wethWbtcPool ,
640
+ setV2Setup . weth ,
641
+ setV2Setup . wbtc ,
642
+ ether ( 0.01 ) ,
643
+ owner . address
644
+ ) ;
645
+ } ) ;
646
+
647
+ it ( "should return update oracle" , async ( ) => {
648
+ const shouldRebalance = await subject ( ) ;
649
+
650
+ expect ( shouldRebalance ) . to . eq ( FOUR ) ;
651
+ } ) ;
652
+ } ) ;
653
+ } ) ;
654
+
655
+ describe ( "when below min leverage ratio" , async ( ) => {
656
+ beforeEach ( async ( ) => {
657
+ await compoundSetup . priceOracle . setUnderlyingPrice ( cEther . address , ether ( 1400 ) ) ;
658
+
659
+ await flexibleLeverageStrategyAdapter . setExecutionSettings (
660
+ {
661
+ unutilizedLeveragePercentage : execution . unutilizedLeveragePercentage ,
662
+ twapMaxTradeSize : execution . twapMaxTradeSize ,
663
+ twapCooldownPeriod : execution . twapCooldownPeriod ,
664
+ slippageTolerance : execution . slippageTolerance ,
665
+ exchangeName : "UniswapTradeAdapter" ,
666
+ leverExchangeData : defaultAbiCoder . encode ( [ "address[]" ] , [ [ setV2Setup . usdc . address , setV2Setup . wbtc . address , setV2Setup . weth . address ] ] ) ,
667
+ deleverExchangeData : EMPTY_BYTES ,
668
+ }
669
+ ) ;
670
+ } ) ;
671
+
672
+ it ( "should return rebalance" , async ( ) => {
673
+ const shouldRebalance = await subject ( ) ;
674
+
675
+ expect ( shouldRebalance ) . to . eq ( ONE ) ;
676
+ } ) ;
677
+
678
+ describe ( "but Uniswap slippage would exceed bounds" , async ( ) => {
679
+ beforeEach ( async ( ) => {
680
+ await setUniswapPoolToPrice (
681
+ uniswapSetup . router ,
682
+ uniswapSetup . wethWbtcPool ,
683
+ setV2Setup . weth ,
684
+ setV2Setup . wbtc ,
685
+ ether ( 0.06 ) ,
686
+ owner . address
687
+ ) ;
688
+ } ) ;
689
+
690
+ it ( "should return update oracle" , async ( ) => {
691
+ const shouldRebalance = await subject ( ) ;
692
+
693
+ expect ( shouldRebalance ) . to . eq ( FOUR ) ;
694
+ } ) ;
695
+ } ) ;
696
+ } ) ;
697
+ } ) ;
584
698
} ) ;
585
699
} ) ;
0 commit comments