A comprehensive DeFi automation platform that enables automated collateral management and conditional order execution across multiple protocols including Aave V3, Euler, and CoW Protocol.
The Auto Collateral Swapper is a sophisticated smart contract system designed to automate collateral management strategies in decentralized finance. It provides users with the ability to:
- Automated Collateral Switching: Seamlessly swap between different collateral assets based on predefined conditions
- Stop-Loss Protection: Automatically trigger protective trades when collateral values drop below specified thresholds
- Multi-Protocol Support: Integrate with major DeFi protocols including Aave V3, Euler, and CoW Protocol
- Price Oracle Integration: Utilize Pyth Network and Chainlink oracles for reliable price feeds
- Conditional Order Execution: Execute complex trading strategies using the ComposableCoW framework
CollateralSwitch.sol: Handles Aave V3 collateral operations including supply, withdrawal, and flash loan integrationCollateralSwitchEuler.sol: Manages Euler protocol deposits and withdrawals through EVault interfaceComposableCoW.sol: Implements conditional order framework with Merkle tree authorization and swap guards
StopLoss.sol: Implements stop-loss orders with Pyth price oracle integrationInterestRateChange.sol: Handles interest rate-based conditional ordersBaseConditionalOrder.sol: Provides base functionality for all conditional order types
PythPriceFeed.sol: Integrates with Pyth Network for real-time price dataMyFirstPythContract.sol: Example implementation of Pyth price oracle usage- Price Pusher Service: Dedicated service for pushing price updates to on-chain oracles
- Brevis Integration: Provides ZK-proof capabilities for APY change verification
- Circuit Implementation: Go-based circuit for generating proofs of interest rate changes
The system automatically manages collateral positions across different protocols:
// Example: Aave V3 collateral supply
function aaveV3Supply(address asset, uint256 amount, address onBehalfOf) public {
IERC20(asset).safeTransferFrom(onBehalfOf, address(this), amount);
IERC20(asset).approve(address(aaveV3Pool), amount);
aaveV3Pool.supply(asset, amount, onBehalfOf, 0);
}Users can create complex conditional orders with various triggers:
- Price-based conditions (stop-loss, take-profit)
- Time-based conditions
- Interest rate changes
- Custom oracle conditions
- Aave V3: Lending protocol integration with flash loan capabilities
- Euler: Advanced lending protocol with EVault architecture
- CoW Protocol: Intent-based trading with MEV protection
- Solidity:
>=0.8.0 <0.9.0 - Foundry: Development framework and testing
- OpenZeppelin: Security-audited contract libraries
- Safe: Multi-signature wallet integration
- CoW Protocol:
@cowprotocol/contracts ^1.6.0 - Pyth Network:
@pythnetwork/pyth-sdk-solidity ^4.0.0 - Aave V3:
@aave/core-v3 ^1.19.3 - Ethers.js:
^5.7.2 - TypeScript:
^5.6.3
- Forge: Smart contract compilation and testing
- TypeScript: Scripting and automation
- Go: ZK circuit implementation
- Docker: Containerized deployment
- Node.js (v16 or higher)
- Foundry
- Go (for ZK circuits)
- Clone the repository:
git clone https://github.com/your-repo/auto-collateral-swapper.git
cd auto-collateral-swapper- Install dependencies:
npm install- Install Foundry dependencies:
forge install- Build contracts:
forge build# Run all tests
forge test
# Run specific test file
forge test --match-path test/ComposableCoW.stoploss.t.sol
# Run with verbose output
forge test -vvvDeploy contracts using Foundry scripts:
# Deploy to Anvil (local)
forge script script/deploy_AnvilStack.s.sol:AnvilStackScript --fork-url http://localhost:8545 --private-key <your_private_key>
# Deploy to production
forge script script/deploy_ProdStack.s.sol:ProdStackScript --rpc-url <your_rpc_url> --private-key <your_private_key>Submit conditional orders using the provided scripts:
# Submit Aave V3 order
npm run submit-aave
# Submit Euler order
npm run submit-eulerRun the price pusher service to maintain up-to-date oracle prices:
cd price_pusher
npm install
npm run start evm --endpoint <rpc_endpoint> --pyth-contract-address <address> --price-service-endpoint <hermes_endpoint> --price-config-file <config_file> --mnemonic-file <mnemonic_file>Configure price feeds in price_pusher/price-config.yaml:
- alias: DAI/USD
id: 0xb0948a5e5313200c632b51bb5ca32f6de0d36e9950a942d19751e833f70dabfd
time_difference: 60
price_deviation: 0.5
confidence_ratio: 1Configure network settings in networks.json:
{
"mainnet": {
"rpc": "https://mainnet.infura.io/v3/YOUR_KEY",
"chainId": 1
},
"sepolia": {
"rpc": "https://sepolia.infura.io/v3/YOUR_KEY",
"chainId": 11155111
}
}- Smart contracts follow OpenZeppelin security standards
- Implements Safe multi-signature wallet integration
- Uses proven oracle solutions (Pyth Network, Chainlink)
- Includes comprehensive test suite
- Stop-loss mechanisms to prevent excessive losses
- Slippage protection on all trades
- Time-based order expiration
- Multi-oracle price verification
The system includes Brevis integration for ZK-proof generation:
- Circuit Implementation: Go-based circuit for APY change verification
- Proof Generation: Off-chain proof generation for on-chain verification
- Privacy: Maintain privacy while proving specific conditions
- Fork the repository
- Create a feature branch
- Implement your changes
- Add comprehensive tests
- Submit a pull request
MIT License - see LICENSE file for details.
For support and questions:
- GitHub Issues: Create an issue in this repository
- Documentation: Refer to the docs/ directory
- Community: Join our Discord server
- CoW Protocol team for the conditional order framework
- Pyth Network for reliable price oracles
- Aave and Euler teams for lending protocol integration
- Safe team for multi-signature wallet support