|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | + |
| 3 | +pragma solidity 0.6.12; |
| 4 | + |
| 5 | +import "./interfaces/ISwap.sol"; |
| 6 | +import "./MathUtils.sol"; |
| 7 | +import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; |
| 8 | + |
| 9 | +interface IERC20Decimals is IERC20 { |
| 10 | + function decimals() external view returns (uint8); |
| 11 | +} |
| 12 | + |
| 13 | +contract SwapCalculator { |
| 14 | + using SafeMath for uint256; |
| 15 | + using MathUtils for uint256; |
| 16 | + |
| 17 | + // Struct storing variables used in calculations in the |
| 18 | + // {add,remove}Liquidity functions to avoid stack too deep errors |
| 19 | + struct ManageLiquidityInfo { |
| 20 | + uint256 d0; |
| 21 | + uint256 d1; |
| 22 | + uint256 preciseA; |
| 23 | + uint256 totalSupply; |
| 24 | + uint256[] balances; |
| 25 | + uint256[] multipliers; |
| 26 | + } |
| 27 | + |
| 28 | + ISwap public immutable pool; |
| 29 | + IERC20 public immutable lpToken; |
| 30 | + uint256 public immutable numTokens; |
| 31 | + uint256 public swapFee; |
| 32 | + |
| 33 | + IERC20[] internal poolTokens; |
| 34 | + uint256[] private tokenPrecisionMultipliers; |
| 35 | + |
| 36 | + uint8 private constant POOL_PRECISION_DECIMALS = 18; |
| 37 | + uint256 private constant A_PRECISION = 100; |
| 38 | + uint256 private constant FEE_DENOMINATOR = 10**10; |
| 39 | + |
| 40 | + constructor(ISwap _pool) public { |
| 41 | + pool = _pool; |
| 42 | + (, , , , uint256 _swapFee, , address _lpToken) = _pool.swapStorage(); |
| 43 | + lpToken = IERC20(_lpToken); |
| 44 | + numTokens = _setPoolTokens(_pool); |
| 45 | + swapFee = _swapFee; |
| 46 | + } |
| 47 | + |
| 48 | + function updateSwapFee() external { |
| 49 | + (, , , , uint256 _swapFee, , ) = pool.swapStorage(); |
| 50 | + swapFee = _swapFee; |
| 51 | + } |
| 52 | + |
| 53 | + function calculateAddLiquidity(uint256[] memory _amounts) |
| 54 | + public |
| 55 | + view |
| 56 | + returns (uint256) |
| 57 | + { |
| 58 | + require( |
| 59 | + _amounts.length == numTokens, |
| 60 | + "Amounts must match pooled tokens" |
| 61 | + ); |
| 62 | + uint256 _numTokens = numTokens; |
| 63 | + |
| 64 | + ManageLiquidityInfo memory v = ManageLiquidityInfo( |
| 65 | + 0, |
| 66 | + 0, |
| 67 | + pool.getAPrecise(), |
| 68 | + lpToken.totalSupply(), |
| 69 | + new uint256[](_numTokens), |
| 70 | + tokenPrecisionMultipliers |
| 71 | + ); |
| 72 | + |
| 73 | + uint256[] memory newBalances = new uint256[](_numTokens); |
| 74 | + |
| 75 | + for (uint8 i = 0; i < _numTokens; i++) { |
| 76 | + v.balances[i] = ISwap(pool).getTokenBalance(i); |
| 77 | + newBalances[i] = v.balances[i].add(_amounts[i]); |
| 78 | + } |
| 79 | + |
| 80 | + if (v.totalSupply != 0) { |
| 81 | + v.d0 = _getD(_xp(v.balances, v.multipliers), v.preciseA); |
| 82 | + } else { |
| 83 | + // pool is empty => all amounts must be >0 |
| 84 | + for (uint8 i = 0; i < _numTokens; i++) { |
| 85 | + require(_amounts[i] > 0, "Must supply all tokens in pool"); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + // invariant after change |
| 90 | + v.d1 = _getD(_xp(newBalances, v.multipliers), v.preciseA); |
| 91 | + require(v.d1 > v.d0, "D should increase"); |
| 92 | + |
| 93 | + if (v.totalSupply == 0) { |
| 94 | + return v.d1; |
| 95 | + } else { |
| 96 | + uint256 feePerToken = _feePerToken(); |
| 97 | + for (uint8 i = 0; i < _numTokens; i++) { |
| 98 | + uint256 idealBalance = v.d1.mul(v.balances[i]).div(v.d0); |
| 99 | + uint256 fees = feePerToken |
| 100 | + .mul(idealBalance.difference(newBalances[i])) |
| 101 | + .div(FEE_DENOMINATOR); |
| 102 | + newBalances[i] = newBalances[i].sub(fees); |
| 103 | + } |
| 104 | + v.d1 = _getD(_xp(newBalances, v.multipliers), v.preciseA); |
| 105 | + return v.d1.sub(v.d0).mul(v.totalSupply).div(v.d0); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + function _setPoolTokens(ISwap _pool) internal returns (uint256) { |
| 110 | + for (uint8 i = 0; true; i++) { |
| 111 | + try _pool.getToken(i) returns (IERC20 token) { |
| 112 | + _addPoolToken(token, i); |
| 113 | + } catch { |
| 114 | + break; |
| 115 | + } |
| 116 | + } |
| 117 | + return tokenPrecisionMultipliers.length; |
| 118 | + } |
| 119 | + |
| 120 | + /// @dev add custom logic in child contracts, |
| 121 | + /// if they need to store more info on the tokens |
| 122 | + function _addPoolToken(IERC20 token, uint8) internal virtual { |
| 123 | + IERC20Decimals _token = IERC20Decimals(address(token)); |
| 124 | + tokenPrecisionMultipliers.push( |
| 125 | + 10**uint256(POOL_PRECISION_DECIMALS - _token.decimals()) |
| 126 | + ); |
| 127 | + poolTokens.push(token); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * @notice Get fee applied to each token when adding |
| 132 | + * or removing assets weighted differently from the pool |
| 133 | + */ |
| 134 | + function _feePerToken() internal view returns (uint256) { |
| 135 | + return swapFee.mul(numTokens).div(numTokens.sub(1).mul(4)); |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * @notice Get pool balances adjusted, as if all tokens had 18 decimals |
| 140 | + */ |
| 141 | + function _xp( |
| 142 | + uint256[] memory balances, |
| 143 | + uint256[] memory precisionMultipliers |
| 144 | + ) internal pure returns (uint256[] memory) { |
| 145 | + uint256 _numTokens = balances.length; |
| 146 | + require( |
| 147 | + _numTokens == precisionMultipliers.length, |
| 148 | + "Balances must match multipliers" |
| 149 | + ); |
| 150 | + uint256[] memory xp = new uint256[](_numTokens); |
| 151 | + for (uint256 i = 0; i < _numTokens; i++) { |
| 152 | + xp[i] = balances[i].mul(precisionMultipliers[i]); |
| 153 | + } |
| 154 | + return xp; |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * @notice Get D: pool invariant |
| 159 | + */ |
| 160 | + function _getD(uint256[] memory xp, uint256 a) |
| 161 | + internal |
| 162 | + pure |
| 163 | + returns (uint256) |
| 164 | + { |
| 165 | + uint256 _numTokens = xp.length; |
| 166 | + uint256 s; |
| 167 | + for (uint256 i = 0; i < _numTokens; i++) { |
| 168 | + s = s.add(xp[i]); |
| 169 | + } |
| 170 | + if (s == 0) { |
| 171 | + return 0; |
| 172 | + } |
| 173 | + |
| 174 | + uint256 prevD; |
| 175 | + uint256 d = s; |
| 176 | + uint256 nA = a.mul(_numTokens); |
| 177 | + |
| 178 | + for (uint256 i = 0; i < 256; i++) { |
| 179 | + uint256 dP = d; |
| 180 | + for (uint256 j = 0; j < _numTokens; j++) { |
| 181 | + dP = dP.mul(d).div(xp[j].mul(_numTokens)); |
| 182 | + // If we were to protect the division loss we would have to keep the denominator separate |
| 183 | + // and divide at the end. However this leads to overflow with large numTokens or/and D. |
| 184 | + // dP = dP * D * D * D * ... overflow! |
| 185 | + } |
| 186 | + prevD = d; |
| 187 | + d = nA.mul(s).div(A_PRECISION).add(dP.mul(_numTokens)).mul(d).div( |
| 188 | + nA.sub(A_PRECISION).mul(d).div(A_PRECISION).add( |
| 189 | + _numTokens.add(1).mul(dP) |
| 190 | + ) |
| 191 | + ); |
| 192 | + if (d.within1(prevD)) { |
| 193 | + return d; |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + // Convergence should occur in 4 loops or less. If this is reached, there may be something wrong |
| 198 | + // with the pool. If this were to occur repeatedly, LPs should withdraw via `removeLiquidity()` |
| 199 | + // function which does not rely on D. |
| 200 | + revert("D does not converge"); |
| 201 | + } |
| 202 | +} |
0 commit comments