Overview
This guide covers deploying MetaVault AI smart contracts using Hardhat, including local development, testnet deployment, and mainnet deployment.
Prerequisites
Install Dependencies
Key Dependencies
Source: package.json
Environment Setup
Create a .env file in the contracts directory:
Never commit your .env file to version control. Add it to .gitignore.
Hardhat Configuration
Source: hardhat.config.ts:1-34
Hardhat Commands
Compile Contracts
This compiles all Solidity contracts and generates TypeChain typings.
Run Tests
Run Local Node
Starts a local Ethereum node with mainnet forking.
Deploy to Network
Deployment Script
The main deployment script deploys the Vault and integrates with the web app:
Source: scripts/deploy_vault_dashboard.ts:1-79
Deployment Steps
1. Local Development
Deploy to local Hardhat network with mainnet forking:
2. Testnet Deployment (Sepolia)
Sepolia LINK Token: 0xf8Fb3713D459D7C1018BD0A49D19b4C44290EBE5
3. Full System Deployment
Deploy Vault, Router, and Strategies:
Contract Addresses
Sepolia Testnet
Verify Contracts on Etherscan
Example:
Post-Deployment Configuration
1. Fund Strategies
2. Test Operations
3. Monitor Leverage
Gas Optimization
The contracts use several gas optimization techniques:
- Compiler Optimization: 200 runs for balanced optimization
- Via IR: Enabled for better optimization
- Immutable Variables: Used for addresses that never change
- Packed Storage: Careful ordering of state variables
- Short-Circuit Logic: Early returns to save gas
Source: hardhat.config.ts:7-15
Testing on Forked Network
Test against real protocols using Hardhat forking:
Source: hardhat.config.ts:18-23
Run tests:
Deployment Checklist
Common Issues
Issue: Transaction Reverts
Solution: Check that:
- Wallet has enough ETH for gas
- Token approvals are set
- Constructor arguments are correct
- Network is correctly configured
Issue: Contract Not Verified
Solution: Run verify command with exact constructor arguments:
Issue: Insufficient Liquidity
Solution: For leverage strategies on testnets:
- Use smaller amounts
- Reduce
borrowFactor
- Decrease
maxDepth
- Check pool liquidity before borrowing
Security Considerations
- Private Keys: Never commit or share private keys
- Constructor Arguments: Double-check all deployment parameters
- Access Control: Verify owner addresses are correct
- Fee Limits: Set reasonable fee percentages
- Strategy Validation: Test strategies thoroughly before deploying
- Upgrade Path: Consider proxy patterns for upgradeable contracts
- Timelock: Add timelock for sensitive operations on mainnet
Mainnet Deployment
Mainnet deployment requires extra caution. Consider:
- Professional audit
- Bug bounty program
- Gradual rollout with caps
- Multi-sig for admin functions
- Emergency pause mechanism
Mainnet Checklist
Next Steps