# Governance Contract

The Governance Contract contains logic for holding governance polls and handling VEGAS Token staking, and allows the Terra-vegas casino to be governed by its users in a decentralised manner.&#x20;

It is a fork from the Governance Contract developed by the Anchor protocol.

After the initial bootstrapping of Terra-vegas contracts, the Gov Contract is assigned to be the owner of all contracts in Terra-vegas.New proposals for change are submitted as polls, and are voted on by VEGAS stakers through the voting procedure. Polls can contain messages that can be executed directly without changing the Terra-vegas code.The Gov Contract keeps a balance of VEGAS tokens, which it uses to reward stakers with funds it receives from the player bets and the reserve contract deposits on the anchor protocol.

## Config <a href="#config" id="config"></a>

| Key                 | Type          | Description                                                                                                               |
| ------------------- | ------------- | ------------------------------------------------------------------------------------------------------------------------- |
| `owner`             | CanonicalAddr | Address of contract owner                                                                                                 |
| `token_contract`    | CanonicalAddr | Contract address of VEGAS Token (VEGAS)                                                                                   |
| `quorum`            | Decimal       | Minimum percentage of participation required for a poll to pass                                                           |
| `threshold`         | Decimal       | Minimum percentage of `yes` votes required for a poll to pass                                                             |
| `voting_period`     | u64           | <p>Number of blocks during which votes can be cast <strong>\[blocks]</strong><br></p>                                     |
| `timelock_period`   | u64           | <p>Number of blocks required after a poll pass before executing changes <strong>\[blocks]</strong><br></p>                |
| `expiration_period` | u64           | <p>Number of blocks after a poll's voting period during which the poll can be executed <strong>\[blocks]</strong><br></p> |
| `proposal_deposit`  | Uint128       | Minimum VEGAS deposit required for submitting a new poll                                                                  |
| `snapshot_period`   | u64           | Window of time (number of blocks) allowed for poll snapshot before a poll's end **\[blocks]**                             |

## InstantiateMsg <a href="#instantiatemsg" id="instantiatemsg"></a>

```rust
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct InstantiateMsg {
    pub quorum: Decimal,
    pub threshold: Decimal,
    pub voting_period: u64,
    pub timelock_period: u64,
    pub expiration_period: u64,
    pub proposal_deposit: Uint128,
    pub snapshot_period: u64, 
}
```

| Key                 | Type    | Description                                                                                                               |
| ------------------- | ------- | ------------------------------------------------------------------------------------------------------------------------- |
| `quorum`            | Decimal | Minimum percentage of participation required for a poll to pass                                                           |
| `threshold`         | Decimal | Minimum percentage of `yes` votes required for a poll to pass                                                             |
| `voting_period`     | u64     | <p>Number of blocks during which votes can be cast <strong>\[blocks]</strong><br></p>                                     |
| `timelock_period`   | u64     | <p>Number of blocks required after a poll pass before executing changes <strong>\[blocks]</strong><br></p>                |
| `expiration_period` | u64     | <p>Number of blocks after a poll's voting period during which the poll can be executed <strong>\[blocks]</strong><br></p> |
| `proposal_deposit`  | Uint128 | Minimum VEGAS deposit required for submitting a new poll                                                                  |
| `snapshot_period`   | u64     | Window of time (number of blocks) allowed for poll snapshot before a poll's end **\[blocks]**                             |

## ExecuteMsg

### `Receive` <a href="#receive" id="receive"></a>

Can be called during a CW20 token transfer when the Gov contract is the recipient. Allows the token transfer to execute a Receive Hook as a subsequent action within the same transaction.

```rust
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum ExecuteMsg {
    Receive {
        sender: String,
        amount: Uint128,
        msg: Binary,
    }
}
```

### &#x20;<a href="#internal-executepollmsgs" id="internal-executepollmsgs"></a>

| Key      | Type    | Description                         |
| -------- | ------- | ----------------------------------- |
| `sender` | String  | Sender of token transfer            |
| `amount` | Uint128 | Amount of tokens received           |
| `msg`    | Binary  | Base64-encoded JSON of Receive Hook |

### `[Internal] ExecutePollMsgs` <a href="#internal-executepollmsgs" id="internal-executepollmsgs"></a>

Executes messages in a passed poll. Can only by issued by `Gov`.

```rust
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum ExecuteMsg {
    ExecutePollMsgs {
        poll_id: u64,     
    }
}
```

| Key       | Type | Description |
| --------- | ---- | ----------- |
| `poll_id` | u64  | Poll ID     |

