Skip to content

Instantly share code, notes, and snippets.

@mattaereal
Created May 11, 2023 23:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattaereal/660b9acbfa5dddd9afc88d98d8bb689f to your computer and use it in GitHub Desktop.
Save mattaereal/660b9acbfa5dddd9afc88d98d8bb689f to your computer and use it in GitHub Desktop.
Return or revert scenarios.
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.4;
contract RevertWithString {
function buy(uint amount) public payable {
if (msg.value < amount)
revert("Not enough Ether provided.");
}
// transaction cost 21499 gas
// execution cost 295 gas
}
contract RevertWithRequire {
function buy(uint amount) public payable {
require(msg.value >= amount, "Not enough Ether provided.");
}
// transaction cost 21499 gas
// execution cost 295 gas
}
contract RevertCustomError {
error NotEnoughEtherProvided();
function buy(uint amount) public payable {
if (msg.value < amount)
revert NotEnoughEtherProvided();
}
// transaction cost 21445 gas
// execution cost 241 gas
}
contract RevertCustomErrorWithData {
error MyCustomError(string);
function buy(uint amount) public payable {
if (msg.value < amount)
revert MyCustomError("Not enough Ether provided.");
}
// transaction cost 21499 gas
// execution cost 295 gas
}
contract JustAGetter {
function buy(uint amount) public payable returns (string memory) {
if (msg.value < amount)
return "Not enough ether provided.";
return "Enough ether provided.";
}
// transaction cost 21756 gas
// execution cost 552 gas
}
contract RevertGetter {
error MyCustomError(string);
RevertCustomErrorWithData custom;
constructor() {
custom = new RevertCustomErrorWithData();
}
function getString(uint256 amount) public payable returns (string memory) {
try custom.buy(amount) {
return "This should never run";
} catch (bytes memory lowLevelData) {
return string(lowLevelData);
}
}
// transaction cost 21756 gas
// execution cost 552 gas
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment