# Epoch Manager

The Epoch Manager is a contract which sole purpose is to generate epochs on MANTRA, acting like a clock for the other\
contracts.

An `Epoch` is a period of time that is defined by the `duration` parameter on `EpochConfig`, and they are used by other\
contracts to take timely actions. For example, the Farm Manager uses epochs to calculate the farm rewards for its users.

## How it works

The epoch configuration is set up when the contract is instantiated. The epoch configuration defines the duration of an\
epoch and when the genesis epoch is going to take place, i.e. the first epoch.

Then, when querying the contract, the epochs in the system are derived based on the genesis epoch and duration values.

## Instantiate

Instantiates an instance of the epoch manager contract

```json
{
  "owner": "mantra1...",
  "epoch_config": {
    "duration": "86400",
    "genesis_epoch": "1571797"
  }
}
```

| Key            | Type        | Description                      |
| -------------- | ----------- | -------------------------------- |
| `owner`        | String      | The owner of the contract        |
| `epoch_config` | EpochConfig | The configuration for the epochs |

## ExecuteMsg

### UpdateConfig

Updates the contract configuration. This can only be triggered by the contract owner.

```json
{
  "update_config": {
    "epoch_config": {
      "duration": "86400",
      "genesis_epoch": "1571797"
    }
  }
}
```

| Key            | Type                 | Description                 |
| -------------- | -------------------- | --------------------------- |
| `epoch_config` | Option\<EpochConfig> | The new epoch configuration |

### UpdateOwnership(::cw\_ownable::Action)

Implements `cw_ownable`. Updates the contract's ownership. `::cw_ownable::Action` can\
be `TransferOwnership`, `AcceptOwnership` and `RenounceOwnership`.

{% hint style="warning" %}
Note: This is a `cw_ownable` message.
{% endhint %}

{% tabs %}
{% tab title="TransferOwnership" %}
Propose to transfer the contract's ownership to another account, optionally with an expiry time. Can only be called by\
the contract's current owner. Any existing pending ownership transfer is overwritten.

```json
{
  "update_ownership": {
    "transfer_ownership": {
      "new_owner": "mantra1...",
      "expiry": {
        "at_height": "424242424242"
      }
    }
  }
}
```

| Key          | Type                | Description                         |
| ------------ | ------------------- | ----------------------------------- |
| `new_owner`  | String              | The new owner proposed,             |
| `expiry`     | Option\<Expiration> | Optional expiration time parameter. |
| {% endtab %} |                     |                                     |

{% tab title="AcceptOwnership" %}
Accept the pending ownership transfer. Can only be called by the pending owner.

```json
{
  "update_ownership": "accept_ownership"
}
```

{% endtab %}

{% tab title="RenounceOwnership" %}
Give up the contract's ownership and the possibility of appointing a new owner. Can only be invoked by the contract's\
current owner. Any existing pending ownership transfer is canceled.

```json
{
  "update_ownership": "renounce_ownership"
}
```

{% endtab %}
{% endtabs %}

## QueryMsg

### Config

Returns the configuration of the contract.

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

```json
{
  "config": {}
}
```

{% endtab %}

{% tab title="Response (ConfigResponse)" %}

```json
{
  "epoch_config": {
    "duration": "86400",
    "genesis_epoch": "1571797"
  }
}
```

| Key            | Type        | Description             |
| -------------- | ----------- | ----------------------- |
| `epoch_config` | EpochConfig | The epoch configuration |
| {% endtab %}   |             |                         |
| {% endtabs %}  |             |                         |

### CurrentEpoch

Returns the current epoch based on the current timestamp.

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

```json
{
  "current_epoch": {}
}
```

{% endtab %}

{% tab title="Response (EpochResponse)" %}

```json
{
  "epoch": {
    "id": 0,
    "start_time": "1571797"
  }
}
```

| Key           | Type  | Description       |
| ------------- | ----- | ----------------- |
| `epoch`       | Epoch | The current epoch |
| {% endtab %}  |       |                   |
| {% endtabs %} |       |                   |

### Epoch

Returns the epoch with the given ID.

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

```json
{
  "epoch": {
    "id": 100
  }
}
```

| Key          | Type | Description                        |
| ------------ | ---- | ---------------------------------- |
| `id`         | u64  | The id of the epoch to be queried. |
| {% endtab %} |      |                                    |

{% tab title="Response (EpochResponse)" %}

```json
{
  "epoch": {
    "id": 100,
    "start_time": "1571797"
  }
}
```

| Key           | Type  | Description                         |
| ------------- | ----- | ----------------------------------- |
| `epoch`       | Epoch | The queried epoch with the given id |
| {% endtab %}  |       |                                     |
| {% endtabs %} |       |                                     |

### Ownership

Returns the ownership of the contract.

{% hint style="warning" %}
Note: This is a `cw_ownable` query.
{% endhint %}

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

```json
{
  "ownership": {}
}
```

{% endtab %}

{% tab title="Response (::cw\_ownable::Ownership<String>)" %}

```json
{
  "owner": "mantra1...",
  "pending_owner": "mantra1...",
  "pending_expiry": 424242424242
}
```

| Key              | Type   | Description                                                                                                                                                           |
| ---------------- | ------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `owner`          | Option | The contract's current owner. None if the ownership has been renounced.                                                                                               |
| `pending_owner`  | Option | The account who has been proposed to take over the ownership. None if there isn't a pending ownership transfer.                                                       |
| `pending_expiry` | Option | The deadline for the pending owner to accept the ownership. None if there isn't a pending ownership transfer, or if a transfer exists and it doesn't have a deadline. |
| {% endtab %}     |        |                                                                                                                                                                       |
| {% endtabs %}    |        |                                                                                                                                                                       |

## MigrateMsg

Message to migrate the contract to a new code ID.

```json
{}
```
