# Borrowing on SigmaFi

> Posting a SigmaFi borrow order, choosing its terms, and repaying, adding collateral or closing the loan early.

Source: https://lithosprotocol.org/docs/sigmafi/borrowing

**Level:** Advanced

*Assumes you know what a token and a block height are, and have read the [SigmaFi overview](https://lithosprotocol.org/docs/sigmafi/overview).*

This page covers everything a borrower does: posting an order, choosing each of its terms, and what you
can do once a lender fills it. It names the action in the Lithos web panel and the API route for each
step. Lending is on [Lending](https://lithosprotocol.org/docs/sigmafi/lending), and the maths behind priced orders is on
[Pricing](https://lithosprotocol.org/docs/sigmafi/pricing).

## Posting an order

An order locks your collateral and states the loan you want. It waits until a lender fills it, you
cancel it, or it expires. In the Lithos web panel, open **Extensions > SigmaFi**, then **Create >
Borrow**. Through the API, call `POST /sigmafi/zc/orders/check` to preview and `POST /sigmafi/zc/orders`
to send.

An order has the following terms. Every one is your choice, and no client fills one in for you except
where a default is named:

| Term | What it sets |
|---|---|
| Market | **ERG** or **SigUSD**: the currency you borrow and repay in |
| Principal | What you receive when the order fills |
| Repayment | What you repay by maturity. It never changes after you post |
| Timing | When the loan matures: a fixed height, a term counted from the fill, or a window the lender chooses inside. See [Choosing when the loan matures](#choosing-when-the-loan-matures) |
| Pricing | **Fixed** principal, or a principal that follows a **yield** while the order waits. See [Fixed or yield pricing](#fixed-or-yield-pricing) |
| Collateral | ERG and up to 30 kinds of token, locked until the loan ends |
| Liquidation rule | **Maturity** or **Oracle**. See [Choosing a liquidation rule](#choosing-a-liquidation-rule) |
| Fill guard | Oracle orders only: the least cover at which the order may fill. On by default |
| Expiry | The height from which anyone may return the order to you. Default 5,040 blocks (about 7 days), or half the term of a fixed maturity if that is shorter |
| Expiry fee | What a batcher earns for returning the order after it expires. Default 0.005 ERG, at least 0.002 ERG. When the order fills instead, the fee comes back to you with the principal |

In a SigUSD order the principal and repayment are SigUSD. Any ERG the order names beside them is at
most 0.001 ERG, enough to cover box minimums.

### Choosing when the loan matures

You choose one of the following timings:

| Timing | Maturity | When to use it |
|---|---|---|
| Fixed | A block height | You need the money back by a known date. A height at least 21,600 blocks away snaps to the nearest multiple of 21,600 (about 30 days), unless you keep it exact |
| Term | The fill's height plus a duration of at least 30 blocks | You want the same loan length whenever the order fills |
| Window | Any duration from your minimum to your maximum, chosen by the lender at the fill | You want the order to meet lenders at many maturities. Needs window pricing |

The snapping to multiples of 21,600 is a client default. Bonds that mature at the same height can be
pooled later, and every maturity stays fillable. The panel shows the snapped height before you sign.

### Fixed or yield pricing

With **fixed** pricing the lender pays exactly your principal. With a fixed maturity, the rate you pay
then rises the longer the order waits, because the same repayment covers fewer blocks.

With **yield** pricing you name the yield you pay instead. The principal is worked out at the fill from
your repayment and the blocks left, so the rate stays where you put it. The principal you enter becomes
a floor: you never receive less. You can also let the yield move in a straight line while the order
waits. For example, ask for 8% now, rising to 12% by the order's expiry, so the order gets easier to
fill the longer it waits.

**Window** pricing goes with window timing. You name a yield curve: the most yield you pay at each
maturity, up to 20 points, and a cap. The lender picks a maturity in your window, and the principal is
your repayment discounted at your curve's yield for that maturity. A longer maturity pays you less now,
because the repayment is fixed. The principal floor still binds at every maturity.

The formulas are on [Pricing](https://lithosprotocol.org/docs/sigmafi/pricing).

### Choosing a liquidation rule

You choose one of the following rules:

| Rule | Liquidated |
|---|---|
| Maturity | From the maturity height on, if not repaid |
| Oracle | From maturity on, and before it whenever the collateral is worth less than your **ratio** of the repayment at an ERG/USD oracle no older than your **oracle age** |

An oracle rule values the collateral against the **repayment**, not the principal, and leaves the
lender's fee reserve out. With a 150% ratio on a 100 SigUSD repayment, the loan can be liquidated as soon
as the collateral is worth less than 150 SigUSD. The default oracle age is 60 blocks.

An oracle loan lets a lender accept less margin, since the lender can liquidate early. It also means a
price drop can cost you the collateral before maturity. The oracle rule counts only the ERG and SigUSD in
the collateral, with SigUSD at $1. Other tokens add nothing to the cover it measures.

> **Danger: A liquidation takes all the collateral**
>
> However far the collateral's value is above the repayment, a liquidation sends all of it to the bond's
> holder. Leave room above your ratio, and repay before maturity.

### The fill guard

An order can sit open while the price moves. Without protection, a lender could then fill it into a bond
that can be liquidated at once. The **fill guard** stops that: the contract refuses a fill unless the new
bond's collateral is worth at least the guard's ratio at a fresh oracle.

Every oracle order gets the fill guard by default, at the rule's own ratio. You may raise it to keep room
after the fill. Turning it off is possible, and the client still refuses to post an unguarded order that
is already under its ratio. The panel shows the ERG price at which the guard stops fills.

## Before the order fills

You can cancel the order at any time: **Portfolio > Cancel**, or `POST /sigmafi/zc/boxes/{boxId}/cancel`.
Cancelling needs one of your own boxes at the address that posted the order.

From the expiry height on, anyone may return the order to you and keep the expiry fee. You get the
collateral back either way.

## When the order fills

A fill pays your principal to you, plus the unused expiry fee, and creates the bond. The bond holds your
collateral and the **fee reserve**, ERG the lender adds to pay whoever liquidates. The default reserve is
0.05 ERG. The bond records its start height and maturity.

From then on the collateral leaves the bond in one of three ways: you repay, you extinguish the loan, or
someone liquidates it when the rule allows.

## Repaying

Repay before maturity: **Portfolio > Repay**, or `POST /sigmafi/zc/bonds/{assetId}/repay`. The
repayment and the lender's fee reserve go to the bond's vault, and all your collateral comes back to
you. Anyone may repay a bond, but the collateral always returns to the borrower.

**Repaying from the collateral.** If your collateral holds the repayment currency (ERG on an ERG loan,
SigUSD on a SigUSD loan), you can pay part or all of the repayment out of it and bring only the
shortfall. Set `fromCollateral` to an amount or to `max`. Only the borrower can do this.

> **Warning: Repay before the maturity height**
>
> A repayment must land in a block below the maturity. From the maturity height on, anyone may liquidate
> the loan instead, and batchers do so within a block or two.

## Adding collateral

Add collateral to move an oracle loan away from its ratio: **Portfolio > Add collateral**, or
`POST /sigmafi/zc/bonds/{assetId}/recollateralize`. You may add the following:

- ERG.
- More of a token the bond already holds.
- On a SigUSD loan holding none, SigUSD.

No other new token can be added, and nothing can be taken out. Only the borrower can add collateral, at
most once every 60 blocks, counted from when the bond last moved. A loan that matures within 60 blocks of
its start can never take more collateral, so add margin before you post.

## Closing a loan early

If you hold the circulating unit of your own loan, you can **extinguish** it at any height, before or
after maturity: **Portfolio > Extinguish**, or `POST /sigmafi/zc/bonds/{assetId}/extinguish`. All three
units of the bond token burn, and the collateral and the fee reserve come back to you. You pay no
repayment.

You get the unit by buying it, for example at a discount from an ask. Listings that sell a claim on your
own loan carry `borrowedByMe` in the API and **Your loan** in the panel, with a **Buy back** action:

- Buy the unit first, then extinguish from **Portfolio** or the bond's page.
- A yield-priced ask reads the live bond while you buy, so the purchase and the extinguish are always
  two transactions.
- If the bond was split into shares, buy all 100 shares, then use **Un-fractionalize** to get the unit
  back before extinguishing.

## While the loan runs

The following happen without you:

- **Refresh.** After about two years (525,600 blocks), anyone may re-create the bond unchanged for a
  bounty of up to 0.01 ERG out of its value, so storage rent never reaches it. A refresh is not allowed in
  the 720 blocks before maturity, and it leaves you free to add collateral at once.
- **Liquidation.** Batchers watch every loan and liquidate it the moment the rule allows. The fee reserve
  pays them.

## Next steps

- [Lending](https://lithosprotocol.org/docs/sigmafi/lending): what the other side of your loan does.
- [Trading bonds and shares](https://lithosprotocol.org/docs/sigmafi/trading): buying back a claim on your loan.
- [Pricing](https://lithosprotocol.org/docs/sigmafi/pricing): how yield and window orders price the principal.
- [The Lithos client and panel](https://lithosprotocol.org/docs/extensions/sigmafi-bonds): the panel views these actions live in.
