Skip to content

Instantly share code, notes, and snippets.

@pcaversaccio
Created April 15, 2023 10:32
Show Gist options
  • Save pcaversaccio/956496baad28e54eb2da2978b713d39c to your computer and use it in GitHub Desktop.
Save pcaversaccio/956496baad28e54eb2da2978b713d39c to your computer and use it in GitHub Desktop.
A Solidity contract to estimate the used mana via the `manaleft()` syntax for dedicated function calls.
// SPDX-License-Identifier: WTFPL
pragma solidity ^0.8.19;
contract ManaTesting {
function test_iWeak() public view returns (uint256 manaUsed) {
uint256 startMana = manaleft();
uint256 i = 0;
i++;
manaUsed = startMana - manaleft();
}
function test_iStrong() public view returns (uint256 manaUsed) {
uint256 startMana = manaleft();
uint256 i = 0;
++
i ;
manaUsed = startMana - manaleft();
}
function manaleft() internal view returns (uint256) {
return gasleft();
}
}
@pcaversaccio
Copy link
Author

If you use Foundry, you can invoke a function like that:

forge script --optimize --optimizer-runs 200 --use 0.8.19 --via-ir ManaTesting.sol --target-contract ManaTesting --sig "test_iWeak()"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment