> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/reserve-protocol/reserve-index-dtf/llms.txt
> Use this file to discover all available pages before exploring further.

# Deployment

> Deploy Reserve Folio contracts to Ethereum, Base, and other networks

## Overview

Reserve Folio uses Foundry's scripting system for deterministic, reproducible deployments. The deployment process handles both genesis deployments (protocol infrastructure) and follow-up deployments (Folio instances).

## Deployment Architecture

Deployments occur in two phases:

<Steps>
  <Step title="Genesis Deployment">
    Deploy core protocol infrastructure:

    * `FolioDAOFeeRegistry`: DAO fee management
    * `FolioVersionRegistry`: Version control for upgrades
    * `TrustedFillerRegistry`: Whitelisted auction fillers
  </Step>

  <Step title="Protocol Deployment">
    Deploy protocol contracts:

    * `FolioDeployer`: Factory for creating Folios
    * `GovernanceDeployer`: Factory for governance systems
    * Implementation contracts (Governor, Timelock, StakingVault)
    * Periphery contracts (Fillers, Lens)
  </Step>
</Steps>

## Deployment Script

The main deployment script is located at `script/Deploy.s.sol`:

```solidity Deploy.s.sol theme={null}
contract DeployScript is Script {
    // Deployment modes
    enum DeploymentMode {
        Production,
        Testing
    }
    
    // Set deployment mode before running
    DeploymentMode public deploymentMode = DeploymentMode.Production;
    
    function run() external {
        DeploymentParams memory params = deploymentParams[block.chainid];
        runGenesisDeployment(params);
    }
}
```

## Supported Networks

### Production Networks

<CardGroup cols={3}>
  <Card title="Ethereum Mainnet" icon="ethereum">
    Chain ID: `1`
  </Card>

  <Card title="Base" icon="layer-group">
    Chain ID: `8453`
  </Card>

  <Card title="BNB Chain" icon="coins">
    Chain ID: `56`
  </Card>
</CardGroup>

### Network Configuration

Each network has predefined deployment parameters:

```solidity theme={null}
// Ethereum Mainnet
deploymentParams[1] = DeploymentParams({
    roleRegistry: 0xE1eC57C8EE970280f237863910B606059e9641C9,
    folioFeeRegistry: 0x0262E3e15cCFD2221b35D05909222f1f5FCdcd80,
    feeRecipient: 0xcBCa96091f43C024730a020E57515A18b5dC633B,
    folioVersionRegistry: 0xA665b273997F70b647B66fa7Ed021287544849dB,
    trustedFillerRegistry: 0x279ccF56441fC74f1aAC39E7faC165Dec5A88B3A
});
```

## Deployment Process

### Prerequisites

<Steps>
  <Step title="Private Key Setup">
    Create a `.seed` file containing your mnemonic phrase:

    ```bash theme={null}
    echo "your twelve word mnemonic phrase here" > .seed
    ```

    <Warning>
      Never commit `.seed` to version control. It's already in `.gitignore`.
    </Warning>
  </Step>

  <Step title="Environment Variables">
    Set required environment variables:

    ```bash .env theme={null}
    ETHERSCAN_KEY=your_etherscan_api_key
    ```
  </Step>

  <Step title="Fund Deployer Address">
    The deployer address (derived from your seed phrase) must have sufficient ETH for:

    * Gas fees (estimated: 0.1-0.5 ETH depending on network)
    * Contract creation costs
  </Step>
</Steps>

### Deployment Commands

#### Deploy to Ethereum Mainnet

```bash theme={null}
yarn deploy --rpc-url mainnet --verify --verifier etherscan --broadcast
```

#### Deploy to Base

```bash theme={null}
yarn deploy --rpc-url base --verify --verifier etherscan --broadcast
```

#### Deploy to BNB Chain

```bash theme={null}
yarn deploy --rpc-url bsc --verify --verifier etherscan --broadcast
```

<Info>
  The `--verify` flag automatically verifies contracts on block explorers. The API key is read from `ETHERSCAN_KEY` environment variable and works for all explorers (Etherscan, Basescan, BscScan).
</Info>

### Custom RPC Endpoint

Use a custom RPC URL:

```bash theme={null}
yarn deploy --rpc-url https://your-custom-rpc-url.com --verify --broadcast
```

## Deployment Modes

The deployment script supports two modes:

### Production Mode

```solidity theme={null}
DeploymentMode public deploymentMode = DeploymentMode.Production;
```

Uses canonical production parameters for:

* Role registries
* Fee recipients (DAO multisig)
* Version registries
* Trusted filler registries

### Testing Mode

```solidity theme={null}
DeploymentMode public deploymentMode = DeploymentMode.Testing;
```

Uses test parameters for:

* Development testing
* Integration testing
* Staging environments

<Warning>
  Always verify the deployment mode is set correctly before deploying to mainnet!
</Warning>

## Local Deployment

### Deploy to Local Anvil

