Skip to content

Commit 9562629

Browse files
committed
move per-user deposit limit to depositLimit()
1 parent fa49fa7 commit 9562629

2 files changed

Lines changed: 13 additions & 19 deletions

File tree

cadence/contracts/FlowALPModels.cdc

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,13 +1037,13 @@ access(all) contract FlowALPModels {
10371037
/// (used when deposits are made)
10381038
access(EImplementation) fun consumeDepositCapacity(_ amount: UFix64, pid: UInt64)
10391039

1040-
/// Returns the per-deposit limit based on user deposit limit cap and available deposit capacity.
1040+
/// Returns the per-deposit limit based on user deposit limit and available deposit capacity.
10411041
/// Rationale: cap per-deposit size to a fraction of the total depositCapacityCap
10421042
/// so a single large deposit cannot monopolize capacity.
10431043
/// Excess is queued and drained in chunks (see asyncUpdatePosition),
10441044
/// enabling fair throughput across many deposits in a block. The 5%
10451045
/// fraction is conservative and can be tuned by protocol parameters.
1046-
access(EImplementation) view fun depositLimit(): UFix64
1046+
access(EImplementation) view fun depositLimit(pid: UInt64): UFix64
10471047

10481048
/// Updates interest indices and regenerates deposit capacity for elapsed time
10491049
access(EImplementation) fun updateForTimeChange()
@@ -1376,13 +1376,15 @@ access(all) contract FlowALPModels {
13761376
)
13771377
}
13781378

1379-
/// Returns the per-deposit limit based on user deposit limit cap and available deposit capacity.
1380-
access(EImplementation) view fun depositLimit(): UFix64 {
1381-
let cap = self.getUserDepositLimitCap()
1382-
if self.depositCapacity < cap {
1383-
return self.depositCapacity
1379+
/// Returns the maximum amount that can be deposited to the given position without being queued.
1380+
access(EImplementation) view fun depositLimit(pid: UInt64): UFix64 {
1381+
let userCap = self.getUserDepositLimitCap()
1382+
let userUsed = self.getDepositUsageForPosition(pid)
1383+
var available = userCap - userUsed
1384+
if self.depositCapacity < available {
1385+
available = self.depositCapacity
13841386
}
1385-
return cap
1387+
return available
13861388
}
13871389

13881390
/// Updates interest indices and regenerates deposit capacity for elapsed time.

cadence/contracts/FlowALPv0.cdc

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,18 +1113,10 @@ access(all) contract FlowALPv0 {
11131113

11141114
// Time-based state is handled by the tokenState() helper function
11151115

1116-
// Deposit rate limiting: prevent a single large deposit from monopolizing capacity.
1116+
// Deposit rate limiting: prevent a single user or single large deposit from monopolizing capacity.
11171117
// Excess is queued to be processed asynchronously (see asyncUpdatePosition).
11181118
let depositAmount = from.balance
1119-
var depositLimit = tokenState.depositLimit()
1120-
1121-
// Per-user deposit limit: check if user has exceeded their per-user limit
1122-
let userDepositLimitCap = tokenState.getUserDepositLimitCap()
1123-
let currentUsage = tokenState.getDepositUsageForPosition(pid)
1124-
let remainingUserLimit = userDepositLimitCap - currentUsage
1125-
if remainingUserLimit < depositLimit {
1126-
depositLimit = remainingUserLimit
1127-
}
1119+
let depositLimit = tokenState.depositLimit(pid: pid)
11281120

11291121
// depositAmount is bounded by the smaller of:
11301122
// User deposit limit, per-deposit limit, and global deposit capacity
@@ -1858,7 +1850,7 @@ access(all) contract FlowALPv0 {
18581850
let queuedVault <- position.removeQueuedDeposit(depositType)!
18591851
let queuedAmount = queuedVault.balance
18601852
let depositTokenState = self._borrowUpdatedTokenState(type: depositType)
1861-
let maxDeposit = depositTokenState.depositLimit()
1853+
let maxDeposit = depositTokenState.depositLimit(pid: pid)
18621854

18631855
if maxDeposit >= queuedAmount {
18641856
// We can deposit all of the queued deposit, so just do it and remove it from the queue

0 commit comments

Comments
 (0)