@@ -30,8 +30,6 @@ contract Payer is Initializable, AccessControlUpgradeable, UUPSUpgradeable, Paus
30
30
uint256 public constant MAX_TOLERABLE_DEBT_AMOUNT_MICRO_DOLLARS = 100_000_000 ;
31
31
uint256 public constant DEFAULT_WITHDRAWAL_LOCK_PERIOD = 3 days ;
32
32
uint256 public constant ABSOLUTE_MINIMUM_WITHDRAWAL_LOCK_PERIOD = 1 days ;
33
- uint256 public constant DEFAULT_MAX_BACKDATED_TIME = 1 days ;
34
- uint256 public constant ABSOLUTE_MINIMUM_MAX_BACKDATED_TIME = 6 hours ;
35
33
string internal constant USDC_SYMBOL = "USDC " ;
36
34
37
35
/* ============ UUPS Storage ============ */
@@ -43,20 +41,16 @@ contract Payer is Initializable, AccessControlUpgradeable, UUPSUpgradeable, Paus
43
41
address distributionContract;
44
42
address nodesContract;
45
43
address payerReportContract;
46
-
47
44
/// @dev Configuration parameters
48
45
uint256 minimumRegistrationAmountMicroDollars;
49
46
uint256 minimumDepositAmountMicroDollars;
50
47
uint256 maxTolerableDebtAmountMicroDollars;
51
48
uint256 withdrawalLockPeriod;
52
- uint256 maxBackdatedTime;
53
-
54
49
/// @dev State variables
55
50
uint256 lastFeeTransferTimestamp;
56
51
uint256 pendingFees;
57
52
uint256 totalAmountDeposited;
58
53
uint256 totalDebtAmount;
59
-
60
54
/// @dev Mappings
61
55
uint256 collectedFees;
62
56
mapping (address => Payer) payers;
@@ -129,7 +123,6 @@ contract Payer is Initializable, AccessControlUpgradeable, UUPSUpgradeable, Paus
129
123
$.minimumRegistrationAmountMicroDollars = DEFAULT_MINIMUM_REGISTRATION_AMOUNT_MICRO_DOLLARS;
130
124
$.minimumDepositAmountMicroDollars = DEFAULT_MINIMUM_DEPOSIT_AMOUNT_MICRO_DOLLARS;
131
125
$.withdrawalLockPeriod = DEFAULT_WITHDRAWAL_LOCK_PERIOD;
132
- $.maxBackdatedTime = DEFAULT_MAX_BACKDATED_TIME;
133
126
134
127
_setUsdcTokenContract (_usdcToken);
135
128
_setNodesContract (_nodesContract);
@@ -177,14 +170,19 @@ contract Payer is Initializable, AccessControlUpgradeable, UUPSUpgradeable, Paus
177
170
PayerStorage storage $ = _getPayerStorage ();
178
171
179
172
require (amount >= $.minimumDepositAmountMicroDollars, InsufficientAmount ());
180
-
181
- if ($.withdrawals[msg .sender ].requestTimestamp != 0 ) revert PayerInWithdrawal ();
173
+ require ($.withdrawals[msg .sender ].requestTimestamp == 0 , PayerInWithdrawal ());
182
174
183
175
_deposit (msg .sender , amount);
184
176
185
- _updatePayerBalance (msg .sender , amount);
177
+ // TODO: Extract this logic to a helper function.
178
+ if ($.payers[msg .sender ].debtAmount > 0 ) {
179
+ _settleDebts (msg .sender , amount);
180
+ } else {
181
+ $.payers[msg .sender ].balance += amount;
182
+ _increaseTotalAmountDeposited (amount);
183
+ }
186
184
187
- $.payers[msg .sender ].latestDepositTimestamp = block . timestamp ;
185
+ emit PayerBalanceUpdated ( msg . sender , $.payers[msg .sender ].balance, $.payers[ msg . sender ].debtAmount) ;
188
186
}
189
187
190
188
/**
@@ -196,14 +194,21 @@ contract Payer is Initializable, AccessControlUpgradeable, UUPSUpgradeable, Paus
196
194
197
195
PayerStorage storage $ = _getPayerStorage ();
198
196
199
- if ($.withdrawals[payer].requestTimestamp != 0 ) revert PayerInWithdrawal ();
197
+ require ($.withdrawals[payer].requestTimestamp == 0 , PayerInWithdrawal () );
200
198
201
199
_deposit (msg .sender , amount);
202
200
203
- _updatePayerBalance (payer, amount);
201
+ // TODO: Extract this logic to a helper function.
202
+ if ($.payers[payer].debtAmount > 0 ) {
203
+ _settleDebts (payer, amount);
204
+ } else {
205
+ $.payers[payer].balance += amount;
206
+ _increaseTotalAmountDeposited (amount);
207
+ }
204
208
205
209
$.payers[payer].latestDonationTimestamp = block .timestamp ;
206
210
211
+ emit PayerBalanceUpdated (payer, $.payers[payer].balance, $.payers[payer].debtAmount);
207
212
emit Donation (msg .sender , payer, amount);
208
213
}
209
214
@@ -224,12 +229,12 @@ contract Payer is Initializable, AccessControlUpgradeable, UUPSUpgradeable, Paus
224
229
225
230
PayerStorage storage $ = _getPayerStorage ();
226
231
232
+ require ($.withdrawals[payer].requestTimestamp == 0 , PayerInWithdrawal ());
233
+
227
234
if ($.payers[payer].balance > 0 || $.payers[payer].debtAmount > 0 ) {
228
235
revert PayerHasBalanceOrDebt ();
229
236
}
230
237
231
- if ($.withdrawals[payer].requestTimestamp != 0 ) revert PayerInWithdrawal ();
232
-
233
238
// Delete all payer data
234
239
delete $.payers[payer];
235
240
require ($.totalPayers.remove (payer), FailedToDeletePayer ());
@@ -253,7 +258,7 @@ contract Payer is Initializable, AccessControlUpgradeable, UUPSUpgradeable, Paus
253
258
254
259
// Balance to be withdrawn is deducted from the payer's balance,
255
260
// it can't be used to settle payments.
256
- _decreasePayerBalance ( msg .sender , amount) ;
261
+ $.payers[ msg .sender ].balance -= amount;
257
262
_decreaseTotalAmountDeposited (amount);
258
263
259
264
uint256 withdrawableTimestamp = block .timestamp + $.withdrawalLockPeriod;
@@ -279,7 +284,8 @@ contract Payer is Initializable, AccessControlUpgradeable, UUPSUpgradeable, Paus
279
284
280
285
delete $.withdrawals[msg .sender ];
281
286
282
- _updatePayerBalance (msg .sender , withdrawal.amount);
287
+ $.payers[msg .sender ].balance += withdrawal.amount;
288
+ _increaseTotalAmountDeposited (withdrawal.amount);
283
289
284
290
emit WithdrawalCancelled (msg .sender , withdrawal.requestTimestamp);
285
291
}
@@ -294,11 +300,13 @@ contract Payer is Initializable, AccessControlUpgradeable, UUPSUpgradeable, Paus
294
300
295
301
Withdrawal memory withdrawal = $.withdrawals[msg .sender ];
296
302
297
- // TODO: A payer withdrawing funds can still incur debt.
298
- // We need to handle this case.
299
- // if ($.payers[msg.sender].debtAmount > 0) {}
303
+ uint256 finalWithdrawalAmount = withdrawal.amount;
300
304
301
- $.usdcToken.safeTransfer (msg .sender , withdrawal.amount);
305
+ if ($.payers[msg .sender ].debtAmount > 0 ) {
306
+ finalWithdrawalAmount = _settleDebts (msg .sender , withdrawal.amount);
307
+ }
308
+
309
+ $.usdcToken.safeTransfer (msg .sender , finalWithdrawalAmount);
302
310
303
311
delete $.withdrawals[msg .sender ];
304
312
@@ -319,37 +327,38 @@ contract Payer is Initializable, AccessControlUpgradeable, UUPSUpgradeable, Paus
319
327
/**
320
328
* @inheritdoc IPayer
321
329
*/
322
- function settleUsage (
323
- address [] calldata payerList ,
324
- uint256 [] calldata amountsList
325
- ) external whenNotPaused onlyPayerReport {
330
+ function settleUsage (uint256 originatorNode , address [] calldata payerList , uint256 [] calldata amountsList )
331
+ external
332
+ whenNotPaused
333
+ onlyPayerReport
334
+ {
326
335
require (payerList.length == amountsList.length , InvalidPayerListLength ());
327
336
328
337
PayerStorage storage $ = _getPayerStorage ();
329
338
339
+ uint256 fees = 0 ;
340
+
330
341
for (uint256 i = 0 ; i < payerList.length ; i++ ) {
331
342
address payer = payerList[i];
332
343
uint256 amount = amountsList[i];
333
344
334
345
// This should never happen, as PayerReport has already verified the payers and amounts.
335
346
// Payers in payerList should always exist and be active.
336
- if (! _payerExists (payer)) {
337
- _decreaseTotalAmountDeposited (amount);
338
-
347
+ if (! _payerExists (payer) || ! _payerIsActive (payer)) {
339
348
continue ;
340
349
}
341
350
342
- if (! _payerIsActive (payer)) continue ;
343
-
344
351
Payer memory storedPayer = $.payers[payer];
345
352
346
353
if (storedPayer.balance < amount) {
347
354
uint256 debt = amount - storedPayer.balance;
348
355
349
356
$.collectedFees += storedPayer.balance;
357
+ fees += storedPayer.balance;
350
358
351
359
storedPayer.balance = 0 ;
352
360
storedPayer.debtAmount = debt;
361
+ $.payers[payer] = storedPayer;
353
362
354
363
_addDebtor (payer);
355
364
_increaseTotalDebtAmount (debt);
@@ -362,17 +371,24 @@ contract Payer is Initializable, AccessControlUpgradeable, UUPSUpgradeable, Paus
362
371
}
363
372
364
373
$.collectedFees += amount;
374
+ fees += amount;
365
375
366
376
storedPayer.balance -= amount;
367
377
378
+ $.payers[payer] = storedPayer;
379
+
368
380
emit PayerBalanceUpdated (payer, storedPayer.balance, storedPayer.debtAmount);
369
381
}
382
+
383
+ emit UsageSettled (originatorNode, block .timestamp , fees);
370
384
}
371
385
372
386
/**
373
387
* @inheritdoc IPayer
374
388
*/
375
- function transferFeesToDistribution () external whenNotPaused onlyRole (ADMIN_ROLE) {
389
+ function transferFeesToDistribution () external whenNotPaused onlyNodeOperator {
390
+ // TODO: Should this be triggered by node operator?
391
+ // Or by settleUsage?
376
392
// TODO: Implement fee transfer logic
377
393
// Update lastFeeTransferTimestamp
378
394
// Update pendingFees
@@ -455,20 +471,6 @@ contract Payer is Initializable, AccessControlUpgradeable, UUPSUpgradeable, Paus
455
471
emit WithdrawalLockPeriodSet (oldWithdrawalLockPeriod, _newWithdrawalLockPeriod);
456
472
}
457
473
458
- /**
459
- * @inheritdoc IPayer
460
- */
461
- function setMaxBackdatedTime (uint256 _newMaxBackdatedTime ) external onlyRole (ADMIN_ROLE) {
462
- require (_newMaxBackdatedTime >= ABSOLUTE_MINIMUM_MAX_BACKDATED_TIME, InvalidMaxBackdatedTime ());
463
-
464
- PayerStorage storage $ = _getPayerStorage ();
465
-
466
- uint256 oldMaxBackdatedTime = $.maxBackdatedTime;
467
- $.maxBackdatedTime = _newMaxBackdatedTime;
468
-
469
- emit MaxBackdatedTimeSet (oldMaxBackdatedTime, _newMaxBackdatedTime);
470
- }
471
-
472
474
/**
473
475
* @inheritdoc IPayer
474
476
*/
@@ -647,13 +649,6 @@ contract Payer is Initializable, AccessControlUpgradeable, UUPSUpgradeable, Paus
647
649
return _getPayerStorage ().pendingFees;
648
650
}
649
651
650
- /**
651
- * @inheritdoc IPayer
652
- */
653
- function getMaxBackdatedTime () external view returns (uint256 maxTime ) {
654
- return _getPayerStorage ().maxBackdatedTime;
655
- }
656
-
657
652
/* ============ Internal ============ */
658
653
659
654
function _deposit (address payer , uint256 amount ) internal {
@@ -662,49 +657,34 @@ contract Payer is Initializable, AccessControlUpgradeable, UUPSUpgradeable, Paus
662
657
$.usdcToken.safeTransferFrom (payer, address (this ), amount);
663
658
}
664
659
665
- /**
666
- * @notice Updates a payer's balance by the specified amount.
667
- * If the payer has debt, the debt is subtracted from the amount.
668
- * If the payer has no debt, the amount is added to the balance.
669
- * @param payerAddress The address of the payer.
670
- * @param amount The amount to update by.
671
- */
672
- function _updatePayerBalance (address payerAddress , uint256 amount ) internal {
673
- // TODO: Remove this logic, in favor of settleDebts() or similar.
674
- Payer storage payer = _getPayerStorage ().payers[payerAddress];
660
+ function _settleDebts (address payer , uint256 amount ) internal returns (uint256 amountAfterSettlement ) {
661
+ PayerStorage storage $ = _getPayerStorage ();
662
+
663
+ Payer memory storedPayer = $.payers[payer];
675
664
676
- if (payer.debtAmount == 0 ) {
677
- payer.balance += amount;
665
+ if (storedPayer.debtAmount < amount) {
666
+ uint256 debtToRemove = storedPayer.debtAmount;
667
+ amount -= debtToRemove;
668
+
669
+ storedPayer.debtAmount = 0 ;
670
+ storedPayer.balance += amount;
671
+
672
+ _removeDebtor (payer);
678
673
_increaseTotalAmountDeposited (amount);
674
+ _decreaseTotalDebtAmount (debtToRemove);
675
+
676
+ amountAfterSettlement = amount;
679
677
} else {
680
- if (payer.debtAmount < amount) {
681
- uint256 debtToRemove = payer.debtAmount;
682
- amount -= debtToRemove;
683
- payer.debtAmount = 0 ;
684
- payer.balance += amount;
685
- _removeDebtor (payerAddress);
686
- _increaseTotalAmountDeposited (amount);
687
- _decreaseTotalDebtAmount (debtToRemove);
688
- } else {
689
- payer.debtAmount -= amount;
690
- _decreaseTotalDebtAmount (amount);
691
- }
692
- }
678
+ storedPayer.debtAmount -= amount;
693
679
694
- emit PayerBalanceUpdated (payerAddress, payer.balance, payer.debtAmount);
695
- }
680
+ _decreaseTotalDebtAmount (amount);
696
681
697
- /**
698
- * @notice Decreases a payer's balance by the specified amount.
699
- * @param payerAddress The address of the payer.
700
- * @param amount The amount to decrease by.
701
- */
702
- function _decreasePayerBalance (address payerAddress , uint256 amount ) internal {
703
- Payer storage payer = _getPayerStorage ().payers[payerAddress];
682
+ amountAfterSettlement = 0 ;
683
+ }
704
684
705
- payer.balance -= amount ;
685
+ $.payers[payer] = storedPayer ;
706
686
707
- emit PayerBalanceUpdated (payerAddress, payer.balance, payer.debtAmount) ;
687
+ return amountAfterSettlement ;
708
688
}
709
689
710
690
/**
0 commit comments