diff --git a/beacon/protocols/beacon_TokenVault.py b/beacon/protocols/beacon_TokenVault.py index eb8767bd..e16e19bb 100644 --- a/beacon/protocols/beacon_TokenVault.py +++ b/beacon/protocols/beacon_TokenVault.py @@ -4,9 +4,11 @@ from typing import TypedDict import argparse import logging +import asyncio +import httpx -from beacon.utils.pyth_prices import * -from beacon.utils.types_liquidation_adapter import * +from beacon.utils.pyth_prices import PriceFeedClient, PriceFeed +from beacon.utils.types_liquidation_adapter import LiquidationOpportunity TOKEN_VAULT_ADDRESS = "0x72A22FfcAfa6684d4EE449620270ac05afE963d0" @@ -40,7 +42,10 @@ async def get_accounts(rpc_url: str) -> list[ProtocolAccount]: """ Returns all the open accounts in the protocol in the form of a list of type ProtocolAccount. - get_accounts(rpc_url) takes the RPC URL of the chain as an argument and returns all the open accounts in the protocol in the form of a list of objects of type ProtocolAccount (defined above). Each ProtocolAccount object represents an account/vault in the protocol. + Args: + rpc_url (str): The RPC URL of the chain + Returns: + List of objects of type ProtocolAccount (defined above). Each ProtocolAccount object represents an account/vault in the protocol. """ abi = get_vault_abi() w3 = web3.AsyncWeb3(web3.AsyncHTTPProvider(rpc_url)) @@ -90,6 +95,12 @@ def create_liquidation_opp( prices: list[PriceFeed]) -> LiquidationOpportunity: """ Constructs a LiquidationOpportunity object from a ProtocolAccount object and a set of relevant Pyth PriceFeeds. + + Args: + account: A ProtocolAccount object, representing an account/vault in the protocol. + prices: A list of PriceFeed objects, representing the relevant Pyth price feeds for the tokens in the ProtocolAccount object. + Returns: + A LiquidationOpportunity object corresponding to the specified account. """ # [bytes.fromhex(update['vaa']) for update in prices] ## TODO: uncomment this, to add back price updates @@ -140,9 +151,11 @@ def get_liquidatable(accounts: list[ProtocolAccount], """ Filters list of ProtocolAccount types to return a list of LiquidationOpportunity types. - get_liquidatable(accounts, prices) takes two arguments: account--a list of ProtocolAccount (defined above) objects--and prices--a dictionary of Pyth prices. - accounts should be the list of all open accounts in the protocol (i.e. the output of get_accounts()). - prices should be a dictionary of Pyth prices, where the keys are Pyth feed IDs and the values are PriceFeed objects. prices can be retrieved from the provided price retrieval functions. + Args: + accounts: A list of ProtocolAccount objects, representing all the open accounts in the protocol. + prices: A dictionary of Pyth price feeds, where the keys are Pyth feed IDs and the values are PriceFeed objects. + Returns: + A list of LiquidationOpportunity objects, one per account that is eligible for liquidation. """ liquidatable = [] @@ -219,7 +232,8 @@ async def main(): logging.error("Provided beacon server endpoint url not found") elif resp.status_code == 405: logging.error("Provided beacon server endpoint url does not support POST requests") - logging.info(f"Response, post to beacon: {resp.text}") + else: + logging.info(f"Response, post to beacon: {resp.text}") else: logging.info(f"List of liquidatable accounts:\n{accounts_liquidatable}") diff --git a/beacon/searcher/searcherA.py b/beacon/searcher/searcherA.py index 064858eb..8bd4ce94 100644 --- a/beacon/searcher/searcherA.py +++ b/beacon/searcher/searcherA.py @@ -1,5 +1,3 @@ -import web3 -from web3.auto import w3 from eth_account import Account from eth_account.signers.local import LocalAccount from eth_abi import encode @@ -7,8 +5,8 @@ import asyncio from beacon.utils.types_liquidation_adapter import * -from beacon.utils.endpoints import * -from beacon.searcher.searcher_utils import * +from beacon.utils.endpoints import BEACON_SERVER_ENDPOINT_GETOPPS, AUCTION_SERVER_ENDPOINT +from beacon.searcher.searcher_utils import UserLiquidationParams, construct_signature_liquidator BID = 10 VALID_UNTIL = 1_000_000_000_000 diff --git a/beacon/searcher/searcher_template.py b/beacon/searcher/searcher_template.py index fc123956..b6d19e07 100644 --- a/beacon/searcher/searcher_template.py +++ b/beacon/searcher/searcher_template.py @@ -13,6 +13,11 @@ def assess_liquidation_opportunity( Individual searchers will have their own methods to determine market impact and the profitability of conducting a liquidation. This function can be expanded to include external prices to perform this evaluation. If the LiquidationOpportunity is deemed worthwhile, this function can return a bid amount representing the amount of native token to bid on this opportunity, and a timestamp representing the time at which the transaction will expire. Otherwise, this function can return None. + + Args: + opp: A LiquidationOpportunity object, representing a single liquidation opportunity. + Returns: + If the LiquidationOpportunity is deemed worthwhile, this function can return a UserLiquidationParams object, representing the user's bid and the timestamp at which the user's transaction should expire. If the LiquidationOpportunity is not deemed worthwhile, this function can return None. """ raise NotImplementedError @@ -27,5 +32,13 @@ def create_liquidation_tx( Processes a LiquidationOpportunity into a LiquidationAdapterTransaction This function can handle constructing the LiquidationAdapterTransaction to submit. The calldata for the LiquidationAdapter contract should be constructed according to the LiquidationAdapterCalldata type; you can leverage the construct_signature_liquidator function to construct the signature field. + + Args: + opp: A LiquidationOpportunity object, representing a single liquidation opportunity. + sk_liquidator: A 0x-prefixed hex string representing the liquidator's private key. + valid_until: An integer representing the timestamp at which the transaction will expire. + bid: An integer representing the amount of native token to bid on this opportunity. + Returns: + A LiquidationAdapterTransaction object, representing the transaction to submit. """ raise NotImplementedError diff --git a/beacon/searcher/searcher_utils.py b/beacon/searcher/searcher_utils.py index 7f10d295..b868995e 100644 --- a/beacon/searcher/searcher_utils.py +++ b/beacon/searcher/searcher_utils.py @@ -15,7 +15,22 @@ def construct_signature_liquidator( bid: int, valid_until: int, secret_key: str -): +) -> web3.types.ECDSASignature: + """ + Constructs a signature for a liquidator's transaction to submit to the LiquidationAdapter contract. + + Args: + repay_tokens: A list of tuples (token address, amount) representing the tokens to repay. + receipt_tokens: A list of tuples (token address, amount) representing the tokens to receive. + address: The address of the LiquidationAdapter contract. + liq_calldata: The calldata for the liquidation method call. + bid: The amount of native token to bid on this opportunity. + valid_until: The timestamp at which the transaction will expire. + secret_key: A 0x-prefixed hex string representing the liquidator's private key. + Returns: + An web3 ECDSASignature object, representing the liquidator's signature. + """ + digest = encode( ['(address,uint256)[]', '(address,uint256)[]', 'address', 'bytes', 'uint256'],