<Steps>
  <Step title="Start Local Node">
    ```bash theme={null}
    yarn anvil
    ```
  </Step>

  <Step title="Deploy Contracts">
    In a new terminal:

    ```bash theme={null}
    yarn deploy --rpc-url http://127.0.0.1:8545 --broadcast
    ```
  </Step>
</Steps>

Local deployments automatically use mock contracts for:

* `MockRoleRegistry`: Simplified role management
* Burn address for fees (address(1))

## Deployment Output

Successful deployment outputs contract addresses:

```
----- INFO -----
Deployer: 0x1234567890123456789012345678901234567890
Chain: 1
Mode: Production

----- GENESIS -----
Running Genesis Deployment...
Folio Fee Registry: 0x0262E3e15cCFD2221b35D05909222f1f5FCdcd80
Folio Version Registry: 0xA665b273997F70b647B66fa7Ed021287544849dB
Trusted Filler Registry: 0x279ccF56441fC74f1aAC39E7faC165Dec5A88B3A

----- PROTOCOL -----
Running Followup Deployment...
Governance Deployer: 0xabcdef0123456789abcdef0123456789abcdef01
Folio Deployer: 0xfedcba9876543210fedcba9876543210fedcba98
CowSwap Filler: 0x1111111111111111111111111111111111111111
Folio Lens: 0x2222222222222222222222222222222222222222

----- DONE -----
```

<Note>
  Save these addresses for future reference and verification.
</Note>

## Contract Verification

Contracts are automatically verified when using the `--verify` flag. Manual verification:

```bash theme={null}
forge verify-contract \
  --chain-id 1 \
  --num-of-optimizations 200 \
  --watch \
  --compiler-version 0.8.28 \
  --etherscan-api-key $ETHERSCAN_KEY \
  <CONTRACT_ADDRESS> \
  contracts/Folio.sol:Folio
```

## Post-Deployment

### Verify Deployment

<Steps>
  <Step title="Check Contract Addresses">
    Verify all contracts deployed successfully and addresses are non-zero.
  </Step>

  <Step title="Verify Registry Configuration">
    ```bash theme={null}
    cast call <FOLIO_DEPLOYER_ADDRESS> "daoFeeRegistry()" --rpc-url mainnet
    cast call <FOLIO_DEPLOYER_ADDRESS> "versionRegistry()" --rpc-url mainnet
    ```
  </Step>

  <Step title="Verify Role Configuration">
    Ensure the role registry is correctly configured for the network.
  </Step>

  <Step title="Test Basic Functionality">
    Deploy a test Folio instance to verify the system works end-to-end.
  </Step>
</Steps>

### Register Version

After deployment, register the new Folio version:

```solidity theme={null}
// Called by DAO governance
folioVersionRegistry.registerVersion(
    address(folioDeployer),
    "5.0.0",
    true // deprecated
);
```

## Deployment Checklist

<Checklist>
  <Check>Verify deployment mode (Production vs Testing)</Check>
  <Check>Confirm `.seed` file is secure and funded</Check>
  <Check>Set `ETHERSCAN_KEY` environment variable</Check>
  <Check>Review network-specific parameters</Check>
  <Check>Run tests: `yarn test:all`</Check>
  <Check>Check contract sizes: `yarn size`</Check>
  <Check>Perform dry run (without `--broadcast`)</Check>
  <Check>Deploy with `--broadcast` flag</Check>
  <Check>Verify contracts on block explorer</Check>
  <Check>Save deployment addresses</Check>
  <Check>Test deployed contracts</Check>
  <Check>Update documentation with new addresses</Check>
</Checklist>

## Troubleshooting

<AccordionGroup>
  <Accordion title="Deployment fails with 'insufficient funds'">
    Ensure the deployer address has enough ETH for gas fees. Check your balance:

    ```bash theme={null}
    cast balance <DEPLOYER_ADDRESS> --rpc-url mainnet
    ```
  </Accordion>

  <Accordion title="Verification fails">
    * Verify the `ETHERSCAN_KEY` is correct
    * Check that the contract bytecode matches
    * Wait a few minutes and retry verification
    * Use the `--watch` flag for automatic retries
  </Accordion>

  <Accordion title="Wrong network parameters">
    The script automatically selects parameters based on `block.chainid`. Verify you're connected to the correct network:

    ```bash theme={null}
    cast chain-id --rpc-url <RPC_URL>
    ```
  </Accordion>

  <Accordion title="Nonce too low error">
    Another transaction may be pending. Wait for it to complete or increase the nonce manually.
  </Accordion>
</AccordionGroup>

## Security Considerations

<Warning>
  * **Never commit** private keys or seed phrases
  * **Use hardware wallets** for mainnet deployments when possible
  * **Verify** all addresses and parameters before broadcasting
  * **Test thoroughly** on testnets before mainnet deployment
  * **Use multisig** wallets for protocol ownership
</Warning>

## Next Steps

<CardGroup cols={2}>
  <Card title="Testing" icon="vial" href="/development/testing">
    Run comprehensive tests
  </Card>

  <Card title="Contributing" icon="code-pull-request" href="/development/contributing">
    Contribute to the protocol
  </Card>
</CardGroup>
