Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/delever fix #1464

Merged
merged 4 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions great_ape_safe/ape_api/aave.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
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:
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion great_ape_safe/ape_api/uni_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down
20 changes: 18 additions & 2 deletions scripts/issue/533/leveraged_wbtc.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
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()
Expand All @@ -20,9 +28,17 @@ 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)
Loading