Skip to main content

Smart Contract Architecture

Loopify Finance implements a modular smart contract architecture supporting three core strategies: flash loan looping, yield optimization, and pre-deposit systems.

Core Contracts

LoopifyStrategy Contract

Main strategy execution contract supporting all three strategies.

// Core strategy parameters
uint256 public constant MAX_LEVERAGE = 500; // 5x
uint256 public constant MAX_LTV = 8000; // 80%
uint256 public constant LIQUIDATION_THRESHOLD = 8000;

Health Factor Calculation

function getHealthFactor(address user) public view returns (uint256) {
if (pos.debtValue == 0) return type(uint256).max;
return (pos.collateralValue * LIQUIDATION_THRESHOLD * 1e18) / (pos.debtValue * 10000);
}

Health Factor Zones:

  • Safe: > 2.0
  • Warning: 1.3 - 2.0
  • Critical: 1.05 - 1.3
  • Emergency: < 1.05 (auto-deleverage)

Flash Loan Integration

Primary flash loan providers with zero fees:

  • Balancer: Primary source
  • Morpho Blue: Backup option
// Flash loan callback
function receiveFlashLoan(
IERC20[] memory tokens,
uint256[] memory amounts,
bytes memory userData
) external nonReentrant {
// Execute strategy logic
_executeStrategy(tokens, amounts, userData);

// Repay loan (zero fees from Balancer/Morpho)
tokens[0].transfer(msg.sender, amounts[0]);
}

Yield Optimization Vault

ERC4626-compliant vault for automated yield strategies.

Withdrawal System

// Smart withdrawal timing
function requestWithdrawal(uint256 shares) external {
uint256 assetsRequested = previewRedeem(shares);
uint256 vaultBalance = IERC20(asset()).balanceOf(address(this));

// Instant if funds available, timelock if deployed
uint256 delay = assetsRequested <= vaultBalance ? 0 : 12 hours;

withdrawalRequests[msg.sender] = WithdrawalRequest({
shares: shares,
unlockTime: block.timestamp + delay,
active: true
});
}

Emergency Controls

Circuit Breakers

// Emergency pause system
function emergencyPause() external onlyRole(PAUSER_ROLE) {
_pause();
emit EmergencyPaused(msg.sender, block.timestamp);
}

Position Recovery

// Automated liquidation protection
function autoProtectPosition(address user) external {
uint256 healthFactor = getHealthFactor(user);
require(healthFactor < 1.5, "Position healthy");

// Execute deleveraging to restore health
_executeDeleveraging(user, healthFactor);
}

Security Features

Access Control

  • Multi-signature requirements for critical operations
  • Role-based permissions (Strategist, Liquidator, Keeper)
  • 48-hour timelock for parameter changes

Error Handling

  • Comprehensive custom errors for gas efficiency
  • Automatic fallback mechanisms
  • Position recovery systems

Oracle Integration

  • Multiple price feed sources
  • Price deviation monitoring
  • Failsafe mechanisms

Contract Addresses:

  • Testnet: Coming Soon
  • Mainnet: In Development

Security Audits: In progress with Trail of Bits and Consensys Diligence