Created
May 11, 2023 23:26
-
-
Save mattaereal/660b9acbfa5dddd9afc88d98d8bb689f to your computer and use it in GitHub Desktop.
Return or revert scenarios.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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