Skip to content

Commit

Permalink
Add createFactory function in BaseVaultTest (#1182)
Browse files Browse the repository at this point in the history
  • Loading branch information
elshan-eth authored Dec 23, 2024
1 parent 080bdd7 commit 822c1cf
Show file tree
Hide file tree
Showing 56 changed files with 339 additions and 263 deletions.
31 changes: 19 additions & 12 deletions pkg/pool-hooks/test/foundry/DirectionalFeeHookExample.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
import { PoolMock } from "@balancer-labs/v3-vault/contracts/test/PoolMock.sol";

import { BaseVaultTest } from "@balancer-labs/v3-vault/test/foundry/utils/BaseVaultTest.sol";
import { PoolFactoryMock } from "@balancer-labs/v3-vault/contracts/test/PoolFactoryMock.sol";

import { DirectionalFeeHookExample } from "../../contracts/DirectionalFeeHookExample.sol";

Expand All @@ -42,9 +43,8 @@ contract DirectionalHookExampleTest is StablePoolContractsDeployer, BaseVaultTes
uint256 internal daiIdx;
uint256 internal usdcIdx;

StablePoolFactory internal stablePoolFactory;

address internal directionalFeeHook;
PoolFactoryMock internal poolFactoryMock;

uint256 internal constant DEFAULT_AMP_FACTOR = 200;

Expand All @@ -54,14 +54,19 @@ contract DirectionalHookExampleTest is StablePoolContractsDeployer, BaseVaultTes
super.setUp();

(daiIdx, usdcIdx) = getSortedIndexes(address(dai), address(usdc));

poolFactoryMock = PoolFactoryMock(address(vault.getPoolFactoryMock()));
}

function createPoolFactory() internal override returns (address) {
return address(deployStablePoolFactory(IVault(address(vault)), 365 days, "Factory v1", "Pool v1"));
}

function createHook() internal override returns (address) {
// Create the factory here, because it needs to be deployed after the Vault, but before the hook contract.
stablePoolFactory = deployStablePoolFactory(IVault(address(vault)), 365 days, "Factory v1", "Pool v1");
// lp will be the owner of the hook. Only LP is able to set hook fee percentages.
vm.prank(lp);
directionalFeeHook = address(new DirectionalFeeHookExample(IVault(address(vault)), address(stablePoolFactory)));
directionalFeeHook = address(new DirectionalFeeHookExample(IVault(address(vault)), poolFactory));
vm.label(directionalFeeHook, "Directional Fee Hook");
return directionalFeeHook;
}
Expand All @@ -76,14 +81,10 @@ contract DirectionalHookExampleTest is StablePoolContractsDeployer, BaseVaultTes
PoolRoleAccounts memory roleAccounts;

vm.expectEmit(true, true, false, false);
emit DirectionalFeeHookExample.DirectionalFeeHookExampleRegistered(
directionalFeeHook,
address(stablePoolFactory),
address(0)
);
emit DirectionalFeeHookExample.DirectionalFeeHookExampleRegistered(directionalFeeHook, poolFactory, address(0));

newPool = address(
stablePoolFactory.create(
StablePoolFactory(poolFactory).create(
name,
symbol,
vault.buildTokenConfig(tokens.asIERC20()),
Expand Down Expand Up @@ -126,7 +127,7 @@ contract DirectionalHookExampleTest is StablePoolContractsDeployer, BaseVaultTes
IVaultErrors.HookRegistrationFailed.selector,
poolHooksContract,
directionalFeePool,
address(factoryMock)
address(poolFactoryMock)
)
);
_registerPoolWithHook(directionalFeePool, tokenConfig);
Expand Down Expand Up @@ -345,6 +346,12 @@ contract DirectionalHookExampleTest is StablePoolContractsDeployer, BaseVaultTes

LiquidityManagement memory liquidityManagement;

factoryMock.registerPool(directionalFeePool, tokenConfig, roleAccounts, poolHooksContract, liquidityManagement);
poolFactoryMock.registerPool(
directionalFeePool,
tokenConfig,
roleAccounts,
poolHooksContract,
liquidityManagement
);
}
}
9 changes: 7 additions & 2 deletions pkg/pool-hooks/test/foundry/ExitFeeHookExample.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { ArrayHelpers } from "@balancer-labs/v3-solidity-utils/contracts/test/Ar
import { FixedPoint } from "@balancer-labs/v3-solidity-utils/contracts/math/FixedPoint.sol";

import { BaseVaultTest } from "@balancer-labs/v3-vault/test/foundry/utils/BaseVaultTest.sol";
import { PoolFactoryMock } from "@balancer-labs/v3-vault/contracts/test/PoolFactoryMock.sol";
import { PoolMock } from "@balancer-labs/v3-vault/contracts/test/PoolMock.sol";

