> For the complete documentation index, see [llms.txt](https://dev.init.capital/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dev.init.capital/guides/basic-interaction/add-and-remove-collateral.md).

# 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
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://dev.init.capital/guides/basic-interaction/add-and-remove-collateral.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
