Skip to content

Commit ccbc6d6

Browse files
committed
add the gas optimization for batch eth functions and clean tests
1 parent 385c36f commit ccbc6d6

File tree

5 files changed

+178
-210
lines changed

5 files changed

+178
-210
lines changed

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

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,12 @@ contract BatchConversionPayments is BatchNoConversionPayments {
7171
* - batchEthConversionPayments, paymentNetworkId=4
7272
* If metaDetails use paymentNetworkId = 4, it must be at the end of the list, or the transaction can be reverted.
7373
* @param pathsToUSD The list of paths into USD for every token, used to limit the batch fees.
74-
* Without paths, there is not limitation.
75-
* @param feeAddress The address where fees should be paid
76-
* @dev batchPayment only reduces gas consumption when using more than a single payment network.
74+
* For batchEth, mock an array of array to apply the limit, e.g: [[]]
75+
* Without paths, there is not limitation, neither for the batchEth functions.
76+
* @param feeAddress The address where fees should be paid.
77+
* @dev Use pathsToUSD only if you are pretty sure the batch fees will higher than the
78+
* USD limit batchFeeAmountUSDLimit, because it increase gas consumption.
79+
* batchPayment only reduces gas consumption when using more than a single payment network.
7780
* For single payment network payments, it is more efficient to use the suited batch function.
7881
*/
7982
function batchPayment(
@@ -120,6 +123,7 @@ contract BatchConversionPayments is BatchNoConversionPayments {
120123
}
121124
batchFeeAmountUSD += batchEthPayments(
122125
metaDetail.requestDetails,
126+
pathsToUSD.length > 0,
123127
batchFeeAmountUSD,
124128
payable(feeAddress)
125129
);
@@ -129,6 +133,7 @@ contract BatchConversionPayments is BatchNoConversionPayments {
129133
} else if (metaDetail.paymentNetworkId == 4) {
130134
batchFeeAmountUSD += batchEthConversionPayments(
131135
metaDetail.requestDetails,
136+
pathsToUSD.length > 0,
132137
batchFeeAmountUSD,
133138
payable(feeAddress)
134139
);
@@ -226,6 +231,7 @@ contract BatchConversionPayments is BatchNoConversionPayments {
226231
* @notice Send a batch of ETH conversion payments with fees and paymentReferences to multiple accounts.
227232
* If one payment fails, the whole batch is reverted.
228233
* @param requestDetails List of ETH requests denominated in fiat to pay.
234+
* @param applyFeeLimitUSD It set to true to apply the USD fee limit.
229235
* @param batchFeeAmountUSD The batch fee amount in USD already paid.
230236
* @param feeAddress The fee recipient.
231237
* @dev It uses EthereumConversionProxy to pay an invoice and fees.
@@ -236,11 +242,12 @@ contract BatchConversionPayments is BatchNoConversionPayments {
236242
*/
237243
function batchEthConversionPayments(
238244
RequestDetail[] calldata requestDetails,
245+
bool applyFeeLimitUSD,
239246
uint256 batchFeeAmountUSD,
240247
address payable feeAddress
241248
) public payable returns (uint256) {
242249
// Avoid the possibility to manually put high value to batchFeeAmountUSD
243-
if (batchPaymentOrigin != true) {
250+
if (batchPaymentOrigin != true && applyFeeLimitUSD) {
244251
batchFeeAmountUSD = 0;
245252
}
246253
uint256 contractBalance = address(this).balance;
@@ -264,12 +271,15 @@ contract BatchConversionPayments is BatchNoConversionPayments {
264271
uint256 batchFeeToPay = (((contractBalance - address(this).balance)) * batchFee) /
265272
feeDenominator;
266273

267-
(batchFeeToPay, batchFeeAmountUSD) = calculateBatchFeeToPay(
268-
batchFeeToPay,
269-
pathsEthToUSD[0][0],
270-
batchFeeAmountUSD,
271-
pathsEthToUSD
272-
);
274+
if (applyFeeLimitUSD == true) {
275+
(batchFeeToPay, batchFeeAmountUSD) = calculateBatchFeeToPay(
276+
batchFeeToPay,
277+
pathsEthToUSD[0][0],
278+
batchFeeAmountUSD,
279+
pathsEthToUSD
280+
);
281+
}
282+
273283
require(address(this).balance >= batchFeeToPay, 'Not enough funds for batch conversion fees');
274284
feeAddress.transfer(batchFeeToPay);
275285

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,18 +108,20 @@ contract BatchNoConversionPayments is Ownable {
108108
* @notice Send a batch of ETH (or EVM native token) payments with fees and paymentReferences to multiple accounts.
109109
* If one payment fails, the whole batch reverts.
110110
* @param requestDetails List of ETH requests to pay.
111+
* @param applyFeeLimitUSD It set to true to apply the USD fee limit.
111112
* @param batchFeeAmountUSD The batch fee amount in USD already paid.
112113
* @param feeAddress The fee recipient.
113114
* @dev It uses EthereumFeeProxy to pay an invoice and fees with a payment reference.
114115
* Make sure: msg.value >= sum(_amouts)+sum(_feeAmounts)+sumBatchFeeAmount
115116
*/
116117
function batchEthPayments(
117118
RequestDetail[] calldata requestDetails,
119+
bool applyFeeLimitUSD,
118120
uint256 batchFeeAmountUSD,
119121
address payable feeAddress
120122
) public payable returns (uint256) {
121123
// Avoid the possibility to manually put high value to batchFeeAmountUSD
122-
if (batchPaymentOrigin != true) {
124+
if (batchPaymentOrigin != true && applyFeeLimitUSD == true) {
123125
batchFeeAmountUSD = 0;
124126
}
125127
// amount is used to get the total amount and then used as batch fee amount
@@ -141,12 +143,14 @@ contract BatchNoConversionPayments is Ownable {
141143

142144
// amount is updated into batch fee amount
143145
amount = (amount * batchFee) / feeDenominator;
144-
(amount, batchFeeAmountUSD) = calculateBatchFeeToPay(
145-
amount,
146-
pathsEthToUSD[0][0],
147-
batchFeeAmountUSD,
148-
pathsEthToUSD
149-
);
146+
if (applyFeeLimitUSD == true) {
147+
(amount, batchFeeAmountUSD) = calculateBatchFeeToPay(
148+
amount,
149+
pathsEthToUSD[0][0],
150+
batchFeeAmountUSD,
151+
pathsEthToUSD
152+
);
153+
}
150154
// Check that batch contract has enough funds to pay batch fee
151155
require(address(this).balance >= amount, 'Not enough funds for batch fee');
152156
// Batch pays batch fee

packages/smart-contracts/src/lib/artifacts/BatchConversionPayments/0.1.0.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,11 @@
196196
"name": "requestDetails",
197197
"type": "tuple[]"
198198
},
199+
{
200+
"internalType": "bool",
201+
"name": "applyFeeLimitUSD",
202+
"type": "bool"
203+
},
199204
{
200205
"internalType": "uint256",
201206
"name": "batchFeeAmountUSD",
@@ -262,6 +267,11 @@
262267
"name": "requestDetails",
263268
"type": "tuple[]"
264269
},
270+
{
271+
"internalType": "bool",
272+
"name": "applyFeeLimitUSD",
273+
"type": "bool"
274+
},
265275
{
266276
"internalType": "uint256",
267277
"name": "batchFeeAmountUSD",

0 commit comments

Comments
 (0)