Skip to content

Commit d2f0cfe

Browse files
committed
add privates functions - rename enable by skip fee USD limit - change variable uint size -
1 parent 1d2855d commit d2f0cfe

File tree

6 files changed

+156
-143
lines changed

6 files changed

+156
-143
lines changed

packages/smart-contracts/src/contracts/BatchConversionPayments.sol

Lines changed: 52 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -86,31 +86,25 @@ contract BatchConversionPayments is BatchNoConversionPayments {
8686
) external payable {
8787
require(metaDetails.length < 6, 'more than 5 metaDetails');
8888

89-
// Check that there are paths to USD, and more than one paymentNetworkId
90-
if (pathsToUSD.length > 0 && metaDetails.length > 1) {
91-
// Set to true to avoid batchFeeAmountUSD to be reset by each batch function
92-
batchPaymentOrigin = true;
93-
}
94-
9589
uint256 batchFeeAmountUSD = 0;
9690
for (uint256 i = 0; i < metaDetails.length; i++) {
9791
MetaDetail calldata metaDetail = metaDetails[i];
9892
if (metaDetail.paymentNetworkId == 0) {
99-
batchFeeAmountUSD += batchMultiERC20ConversionPayments(
93+
batchFeeAmountUSD += _batchMultiERC20ConversionPayments(
10094
metaDetail.requestDetails,
10195
batchFeeAmountUSD,
10296
pathsToUSD,
10397
feeAddress
10498
);
10599
} else if (metaDetail.paymentNetworkId == 1) {
106-
batchFeeAmountUSD += batchERC20Payments(
100+
batchFeeAmountUSD += _batchERC20Payments(
107101
metaDetail.requestDetails,
108102
pathsToUSD,
109103
batchFeeAmountUSD,
110-
feeAddress
104+
payable(feeAddress)
111105
);
112106
} else if (metaDetail.paymentNetworkId == 2) {
113-
batchFeeAmountUSD += batchMultiERC20Payments(
107+
batchFeeAmountUSD += _batchMultiERC20Payments(
114108
metaDetail.requestDetails,
115109
pathsToUSD,
116110
batchFeeAmountUSD,
@@ -121,51 +115,79 @@ contract BatchConversionPayments is BatchNoConversionPayments {
121115
// Set to false only if batchEthConversionPayments is called after this function
122116
transferBackRemainingEth = false;
123117
}
124-
batchFeeAmountUSD += batchEthPayments(
118+
batchFeeAmountUSD += _batchEthPayments(
125119
metaDetail.requestDetails,
126-
pathsToUSD.length > 0,
120+
pathsToUSD.length == 0,
127121
batchFeeAmountUSD,
128122
payable(feeAddress)
129123
);
130124
if (metaDetails[metaDetails.length - 1].paymentNetworkId == 4) {
131125
transferBackRemainingEth = true;
132126
}
133127
} else if (metaDetail.paymentNetworkId == 4) {
134-
batchFeeAmountUSD += batchEthConversionPayments(
128+
batchFeeAmountUSD += _batchEthConversionPayments(
135129
metaDetail.requestDetails,
136-
pathsToUSD.length > 0,
130+
pathsToUSD.length == 0,
137131
batchFeeAmountUSD,
138132
payable(feeAddress)
139133
);
140134
} else {
141135
revert('Wrong paymentNetworkId');
142136
}
143137
}
144-
if (pathsToUSD.length > 0 && metaDetails.length > 1) {
145-
// Set back to false, its default value
146-
batchPaymentOrigin = false;
147-
}
148138
}
149139

150140
/**
151141
* @notice Send a batch of ERC20 payments with amounts based on a request
152142
* currency (e.g. fiat), with fees and paymentReferences to multiple accounts, with multiple tokens.
153143
* @param requestDetails List of ERC20 requests denominated in fiat to pay.
154-
* @param batchFeeAmountUSD The batch fee amount in USD already paid.
155144
* @param pathsToUSD The list of paths into USD for every token, used to limit the batch fees.
156-
* Without paths, there is not limitation.
145+
* Without paths, there is not a fee limitation, and it consumes less gas.
157146
* @param feeAddress The fee recipient.
158147
*/
159148
function batchMultiERC20ConversionPayments(
160149
RequestDetail[] calldata requestDetails,
161-
uint256 batchFeeAmountUSD,
162150
address[][] calldata pathsToUSD,
163151
address feeAddress
164152
) public returns (uint256) {
165-
// Avoid the possibility to manually put high value to batchFeeAmountUSD
166-
if (batchPaymentOrigin != true) {
167-
batchFeeAmountUSD = 0;
168-
}
153+
return _batchMultiERC20ConversionPayments(requestDetails, 0, pathsToUSD, feeAddress);
154+
}
155+
156+
/**
157+
* @notice Send a batch of ETH conversion payments with fees and paymentReferences to multiple accounts.
158+
* If one payment fails, the whole batch is reverted.
159+
* @param requestDetails List of ETH requests denominated in fiat to pay.
160+
* @param skipFeeUSDLimit Setting the value to true skips the USD fee limit, and reduce gas consumption.
161+
* @param feeAddress The fee recipient.
162+
* @dev It uses EthereumConversionProxy to pay an invoice and fees.
163+
* Please:
164+
* Note that if there is not enough ether attached to the function call,
165+
* the following error is thrown: "revert paymentProxy transferExactEthWithReferenceAndFee failed"
166+
* This choice reduces the gas significantly, by delegating the whole conversion to the payment proxy.
167+
*/
168+
function batchEthConversionPayments(
169+
RequestDetail[] calldata requestDetails,
170+
bool skipFeeUSDLimit,
171+
address payable feeAddress
172+
) public payable returns (uint256) {
173+
return _batchEthConversionPayments(requestDetails, skipFeeUSDLimit, 0, feeAddress);
174+
}
175+
176+
/**
177+
* @notice Send a batch of ERC20 payments with amounts based on a request
178+
* currency (e.g. fiat), with fees and paymentReferences to multiple accounts, with multiple tokens.
179+
* @param requestDetails List of ERC20 requests denominated in fiat to pay.
180+
* @param batchFeeAmountUSD The batch fee amount in USD already paid.
181+
* @param pathsToUSD The list of paths into USD for every token, used to limit the batch fees.
182+
* Without paths, there is not a fee limitation, and it consumes less gas.
183+
* @param feeAddress The fee recipient.
184+
*/
185+
function _batchMultiERC20ConversionPayments(
186+
RequestDetail[] calldata requestDetails,
187+
uint256 batchFeeAmountUSD,
188+
address[][] calldata pathsToUSD,
189+
address feeAddress
190+
) private returns (uint256) {
169191
Token[] memory uTokens = getUTokens(requestDetails);
170192

171193
IERC20 requestedToken;
@@ -231,7 +253,7 @@ contract BatchConversionPayments is BatchNoConversionPayments {
231253
* @notice Send a batch of ETH conversion payments with fees and paymentReferences to multiple accounts.
232254
* If one payment fails, the whole batch is reverted.
233255
* @param requestDetails List of ETH requests denominated in fiat to pay.
234-
* @param applyFeeLimitUSD It set to true to apply the USD fee limit.
256+
* @param skipFeeUSDLimit Setting the value to true skips the USD fee limit, and reduce gas consumption.
235257
* @param batchFeeAmountUSD The batch fee amount in USD already paid.
236258
* @param feeAddress The fee recipient.
237259
* @dev It uses EthereumConversionProxy to pay an invoice and fees.
@@ -240,16 +262,12 @@ contract BatchConversionPayments is BatchNoConversionPayments {
240262
* the following error is thrown: "revert paymentProxy transferExactEthWithReferenceAndFee failed"
241263
* This choice reduces the gas significantly, by delegating the whole conversion to the payment proxy.
242264
*/
243-
function batchEthConversionPayments(
265+
function _batchEthConversionPayments(
244266
RequestDetail[] calldata requestDetails,
245-
bool applyFeeLimitUSD,
267+
bool skipFeeUSDLimit,
246268
uint256 batchFeeAmountUSD,
247269
address payable feeAddress
248-
) public payable returns (uint256) {
249-
// Avoid the possibility to manually put high value to batchFeeAmountUSD
250-
if (batchPaymentOrigin != true && applyFeeLimitUSD) {
251-
batchFeeAmountUSD = 0;
252-
}
270+
) private returns (uint256) {
253271
uint256 contractBalance = address(this).balance;
254272
payerAuthorized = true;
255273

@@ -271,7 +289,7 @@ contract BatchConversionPayments is BatchNoConversionPayments {
271289
uint256 batchFeeToPay = (((contractBalance - address(this).balance)) * batchFee) /
272290
feeDenominator;
273291

274-
if (applyFeeLimitUSD == true) {
292+
if (skipFeeUSDLimit == false) {
275293
(batchFeeToPay, batchFeeAmountUSD) = calculateBatchFeeToPay(
276294
batchFeeToPay,
277295
pathsEthToUSD[0][0],

packages/smart-contracts/src/contracts/BatchNoConversionPayments.sol

Lines changed: 70 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,14 @@ contract BatchNoConversionPayments is Ownable {
2929
ChainlinkConversionPath public chainlinkConversionPath;
3030

3131
/** Used to calculate batch fees: batchFee = 30 represent 0.30% of fee */
32-
uint256 public batchFee;
32+
uint16 public batchFee;
3333
/** Used to calculate batch fees: divide batchFee by feeDenominator */
34-
uint256 internal feeDenominator = 10000;
34+
uint16 internal feeDenominator = 10000;
3535
/** The amount of the batch fee cannot exceed a predefined amount in USD */
36-
uint256 public batchFeeAmountUSDLimit;
36+
uint64 public batchFeeAmountUSDLimit;
3737

3838
/** payerAuthorized is set to true only when needed for batch Eth conversion */
3939
bool internal payerAuthorized = false;
40-
/** batchPayment function is the caller */
41-
bool internal batchPaymentOrigin = false;
4240
/** transferBackRemainingEth is set to false only if the payer use batchPayment
4341
and call both batchEthPayments and batchConversionEthPaymentsWithReference */
4442
bool internal transferBackRemainingEth = true;
@@ -108,22 +106,71 @@ contract BatchNoConversionPayments is Ownable {
108106
* @notice Send a batch of ETH (or EVM native token) payments with fees and paymentReferences to multiple accounts.
109107
* If one payment fails, the whole batch reverts.
110108
* @param requestDetails List of ETH requests to pay.
111-
* @param applyFeeLimitUSD It set to true to apply the USD fee limit.
112-
* @param batchFeeAmountUSD The batch fee amount in USD already paid.
109+
* @param skipFeeUSDLimit Setting the value to true skips the USD fee limit, and reduce gas consumption.
113110
* @param feeAddress The fee recipient.
114111
* @dev It uses EthereumFeeProxy to pay an invoice and fees with a payment reference.
115112
* Make sure: msg.value >= sum(_amouts)+sum(_feeAmounts)+sumBatchFeeAmount
116113
*/
117114
function batchEthPayments(
118115
RequestDetail[] calldata requestDetails,
119-
bool applyFeeLimitUSD,
120-
uint256 batchFeeAmountUSD,
116+
bool skipFeeUSDLimit,
121117
address payable feeAddress
122118
) public payable returns (uint256) {
123-
// Avoid the possibility to manually put high value to batchFeeAmountUSD
124-
if (batchPaymentOrigin != true && applyFeeLimitUSD == true) {
125-
batchFeeAmountUSD = 0;
126-
}
119+
return _batchEthPayments(requestDetails, skipFeeUSDLimit, 0, payable(feeAddress));
120+
}
121+
122+
/**
123+
* @notice Send a batch of ERC20 payments with fees and paymentReferences to multiple accounts.
124+
* @param requestDetails List of ERC20 requests to pay, with only one ERC20 token.
125+
* @param pathsToUSD The list of paths into USD for every token, used to limit the batch fees.
126+
* Without paths, there is not a fee limitation, and it consumes less gas.
127+
* @param feeAddress The fee recipient.
128+
* @dev Uses ERC20FeeProxy to pay an invoice and fees, with a payment reference.
129+
* Make sure this contract has enough allowance to spend the payer's token.
130+
* Make sure the payer has enough tokens to pay the amount, the fee, and the batch fee.
131+
*/
132+
function batchERC20Payments(
133+
RequestDetail[] calldata requestDetails,
134+
address[][] calldata pathsToUSD,
135+
address feeAddress
136+
) public returns (uint256) {
137+
return _batchERC20Payments(requestDetails, pathsToUSD, 0, feeAddress);
138+
}
139+
140+
/**
141+
* @notice Send a batch of ERC20 payments with fees and paymentReferences to multiple accounts, with multiple tokens.
142+
* @param requestDetails List of ERC20 requests to pay.
143+
* @param pathsToUSD The list of paths into USD for every token, used to limit the batch fees.
144+
* Without paths, there is not a fee limitation, and it consumes less gas.
145+
* @param feeAddress The fee recipient.
146+
* @dev It uses ERC20FeeProxy to pay an invoice and fees, with a payment reference.
147+
* Make sure this contract has enough allowance to spend the payer's token.
148+
* Make sure the payer has enough tokens to pay the amount, the fee, and the batch fee.
149+
*/
150+
function batchMultiERC20Payments(
151+
RequestDetail[] calldata requestDetails,
152+
address[][] calldata pathsToUSD,
153+
address feeAddress
154+
) public returns (uint256) {
155+
return _batchMultiERC20Payments(requestDetails, pathsToUSD, 0, feeAddress);
156+
}
157+
158+
/**
159+
* @notice Send a batch of ETH (or EVM native token) payments with fees and paymentReferences to multiple accounts.
160+
* If one payment fails, the whole batch reverts.
161+
* @param requestDetails List of ETH requests to pay.
162+
* @param skipFeeUSDLimit Setting the value to true skips the USD fee limit, and reduce gas consumption.
163+
* @param batchFeeAmountUSD The batch fee amount in USD already paid.
164+
* @param feeAddress The fee recipient.
165+
* @dev It uses EthereumFeeProxy to pay an invoice and fees with a payment reference.
166+
* Make sure: msg.value >= sum(_amouts)+sum(_feeAmounts)+sumBatchFeeAmount
167+
*/
168+
function _batchEthPayments(
169+
RequestDetail[] calldata requestDetails,
170+
bool skipFeeUSDLimit,
171+
uint256 batchFeeAmountUSD,
172+
address payable feeAddress
173+
) internal returns (uint256) {
127174
// amount is used to get the total amount and then used as batch fee amount
128175
uint256 amount = 0;
129176

@@ -143,7 +190,7 @@ contract BatchNoConversionPayments is Ownable {
143190

144191
// amount is updated into batch fee amount
145192
amount = (amount * batchFee) / feeDenominator;
146-
if (applyFeeLimitUSD == true) {
193+
if (skipFeeUSDLimit == false) {
147194
(amount, batchFeeAmountUSD) = calculateBatchFeeToPay(
148195
amount,
149196
pathsEthToUSD[0][0],
@@ -168,23 +215,19 @@ contract BatchNoConversionPayments is Ownable {
168215
* @notice Send a batch of ERC20 payments with fees and paymentReferences to multiple accounts.
169216
* @param requestDetails List of ERC20 requests to pay, with only one ERC20 token.
170217
* @param pathsToUSD The list of paths into USD for every token, used to limit the batch fees.
171-
* Without paths, there is not limitation.
218+
* Without paths, there is not a fee limitation, and it consumes less gas.
172219
* @param batchFeeAmountUSD The batch fee amount in USD already paid.
173220
* @param feeAddress The fee recipient.
174221
* @dev Uses ERC20FeeProxy to pay an invoice and fees, with a payment reference.
175222
* Make sure this contract has enough allowance to spend the payer's token.
176223
* Make sure the payer has enough tokens to pay the amount, the fee, and the batch fee.
177224
*/
178-
function batchERC20Payments(
225+
function _batchERC20Payments(
179226
RequestDetail[] calldata requestDetails,
180227
address[][] calldata pathsToUSD,
181228
uint256 batchFeeAmountUSD,
182229
address feeAddress
183-
) public returns (uint256) {
184-
// Avoid the possibility to manually put high value to batchFeeAmountUSD
185-
if (batchPaymentOrigin != true) {
186-
batchFeeAmountUSD = 0;
187-
}
230+
) internal returns (uint256) {
188231
uint256 amountAndFee = 0;
189232
uint256 batchFeeAmount = 0;
190233
for (uint256 i = 0; i < requestDetails.length; i++) {
@@ -236,23 +279,19 @@ contract BatchNoConversionPayments is Ownable {
236279
* @notice Send a batch of ERC20 payments with fees and paymentReferences to multiple accounts, with multiple tokens.
237280
* @param requestDetails List of ERC20 requests to pay.
238281
* @param pathsToUSD The list of paths into USD for every token, used to limit the batch fees.
239-
* Without paths, there is not limitation.
282+
* Without paths, there is not a fee limitation, and it consumes less gas.
240283
* @param batchFeeAmountUSD The batch fee amount in USD already paid.
241284
* @param feeAddress The fee recipient.
242285
* @dev It uses ERC20FeeProxy to pay an invoice and fees, with a payment reference.
243286
* Make sure this contract has enough allowance to spend the payer's token.
244287
* Make sure the payer has enough tokens to pay the amount, the fee, and the batch fee.
245288
*/
246-
function batchMultiERC20Payments(
289+
function _batchMultiERC20Payments(
247290
RequestDetail[] calldata requestDetails,
248291
address[][] calldata pathsToUSD,
249292
uint256 batchFeeAmountUSD,
250293
address feeAddress
251-
) public returns (uint256) {
252-
// Avoid the possibility to manually put high value to batchFeeAmountUSD
253-
if (batchPaymentOrigin != true) {
254-
batchFeeAmountUSD = 0;
255-
}
294+
) internal returns (uint256) {
256295
Token[] memory uTokens = getUTokens(requestDetails);
257296

258297
// The payer transfers tokens to the batch contract and pays batch fee
@@ -398,7 +437,7 @@ contract BatchNoConversionPayments is Ownable {
398437
* @param tokenAddress The address of the token
399438
* @param batchFeeAmountUSD The batch fee amount in USD already paid.
400439
* @param pathsToUSD The list of paths into USD for every token, used to limit the batch fees.
401-
* Without paths, there is not limitation.
440+
* Without paths, there is not a fee limitation, and it consumes less gas.
402441
*/
403442
function calculateBatchFeeToPay(
404443
uint256 batchFeeToPay,
@@ -505,7 +544,7 @@ contract BatchNoConversionPayments is Ownable {
505544
* @notice Fees added when using Erc20/Eth batch functions
506545
* @param _batchFee Between 0 and 200, i.e: batchFee = 30 represent 0.30% of fee
507546
*/
508-
function setBatchFee(uint256 _batchFee) external onlyOwner {
547+
function setBatchFee(uint16 _batchFee) external onlyOwner {
509548
// safety to avoid wrong setting
510549
require(_batchFee <= 200, 'The batch fee value is too high: > 2%');
511550
batchFee = _batchFee;
@@ -548,7 +587,7 @@ contract BatchNoConversionPayments is Ownable {
548587
/**
549588
* @param _batchFeeAmountUSDLimit The limitation of the batch fee amount in USD.
550589
*/
551-
function setBatchFeeAmountUSDLimit(uint256 _batchFeeAmountUSDLimit) external onlyOwner {
590+
function setBatchFeeAmountUSDLimit(uint64 _batchFeeAmountUSDLimit) external onlyOwner {
552591
batchFeeAmountUSDLimit = _batchFeeAmountUSDLimit;
553592
}
554593
}

0 commit comments

Comments
 (0)