Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 47 additions & 9 deletions cadence/contracts/TidalProtocol.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -306,18 +306,57 @@ access(all) contract TidalProtocol {

// Enhanced updateInterestIndices with deposit capacity update
access(all) fun updateInterestIndices() {
let currentTime = getCurrentBlock().timestamp
let timeDelta: UFix64 = currentTime - self.lastUpdate
self.creditInterestIndex = TidalProtocol.compoundInterestIndex(oldIndex: self.creditInterestIndex, perSecondRate: self.currentCreditRate, elapsedSeconds: timeDelta)
self.debitInterestIndex = TidalProtocol.compoundInterestIndex(oldIndex: self.debitInterestIndex, perSecondRate: self.currentDebitRate, elapsedSeconds: timeDelta)
let currentTime: UFix64 = getCurrentBlock().timestamp
let dt: UFix64 = currentTime - self.lastUpdate

// No time elapsed or already at cap → nothing to do
if dt <= 0.0 {
return
}

// Update interest indices (dt > 0 ensures sensible compounding)
self.creditInterestIndex = TidalProtocol.compoundInterestIndex(
oldIndex: self.creditInterestIndex,
perSecondRate: self.currentCreditRate,
elapsedSeconds: dt
)
self.debitInterestIndex = TidalProtocol.compoundInterestIndex(
oldIndex: self.debitInterestIndex,
perSecondRate: self.currentDebitRate,
elapsedSeconds: dt
)

// Record the moment we accounted for
self.lastUpdate = currentTime

// Update deposit capacity based on time
let newDepositCapacity = self.depositCapacity + (self.depositRate * timeDelta)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this calculation causes overflow if time delta is big

if newDepositCapacity >= self.depositCapacityCap {
// ---- Deposit capacity with overflow-proof, saturating growth ----
// Early exits keep gas down and avoid unnecessary math
if self.depositCapacity >= self.depositCapacityCap {
self.depositCapacity = self.depositCapacityCap
return
}
if self.depositRate <= 0.0 {
return
}

// Remaining headroom before hitting the cap
let remaining: UFix64 = self.depositCapacityCap - self.depositCapacity

// Bound the multiplier BEFORE multiplying to prevent overflow:
// choose the largest rate that still ensures rate*dt <= remaining
let maxRateForStep: UFix64 = remaining / dt
let effectiveRate: UFix64 = self.depositRate < maxRateForStep
? self.depositRate
: maxRateForStep

// Safe: growth <= remaining, so the addition cannot overflow
let growth: UFix64 = effectiveRate * dt

// Defensive clamp (handles any rounding edge cases)
if self.depositCapacity < (self.depositCapacityCap - growth) {
self.depositCapacity = self.depositCapacity + growth
} else {
self.depositCapacity = newDepositCapacity
self.depositCapacity = self.depositCapacityCap
}
}

Expand Down Expand Up @@ -928,7 +967,6 @@ access(all) contract TidalProtocol {
let uintDepositCollateralFactor = DeFiActionsMathUtils.toUInt128(self.collateralFactor[depositType]!)
var requiredEffectiveCollateral = DeFiActionsMathUtils.mul(uintHealthChange, effectiveDebtAfterWithdrawal)
requiredEffectiveCollateral = DeFiActionsMathUtils.div(requiredEffectiveCollateral, uintDepositCollateralFactor)
requiredEffectiveCollateral = DeFiActionsMathUtils.div(requiredEffectiveCollateral, uintWithdrawBorrowFactor)

// The amount of the token to deposit, in units of the token.
let collateralTokenCount = DeFiActionsMathUtils.div(requiredEffectiveCollateral, uintDepositPrice)
Expand Down
Loading