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

# Repay and withdraw

> Repay USDG and withdraw collateral through the core or the router.

Repay is always live. Withdrawing collateral needs LIVE, no pause, and a healthy account after the pull.

Addresses: [Addresses](/resources/addresses). ABI: [`Stockline.json`](/abis/Stockline.json), [`PositionRouter.json`](/abis/PositionRouter.json).

<Steps>
  <Step title="Repay">
    Approve USDG on `Stockline`, then `repay(bookId, amount, onBehalf)`. Any amount. Any regime. Pause does not block it.

    <CodeGroup>
      ```typescript theme={"system"}
      await wallet.writeContract({
        address: addresses.stockline,
        abi: coreAbi,
        functionName: "repay",
        args: [bookId, amount, account],
      })
      ```

      ```tsx theme={"system"}
      <button onClick={() => repay(bookId, amount)}>Repay</button>
      ```

      ```solidity theme={"system"}
      interface IStockline {
          function repay(uint16 bookId, uint256 amount, address onBehalf) external;
      }
      ```
    </CodeGroup>
  </Step>

  <Step title="Withdraw collateral">
    `withdrawCollateral` is risk-up. It needs LIVE and not paused. `PositionRouter.closePosition` repays then withdraws in one transaction if the router is an operator.
  </Step>

  <Step title="Zap out">
    `Zap.zapWithdraw(usdgAmount, token)` pulls sUSDG and pays USDC, USDT, or ETH. USDG withdraw can pause. See [Zap](/developers/positions/zap).
  </Step>
</Steps>

## What can go wrong

* `Unhealthy` if withdraw would put the account under LT.
* `RegimeBlocked` / `Paused` on withdraw.
* Zap leftover ETH transfer revert `ZeroAmount`.
* Decimal mixup on USDG (18 on testnet, 6 on mainnet when listed).

Deleveraging is always live. That is an invariant, not a courtesy. Guardian pause cannot block repay or supplyCollateral. `deleverage` on the router is risk-down and skips LIVE and pause gates. ARCHITECTURE.md omitted that row. The contract includes it.

`closePosition` on the router repays then withdraws. It still needs an operator grant for the withdraw half. If you only repay, call the core. If you zap from USDC, `zapRepay` is always live and does not need LIVE.

Withdrawing sUSDG can pause. That is the USDG pool side, not collateral. Users who need Cash during a pause should already hold Cash, not only Earning.

Decimals: testnet faucet USDG is 18. Mainnet USDG is 6 when listed. Read `decimals()`. Round debt up and shares down if you write your own math. The core already rounds in the protocol's favor.
