# Aggregator Contract

The Aggregator Contract implements the functions required in the [Aggregator Interface](https://dev.bondprotocol.finance/smart-contracts/bond-system/aggregator/aggregator-interface).&#x20;

Additionally, it implements state variables for the management of markets across multiple Auctioneers, tracking whitelisted Auctioneers.

The Aggregator contract keeps a unique set of market IDs across multiple Tellers and Auctioneers. Additionally, it aggregates market data from multiple Auctioneers in convenient view functions for front-end interfaces.

## Methods

### currentCapacity

```solidity
function currentCapacity(uint256 id_) external view returns (uint256)
```

Returns current capacity of a market

#### Parameters

| Name | Type    | Description |
| ---- | ------- | ----------- |
| id\_ | uint256 | Market ID   |

#### Returns

| Name     | Type    | Description                    |
| -------- | ------- | ------------------------------ |
| capacity | uint256 | Current capacity of the market |

### findMarketFor

```solidity
function findMarketFor(address payout_, address quote_, uint256 amountIn_, uint256 minAmountOut_, uint256 maxExpiry_) external view returns (uint256)
```

Returns the market ID with the highest current payoutToken payout for depositing quoteToken

#### Parameters

| Name           | Type    | Description                                                                                                           |
| -------------- | ------- | --------------------------------------------------------------------------------------------------------------------- |
| payout\_       | address | Address of payout token                                                                                               |
| quote\_        | address | Address of quote token                                                                                                |
| amountIn\_     | uint256 | Amount of quote tokens to deposit                                                                                     |
| minAmountOut\_ | uint256 | Minimum amount of payout tokens to receive as payout                                                                  |
| maxExpiry\_    | uint256 | Latest acceptable vesting timestamp for bond Inputting the zero address will take into account just the protocol fee. |

#### Returns

| Name | Type    | Description |
| ---- | ------- | ----------- |
| id\_ | uint256 | Market ID   |

### getAuctioneer

```solidity
function getAuctioneer(uint256 id_) external view returns (contract IBondAuctioneer)
```

Get the auctioneer for the provided market ID

#### Parameters

| Name | Type    | Description |
| ---- | ------- | ----------- |
| id\_ | uint256 | Market ID   |

#### Returns

| Name       | Type                     | Description                                        |
| ---------- | ------------------------ | -------------------------------------------------- |
| auctioneer | contract IBondAuctioneer | Address of the auctioneer that provides the market |

### getTeller

```solidity
function getTeller(uint256 id_) external view returns (contract IBondTeller)
```

Returns the Teller that services the market ID

#### Parameters

| Name | Type    | Description |
| ---- | ------- | ----------- |
| id\_ | uint256 | Market ID   |

#### Returns

| Name   | Type                 | Description                                    |
| ------ | -------------------- | ---------------------------------------------- |
| teller | contract IBondTeller | Address of the teller that services the market |

### isInstantSwap

```solidity
function isInstantSwap(uint256 id_) external view returns (bool)
```

Does market send payout immediately

#### Parameters

| Name | Type    | Description             |
| ---- | ------- | ----------------------- |
| id\_ | uint256 | Market ID to search for |

#### Returns

| Name      | Type | Description                                                                    |
| --------- | ---- | ------------------------------------------------------------------------------ |
| isInstant | bool | Whether the market is instant swap (true) or has vesting of the payout (false) |

### isLive

```solidity
function isLive(uint256 id_) external view returns (bool)
```

Is a given market accepting purchases

#### Parameters

| Name | Type    | Description  |
| ---- | ------- | ------------ |
| id\_ | uint256 | ID of market |

#### Returns

| Name   | Type | Description                               |
| ------ | ---- | ----------------------------------------- |
| isLive | bool | Whether the market is accepting purchases |

### liveMarketsBetween

```solidity
function liveMarketsBetween(uint256 firstIndex_, uint256 lastIndex_) external view returns (uint256[])
```

Returns array of active market IDs within a range

*Should be used if length exceeds max to query entire array*

#### Parameters

| Name         | Type    | Description                 |
| ------------ | ------- | --------------------------- |
| firstIndex\_ | uint256 | Start index of market range |
| lastIndex\_  | uint256 | End index of market range   |

#### Returns

| Name | Type       | Description    |
| ---- | ---------- | -------------- |
| ids  | uint256\[] | IDs of markets |

### liveMarketsBy

```solidity
function liveMarketsBy(address owner_) external view returns (uint256[])
```

Returns an array of all active market IDs for a given owner

#### Parameters

| Name    | Type    | Description                  |
| ------- | ------- | ---------------------------- |
| owner\_ | address | Address of owner to query by |

#### Returns

| Name | Type       | Description    |
| ---- | ---------- | -------------- |
| ids  | uint256\[] | IDs of markets |

### liveMarketsFor

```solidity
function liveMarketsFor(address token_, bool isPayout_) external view returns (uint256[])
```

Returns an array of all active market IDs for a given quote token

#### Parameters

| Name       | Type    | Description                                                  |
| ---------- | ------- | ------------------------------------------------------------ |
| token\_    | address | Address of token to query by                                 |
| isPayout\_ | bool    | If true, search by payout token, else search for quote token |

#### Returns

| Name | Type       | Description    |
| ---- | ---------- | -------------- |
| ids  | uint256\[] | IDs of markets |

### marketCounter

```solidity
function marketCounter() external view returns (uint256)
```

Counter for bond markets on approved auctioneers

#### Returns

| Name   | Type    | Description                   |
| ------ | ------- | ----------------------------- |
| nextId | uint256 | Next market ID to be assigned |

### marketPrice

```solidity
function marketPrice(uint256 id_) external view returns (uint256)
```

Calculate current market price of payout token in quote tokens

*Accounts for debt and control variable decay since last deposit (vs \_marketPrice())*

#### Parameters

| Name | Type    | Description  |
| ---- | ------- | ------------ |
| id\_ | uint256 | ID of market |

#### Returns

| Name  | Type    | Description                                              |
| ----- | ------- | -------------------------------------------------------- |
| price | uint256 | Price for market (see the specific auctioneer for units) |

### marketScale

```solidity
function marketScale(uint256 id_) external view returns (uint256)
```

Scale value to use when converting between quote token and payout token amounts with marketPrice()

#### Parameters

| Name | Type    | Description  |
| ---- | ------- | ------------ |
| id\_ | uint256 | ID of market |

#### Returns

| Name  | Type    | Description                                                                 |
| ----- | ------- | --------------------------------------------------------------------------- |
| scale | uint256 | Scaling factor for market in configured decimals (see auctioneer for units) |

### marketsFor

```solidity
function marketsFor(address payout_, address quote_) external view returns (uint256[])
```

Returns an array of all active market IDs for a given payout and quote token

#### Parameters

| Name     | Type    | Description             |
| -------- | ------- | ----------------------- |
| payout\_ | address | Address of payout token |
| quote\_  | address | Address of quote token  |

#### Returns

| Name | Type       | Description    |
| ---- | ---------- | -------------- |
| ids  | uint256\[] | IDs of markets |

### marketsForPayout

```solidity
function marketsForPayout(address, uint256) external view returns (uint256)
```

Market IDs for payout token

#### Parameters

| Name          | Type    | Description             |
| ------------- | ------- | ----------------------- |
| payoutToken\_ | address | Address of payout token |

#### Returns

| Name | Type    | Description    |
| ---- | ------- | -------------- |
| ids  | uint256 | IDs of markets |

### marketsForQuote

```solidity
function marketsForQuote(address) external view returns (uint256)
```

Market IDs for quote token

#### Parameters

| Name         | Type    | Description            |
| ------------ | ------- | ---------------------- |
| quoteToken\_ | address | Address of quote token |

#### Returns

| Name | Type    | Description    |
| ---- | ------- | -------------- |
| ids  | uint256 | IDs of markets |

### marketsToAuctioneers

```solidity
function marketsToAuctioneers(uint256) external view returns (contract IBondAuctioneer)
```

Auctioneer for Market ID

#### Parameters

| Name | Type    | Description  |
| ---- | ------- | ------------ |
| id\_ | uint256 | ID of market |

#### Returns

| Name       | Type                     | Description                     |
| ---------- | ------------------------ | ------------------------------- |
| auctioneer | contract IBondAuctioneer | Auctioneer that provides market |

### maxAmountAccepted

```solidity
function maxAmountAccepted(uint256 id_, address referrer_) external view returns (uint256)
```

Returns maximum amount of quote token accepted by the market

#### Parameters

| Name       | Type    | Description                                                                                                                                         |
| ---------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------- |
| id\_       | uint256 | ID of market                                                                                                                                        |
| referrer\_ | address | Address of referrer, used to get fees to calculate accurate payout amount. Inputting the zero address will take into account just the protocol fee. |

#### Returns

| Name     | Type    | Description                                                               |
| -------- | ------- | ------------------------------------------------------------------------- |
| amountIn | uint256 | Max amount of quote token that can currently be exchanged with the market |

### payoutFor

```solidity
function payoutFor(uint256 amount_, uint256 id_, address referrer_) external view returns (uint256)
```

Payout due for amount of quote tokens

*Accounts for debt and control variable decay so it is up to date*

#### Parameters

| Name       | Type    | Description                                                                                                                                         |
| ---------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------- |
| amount\_   | uint256 | Amount of quote tokens to spend                                                                                                                     |
| id\_       | uint256 | ID of market                                                                                                                                        |
| referrer\_ | address | Address of referrer, used to get fees to calculate accurate payout amount. Inputting the zero address will take into account just the protocol fee. |

#### Returns

| Name   | Type    | Description                        |
| ------ | ------- | ---------------------------------- |
| payout | uint256 | amount of payout tokens to be paid |

### registerAuctioneer

```solidity
function registerAuctioneer(contract IBondAuctioneer auctioneer_) external nonpayable
```

Register a auctioneer with the aggregatorOnly Guardian

*A auctioneer must be registered with an aggregator to create markets*

#### Parameters

| Name         | Type                     | Description                           |
| ------------ | ------------------------ | ------------------------------------- |
| auctioneer\_ | contract IBondAuctioneer | Address of the Auctioneer to register |

### registerMarket

```solidity
function registerMarket(contract ERC20 payoutToken_, contract ERC20 quoteToken_) external nonpayable returns (uint256 marketId)
```

Register a new market with the aggregatorOnly registered depositories

#### Parameters

| Name          | Type           | Description                        |
| ------------- | -------------- | ---------------------------------- |
| payoutToken\_ | contract ERC20 | Token to be paid out by the market |
| quoteToken\_  | contract ERC20 | Token to be accepted by the market |

#### Returns

| Name     | Type    | Description  |
| -------- | ------- | ------------ |
| marketId | uint256 | ID of market |

## Errors

### Aggregator\_OnlyAuctioneer

```solidity
error Aggregator_OnlyAuctioneer()
```
