@@ -50,29 +50,25 @@ contract Payer is
50
50
51
51
/// @custom:storage-location erc7201:xmtp.storage.Payer
52
52
struct PayerStorage {
53
- // 1. Configuration and state parameters packed in 2 slots.
53
+ // Configuration and state parameters (fits in 2 slots)
54
54
uint64 minimumRegistrationAmountMicroDollars; // 8 bytes
55
55
uint64 minimumDepositAmountMicroDollars; // 8 bytes
56
56
uint64 maxTolerableDebtAmountMicroDollars; // 8 bytes
57
57
uint64 lastFeeTransferTimestamp; // 8 bytes
58
-
59
58
uint64 totalAmountDeposited; // 8 bytes
60
59
uint64 totalDebtAmount; // 8 bytes
61
60
uint64 pendingFees; // 8 bytes
62
61
uint64 collectedFees; // 8 bytes
63
-
64
62
uint32 withdrawalLockPeriod; // 4 bytes
65
63
uint32 transferFeesPeriod; // 4 bytes
66
- uint32 reserved1; // 4 bytes (reserved for future use)
67
- uint32 reserved2; // 4 bytes (reserved for future use)
68
64
69
- // 2. Contract addresses (each 20 bytes, fits in 3 slots)
65
+ // Contract addresses (fits in 3 slots)
70
66
IERC20 usdcToken; // 20 bytes
71
67
address distributionContract; // 20 bytes
72
68
address nodesContract; // 20 bytes
73
69
address payerReportContract; // 20 bytes
74
70
75
- // 3. Mappings and dynamic sets (each starts at its own storage slot)
71
+ // Mappings and dynamic sets (each starts at its own storage slot)
76
72
mapping (address => Payer) payers;
77
73
mapping (address => Withdrawal) withdrawals;
78
74
EnumerableSet.AddressSet totalPayers;
@@ -237,7 +233,7 @@ contract Payer is
237
233
require ($.totalPayers.remove (payer), FailedToDeletePayer ());
238
234
require ($.activePayers.remove (payer), FailedToDeletePayer ());
239
235
240
- emit PayerDeleted (payer, block .timestamp );
236
+ emit PayerDeleted (payer, uint64 ( block .timestamp ) );
241
237
}
242
238
243
239
/* ========== Payers Balance Management ========= */
@@ -260,15 +256,17 @@ contract Payer is
260
256
$.payers[msg .sender ].balance -= amount;
261
257
_decreaseTotalAmountDeposited (amount);
262
258
263
- uint256 withdrawableTimestamp = block .timestamp + $.withdrawalLockPeriod;
259
+ emit PayerBalanceUpdated (msg .sender , $.payers[msg .sender ].balance, $.payers[msg .sender ].debtAmount);
260
+
261
+ uint64 withdrawableTimestamp = uint64 (block .timestamp ) + $.withdrawalLockPeriod;
264
262
265
263
$.withdrawals[msg .sender ] = Withdrawal ({
266
264
requestTimestamp: uint64 (block .timestamp ),
267
- withdrawableTimestamp: uint64 ( withdrawableTimestamp) ,
265
+ withdrawableTimestamp: withdrawableTimestamp,
268
266
amount: amount
269
267
});
270
268
271
- emit WithdrawalRequested (msg .sender , block .timestamp , withdrawableTimestamp, amount);
269
+ emit WithdrawalRequested (msg .sender , uint64 ( block .timestamp ) , withdrawableTimestamp, amount);
272
270
}
273
271
274
272
/**
@@ -286,6 +284,8 @@ contract Payer is
286
284
$.payers[msg .sender ].balance += _withdrawal.amount;
287
285
_increaseTotalAmountDeposited (_withdrawal.amount);
288
286
287
+ emit PayerBalanceUpdated (msg .sender , $.payers[msg .sender ].balance, $.payers[msg .sender ].debtAmount);
288
+
289
289
emit WithdrawalCancelled (msg .sender , _withdrawal.requestTimestamp);
290
290
}
291
291
@@ -299,18 +299,22 @@ contract Payer is
299
299
300
300
Withdrawal memory _withdrawal = $.withdrawals[msg .sender ];
301
301
302
+ require (block .timestamp >= _withdrawal.withdrawableTimestamp, WithdrawalPeriodNotElapsed ());
303
+
302
304
delete $.withdrawals[msg .sender ];
303
305
304
- uint256 _finalWithdrawalAmount = _withdrawal.amount;
306
+ uint64 _finalWithdrawalAmount = _withdrawal.amount;
305
307
306
308
if ($.payers[msg .sender ].debtAmount > 0 ) {
307
- _finalWithdrawalAmount = _settleDebts (msg .sender , _withdrawal.amount);
309
+ _finalWithdrawalAmount = _settleDebts (msg .sender , _withdrawal.amount, true );
308
310
}
309
311
310
312
if (_finalWithdrawalAmount > 0 ) {
311
313
$.usdcToken.safeTransfer (msg .sender , _finalWithdrawalAmount);
312
314
}
313
315
316
+ emit PayerBalanceUpdated (msg .sender , $.payers[msg .sender ].balance, $.payers[msg .sender ].debtAmount);
317
+
314
318
emit WithdrawalFinalized (msg .sender , _withdrawal.requestTimestamp, _finalWithdrawalAmount);
315
319
}
316
320
@@ -383,7 +387,7 @@ contract Payer is
383
387
384
388
$.pendingFees = _pendingFees;
385
389
386
- emit UsageSettled (originatorNode, block .timestamp , _settledFees);
390
+ emit UsageSettled (originatorNode, uint64 ( block .timestamp ) , _settledFees);
387
391
}
388
392
389
393
/**
@@ -408,7 +412,7 @@ contract Payer is
408
412
$.collectedFees += _pendingFeesAmount;
409
413
$.pendingFees = 0 ;
410
414
411
- emit FeesTransferred (block .timestamp , _pendingFeesAmount);
415
+ emit FeesTransferred (uint64 ( block .timestamp ) , _pendingFeesAmount);
412
416
}
413
417
414
418
/* ========== Administrative Functions ========== */
@@ -449,7 +453,7 @@ contract Payer is
449
453
450
454
PayerStorage storage $ = _getPayerStorage ();
451
455
452
- uint256 oldMinimumDeposit = $.minimumDepositAmountMicroDollars;
456
+ uint64 oldMinimumDeposit = $.minimumDepositAmountMicroDollars;
453
457
$.minimumDepositAmountMicroDollars = newMinimumDeposit;
454
458
455
459
emit MinimumDepositSet (oldMinimumDeposit, newMinimumDeposit);
@@ -466,7 +470,7 @@ contract Payer is
466
470
467
471
PayerStorage storage $ = _getPayerStorage ();
468
472
469
- uint256 _oldMinimumRegistrationAmount = $.minimumRegistrationAmountMicroDollars;
473
+ uint64 _oldMinimumRegistrationAmount = $.minimumRegistrationAmountMicroDollars;
470
474
$.minimumRegistrationAmountMicroDollars = newMinimumRegistrationAmount;
471
475
472
476
emit MinimumRegistrationAmountSet (_oldMinimumRegistrationAmount, newMinimumRegistrationAmount);
@@ -480,7 +484,7 @@ contract Payer is
480
484
481
485
PayerStorage storage $ = _getPayerStorage ();
482
486
483
- uint256 _oldWithdrawalLockPeriod = $.withdrawalLockPeriod;
487
+ uint32 _oldWithdrawalLockPeriod = $.withdrawalLockPeriod;
484
488
$.withdrawalLockPeriod = newWithdrawalLockPeriod;
485
489
486
490
emit WithdrawalLockPeriodSet (_oldWithdrawalLockPeriod, newWithdrawalLockPeriod);
@@ -542,7 +546,7 @@ contract Payer is
542
546
/**
543
547
* @inheritdoc IPayer
544
548
*/
545
- function getActivePayers (uint256 offset , uint256 limit )
549
+ function getActivePayers (uint32 offset , uint32 limit )
546
550
external
547
551
view
548
552
returns (Payer[] memory payers , bool hasMore )
@@ -571,7 +575,7 @@ contract Payer is
571
575
/**
572
576
* @inheritdoc IPayer
573
577
*/
574
- function getPayerBalance (address payer ) external view returns (uint256 balance ) {
578
+ function getPayerBalance (address payer ) external view returns (uint64 balance ) {
575
579
_revertIfPayerDoesNotExist (payer);
576
580
577
581
return _getPayerStorage ().payers[payer].balance;
@@ -580,7 +584,7 @@ contract Payer is
580
584
/**
581
585
* @inheritdoc IPayer
582
586
*/
583
- function getPayersInDebt (uint256 offset , uint256 limit )
587
+ function getPayersInDebt (uint32 offset , uint32 limit )
584
588
external
585
589
view
586
590
returns (Payer[] memory payers , bool hasMore )
@@ -614,14 +618,14 @@ contract Payer is
614
618
/**
615
619
* @inheritdoc IPayer
616
620
*/
617
- function getLastFeeTransferTimestamp () external view returns (uint256 timestamp ) {
621
+ function getLastFeeTransferTimestamp () external view returns (uint64 timestamp ) {
618
622
return _getPayerStorage ().lastFeeTransferTimestamp;
619
623
}
620
624
621
625
/**
622
626
* @inheritdoc IPayer
623
627
*/
624
- function getTotalValueLocked () external view returns (uint256 totalValueLocked ) {
628
+ function getTotalValueLocked () external view returns (uint64 totalValueLocked ) {
625
629
PayerStorage storage $ = _getPayerStorage ();
626
630
627
631
if ($.totalDebtAmount > $.totalAmountDeposited) return 0 ;
@@ -632,7 +636,7 @@ contract Payer is
632
636
/**
633
637
* @inheritdoc IPayer
634
638
*/
635
- function getTotalDebtAmount () external view returns (uint256 totalDebt ) {
639
+ function getTotalDebtAmount () external view returns (uint64 totalDebt ) {
636
640
return _getPayerStorage ().totalDebtAmount;
637
641
}
638
642
@@ -667,28 +671,28 @@ contract Payer is
667
671
/**
668
672
* @inheritdoc IPayer
669
673
*/
670
- function getMinimumDeposit () external view returns (uint256 minimumDeposit ) {
674
+ function getMinimumDeposit () external view returns (uint64 minimumDeposit ) {
671
675
return _getPayerStorage ().minimumDepositAmountMicroDollars;
672
676
}
673
677
674
678
/**
675
679
* @inheritdoc IPayer
676
680
*/
677
- function getMinimumRegistrationAmount () external view returns (uint256 minimumRegistrationAmount ) {
681
+ function getMinimumRegistrationAmount () external view returns (uint64 minimumRegistrationAmount ) {
678
682
return _getPayerStorage ().minimumRegistrationAmountMicroDollars;
679
683
}
680
684
681
685
/**
682
686
* @inheritdoc IPayer
683
687
*/
684
- function getWithdrawalLockPeriod () external view returns (uint256 lockPeriod ) {
688
+ function getWithdrawalLockPeriod () external view returns (uint32 lockPeriod ) {
685
689
return _getPayerStorage ().withdrawalLockPeriod;
686
690
}
687
691
688
692
/**
689
693
* @inheritdoc IPayer
690
694
*/
691
- function getPendingFees () external view returns (uint256 fees ) {
695
+ function getPendingFees () external view returns (uint64 fees ) {
692
696
return _getPayerStorage ().pendingFees;
693
697
}
694
698
@@ -719,11 +723,11 @@ contract Payer is
719
723
* @param amount The amount to add to the payer's balance.
720
724
* @return leftoverAmount Amount remaining after debt settlement (if any).
721
725
*/
722
- function _updatePayerBalance (address payerAddress , uint64 amount ) internal returns (uint256 leftoverAmount ) {
726
+ function _updatePayerBalance (address payerAddress , uint64 amount ) internal returns (uint64 leftoverAmount ) {
723
727
Payer storage _payer = _getPayerStorage ().payers[payerAddress];
724
728
725
729
if (_payer.debtAmount > 0 ) {
726
- return _settleDebts (payerAddress, amount);
730
+ return _settleDebts (payerAddress, amount, false );
727
731
} else {
728
732
_payer.balance += amount;
729
733
_increaseTotalAmountDeposited (amount);
@@ -735,9 +739,10 @@ contract Payer is
735
739
* @notice Settles debts for a payer, updating their balance and total amounts.
736
740
* @param payer The address of the payer.
737
741
* @param amount The amount to settle debts for.
742
+ * @param isWithdrawal Whether the debt settlement happens during a withdrawal.
738
743
* @return amountAfterSettlement The amount remaining after debt settlement.
739
744
*/
740
- function _settleDebts (address payer , uint64 amount ) internal returns (uint256 amountAfterSettlement ) {
745
+ function _settleDebts (address payer , uint64 amount , bool isWithdrawal ) internal returns (uint64 amountAfterSettlement ) {
741
746
PayerStorage storage $ = _getPayerStorage ();
742
747
743
748
Payer memory _storedPayer = $.payers[payer];
@@ -746,8 +751,12 @@ contract Payer is
746
751
uint64 _debtToRemove = _storedPayer.debtAmount;
747
752
amount -= _debtToRemove;
748
753
749
- _storedPayer.debtAmount = 0 ;
750
- _storedPayer.balance += amount;
754
+ // For regular deposits, add remaining amount to balance.
755
+ // In withdrawals, that amount was moved to the withdrawal balance.
756
+ if (! isWithdrawal) {
757
+ _storedPayer.balance += amount;
758
+ _increaseTotalAmountDeposited (amount);
759
+ }
751
760
752
761
_removeDebtor (payer);
753
762
_increaseTotalAmountDeposited (amount);
0 commit comments