> ## 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.

# Zap

> Zap converts between USDG and allowlisted assets (USDC, USDT, ETH at launch) around the core.

Zap converts between USDG and allowlisted assets (USDC, USDT, ETH at launch) around the core. It lets suppliers deposit and withdraw in the asset they hold, and lets borrowers repay or receive loans in that asset. Routes and slippage caps come from RiskRegistry, swaps run through the shared DEX router, and every leftover balance returns to the caller. The core only ever sees USDG.

`Zap` lets you:

* Deposit any allowlisted token or ETH into the pool and receive sUSDG
* Repay a book with any allowlisted token or ETH
* Borrow USDG and receive it as another token or ETH
* Withdraw sUSDG and receive another token or ETH

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

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

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

## Write methods

### zapSupply

```solidity theme={"system"}
function zapSupply(address token, uint256 amountIn) external payable nonReentrant
```

Converts `amountIn` of `token` (or the ETH sent as `msg.value`) to USDG and deposits it into USDGPool for the caller. USDG passes through without a swap. Reverts with ZeroAddress if `token` is zero and no ETH was sent, ZeroAmount for a zero input, and InvalidRoute if the token is not allowlisted.

<Warning>
  For ERC-20 input the caller must approve Zap for `amountIn`. Slippage is capped by the token's registry route against the router quote.
</Warning>

**Input parameters**

| Name     | Type      | Description                                                                                                             |
| -------- | --------- | ----------------------------------------------------------------------------------------------------------------------- |
| token    | `address` | The input token. Pass the zero address and send ETH as `msg.value` to supply ETH. Ignored when `msg.value` is non-zero. |
| amountIn | `uint256` | Token amount to pull from the caller. Ignored for ETH.                                                                  |

### zapRepay

```solidity theme={"system"}
function zapRepay(
    uint16 bookId,
    address token,
    uint256 amountIn
) external payable nonReentrant
```

Converts `amountIn` of `token` (or ETH) to USDG and repays the caller's debt in `bookId`. Always live, like `Stockline.repay`. Excess USDG beyond the debt returns to the caller.

<Warning>
  For ERC-20 input the caller must approve Zap for `amountIn`.
</Warning>

**Input parameters**

| Name     | Type      | Description                                                   |
| -------- | --------- | ------------------------------------------------------------- |
| bookId   | `uint16`  | The book to repay.                                            |
| token    | `address` | The input token, or the zero address with ETH as `msg.value`. |
| amountIn | `uint256` | Token amount to pull. Ignored for ETH.                        |

### borrowTo

```solidity theme={"system"}
function borrowTo(
    uint16 bookId,
    uint256 usdgAmount,
    address token
) external nonReentrant
```

Borrows `usdgAmount` USDG on the caller's account, swaps it to `token`, and sends the output to the caller. The zero address or WETH delivers native ETH; USDG is sent as is. Follows Stockline borrow rules: LIVE, unpaused, caps, and LTV. Reverts with ZeroAmount for a zero amount.

<Warning>
  The caller must first set Zap as operator with `Stockline.setOperator`, because Zap borrows on their behalf.
</Warning>

<Note>
  Uses core.borrow, so LIVE and pause gates apply. Caller must set this Zap as operator.
</Note>

**Input parameters**

| Name       | Type      | Description                                                        |
| ---------- | --------- | ------------------------------------------------------------------ |
| bookId     | `uint16`  | The book to borrow against.                                        |
| usdgAmount | `uint256` | USDG to borrow.                                                    |
| token      | `address` | The output token. Zero address or WETH gives ETH; USDG gives USDG. |

### zapWithdraw

```solidity theme={"system"}
function zapWithdraw(uint256 usdgAmount, address token) external nonReentrant
```

Withdraws `usdgAmount` USDG from the caller's sUSDG, swaps it to `token`, and sends the output to the caller. Subject to pool cash limits and the guardian pause through `USDGPool.maxWithdraw`.

<Warning>
  The caller must approve Zap for the sUSDG shares that the withdrawal burns.
</Warning>

**Input parameters**

| Name       | Type      | Description                                                        |
| ---------- | --------- | ------------------------------------------------------------------ |
| usdgAmount | `uint256` | USDG to withdraw from the pool.                                    |
| token      | `address` | The output token. Zero address or WETH gives ETH; USDG gives USDG. |

## Events

### ZappedSupply

```solidity theme={"system"}
event ZappedSupply(
    address indexed account,
    address indexed token,
    uint256 amountIn,
    uint256 usdgOut
)
```

Fires after a zapSupply, with the input and the USDG deposited.

### ZappedRepay

```solidity theme={"system"}
event ZappedRepay(
    address indexed account,
    uint16 indexed bookId,
    address token,
    uint256 usdgOut
)
```

Fires after a zapRepay, with the USDG applied to the book.

### BorrowedTo

```solidity theme={"system"}
event BorrowedTo(
    address indexed account,
    uint16 indexed bookId,
    address token,
    uint256 usdgIn,
    uint256 amountOut
)
```

Fires after a borrowTo, with the USDG borrowed and the tokens delivered.

### ZappedWithdraw

```solidity theme={"system"}
event ZappedWithdraw(
    address indexed account,
    address indexed token,
    uint256 usdgIn,
    uint256 amountOut
)
```

Fires after a zapWithdraw, with the USDG withdrawn and the tokens delivered.

## Errors

### ZeroAddress

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

The constructor addresses were zero, or `token` was zero with no ETH sent.

### ZeroAmount

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

The input amount was zero, or an ETH transfer to the caller failed.

### InvalidRoute

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

The token is not allowlisted or its route does not connect to USDG.
