Skip to content

ZieldProtocol/contracts

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Zield Protocol - Smart Contracts

A multi-protocol CDP (Collateralized Debt Position) platform that accepts yield-bearing tokens from various DeFi protocols as collateral. Users can deposit assets like stETH (Lido), aUSDC (Aave), cDAI (Compound) and borrow stablecoins while continuing to earn yield on their collateral.

🌟 Features

  • Multi-Protocol Support: Accept yield-bearing tokens from Aave, Lido, Compound, and more
  • Yield Preservation: Continue earning yield on deposited collateral
  • Zield Receipt Tokens: Receive protocol-specific receipt tokens (zstETH, zaUSDC, etc.)
  • Flexible Collateral: Mix and match different yield-bearing tokens
  • Aggregated APY: View weighted average yield across all positions
  • Health Factor Monitoring: Real-time position health tracking
  • Liquidation Protection: 150% collateralization ratio with 125% liquidation threshold

πŸ“¦ Project Structure

contracts/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ MultiProtocolCDP.sol      # Main CDP contract
β”‚   β”œβ”€β”€ VaultRegistry.sol         # Protocol adapter registry
β”‚   β”œβ”€β”€ ZieldToken.sol            # Receipt token implementation
β”‚   β”œβ”€β”€ adapters/
β”‚   β”‚   β”œβ”€β”€ AaveAdapter.sol       # Aave V3 adapter
β”‚   β”‚   β”œβ”€β”€ LidoAdapter.sol       # Lido staking adapter
β”‚   β”‚   └── CompoundAdapter.sol   # Compound V3 adapter
β”‚   └── interfaces/
β”‚       └── IProtocolAdapter.sol  # Adapter interface
β”œβ”€β”€ script/
β”‚   └── Deploy.s.sol              # Deployment script
β”œβ”€β”€ test/
β”‚   └── MultiProtocolCDP.t.sol    # Contract tests
└── foundry.toml                  # Foundry configuration

πŸ—οΈ Architecture

Core Contracts

  1. MultiProtocolCDP: Main contract handling deposits, withdrawals, borrowing, and repayments
  2. VaultRegistry: Central registry managing protocol adapters and token whitelisting
  3. ZieldToken: ERC20 receipt tokens representing user collateral positions

Protocol Adapters

  • AaveAdapter: Handles Aave V3 aTokens (aUSDC, aDAI, etc.)
  • LidoAdapter: Manages Lido stETH positions
  • CompoundAdapter: Integrates with Compound V3 cTokens

πŸš€ Getting Started

Prerequisites

Installation

# Clone the repository
git clone <repository-url>
cd contracts

# Install dependencies
forge install

# Build contracts
forge build

Configuration

  1. Copy environment example:
cp .env.example .env
  1. Configure .env:
PRIVATE_KEY=your_private_key_here
BASE_TESTNET_RPC_URL=https://base-sepolia.drpc.org
BASE_TESTNET_RPC_BACKUP=https://sepolia.base.org
BASE_EXPLORER_API_KEY=your_basescan_api_key

πŸ§ͺ Testing

Run the test suite:

# Run all tests
forge test

# Run tests with verbosity
forge test -vvv

# Run specific test
forge test --match-test testDepositCollateral

# Generate gas report
forge test --gas-report

# Check coverage
forge coverage

πŸ“€ Deployment

Deploy to Base Sepolia Testnet

Using the deployment script:

# Make deploy script executable
chmod +x deploy.sh

# Run deployment
./deploy.sh

Or manually with Forge:

forge script script/Deploy.s.sol:DeployScript \
    --rpc-url $BASE_TESTNET_RPC_URL \
    --private-key $PRIVATE_KEY \
    --broadcast \
    --verify \
    --etherscan-api-key $BASE_EXPLORER_API_KEY \
    -vvv

Verify Contracts

forge verify-contract <CONTRACT_ADDRESS> \
    src/MultiProtocolCDP.sol:MultiProtocolCDP \
    --chain-id 84532 \
    --etherscan-api-key $BASE_EXPLORER_API_KEY \
    --constructor-args $(cast abi-encode "constructor(address,address,address)" <REGISTRY> <STABLECOIN> <ORACLE>)

πŸ’‘ Usage Examples

Deposit Collateral

// Approve yield token
IERC20(stETH).approve(address(cdp), amount);

// Deposit stETH as collateral
cdp.depositCollateral(stETH, amount);

Borrow Stablecoins

// Check max borrow amount
uint256 maxBorrow = cdp.getMaxBorrow(msg.sender);

// Borrow zUSD
cdp.borrow(borrowAmount);

Repay Debt

// Approve stablecoin
IERC20(zUSD).approve(address(cdp), repayAmount);

// Repay debt
cdp.repay(repayAmount);

Withdraw Collateral

// Withdraw collateral (if health factor allows)
cdp.withdrawCollateral(stETH, withdrawAmount);

Check Position Health

// Get health factor (returns percentage, e.g., 150 = 150%)
uint256 healthFactor = cdp.getHealthFactor(msg.sender);

// Get total collateral value
uint256 collateralValue = cdp.getTotalCollateralValue(msg.sender);

// Get aggregated APY
uint256 weightedApy = cdp.getTotalApy(msg.sender);

πŸ“Š Key Parameters

Parameter Value Description
Collateral Ratio 150% Minimum collateralization required
Liquidation Threshold 125% Health factor triggers liquidation
Liquidation Penalty 10% Penalty for liquidated positions

πŸ” Security Features

  • βœ… ReentrancyGuard on all state-changing functions
  • βœ… SafeERC20 for token transfers
  • βœ… Ownable access control
  • βœ… Input validation and error handling
  • βœ… Health factor checks before borrowing/withdrawing

πŸ› οΈ Development

Local Development

# Start local Anvil node
anvil

# Deploy to local node
forge script script/Deploy.s.sol:DeployScript \
    --rpc-url http://127.0.0.1:8545 \
    --private-key $PRIVATE_KEY \
    --broadcast

Code Formatting

# Format code
forge fmt

# Check formatting
forge fmt --check

Generate Documentation

# Generate docs
forge doc

# Serve docs locally
forge doc --serve --port 3000

πŸ“ Contract Addresses (Base Sepolia)

After deployment, update with your contract addresses:

MultiProtocolCDP: 0x...
VaultRegistry: 0x...
Stablecoin (zUSD): 0x...

Adapters:
  AaveAdapter: 0x...
  LidoAdapter: 0x...
  CompoundAdapter: 0x...

Yield Tokens:
  stETH: 0x...
  aUSDC: 0x...
  cDAI: 0x...

πŸ”— Resources

🀝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

πŸ“„ License

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

⚠️ Disclaimer

This is experimental software and is provided on an "as is" and "as available" basis. We do not give any warranties and will not be liable for any losses incurred through any use of this code base.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published