# Contracts

## Active Contract Set

<mark style="color:blue;">`GET`</mark> `http://127.0.0.1:31567/contract/active`

Returns all currently active contracts. May not be accurate until the blockchain is fully synced.

{% tabs %}
{% tab title="200 An array of contract objects, including their actual F-Star code." %}

```javascript
[
    {
        "contractHash": "55be2edd1e537869cce832bd014aa1e3251aba651c5c3f38f70db86417faa9c4",
        "address": "tc1q2klzahg72duxnn8gx27szj4puvj34wn9r3wr7w8hpkuxg9l648zqhc76xa",
        "expire": 19375,
        "code": "(* NAME_START: Example Token :NAME_END *)\nopen Zen.Types\nopen Zen.Vector\nopen Zen.Base\nopen Zen.Cost\nopen Zen.Asset\nopen Zen.Data\n\nmodule ET = Zen.ErrorT\nmodule OT = Zen.OptionT\nmodule Tx = Zen.TxSkeleton\nmodule CR = Zen.ContractResult.NoMessage\n\nlet buy txSkeleton contractHash returnAddress =\n  let! tokens = Tx.getAvailableTokens zenAsset txSkeleton in\n\n  let! contractAsset = getDefault contractHash in\n\n  let! txSkeleton =\n    Tx.lockToContract zenAsset tokens contractHash txSkeleton\n    >>= Tx.mint tokens contractAsset\n    >>= Tx.lockToAddress contractAsset tokens returnAddress in\n\n  CR.ret txSkeleton\n\nlet redeem txSkeleton contractHash returnAddress wallet =\n  let! contractAsset = getDefault contractHash in\n  let! tokens = Tx.getAvailableTokens contractAsset txSkeleton in\n\n  let! txSkeleton =\n    Tx.destroy tokens contractAsset txSkeleton\n    >>= Tx.lockToAddress zenAsset tokens returnAddress\n    >>= Tx.fromWallet zenAsset tokens contractHash wallet in\n\n  CR.ofOption \"contract doesn't have enough zens to pay you\" txSkeleton\n\nval main: txSkeleton -> hash -> string -> option data -> wallet:wallet -> cost (result (txSkeleton ** option message))  (2 + 66 + (64 + (64 + (64 + 64 + (Zen.Wallet.size wallet * 128 + 192) + 0)) + 25) + 29)\nlet main txSkeleton contractHash command data wallet =\n  let! returnAddress = data >!> tryDict >?> tryFindLock \"returnAddress\" in\n\n  match returnAddress with\n  | Some returnAddress ->\n      if command = \"redeem\" then\n        redeem txSkeleton contractHash returnAddress wallet\n      else if command = \"\" || command = \"buy\" then\n        buy txSkeleton contractHash returnAddress\n        |> autoInc\n      else\n        ET.autoFailw \"unsupported command\"\n  | None ->\n      ET.autoFailw \"returnAddress is required\"\n\nval cf: txSkeleton -> string -> option data -> wallet -> cost nat 24\nlet cf _ _ _ wallet = ret  (2 + 66 + (64 + (64 + (64 + 64 + (Zen.Wallet.size wallet * 128 + 192) + 0)) + 25) + 29)"
    },
    {
        "contractHash": "55be2edd1e537869cce832bd014aa1e3251aba651c5c3f38f70db86417faa9c4",
        "address": "tc1q2klzahg72duxnn8gx27szj4puvj34wn9r3wr7w8hpkuxg9l648zqhc76xa",
        "expire": 19375,
        "code": "(* NAME_START: Example Token :NAME_END *)\nopen Zen.Types\nopen Zen.Vector\nopen Zen.Base\nopen Zen.Cost\nopen Zen.Asset\nopen Zen.Data\n\nmodule ET = Zen.ErrorT\nmodule OT = Zen.OptionT\nmodule Tx = Zen.TxSkeleton\nmodule CR = Zen.ContractResult.NoMessage\n\nlet buy txSkeleton contractHash returnAddress =\n  let! tokens = Tx.getAvailableTokens zenAsset txSkeleton in\n\n  let! contractAsset = getDefault contractHash in\n\n  let! txSkeleton =\n    Tx.lockToContract zenAsset tokens contractHash txSkeleton\n    >>= Tx.mint tokens contractAsset\n    >>= Tx.lockToAddress contractAsset tokens returnAddress in\n\n  CR.ret txSkeleton\n\nlet redeem txSkeleton contractHash returnAddress wallet =\n  let! contractAsset = getDefault contractHash in\n  let! tokens = Tx.getAvailableTokens contractAsset txSkeleton in\n\n  let! txSkeleton =\n    Tx.destroy tokens contractAsset txSkeleton\n    >>= Tx.lockToAddress zenAsset tokens returnAddress\n    >>= Tx.fromWallet zenAsset tokens contractHash wallet in\n\n  CR.ofOption \"contract doesn't have enough zens to pay you\" txSkeleton\n\nval main: txSkeleton -> hash -> string -> option data -> wallet:wallet -> cost (result (txSkeleton ** option message))  (2 + 66 + (64 + (64 + (64 + 64 + (Zen.Wallet.size wallet * 128 + 192) + 0)) + 25) + 29)\nlet main txSkeleton contractHash command data wallet =\n  let! returnAddress = data >!> tryDict >?> tryFindLock \"returnAddress\" in\n\n  match returnAddress with\n  | Some returnAddress ->\n      if command = \"redeem\" then\n        redeem txSkeleton contractHash returnAddress wallet\n      else if command = \"\" || command = \"buy\" then\n        buy txSkeleton contractHash returnAddress\n        |> autoInc\n      else\n        ET.autoFailw \"unsupported command\"\n  | None ->\n      ET.autoFailw \"returnAddress is required\"\n\nval cf: txSkeleton -> string -> option data -> wallet -> cost nat 24\nlet cf _ _ _ wallet = ret  (2 + 66 + (64 + (64 + (64 + 64 + (Zen.Wallet.size wallet * 128 + 192) + 0)) + 25) + 29)"
    }
]
```

