Skip to content
Draft
Show file tree
Hide file tree
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
21 changes: 21 additions & 0 deletions cadence/contracts/FlowALPModels.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -1176,6 +1176,10 @@ access(all) contract FlowALPModels {
/// (used when deposits are made)
access(EImplementation) fun consumeDepositCapacity(_ amount: UFix64, pid: UInt64)

/// Increases deposit capacity by the specified amount and tracks per-user deposit usage
/// (used when withdrawals are made)
access(EImplementation) fun refillDepositCapacity(_ amount: UFix64, pid: UInt64)

/// Returns the per-deposit limit based on user deposit limit and available deposit capacity.
/// Rationale: cap per-deposit size to a fraction of the total depositCapacityCap
/// so a single large deposit cannot monopolize capacity.
Expand Down Expand Up @@ -1515,6 +1519,23 @@ access(all) contract FlowALPModels {
)
}

/// Increases deposit capacity by the specified amount when a withdrawal is made.
/// This is because we want to limit net deposits within a certain time period rather than gross deposits.
access(EImplementation) fun refillDepositCapacity(_ amount: UFix64, pid: UInt64) {
self.depositCapacity = self.depositCapacity + amount
// Track per-user deposit usage
let currentUserUsage = self.depositUsage[pid] ?? 0.0
self.depositUsage[pid] = currentUserUsage.saturatingSubtract(amount)

FlowALPEvents.emitDepositCapacityConsumed(
tokenType: self.tokenType,
pid: pid,
amount: 0.0,
remainingCapacity: self.depositCapacity
)
}


/// Returns the maximum amount that can be deposited to the given position without being queued.
access(EImplementation) view fun depositLimit(pid: UInt64): UFix64 {
let userCap = self.getUserDepositLimitCap()
Expand Down
3 changes: 3 additions & 0 deletions cadence/contracts/FlowALPv0.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -1346,6 +1346,7 @@ access(all) contract FlowALPv0 {
)

let fromReserve <- reserveVault.withdraw(amount: reserveWithdrawAmount)
tokenState.refillDepositCapacity(reserveWithdrawAmount, pid: pid)
withdrawn.deposit(from: <-fromReserve)
}

Expand Down Expand Up @@ -1823,6 +1824,8 @@ access(all) contract FlowALPv0 {
amount: uintSinkAmount,
tokenState: tokenState
)
tokenState.refillDepositCapacity(UFix64(uintSinkAmount), pid: pid)

let sinkVault <- FlowALPv0._borrowMOETMinter().mintTokens(amount: sinkAmount)

FlowALPEvents.emitRebalanced(
Expand Down