-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit introduces a first version of a `VaultFactory` that later will be extended to be capable of instantiating reward vaults and possible keep track of vault instances per owner. As a first step, this implementation comes with a `createVault()` function which takes care of creating vaults. Because `VaultFactory` also knows about `StakeManager` it can derive the manager's address and stake token from it when creating vaults, allowing the API to be without arguments. Partially addresses #37
- Loading branch information
Showing
5 changed files
with
123 additions
and
6 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.18; | ||
|
||
// import { ERC20 } from "@openzeppelin/contracts/token/ERC20/ERC20.sol"; | ||
import { Ownable2Step } from "@openzeppelin/contracts/access/Ownable2Step.sol"; | ||
import { StakeManager } from "./StakeManager.sol"; | ||
import { StakeVault } from "./StakeVault.sol"; | ||
|
||
contract VaultFactory is Ownable2Step { | ||
error VaultFactory__InvalidStakeManagerAddress(); | ||
|
||
event VaultCreated(address indexed vault, address indexed owner); | ||
|
||
StakeManager public stakeManager; | ||
|
||
constructor(address _stakeManager) { | ||
if (_stakeManager == address(0)) { | ||
revert VaultFactory__InvalidStakeManagerAddress(); | ||
} | ||
stakeManager = StakeManager(_stakeManager); | ||
} | ||
|
||
function setStakeManager(address _stakeManager) external onlyOwner { | ||
if (_stakeManager == address(0) || _stakeManager == address(stakeManager)) { | ||
revert VaultFactory__InvalidStakeManagerAddress(); | ||
} | ||
stakeManager = StakeManager(_stakeManager); | ||
} | ||
|
||
function createVault() external returns (StakeVault) { | ||
StakeVault vault = new StakeVault(msg.sender, stakeManager.stakedToken(), stakeManager); | ||
emit VaultCreated(address(vault), msg.sender); | ||
return vault; | ||
} | ||
} |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.19; | ||
|
||
import { ERC20 } from "@openzeppelin/contracts/token/ERC20/ERC20.sol"; | ||
|
||
import { Test } from "forge-std/Test.sol"; | ||
import { Deploy } from "../script/Deploy.s.sol"; | ||
import { DeploymentConfig } from "../script/DeploymentConfig.s.sol"; | ||
|
||
import { StakeManager } from "../contracts/StakeManager.sol"; | ||
import { StakeVault } from "../contracts/StakeVault.sol"; | ||
import { VaultFactory } from "../contracts/VaultFactory.sol"; | ||
|
||
contract VaultFactoryTest is Test { | ||
DeploymentConfig internal deploymentConfig; | ||
|
||
StakeManager internal stakeManager; | ||
|
||
VaultFactory internal vaultFactory; | ||
|
||
address internal deployer; | ||
|
||
address internal stakedToken; | ||
|
||
address internal testUser = makeAddr("testUser"); | ||
|
||
function setUp() public virtual { | ||
Deploy deployment = new Deploy(); | ||
(vaultFactory, stakeManager, deploymentConfig) = deployment.run(); | ||
(deployer, stakedToken) = deploymentConfig.activeNetworkConfig(); | ||
} | ||
|
||
function testDeployment() public { | ||
assertEq(address(vaultFactory.stakeManager()), address(stakeManager)); | ||
} | ||
} | ||
|
||
contract SetStakeManagerTest is VaultFactoryTest { | ||
function setUp() public override { | ||
VaultFactoryTest.setUp(); | ||
} | ||
|
||
function test_RevertWhen_InvalidStakeManagerAddress() public { | ||
vm.startPrank(deployer); | ||
vm.expectRevert(VaultFactory.VaultFactory__InvalidStakeManagerAddress.selector); | ||
vaultFactory.setStakeManager(address(0)); | ||
|
||
vm.expectRevert(VaultFactory.VaultFactory__InvalidStakeManagerAddress.selector); | ||
vaultFactory.setStakeManager(address(stakeManager)); | ||
} | ||
|
||
function test_SetStakeManager() public { | ||
vm.prank(deployer); | ||
vaultFactory.setStakeManager(address(this)); | ||
assertEq(address(vaultFactory.stakeManager()), address(this)); | ||
} | ||
} | ||
|
||
contract CreateVaultTest is VaultFactoryTest { | ||
event VaultCreated(address indexed vault, address indexed owner); | ||
|
||
function setUp() public override { | ||
VaultFactoryTest.setUp(); | ||
} | ||
|
||
function test_createVault() public { | ||
vm.prank(testUser); | ||
vm.expectEmit(false, false, false, false); | ||
emit VaultCreated(makeAddr("some address"), testUser); | ||
StakeVault vault = vaultFactory.createVault(); | ||
assertEq(vault.owner(), testUser); | ||
assertEq(address(vault.stakedToken()), address(stakedToken)); | ||
} | ||
} |