Skip to content

Commit

Permalink
make changes pt 3
Browse files Browse the repository at this point in the history
  • Loading branch information
Anirudh Suresh authored and Anirudh Suresh committed Jan 22, 2024
1 parent aeeca51 commit d329f2e
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 12 deletions.
28 changes: 21 additions & 7 deletions beacon/protocols/beacon_TokenVault.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 = []
Expand Down Expand Up @@ -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}")

Expand Down
6 changes: 2 additions & 4 deletions beacon/searcher/searcherA.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import web3
from web3.auto import w3
from eth_account import Account
from eth_account.signers.local import LocalAccount
from eth_abi import encode
import httpx
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
Expand Down
13 changes: 13 additions & 0 deletions beacon/searcher/searcher_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
17 changes: 16 additions & 1 deletion beacon/searcher/searcher_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down

0 comments on commit d329f2e

Please sign in to comment.