Robert Zaremba
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # Hackmos 2023 ## Problem Statement Lending systems and leverage trading (like perps) have liquidation when a position goes to an unhealthy state. For lending systems this happens when the Loan To Value has crossed a liquidation threshold. For perps, liquidation happens when there is no margin (or the margin is too low). Liquidation reduces (or closes) a trader position, to protect a system from unhealthy debts. Usually, a liquidator buys out the trader position and receives a premium from the trader collateral. Liquidations brings around 5% immediate profit. For example, liquidating a $100k position will bring $5k immediately. If that happens every hour, that would make `5*24*365/100 * 100% = 43800%` APY! ## Context Cross chain liquidations are complex, they require: + Moving liquidity from a chain where a user has liquidity to another one (where there is a liquidation option). + An overview of available liquidation options across all chains. Today, this requires lot of work by querying the chains, moving the liquidity and executing the liquidation. ## Proposal + A unified, user friendly dashboard to list available liquidations across different chains. + Use IBC to orchestrate the liquidity transfer from one chain to another and automatically liquidate. ## Proof of Concept We will use: 1. Umee for both a liquidity (lending) provider and a potential liquidation target (borrowing). 2. Nibiru for both an orchestrator (smart contract) and a potential liquidation target (perps DEX). ### What Alice, Bob and Charlie supply money in Umee, the biggest Cosmos lending system. Alice and Bob supply Umee. Charlie collateralizes Atom and Umee and borrows Juno. Dylan trades perps (SC <> USD) in Niburu with Atom as a collateral. `SC` is a coin registered in Niburu x/bank. Dylan and Charlie are overly optimistic and they put their positions under the water. The dashboard displays 2 liquidation targets: 1. Dylan's to liquidate SC. - Liquidator has to repay `SC` and he receives `Atom` (with extra %5 of Atom). 2. Charlie's to liquidate Juno. (For the example: Atom has a constant price: $10, and Umee: $0.04, while Juno drops from $3 to $0.3). - Liquidator repays `Juno` and has an option to get a reward of Umee or Atom. For the first liquidation, Alice or Bob borrow Atom (on the Umee Chain), against the Umee collateral they have, send it to Nibiru and liquidate Dylan's SC. Note: in a later step they should sell SC. ## How + IBC over cosmwasm to orchestrate liquidity provisioning. + DeFi adapters: set of contracts to interact with different DeFi protocols to browse and liquidate. + Liquidation Targets API (which queries available liquidaiton targest for the UI). + ABCI++ Handlers to organize transactions, adjust liquidaiton so every transaction will get in, making it fair and available for everyone. ## ABCI++ track Liquidations are very profitable transactions. However, liquidaitons suffer heavily from MEV!!!! The goal is to use a block proposer to reorder and adjust transaction in a block to allow that all liquidation requests will be executed. + we created a custom **Mempool** which prioritizes and puts at the front `MsgLiquidate` transactions + **`PrepareProposal`** computes total liquidation that is being made in the block, and inserts adjustmet data in the transaction chain of the block in case there are more liquidators than available targets (by total volume to liquidate). + **`ProcessProposal`** that tries to extract the adjustment number, recalculates the total liquidation amount in transactions in the block and check if it works. + **`PreBlock`** handler to inject the adjustment info in the keeper. ## Deliverables The liquidation aggregation is done through IBC using polytone contract to exchange packets between chains. There are liquidation adapter contracts on each of the chains aggregated. Contracts: 1. Controller contract: The contract keep the funds deposited by users to be used for liquidations. The admin can trigger a check where the controller queries the adapters to multiple chains if there is an open position up for liquidation. If there is such position found, the funds are IBCed over to the adapter chain and the adapter performs the liquidation and returns the liquidated collateral back over to the controller chain. 2. Umee liquidation adapter: The contract performs liquidation on Umee on behalf of the controller, since the controller contract sits on Umee, it does not require funds to be sent over IBC and the liquidation can be performed within the same chain atomically (transfer of funds + liquidation action), so there is no requirement of sending back funds to controller chain. 3. Nibiru liquidation adapter: The contract performs liquidations on Niburu chain for the perps positions that are up for liqudiation, the condition for that is the perp position is low on margin. In Nibiru, meeting the margin requirement means maintaining a margin ratio on the position that exceeds the maintenance margin ratio (6.25%), which is the minimum margin ratio that a position can have before being liquidated. 4. Mars liquidation adapter: The contract keeps track of all the accounts that have a loan position in Mars contracts deployed on Osmosis. Whenever the contract receives liquidate message from controller contract, it checks the position and if the position is still open, it performs the liquidation and returns the collateral to controller or else with return the funds sent by controller back to it. The adapter calls the `red_bank` contract on Osmosis and abstracts the calls to a genralised txn calls for controller contract. a. `LiquidatePosition()`: It calls the `Liquidate()` msg of `red_bank` to liquidate a position. If successful, collateral is received by the adapter from red bank, else the sent funds remain in the adapter. b. `DepositAndBorrow()`: Deposits the collateral and borrows the asset from red bank. c. `WithdrawCollateral()`: Repay the asset loan and withdraw the collateral from red bank. ### Deploy polytone 1. Clone the repository, `git clone https://github.com/udit-gulati/polytone` 2. Install wasmkit, `cd polytone && yarn install` 3. Compile contracts to generate wasm binary and schema files, `npx wasmkit compile` 4. Run the deploy script, `npx wasmkit run scripts/deploy.ts --network umee` and `npx wasmkit run scripts/deploy.ts --network nibiru` 5. If deployed correctly, the contract address and code id for 3 contracts (note, voice and proxy) for each network should show up in `artifacts/checkpoints/` directory as `.yaml` files. ### Nibiru + Nibiru has an SDK module for Perps and Cosmowasm for deploying contracts. + So, we can have a contract which will call a native module and trigger a liquidation. ### Umee 1. Note: * codeId: 1 * contractAddress: umee14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9scsdqqx 2. Proxy: * codeId: 3 * contractAddress: umee1qg5ega6dykkxc307y25pecuufrjkxkaggkkxh7nad0vhyhtuhw3shmlsys 4. Voice: * codeId: 2 * contractAddress: umee1xr3rq8yvd7qplsw5yx90ftsr2zdhg4e9z60h5duusgxpv72hud3scz87vz 5. Liquidation adapter: * codeId: 4 * contractAddress: umee1ghd753shjuwexxywmgs4xz7x2q732vcnkm6h2pyv9s6ah3hylvrqsgm7t9 ### Nibiru 1. Liquidation adapter: * codeId: 1 * contractAddress: nibi14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9ssa9gcs ### Deployed endpoints UMEE: RPC: 35.246.147.222:36657 GRPC: 35.246.147.222:8090 GRPC-WEB: 35.246.147.222:8091 REST: 35.246.147.222:2317 Key mnemonic: only song turkey pelican settle claw loud glass raccoon school alert powder clap suit harvest pizza street split point swallow filter element carry memory NIBIRU: RPC: 35.246.147.222:26657 GRPC: 35.246.147.222:9090 GRPC-WEB: 35.246.147.222:9091 REST: 35.246.147.222:1317 ## Prerequisits Deploy two local chains and connect them via IBC using a relayer. Chain 1: nibiru (https://github.com/NibiruChain/nibiru) Chain 2: umee (https://github.com/umee-network/umee) Relayer: hermes (https://github.com/informalsystems/hermes/) ## What's going for v2 + Liquidation delay to allow more IBC tx aggregation. + Bot with subscription to automatically trigger liquidations.

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully