Skip to content

Commit

Permalink
Merge pull request #25 from GenerationSoftware/gen-427-c4-issue-416
Browse files Browse the repository at this point in the history
Added CREATE2 to VaultFactory
  • Loading branch information
asselstine authored Aug 17, 2023
2 parents 8985bea + 1abbe58 commit 9ac530b
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 11 deletions.
37 changes: 26 additions & 11 deletions src/VaultFactory.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;

import { Create2 } from "openzeppelin/utils/Create2.sol";
import { IERC20, IERC4626 } from "openzeppelin/token/ERC20/extensions/ERC4626.sol";

import { ILiquidationPair } from "pt-v5-liquidator-interfaces/ILiquidationPair.sol";
Expand Down Expand Up @@ -35,6 +36,11 @@ contract VaultFactory {
*/
mapping(Vault => bool) public deployedVaults;

/**
* @notice Mapping to store deployer nonces for CREATE2
*/
mapping(address => uint) public deployerNonces;

/* ============ External Functions ============ */

/**
Expand Down Expand Up @@ -64,19 +70,28 @@ contract VaultFactory {
uint256 _yieldFeePercentage,
address _owner
) external returns (address) {
Vault _vault = new Vault(
_asset,
_name,
_symbol,
_twabController,
_yieldVault,
_prizePool,
_claimer,
_yieldFeeRecipient,
_yieldFeePercentage,
_owner
bytes memory bytecode = abi.encodePacked(
type(Vault).creationCode,
abi.encode(
_asset,
_name,
_symbol,
_twabController,
_yieldVault,
_prizePool,
_claimer,
_yieldFeeRecipient,
_yieldFeePercentage,
_owner
)
);

Vault _vault = Vault(Create2.deploy(
0,
keccak256(abi.encode(msg.sender, deployerNonces[msg.sender]++)),
bytecode
));

allVaults.push(_vault);
deployedVaults[_vault] = true;

Expand Down
32 changes: 32 additions & 0 deletions test/unit/VaultFactory.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,39 @@ contract VaultFactoryTest is Test {
address(this)
);

assertEq(address(Vault(_vault).asset()), address(asset));

assertEq(vaultFactory.totalVaults(), 1);
assertTrue(vaultFactory.deployedVaults(Vault(_vault)));
}

function testDeployVault_secondDeployShouldHaveDiffAddress() public {
Vault _vault1 = Vault(vaultFactory.deployVault(
asset,
name,
symbol,
twabController,
yieldVault,
prizePool,
claimer,
address(this),
0,
address(this)
));

Vault _vault2 = Vault(vaultFactory.deployVault(
asset,
name,
symbol,
twabController,
yieldVault,
prizePool,
claimer,
address(this),
0,
address(this)
));

assertNotEq(address(_vault1), address(_vault2));
}
}

0 comments on commit 9ac530b

Please sign in to comment.