### `RegisterContracts` <a href="#registercontracts" id="registercontracts"></a>

Registers the contract addresses (i.e. Terra-vegas Token, VEGAS) to Gov.

```rust
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum ExecuteMsg {
    RegisterContracts {
        token_contract: String, 
    }
}
```

| Key              | Type   | Description                                   |
| ---------------- | ------ | --------------------------------------------- |
| `token_contract` | String | Contract address of Terra-vegas Token (VEGAS) |

### `UpdateConfig` <a href="#updateconfig" id="updateconfig"></a>

Updates the configuration of the Gov contract.

```rust
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum ExecuteMsg {
    UpdateConfig {
        owner: Option<String>, 
        quorum: Option<Decimal>, 
        threshold: Option<Decimal>, 
        voting_period: Option<u64>, 
        timelock_period: Option<u64>, 
        expiration_period: Option<u64>, 
        proposal_deposit: Option<Uint128>, 
        snapshot_period: Option<u64>,
    }
}
```

| Key                 | Type    | Description                                                                                                                   |
| ------------------- | ------- | ----------------------------------------------------------------------------------------------------------------------------- |
| `owner`             | String  | Address of contract owner                                                                                                     |
| `quorum`            | Decimal | `New` percentage of participation required for a poll to pass                                                                 |
| `threshold`         | Decimal | New percentage of `yes` votes required for a poll to pass                                                                     |
| `voting_period`     | u64     | <p>New number of blocks during which votes can be cast <strong>\[blocks]</strong><br></p>                                     |
| `timelock_period`   | u64     | <p>New number of blocks required after a poll pass before executing changes <strong>\[blocks]</strong><br></p>                |
| `expiration_period` | u64     | <p>New number of blocks after a poll's voting period during which the poll can be executed <strong>\[blocks]</strong><br></p> |
| `proposal_deposit`  | Uint128 | New minimum VEGAS deposit required for submitting a new poll                                                                  |
| `snapshot_period`   | u64     | New window of time (number of blocks) allowed for poll snapshot before a poll's end **\[blocks]**                             |

### `CastVote` <a href="#castvote" id="castvote"></a>

Submits a user's vote for an active poll. Once a user has voted, they cannot change their vote with subsequent messages (increasing voting power, changing vote option, cancelling vote, etc.)

```rust
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum ExecuteMsg {
    CastVote {
        poll_id: u64, 
        vote: VoteOption, 
        amount: Uint128, 
    }
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum VoteOption {
    Yes,
    No,
}
```

| Key       | Type       | Description                                       |
| --------- | ---------- | ------------------------------------------------- |
| `poll_id` | u64        | Poll ID                                           |
| `vote`    | VoteOption | Can be `yes` or `no`                              |
| `amount`  | Uint128    | Amount of voting power (staked VEGAS) to allocate |

### `WithdrawVotingTokens` <a href="#withdrawvotingtokens" id="withdrawvotingtokens"></a>

Removes specified amount of staked VEGAS tokens from a staking position and returns them to a user's balance. Withdraws all staked VEGAS tokens if `amount` is not specified.

```
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum ExecuteMsg {
    WithdrawVotingTokens {
        amount: Option<Uint128>, 
    }
}
```

| Key        | Type    | Description                        |
| ---------- | ------- | ---------------------------------- |
| `amount`\* | Uint128 | Amount of VEGAS tokens to withdraw |

\* = optional

### `EndPoll` <a href="#endpoll" id="endpoll"></a>

Can be issued by anyone to end the voting for an active poll. Triggers tally the results to determine whether the poll has passed. The current block height must exceed the end height of voting phase.

```
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum ExecuteMsg {
    EndPoll {
        poll_id: u64, 
    }
}
```

| Key       | Type | Description |
| --------- | ---- | ----------- |
| `poll_id` | u64  | Poll ID     |

### `ExecutePoll` <a href="#executepoll" id="executepoll"></a>

Can be issued by anyone to implement into action the contents of a passed poll. The current block height must exceed the end height of the poll's effective delay.

```
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum ExecuteMsg {
    ExecutePoll {
        poll_id: u64, 
    }
}
```

| Key       | Type | Description |
| --------- | ---- | ----------- |
| `poll_id` | u64  | Poll ID     |

### `SnapshotPoll` <a href="#snapshotpoll" id="snapshotpoll"></a>

