Skip to content

Commit

Permalink
Merge pull request #1464 from Badger-Finance/feat/delever-fix
Browse files Browse the repository at this point in the history
Feat/delever fix
  • Loading branch information
sajanrajdev authored Dec 6, 2023
2 parents a26256b + 80004d9 commit 027ac60
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
10 changes: 3 additions & 7 deletions great_ape_safe/ape_api/aave.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,7 @@ 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,
)

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
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
18 changes: 17 additions & 1 deletion 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])

wbtc_bal_before = WBTC.balanceOf(SAFE.account)
SAFE.aave.delever(WBTC, 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)

0 comments on commit 027ac60

Please sign in to comment.