# GaugeFactoryV1

### Overview <a href="#overview" id="overview"></a>

**GaugeFactoryV1** is an upgradeable factory contract responsible for deploying and initializing **Gauge** contracts within the CrossCurve ecosystem. These gauges are used to manage and distribute rewards for various liquidity pools or other vote-driven incentives. The factory integrates with an **Escrow Vote Manager** to ensure that only the authorized vote manager can create new gauges, thus maintaining a secure and controlled environment for gauge deployments.

**Key Roles and Features:**

1. **Gauge Deployment:** Deploys a new **GaugeV1** instance as an upgradeable proxy (using **ERC1967Proxy**), specifying an implementation, an owner, and initial parameters for reward distribution campaigns.
2. **Access Control:** Restricts **createGauge** calls to the **escrow vote manager** (`s_escrowVoteManager`).
3. **Upgradeable via UUPS:** Uses **UUPSUpgradeable** and **OwnableUpgradeable** patterns, restricting contract upgrades to the owner.

By implementing **IGaugeFactoryV1**, **GaugeFactoryV1** provides a standardized interface and event structure for gauge creation, enabling other contracts in the CrossCurve ecosystem to request new gauges securely.

***

### Inherited Contracts and Interfaces <a href="#inherited-contracts-and-interfaces" id="inherited-contracts-and-interfaces"></a>

* **UUPSUpgradeable (OpenZeppelin):**\
  Provides upgrade functionality using the UUPS proxy pattern. Only the owner can authorize upgrades.
* **OwnableUpgradeable (OpenZeppelin):**\
  Establishes ownership and restricts certain critical functions (like upgrades) to the contract owner.
* **IGaugeFactoryV1:**\
  Declares functions (`initialize` and `createGauge`) and the `GaugeCreated` event. Also defines the `InvalidCaller` error.

**Additional External References:**

* **ERC1967Proxy (OpenZeppelin):**\
  A proxy implementation that stores the logic contract address in storage per EIP-1967.
* **IDistributionCreator.CampaignParameters:**\
  Used to initialize reward distribution parameters for newly created gauges.
* **GaugeV1:**\
  The gauge implementation contract being deployed as a proxy.

***

### State Variables <a href="#state-variables" id="state-variables"></a>

```solidity
address public s_escrowVoteManager;
```

* **`s_escrowVoteManager (address)`:**\
  The address of the escrow vote manager contract authorized to create new gauges. Only `s_escrowVoteManager` can call the `createGauge` function.

***

### Constructor <a href="#constructor" id="constructor"></a>

```solidity
constructor() {
    _disableInitializers();
}
```

* **Description:**\
  Disables initializers to prevent re-initialization in a UUPS upgradeable setup. Ensures the `initialize` function can only be called once.

***

### External Functions <a href="#external-functions" id="external-functions"></a>

#### `initialize(...)` <a href="#initialize" id="initialize"></a>

```solidity
function initialize(address owner_, address escrowVoteManager_) external initializer
```

* **Description:**\
  Initializes the factory contract by setting the contract owner (`owner_`) and the escrow vote manager (`escrowVoteManager_`). This function can only be called once due to the `initializer` modifier.
* **Parameters:**
  * `owner_`: The address designated as the owner of this factory.
  * `escrowVoteManager_`: The address of the authorized escrow vote manager contract.
* **Effects:**
  * Calls `__UUPSUpgradeable_init()` to set up the UUPS upgrade mechanism.
  * Calls `__Ownable_init(owner_)`, assigning ownership to `owner_`.
  * Sets `s_escrowVoteManager` to `escrowVoteManager_`.

***

#### `createGauge(...)` <a href="#creategauge" id="creategauge"></a>

```solidity
function createGauge(
    address owner_,
    address eywa_,
    IDistributionCreator.CampaignParameters calldata campaignParameters_
) 
    external 
    returns (address)
```

* **Description:**\
  Deploys and initializes a new **GaugeV1** contract as an **ERC1967Proxy**, passing in the gauge implementation address, initialization arguments, and returning the newly created gauge address.
* **Parameters:**
  * `owner_`: The address that will become the owner of the new gauge contract.
  * `eywa_`: The address of the EYWA token the gauge will handle for reward distribution.
  * `campaignParameters_`: A struct of parameters (e.g., schedule, amounts) used by the gauge for reward distribution.
* **Checks:**
  * The caller must be `s_escrowVoteManager`. Otherwise, `InvalidCaller()` is thrown.
* **Logic:**
  1. Deploys a new `GaugeV1` implementation contract.
  2. Instantiates an **ERC1967Proxy** pointing to that implementation.
  3. Encodes the constructor arguments (owner, escrow vote manager address, EYWA token address, campaign parameters) for the gauge’s `initialize` function call via the proxy.
  4. Emits the `GaugeCreated` event with the new gauge’s proxy address and the gauge implementation address.
* **Return:**
  * `address`: The newly deployed gauge proxy contract.
* **Events:**
  * `GaugeCreated(m_gauge, m_implementation)`: Indicates a new gauge contract was created.

***

### Internal Functions <a href="#internal-functions" id="internal-functions"></a>

#### `_authorizeUpgrade(address)` <a href="#authorizeupgrade-address" id="authorizeupgrade-address"></a>

```solidity
function _authorizeUpgrade(address) internal override onlyOwner
```

* **Description:**\
  Restricts the contract’s upgrade function (in a UUPS proxy context) to the owner, ensuring unauthorized parties cannot upgrade the factory logic.

***

### Events <a href="#events" id="events"></a>

#### `GaugeCreated(address indexed gauge, address indexed implementation)` <a href="#gaugecreated-address-indexed-gauge-address-indexed-implementation" id="gaugecreated-address-indexed-gauge-address-indexed-implementation"></a>

* **Emitted When:**\
  A new gauge contract is deployed via `createGauge`.
* **Parameters:**
  * `gauge`: The address of the newly created gauge (proxy).
  * `implementation`: The address of the gauge implementation contract used for the new proxy.

***

### Errors <a href="#errors" id="errors"></a>

#### `InvalidCaller()` <a href="#invalidcaller" id="invalidcaller"></a>

* **Description:**\
  Thrown if `createGauge` is called by an address other than `s_escrowVoteManager`. Ensures gauge creation is limited to the authorized escrow vote manager.

***

### Summary <a href="#summary" id="summary"></a>

**GaugeFactoryV1** securely and upgradeably deploys **GaugeV1** contracts under the control of the **escrow vote manager**. By enforcing that only the designated manager can call `createGauge`, it prevents unauthorized deployments while still allowing flexible, time-extended reward distribution campaigns. The combination of UUPS upgradeability, ownership checks, and a standardized creation event (`GaugeCreated`) supports a robust, maintainable environment for launching new gauges in the CrossCurve ecosystem.


---

# 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.crosscurve.fi/developer-documentation/guide-for-developers/technical-documentation-for-crosscurve-dao-smart-contracts/gaugefactoryv1.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.
