Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update GasChecker.sol #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions contracts/GasChecker.sol
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
/**
* @title GasChecker
* @dev A contract that allows to check the gas cost of a function call
*/
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

contract GasChecker {
/**
* @dev Emitted when the actual gas cost is different than the expected one
* @param expected Expected gas cost
* @param actual Actual gas cost
*/
error GasCostDiffers(uint256 expected, uint256 actual);

/**
* @dev Modifier that checks the gas cost of a function call
* @param expected Expected gas cost
*/
modifier checkGasCost(uint256 expected) {
uint256 gas = gasleft();
uint256 gas = gasleft(); // Get the gas remaining at the beginning of the function call
_;
unchecked {
gas -= gasleft();
gas -= gasleft(); // Calculate the actual gas cost by subtracting the remaining gas at the end of the function call from the initial gas
}
if (expected > 0 && gas != expected) {
if (expected > 0 && gas != expected) { // If an expected gas cost is specified and it is not equal to the actual gas cost, revert
revert GasCostDiffers(expected, gas);
}
}
Expand Down