> ## Documentation Index
> Fetch the complete documentation index at: https://docs.mystockline.xyz/llms.txt
> Use this file to discover all available pages before exploring further.

# PositionRouter

> PositionRouter bundles common position flows into one transaction: open (permit, supply, borrow), close (repay, withdraw), and deleverage (sell collateral, repa

PositionRouter bundles common position flows into one transaction: open (permit, supply, borrow), close (repay, withdraw), and deleverage (sell collateral, repay). Positions always stay on the user's account in Stockline; the router holds nothing between calls and sweeps leftovers back to the caller. It also implements the deleverage callback that sells collateral for USDG along the registry's zap route.

`PositionRouter` lets you:

* Supply collateral and borrow USDG in one transaction, with an optional permit
* Repay and withdraw in one transaction, using max sentinels for full close
* Sell collateral and repay debt atomically

Addresses per network. Every address is also on [Addresses](/resources/addresses).

| Network                 | Address                                                                                                                                       |
| ----------------------- | --------------------------------------------------------------------------------------------------------------------------------------------- |
| Robinhood Chain testnet | [0x4674e8f34790Ee368A004f7469e825DCD61bE942](https://explorer.testnet.chain.robinhood.com/address/0x4674e8f34790Ee368A004f7469e825DCD61bE942) |

The source code is [`src/periphery/PositionRouter.sol`](https://github.com/stockline-xyz/contracts/blob/main/src/periphery/PositionRouter.sol) on GitHub. The ABI is [`/abis/PositionRouter.json`](/abis/PositionRouter.json).

## Write methods

### openPosition

```solidity theme={"system"}
function openPosition(
    uint16 bookId,
    uint256 supplyAmount,
    uint256 borrowAmount,
    PermitData calldata permit
) external nonReentrant
```

Optionally applies an ERC-2612 permit, pulls `supplyAmount` collateral from the caller, supplies it to the caller's account, and borrows `borrowAmount` USDG to the caller if non-zero. Reverts with ZeroAmount for a zero supply. Borrow reverts follow Stockline rules (LIVE, unpaused, caps, LTV).

<Warning>
  The caller must approve the router for the collateral, either through the permit or a prior approval. If `borrowAmount` is non-zero the caller must first set the router as operator with `Stockline.setOperator`.
</Warning>

**Input parameters**

| Name         | Type         | Description                                                                                                                                             |
| ------------ | ------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------- |
| bookId       | `uint16`     | The book to open in.                                                                                                                                    |
| supplyAmount | `uint256`    | Collateral tokens to supply.                                                                                                                            |
| borrowAmount | `uint256`    | USDG to borrow. 0 skips the borrow.                                                                                                                     |
| permit       | `PermitData` | PermitData struct: `value`, `deadline`, `v`, `r`, `s`. A `deadline` of 0 skips the permit. Permit failures are ignored, so a prior approval also works. |

### closePosition

```solidity theme={"system"}
function closePosition(
    uint16 bookId,
    uint256 repayAmount,
    uint256 withdrawAmount
) external nonReentrant
```

Pulls `repayAmount` USDG from the caller and repays their book, then withdraws `withdrawAmount` collateral to them. Passing the maximum uint256 for either value uses the full debt or full collateral. Reverts with ZeroAmount when both are 0.

<Warning>
  The caller must approve the router for USDG and, if withdrawing, set the router as operator. Withdraw needs LIVE regime and unpaused state; repay always works.
</Warning>

**Input parameters**

| Name           | Type      | Description                                                                         |
| -------------- | --------- | ----------------------------------------------------------------------------------- |
| bookId         | `uint16`  | The book to close.                                                                  |
| repayAmount    | `uint256` | USDG to repay, or the maximum uint256 for all debt in the book.                     |
| withdrawAmount | `uint256` | Collateral to withdraw, or the maximum uint256 for all of it. 0 skips the withdraw. |

### deleverage

```solidity theme={"system"}
function deleverage(
    uint16 fromBook,
    uint256 amount,
    uint16 repayBook
) external nonReentrant
```

Calls `Stockline.deleverage` with this router as callee. The router sells `amount` of `fromBook` collateral for USDG along the token's zap route and the proceeds repay `repayBook`. Leftover collateral and USDG return to the caller. Works in every regime and while paused because it only reduces risk.

<Warning>
  The caller must first set the router as operator. The swap uses the registry's slippage cap against the router's TWAP quote; a thin market can revert.
</Warning>

**Input parameters**

| Name      | Type      | Description                       |
| --------- | --------- | --------------------------------- |
| fromBook  | `uint16`  | The book to sell collateral from. |
| amount    | `uint256` | Collateral tokens to sell.        |
| repayBook | `uint16`  | The book whose debt is repaid.    |

### onDeleverage

```solidity theme={"system"}
function onDeleverage(
    address,
    address account,
    uint16 bookId,
    uint256 amount,
    bytes calldata
) external override
```

Callback invoked by Stockline during `deleverage`. Sells the received collateral for USDG along its zap route, sends the USDG back to Stockline, and returns any collateral dust to the account. Reverts with NotCore for any other caller and InvalidRoute if the token has no allowed route ending in USDG.

<Warning>
  Core only. Do not call directly.
</Warning>

**Input parameters**

| Name    | Type      | Description                                |
| ------- | --------- | ------------------------------------------ |
| -       | `address` |                                            |
| account | `address` | The account whose collateral was released. |
| bookId  | `uint16`  | The book the collateral came from.         |
| amount  | `uint256` | Collateral tokens received.                |
| -       | `bytes`   |                                            |

## Events

### Opened

```solidity theme={"system"}
event Opened(
    address indexed account,
    uint16 indexed bookId,
    uint256 supplied,
    uint256 borrowed
)
```

Fires after openPosition, with the supplied and borrowed amounts.

### Closed

```solidity theme={"system"}
event Closed(
    address indexed account,
    uint16 indexed bookId,
    uint256 repaid,
    uint256 withdrawn
)
```

Fires after closePosition, with the resolved repay and withdraw amounts.

## Errors

### ZeroAddress

```solidity theme={"system"}
error ZeroAddress()
```

The core was zero at deployment.

### ZeroAmount

```solidity theme={"system"}
error ZeroAmount()
```

The supply, deleverage amount, or both close amounts were zero.

### InvalidRoute

```solidity theme={"system"}
error InvalidRoute()
```

The collateral token has no allowed zap route to USDG.

### NotCore

```solidity theme={"system"}
error NotCore()
```

Only Stockline can call onDeleverage.