Snapshots the total amount of staked VEGAS and stores the number to the specified poll. This staked VEGAS amount is used to determine the degree of participation for this poll, calculated by dividing the total amount of VEGAS voted to the poll with the total staked VEGAS supply at the time of [EndPoll](broken://pages/-MT4Yqv-t20eNkEGoSwy#endpoll). Can only be issued within a window of `snapshot_period` blocks before the poll's `end_height`.

```rust
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum ExecuteMsg {
    SnapshotPoll {
        poll_id: u64, 
    }
}
```

| Key       | Type | Description |
| --------- | ---- | ----------- |
| `poll_id` | u64  | Poll ID     |

### Receive Hooks <a href="#castvote" id="castvote"></a>

### `StakeVotingTokens` <a href="#stakevotingtokens" id="stakevotingtokens"></a>

{% hint style="danger" %}
**WARNING**\
\
Sending VEGAS tokens to the Gov contract without issuing this hook will lead to **PERMANENT LOSS OF FUNDS** and will be irrevocably donated to the reward pool for stakers.
{% endhint %}

### `CreatePoll` <a href="#createpoll" id="createpoll"></a>

Issued when sending VEGAS tokens to the Gov contract to create a new poll. Will only succeed if the amount of tokens sent meets the configured `proposal_deposit` amount. Can contain a list of generic messages to be issued by the Gov contract if it passes (can invoke messages in other contracts it owns).

```rust
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum Cw20HookMsg {
    CreatePoll {
        title: String, 
        description: String, 
        link: Option<String>, 
        execute_msgs: Option<Vec<PollExecuteMsg>>, 
    }
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct PollExecuteMsg {
    pub order: u64, 
    pub contract: String,
    pub msg: Binary,
}QueryMsg
```

### &#x20;<a href="#config-1" id="config-1"></a>

| Key               | Type                 | Description                                                                  |
| ----------------- | -------------------- | ---------------------------------------------------------------------------- |
| `title`           | String               | Poll title                                                                   |
| `description`     | String               | Poll description                                                             |
| `link`\*          | String               | URL to external post about poll (forum, PDF, etc.)                           |
| `execute_msgs`\*  | Vec\<PollExecuteMsg> | List of governance messages to be issued by Gov contract upon poll execution |
|                   |                      |                                                                              |
| `order`           | u64                  | Order sequence of message                                                    |
| `contract`        | String               | Contract address of governance message recipient                             |
| `msg`             | Binary               | New minimum VEGAS deposit required for submitting a new poll                 |
| `snapshot_period` | u64                  | Base64-encoded JSON of governance message                                    |

\* = optional

## QueryMsg

### `Config` <a href="#config-1" id="config-1"></a>

Gets the configuration for the Gov contract.

```rust
#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema)]
pub struct ConfigResponse {
    pub owner: String,
    pub token_contract: String,
    pub quorum: Decimal,
    pub threshold: Decimal,
    pub voting_period: u64,
    pub timelock_period: u64,
    pub expiration_period: u64,
    pub proposal_deposit: Uint128, 
    pub snapshot_period: u64, 
}
```

### `ConfigResponse`

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

```rust
#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema)]
pub struct ConfigResponse {
    pub owner: String,
    pub token_contract: String,
    pub quorum: Decimal,
    pub threshold: Decimal,
    pub voting_period: u64,
    pub timelock_period: u64,
    pub expiration_period: u64,
    pub proposal_deposit: Uint128, 
    pub snapshot_period: u64, 
}
```

{% endtab %}
{% endtabs %}

### `State` <a href="#state" id="state"></a>

Gets state information for the Gov contract.

```rust
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum QueryMsg {
    State {}
}
```

### `StateResponse` <a href="#stateresponse" id="stateresponse"></a>

```rust
#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema)]
pub struct StateResponse {
    pub poll_count: u64,
    pub total_share: Uint128,
    pub total_deposit: Uint128,
}
```

### `Staker` <a href="#staker" id="staker"></a>

Gets information for the specified VEGAS staker.

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

```rust
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum QueryMsg {
    Staker {
        address: String, 
    }
}
```

{% endtab %}
{% endtabs %}

### `StakerResponse` <a href="#stakerresponse" id="stakerresponse"></a>

```rust
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema)]
pub struct StakerResponse {
    pub balance: Uint128,
    pub share: Uint128,
    pub locked_balance: Vec<(u64, VoterInfo)>, // (Voted Poll's ID, VoterInfo)
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct VoterInfo {
    pub vote: VoteOption,
    pub balance: Uint128,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum VoteOption {
    Yes,
    No,
}Poll
```

### `Poll`

Gets information for the specified poll.

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

```rust
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum QueryMsg {
    Poll {
        poll_id: u64, 
    }
}
```

{% endtab %}

{% tab title="JSON" %}

```json
{
  "poll": {
    "poll_id": 8 
  }
}
```

{% endtab %}
{% endtabs %}

### `PollResponse`

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

```rust
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema)]
pub struct PollResponse {
    pub id: u64,
    pub creator: String,
    pub status: PollStatus,
    pub end_height: u64,
    pub title: String,
    pub description: String,
    pub link: Option<String>,
    pub deposit_amount: Uint128,
    pub execute_data: Option<PollExecuteMsg>,
    pub yes_votes: Uint128, // balance
    pub no_votes: Uint128,  // balance
    pub staked_amount: Option<Uint128>, 
    pub total_balance_at_end_poll: Option<Uint128>,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum PollStatus {
    InProgress,
    Passed,
    Rejected,
    Executed,
    Expired, // Deprecated 
    Failed, 
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct ExecuteMsg {
    pub order: u64, 
    pub contract: String,
    pub msg: Binary,
}
```

{% endtab %}

{% tab title="JSON" %}

```javascript
{
  "id": 8, 
  "creator": "terra1...", 
  "status": "executed", 
  "end_height": 123456, 
  "title": "...", 
  "description": "...", 
  "link": "https://...", 
  "deposit_amount": "100000000", 
  "execute_data": [
    {
      "order": 1, 
      "contract": "terra1...", 
      "msg": "eyAiZXhlY3V0ZV9tc2ciOiAiYmluYXJ5IiB9" 
    }, 
    {
      "order": 2, 
      "contract": "terra1...", 
      "msg": "eyAiZXhlY3V0ZV9tc2ciOiAiYmluYXJ5IiB9" 
    }
  ], 
  "yes_votes": "100000000", 
  "no_votes": "100000000", 
  "staked_amount": "100000000", 
  "total_balance_at_end_poll": "100000000" 
}
```

{% endtab %}
{% endtabs %}

### `Polls` <a href="#polls" id="polls"></a>

Gets information for all polls.

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

```rust
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum QueryMsg {
    Polls {
        filter: Option<PollStatus>, 
        start_after: Option<u64>, 
        limit: Option<u32>, 
        order_by: Option<OrderBy>, 
    }
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum PollStatus {
    InProgress,
    Passed,
    Rejected,
    Executed,
    Expired, // Deprecated 
    Failed, 
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum OrderBy {
    Asc,
    Desc,
}
```

{% endtab %}
{% endtabs %}

### `PollsResponse` <a href="#pollsresponse" id="pollsresponse"></a>

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

```rust
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema)]
pub struct PollsResponse {
    pub polls: Vec<PollResponse>,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema)]
pub struct PollResponse {
    pub id: u64,
    pub creator: String,
    pub status: PollStatus,
    pub end_height: u64,
    pub title: String,
    pub description: String,
    pub link: Option<String>,
    pub deposit_amount: Uint128,
    pub execute_data: Option<PollExecuteMsg>,
    pub yes_votes: Uint128, // balance
    pub no_votes: Uint128,  // balance
    pub staked_amount: Option<Uint128>, 
    pub total_balance_at_end_poll: Option<Uint128>,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum PollStatus {
    InProgress,
    Passed,
    Rejected,
    Executed,
    Expired, // Deprecated 
    Failed, 
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct PollExecuteMsg {
    pub order: u32, 
    pub contract: String,
    pub msg: Binary,
}
```

{% endtab %}
{% endtabs %}

### `Voters` <a href="#voters" id="voters"></a>

Gets voter information of the poll with the specified ID.

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

```rust
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum QueryMsg {
    Voters {
        poll_id: u64, 
        start_after: Option<String>, 
        limit: Option<u32>, 
        order_by: Option<OrderBy>, 
    }
}
```

{% endtab %}
{% endtabs %}

### `VotersResponse` <a href="#votersresponse" id="votersresponse"></a>

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

```rust
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema)]
pub struct VotersResponse {
    pub voters: Vec<VotersResponseItem>,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema)]
pub struct VotersResponseItem {
    pub voter: String,
    pub vote: VoteOption,
    pub balance: Uint128,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum VoteOption {
    Yes,
    No,
}
```

{% 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://terra-vegas.gitbook.io/docs/contracts/governance-contract.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.
