Skip to content

Commit c032e67

Browse files
committed
cap deposit amount by XCH amount held in wallet
1 parent 9e13f00 commit c032e67

1 file changed

Lines changed: 45 additions & 15 deletions

File tree

keeper_bots/liquidation_bid_bot.py

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -728,29 +728,37 @@ async def run_liquidation_bid_bot():
728728
balances = await rpc_client.wallet_balances()
729729
except httpx.HTTPStatusError as err:
730730
log.error("Failed to get wallet balances due to HTTPStatusError: %s", err)
731+
available_xch_amount = None
731732
available_byc_amount = None
732733
except httpx.ReadTimeout as err:
733734
log.error("Failed to get wallet balances due to ReadTimeout: %s", err)
735+
available_xch_amount = None
734736
available_byc_amount = None
735737
except ValueError as err:
736738
log.error("Failed to get wallet balances due to ValueError: %s", err)
739+
available_xch_amount = None
737740
available_byc_amount = None
738741
except Exception as err:
739742
log.error("Failed to get wallet balances: %s", err)
743+
available_xch_amount = None
740744
available_byc_amount = None
741745
else:
746+
available_xch_amount = balances["xch"]
742747
available_byc_amount = balances["byc"]
743748

744749
if not vaults_in_liquidation:
745750
log.info("No vaults in liquidation")
746751
# Print account balances
747752

748753
log.info(
749-
"Wallet balance: %s. OKX balance: %s. Sleeping for %s seconds",
750-
f"{available_byc_amount / MCAT} BYC"
754+
"Wallet balances: %s, %s. OKX balance: %s. Sleeping for %s seconds",
755+
f"{available_xch_amount / MOJOS_PER_XCH:.12f} XCH"
756+
if available_xch_amount is not None
757+
else None,
758+
f"{available_byc_amount / MCAT:.3f} BYC"
751759
if available_byc_amount is not None
752760
else None,
753-
f"{available_xch_balance} {collateral_symbol}"
761+
f"{available_xch_balance:.12f} {collateral_symbol}"
754762
if available_xch_balance is not None
755763
else None,
756764
RUN_INTERVAL,
@@ -803,6 +811,7 @@ async def run_liquidation_bid_bot():
803811
if available_byc_amount < debt:
804812
# get min debt amount from Statutes
805813
min_debt = 100 * MCAT # fall back amount
814+
liquidation_ratio_pct = 170 # fall back amount
806815
try:
807816
statutes = await rpc_client.statutes_list()
808817
except httpx.HTTPStatusError as err:
@@ -815,22 +824,43 @@ async def run_liquidation_bid_bot():
815824
log.error("Failed to get Statutes: %s", err)
816825
else:
817826
min_debt = int(statutes["implemented_statutes"]["VAULT_MINIMUM_DEBT"])
827+
liquidation_ratio_pct = int(
828+
statutes["implemented_statutes"]["VAULT_LIQUIDATION_RATIO_PCT"]
829+
)
830+
831+
collateralization_ratio = (
832+
max(
833+
100 + 3 * (liquidation_ratio_pct - 100),
834+
LIQUIDATION_COLLATERAL_RATIO_PCT,
835+
)
836+
/ 100
837+
)
818838

819-
borrow_amount = max(
820-
min_debt, debt - available_byc_amount
821-
) # in mBYC # TODO: needs to result in at least min debt
822-
deposit_amount = int(
823-
MOJOS_PER_XCH
824-
* (borrow_amount / MCAT)
825-
* (LIQUIDATION_COLLATERAL_RATIO_PCT / 100)
826-
/ price
839+
borrow_amount = max(min_debt, debt - available_byc_amount) # in mBYC
840+
deposit_amount = min(
841+
max(
842+
available_xch_amount - MOJOS_PER_XCH
843+
), # keep 1 XCH for fees. TODO: better heuristic
844+
int(
845+
MOJOS_PER_XCH
846+
* (borrow_amount / MCAT)
847+
* collateralization_ratio
848+
/ price
849+
),
827850
) # in mojos
851+
borrow_amount = (
852+
MCAT
853+
* (deposit_amount / MOJOS_PER_XCH)
854+
* price
855+
/ collateralization_ratio
856+
) # in mBYC
828857

829858
log.info(
830-
"Depositing %s XCH to borrow %s BYC to bid on debt. Existing balance: %s BYC",
831-
deposit_amount,
832-
borrow_amount,
833-
available_byc_amount,
859+
"Depositing %.12f XCH to borrow %.3f BYC to bid on debt. Existing wallet balances: %.12f XCH, %.3f BYC",
860+
deposit_amount / MOJOS_PER_XCH,
861+
borrow_amount / MCAT,
862+
available_xch_amount / MOJOS_PER_XCH,
863+
available_byc_amount / MCAT,
834864
)
835865

836866
# borrow enough BYC to liquidate all vaults

0 commit comments

Comments
 (0)