-
Notifications
You must be signed in to change notification settings - Fork 38
/
WeightedPool.t.sol
176 lines (144 loc) · 7.16 KB
/
WeightedPool.t.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity ^0.8.24;
import "forge-std/Test.sol";
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { IRateProvider } from "@balancer-labs/v3-interfaces/contracts/solidity-utils/helpers/IRateProvider.sol";
import { TokenConfig, PoolRoleAccounts } from "@balancer-labs/v3-interfaces/contracts/vault/VaultTypes.sol";
import { IVaultErrors } from "@balancer-labs/v3-interfaces/contracts/vault/IVaultErrors.sol";
import { IPoolInfo } from "@balancer-labs/v3-interfaces/contracts/pool-utils/IPoolInfo.sol";
import { IBasePool } from "@balancer-labs/v3-interfaces/contracts/vault/IBasePool.sol";
import { IVault } from "@balancer-labs/v3-interfaces/contracts/vault/IVault.sol";
import {
IWeightedPool,
WeightedPoolImmutableData,
WeightedPoolDynamicData
} from "@balancer-labs/v3-interfaces/contracts/pool-weighted/IWeightedPool.sol";
import { CastingHelpers } from "@balancer-labs/v3-solidity-utils/contracts/helpers/CastingHelpers.sol";
import { InputHelpers } from "@balancer-labs/v3-solidity-utils/contracts/helpers/InputHelpers.sol";
import { ArrayHelpers } from "@balancer-labs/v3-solidity-utils/contracts/test/ArrayHelpers.sol";
import { WeightedMath } from "@balancer-labs/v3-solidity-utils/contracts/math/WeightedMath.sol";
import { BasePoolTest } from "@balancer-labs/v3-vault/test/foundry/utils/BasePoolTest.sol";
import { PoolFactoryMock } from "@balancer-labs/v3-vault/contracts/test/PoolFactoryMock.sol";
import { PoolHooksMock } from "@balancer-labs/v3-vault/contracts/test/PoolHooksMock.sol";
import { WeightedPoolFactory } from "../../contracts/WeightedPoolFactory.sol";
import { WeightedPool } from "../../contracts/WeightedPool.sol";
import { WeightedPoolContractsDeployer } from "./utils/WeightedPoolContractsDeployer.sol";
contract WeightedPoolTest is WeightedPoolContractsDeployer, BasePoolTest {
using CastingHelpers for address[];
using ArrayHelpers for *;
string constant POOL_VERSION = "Pool v1";
uint256 constant DEFAULT_SWAP_FEE = 1e16; // 1%
uint256 constant TOKEN_AMOUNT = 1e3 * 1e18;
uint256[] internal weights;
uint256 daiIdx;
uint256 usdcIdx;
function setUp() public virtual override {
expectedAddLiquidityBptAmountOut = TOKEN_AMOUNT;
tokenAmountIn = TOKEN_AMOUNT / 4;
isTestSwapFeeEnabled = false;
BasePoolTest.setUp();
(daiIdx, usdcIdx) = getSortedIndexes(address(dai), address(usdc));
poolMinSwapFeePercentage = 0.001e16; // 0.001%
poolMaxSwapFeePercentage = 10e16;
}
function createPoolFactory() internal override returns (address) {
return address(deployWeightedPoolFactory(IVault(address(vault)), 365 days, "Factory v1", POOL_VERSION));
}
function createPool() internal override returns (address newPool, bytes memory poolArgs) {
string memory name = "ERC20 Pool";
string memory symbol = "ERC20POOL";
IERC20[] memory sortedTokens = InputHelpers.sortTokens(
[address(dai), address(usdc)].toMemoryArray().asIERC20()
);
for (uint256 i = 0; i < sortedTokens.length; i++) {
poolTokens.push(sortedTokens[i]);
tokenAmounts.push(TOKEN_AMOUNT);
}
weights = [uint256(50e16), uint256(50e16)].toMemoryArray();
PoolRoleAccounts memory roleAccounts;
// Allow pools created by `factory` to use poolHooksMock hooks
PoolHooksMock(poolHooksContract).allowFactory(poolFactory);
newPool = WeightedPoolFactory(poolFactory).create(
name,
symbol,
vault.buildTokenConfig(sortedTokens),
weights,
roleAccounts,
DEFAULT_SWAP_FEE,
poolHooksContract,
false, // Do not enable donations
false, // Do not disable unbalanced add/remove liquidity
ZERO_BYTES32
);
// poolArgs is used to check pool deployment address with create2.
poolArgs = abi.encode(
WeightedPool.NewPoolParams({
name: name,
symbol: symbol,
numTokens: sortedTokens.length,
normalizedWeights: weights,
version: POOL_VERSION
}),
vault
);
}
function initPool() internal override {
vm.startPrank(lp);
bptAmountOut = _initPool(
pool,
tokenAmounts,
// Account for the precision loss
expectedAddLiquidityBptAmountOut - DELTA
);
vm.stopPrank();
}
function testGetBptRate() public {
vm.expectRevert(WeightedPool.WeightedPoolBptRateUnsupported.selector);
IRateProvider(pool).getRate();
}
function testFailSwapFeeTooLow() public {
TokenConfig[] memory tokenConfigs = new TokenConfig[](2);
tokenConfigs[daiIdx].token = IERC20(dai);
tokenConfigs[usdcIdx].token = IERC20(usdc);
PoolRoleAccounts memory roleAccounts;
address lowFeeWeightedPool = WeightedPoolFactory(poolFactory).create(
"ERC20 Pool",
"ERC20POOL",
tokenConfigs,
[uint256(50e16), uint256(50e16)].toMemoryArray(),
roleAccounts,
IBasePool(pool).getMinimumSwapFeePercentage() - 1, // Swap fee too low
poolHooksContract,
false, // Do not enable donations
false, // Do not disable unbalanced add/remove liquidity
"Low fee pool"
);
vm.expectRevert(IVaultErrors.SwapFeePercentageTooLow.selector);
PoolFactoryMock(poolFactory).registerTestPool(lowFeeWeightedPool, tokenConfigs);
}
function testGetWeightedPoolImmutableData() public view {
WeightedPoolImmutableData memory data = IWeightedPool(pool).getWeightedPoolImmutableData();
(uint256[] memory scalingFactors, ) = vault.getPoolTokenRates(pool);
IERC20[] memory tokens = IPoolInfo(pool).getTokens();
for (uint256 i = 0; i < tokens.length; ++i) {
assertEq(address(data.tokens[i]), address(tokens[i]), "Token mismatch");
assertEq(data.decimalScalingFactors[i], scalingFactors[i], "Decimal scaling factors mismatch");
assertEq(data.normalizedWeights[i], uint256(50e16), "Weight mismatch");
}
}
function testGetWeightedPoolDynamicData() public view {
WeightedPoolDynamicData memory data = IWeightedPool(pool).getWeightedPoolDynamicData();
(, uint256[] memory tokenRates) = vault.getPoolTokenRates(pool);
IERC20[] memory tokens = IPoolInfo(pool).getTokens();
uint256 totalSupply = IERC20(pool).totalSupply();
assertTrue(data.isPoolInitialized, "Pool not initialized");
assertFalse(data.isPoolPaused, "Pool paused");
assertFalse(data.isPoolInRecoveryMode, "Pool in Recovery Mode");
assertEq(data.totalSupply, totalSupply, "Total supply mismatch");
assertEq(data.staticSwapFeePercentage, DEFAULT_SWAP_FEE, "Swap fee mismatch");
for (uint256 i = 0; i < tokens.length; ++i) {
assertEq(data.balancesLiveScaled18[i], DEFAULT_AMOUNT, "Live balance mismatch");
assertEq(data.tokenRates[i], tokenRates[i], "Token rate mismatch");
}
}
}