import { ExitFeeHookExample } from "../../contracts/ExitFeeHookExample.sol";
Expand All @@ -29,6 +30,8 @@ contract ExitFeeHookExampleTest is BaseVaultTest {
using FixedPoint for uint256;
using ArrayHelpers for *;

PoolFactoryMock poolFactoryMock;

uint256 internal daiIdx;
uint256 internal usdcIdx;

Expand All @@ -39,6 +42,8 @@ contract ExitFeeHookExampleTest is BaseVaultTest {
super.setUp();

(daiIdx, usdcIdx) = getSortedIndexes(address(dai), address(usdc));

poolFactoryMock = PoolFactoryMock(address(vault.getPoolFactoryMock()));
}

function createHook() internal override returns (address) {
Expand Down Expand Up @@ -70,7 +75,7 @@ contract ExitFeeHookExampleTest is BaseVaultTest {
vm.expectEmit();
emit ExitFeeHookExample.ExitFeeHookExampleRegistered(poolHooksContract, newPool);

factoryMock.registerPool(
PoolFactoryMock(poolFactory).registerPool(
newPool,
vault.buildTokenConfig(tokens.asIERC20()),
roleAccounts,
Expand Down Expand Up @@ -200,6 +205,6 @@ contract ExitFeeHookExampleTest is BaseVaultTest {
liquidityManagement.disableUnbalancedLiquidity = true;
liquidityManagement.enableDonation = enableDonation;

factoryMock.registerPool(exitFeePool, tokenConfig, roleAccounts, poolHooksContract, liquidityManagement);
poolFactoryMock.registerPool(exitFeePool, tokenConfig, roleAccounts, poolHooksContract, liquidityManagement);
}
}
13 changes: 8 additions & 5 deletions pkg/pool-hooks/test/foundry/ExitFeeHookExampleStablePool.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,28 @@ contract ExitFeeHookExampleStablePoolTest is StablePoolContractsDeployer, ExitFe
using ArrayHelpers for *;
using FixedPoint for uint256;

string constant POOL_VERSION = "Pool v1";

// The minimum swap fee for a Stable Pool is 0.0001%.
uint256 internal constant MIN_STABLE_SWAP_FEE = 1e12;

StablePoolFactory internal stablePoolFactory;
uint256 internal constant DEFAULT_AMP_FACTOR = 200;

function createPoolFactory() internal override returns (address) {
return address(deployStablePoolFactory(IVault(address(vault)), 365 days, "Factory v1", POOL_VERSION));
}

// Overrides pool creation to set liquidityManagement (disables unbalanced liquidity and enables donation).
function _createPool(
address[] memory tokens,
string memory label
) internal override returns (address newPool, bytes memory poolArgs) {
string memory name = "Stable Pool Test";
string memory symbol = "STABLE-TEST";
string memory poolVersion = "Pool v1";

stablePoolFactory = deployStablePoolFactory(IVault(address(vault)), 365 days, "Factory v1", poolVersion);
PoolRoleAccounts memory roleAccounts;

newPool = stablePoolFactory.create(
newPool = StablePoolFactory(poolFactory).create(
name,
symbol,
vault.buildTokenConfig(tokens.asIERC20()),
Expand All @@ -67,7 +70,7 @@ contract ExitFeeHookExampleStablePoolTest is StablePoolContractsDeployer, ExitFe
name: name,
symbol: symbol,
amplificationParameter: DEFAULT_AMP_FACTOR,
version: poolVersion
version: POOL_VERSION
}),
vault
);
Expand Down
13 changes: 8 additions & 5 deletions pkg/pool-hooks/test/foundry/ExitFeeHookExampleWeightedPool.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,30 @@ contract ExitFeeHookExampleWeightedPoolTest is WeightedPoolContractsDeployer, Ex
using ArrayHelpers for *;
using FixedPoint for uint256;

string constant POOL_VERSION = "Pool v1";

// The minimum swap fee for a Weighted Pool is 0.001%.
uint256 internal constant MIN_WEIGHTED_SWAP_FEE = 0.001e16;

WeightedPoolFactory internal weightedPoolFactory;
uint256[] internal weights;

function createPoolFactory() internal override returns (address) {
return address(deployWeightedPoolFactory(IVault(address(vault)), 365 days, "Factory v1", POOL_VERSION));
}

// Overrides pool creation to set liquidityManagement (disables unbalanced liquidity and enables donation).
function _createPool(
address[] memory tokens,
string memory label
) internal override returns (address newPool, bytes memory poolArgs) {
string memory name = "Weighted Pool Test";
string memory symbol = "WEIGHTED-TEST";
string memory poolVersion = "Pool v1";

weightedPoolFactory = deployWeightedPoolFactory(IVault(address(vault)), 365 days, "Factory v1", poolVersion);
PoolRoleAccounts memory roleAccounts;

weights = [uint256(50e16), uint256(50e16)].toMemoryArray();

newPool = weightedPoolFactory.create(
newPool = WeightedPoolFactory(poolFactory).create(
name,
symbol,
vault.buildTokenConfig(tokens.asIERC20()),
Expand All @@ -69,7 +72,7 @@ contract ExitFeeHookExampleWeightedPoolTest is WeightedPoolContractsDeployer, Ex
symbol: symbol,
numTokens: tokens.length,
normalizedWeights: [uint256(50e16), uint256(50e16)].toMemoryArray(),
version: poolVersion
version: POOL_VERSION
}),
vault
);
Expand Down
3 changes: 2 additions & 1 deletion pkg/pool-hooks/test/foundry/FeeTakingHookExample.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { FixedPoint } from "@balancer-labs/v3-solidity-utils/contracts/math/Fixe
import { BasePoolMath } from "@balancer-labs/v3-vault/contracts/BasePoolMath.sol";

import { BaseVaultTest } from "@balancer-labs/v3-vault/test/foundry/utils/BaseVaultTest.sol";
import { PoolFactoryMock } from "@balancer-labs/v3-vault/contracts/test/PoolFactoryMock.sol";
import { BalancerPoolToken } from "@balancer-labs/v3-vault/contracts/BalancerPoolToken.sol";
import { PoolMock } from "@balancer-labs/v3-vault/contracts/test/PoolMock.sol";

Expand Down Expand Up @@ -70,7 +71,7 @@ contract FeeTakingHookExampleTest is BaseVaultTest {
vm.expectEmit();
emit FeeTakingHookExample.FeeTakingHookExampleRegistered(poolHooksContract, newPool);

factoryMock.registerPool(
PoolFactoryMock(poolFactory).registerPool(
newPool,
vault.buildTokenConfig(tokens.asIERC20()),
roleAccounts,
Expand Down
3 changes: 2 additions & 1 deletion pkg/pool-hooks/test/foundry/LotteryHookExample.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { CastingHelpers } from "@balancer-labs/v3-solidity-utils/contracts/helpe
import { FixedPoint } from "@balancer-labs/v3-solidity-utils/contracts/math/FixedPoint.sol";

import { BaseVaultTest } from "@balancer-labs/v3-vault/test/foundry/utils/BaseVaultTest.sol";
import { PoolFactoryMock } from "@balancer-labs/v3-vault/contracts/test/PoolFactoryMock.sol";
import { PoolMock } from "@balancer-labs/v3-vault/contracts/test/PoolMock.sol";

import { LotteryHookExample } from "../../contracts/LotteryHookExample.sol";
Expand Down Expand Up @@ -69,7 +70,7 @@ contract LotteryHookExampleTest is BaseVaultTest {
vm.expectEmit();
emit LotteryHookExample.LotteryHookExampleRegistered(poolHooksContract, address(newPool));

factoryMock.registerPool(
PoolFactoryMock(poolFactory).registerPool(
newPool,
vault.buildTokenConfig(tokens.asIERC20()),
roleAccounts,
Expand Down
6 changes: 3 additions & 3 deletions pkg/pool-hooks/test/foundry/NftLiquidityPositionExample.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ contract NftLiquidityPositionExampleTest is BaseVaultTest {
vm.label(address(vaultAdmin), "vaultAdmin");
authorizer = BasicAuthorizerMock(address(vault.getAuthorizer()));
vm.label(address(authorizer), "authorizer");
factoryMock = PoolFactoryMock(address(vault.getPoolFactoryMock()));
vm.label(address(factoryMock), "factory");
poolFactory = address(PoolFactoryMock(address(vault.getPoolFactoryMock())));
vm.label(address(poolFactory), "factory");
router = deployRouterMock(IVault(address(vault)), weth, permit2);
vm.label(address(router), "router");
batchRouter = deployBatchRouterMock(IVault(address(vault)), weth, permit2);
Expand Down Expand Up @@ -131,7 +131,7 @@ contract NftLiquidityPositionExampleTest is BaseVaultTest {
liquidityManagement.disableUnbalancedLiquidity = true;
liquidityManagement.enableDonation = true;

factoryMock.registerPool(
PoolFactoryMock(poolFactory).registerPool(
newPool,
vault.buildTokenConfig(tokens.asIERC20()),
roleAccounts,
Expand Down
11 changes: 6 additions & 5 deletions pkg/pool-hooks/test/foundry/StableSurgeHook.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ contract StableSurgeHookTest is BaseVaultTest {
uint256 internal daiIdx;
uint256 internal usdcIdx;

StablePoolFactory internal stablePoolFactory;
StableSurgeHook internal stableSurgeHook;

StableSurgeMedianMathMock stableSurgeMedianMathMock = new StableSurgeMedianMathMock();
Expand All @@ -44,10 +43,12 @@ contract StableSurgeHookTest is BaseVaultTest {
(daiIdx, usdcIdx) = getSortedIndexes(address(dai), address(usdc));
}

function createHook() internal override returns (address) {
stablePoolFactory = new StablePoolFactory(IVault(address(vault)), 365 days, "Factory v1", "Pool v1");
function createPoolFactory() internal override returns (address) {
return address(new StablePoolFactory(IVault(address(vault)), 365 days, "Factory v1", "Pool v1"));
}

vm.prank(address(stablePoolFactory));
function createHook() internal override returns (address) {
vm.prank(poolFactory);
stableSurgeHook = new StableSurgeHook(
vault,
DEFAULT_MAX_SURGE_FEE_PERCENTAGE,
Expand All @@ -63,7 +64,7 @@ contract StableSurgeHookTest is BaseVaultTest {
) internal override returns (address newPool, bytes memory poolArgs) {
PoolRoleAccounts memory roleAccounts;

newPool = stablePoolFactory.create(
newPool = StablePoolFactory(poolFactory).create(
"Stable Pool",
"STABLEPOOL",
vault.buildTokenConfig(tokens.asIERC20()),
Expand Down
6 changes: 3 additions & 3 deletions pkg/pool-hooks/test/foundry/StableSurgeHookUnit.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ contract StableSurgeHookUnitTest is BaseVaultTest {
function setUp() public override {
super.setUp();

vm.prank(address(factoryMock));
vm.prank(address(poolFactory));
stableSurgeHook = new StableSurgeHook(
vault,
DEFAULT_MAX_SURGE_FEE_PERCENTAGE,
Expand All @@ -51,7 +51,7 @@ contract StableSurgeHookUnitTest is BaseVaultTest {
assertEq(stableSurgeHook.getSurgeThresholdPercentage(pool), 0, "Surge threshold percentage should be 0");

vm.expectEmit();
emit StableSurgeHook.StableSurgeHookRegistered(pool, address(factoryMock));
emit StableSurgeHook.StableSurgeHookRegistered(pool, poolFactory);
_registerPool();

assertEq(
Expand All @@ -78,7 +78,7 @@ contract StableSurgeHookUnitTest is BaseVaultTest {
LiquidityManagement memory emptyLiquidityManagement;

vm.prank(address(vault));
stableSurgeHook.onRegister(address(factoryMock), pool, new TokenConfig[](0), emptyLiquidityManagement);
stableSurgeHook.onRegister(poolFactory, pool, new TokenConfig[](0), emptyLiquidityManagement);
}

function testGetHookFlags() public view {
Expand Down
12 changes: 6 additions & 6 deletions pkg/pool-hooks/test/foundry/VeBALFeeDiscountHookExample.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ contract VeBALFeeDiscountHookExampleTest is BaseVaultTest {
// lp will be the owner of the hook. Only LP is able to set hook fee percentages.
vm.prank(lp);
address veBalFeeHook = address(
new VeBALFeeDiscountHookExample(IVault(address(vault)), address(factoryMock), address(veBAL), trustedRouter)
new VeBALFeeDiscountHookExample(IVault(address(vault)), poolFactory, address(veBAL), trustedRouter)
);
vm.label(veBalFeeHook, "VeBAL Fee Hook");
return veBalFeeHook;
Expand Down Expand Up @@ -92,27 +92,27 @@ contract VeBALFeeDiscountHookExampleTest is BaseVaultTest {
IVaultErrors.HookRegistrationFailed.selector,
poolHooksContract,
veBalFeePool,
address(factoryMock)
poolFactory
)
);
_registerPoolWithHook(veBalFeePool, tokenConfig, address(factoryMock));
_registerPoolWithHook(veBalFeePool, tokenConfig, poolFactory);
}

function testSuccessfulRegistry() public {
// Register with the allowed factory.
address veBalFeePool = factoryMock.createPool("Test Pool", "TEST");
address veBalFeePool = PoolFactoryMock(poolFactory).createPool("Test Pool", "TEST");
TokenConfig[] memory tokenConfig = vault.buildTokenConfig(
[address(dai), address(usdc)].toMemoryArray().asIERC20()
);

vm.expectEmit();
emit VeBALFeeDiscountHookExample.VeBALFeeDiscountHookExampleRegistered(
poolHooksContract,
address(factoryMock),
poolFactory,
veBalFeePool
);

_registerPoolWithHook(veBalFeePool, tokenConfig, address(factoryMock));
_registerPoolWithHook(veBalFeePool, tokenConfig, poolFactory);

HooksConfig memory hooksConfig = vault.getHooksConfig(veBalFeePool);

Expand Down
Loading

0 comments on commit 822c1cf

Please sign in to comment.