# Developer Setup

Ocean Protocol uses [Foundry](https://book.getfoundry.sh/) for smart contract development with a comprehensive testing and deployment framework.

## Prerequisites

### Core Requirements

* **Foundry**: Latest version for contract compilation and testing
* **Node.js**: v18+ for tooling and scripts
* **Git**: For repository management
* **Code Editor**: VS Code with Solidity extensions recommended

### Optional Tools

* **bun**: Fast package manager and script runner
* **Docker**: For containerized development
* **Hardhat**: Alternative testing framework (if needed)

## Environment Setup

### 1. Clone Repository

```bash
git clone https://github.com/ocean-finance/ocean-contract.git
cd ocean-contract
```

### 2. Install Dependencies

```bash
# Install Foundry dependencies
forge install

# Install Node.js dependencies (if using bun)
bun install
# OR with npm
npm install
```

### 3. Environment Configuration

```bash
# Copy environment template
cp .env.example .env

# Configure required variables
MNEMONIC="your bip39 mnemonic phrase"
ETHERSCAN_API_KEY="your etherscan api key"
ALCHEMY_API_KEY="your alchemy api key"
```

## Development Commands

### Contract Development

```bash
# Compile all contracts
forge build

# Clean build artifacts
forge clean

# Format Solidity code
forge fmt

# Check Solidity style
forge fmt --check
```

### Testing Framework

```bash
# Run all tests
forge test

# Run tests with gas reporting
forge test --gas-report

# Run specific test file
forge test --match-path test/unit/BaseHandler.t.sol

# Run tests with verbosity
forge test -vvv

# Generate coverage report
forge coverage

# HTML coverage report (with bun)
bun run test:coverage:report
```

### Linting and Quality

```bash
# Lint Solidity files
bun run lint

# Fix linting issues
bun run lint:fix

# Run security analysis
slither .

# Static analysis
mythril analyze contracts/
```

## Testing Strategies

### Unit Testing

```bash
# Test individual contract functions
forge test --match-path test/unit/

# Test specific function
forge test --match-test testStakeUsds
```

### Integration Testing

```bash
# Test cross-contract interactions
forge test --match-path test/integration/

# Test handler integration with StrategyAllocator
forge test --match-test testHandlerIntegration
```

### Fork Testing

```bash
# Test against mainnet fork
forge test --fork-url https://eth-mainnet.alchemyapi.io/v2/$ALCHEMY_API_KEY

# Test specific handler against live protocols
forge test --match-path test/fork/EthenaHandler.t.sol --fork-url $MAINNET_RPC
```

## Deployment

### Local Development

```bash
# Start local Anvil node
anvil

# Deploy to local network
forge script script/Deploy.s.sol --broadcast --fork-url http://localhost:8545
```

### Testnet Deployment

```bash
# Deploy to Sepolia
forge script script/Deploy.s.sol \
  --broadcast \
  --verify \
  --fork-url https://sepolia.infura.io/v3/$INFURA_KEY \
  --etherscan-api-key $ETHERSCAN_API_KEY
```

### Mainnet Deployment

```bash
# Deploy to Ethereum mainnet
forge script script/Deploy.s.sol \
  --broadcast \
  --verify \
  --fork-url https://mainnet.infura.io/v3/$INFURA_KEY \
  --etherscan-api-key $ETHERSCAN_API_KEY \
  --gas-estimate-multiplier 120
```

## Debugging and Troubleshooting

### Common Issues

1. **Compilation Errors**

   ```bash
   # Clear cache and rebuild
   forge clean && forge build
   ```
2. **Test Failures**

   ```bash
   # Run with maximum verbosity
   forge test -vvvv --match-test testFailingFunction
   ```
3. **Gas Optimization**

   ```bash
   # Generate gas report
   forge test --gas-report > gas-report.txt
   ```

### Development Tools

* **Foundry Debugger**: Step-through debugging for failed transactions
* **Trace Analysis**: Detailed execution traces for complex transactions
* **Storage Layout**: Analyze contract storage for optimization

## Best Practices

### Code Quality

* Follow Ocean's coding standards
* Use consistent naming conventions
* Implement comprehensive error handling
* Add detailed natspec documentation

### Security

* Always inherit from BaseHandler for handlers
* Use OpenZeppelin's security libraries
* Implement proper access controls
* Add reentrancy protection where needed

### Testing

* Aim for >95% test coverage
* Test both success and failure cases
* Include edge case testing
* Use fuzz testing for parameter validation

### Gas Optimization

* Use efficient data structures
* Minimize external calls
* Batch operations when possible
* Profile gas usage regularly

## Contributing

### Pull Request Process

1. Fork the repository
2. Create feature branch
3. Implement changes with tests
4. Ensure all tests pass
5. Submit pull request with description

### Code Review Requirements

* All tests must pass
* Code coverage must be maintained
* Security review for handler changes
* Documentation updates where needed

This development environment provides everything needed to build, test, and deploy Ocean Protocol contracts and handlers efficiently.
