# Add and Remove Collateral

## Add Collateral

To add collateral to a position, users must already create the position and specify the position ID. The collateral token must be an inToken from lending pools.&#x20;

{% hint style="info" %}
The position must be in a mode that allows the collateral to be used.
{% endhint %}

{% hint style="warning" %}
The above 2 steps must be performed in a single transaction (atomically) to avoid potential front-run attack.
{% endhint %}

```solidity
// Example add collateral function
function addCollateral(uint256 posId, address lendingPool, uint256 shares) external {
    // 0. .. pull in lending pool tokens from the caller ..

    // 1. transfer inToken to PosManager
    IERC20(lendingPool).safeTransfer(POS_MANAGER, shares);

    // 2. add collateral to position
    IInitCore(INIT_CORE).collateralize(posId, lendingPool);
}
```

## Remove Collateral

To remove collateral, specify the position ID to remove from, shares of collateral to remove, and also the receiver address. The caller must be the position owner or approved party to modify the position.

{% hint style="info" %}
Always check that the position's health is over 1 after removing collateral to avoid transaction revert.
{% endhint %}

```solidity
// Example remove collateral function
function removeCollateral(uint256 posId, address lendingPool, uint256 shares, address receiver) external {
    // 1. remove collateral from position
    IInitCore(INIT_CORE).decollateralize(posId, lendingPool, shares, receiver);
}
```


---

# 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://dev.init.capital/guides/basic-interaction/add-and-remove-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.
