# Terra Classic Delta-Neutral Strategy (Retired)

This guide is intended for developers who wish to interact with Aperture contracts directly on Avalanche.  Regular users are advised to use the official Aperture web app.

Please consult the [Deployed Contracts](https://docs.aperture.finance/docs/transparency/deployed-contracts) page for contract addresses on various networks.

## Open Position

To open a position on Aperture, interact with the **Aperture Manager** contract on Terra with the ExecuteMsg variant `CreatePosition`:

{% tabs %}
{% tab title="Rust" %}

```rust
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum ExecuteMsg {
    /// Create a new position with the specified strategy.
    /// Can be called by any Terra address.
    CreatePosition {
        strategy: Strategy,
        data: Option<Binary>,
        assets: Vec<terraswap::asset::Asset>,
    }
}

/// The strategy id and chain id can uniquely identify what strategy it is
/// and on which chain is it located.
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct Strategy {
    pub chain_id: u16,
    pub strategy_id: Uint64,
}
```

{% endtab %}

{% tab title="JSON" %}

```javascript
{
  "create_position": {
    "strategy": {
      "chain_id": 3,
      "strategy_id": "0"
    },
    "data": "e...",
    "assets": [
      {
        "info": {
          "native_token": {
            "denom":"uusd"
          }
        },
        "amount":"2000000000"
      }
    ]
  }
}
```

{% endtab %}
{% endtabs %}

<table><thead><tr><th width="150">Key</th><th width="228">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>strategy</code></td><td>Strategy</td><td>Specifies the strategy to use.</td></tr><tr><td><code>data</code></td><td>Binary</td><td>Binary representing parameters taken by the specificed strategy.</td></tr><tr><td><code>assets</code></td><td>Vec&#x3C;<a href="https://github.com/terraswap/terraswap/blob/c6fdef72f20d73f42f45525210a39656c826d6d6/packages/terraswap/src/asset.rs#L14">terraswap::asset::Asset</a>></td><td>Vector of assets to deposit into the position.</td></tr></tbody></table>

### Delta-neutral Strategy (Terra)

#### Strategy JSON:

```json
{
  "chain_id": 3,
  "strategy_id": "0"
}
```

Note that the `chain_id` value for Terra is `3`.  Aperture uses the same chain\_id values as [Wormhole](https://docs.wormholenetwork.com/wormhole/contracts).

#### Parameter (data) format:

The delta-neutral strategy requires a set of parameters of the following JSON format:

```json
{
  "target_min_collateral_ratio": "2.4",
  "target_max_collateral_ratio": "2.8",
  "mirror_asset_cw20_addr": "terra1..."
}
```

`target_min_collateral_ratio` and `target_max_collateral_ratio` specify the target collateral ratio range with the following requirements:

* `target_min_collateral_ratio` must exceed the minimum required collateral ratio for the mAsset by at least the safety margin amount.  The safety margin value is configurable by the Delta-neutral Manager contract, currently set at `0.3`.  For example, if the minimum collateral ratio required for mAAPL is `1.5`, then `target_min_collateral_ratio` must be at least `1.8`.
* Currently the target CR range must be `0.4` wide, i.e. the equality `target_min_collateral_ratio + 0.4 = target_max_collateral_ratio` must hold.

The parameter JSON should be encoded in base-64 and passed as the `data` field of the `CreatePosition` enum variant when opening a position.

Sample position-open tx: <https://terrasco.pe/mainnet/tx/A87F26A7A6137C87E968B02AC20CCB2F1FE2313D42349FAD3F330D3A50CA1AC8>

## Query Position Ids

To get a list of position ids held by a Terra address, query the **Aperture Manager** contract with the following request:

```json
{
  "get_terra_positions_by_holder": {
    "holder": "terra1..."
  }
}
```

Note that *all* position ids are returned, including active and closed positions.

## Query Position Information

You may query the **Delta-neutral Manager** contract for detailed position information with the following request:

```json
{
  "batch_get_position_info": {
    "positions": [
        {
            "chain_id": 3,
            "position_id": "1"
        }
    ]
  }
}
```

Note that you may specify multiple position ids in the `positions` array.  However, most Terra nodes set a low query gas limit so you may receive an out-of-gas error if more than one position id is requested.  It is recommended to use [Terra Hive](https://github.com/terra-money/hive-graph) for batch query use cases.

## Close Position

To open a position on Aperture, interact with the **Aperture Manager** contract on Terra with the ExecuteMsg variant `CreatePosition`:

{% tabs %}
{% tab title="Rust" %}

```rust
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum ExecuteMsg {
    /// Perform an action on an existing positions held by a Terra address.
    /// Can only be called by the position holder.
    ExecuteStrategy {
        position_id: Uint128,
        action: Action,
        assets: Vec<terraswap::asset::Asset>,
    },
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum Action {
    ClosePosition {
        recipient: Recipient,
    },
    ...
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum Recipient {
    TerraChain {
        recipient: String,
    },
    ExternalChain {
      /// Details to be added prior to Aperture's cross-chain launch.
    },
}
```

{% endtab %}

{% tab title="JSON" %}

```javascript
{
  "execute_strategy": {
    "position_id": "12345",
    "action": {
      "close_position": {
        "recipient": {
          "terra_chain": {
            "recipient": "terra1..."
          }
        }
      }
    },
    "assets": []
  }
}
```

{% endtab %}
{% endtabs %}

<table><thead><tr><th width="150">Key</th><th width="228">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>position_id</code></td><td>Uint128</td><td>Specifies the id of the position to close.  The caller must be the position holder.</td></tr><tr><td><code>action</code></td><td>Action</td><td>Should specify the <code>ClosePosition</code> Action variant with the desired recipient (could be the position holder address or any other address).</td></tr><tr><td><code>assets</code></td><td>Vec&#x3C;<a href="https://github.com/terraswap/terraswap/blob/c6fdef72f20d73f42f45525210a39656c826d6d6/packages/terraswap/src/asset.rs#L14">terraswap::asset::Asset</a>></td><td>Must be an empty vector since no deposits are needed for the close-position action.</td></tr></tbody></table>

Sample position-close tx: <https://terrasco.pe/mainnet/tx/5F437BD5AC9DD6B038D8C1A5B2AEAF4928BE817F5BFE4A82530E5B914354E763>


---

# 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.aperture.finance/docs/legacy-products/integration-guide/terra-classic-delta-neutral-strategy-retired.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.
