Skip to content

Instantly share code, notes, and snippets.

@nakajo2011

nakajo2011/PanicTest.sol

Last active Feb 24, 2021
Embed
What would you like to do?
GBEC 2021.02.25 sample code
pragma solidity >= 0.8.0;
contract CalledContract {
uint[] nums;
function onlyEven(uint256 a) external pure returns(uint) {
uint b = 300 / a;
require(b % 2 == 0, "Ups! Reverting");
return b;
}
function outOfBounds(uint256 _index) external pure returns(uint) {
uint8[4] memory someNums = [1, 2, 3, 4];
return someNums[_index];
}
function getNums(uint _index) external view returns(uint) {
return nums[_index];
}
function addNum(uint num) external {
nums.push(num);
}
function popNum() external {
nums.pop();
}
}
contract PanicTest {
event ReturnDataEvent(bytes someData);
event SuccessEvent(uint _value);
event CatchEvent(string revertReason);
event PanicEvent(uint code);
CalledContract public externalContract;
constructor() {
externalContract = new CalledContract();
}
function execute(uint amount) public {
try externalContract.onlyEven(amount) returns(uint _value) {
emit SuccessEvent(_value);
} catch Error(string memory revertReason) {
emit CatchEvent(revertReason);
} catch Panic(uint _code) {
emit PanicEvent(_code);
} catch (bytes memory returnData) {
emit ReturnDataEvent(returnData);
}
}
function outOfBoundsTest(uint _index) public {
try externalContract.outOfBounds(_index) returns(uint _value) {
emit SuccessEvent(_value);
} catch Error(string memory revertReason) {
emit CatchEvent(revertReason);
} catch Panic(uint _code) {
emit PanicEvent(_code);
} catch (bytes memory returnData) {
emit ReturnDataEvent(returnData);
}
}
function getNums(uint _index) external {
try externalContract.getNums(_index) returns(uint _value) {
emit SuccessEvent(_value);
} catch Error(string memory revertReason) {
emit CatchEvent(revertReason);
} catch Panic(uint _code) {
emit PanicEvent(_code);
} catch (bytes memory returnData) {
emit ReturnDataEvent(returnData);
}
}
function addNum(uint num) external {
try externalContract.addNum(num) {
emit SuccessEvent(num);
} catch Error(string memory revertReason) {
emit CatchEvent(revertReason);
} catch Panic(uint _code) {
emit PanicEvent(_code);
} catch (bytes memory returnData) {
emit ReturnDataEvent(returnData);
}
}
function popNum() external {
try externalContract.popNum() {
emit SuccessEvent(0);
} catch Error(string memory revertReason) {
emit CatchEvent(revertReason);
} catch Panic(uint _code) {
emit PanicEvent(_code);
} catch (bytes memory returnData) {
emit ReturnDataEvent(returnData);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment