Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
192 changes: 192 additions & 0 deletions docs/CHAIN_CONFIG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
# Chain Configuration System

## Overview

The chain configuration system provides a centralized location for all chain-specific deployment parameters, eliminating hardcoded values scattered across deployment scripts.

## Architecture

### ChainConfig Library (`src/contracts/libraries/ChainConfig.sol`)

A pure Solidity library that stores all chain-specific parameters:
- **Escrow Duration**: Number of blocks for escrow period
- **L2 Gas Calculator**: Address of L2 gas calculator (if needed)
- **Atlas Surcharge Rate**: Fee percentage for Atlas operations (in basis points)
- **Bundler Surcharge Rate**: Fee percentage for bundler operations (in basis points)

### Supported Chains

#### Mainnet Chains
| Chain | Chain ID | Escrow Duration | Block Time | L2 Gas Calculator |
|-------|----------|-----------------|------------|-------------------|
| Ethereum | 1 | 64 blocks | ~12s | Not required |
| Polygon | 137 | 64 blocks | ~2s | Not required |
| Optimism | 10 | 16 blocks | ~2s | Required (OP Stack) |
| Base | 8453 | 16 blocks | ~2s | Required (OP Stack) |
| Arbitrum* | 42161 | - | ~250ms | Not supported on main |
| Hyperliquid | 999 | 30 blocks | ~1s | Not required |
| Unichain | 130 | 30 blocks | ~1s | Required (OP Stack) |
| Berachain | 80094 | 15 blocks | ~5s | Not required |

*Arbitrum requires special handling and is supported on separate branches: `arbitrum/atlas-v1.6.1` or `arbitrum/atlas-v1.7-exp`

#### Testnet Chains
| Chain | Chain ID | Escrow Duration | Block Time | L2 Gas Calculator |
|-------|----------|-----------------|------------|-------------------|
| Sepolia | 11155111 | 64 blocks | ~12s | Not required |
| Polygon Amoy | 80002 | 64 blocks | ~2s | Not required |
| OP Sepolia | 11155420 | 16 blocks | ~2s | Required (OP Stack) |
| Base Sepolia | 84532 | 16 blocks | ~2s | Required (OP Stack) |
| Arbitrum Sepolia* | 421614 | - | ~250ms | Not supported on main |
| Unichain Sepolia | 1301 | 30 blocks | ~1s | Required (OP Stack) |
| Berachain Bepolia | 80069 | 15 blocks | ~5s | Not required |

*Arbitrum requires special handling and is supported on separate branches: `arbitrum/atlas-v1.6.1` or `arbitrum/atlas-v1.7-exp`

## Usage

### In Deployment Scripts

```solidity
import { ChainConfig } from "../src/contracts/libraries/ChainConfig.sol";

contract DeployAtlasScript is DeployBaseScript {
function run() external {
// Get chain-specific configuration
ChainConfig.ChainParameters memory chainParams = ChainConfig.getChainParameters(block.chainid);

// Use parameters in deployment
atlas = new Atlas({
escrowDuration: chainParams.escrowDuration,
atlasSurchargeRate: chainParams.atlasSurchargeRate,
l2GasCalculator: chainParams.l2GasCalculator,
// ... other parameters
});
}
}
```

### Helper Functions

```solidity
// Get chain name
string memory chainName = ChainConfig.getChainName(block.chainid);

// Check if L2 gas calculator is required
bool requiresCalc = ChainConfig.requiresL2GasCalculator(block.chainid);

// Get surcharge rates
ChainConfig.ChainParameters memory params = ChainConfig.getChainParameters(block.chainid);
uint256 atlasSurcharge = params.atlasSurchargeRate; // in basis points (1000 = 10%)
uint256 bundlerSurcharge = params.bundlerSurchargeRate; // in basis points
```

## Adding New Chains

To add support for a new chain:

1. Add the chain configuration in `ChainConfig.sol`:
```solidity
} else if (chainId == YOUR_CHAIN_ID) {
return ChainParameters({
escrowDuration: BLOCKS, // Calculate based on block time
l2GasCalculator: address(0), // Or specific address if needed
atlasSurchargeRate: 1000, // 10% default
bundlerSurchargeRate: 1000, // 10% default
name: "YOUR_CHAIN_NAME"
});
}
```

2. Update `requiresL2GasCalculator()` if the chain needs a gas calculator:
```solidity
function requiresL2GasCalculator(uint256 chainId) internal pure returns (bool) {
return chainId == 42161 || chainId == 421614 || // Arbitrum chains
chainId == YOUR_CHAIN_ID; // Add your chain if needed
}
```

## Escrow Duration Calculation

Escrow duration is calculated to achieve approximately 30-35 seconds of real time:

- **Ethereum/Sepolia**: 64 blocks × 12s = ~768s (adjusted for network conditions)
- **Polygon/Amoy**: 64 blocks × 2s = ~128s (adjusted for faster finality)
- **OP Stack (Optimism/Base)**: 16 blocks × 2s = ~32s
- **Arbitrum**: 128 blocks × 250ms = ~32s
- **Hyperliquid/Unichain**: 30 blocks × 1s = ~30s
- **Berachain**: 15 blocks × 5s = ~75s (Cosmos-based consensus)

## L2 Gas Calculator

