# liquidate

The liquidation process occurs when another user repays part of the outstanding amount of the borrower and thus purchases his collateral at a discount. To do this, user call **liquidate** method.

{% hint style="warning" %}
The `yToken` contract expects that underlying token infos of `collateralToken` and **all borrowed** by `borrower` before `tokenId`s  are updated by calling [PriceFeed.getPrice](https://yupana-finance.gitbook.io/yupana-document-portal/developer-space/pricefeed-contract/getprice) and [updateInterest](https://yupana-finance.gitbook.io/yupana-document-portal/developer-space/ytoken-contract-methods/updateinterest) in the same block **before** this contract method
{% endhint %}

### Types

```pascaligo
type liquidateParams    is [@layout:comb] record [
  borrowToken             : nat;
  collateralToken         : nat;
  borrower                : address;
  amount                  : nat;
]
```

|    Parameter    |   Type  | Description                                                                                |
| :-------------: | :-----: | ------------------------------------------------------------------------------------------ |
|   borrowToken   |   nat   | yToken identifier of borrow token market                                                   |
| collateralToken |   nat   | yToken identifier of collateral token market                                               |
|     borrower    | address | address of borrower to liquidate                                                           |
|      amount     |   nat   | amount of yTokens borrow to pay with max amount is less or equal `borrow` \* `closeFactor` |

### Usage

{% tabs %}
{% tab title="🌮 Taquito" %}

```typescript
const collateralTokenId = 0; // or new BigNumber(0) or "0"
const borrowTokenIdToClose = 2;
const amount = 10_000_000; // amount of collateral.
const borrower = "tz...";
const yupana = await tezos.contract.at(yTokenAddress);
const proxy = await tezos.contract.at(proxyAddress);
const borrowedTokenIds = [1, 2];
const updBorrowed = borrowedTokenIds.reduce(
  (batch, tokenId) => {
    batch.push({
        kind: "transaction",
        ...yupana.methods.updateInterest(tokenId).toTransferParams(),
      },
      {
        kind: "transaction",
        ...proxy.methods.getPrice([tokenId]).toTransferParams(),
      });
      return batch;
    }
const batchArray = [
      ...updBorrowed,
      {
        kind: "transaction",
        ...yupana.methods.updateInterest(collateralTokenId).toTransferParams(),
      },
      {
        kind: "transaction",
        ...proxy.methods.getPrice([collateralTokenId]).toTransferParams(),
      },
      {
        kind: "transaction",
        ...yupana.methods
          .liquidate(borrowTokenIdToClose, collateralTokenId, borrower, amount)
          .toTransferParams(),
      },
    ];
const batch = await tezos.wallet.batch(batchArray);
const operation = await batch.send();
await operation.confirmation();
```

{% endtab %}

{% tab title="🐍   Pytezos" %}

```python
collateral_token_id = 0
borrow_token_id = 2
amount = 10_000_000 # amount of underlying tokens.
borrower_address = "tz..."
yupana = ContractInterface.from_michelson(code) # or client.contract(contract_address)...
proxy = ContractInterface.from_michelson(prx_code) # or client.contract(prx_contract_address)...
borrow_ids = [1, 2] # user borrowed tokens
borrow_updates = [yupana.updateInterest(borrow_token_id) for borrow_token_id in borrow_ids]
borrow_updates.append(proxy.getPrice([borrow_token_id for borrow_token_id in borrow_ids]))
call = pytezos.bulk(
        *borrow_updates,
        yupana.updateInterest(token_id),
        proxy.getPrice([token_id]),
        yupana.liquidate(
                borrow_token_id, 
                collateral_token_id, 
                borrower_address, 
                amount
        )
    ).autofill().sign()
opg = call.inject()


```

{% endtab %}
{% endtabs %}

### Errors

* `yToken/borrower-cannot-be-liquidator` - borrower can't be liquidator
* `yToken/liquidation-not-achieved` - this debt is not in "liquidation" case.
* `yToken/debt-is-zero` - user's debt is zero.
* `yToken/too-much-repay` - too many tokens sent to repay.
* `underflow/borrowerAccount.borrow` - not enough `borrow` of borrower to liquidate with `amount` wanted by user.
* `underflow/totalBorrowsF` - total borrows less than `amount`.
* `yToken/no-such-collateral` - `borrower`  doesn't has collateral in token with `collateralToken`
* `yToken/seize/not-enough-tokens` - seize tokens greater than borrower balance.
* `token/cant-get-contract-token` - FA12 token contract address does not contain `transfer` entrypoint from FA12 interface.
* `token/cant-get-contract-fa2-token` - FA2 token contract address does not contain `transfer` entrypoint from FA2 interface.
* `underflow/liquidity - reserves` - liquidity more than reserves.
* `yToken/amount-is-zero` - passed zero `amount`.
* `ceil-div-error` - division of two numbers fails.
* `yToken/need-update` - token price and interest not updated (see <mark style="color:orange;">warning</mark> above)
* `yToken/borrow-id-undefined` - borrow token identifier is not assigned to any known yTokens.
* `yToken/collateral-id-undefined` - collateral token identifier is not assigned to any known yTokens.
