changed 2 years ago
Published Linked with GitHub

Lyra v2 liquidations

Mainnet constants

Explorer URL: https://explorer.lyra.finance

RPC URL: https://rpc.lyra.finance

ChainID: 957

AUCTION_ADDRESS=0xC51E95b72e116020B138cd5D97Ed4A72DE8Dc48B
SUBACCOUNT_ADDRESS=0xE7603DF191D699d8BD9891b821347dbAb889E5a5
AUCTION_UTILS_ADDRESS=0x69812bDf2b6466E8e40A9790105e547d9Aed77B7

Bidding/Margin helper

Helper contract to make the bidding process simpler, but this is unaudited and somewhat higher risk as you have to approve it to access your full subaccount.

code can be found here: https://github.com/lyra-finance/v2-matching/blob/master/src/periphery/LyraAuctionUtils.sol

This was deployed to testnet with address: 0x69812bDf2b6466E8e40A9790105e547d9Aed77B7

Bidding script

Glossary

  • Exchange account owner
  • Trading subaccount
    • An account on the exchange which will receive the proceeds of the liquidation
  • Bidding subaccount
    • A subaccount that lives off the exchange. It holds cash that is used for collateral for auctions that are being bid on. This must be owned by the hotwallet that is used by the scripts
  • Session key/hotwallet
    • An authorised key that can make trades on behalf of the exchange account owner
    • This private key is used in the scripts to execute on-chain actions and to sign messages on the exchange to do transfers/deposits etc.
    • NOTE: currently scripts only support this being the same as the exchange account owner

Running the liquidation scripts

  • Clone: https://github.com/lyra-finance/v2-scripts
  • Install pre-requisites
  • Follow installation instructions
    • In the v2-scripts directory
    • pnpm i
    • cp .env.mainnet.example .env
  • Modify SIGNING_KEY field in the .env
    • NOTE: only the exchange owner key is currently supported
    • This key will need to be a signing key for an exchange account
    • If not the owner of the exchange account, withdrawals from the exchange will not go to this address (they always go to the owner)
    • This key will also own the "bidder subaccount", which will hold funds for collateral for liquidations
  • For any of the following commands, run them with --help to see more options. E.g. pnpm cmd liquidationFlow --help
  • Run pnpm cmd setupLiquidationAccs -t <tradingDepositAmt> -b <biddingDepositAmt>
    • If TRADING_SUBACCOUNT or BIDDING_SUBBACOUNT don't exist in the .env they are created and deposited into
    • If they exist, then the deposit amount is added to the existing accounts
      • <TODO: trading account deposit not yet implemented>
    • tradingDepositAmount: Amount of total funds to deposit into the trading account. There are the funds you want to trade with.
    • bidderDepositAmount: Amount of total funds to prepare to use as collateral in bidding. These are not accessible on the exchange.
  • Add the generated TRADING_SUBACCOUNT and BIDDING_SUBBACOUNT ids to the .env
  • Run pnpm cmd getAllCurrentAuctions
    • This will list out all current auctions (based on auctionStarted events)
    • This will not capture accounts that are below margin requirements but have not yet been flagged
    • Take note of the subaccount ids for accounts that you want to bid on
    • Without freshly submitted feed data, these values may be innacurate.
  • If you have found an account that you want to bid on, then run pnpm cmd liquidationFlow -d -p 0.1 -c <collateralAmt> <accountToLiquidate>
    • This will do the following steps:
      • approve AuctionUtils contract to use the bidding subaccount
      • bid on the account, creating a new subaccount with added collateral
      • With the -m (merge) flag:
        • transfer all the new subaccount assets back into the bidderSubaccount
      • With the -d (deposit) flag:
        • deposit the newly generated subaccount to the exchange
        • <TODO: move assets from this new subaccount to the trading subaccount>
        • <TODO: delete the new subaccount reference from the exchange>

Flagging accounts

https://gist.github.com/0xdomrom/663e6a83ea08a439497c99a49770d314

Here is the logic for scraping all subaccounts + fetching balances + margin requirements (using the contract above for margin reqs). Wont work as is, has a bunch of imports I'd need to compile into a single file to work.

This also:

  1. doesnt do any event caching for the balance fetching
  2. doesn't parallelise the margin checks
    which both would improve efficiency tremendously

Logic starts at FlagAccounts.run_job().

Once you've figured out that an account is below margin requirements, you can atomically flag + bid using the function in the utils contract, or you can flag and then wait for a larger discount.

Testnet

This whole process can be run on testnet. Follow the same instructions but use .env.testnet.example instead of .env.mainnet.example

Testnet constants

Explorer URL: https://explorerl2new-prod-testnet-0eakp60405.t.conduit.xyz/

RPC URL: https://l2-prod-testnet-0eakp60405.t.conduit.xyz

ChainID: 901

AUCTION_ADDRESS=0x026dD5F94275faa74E41b16fea68f664d1ec68cC
SUBACCOUNT_ADDRESS=0xb9ed1cc0c50bca7a391a6819e9cAb466f5501d73
AUCTION_UTILS_ADDRESS=0x4fCc6b8F7887B39446555b5AaB0415D859d299BC
Select a repo