Skip to content

Commit

Permalink
improve gas usage estimation
Browse files Browse the repository at this point in the history
  • Loading branch information
xdev10 committed Jun 15, 2023
1 parent 0da94bc commit 0e784e3
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 9 deletions.
5 changes: 4 additions & 1 deletion contracts/deposit/DepositUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ library DepositUtils {
string memory reason,
bytes memory reasonBytes
) external {
// 63/64 gas is forwarded to external calls, reduce the startingGas to account for this
startingGas -= gasleft() / 63;

Deposit.Props memory deposit = DepositStoreUtils.get(dataStore, key);
if (deposit.account() == address(0)) {
revert Errors.EmptyDeposit();
Expand Down Expand Up @@ -197,7 +200,7 @@ library DepositUtils {
eventEmitter,
depositVault,
deposit.executionFee(),
startingGas - gasleft(),
startingGas,
keeper,
deposit.account()
);
Expand Down
5 changes: 4 additions & 1 deletion contracts/deposit/ExecuteDepositUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ library ExecuteDepositUtils {
// @dev executes a deposit
// @param params ExecuteDepositParams
function executeDeposit(ExecuteDepositParams memory params) external {
// 63/64 gas is forwarded to external calls, reduce the startingGas to account for this
params.startingGas -= gasleft() / 63;

Deposit.Props memory deposit = DepositStoreUtils.get(params.dataStore, params.key);
DepositStoreUtils.remove(params.dataStore, params.key, deposit.account());

Expand Down Expand Up @@ -235,7 +238,7 @@ library ExecuteDepositUtils {
params.eventEmitter,
params.depositVault,
deposit.executionFee(),
params.startingGas - gasleft(),
params.startingGas,
params.keeper,
deposit.account()
);
Expand Down
9 changes: 7 additions & 2 deletions contracts/gas/GasUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,23 @@ library GasUtils {
// @param dataStore DataStore
// @param bank the StrictBank contract holding the execution fee
// @param executionFee the executionFee amount
// @param gasUsed the amount of gas used
// @param startingGas the starting gas
// @param keeper the keeper to pay
// @param refundReceiver the account that should receive any excess gas refunds
function payExecutionFee(
DataStore dataStore,
EventEmitter eventEmitter,
StrictBank bank,
uint256 executionFee,
uint256 gasUsed,
uint256 startingGas,
address keeper,
address refundReceiver
) external {
// 63/64 gas is forwarded to external calls, reduce the startingGas to account for this
startingGas -= gasleft() / 63;
uint256 gasUsed = startingGas - gasleft();

// each external call forwards 63/64 of the remaining gas
uint256 executionFeeForKeeper = adjustGasUsage(dataStore, gasUsed) * tx.gasprice;

if (executionFeeForKeeper > executionFee) {
Expand Down
15 changes: 12 additions & 3 deletions contracts/order/OrderUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ library OrderUtils {
// @dev executes an order
// @param params BaseOrderUtils.ExecuteOrderParams
function executeOrder(BaseOrderUtils.ExecuteOrderParams memory params) external {
// 63/64 gas is forwarded to external calls, reduce the startingGas to account for this
params.startingGas -= gasleft() / 63;

OrderStoreUtils.remove(params.contracts.dataStore, params.key, params.order.account());

BaseOrderUtils.validateNonEmptyOrder(params.order);
Expand Down Expand Up @@ -182,7 +185,7 @@ library OrderUtils {
params.contracts.eventEmitter,
params.contracts.orderVault,
params.order.executionFee(),
params.startingGas - gasleft(),
params.startingGas,
params.keeper,
params.order.account()
);
Expand Down Expand Up @@ -224,6 +227,9 @@ library OrderUtils {
string memory reason,
bytes memory reasonBytes
) external {
// 63/64 gas is forwarded to external calls, reduce the startingGas to account for this
startingGas -= gasleft() / 63;

Order.Props memory order = OrderStoreUtils.get(dataStore, key);
BaseOrderUtils.validateNonEmptyOrder(order);

Expand All @@ -250,7 +256,7 @@ library OrderUtils {
eventEmitter,
orderVault,
order.executionFee(),
startingGas - gasleft(),
startingGas,
keeper,
order.account()
);
Expand All @@ -274,6 +280,9 @@ library OrderUtils {
string memory reason,
bytes memory reasonBytes
) external {
// 63/64 gas is forwarded to external calls, reduce the startingGas to account for this
startingGas -= gasleft() / 63;

Order.Props memory order = OrderStoreUtils.get(dataStore, key);
BaseOrderUtils.validateNonEmptyOrder(order);

Expand All @@ -297,7 +306,7 @@ library OrderUtils {
eventEmitter,
orderVault,
executionFee,
startingGas - gasleft(),
startingGas,
keeper,
order.account()
);
Expand Down
10 changes: 8 additions & 2 deletions contracts/withdrawal/WithdrawalUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ library WithdrawalUtils {
* @param params The parameters for executing the withdrawal.
*/
function executeWithdrawal(ExecuteWithdrawalParams memory params) external {
// 63/64 gas is forwarded to external calls, reduce the startingGas to account for this
params.startingGas -= gasleft() / 63;

Withdrawal.Props memory withdrawal = WithdrawalStoreUtils.get(params.dataStore, params.key);
WithdrawalStoreUtils.remove(params.dataStore, params.key, withdrawal.account());

Expand Down Expand Up @@ -232,7 +235,7 @@ library WithdrawalUtils {
params.eventEmitter,
params.withdrawalVault,
withdrawal.executionFee(),
params.startingGas - gasleft(),
params.startingGas,
params.keeper,
withdrawal.account()
);
Expand All @@ -257,6 +260,9 @@ library WithdrawalUtils {
string memory reason,
bytes memory reasonBytes
) external {
// 63/64 gas is forwarded to external calls, reduce the startingGas to account for this
startingGas -= gasleft() / 63;

Withdrawal.Props memory withdrawal = WithdrawalStoreUtils.get(dataStore, key);
if (withdrawal.account() == address(0)) {
revert Errors.EmptyWithdrawal();
Expand Down Expand Up @@ -285,7 +291,7 @@ library WithdrawalUtils {
eventEmitter,
withdrawalVault,
withdrawal.executionFee(),
startingGas - gasleft(),
startingGas,
keeper,
withdrawal.account()
);
Expand Down

0 comments on commit 0e784e3

Please sign in to comment.