# SigmaFi pricing

> How SigmaFi prices loans, with the yield convention, two-point yields, lend offer curves, window orders, fees, share prices and auction prices, and worked examples.

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

**Level:** Advanced

*Assumes you are comfortable with percentages and simple formulas, and have read the [SigmaFi overview](https://lithosprotocol.org/docs/sigmafi/overview).*

This page gives the exact rules SigmaFi uses to price loans, bonds and shares, with a worked example for
each. Every rule here is checked by a contract or an expression on chain, except where it says a client
chooses. Which expression a box uses is on [Contracts](https://lithosprotocol.org/docs/sigmafi/contracts).

Pricing is either **fixed** or **yield**. A fixed price is a number the owner commits to. A yield price is
worked out at the moment of the trade from the yield the owner commits to.

## The yield convention

Every SigmaFi yield is a **simple return in basis points per 262,800 blocks**, which is one year of
2-minute blocks. For a bond repaying R with D blocks to maturity, bought for P:

```text
yield = (R / P - 1) × 262,800 / D
```

The following hold for every yield on SigmaFi:

- It is simple, never compounded.
- It is measured in blocks, not calendar time. Ergo's block time varies.
- It assumes full repayment at maturity. A liquidated bond pays its collateral instead.
- 100 basis points are 1%, so 1,000 bps is 10%.

Contracts never divide. Each test below multiplies both sides out in `BigInt` and compares, so the
boundary is exact to one raw unit.

## Two-point yields

A yield-priced order or ask commits two points in time:

```text
(start height h0, start yield y0, end height h1, end yield y1),   h1 > h0
```

The yield is y0 up to h0, y1 from h1 on, and on the straight line between them in between. Equal yields
give one fixed rate. Clients put h0 at the current height and h1 at the box's expiry. With:

```text
B  = 10,000 × 262,800
L  = h1 - h0
at = HEIGHT, clamped to [h0, h1]
Y  = y0 × L + (y1 - y0) × (at - h0)          the yield at HEIGHT is exactly Y / L
D  = maturity - HEIGHT
F  = R for a whole bond, or R × units / N for shares
```

the least principal or payment satisfies:

```text
paid × (B × L + Y × D) ≥ F × B × L          that is,   paid ≥ F / (1 + yield × D / 262,800)
```

A rising yield makes the price fall while the box waits. A falling yield makes it rise. Any fixed floor
the owner set still binds.

**Example.** A borrower posts at height 1,900,000 with a yield moving from 800 bps to 1,200 bps by the
order's expiry at 1,905,040. At height 1,902,520, halfway, `Y = 800 × 5,040 + 400 × 2,520`, so the yield is
`Y / L = 1,000` bps, 10%. A bond repaying 110 with 131,400 blocks left then needs a principal of at least
`110 / (1 + 0.10 × 131,400 / 262,800) = 104.7619`.

Because the price can move between signing and inclusion, a client pays the most the price asks at any
height from now to 3 blocks ahead.

## Priced orders

A priced order prices **only the principal**. The repayment, the collateral, the maturity rule, the
liquidation rule and the fees never move. The principal stored in the order is a floor. The bond records
the principal actually paid:

- ERG: the payout's nanoERG, less the refunded expiry fee.
- SigUSD: the payout's SigUSD exactly. The small ERG beside it stays as committed.

Paying above the minimum is allowed, which gives the lender room for the inclusion height to move.

## Fixed or yield: choosing for an order

How the rate behaves while an order waits depends on its timing and pricing:

| Timing | Pricing | While the order waits |
|---|---|---|
| Term from the fill | Fixed | Principal, repayment and duration stay the same, so the rate does too |
| Term from the fill | Yield | Duration stays the same. Only a moving yield changes the quote |
| Fixed maturity | Fixed | Fewer blocks remain, so the rate the borrower pays rises |
| Fixed maturity | Yield | The principal follows the fixed repayment at the stated yield as blocks run out |

A borrower who thinks in a target rate can work out the repayment: `R = P × (1 + yield × D / 262,800)`,
rounded up to a raw unit.

## The lend offer's yield curve

A lend offer holds a minimum yield and up to 20 points of (yield, blocks left to maturity), blocks
strictly increasing. For a bond with D blocks left the yield it requires follows these rules:

- Between two neighbouring points, it is on the straight line joining them.
- Below the first point it is the first point's yield, above the last it is the last's.
- It is never below the minimum yield. With no points, the minimum is the one yield.
- The offer's least and most blocks to maturity bound what it buys at all.

On a segment from (y1, D1) to (y2, D2), with `L = D2 - D1` and `Y = y1 × L + (y2 - y1) × (D - D1)`, the
offer buys when:

```text
R × B × L ≥ cost × (B × L + Y × D)
```

For u of N shares, R is replaced by `R × u / N`. On an ERG offer, the cost includes the batcher fee. The
curve is keyed by blocks left, so it rolls with time: a resting offer keeps bidding the same rate for a
30-day bond whatever day it is.

**Example.** An offer with a 5% minimum and points 6% at 21,600 blocks, 8% at 131,400 and 10% at 262,800
meets a bond repaying 107 with 76,500 blocks left. That is halfway along the first segment, so it requires
7%. The most it pays is `107 / (1 + 0.07 × 76,500 / 262,800) = 104.8632`. On an ERG offer with a
0.005 ERG batcher fee, the price itself may be at most 104.8582.

## Window orders

A window order lets the lender pick the maturity, any duration from the borrower's minimum to their
maximum. The borrower commits a curve: a cap and up to 20 points of (yield, blocks left). It is the offer's
curve read the other way round: each point is the **most** the borrower pays, and the cap is a ceiling.
With D the blocks to the chosen maturity, the yield is the curve's at D, capped, and:

```text
principal × (B × L + Y × D) ≥ R × B × L          that is,   principal ≥ R / (1 + yield × D / 262,800)
```

The repayment is fixed, so a longer maturity pays the borrower less now. The principal floor still binds
at every maturity.

**Example.** A borrower repays 6, with a 12% cap and points 8% at 21,600 blocks and 12% at 131,400. A
lender who picks 43,200 blocks gets a curve yield of 8.787%, so the principal is at least 5.9146. At
131,400 blocks the yield is 12% and the principal at least 5.6604.

When a lend offer fills a window order, zcs1 fixes the maturity a batcher picks. See
[Batching](https://lithosprotocol.org/docs/sigmafi/offchain-standard#batching).

## Fees and yields

A borrower's or seller's yield is the most they offer, which sets a least price. A lender's yield is the
least they accept, which sets a most cost. Ignoring fees, an ask offering 10% crosses a bid requiring 8%,
and one offering 6% does not.

Fees change that in the following ways:

- On an ERG lend offer, the cost is the price plus the batcher fee.
- A SigUSD offer pays its fees in ERG, separately, with a reward per fill proportional to the capital
  filled and a cap on the total.
- On a short or small loan the batcher fee can exceed the interest, so equal rates may not match. On a
  60-block loan repaying 0.12 ERG, leaving room for a 0.005 ERG fee takes a yield in the tens of thousands
  of percent.

## Share prices

Shares are quoted as a **percentage of face**. A price p for u raw shares of a bond repaying R, with N raw
shares in all, is:

```text
percentage of face = p × N / (u × R)
```

**Example.** 10 shares (10,000,000 raw) of a bond repaying 105.5 SigUSD, offered for 10.20 SigUSD, are at
`10.20 × 100,000,000 / (10,000,000 × 105.5) = 96.68%` of face.

## Auction prices

Auctions use straight-line prices over blocks, and each price only falls:

| Auction | Price at a height |
|---|---|
| Lot auction | The opening until the start, falling in a straight line to the floor at the end, then the floor until the expiry |
| Share vault, floor auction | A target for the vault's currency: the repayment times a basis-point figure falling from the opening to the floor over the length, then holding. A buyer brings the vault up to the target |
| Share vault, excess auction | The opening times the repayment, falling to zero over the length. A sale must pay more than zero |

A falling price means a purchase priced now stays valid when it lands, so clients buy at the current
height's price.

## Next steps

- [Lending](https://lithosprotocol.org/docs/sigmafi/lending) and [Borrowing](https://lithosprotocol.org/docs/sigmafi/borrowing): where these prices meet in practice.
- [The zcs1 off-chain standard](https://lithosprotocol.org/docs/sigmafi/offchain-standard): the matching order and pricing-ahead rules batchers follow.
- [Contracts](https://lithosprotocol.org/docs/sigmafi/contracts): which expression checks which formula.
