4.2 Core Module of Interstellar Bank: Auto-Compounding Logic

Star Bank's compound interest mechanism is fully automated through on-chain smart contracts, eliminating reliance on centralized servers.

Core function compound():

This function is called at the end of each block or before the user withdraws to calculate compound interest.

Solidity
// StarBank.sol: Simplified compound interest calculation function
 
function calculateRewards(address user, uint256 cycleDuration) public view returns (uint256) {
// P = collateral principal, r = daily yield (BPS), t = number of days pledged
    uint256 principal = deposits[user].principal;
    uint256 dailyRateBPS = stakingRates[cycleDuration];
    uint256 daysPassed = (block.timestamp - deposits[user].startTime) / 1 days;
 
    if (daysPassed == 0) return 0;
 
// Use efficient on-chain power operation approximation to avoid excessive Gas consumption
// In practice, Taylor series or precomputed lookup tables are typically used
    uint256 rewardFactor = Power(10000 + dailyRateBPS, daysPassed) / Power(10000, daysPassed);
    uint256 totalAccumulated = principal * rewardFactor / 10000;
    
    return totalAccumulated - principal;
}
 
// Actual compound interest execution
function claimRewards() external {
// 1. Turbine escort inspection
    require(checkContribution(msg.sender, pendingRewards[msg.sender]), "Turbine Escort Failed: Insufficient PoC");
    
// 2. Calculate and update compound interest
    uint256 rewards = calculateRewards(msg.sender, deposits[msg.sender].cycle);
//... Reward distribution logic
    
// 3. Reset timestamp
    deposits[msg.sender].startTime = block.timestamp;
}

Last updated