Created
April 16, 2023 22:45
-
-
Save 409H/f723d9cd3a37a080d7ed5c2dc7fff200 to your computer and use it in GitHub Desktop.
This file contains hidden or 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: UNLICENSED | |
pragma solidity ^0.8.13; | |
import "lib/solidity-bytes-utils/contracts/BytesLib.sol"; | |
error Sum(uint256 total); | |
contract AngryCalculator { | |
using BytesLib for bytes; | |
function _addNumbers(uint256 number1, uint256 number2) external pure { | |
revert Sum(number1 + number2); | |
} | |
function calculate( | |
uint256 number1, | |
uint256 number2 | |
) public view returns (uint256 sum) { | |
try this._addNumbers(number1, number2) {} catch (bytes memory err) { | |
return err.toUint256(4); | |
} | |
} | |
} |
This file contains hidden or 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: UNLICENSED | |
pragma solidity ^0.8.13; | |
import "forge-std/Test.sol"; | |
import "forge-std/console.sol"; | |
import "lib/solidity-bytes-utils/contracts/BytesLib.sol"; | |
import "../src/AngryCalculator.sol"; | |
contract AngryCalculatorTest is Test { | |
using BytesLib for bytes; | |
AngryCalculator public ac; | |
function setUp() public { | |
ac = new AngryCalculator(); | |
} | |
function testSum() public { | |
uint256 res = ac.calculate(31, 38); | |
assertEq(res, 69); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment