---
# System prepended metadata

title: Alpha liquidation contract

---

# Alpha liquidation contract

Basically, the concept of this contract is that user can liquidate any position by sending UST to this helper contract to liquidate any unhealthy position atomically.

### Query function

**LoanInfo**

Msg format:
```
{
  "loan_info": {
      "repay_assets": [
          {
              "asset": {
                  "info": AssetInfo, // AssetInfo type
                  "amount": Uint128 // amount to repay
              }
              "ust_token_pair_address": Optional<String> // pair address between ust and repay-token (any DEXes)
          },
          ...
      ] // list must be in order according to tokenA and tokenB
  }
}
```

   To call this function, user have to input token address, token amount and pair address from any DEXes (to swap between UST and token) as a list.
   This function will calculate

   1. `loan_amount`: how much UST the contract requires for liquidation

      - loop through each repay asset and calculate how much input amount for exact output tokens using reverse_simulation from pair contract and sum all input amounts as `loan_amount`

   2. `loan_fee`: how much fee Whitewhale takes by using `loan_amount` (from step 1.) multiplied by `flash_loan_fee` 0.001% (in case, you execute by flash_liquidate)

   User should call this function before calling execute function since he/she should need to know how much UST to attach.

### Execute function

There are two modes to execute this contract

- liquidate (using own UST balance)

  User just sends `loan_amount` from query function and additional information to liquidate position such as pair_address, farm_address, position_id or etc.
  
```
{
   "liquidate": {
        "farm_addr": String, // leverage-farm contract address
        "pair_addr": String, // pair contract address
        "liquidity_token_addr": String, // lp address of pair contract
        "repay_assets": [
              {
                  "asset": {
                      "info": AssetInfo, // AssetInfo type
                      "amount": Uint128 // amount to repay
                  }
                  "ust_token_pair_address": Optional<String> // pair address between ust and repay-token (any DEXes), null if asset is UST
              },
              ...
         ] // list must be in order according to tokenA and tokenB
        "user": String, // user address of liquidable position
        "position_id": u64, // position id from leverage-farm contract
    }
}
```

**How it works**

  0.  User sends UST to the contract
  1.  Contract swaps UST to token A (or B) to prepare for repaying the position debt.
  2.  Contract executes liquidation on leverage-farm contract. Repay the repay-token (can be UST and/or token A/B) and receive LP back to the contract (with certain liquidation premium).
  3.  Contract removes LP liquidity and gets the underlying base tokens.
  - NOTE: This process can be unsuccessful in case LP amount is very very low. In this case, it'll skip removing liquidity and simply return the LP token to the liquidator. [**alt section will be skipped**]. 
  4.  Contract swaps non-UST tokens received back to UST
  5.  Return all UST to the liquidator (or LP token in case **alt** was unsuccessful).

  ![plot](out/docs/liquidate/liquidate.png)

- flash_liquidate (requesting UST loan from Whtiewhale's vault)

  User sends only `loan_fee` (in native UST) from query function and additional information to liquidate position such as pair_address, farm_address, position_id or etc.
  
```
{
   "flash_liquidate": {
        "farm_addr": String, // leverage-farm contract address
        "pair_addr": String, // pair contract address
        "liquidity_token_addr": String, // lp address of pair contract
        "repay_assets": [
              {
                  "asset": {
                      "info": AssetInfo, // AssetInfo type
                      "amount": Uint128 // amount to repay
                  }
                  "ust_token_pair_address": Optional<String> // pair address between ust and repay-token (any DEXes), null if asset is UST
              },
              ...
         ] // list must be in order according to tokenA and tokenB
        "user": String, // user address of liquidable position
        "position_id": u64, // position id from leverage-farm contract
    }
}
```

**How it works**

  0.  User sends UST (only fee) to the contract
  1.  Contract calculates how much UST it requires for liquidation and then requests specific UST loan from Whitewhale's vault contract
  2.  Contract swaps UST to token A (or B) to prepare for repaying the position debt.
  3.  Contract executes liquidation on leverage-farm contract. Repay the repay-token (can be UST and/or token A/B) and receive LP back to the contract (with certain liquidation premium).
  4.  Contract removes LP liquidity and gets the underlying base tokens.
  - NOTE: This process can be unsuccessful in case LP amount is very very low. In this case, it'll skip removing liquidity and simply return the LP token to the liquidator. [**alt section will be skipped**]. 
  5.  Contract swaps non-UST tokens received back to UST
  6.  Contract repays loan + fee amount to Whitewhale's vault contract and returns remaining UST back to user
      ![plot](out/docs/flash_liquidate/flash_liquidate.png)
