Skip to main content

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:
  1. Compiler Optimization: 200 runs for balanced optimization
  2. Via IR: Enabled for better optimization
  3. Immutable Variables: Used for addresses that never change
  4. Packed Storage: Careful ordering of state variables
  5. 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

  • Compile contracts: npx hardhat compile
  • Run tests: npx hardhat test
  • Configure .env with API keys and private key
  • Deploy Vault contract
  • Deploy StrategyRouter contract
  • Deploy Strategy contracts
  • Configure router with strategies and allocations
  • Connect vault to router
  • Verify contracts on Etherscan
  • Test deposit/withdraw flow
  • Test strategy operations (invest, harvest, rebalance)
  • Monitor gas costs and optimize if needed
  • Document deployed addresses
  • Update frontend with contract addresses and ABIs

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

  1. Private Keys: Never commit or share private keys
  2. Constructor Arguments: Double-check all deployment parameters
  3. Access Control: Verify owner addresses are correct
  4. Fee Limits: Set reasonable fee percentages
  5. Strategy Validation: Test strategies thoroughly before deploying
  6. Upgrade Path: Consider proxy patterns for upgradeable contracts
  7. 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

  • Complete professional security audit
  • Deploy to testnet and run for at least 1 week
  • Set up monitoring and alerting
  • Prepare emergency response plan
  • Use multi-sig wallet for ownership
  • Set initial deposit caps
  • Announce deployment and risk disclosures

Next Steps