{% endtab %}
{% endtabs %}

## Contract ID

<mark style="color:blue;">`GET`</mark> `http://127.0.0.1:31567/contract/contractId`

Get a contract identifier from a contract address.

#### Path Parameters

| Name                                      | Type   | Description             |
| ----------------------------------------- | ------ | ----------------------- |
| address<mark style="color:red;">\*</mark> | string | address of the contract |

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

```json
00000000e89738718a802a7d217941882efe8e585e20b20901391bc37af25fac2f22c8ab
```

{% endtab %}

{% tab title="400 " %}

```
invalid address map
  [("address",
    "notExistingContractAddress")]
```

{% endtab %}
{% endtabs %}

## Activate Contract

<mark style="color:green;">`POST`</mark> `http://127.0.0.1:31567/wallet/contract/activate`

This how you activate a contract

#### Request Body

| Name           | Type   | Description                                                                                                                                                                                         |
| -------------- | ------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| password       | string | wallet password                                                                                                                                                                                     |
| numberOfBlocks | number | The number of blocks you want to activate your contract for                                                                                                                                         |
| code           | string | <p>Send valid fstar code, for example:<br><code>let main \_ \_ \_ \_ \_ \_ \_ \_ = Zen.ResultT.failw "This contract does nothing"\nlet cf \_ \_ \_ \_ \_ \_ \_ = Zen.Cost.ret (1 <: nat)</code></p> |

{% tabs %}
{% tab title="200 Cake successfully retrieved." %}

```javascript
{
    "address": "ctzn1qqqqqqqym4kfe8y5qg5tazkgjwn4qpesj05x6mqsvd5auwmzkhhs444fnkvwxhahr",
    "contractId": "000000009bad939392804517d1591274ea00e6127d0dad820c6d3bc76c56bde15ad533b3"
}
```

{% endtab %}

{% tab title="400 Could not find a matching in the query." %}

```javascript
```

{% endtab %}
{% endtabs %}

## Execute Contract

<mark style="color:green;">`POST`</mark> `http://127.0.0.1:31567/wallet/contract/execute`

Run a smart contract with parameters

#### Request Body

| Name                                            | Type    | Description                                                                  |
| ----------------------------------------------- | ------- | ---------------------------------------------------------------------------- |
| options<mark style="color:red;">\*</mark>       | string  | add sign and return address here if needed                                   |
| sign<mark style="color:red;">\*</mark>          | string  | the derivation path for the execution of the contract                        |
| password<mark style="color:red;">\*</mark>      | string  | wallet password                                                              |
| returnAddress<mark style="color:red;">\*</mark> | boolean | allows the contract to automatically send funds back to in case it needs to. |
| messageBody<mark style="color:red;">\*</mark>   | string  | send the contract some data                                                  |
| address<mark style="color:red;">\*</mark>       | string  | The contract address                                                         |
| command<mark style="color:red;">\*</mark>       | string  | Trigger one of the functions in the contract                                 |
| spends<mark style="color:red;">\*</mark>        | array   | an array of outputs                                                          |

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

```javascript
5222c826d8d403636a75f48fd91d7d177be6efbf04ac266f3822322733e938ad
```

{% endtab %}
{% endtabs %}

## Extend Contract

<mark style="color:green;">`POST`</mark> `http://127.0.0.1:31567/wallet/contract/extend`

This is to extend a contract

#### Request Body

| Name                                             | Type   | Description                                            |
| ------------------------------------------------ | ------ | ------------------------------------------------------ |
| password<mark style="color:red;">\*</mark>       | string | wallet password                                        |
| numberOfBlocks<mark style="color:red;">\*</mark> | string | Number of block until when the contract will be active |
| contractId<mark style="color:red;">\*</mark>     | string | ID of the contract                                     |

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

```
5222c826d8d403636a75f48fd91d7d177be6efbf04ac266f3822322733e938ad
```

{% endtab %}
{% endtabs %}


---

# 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.zenprotocol.com/apps/headless/api/contracts.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.
