> For the complete documentation index, see [llms.txt](https://docs.eywa.fi/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.eywa.fi/developer-documentation/guide-for-developers/technical-documentation-for-eywa-dao-smart-contracts/gaugefactoryv1.md).

# GaugeFactoryV1

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

**GaugeFactoryV1** is an upgradeable factory contract responsible for deploying and initializing **Gauge** contracts within the EYWA 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 EYWA ecosystem to request new gauges securely.

***

### Inherited Contracts and Interfaces <a href="#inherited-contracts-and-interfaces-4" id="inherited-contracts-and-interfaces-4"></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-3" id="state-variables-3"></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-4" id="constructor-4"></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-2" id="external-functions-2"></a>

#### `initialize(...)` <a href="#initialize-2" id="initialize-2"></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="#authorizeupgradeaddress-3" id="authorizeupgradeaddress-3"></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-3" id="events-3"></a>

#### `GaugeCreated(address indexed gauge, address indexed implementation)` <a href="#gaugecreatedaddress-indexed-gauge-address-indexed-implementation" id="gaugecreatedaddress-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-4" id="errors-4"></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-4" id="summary-4"></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 EYWA ecosystem.
