Skip to content

test-lexion/contract-tradefiv2

Repository files navigation

Trade Protocol

A comprehensive DeFi protocol built on Ethereum, featuring governance, staking, spot trading, and asset management with enhanced security features.

πŸ“‹ Overview

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

πŸš€ Key Features

πŸ” Security First

  • βœ… Comprehensive access controls
  • βœ… Emergency pause mechanisms
  • βœ… Timelock governance with guardian protection
  • βœ… Slippage protection on trades
  • βœ… Reentrancy protection across all contracts

πŸ›οΈ Advanced Governance

  • βœ… Token delegation system
  • βœ… Historical voting power snapshots
  • βœ… Proposal validation and timelock execution
  • βœ… Guardian emergency controls
  • βœ… Governance-controlled minting

πŸ’° Multi-Tier Staking

  • βœ… 4 staking tiers with increasing rewards (1x to 1.5x multipliers)
  • βœ… Early withdrawal penalties
  • βœ… Automatic reward compounding
  • βœ… Lock periods for higher tiers

πŸ“ˆ Spot Trading

  • βœ… Multi-token support with proper decimal handling
  • βœ… Gas-efficient internal settlement
  • βœ… Authorized price updater system
  • βœ… Trading fee collection

πŸ“Š Contract Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   TradeFiToken  β”‚    β”‚   Governance    β”‚    β”‚  StakingRewards β”‚
β”‚      (TFT)      │◄──►│                 │◄──►│                 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β–²                       β–²                       β–²
         β”‚                       β”‚                       β”‚
         β–Ό                       β–Ό                       β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   AssetVault    │◄──►│  SpotExchange   β”‚    β”‚   Emergency     β”‚
β”‚                 β”‚    β”‚                 β”‚    β”‚   Controls      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ› οΈ Installation & Setup

Prerequisites

Installation

  1. Clone the repository

    git clone https://github.com/your-org/contract-tradefiv2.git
    cd contract-tradefiv2
  2. Install dependencies

    forge install
  3. Set up environment

    cp .env.example .env
    # Edit .env with your configuration
  4. Compile contracts

    forge build
  5. Run tests

    forge test

πŸ§ͺ Testing

Run all tests

forge test -vvv

Run specific test file

forge test --match-path test/TradeFiIntegration.t.sol -vvv

Run with gas reporting

forge test --gas-report

Coverage report

forge coverage

πŸš€ Deployment

Local Deployment (Anvil)

# Start local node
anvil

# Deploy contracts
forge script scripts/Deploy.s.sol:DeployTradeFi --rpc-url http://localhost:8545 --broadcast

Testnet Deployment

# Deploy to Sepolia
./scripts/deploy.sh --network sepolia

# Deploy with verification
./scripts/deploy.sh --network sepolia --verify

Mainnet Deployment

# Dry run first
./scripts/deploy.sh --network mainnet --dry-run

# Actual deployment
./scripts/deploy.sh --network mainnet

βš™οΈ Configuration

Environment Variables

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

Post-Deployment Setup

  1. Configure Asset Vault

    assetVault.setAuthorizedContract(spotExchange, true);
    assetVault.setEmergencyAdmin(emergencyAdmin);
  2. Setup Price Updaters

    spotExchange.setPriceUpdater(oracleAddress, true);
  3. Transfer Governance

    tradeFiToken.transferGovernance(governanceAddress);
    tradeFiToken.activateGovernance(); // Irreversible!

πŸ“ Usage Examples

Staking Tokens

// 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

Trading Assets

// 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);

Governance Participation

// 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);

πŸ” Security Considerations

Implemented Protections

  • βœ… 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

Best Practices

  • 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

πŸ“œ Contract Addresses

Mainnet (TO BE DEPLOYED)

  • TradeFiToken: TBD
  • AssetVault: TBD
  • SpotExchange: TBD
  • StakingRewards: TBD
  • Governance: TBD

Sepolia Testnet

  • TradeFiToken: 0x...
  • AssetVault: 0x...
  • SpotExchange: 0x...
  • StakingRewards: 0x...
  • Governance: 0x...

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests for new functionality
  5. Ensure all tests pass
  6. Submit a pull request

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ†˜ Support

  • Create an issue for bug reports
  • Join our Discord for discussions
  • Check our documentation for detailed guides

πŸ”— Links


⚠️ Disclaimer: This software is provided as-is and has not been audited. Use at your own risk in production environments.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors