forked from balancer/balancer-v3-monorepo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WeightedPool.sol
198 lines (167 loc) · 8.23 KB
/
WeightedPool.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity ^0.8.24;
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { ERC165 } from "@openzeppelin/contracts/utils/introspection/ERC165.sol";
import {
IWeightedPool,
WeightedPoolDynamicData,
WeightedPoolImmutableData
} from "@balancer-labs/v3-interfaces/contracts/pool-weighted/IWeightedPool.sol";
import { ISwapFeePercentageBounds } from "@balancer-labs/v3-interfaces/contracts/vault/ISwapFeePercentageBounds.sol";
import { IVault } from "@balancer-labs/v3-interfaces/contracts/vault/IVault.sol";
import { IVaultErrors } from "@balancer-labs/v3-interfaces/contracts/vault/IVaultErrors.sol";
import { SwapKind } from "@balancer-labs/v3-interfaces/contracts/vault/VaultTypes.sol";
import { IBasePool } from "@balancer-labs/v3-interfaces/contracts/vault/IBasePool.sol";
import { BalancerPoolToken } from "@balancer-labs/v3-vault/contracts/BalancerPoolToken.sol";
import { PoolInfo } from "@balancer-labs/v3-pool-utils/contracts/PoolInfo.sol";
import { FixedPoint } from "@balancer-labs/v3-solidity-utils/contracts/math/FixedPoint.sol";
import { WeightedMath } from "@balancer-labs/v3-solidity-utils/contracts/math/WeightedMath.sol";
import { InputHelpers } from "@balancer-labs/v3-solidity-utils/contracts/helpers/InputHelpers.sol";
import { Version } from "@balancer-labs/v3-solidity-utils/contracts/helpers/Version.sol";
/// @notice Basic Weighted Pool with immutable weights.
contract WeightedPool is IWeightedPool, BalancerPoolToken, PoolInfo, Version {
// Fees are 18-decimal, floating point values, which will be stored in the Vault using 24 bits.
// This means they have 0.00001% resolution (i.e., any non-zero bits < 1e11 will cause precision loss).
// Minimum values help make the math well-behaved (i.e., the swap fee should overwhelm any rounding error).
// Maximum values protect users by preventing permissioned actors from setting excessively high swap fees.
uint256 private constant _MIN_SWAP_FEE_PERCENTAGE = 1e12; // 0.0001%
uint256 private constant _MAX_SWAP_FEE_PERCENTAGE = 0.1e18; // 10%
uint256 private immutable _totalTokens;
uint256 internal immutable _normalizedWeight0;
uint256 internal immutable _normalizedWeight1;
uint256 internal immutable _normalizedWeight2;
uint256 internal immutable _normalizedWeight3;
struct NewPoolParams {
string name;
string symbol;
uint256 numTokens;
uint256[] normalizedWeights;
string version;
}
/// @dev Indicates that one of the pool tokens' weight is below the minimum allowed.
error MinWeight();
/// @dev Indicates that the sum of the pool tokens' weights is not FP 1.
error NormalizedWeightInvariant();
constructor(
NewPoolParams memory params,
IVault vault
) BalancerPoolToken(vault, params.name, params.symbol) PoolInfo(vault) Version(params.version) {
uint256 numTokens = params.numTokens;
InputHelpers.ensureInputLengthMatch(numTokens, params.normalizedWeights.length);
_totalTokens = numTokens;
// Ensure each normalized weight is above the minimum
uint256 normalizedSum = 0;
for (uint8 i = 0; i < numTokens; ++i) {
uint256 normalizedWeight = params.normalizedWeights[i];
if (normalizedWeight < WeightedMath._MIN_WEIGHT) {
revert MinWeight();
}
normalizedSum = normalizedSum + normalizedWeight;
}
// Ensure that the normalized weights sum to ONE
if (normalizedSum != FixedPoint.ONE) {
revert NormalizedWeightInvariant();
}
// Immutable variables cannot be initialized inside an if statement, so we must do conditional assignments
_normalizedWeight0 = params.normalizedWeights[0];
_normalizedWeight1 = params.normalizedWeights[1];
_normalizedWeight2 = numTokens > 2 ? params.normalizedWeights[2] : 0;
_normalizedWeight3 = numTokens > 3 ? params.normalizedWeights[3] : 0;
}
/// @inheritdoc IBasePool
function computeInvariant(uint256[] memory balancesLiveScaled18) public view returns (uint256) {
return WeightedMath.computeInvariant(_getNormalizedWeights(), balancesLiveScaled18);
}
/// @inheritdoc IBasePool
function computeBalance(
uint256[] memory balancesLiveScaled18,
uint256 tokenInIndex,
uint256 invariantRatio
) external view returns (uint256 newBalance) {
return
WeightedMath.computeBalanceOutGivenInvariant(
balancesLiveScaled18[tokenInIndex],
_getNormalizedWeights()[tokenInIndex],
invariantRatio
);
}
/// @inheritdoc IWeightedPool
function getNormalizedWeights() external view returns (uint256[] memory) {
return _getNormalizedWeights();
}
/// @inheritdoc IBasePool
function onSwap(IBasePool.PoolSwapParams memory request) public view onlyVault returns (uint256) {
uint256 balanceTokenInScaled18 = request.balancesScaled18[request.indexIn];
uint256 balanceTokenOutScaled18 = request.balancesScaled18[request.indexOut];
if (request.kind == SwapKind.EXACT_IN) {
uint256 amountOutScaled18 = WeightedMath.computeOutGivenExactIn(
balanceTokenInScaled18,
_getNormalizedWeight(request.indexIn),
balanceTokenOutScaled18,
_getNormalizedWeight(request.indexOut),
request.amountGivenScaled18
);
return amountOutScaled18;
} else {
uint256 amountInScaled18 = WeightedMath.computeInGivenExactOut(
balanceTokenInScaled18,
_getNormalizedWeight(request.indexIn),
balanceTokenOutScaled18,
_getNormalizedWeight(request.indexOut),
request.amountGivenScaled18
);
// Fees are added after scaling happens, to reduce the complexity of the rounding direction analysis.
return amountInScaled18;
}
}
function _getNormalizedWeight(uint256 tokenIndex) internal view virtual returns (uint256) {
// prettier-ignore
if (tokenIndex == 0) { return _normalizedWeight0; }
else if (tokenIndex == 1) { return _normalizedWeight1; }
else if (tokenIndex == 2) { return _normalizedWeight2; }
else if (tokenIndex == 3) { return _normalizedWeight3; }
else {
revert IVaultErrors.InvalidToken();
}
}
function _getNormalizedWeights() internal view virtual returns (uint256[] memory) {
uint256 numTokens = _totalTokens;
uint256[] memory normalizedWeights = new uint256[](numTokens);
// prettier-ignore
normalizedWeights[0] = _normalizedWeight0;
normalizedWeights[1] = _normalizedWeight1;
if (numTokens > 2) {
normalizedWeights[2] = _normalizedWeight2;
} else {
return normalizedWeights;
}
if (numTokens > 3) {
normalizedWeights[3] = _normalizedWeight3;
} else {
return normalizedWeights;
}
return normalizedWeights;
}
/// @inheritdoc ISwapFeePercentageBounds
function getMinimumSwapFeePercentage() external pure returns (uint256) {
return _MIN_SWAP_FEE_PERCENTAGE;
}
/// @inheritdoc ISwapFeePercentageBounds
function getMaximumSwapFeePercentage() external pure returns (uint256) {
return _MAX_SWAP_FEE_PERCENTAGE;
}
/// @inheritdoc IWeightedPool
function getWeightedPoolDynamicData() external view returns (WeightedPoolDynamicData memory data) {
data.liveBalances = _vault.getCurrentLiveBalances(address(this));
(, data.tokenRates) = _vault.getPoolTokenRates(address(this));
data.staticSwapFeePercentage = _vault.getStaticSwapFeePercentage((address(this)));
data.totalSupply = totalSupply();
data.bptRate = getRate();
}
/// @inheritdoc IWeightedPool
function getWeightedPoolImmutableData() external view returns (WeightedPoolImmutableData memory data) {
data.tokens = _vault.getPoolTokens(address(this));
(data.decimalScalingFactors, ) = _vault.getPoolTokenRates(address(this));
data.normalizedWeights = _getNormalizedWeights();
}
}