A comprehensive DeFi protocol built on Ethereum, featuring governance, staking, spot trading, and asset management with enhanced security features.
The TradeFi Protocol consists of 5 interconnected smart contracts:
- TradeFiToken (TFT): Governance token with delegation and snapshot capabilities
- AssetVault: Secure custody and management of protocol assets
- SpotExchange: Decentralized spot trading with slippage protection
- StakingRewards: Multi-tier staking system with reward boosts
- Governance: Timelock-secured governance with proposal validation
- ✅ Comprehensive access controls
- ✅ Emergency pause mechanisms
- ✅ Timelock governance with guardian protection
- ✅ Slippage protection on trades
- ✅ Reentrancy protection across all contracts
- ✅ Token delegation system
- ✅ Historical voting power snapshots
- ✅ Proposal validation and timelock execution
- ✅ Guardian emergency controls
- ✅ Governance-controlled minting
- ✅ 4 staking tiers with increasing rewards (1x to 1.5x multipliers)
- ✅ Early withdrawal penalties
- ✅ Automatic reward compounding
- ✅ Lock periods for higher tiers
- ✅ Multi-token support with proper decimal handling
- ✅ Gas-efficient internal settlement
- ✅ Authorized price updater system
- ✅ Trading fee collection
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ TradeFiToken │ │ Governance │ │ StakingRewards │
│ (TFT) │◄──►│ │◄──►│ │
└─────────────────┘ └─────────────────┘ └─────────────────┘
▲ ▲ ▲
│ │ │
▼ ▼ ▼
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ AssetVault │◄──►│ SpotExchange │ │ Emergency │
│ │ │ │ │ Controls │
└─────────────────┘ └─────────────────┘ └─────────────────┘
-
Clone the repository
git clone https://github.com/your-org/contract-tradefiv2.git cd contract-tradefiv2 -
Install dependencies
forge install
-
Set up environment
cp .env.example .env # Edit .env with your configuration -
Compile contracts
forge build
-
Run tests
forge test
forge test -vvvforge test --match-path test/TradeFiIntegration.t.sol -vvvforge test --gas-reportforge coverage# Start local node
anvil
# Deploy contracts
forge script scripts/Deploy.s.sol:DeployTradeFi --rpc-url http://localhost:8545 --broadcast# Deploy to Sepolia
./scripts/deploy.sh --network sepolia
# Deploy with verification
./scripts/deploy.sh --network sepolia --verify# Dry run first
./scripts/deploy.sh --network mainnet --dry-run
# Actual deployment
./scripts/deploy.sh --network mainnet| Variable | Description | Default |
|---|---|---|
DEPLOYER_PRIVATE_KEY |
Private key for deployment | Required |
FEE_WALLET |
Address to receive protocol fees | Deployer address |
EMERGENCY_ADMIN |
Emergency admin address | Deployer address |
GUARDIAN |
Governance guardian address | Deployer address |
INITIAL_SUPPLY |
Initial token supply (wei) | 100M tokens |
PROPOSAL_THRESHOLD |
Min tokens to create proposal | 10K tokens |
QUORUM_VOTES |
Min votes for proposal success | 400K tokens |
-
Configure Asset Vault
assetVault.setAuthorizedContract(spotExchange, true); assetVault.setEmergencyAdmin(emergencyAdmin);
-
Setup Price Updaters
spotExchange.setPriceUpdater(oracleAddress, true);
-
Transfer Governance
tradeFiToken.transferGovernance(governanceAddress); tradeFiToken.activateGovernance(); // Irreversible!
// Approve and stake tokens
tradeFiToken.approve(stakingRewards, amount);
stakingRewards.stake(amount);
// Check tier and rewards
(uint256 tierIndex, uint256 boost) = stakingRewards.getQualifiedTier(amount);
uint256 earned = stakingRewards.earned(user);
// Claim or compound rewards
stakingRewards.claimReward();
stakingRewards.compound(); // Auto-restake rewards// Deposit assets to vault
token.approve(assetVault, amount);
assetVault.deposit(tokenAddress, amount);
// Get trading quote
(uint256 amountOut, uint256 fee, uint256 amountAfterFee) =
spotExchange.getQuote(tokenA, tokenB, amountIn);
// Execute trade with slippage protection
spotExchange.executeTrade(tokenA, tokenB, amountIn, minAmountOut);// Delegate voting power
tradeFiToken.delegate(delegateAddress);
// Create proposal
uint256 proposalId = governance.propose(
targets, values, signatures, calldatas, description
);
// Vote on proposal
governance.castVote(proposalId, true); // true for support
// Queue and execute
governance.queue(proposalId);
// Wait for timelock...
governance.execute(proposalId);- ✅ Reentrancy guards on all state-changing functions
- ✅ Access control with role-based permissions
- ✅ Integer overflow protection (Solidity 0.8+)
- ✅ Emergency pause mechanisms
- ✅ Timelock governance with 2-day minimum delay
- ✅ Guardian can cancel malicious proposals
- Regular security audits recommended
- Use multi-signature wallets for admin functions
- Monitor for unusual activity
- Keep emergency admin keys secure and offline
- Test all governance proposals on testnets first
- TradeFiToken:
TBD - AssetVault:
TBD - SpotExchange:
TBD - StakingRewards:
TBD - Governance:
TBD
- TradeFiToken:
0x... - AssetVault:
0x... - SpotExchange:
0x... - StakingRewards:
0x... - Governance:
0x...
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Ensure all tests pass
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
- Create an issue for bug reports
- Join our Discord for discussions
- Check our documentation for detailed guides