Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(StakeManager): use custom error in onlyVault modifier #28

Merged
merged 1 commit into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion .gas-snapshot
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
StakeManagerTest:testDeployment() (gas: 26172)
LeaveTest:testDeployment() (gas: 26172)
LeaveTest:test_RevertWhen_SenderIsNotVault() (gas: 10562)
LockTest:testDeployment() (gas: 26172)
LockTest:test_RevertWhen_SenderIsNotVault() (gas: 10573)
MigrateTest:testDeployment() (gas: 26172)
MigrateTest:test_RevertWhen_SenderIsNotVault() (gas: 10629)
StakeManagerTest:testDeployment() (gas: 26172)
StakeTest:testDeployment() (gas: 26172)
StakeTest:test_RevertWhen_SenderIsNotVault() (gas: 10638)
UnstakeTest:testDeployment() (gas: 26172)
UnstakeTest:test_RevertWhen_SenderIsNotVault() (gas: 10575)
6 changes: 5 additions & 1 deletion contracts/StakeManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { Ownable } from "@openzeppelin/contracts/access/Ownable.sol";
import { StakeVault } from "./StakeVault.sol";

contract StakeManager is Ownable {
error StakeManager__SenderIsNotVault();

struct Account {
uint256 lockUntil;
uint256 balance;
Expand Down Expand Up @@ -40,7 +42,9 @@ contract StakeManager is Ownable {
ERC20 public immutable stakedToken;

modifier onlyVault() {
require(isVault[msg.sender.codehash], "Not a vault");
if (!isVault[msg.sender.codehash]) {
revert StakeManager__SenderIsNotVault();
}
_;
}

Expand Down
55 changes: 55 additions & 0 deletions test/StakeManager.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,58 @@ contract StakeManagerTest is Test {
assertEq(stakeManager.totalSupply(), 0);
}
}

contract StakeTest is StakeManagerTest {
function setUp() public override {
StakeManagerTest.setUp();
}

function test_RevertWhen_SenderIsNotVault() public {
vm.expectRevert(StakeManager.StakeManager__SenderIsNotVault.selector);
stakeManager.stake(100, 1);
}
}

contract UnstakeTest is StakeManagerTest {
function setUp() public override {
StakeManagerTest.setUp();
}

function test_RevertWhen_SenderIsNotVault() public {
vm.expectRevert(StakeManager.StakeManager__SenderIsNotVault.selector);
stakeManager.unstake(100);
}
}

contract LockTest is StakeManagerTest {
function setUp() public override {
StakeManagerTest.setUp();
}

function test_RevertWhen_SenderIsNotVault() public {
vm.expectRevert(StakeManager.StakeManager__SenderIsNotVault.selector);
stakeManager.lock(100);
}
}

contract LeaveTest is StakeManagerTest {
function setUp() public override {
StakeManagerTest.setUp();
}

function test_RevertWhen_SenderIsNotVault() public {
vm.expectRevert(StakeManager.StakeManager__SenderIsNotVault.selector);
stakeManager.leave();
}
}

contract MigrateTest is StakeManagerTest {
function setUp() public override {
StakeManagerTest.setUp();
}

function test_RevertWhen_SenderIsNotVault() public {
vm.expectRevert(StakeManager.StakeManager__SenderIsNotVault.selector);
stakeManager.migrate();
}
}