Verified contractExactBUSL-1.1
GasUtils
Address0x2b90eebdcf7455dddede20f85de1fd98257e7960
- Balance
0 ETH
- Transactions
1
- Compiler
solc 0.8.18
- Same code
Only this contract
Details
- Compiler
- solc 0.8.18+commit.87f61d96
- Language
- solidity
- Optimization
- Yes, 10 runs
- License
- BUSL-1.1
- Match
- Exactcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 17,872,609
- Creation transaction
- 0x13dfc9fb28…bb3d9c002b
80 files
The source code is large: one file is shown at a time. Choose a file in the list.
- contracts/gas/GasUtils.solmain10.98 KB
- @openzeppelin/contracts/security/ReentrancyGuard.sol2.66 KB
- @openzeppelin/contracts/token/ERC20/ERC20.sol12.62 KB
- @openzeppelin/contracts/token/ERC20/IERC20.sol2.77 KB
- @openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol683 B
- @openzeppelin/contracts/token/ERC20/extensions/draft-IERC20Permit.sol2.25 KB
- @openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol4.46 KB
- @openzeppelin/contracts/utils/Address.sol8.31 KB
- @openzeppelin/contracts/utils/Context.sol844 B
- @openzeppelin/contracts/utils/Strings.sol2.4 KB
- @openzeppelin/contracts/utils/cryptography/ECDSA.sol8.75 KB
- @openzeppelin/contracts/utils/math/Math.sol8.83 KB
- @openzeppelin/contracts/utils/math/SafeCast.sol35.53 KB
- @openzeppelin/contracts/utils/math/SafeMath.sol6.78 KB
- @openzeppelin/contracts/utils/math/SignedMath.sol1.26 KB
- @openzeppelin/contracts/utils/structs/EnumerableSet.sol12.68 KB
- contracts/adl/AdlUtils.sol12.94 KB
- contracts/bank/Bank.sol3.77 KB
- contracts/bank/StrictBank.sol2.65 KB
- contracts/callback/CallbackUtils.sol8.46 KB
- contracts/callback/IDepositCallbackReceiver.sol799 B
- contracts/callback/IOrderCallbackReceiver.sol1.08 KB
- contracts/callback/IWithdrawalCallbackReceiver.sol856 B
- contracts/chain/ArbSys.sol388 B
- contracts/chain/Chain.sol1.51 KB
- contracts/data/DataStore.sol19.45 KB
- contracts/data/Keys.sol52.65 KB
- contracts/deposit/Deposit.sol6.46 KB
- contracts/error/ErrorUtils.sol1.79 KB
- contracts/error/Errors.sol11.61 KB
- contracts/event/EventEmitter.sol4.2 KB
- contracts/event/EventUtils.sol7.39 KB
- contracts/fee/FeeUtils.sol7.93 KB
- contracts/market/Market.sol1.69 KB
- contracts/market/MarketEventUtils.sol16.67 KB
- contracts/market/MarketPoolValueInfo.sol1.28 KB
- contracts/market/MarketStoreUtils.sol3.88 KB
- contracts/market/MarketToken.sol964 B
- contracts/market/MarketUtils.sol108.38 KB
- contracts/nonce/NonceUtils.sol1.32 KB
- contracts/oracle/IPriceFeed.sol333 B
- contracts/oracle/Oracle.sol27.81 KB
- contracts/oracle/OracleStore.sol2.81 KB
- contracts/oracle/OracleUtils.sol11.14 KB
- contracts/order/BaseOrderUtils.sol21.21 KB
- contracts/order/Order.sol15.26 KB
- contracts/order/OrderEventUtils.sol6.97 KB
- contracts/order/OrderStoreUtils.sol10.92 KB
- contracts/order/OrderVault.sol279 B
- contracts/position/Position.sol7.3 KB
- contracts/position/PositionStoreUtils.sol8.23 KB
- contracts/position/PositionUtils.sol26.16 KB
- contracts/price/Price.sol1.81 KB
- contracts/pricing/PositionPricingUtils.sol20 KB
- contracts/pricing/PricingUtils.sol5.51 KB
- contracts/pricing/SwapPricingUtils.sol15.05 KB
- contracts/referral/IReferralStorage.sol2.41 KB
- contracts/referral/ReferralEventUtils.sol2.41 KB
- contracts/referral/ReferralTier.sol392 B
- contracts/referral/ReferralUtils.sol4.7 KB
- contracts/role/Role.sol1.92 KB
- contracts/role/RoleModule.sol3.56 KB
- contracts/role/RoleStore.sol4.31 KB
- contracts/swap/SwapHandler.sol754 B
- contracts/swap/SwapUtils.sol14.37 KB
- contracts/token/IWNT.sol366 B
- contracts/token/TokenUtils.sol8.29 KB
- contracts/utils/AccountUtils.sol438 B
- contracts/utils/Array.sol5.91 KB
- contracts/utils/Bits.sol755 B
- contracts/utils/Calc.sol5.29 KB
- contracts/utils/Cast.sol268 B
- contracts/utils/EnumerableValues.sol2.89 KB
- contracts/utils/Precision.sol5.58 KB
- contracts/utils/Printer.sol643 B
- contracts/utils/Uint256Mask.sol587 B
- contracts/withdrawal/Withdrawal.sol5.86 KB
- hardhat/console.sol63.94 KB
- prb-math/contracts/PRBMath.sol25.28 KB
- prb-math/contracts/PRBMathUD60x18.sol25.07 KB
contracts/gas/GasUtils.sol
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.0;
import "../data/DataStore.sol";
import "../data/Keys.sol";
import "../utils/Precision.sol";
import "../deposit/Deposit.sol";
import "../withdrawal/Withdrawal.sol";
import "../order/Order.sol";
import "../order/BaseOrderUtils.sol";
import "../bank/StrictBank.sol";
// @title GasUtils
// @dev Library for execution fee estimation and payments
library GasUtils {
using Deposit for Deposit.Props;
using Withdrawal for Withdrawal.Props;
using Order for Order.Props;
using EventUtils for EventUtils.AddressItems;
using EventUtils for EventUtils.UintItems;
using EventUtils for EventUtils.IntItems;
using EventUtils for EventUtils.BoolItems;
using EventUtils for EventUtils.Bytes32Items;
using EventUtils for EventUtils.BytesItems;
using EventUtils for EventUtils.StringItems;
// @param keeper address of the keeper
// @param amount the amount of execution fee received
event KeeperExecutionFee(address keeper, uint256 amount);
// @param user address of the user
// @param amount the amount of execution fee refunded
event UserRefundFee(address user, uint256 amount);
function getMinHandleExecutionErrorGas(DataStore dataStore) internal view returns (uint256) {
return dataStore.getUint(Keys.MIN_HANDLE_EXECUTION_ERROR_GAS);
}
function getExecutionGas(DataStore dataStore, uint256 startingGas) internal view returns (uint256) {
uint256 minHandleErrorGas = GasUtils.getMinHandleExecutionErrorGas(dataStore);
if (startingGas < minHandleErrorGas) {
revert Errors.InsufficientExecutionGas(startingGas, minHandleErrorGas);
}
return startingGas - minHandleErrorGas;
}
// @dev pay the keeper the execution fee and refund any excess amount
//
// @param dataStore DataStore
// @param bank the StrictBank contract holding the execution fee
// @param executionFee the executionFee amount
// @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 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) {
executionFeeForKeeper = executionFee;
}
bank.transferOutNativeToken(
keeper,
executionFeeForKeeper
);
emitKeeperExecutionFee(eventEmitter, keeper, executionFeeForKeeper);
uint256 refundFeeAmount = executionFee - executionFeeForKeeper;
if (refundFeeAmount == 0) {
return;
}
bank.transferOutNativeToken(
refundReceiver,
refundFeeAmount
);
emitExecutionFeeRefund(eventEmitter, refundReceiver, refundFeeAmount);
}
// @dev validate that the provided executionFee is sufficient based on the estimatedGasLimit
// @param dataStore DataStore
// @param estimatedGasLimit the estimated gas limit
// @param executionFee the execution fee provided
function validateExecutionFee(DataStore dataStore, uint256 estimatedGasLimit, uint256 executionFee) internal view {
uint256 gasLimit = adjustGasLimitForEstimate(dataStore, estimatedGasLimit);
uint256 minExecutionFee = gasLimit * tx.gasprice;
if (executionFee < minExecutionFee) {
revert Errors.InsufficientExecutionFee(minExecutionFee, executionFee);
}
}
// @dev adjust the gas usage to pay a small amount to keepers
// @param dataStore DataStore
// @param gasUsed the amount of gas used
function adjustGasUsage(DataStore dataStore, uint256 gasUsed) internal view returns (uint256) {
// gas measurements are done after the call to withOraclePrices
// withOraclePrices may consume a significant amount of gas
// the baseGasLimit used to calculate the execution cost
// should be adjusted to account for this
// additionally, a transaction could fail midway through an execution transaction
// before being cancelled, the possibility of this additional gas cost should
// be considered when setting the baseGasLimit
uint256 baseGasLimit = dataStore.getUint(Keys.EXECUTION_GAS_FEE_BASE_AMOUNT);
// the gas cost is estimated based on the gasprice of the request txn
// the actual cost may be higher if the gasprice is higher in the execution txn
// the multiplierFactor should be adjusted to account for this
uint256 multiplierFactor = dataStore.getUint(Keys.EXECUTION_GAS_FEE_MULTIPLIER_FACTOR);
uint256 gasLimit = baseGasLimit + Precision.applyFactor(gasUsed, multiplierFactor);
return gasLimit;
}
// @dev adjust the estimated gas limit to help ensure the execution fee is sufficient during
// the actual execution
// @param dataStore DataStore
// @param estimatedGasLimit the estimated gas limit
function adjustGasLimitForEstimate(DataStore dataStore, uint256 estimatedGasLimit) internal view returns (uint256) {
uint256 baseGasLimit = dataStore.getUint(Keys.ESTIMATED_GAS_FEE_BASE_AMOUNT);
uint256 multiplierFactor = dataStore.getUint(Keys.ESTIMATED_GAS_FEE_MULTIPLIER_FACTOR);
uint256 gasLimit = baseGasLimit + Precision.applyFactor(estimatedGasLimit, multiplierFactor);
return gasLimit;
}
// @dev the estimated gas limit for deposits
// @param dataStore DataStore
// @param deposit the deposit to estimate the gas limit for
function estimateExecuteDepositGasLimit(DataStore dataStore, Deposit.Props memory deposit) internal view returns (uint256) {
uint256 gasPerSwap = dataStore.getUint(Keys.singleSwapGasLimitKey());
uint256 swapCount = deposit.longTokenSwapPath().length + deposit.shortTokenSwapPath().length;
uint256 gasForSwaps = swapCount * gasPerSwap;
if (deposit.initialLongTokenAmount() == 0 || deposit.initialShortTokenAmount() == 0) {
return dataStore.getUint(Keys.depositGasLimitKey(true)) + deposit.callbackGasLimit() + gasForSwaps;
}
return dataStore.getUint(Keys.depositGasLimitKey(false)) + deposit.callbackGasLimit() + gasForSwaps;
}
// @dev the estimated gas limit for withdrawals
// @param dataStore DataStore
// @param withdrawal the withdrawal to estimate the gas limit for
function estimateExecuteWithdrawalGasLimit(DataStore dataStore, Withdrawal.Props memory withdrawal) internal view returns (uint256) {
uint256 gasPerSwap = dataStore.getUint(Keys.singleSwapGasLimitKey());
uint256 swapCount = withdrawal.longTokenSwapPath().length + withdrawal.shortTokenSwapPath().length;
uint256 gasForSwaps = swapCount * gasPerSwap;
return dataStore.getUint(Keys.withdrawalGasLimitKey()) + withdrawal.callbackGasLimit() + gasForSwaps;
}
// @dev the estimated gas limit for orders
// @param dataStore DataStore
// @param order the order to estimate the gas limit for
function estimateExecuteOrderGasLimit(DataStore dataStore, Order.Props memory order) internal view returns (uint256) {
if (BaseOrderUtils.isIncreaseOrder(order.orderType())) {
return estimateExecuteIncreaseOrderGasLimit(dataStore, order);
}
if (BaseOrderUtils.isDecreaseOrder(order.orderType())) {
return estimateExecuteDecreaseOrderGasLimit(dataStore, order);
}
if (BaseOrderUtils.isSwapOrder(order.orderType())) {
return estimateExecuteSwapOrderGasLimit(dataStore, order);
}
revert Errors.UnsupportedOrderType();
}
// @dev the estimated gas limit for increase orders
// @param dataStore DataStore
// @param order the order to estimate the gas limit for
function estimateExecuteIncreaseOrderGasLimit(DataStore dataStore, Order.Props memory order) internal view returns (uint256) {
uint256 gasPerSwap = dataStore.getUint(Keys.singleSwapGasLimitKey());
return dataStore.getUint(Keys.increaseOrderGasLimitKey()) + gasPerSwap * order.swapPath().length + order.callbackGasLimit();
}
// @dev the estimated gas limit for decrease orders
// @param dataStore DataStore
// @param order the order to estimate the gas limit for
function estimateExecuteDecreaseOrderGasLimit(DataStore dataStore, Order.Props memory order) internal view returns (uint256) {
uint256 gasPerSwap = dataStore.getUint(Keys.singleSwapGasLimitKey());
if (order.decreasePositionSwapType() != Order.DecreasePositionSwapType.NoSwap) {
gasPerSwap += 1;
}
return dataStore.getUint(Keys.decreaseOrderGasLimitKey()) + gasPerSwap * order.swapPath().length + order.callbackGasLimit();
}
// @dev the estimated gas limit for swap orders
// @param dataStore DataStore
// @param order the order to estimate the gas limit for
function estimateExecuteSwapOrderGasLimit(DataStore dataStore, Order.Props memory order) internal view returns (uint256) {
uint256 gasPerSwap = dataStore.getUint(Keys.singleSwapGasLimitKey());
return dataStore.getUint(Keys.swapOrderGasLimitKey()) + gasPerSwap * order.swapPath().length + order.callbackGasLimit();
}
function emitKeeperExecutionFee(
EventEmitter eventEmitter,
address keeper,
uint256 executionFeeAmount
) internal {
EventUtils.EventLogData memory eventData;
eventData.addressItems.initItems(1);
eventData.addressItems.setItem(0, "keeper", keeper);
eventData.uintItems.initItems(1);
eventData.uintItems.setItem(0, "executionFeeAmount", executionFeeAmount);
eventEmitter.emitEventLog1(
"KeeperExecutionFee",
Cast.toBytes32(keeper),
eventData
);
}
function emitExecutionFeeRefund(
EventEmitter eventEmitter,
address receiver,
uint256 refundFeeAmount
) internal {
EventUtils.EventLogData memory eventData;
eventData.addressItems.initItems(1);
eventData.addressItems.setItem(0, "receiver", receiver);
eventData.uintItems.initItems(1);
eventData.uintItems.setItem(0, "refundFeeAmount", refundFeeAmount);
eventEmitter.emitEventLog1(
"ExecutionFeeRefund",
Cast.toBytes32(receiver),
eventData
);
}
}
Compiler settingsShow
{
"metadata": {
"useLiteralContent": true
},
"libraries": {},
"optimizer": {
"runs": 10,
"details": {
"constantOptimizer": true
},
"enabled": true
}
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.