# Surplus Collateral

In addition to paying and receiving surplus collateral in the input/output of swap, mint and burn calls user can directly deposit, withdraw or transfer their surplus collateral balance.&#x20;

All of these surplus collateral use callpath 0 with specific&#x20;

### Deposits

To deposit surplus collateral, the user should call the following user command:

```
userCmd(1, abi.encode(
    73,     // Fixed sub-command code
    recv,   // address
    value,  // uint128
    token   // address
))
```

* recv - The address the surplus collateral will be credited
* value - The total amount deposited
* token - The address of the ERC20 token deposited or `address(0)` for native Ethereum)

Note that deposit will use `transferFrom()` in to deposit the surplus collateral, so the user must approve or permit at least `value` amount of the token. In the case of native Ethereum, the deposit is collected from `msg.value` so the user must attach at least the amount matching the value parameter to the Ethereum transaction.

For EIP-2612 compliant tokens, users can deposit using an off-chain permit signature avoiding the need to approve the DEX contract:

```
userCmd(0, abi.encode(
    83,     // Fixed sub-command code
    recv,       // address
    value,      // uint128
    token,      // address
    deadline,   // uint256
    v,          // uint8
    r,          // bytes32
    s           // bytes32
))
```

* deadline - The deadline for the permit call
* v, r, s - The EIP-712 signature to be passed directly to the token contract

### Withdraw

To withdraw surplus collateral, the user should call the following user command:

```
userCmd(0, abi.encode(
    74,     // Fixed sub-command code
    recv,   // address
    value,  // uint128
    token   // address
))
```

* recv - The address the surplus collateral will be sent to
* value - The total amount to withdraw from surplus collateral. 0 is treated as a magic value that will withdraw the entire balance.
* token - The address of the ERC20 token or `address(0)` for native Ethereum)

### Transfer

Users can also bilaterally transfer surplus liquidity between owner addresses:

```
userCmd(0, abi.encode(
    75,     // Fixed sub-command code
    recv,   // address
    value,  // uint128
    token   // address
))
```

* recv - The address the surplus collateral will be transferred to
* value - The total amount to transfer from surplus collateral. 0 is treated as a magic value that will withdraw the entire balance.
* token - The address of the ERC20 token or `address(0)` for native Ethereum


---

# 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.ambient.finance/developers/dex-contract-interface/surplus-collateral.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.
