@@ -37,6 +37,7 @@ contract Payer is
37
37
38
38
bytes32 public constant ADMIN_ROLE = keccak256 ("ADMIN_ROLE " );
39
39
string internal constant USDC_SYMBOL = "USDC " ;
40
+ uint8 private constant PAYER_OPERATOR_ID = 1 ;
40
41
uint64 private constant DEFAULT_MINIMUM_REGISTRATION_AMOUNT_MICRO_DOLLARS = 10_000_000 ; // 10 USD
41
42
uint64 private constant DEFAULT_MINIMUM_DEPOSIT_AMOUNT_MICRO_DOLLARS = 10_000_000 ; // 10 USD
42
43
uint64 private constant DEFAULT_MAX_TOLERABLE_DEBT_AMOUNT_MICRO_DOLLARS = 50_000_000 ; // 50 USD
@@ -73,6 +74,7 @@ contract Payer is
73
74
EnumerableSet.AddressSet activePayers;
74
75
EnumerableSet.AddressSet debtPayers;
75
76
}
77
+ // TODO: pack struct
76
78
77
79
// keccak256(abi.encode(uint256(keccak256("xmtp.storage.Payer")) - 1)) & ~bytes32(uint256(0xff))
78
80
bytes32 internal constant PAYER_STORAGE_LOCATION =
@@ -90,20 +92,16 @@ contract Payer is
90
92
/**
91
93
* @dev Modifier to check if caller is an active node operator.
92
94
*/
93
- modifier onlyNodeOperator () {
94
- if (! _getIsActiveNodeOperator (msg .sender )) {
95
- revert UnauthorizedNodeOperator ();
96
- }
95
+ modifier onlyNodeOperator (uint256 nodeId ) {
96
+ require (_getIsActiveNodeOperator (nodeId), UnauthorizedNodeOperator ());
97
97
_;
98
98
}
99
99
100
100
/**
101
101
* @dev Modifier to check if caller is the payer report contract.
102
102
*/
103
103
modifier onlyPayerReport () {
104
- if (msg .sender != _getPayerStorage ().payerReportContract) {
105
- revert Unauthorized ();
106
- }
104
+ require (msg .sender == _getPayerStorage ().payerReportContract, Unauthorized ());
107
105
_;
108
106
}
109
107
@@ -158,7 +156,8 @@ contract Payer is
158
156
PayerStorage storage $ = _getPayerStorage ();
159
157
160
158
require (amount >= $.minimumRegistrationAmountMicroDollars, InsufficientAmount ());
161
- require (! _payerExists (msg .sender ), PayerAlreadyRegistered ());
159
+
160
+ if (_payerExists (msg .sender )) revert PayerAlreadyRegistered ();
162
161
163
162
_deposit (msg .sender , amount);
164
163
@@ -185,46 +184,30 @@ contract Payer is
185
184
* @inheritdoc IPayer
186
185
*/
187
186
function deposit (uint256 amount ) external whenNotPaused nonReentrant onlyPayer (msg .sender ) {
188
- PayerStorage storage $ = _getPayerStorage ();
189
-
190
- require (amount >= $.minimumDepositAmountMicroDollars, InsufficientAmount ());
191
- require ($.withdrawals[msg .sender ].requestTimestamp == 0 , PayerInWithdrawal ());
192
-
193
- _deposit (msg .sender , amount);
194
-
195
- _updatePayerBalance (msg .sender , amount);
196
-
197
- emit PayerBalanceUpdated (msg .sender , $.payers[msg .sender ].balance, $.payers[msg .sender ].debtAmount);
187
+ _validateAndProcessDeposit (msg .sender , msg .sender , amount);
198
188
}
199
189
200
190
/**
201
191
* @inheritdoc IPayer
202
192
*/
203
193
function donate (address payer , uint256 amount ) external whenNotPaused {
204
- require (amount > 0 , InsufficientAmount ());
205
194
_revertIfPayerDoesNotExist (payer);
206
195
196
+ _validateAndProcessDeposit (msg .sender , payer, amount);
207
197
PayerStorage storage $ = _getPayerStorage ();
208
198
209
- require ($.withdrawals[payer].requestTimestamp == 0 , PayerInWithdrawal ());
210
-
211
- _deposit (msg .sender , amount);
212
-
213
- _updatePayerBalance (payer, amount);
214
-
215
199
$.payers[payer].latestDonationTimestamp = block .timestamp ;
216
200
217
- emit PayerBalanceUpdated (payer, $.payers[payer].balance, $.payers[payer].debtAmount);
218
201
emit Donation (msg .sender , payer, amount);
219
202
}
220
203
221
204
/**
222
205
* @inheritdoc IPayer
223
206
*/
224
- function deactivatePayer (address payer ) external whenNotPaused onlyNodeOperator {
207
+ function deactivatePayer (uint256 nodeId , address payer ) external whenNotPaused onlyNodeOperator (nodeId) {
225
208
_revertIfPayerDoesNotExist (payer);
226
209
227
- _deactivatePayer (payer);
210
+ _deactivatePayer (nodeId, payer);
228
211
}
229
212
230
213
/**
@@ -237,7 +220,9 @@ contract Payer is
237
220
238
221
require ($.withdrawals[payer].requestTimestamp == 0 , PayerInWithdrawal ());
239
222
240
- if ($.payers[payer].balance > 0 || $.payers[payer].debtAmount > 0 ) {
223
+ Payer memory _storedPayer = $.payers[payer];
224
+
225
+ if (_storedPayer.balance > 0 || _storedPayer.debtAmount > 0 ) {
241
226
revert PayerHasBalanceOrDebt ();
242
227
}
243
228
@@ -259,8 +244,10 @@ contract Payer is
259
244
260
245
PayerStorage storage $ = _getPayerStorage ();
261
246
262
- require ($.payers[msg .sender ].debtAmount == 0 , PayerHasDebt ());
263
- require ($.payers[msg .sender ].balance >= amount, InsufficientBalance ());
247
+ Payer memory _storedPayer = $.payers[msg .sender ];
248
+
249
+ require (_storedPayer.debtAmount == 0 , PayerHasDebt ());
250
+ require (_storedPayer.balance >= amount, InsufficientBalance ());
264
251
265
252
// Balance to be withdrawn is deducted from the payer's balance,
266
253
// it can't be used to settle payments.
@@ -318,7 +305,7 @@ contract Payer is
318
305
$.usdcToken.safeTransfer (msg .sender , _finalWithdrawalAmount);
319
306
}
320
307
321
- emit WithdrawalFinalized (msg .sender , _withdrawal.requestTimestamp);
308
+ emit WithdrawalFinalized (msg .sender , _withdrawal.requestTimestamp, _finalWithdrawalAmount );
322
309
}
323
310
324
311
/**
@@ -345,25 +332,24 @@ contract Payer is
345
332
346
333
PayerStorage storage $ = _getPayerStorage ();
347
334
348
- uint256 _fees = 0 ;
335
+ uint256 _settledFees = 0 ;
336
+ uint256 _pendingFees = $.pendingFees;
349
337
350
338
for (uint256 i = 0 ; i < payerList.length ; i++ ) {
351
339
address payer = payerList[i];
352
340
uint256 usage = usageAmountsList[i];
353
341
354
342
// This should never happen, as PayerReport has already verified the payers and amounts.
355
343
// Payers in payerList should always exist and be active.
356
- if (! _payerExists (payer) || ! _payerIsActive (payer)) {
357
- continue ;
358
- }
344
+ if (! _payerExists (payer) || ! _payerIsActive (payer)) continue ;
359
345
360
346
Payer memory _storedPayer = $.payers[payer];
361
347
362
348
if (_storedPayer.balance < usage) {
363
349
uint256 _debt = usage - _storedPayer.balance;
364
350
365
- $.pendingFees += _storedPayer.balance;
366
- _fees += _storedPayer.balance;
351
+ _settledFees += _storedPayer.balance;
352
+ _pendingFees += _storedPayer.balance;
367
353
368
354
_storedPayer.balance = 0 ;
369
355
_storedPayer.debtAmount = _debt;
@@ -372,15 +358,15 @@ contract Payer is
372
358
_addDebtor (payer);
373
359
_increaseTotalDebtAmount (_debt);
374
360
375
- if (_debt > $.maxTolerableDebtAmountMicroDollars) _deactivatePayer (payer);
361
+ if (_debt > $.maxTolerableDebtAmountMicroDollars) _deactivatePayer (PAYER_OPERATOR_ID, payer);
376
362
377
363
emit PayerBalanceUpdated (payer, _storedPayer.balance, _storedPayer.debtAmount);
378
364
379
365
continue ;
380
366
}
381
367
382
- $.pendingFees += usage;
383
- _fees += usage;
368
+ _settledFees += usage;
369
+ _pendingFees += usage;
384
370
385
371
_storedPayer.balance -= usage;
386
372
@@ -389,7 +375,9 @@ contract Payer is
389
375
emit PayerBalanceUpdated (payer, _storedPayer.balance, _storedPayer.debtAmount);
390
376
}
391
377
392
- emit UsageSettled (originatorNode, block .timestamp , _fees);
378
+ $.pendingFees = _pendingFees;
379
+
380
+ emit UsageSettled (originatorNode, block .timestamp , _settledFees);
393
381
}
394
382
395
383
/**
@@ -404,17 +392,17 @@ contract Payer is
404
392
// slither-disable-next-line timestamp
405
393
require (block .timestamp - $.lastFeeTransferTimestamp >= $.transferFeesPeriod, InsufficientTimePassed ());
406
394
407
- require ($.pendingFees > 0 , InsufficientAmount ()) ;
395
+ uint256 _pendingFeesAmount = $.pendingFees ;
408
396
409
- uint256 _feesToTransfer = $.pendingFees ;
397
+ require (_pendingFeesAmount > 0 , InsufficientAmount ()) ;
410
398
411
- $.usdcToken.safeTransfer ($.distributionContract, _feesToTransfer );
399
+ $.usdcToken.safeTransfer ($.distributionContract, _pendingFeesAmount );
412
400
413
401
$.lastFeeTransferTimestamp = block .timestamp ;
414
- $.collectedFees += $.pendingFees ;
402
+ $.collectedFees += _pendingFeesAmount ;
415
403
$.pendingFees = 0 ;
416
404
417
- emit FeesTransferred (block .timestamp , _feesToTransfer );
405
+ emit FeesTransferred (block .timestamp , _pendingFeesAmount );
418
406
}
419
407
420
408
/* ========== Administrative Functions ========== */
@@ -700,6 +688,24 @@ contract Payer is
700
688
701
689
/* ============ Internal ============ */
702
690
691
+ /**
692
+ * @notice Validates and processes a deposit or donation
693
+ * @param from The address funds are coming from
694
+ * @param to The payer account receiving the deposit
695
+ * @param amount The amount to deposit
696
+ */
697
+ function _validateAndProcessDeposit (address from , address to , uint256 amount ) internal {
698
+ PayerStorage storage $ = _getPayerStorage ();
699
+
700
+ require (amount >= $.minimumDepositAmountMicroDollars, InsufficientAmount ());
701
+ require ($.withdrawals[to].requestTimestamp == 0 , PayerInWithdrawal ());
702
+
703
+ _deposit (from, amount);
704
+ _updatePayerBalance (to, amount);
705
+
706
+ emit PayerBalanceUpdated (to, $.payers[to].balance, $.payers[to].debtAmount);
707
+ }
708
+
703
709
/**
704
710
* @notice Deposits USDC from a payer to the contract.
705
711
* @param payer The address of the payer.
@@ -720,11 +726,16 @@ contract Payer is
720
726
function _updatePayerBalance (address payerAddress , uint256 amount ) internal returns (uint256 leftoverAmount ) {
721
727
PayerStorage storage $ = _getPayerStorage ();
722
728
723
- if ($.payers[payerAddress].debtAmount > 0 ) {
729
+ Payer memory _payer = $.payers[payerAddress];
730
+
731
+ if (_payer.debtAmount > 0 ) {
724
732
return _settleDebts (payerAddress, amount);
725
733
} else {
726
- $.payers[payerAddress] .balance += amount;
734
+ _payer .balance += amount;
727
735
_increaseTotalAmountDeposited (amount);
736
+
737
+ $.payers[payerAddress] = _payer;
738
+
728
739
return amount;
729
740
}
730
741
}
@@ -787,15 +798,15 @@ contract Payer is
787
798
* @notice Deactivates a payer.
788
799
* @param payer The address of the payer to deactivate.
789
800
*/
790
- function _deactivatePayer (address payer ) internal {
801
+ function _deactivatePayer (uint256 operatorId , address payer ) internal {
791
802
PayerStorage storage $ = _getPayerStorage ();
792
803
793
804
$.payers[payer].isActive = false ;
794
805
795
806
// Deactivating a payer only removes them from the active payers set
796
807
require ($.activePayers.remove (payer), FailedToDeactivatePayer ());
797
808
798
- emit PayerDeactivated (payer);
809
+ emit PayerDeactivated (operatorId, payer);
799
810
}
800
811
801
812
/**
@@ -849,17 +860,16 @@ contract Payer is
849
860
850
861
/**
851
862
* @notice Checks if a given address is an active node operator.
852
- * @param operator The address to check.
863
+ * @param nodeId The nodeID of the operator to check.
853
864
* @return isActiveNodeOperator True if the address is an active node operator, false otherwise.
854
865
*/
855
- function _getIsActiveNodeOperator (address operator ) internal view returns (bool isActiveNodeOperator ) {
866
+ function _getIsActiveNodeOperator (uint256 nodeId ) internal view returns (bool isActiveNodeOperator ) {
856
867
INodes nodes = INodes (_getPayerStorage ().nodesContract);
857
868
858
- require (address (nodes) != address ( 0 ), Unauthorized ());
869
+ require (msg . sender == nodes. ownerOf (nodeId ), Unauthorized ());
859
870
860
- // TODO: Implement this in Nodes contract
861
- // return nodes.isActiveNodeOperator(operator);
862
- return true ;
871
+ // TODO: Change for a better filter.
872
+ return nodes.getReplicationNodeIsActive (nodeId);
863
873
}
864
874
865
875
/**
0 commit comments