# Maker

## 1. Overview

The Maker contract collects fees from each Astroport pair (according to the factory's `maker_fee`) and tries to swap them to ASTRO. The ASTRO received from each swap is sent to xASTRO stakers and to vxASTRO stakers as well (according to the `governance_percent`).

The Maker contract code can be found [here](https://github.com/astroport-fi/astroport-core/blob/main/contracts/tokenomics/maker/src/contract.rs).

## 2. Variables & Functions

### Constants

| Name                          | Description                                                   |
| ----------------------------- | ------------------------------------------------------------- |
| `BRIDGES_INITIAL_DEPTH`       | The default bridge depth for a fee token                      |
| `BRIDGES_MAX_DEPTH`           | The max amount of bridges that a fee token can have           |
| `BRIDGES_EXECUTION_MAX_DEPTH` | Swap depth limit when swapping to ASTRO                       |
| `UUSD_DENOM`                  | UST token denomination                                        |
| `ULUNA_DENOM`                 | LUNA token denomination                                       |
| `CONFIG`                      | Stores the contract configuration at the given key            |
| `OWNERSHIP_PROPOSAL`          | Stores the latest proposal to change contract ownership       |
| `BRIDGES`                     | Stores bridge tokens used to swap fee tokens to ASTRO         |
| `CONTRACT_NAME`               | Contract name                                                 |
| `CONTRACT_VERSION`            | Contract version                                              |
| `DEFAULT_MAX_SPREAD`          | Default maximum spread used when swapping fee tokens to ASTRO |

### Structs

| Name               | Description                                                                                  | Contains                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
| ------------------ | -------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `InstantiateMsg`   | This struct holds parameters used to instantiate the Maker contract                          | <ul><li><code>owner</code> - Address allowed to change contract parameters</li><li><code>astro\_token\_contract</code> - The ASTRO token contract address</li><li><code>factory\_contract</code> - The factory contract address</li><li><code>staking\_contract</code> - The xASTRO staking contract address</li><li><code>governance\_contract</code> - The governance contract address (fee distributor for vxASTRO)</li><li><code>governance\_percent</code> - The percentage of fees that go to governance\_contract</li><li>max\_spread - The maximum spread used when swapping fee tokens to ASTRO</li></ul>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
| `ConfigResponse`   | This structure describes a custom struct used to return the contract configuration           | <ul><li><code>owner</code> - Address allowed to set contract parameters</li><li><code>astro\_token\_contract</code> - The ASTRO token contract address</li><li><code>factory\_contract</code> - The factory contract address</li><li><code>staking\_contract</code> - The xASTRO staking contract address</li><li><code>governance\_contract</code> - The governance contract address (fee distributor for vxASTRO stakers)</li><li><code>governance\_percent</code> - The percentage of fees that go to <code>governance\_contract</code></li><li><code>max\_spread</code> - The maximum spread used when swapping fee tokens to ASTRO</li><li><code>remainder\_reward</code> - The remainder ASTRO tokens (accrued before the Maker is upgraded) to be distributed to xASTRO stakers</li><li><code>pre\_upgrade\_astro\_amount</code> - The amount of ASTRO tokens accrued before upgrading the Maker implementation and enabling the reward distribution</li></ul>                                                                                                                                                                                                                                             |
| `BalancesResponse` | This struct is used to return multiple asset balances                                        | <ul><li><code>balances</code> - A vector of asset balances</li></ul>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
| `AssetWithLimit`   | This struct holds parameters to help with swapping a specific amount of a fee token to ASTRO | <ul><li><code>info</code> - Information about the fee token to swap</li><li><code>limit</code> - The amount of tokens to swap</li></ul>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| `Config`           | This structure stores the main parameters for the Maker contract                             | <ul><li><code>owner</code> - Address that's allowed to change contract parameters</li><li><code>factory\_contract</code> - The factory contract address</li><li><code>staking\_contract</code> - The xASTRO staking contract address</li><li><code>governance\_contract</code> - The governance contract address (fee distributor for vxASTRO stakers)</li><li><code>governance\_percent</code> - The percentage of fees that go to <code>governance\_contract</code></li><li><code>astro\_token\_contract</code> - The ASTRO token contract address</li><li><code>max\_spread</code> - The maximum spread used when swapping fee tokens to ASTRO</li><li><code>rewards\_enabled</code> - Whether reward distribution is enabled</li><li><code>pre\_upgrade\_blocks</code> - The number of blocks over which ASTRO that accrued pre Maker upgrade will be distributed</li><li><code>last\_distribution\_block</code> - The last block until which pre-upgrade ASTRO will be distributed</li><li><code>remainder\_reward</code> - The remainder of pre-upgrade ASTRO to distribute</li><li><code>pre\_upgrade\_astro\_amount</code> - The amount of collected ASTRO before enabling rewards distribution</li></ul> |

### Functions

| Name                 | Params                                                                                                                                                                                                                                                                  | Description                                                                                                                                                                                                                                    |
| -------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `instantiate`        | `(deps: DepsMut`, `_env: Env`, `_info: MessageInfo`, `msg: InstantiateMsg)` `->` `StdResult<Response>`                                                                                                                                                                  | Instantiate the Vesting contract                                                                                                                                                                                                               |
| `execute`            | `(deps: DepsMut`, `env: Env`, `info: MessageInfo`, `msg: ExecuteMsg)` `->` `Result<Response`, `ContractError>`                                                                                                                                                          | Execute a function from the contract                                                                                                                                                                                                           |
| `collect`            | `(deps: DepsMut`, `env: Env`, `assets: Vec<AssetWithLimit>)` `->` `Result<Response`, `ContractError>`                                                                                                                                                                   | Swaps fee tokens to ASTRO and distribute the resulting ASTRO to xASTRO and vxASTRO stakers                                                                                                                                                     |
| `swap_assets`        | `(deps: Deps`, `env: Env`, `cfg: &Config`, `assets: Vec<AssetWithLimit>`, `with_validation: bool)` `->` `Result<(Response, Vec)`, `ContractError>`                                                                                                                      | Swap non ASTRO tokens to ASTRO                                                                                                                                                                                                                 |
| `swap`               | `(deps: Deps`, `cfg: &Config`, `from_token: AssetInfo`, `amount_in: Uint128)` `->` `Result<SwapTarget`, `ContractError>`                                                                                                                                                | Internal function that checks if all required pools and bridges exists and performs a swap operation to ASTRO. `AssetInfo` is imported from the [asset file](https://docs.astroport.fi/astroport/smart-contracts/common-types-and-utils/asset) |
| `swap_no_validate`   | `(deps: Deps`, `cfg: &Config`, `from_token: AssetInfo`, `amount_in: Uint128)` `->` `Result<SwapTarget`, `ContractError>`                                                                                                                                                | Internal function that performs a swap operation to ASTRO without additional checks. `AssetInfo` is imported from the [asset file](https://docs.astroport.fi/astroport/smart-contracts/common-types-and-utils/asset)                           |
| `swap_bridge_assets` | `(deps: DepsMut`, `env: Env`, `info: MessageInfo`, `assets: Vec<AssetInfo>`, `depth: u64)` `->` `Result<Response`, `ContractError>`                                                                                                                                     | Internal function that swaps collected fees using bridge assets. `AssetInfo` is imported from the [asset file](https://docs.astroport.fi/astroport/smart-contracts/common-types-and-utils/asset)                                               |
| `update_config`      | `(deps: DepsMut`, `info: MessageInfo`, `factory_contract: Option<String>`, `staking_contract: Option<String>`, `governance_contract: Option<UpdateAddr>`, `governance_percent: Option<Uint64>`, `max_spread: Option<Decimal>)` `->` `Result<Response`, `ContractError>` | Updates general contract parameters                                                                                                                                                                                                            |
| `update_bridges`     | `(deps: DepsMut`, `info: MessageInfo`, `add: Option<Vec<(AssetInfo`, `AssetInfo)>>`, `remove: Option<Vec<AssetInfo>>)` `->` `Result<Response`, `ContractError>`                                                                                                         | Adds or removes bridge tokens used to swap fee tokens to ASTRO. Can only be called by governance. `AssetInfo` is imported from the [asset file](https://docs.astroport.fi/astroport/smart-contracts/common-types-and-utils/asset)              |
| `query`              | `(deps: Deps`, `env: Env`, `msg: QueryMsg)` `->` `StdResult<Binary>`                                                                                                                                                                                                    | Exposes all the queries available in the contract                                                                                                                                                                                              |
| `query_get_config`   | `(deps: Deps)` `->` `StdResult<ConfigResponse>`                                                                                                                                                                                                                         | Returns information about the Maker configuration                                                                                                                                                                                              |
| `query_get_balances` | `(deps: Deps`, `env: Env`, `assets: Vec)` `->` `StdResult<BalancesResponse>`                                                                                                                                                                                            | Returns the Maker's fee token balances for specific tokens                                                                                                                                                                                     |
| `query_bridges`      | `(deps: Deps`, `_env: Env)` `->` `StdResult<Vec<(String`, `String)>>`                                                                                                                                                                                                   | Returns bridge tokens used for swapping fee tokens to ASTRO                                                                                                                                                                                    |
| `query_pair`         | `(deps: Deps`, `contract_addr: Addr)` `->` `StdResult<[AssetInfo; 2]>`                                                                                                                                                                                                  | Returns asset information for the specified pair. `AssetInfo` is imported from the [asset file](https://docs.astroport.fi/astroport/smart-contracts/common-types-and-utils/asset)                                                              |
| `migrate`            | `(deps: DepsMut`, `_env: Env`, `_msg: MigrateMsg)` `->` `Result<Response`, `ContractError>`                                                                                                                                                                             | Used for contract migration                                                                                                                                                                                                                    |

## 3. Walkthrough

Part of the swap fees from each Astroport pool are sent to the Maker contract. The Maker is designed to swap fee tokens to ASTRO and then send ASTRO to xASTRO and vxASTRO stakers.

#### `collect`

Any address can execute the `collect` operation to swap all the available fee token balances to the ASTRO token and send ASTRO to stakers.&#x20;

```
{
  "collect": {
    "pair_addresses": [
      "terra...",
      "terra..."
    ]
  }
}
```

#### `balances`

An address can query for `balances` to return token balances for each specified asset held by the Maker.

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

```
{
  "balances": {
    "assets": [
      {
        "token": {
          "contract_addr": "terra..."
        }
      },
      {
        "native_token": {
          "denom": "uusd"
        }
      }
    ]
  }
}
```

{% endtab %}

{% tab title="ANC-UST Ex. Response" %}

```
{
  "balances": [
    {
      "info": {
        "token": {
          "contract_addr": "terra14z56l0fp2lsf86zy3hty2z47ezkhnthtr9yq76"
        }
      },
      "amount": "212524075688"
    },
    {
      "info": {
        "native_token": {
          "denom": "uusd"
        }
      },
      "amount": "3820471519586"
    }
  ]
}
```

{% endtab %}

{% tab title="bLUNA-LUNA Ex. Response" %}

```
{
  "balances": [
    {
      "info": {
        "token": {
          "contract_addr": "terra1kc87mu460fwkqte29rquh4hc20m54fxwtsx7gp"
        }
      },
      "amount": "7846111444"
    },
    {
      "info": {
        "native_token": {
          "denom": "uluna"
        }
      },
      "amount": "51036755763"
    }
  ]
}
```

{% endtab %}
{% endtabs %}

**`query_bridges`**

An address can query the query\_bridges operation to return the bridges used for swapping fee tokens denoting Asset -> Bridge connections.

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

```
{
  "query_bridges": {}
}
```

{% endtab %}
{% endtabs %}

## 4. Swap Details

### Bridges

The Maker contract uses bridge tokens in order to swap different fee tokens to ASTRO. Each fee token can have up to two bridge tokens (e.g A -> B -> UST -> ASTRO). The reason is that many tokens may not have direct pairs with UST, LUNA, or ASTRO and so they cannot be easily swapped to ASTRO without specifying a custom path.

**`swap_bridge_assets`**

&#x20;The Maker contract executes the **`swap_bridge_assets`**&#x6F;peration to swap collected fees using bridge assets. There is an option to use one or two bridge tokens to swap a fee token to ASTRO. Each fee token can have **`bridge_1`** and **`bridge_2`** set up for this purpose.

* In some cases, only **`bridge_1`** is specified and **`bridge_2`** will be null. If that's the case, the Maker performs the following swaps: Fee Token → bridge\_1 → ASTRO. If this fails, the function reverts
* In other cases, both **`bridge_1`** and **`bridge_2`** will be specified. If that's the case, the contract performs the following swaps: Fee Token → bridge\_1 → bridge\_2 → ASTRO. If this fails, the call reverts
* If both bridges are null, the swap cannot be executed

**Restrictions:**

* If only **`bridge_1`** is specified, the contract will check if the **`bridge_1`** token has a direct pair with ASTRO
* If both bridges are specified, the contract will check if **`bridge_2`** has a direct pair with ASTRO

### Flow of Swapping Fee Tokens to ASTRO

The latest flow to swap fee tokens is the following:

1. Check if the fee token is UST, in which case, swap directly to ASTRO
2. Check if the fee token is ASTRO itself, in which case there's no need for a swap
3. Check if the fee token has bridges set up. If yes, use the bridges to swap to ASTRO&#x20;
4. Check if the token has a direct pair with UST. If yes, execute the following swaps: Fee Token → UST → ASTRO
5. Check if the token has a direct pair with LUNA. If yes, execute the following swaps: Fee Token → LUNA -> UST → ASTRO
6. If all the above fails, check if the token has a direct pair with ASTRO. If yes, swap it directly

#### Swapping a Portion of the Total Fees

**`collect`** and **`swap_bridge_assets`** have the option to specify an amount of fee tokens to swap that is smaller than the total amount of fee tokens that the Maker accrued.

This option exists because in some cases, the Maker may accumulate so much of a particular token that swapping everything at once becomes unfeasible.

**Checks & restrictions:**

* If the **`collect`** or **`swap_bridge_assets`** caller specifies an amount of tokens that’s higher than the amount held by the Maker, the Maker will use the total amount of fee tokens it holds
* If the **`collect`** or **`swap_bridge_assets`** caller specifies 0 or NULL for the amount of tokens to swap, the Maker will use the total amount of fee tokens it holds

## 5. Risks

1. Fee swaps to ASTRO can be frontrun, resulting in suboptimal execution.
2. Bridge tokens may be set up incorrectly, resulting in suboptimal fee token swaps.
3. A major bug may lead to the Maker being drained of most or all fee tokens accrued in it.


---

# 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://astroport.gitbook.io/astroport/smart-contracts/tokenomics/maker.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.
