> For the complete documentation index, see [llms.txt](https://docs.rfy.finance/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.rfy.finance/resources/trader-integration/settlement-reference.md).

# Settlement Reference

```
If PnL ≥ 0:
    Amount Returned = Borrowed + PnL

If PnL < 0:
    Amount Returned = Borrowed - |PnL|
```

Settlement call:

```
settle(int256 pnl)
```

### Settlement Checklist

Before calling `settle()`:

* [ ] Epoch duration has elapsed
* [ ] All contract interactions must use **base units (raw token amounts)**.
* [ ] Total borrowed amount is accurately tracked (If borrowed)
* [ ] Token approval is set for the vault contract

***

### Settlement Scenarios

#### 1. Borrowed Capital with Profit

| Parameter | Value   |
| --------- | ------- |
| Borrowed  | 8 USDC  |
| PnL       | +1 USDC |

**Conversion (USDC, 6 decimals):**

* Borrowed = `8,000,000`
* PnL = `1,000,000`
* Return = `9,000,000`

**Execution**

Transfer:

```
9000000
```

Call:

```
settle(1000000)
```

***

#### 2. Borrowed Capital with Loss

| Parameter | Value   |
| --------- | ------- |
| Borrowed  | 8 USDC  |
| PnL       | -2 USDC |

**Conversion (USDC, 6 decimals):**

* Borrowed = `8,000,000`
* PnL = `-2,000,000`
* Return = `6,000,000`

**Execution**

Transfer:

```
6000000
```

Call:

```
settle(-2000000)
```

{% hint style="warning" %}
Losses are reflected by returning less capital. No additional capital injection is required.
{% endhint %}

***

#### 3. No Borrowing with Profit

| Parameter | Value   |
| --------- | ------- |
| Borrowed  | 0       |
| PnL       | +1 USDC |

**Conversion (USDC, 6 decimals):**

* Borrowed = `0`
* PnL = `1,000,000`
* Return = `1,000,000`

**Execution**

Transfer:

```
1000000
```

Call:

```
settle(1000000)
```

***

#### 4. No Borrowing with Loss

This scenario is **not permitted as-is**.

If no capital was borrowed during the epoch, the trader cannot settle with a negative PnL directly, since the contract enforces that losses cannot exceed borrowed capital.

{% hint style="danger" %}
A negative `pnl` is only valid if there is sufficient recorded borrowed balance.
{% endhint %}

**Resolution**

If the strategy resulted in a loss but no capital had previously been borrowed, the trader must first borrow the required amount from the vault, then settle using the negative PnL value.

**Example**

| Parameter        | Value   |
| ---------------- | ------- |
| Initial Borrowed | 0 USDC  |
| Realized PnL     | -1 USDC |

Since `Borrowed = 0`, calling `settle(-1 USDC)` directly is invalid.

**Required process:**

1. Borrow the required amount
2. Approve the vault
3. Call settlement with the negative PnL

**Conversion (USDC, 6 decimals):**

* Borrow = `1000000`
* PnL = `-1000000`
* Return amount at settlement = `0`

**Execution**

Borrow:

```
borrow(1000000)
```

Approve:

```
approve(vault, 0)
```

Call:

```
settle(-1000000)
```

***

### Summary

* Settlement enforces final PnL onchain
* Return amount must reflect borrowed capital ± PnL
* All values must be provided in base units
* `settle(pnl)` finalizes the epoch


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.rfy.finance/resources/trader-integration/settlement-reference.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
