# PtHandler

[Git Source](https://github.com/Ion-Protocol/ion-protocol/blob/88cc595825f1dc2eb738fb93e172a3e8ab7a5c43/src/flash/PtHandler.sol)

**Inherits:** IonHandlerBase, IPMarketSwapCallback

This contract allows for easy creation of leverage positions for PT collateralized Ion markets.

### [State Variables](broken://pages/JEMAIzpUvknk4JMMfRz2) <a href="#state-variables" id="state-variables"></a>

#### [MARKET](broken://pages/JEMAIzpUvknk4JMMfRz2) <a href="#market" id="market"></a>

```
IPMarketV3 public immutable MARKET;
```

#### [SY](broken://pages/JEMAIzpUvknk4JMMfRz2) <a href="#sy" id="sy"></a>

```
IStandardizedYield public immutable SY;
```

#### [PT](broken://pages/JEMAIzpUvknk4JMMfRz2) <a href="#pt" id="pt"></a>

```
IERC20 public immutable PT;
```

#### [YT](broken://pages/JEMAIzpUvknk4JMMfRz2) <a href="#yt" id="yt"></a>

```
IERC20 public immutable YT;
```

#### [flashswapInitiated](broken://pages/JEMAIzpUvknk4JMMfRz2) <a href="#flashswapinitiated" id="flashswapinitiated"></a>

```
uint256 private flashswapInitiated = 1;
```

### [Functions](broken://pages/JEMAIzpUvknk4JMMfRz2) <a href="#functions" id="functions"></a>

#### [constructor](broken://pages/JEMAIzpUvknk4JMMfRz2) <a href="#constructor" id="constructor"></a>

Creates a new `PtHandler` instance

```
constructor(
    IonPool pool,
    GemJoin join,
    Whitelist whitelist,
    IPMarketV3 _market
)
    IonHandlerBase(0, pool, join, whitelist);
```

**Parameters**

| Name        | Type         | Description                |
| ----------- | ------------ | -------------------------- |
| `pool`      | `IonPool`    | The related IonPool.       |
| `join`      | `GemJoin`    | The related GemJoin.       |
| `whitelist` | `Whitelist`  | The whitelist contract.    |
| `_market`   | `IPMarketV3` | The related Pendle market. |

#### [ptLeverage](broken://pages/JEMAIzpUvknk4JMMfRz2) <a href="#ptleverage" id="ptleverage"></a>

Allows a borrower to create a leveraged position on Ion Protocol

*Transfer PT from user -> Flashswap PT token -> Deposit all PT into IonPool -> Borrow base asset -> Mint SY using base asset -> Repay Flashswap with SY.*

```
function ptLeverage(
    uint256 initialDeposit,
    uint256 resultingAdditionalCollateral,
    uint256 maxResultingDebt,
    uint256 deadline,
    bytes32[] calldata proof
)
    external
    onlyWhitelistedBorrowers(proof)
    checkDeadline(deadline);
```

#### [swapCallback](broken://pages/JEMAIzpUvknk4JMMfRz2) <a href="#swapcallback" id="swapcallback"></a>

This function should never be called directly.

*On small enough swaps, the SY to send back can be 0. This function can only be called by the market. This function can only be called by market if the swap was initiated by this contract.*

```
function swapCallback(int256 ptToAccount, int256 syToAccount, bytes calldata data) external;
```

**Parameters**

| Name          | Type     | Description                                                                                                           |
| ------------- | -------- | --------------------------------------------------------------------------------------------------------------------- |
| `ptToAccount` | `int256` | Amount of PT sent from the perspective of the pool (negative means pool is sending, positive means user is receiving) |
| `syToAccount` | `int256` | Amount of SY sent from the perspective of the pool (negative means pool is sending, positive means user is receiving) |
| `data`        | `bytes`  | Arbitrary data passed by the market                                                                                   |

### [Errors](broken://pages/JEMAIzpUvknk4JMMfRz2) <a href="#errors" id="errors"></a>

#### [InvalidGemJoin](broken://pages/JEMAIzpUvknk4JMMfRz2) <a href="#invalidgemjoin" id="invalidgemjoin"></a>

```
error InvalidGemJoin(address invalidJoin);
```

#### [MarketMustBeCaller](broken://pages/JEMAIzpUvknk4JMMfRz2) <a href="#marketmustbecaller" id="marketmustbecaller"></a>

```
error MarketMustBeCaller(address caller);
```

#### [ExternalFlashswapNotAllowed](broken://pages/JEMAIzpUvknk4JMMfRz2) <a href="#externalflashswapnotallowed" id="externalflashswapnotallowed"></a>

```
error ExternalFlashswapNotAllowed();
```

#### [InvalidSwapDirection](broken://pages/JEMAIzpUvknk4JMMfRz2) <a href="#invalidswapdirection" id="invalidswapdirection"></a>

```
error InvalidSwapDirection();
```

#### [UnexpectedSyOut](broken://pages/JEMAIzpUvknk4JMMfRz2) <a href="#unexpectedsyout" id="unexpectedsyout"></a>

```
error UnexpectedSyOut(uint256 amountSyOut, uint256 expectedSyOut);
```

#### [FlashswapTooExpensive](broken://pages/JEMAIzpUvknk4JMMfRz2) <a href="#flashswaptooexpensive" id="flashswaptooexpensive"></a>

```
error FlashswapTooExpensive(uint256 amountSyIn, uint256 maxResultingDebt);
```


---

# Agent Instructions: 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:

```
GET https://docs.ionprotocol.io/devs/smart-contract-architecture/flash/pthandler.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
