A Foundry project for the Rebalancer contract with build, testing, and deployment functionality.
.
├── src/ # Contract source code
│ └── Rebalancer.sol
├── test/ # Tests
│ ├── Rebalancer.t.sol # Unit tests with mocks
│ └── integration/ # Integration tests
│ └── RebalancerIntegration.t.sol # Tests with real contracts
├── script/ # Deployment scripts
│ ├── Deploy.s.sol # Basic deployment script
│ ├── DeployAndTest.s.sol # Deployment script with integration tests
│ └── CloseAndWithdraw.s.sol # Close positions and withdraw script
├── deploy.sh # Basic deployment script
├── DeployAndTest.sh # Deployment with testing script
└── closeAndWithdraw.sh # Close and withdraw operations script
├── lib/ # Dependencies (OpenZeppelin, etc.)
└── foundry.toml # Foundry configuration
- Install Foundry:
curl -L https://foundry.paradigm.xyz | bash
foundryup- Clone the repository and install dependencies:
git clone <repository-url>
cd copy-trading-contract
forge installforge build# Run all tests
forge test
# Run a specific test
forge test --match-test testDeposit
# Run with verbose output
forge test -vvvCreate a .env file in the project root with the following variables:
PRIVATE_KEY=your_private_key_here
NFT_MANAGER_ADDRESS=0x...
GAUGE_ADDRESS=0x...
OWNER_ADDRESS=0x... # optional, defaults to deployer address
MAINNET_RPC_URL=https://...
SEPOLIA_RPC_URL=https://... # optional
ETHERSCAN_API_KEY=your_api_key # for verificationTwo deployment scripts are provided:
1. Basic Deployment (deploy.sh)
Simple deployment without integration tests:
./deploy.sh2. Deployment with Testing (DeployAndTest.sh)
Deployment with automatic integration testing:
./DeployAndTest.shBoth scripts:
- Automatically load variables from
.envfile - Detect network (Base/Mainnet/Sepolia) from RPC URL
- Handle verification appropriately for each network
- Provide clear instructions after deployment
- Start a local node:
anvil- Load environment variables and deploy:
source .env
forge script script/Deploy.s.sol:DeployScript --rpc-url anvil --broadcastsource .env
forge script script/Deploy.s.sol:DeployScript \
--rpc-url $SEPOLIA_RPC_URL \
--broadcast \
--verify \
-vvvvsource .env
forge script script/Deploy.s.sol:DeployScript \
--rpc-url $MAINNET_RPC_URL \
--broadcast \
--verify \
-vvvv- Uses
script/Deploy.s.sol:DeployScript - Quick deployment without integration tests
- Faster for production deployments
- Use when you just need to deploy quickly
- Uses
script/DeployAndTest.s.sol:DeployAndTestScript - Deploys and runs integration tests
- Verifies contract initialization and gauge integration
- Use when you want to verify everything works after deployment
Integration tests automatically check:
- Contract initialization
- Token retrieval from gauge
- ERC20 token balance checks
- Current position check
- Token metadata (symbols)
Note: Both scripts read all values from .env file. Make sure PRIVATE_KEY, NFT_MANAGER_ADDRESS, and GAUGE_ADDRESS are set.
Script to close all positions and withdraw all tokens and rewards from an existing Rebalancer contract:
./closeAndWithdraw.sh <REBALANCER_CONTRACT_ADDRESS>Example:
./closeAndWithdraw.sh 0x1234567890123456789012345678901234567890What it does:
-
Close All Positions: Closes all active Uniswap V3 positions
- Withdraws positions from gauge
- Decreases liquidity
- Collects all tokens and fees
- Burns the NFT position
-
Withdraw All Tokens: Withdraws all Token0 and Token1 to the owner
- Transfers all Token0 balance to owner
- Transfers all Token1 balance to owner
-
Withdraw Rewards: Withdraws all AERO reward tokens to the owner
- Gets reward token address from gauge
- Transfers all reward token balance to owner
Requirements:
PRIVATE_KEYin.envmust be the owner of the contractMAINNET_RPC_URLorSEPOLIA_RPC_URLmust be set in.env- Valid contract address as parameter
Note: The script validates that the deployer (from PRIVATE_KEY) is the owner of the contract before executing any operations.
Run integration tests on a production network fork (without real transactions):
# Load .env
source .env
# Run integration tests on Mainnet fork
forge test --match-contract RebalancerIntegrationTest \
--fork-url $MAINNET_RPC_URL \
-vv
# Run a specific test
forge test --match-test testGaugeTokens \
--fork-url $MAINNET_RPC_URL \
-vvvIntegration tests include:
testContractInitialization- initialization checktestGaugeTokens- token retrieval from gauge checktestTokenMetadata- token metadata checktestNFTManagerInterface- NFT Manager interface checktestGaugeInterface- Gauge interface checktestCurrentPosition- current position checktestAllReadOperations- all view functions checktestAddressesAreValid- address validation
Verification is attempted automatically during deployment. For Base network, auto-verification is skipped due to API V2 compatibility. After deployment, you'll see instructions for manual verification.
Use the provided verification script:
# Verify on Base network
./verify.sh <CONTRACT_ADDRESS> base
# Verify on Ethereum Mainnet
./verify.sh <CONTRACT_ADDRESS> mainnet
# Verify on Sepolia
./verify.sh <CONTRACT_ADDRESS> sepoliaOr use forge directly:
# For Base network
source .env
forge verify-contract \
<CONTRACT_ADDRESS> \
src/Rebalancer.sol:Rebalancer \
--chain base \
--etherscan-api-key $BASESCAN_API_KEY \
--num-of-optimizations 200 \
--compiler-version 0.8.28
# For Ethereum Mainnet/Sepolia
source .env
forge verify-contract \
<CONTRACT_ADDRESS> \
src/Rebalancer.sol:Rebalancer \
--chain mainnet \
--etherscan-api-key $ETHERSCAN_API_KEY \
--num-of-optimizations 200 \
--compiler-version 0.8.28Note: The configuration uses Etherscan API V2 format. For Base network, make sure to set BASESCAN_API_KEY in your .env file.
The foundry.toml file contains project settings:
- Compiler version:
0.8.28 - Optimizer enabled (200 runs)
- Remappings for OpenZeppelin
The project includes the following unit tests:
testConstructor- contract initialization checktestDeposit- token deposittestRebalance- position rebalancingtestCloseAllPositions- closing all positionstestWithdrawAll- withdrawing all fundstestRescueERC20- token rescue function- Access control tests (onlyOwner)
Run on a production network fork to test interaction with real contracts.
- OpenZeppelin Contracts v5.4.0
- Forge Standard Library (forge-std) v1.11.0
MIT