diff --git a/cadence/contracts/FlowALPModels.cdc b/cadence/contracts/FlowALPModels.cdc index f6bd5389..25fe2f5f 100644 --- a/cadence/contracts/FlowALPModels.cdc +++ b/cadence/contracts/FlowALPModels.cdc @@ -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. @@ -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() diff --git a/cadence/contracts/FlowALPv0.cdc b/cadence/contracts/FlowALPv0.cdc index 66cf3129..ed21dbb7 100644 --- a/cadence/contracts/FlowALPv0.cdc +++ b/cadence/contracts/FlowALPv0.cdc @@ -1346,6 +1346,7 @@ access(all) contract FlowALPv0 { ) let fromReserve <- reserveVault.withdraw(amount: reserveWithdrawAmount) + tokenState.refillDepositCapacity(reserveWithdrawAmount, pid: pid) withdrawn.deposit(from: <-fromReserve) } @@ -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(