L2 Gas Calculator requirements by chain type:
- **OP Stack chains (Base, Optimism, Unichain)**: Require L2 gas calculator for proper gas accounting
- **Automatic Deployment**: The Atlas deployment script automatically deploys an L2 gas calculator if one is required but not configured
- **Manual Deployment** (optional): Can be deployed separately using `script/deploy-gas-calculator-op-stack.s.sol`
- **Arbitrum**: Not supported on main branch - use branches `arbitrum/atlas-v1.6.1` or `arbitrum/atlas-v1.7-exp`
- **Other chains (Ethereum, Polygon, Hyperliquid, Berachain)**: Standard gas calculation, no L2 calculator needed

## Arbitrum Deployment

Arbitrum requires special handling due to its unique architecture and is maintained on separate branches:

### Available Arbitrum Branches
- `arbitrum/atlas-v1.6.1` - Stable version for Arbitrum One and Arbitrum Sepolia
- `arbitrum/atlas-v1.7-exp` - Experimental version with latest features
- `arbitrum/atlas-v1.1` - Legacy version (not recommended for new deployments)
- `arbitrum/atlas-v1.0` - Legacy version (not recommended for new deployments)

### Switching to Arbitrum Branch
```bash
# List available Arbitrum branches
git branch -r | grep arbitrum

# Checkout the desired Arbitrum branch
git checkout arbitrum/atlas-v1.6.1

# Deploy on Arbitrum
forge script script/deploy-atlas.s.sol --chain-id 42161 --broadcast
```

If you attempt to deploy on Arbitrum from the main branch, you'll receive an error directing you to use the appropriate Arbitrum branch.

## Migration from Hardcoded Values

Previous approach:
```solidity
// In deploy-atlas.s.sol
uint256 ESCROW_DURATION = 128; // Hardcoded
address L2_GAS_CALCULATOR = 0x870584...; // Hardcoded

// In deploy-base.s.sol
function _getSurchargeRates() internal view returns (...) {
if (chainId == 137) {
atlasSurchargeRate = 500; // Hardcoded
}
}
```

New approach:
```solidity
// All configuration in one place
ChainConfig.ChainParameters memory params = ChainConfig.getChainParameters(block.chainid);
// Use params.escrowDuration, params.l2GasCalculator, etc.
```

## Testing

Run the test script to verify configuration:
```bash
forge script script/test-chain-config.s.sol -vv
```

This will display all chain configurations and verify the values are correctly set.

## Benefits

1. **Single Source of Truth**: All chain parameters in one location
2. **Type Safety**: Compile-time checking prevents errors
3. **Easy Maintenance**: Add new chains or update parameters in one place
4. **Reduced Duplication**: No more hardcoded values across multiple scripts
5. **Better Documentation**: Clear overview of all chain configurations
6. **Testability**: Easy to verify all configurations
61 changes: 8 additions & 53 deletions script/base/deploy-base.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { Sorter } from "../../src/contracts/helpers/Sorter.sol";
import { SimpleRFQSolver } from "../../test/SwapIntent.t.sol";

import { Utilities } from "../../src/contracts/helpers/Utilities.sol";
import { ChainConfig } from "../../src/contracts/libraries/ChainConfig.sol";

contract DeployBaseScript is Script {
using stdJson for string;
Expand All @@ -35,62 +36,16 @@ contract DeployBaseScript is Script {

// Uses block.chainid to determine current chain to update deployments.json
function _getDeployChain() internal view returns (string memory) {
uint256 chainId = block.chainid;

if (chainId == 31_337) {
return "LOCAL";
} else if (chainId == 1) {
return "MAINNET";
} else if (chainId == 11_155_111) {
return "SEPOLIA";
} else if (chainId == 17_000) {
return "HOLESKY";
} else if (chainId == 137) {
return "POLYGON";
} else if (chainId == 80_002) {
return "AMOY";
} else if (chainId == 56) {
return "BSC";
} else if (chainId == 97) {
return "BSC TESTNET";
} else if (chainId == 10) {
return "OP MAINNET";
} else if (chainId == 11_155_420) {
return "OP SEPOLIA";
} else if (chainId == 42_161) {
return "ARBITRUM";
} else if (chainId == 421_614) {
return "ARBITRUM_SEPOLIA";
} else if (chainId == 8453) {
return "BASE";
} else if (chainId == 84_532) {
return "BASE SEPOLIA";
} else if (chainId == 80_094) {
return "BERACHAIN";
} else if (chainId == 80_069) {
return "BERACHAIN BEPOLIA";
} else if (chainId == 130) {
return "UNICHAIN";
} else if (chainId == 1301) {
return "UNICHAIN SEPOLIA";
} else if (chainId == 999) {
return "HYPERLIQUID";
} else {
revert(string.concat("Error: Chain ID not recognized: ", vm.toString(chainId)));
}
return ChainConfig.getChainName(block.chainid);
}

function _getSurchargeRates() internal view returns (uint256 atlasSurchargeRate, uint256 bundlerSurchargeRate) {
uint256 chainId = block.chainid;
if (chainId == 137 || chainId == 80_002) {
// POLYGON and AMOY
atlasSurchargeRate = 500; // 5%
bundlerSurchargeRate = 2000; // 20%
} else {
// Default - for all other chains
atlasSurchargeRate = 1000; // 10%
bundlerSurchargeRate = 1000; // 10%
}
ChainConfig.ChainParameters memory params = ChainConfig.getChainParameters(block.chainid);
return (params.atlasSurchargeRate, params.bundlerSurchargeRate);
}

function _getChainConfig() internal view returns (ChainConfig.ChainParameters memory) {
return ChainConfig.getChainParameters(block.chainid);
}

// NOTE: When handling JSON with StdJson, prefix keys with '.' e.g. '.ATLAS'
Expand Down
Loading