From cbe28a32c7e16900b302f6994b5b5c4ca0845d9d Mon Sep 17 00:00:00 2001 From: sajanrajdev Date: Wed, 6 Dec 2023 13:06:11 +0100 Subject: [PATCH 1/4] feat: divest to usdc --- great_ape_safe/ape_api/aave.py | 17 +++++++++-------- scripts/issue/533/leveraged_wbtc.py | 19 +++++++++++++++++-- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/great_ape_safe/ape_api/aave.py b/great_ape_safe/ape_api/aave.py index c9ac6a93..0c9dcbc3 100644 --- a/great_ape_safe/ape_api/aave.py +++ b/great_ape_safe/ape_api/aave.py @@ -149,8 +149,8 @@ def lever_up(self, collateral_token, borrow_token, perc): [borrow_token, collateral_token], we_want_to_borrow ) self.deposit(collateral_token, to_reinvest) - - def delever(self, collateral_token, borrow_token): + + def delever(self, collateral_token, borrow_token, gains_token): debt_to_repay = self._get_debt_in_token(borrow_token) debt_paid = 0 while debt_paid < debt_to_repay: @@ -214,11 +214,12 @@ def delever(self, collateral_token, borrow_token): bal_borrow_token_after = borrow_token.balanceOf(self.safe) - ## Swap remaining margin of borrow token back into collateral token - self.safe.init_uni_v3() - self.safe.uni_v3.swap( - [borrow_token, collateral_token], - bal_borrow_token_after - bal_borrow_token_before, - ) + if gains_token == collateral_token: + ## Swap remaining margin of borrow token back into collateral token + self.safe.init_uni_v3() + self.safe.uni_v3.swap( + [borrow_token, collateral_token], + bal_borrow_token_after - bal_borrow_token_before, + ) assert self._get_debt_in_token(borrow_token) == 0 diff --git a/scripts/issue/533/leveraged_wbtc.py b/scripts/issue/533/leveraged_wbtc.py index cc13dedd..f1a363c0 100644 --- a/scripts/issue/533/leveraged_wbtc.py +++ b/scripts/issue/533/leveraged_wbtc.py @@ -7,6 +7,11 @@ AWBTC = SAFE.contract(registry.eth.treasury_tokens.aWBTC) USDC = SAFE.contract(registry.eth.treasury_tokens.USDC) +INITIAL_DEPOSIT = 5 * 10**WBTC.decimals() #Ref: https://etherscan.io/tx/0x52b76fb6f90df24eaa23d2793e3aef3d35a7f0f5ed2e95b6f8fed6f488b8c77b + +# slippages +COEF = 0.95 +DEADLINE = 60 * 60 * 12 def lever_up(): SAFE.init_aave() @@ -20,9 +25,19 @@ def lever_up(): def delever(): SAFE.init_aave() + SAFE.init_cow(True) SAFE.take_snapshot([WBTC, AWBTC, USDC]) - SAFE.aave.delever(WBTC, USDC) + wbtc_bal_before = WBTC.balanceOf(SAFE.account) + SAFE.aave.delever(WBTC, USDC, USDC) SAFE.aave.withdraw_all(WBTC) + wbtc_bal_after = WBTC.balanceOf(SAFE.account) - SAFE.post_safe_tx() + wbtc_gained = (wbtc_bal_after - wbtc_bal_before) - INITIAL_DEPOSIT + print("wBTC Gained:", wbtc_gained) + + SAFE.cow.market_sell( + WBTC, USDC, wbtc_gained, deadline=DEADLINE, coef=COEF + ) + + SAFE.post_safe_tx(skip_preview=True, replace_nonce=231) From 1b929ffc787190862d1c54e6285d20a89c75a8d7 Mon Sep 17 00:00:00 2001 From: sajanrajdev Date: Wed, 6 Dec 2023 13:12:38 +0100 Subject: [PATCH 2/4] feat: fix univ3 slippage --- great_ape_safe/ape_api/uni_v3.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/great_ape_safe/ape_api/uni_v3.py b/great_ape_safe/ape_api/uni_v3.py index 91761948..820b0665 100644 --- a/great_ape_safe/ape_api/uni_v3.py +++ b/great_ape_safe/ape_api/uni_v3.py @@ -455,7 +455,7 @@ def swap(self, path, mantissa, destination=None): path_encoded = self._encode_path(multihop_path) min_out = self.quoter.quoteExactInput.call(path_encoded, mantissa) * ( - 1 - self.slippage + self.slippage ) params = ( From 36d8bcb764c8eb2a1753f460878335cf38c22cc5 Mon Sep 17 00:00:00 2001 From: sajanrajdev Date: Wed, 6 Dec 2023 13:14:42 +0100 Subject: [PATCH 3/4] fix: lint --- great_ape_safe/ape_api/aave.py | 4 ++-- scripts/issue/533/leveraged_wbtc.py | 9 +++++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/great_ape_safe/ape_api/aave.py b/great_ape_safe/ape_api/aave.py index 0c9dcbc3..e5e56b7e 100644 --- a/great_ape_safe/ape_api/aave.py +++ b/great_ape_safe/ape_api/aave.py @@ -149,7 +149,7 @@ def lever_up(self, collateral_token, borrow_token, perc): [borrow_token, collateral_token], we_want_to_borrow ) self.deposit(collateral_token, to_reinvest) - + def delever(self, collateral_token, borrow_token, gains_token): debt_to_repay = self._get_debt_in_token(borrow_token) debt_paid = 0 @@ -220,6 +220,6 @@ def delever(self, collateral_token, borrow_token, gains_token): self.safe.uni_v3.swap( [borrow_token, collateral_token], bal_borrow_token_after - bal_borrow_token_before, - ) + ) assert self._get_debt_in_token(borrow_token) == 0 diff --git a/scripts/issue/533/leveraged_wbtc.py b/scripts/issue/533/leveraged_wbtc.py index f1a363c0..c4812f93 100644 --- a/scripts/issue/533/leveraged_wbtc.py +++ b/scripts/issue/533/leveraged_wbtc.py @@ -7,12 +7,15 @@ AWBTC = SAFE.contract(registry.eth.treasury_tokens.aWBTC) USDC = SAFE.contract(registry.eth.treasury_tokens.USDC) -INITIAL_DEPOSIT = 5 * 10**WBTC.decimals() #Ref: https://etherscan.io/tx/0x52b76fb6f90df24eaa23d2793e3aef3d35a7f0f5ed2e95b6f8fed6f488b8c77b +INITIAL_DEPOSIT = ( + 5 * 10 ** WBTC.decimals() +) # Ref: https://etherscan.io/tx/0x52b76fb6f90df24eaa23d2793e3aef3d35a7f0f5ed2e95b6f8fed6f488b8c77b # slippages COEF = 0.95 DEADLINE = 60 * 60 * 12 + def lever_up(): SAFE.init_aave() SAFE.take_snapshot([WBTC, AWBTC, USDC]) @@ -36,8 +39,6 @@ def delever(): wbtc_gained = (wbtc_bal_after - wbtc_bal_before) - INITIAL_DEPOSIT print("wBTC Gained:", wbtc_gained) - SAFE.cow.market_sell( - WBTC, USDC, wbtc_gained, deadline=DEADLINE, coef=COEF - ) + SAFE.cow.market_sell(WBTC, USDC, wbtc_gained, deadline=DEADLINE, coef=COEF) SAFE.post_safe_tx(skip_preview=True, replace_nonce=231) From 80004d9be2ee96aa42067e8d328813313ac5386d Mon Sep 17 00:00:00 2001 From: sajanrajdev Date: Wed, 6 Dec 2023 13:55:13 +0100 Subject: [PATCH 4/4] feat: clean-up aave method --- great_ape_safe/ape_api/aave.py | 13 ++++--------- scripts/issue/533/leveraged_wbtc.py | 2 +- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/great_ape_safe/ape_api/aave.py b/great_ape_safe/ape_api/aave.py index e5e56b7e..eb281fa4 100644 --- a/great_ape_safe/ape_api/aave.py +++ b/great_ape_safe/ape_api/aave.py @@ -150,7 +150,7 @@ def lever_up(self, collateral_token, borrow_token, perc): ) self.deposit(collateral_token, to_reinvest) - def delever(self, collateral_token, borrow_token, gains_token): + def delever(self, collateral_token, borrow_token): debt_to_repay = self._get_debt_in_token(borrow_token) debt_paid = 0 while debt_paid < debt_to_repay: @@ -214,12 +214,7 @@ def delever(self, collateral_token, borrow_token, gains_token): bal_borrow_token_after = borrow_token.balanceOf(self.safe) - if gains_token == collateral_token: - ## Swap remaining margin of borrow token back into collateral token - self.safe.init_uni_v3() - self.safe.uni_v3.swap( - [borrow_token, collateral_token], - bal_borrow_token_after - bal_borrow_token_before, - ) - assert self._get_debt_in_token(borrow_token) == 0 + + ## Return margin of borrow token in case a swap back to collateral is desired + return bal_borrow_token_after - bal_borrow_token_before diff --git a/scripts/issue/533/leveraged_wbtc.py b/scripts/issue/533/leveraged_wbtc.py index c4812f93..33233b75 100644 --- a/scripts/issue/533/leveraged_wbtc.py +++ b/scripts/issue/533/leveraged_wbtc.py @@ -32,7 +32,7 @@ def delever(): SAFE.take_snapshot([WBTC, AWBTC, USDC]) wbtc_bal_before = WBTC.balanceOf(SAFE.account) - SAFE.aave.delever(WBTC, USDC, USDC) + SAFE.aave.delever(WBTC, USDC) SAFE.aave.withdraw_all(WBTC) wbtc_bal_after = WBTC.balanceOf(SAFE.account)