Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ewilz committed Jul 27, 2022
1 parent 6529ea4 commit c5fa01c
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 19 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# narwhal

## Usage

```
forge install
forge build
forge test
```
6 changes: 5 additions & 1 deletion foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ src = 'src'
out = 'out'
libs = ['lib']

# See more config options https://github.com/foundry-rs/foundry/tree/master/config
[fmt]
line_length = 120
quote_style = 'single'

# See more config options https://github.com/foundry-rs/foundry/tree/master/config
4 changes: 2 additions & 2 deletions script/Contract.s.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;
pragma solidity ^0.8.15;

import "forge-std/Script.sol";
import 'forge-std/Script.sol';

contract ContractScript is Script {
function setUp() public {}
Expand Down
4 changes: 0 additions & 4 deletions src/Contract.sol

This file was deleted.

18 changes: 18 additions & 0 deletions src/Router.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.15;

contract Router {
struct ModuleCall {
address module;
bytes data;
}

function route(ModuleCall[] calldata calls) external {
for (uint256 i = 0; i < calls.length; i++) {
(bool success, bytes memory resultData) = calls[i].module.delegatecall(calls[i].data);
if (!success) assembly {
revert(add(resultData, 32), mload(resultData))
}
}
}
}
16 changes: 16 additions & 0 deletions src/modules/ExampleModule.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.15;

contract ExampleModule {
event ExampleModuleEvent(string message);

error CauseRevert();

function logEvent() public {
emit ExampleModuleEvent('testEvent');
}

function causeRevert() public pure {
revert CauseRevert();
}
}
12 changes: 0 additions & 12 deletions test/Contract.t.sol

This file was deleted.

32 changes: 32 additions & 0 deletions test/Router.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.15;

import 'forge-std/Test.sol';
import {Router} from '../src/Router.sol';
import {ExampleModule} from '../src/modules/ExampleModule.sol';

contract RouterTest is Test {
Router router;
ExampleModule testModule;
Router.ModuleCall[] calls;

function setUp() public {
router = new Router();
testModule = new ExampleModule();
}

event ExampleModuleEvent(string message);

function testCallModule() public {
calls.push(Router.ModuleCall(address(testModule), abi.encode(testModule.logEvent.selector)));
vm.expectEmit(false, false, false, true);
emit ExampleModuleEvent('testEvent');
router.route(calls);
}

function testModuleRevert() public {
calls.push(Router.ModuleCall(address(testModule), abi.encode(testModule.causeRevert.selector)));
vm.expectRevert(ExampleModule.CauseRevert.selector);
router.route(calls);
}
}

0 comments on commit c5fa01c

Please sign in to comment.