├── .editorconfig ├── .eslintignore ├── .eslintrc ├── .gitattributes ├── .github └── workflows │ └── test.yml ├── .gitignore ├── .solcover.js ├── .solhint.json ├── LICENSE.md ├── README.md ├── contracts └── FixedRateSwap.sol ├── deploy └── deploy.js ├── deployments ├── arbitrum │ ├── .chainId │ └── FixedRateSwap.json ├── bsc │ ├── .chainId │ └── FixedRateSwap.json ├── mainnet │ ├── .chainId │ └── FixedRateSwap.json ├── matic │ ├── .chainId │ └── FixedRateSwap.json └── optimistic │ ├── .chainId │ └── FixedRateSwap.json ├── hardhat.config.js ├── hardhat.networks.js ├── package.json ├── test ├── FixedRateSwap.js └── helpers │ ├── profileEVM.js │ └── utils.js └── yarn.lock /.editorconfig: -------------------------------------------------------------------------------- 1 | # Editor configuration, see https://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | charset = utf-8 6 | indent_style = space 7 | indent_size = 4 8 | insert_final_newline = true 9 | trim_trailing_whitespace = true 10 | 11 | [*.{json,yml,xml,yaml}] 12 | indent_size = 2 13 | 14 | [*.md] 15 | max_line_length = off 16 | trim_trailing_whitespace = false 17 | -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | coverage/ 3 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends" : [ 3 | "standard", 4 | "plugin:promise/recommended" 5 | ], 6 | "plugins": [ 7 | "promise" 8 | ], 9 | "env": { 10 | "browser" : true, 11 | "node" : true, 12 | "mocha" : true, 13 | "jest" : true 14 | }, 15 | "globals" : { 16 | "artifacts": false, 17 | "contract": false, 18 | "assert": false, 19 | "web3": false 20 | }, 21 | "rules": { 22 | 23 | // Strict mode 24 | "strict": [2, "global"], 25 | 26 | // Code style 27 | "indent": [2, 4], 28 | "quotes": [2, "single"], 29 | "semi": ["error", "always"], 30 | "space-before-function-paren": ["error", "always"], 31 | "no-use-before-define": 0, 32 | "no-unused-expressions": "off", 33 | "eqeqeq": [2, "smart"], 34 | "dot-notation": [2, {"allowKeywords": true, "allowPattern": ""}], 35 | "no-redeclare": [2, {"builtinGlobals": true}], 36 | "no-trailing-spaces": [2, { "skipBlankLines": true }], 37 | "eol-last": 1, 38 | "comma-spacing": [2, {"before": false, "after": true}], 39 | "camelcase": [2, {"properties": "always"}], 40 | "no-mixed-spaces-and-tabs": [2, "smart-tabs"], 41 | "comma-dangle": [1, "always-multiline"], 42 | "no-dupe-args": 2, 43 | "no-dupe-keys": 2, 44 | "no-debugger": 0, 45 | "no-undef": 2, 46 | "object-curly-spacing": [2, "always"], 47 | "max-len": [2, 200, 2], 48 | "generator-star-spacing": ["error", "before"], 49 | "promise/avoid-new": 0, 50 | "promise/always-return": 0 51 | } 52 | } -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | *.sol linguist-language=Solidity 2 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | push: 5 | branches: [ master ] 6 | pull_request: 7 | branches: [ master ] 8 | jobs: 9 | lint: 10 | runs-on: ubuntu-latest 11 | 12 | steps: 13 | - uses: actions/checkout@v2 14 | 15 | - uses: actions/setup-node@v2 16 | with: 17 | node-version: '14.x' 18 | 19 | - run: npm install -g yarn 20 | 21 | - id: yarn-cache 22 | run: echo "::set-output name=dir::$(yarn cache dir)" 23 | 24 | - uses: actions/cache@v2 25 | with: 26 | path: ${{ steps.yarn-cache.outputs.dir }} 27 | key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }} 28 | restore-keys: | 29 | ${{ runner.os }}-yarn- 30 | 31 | - run: yarn 32 | - run: yarn lint 33 | 34 | test: 35 | runs-on: ubuntu-latest 36 | 37 | steps: 38 | - uses: actions/checkout@v2 39 | 40 | - uses: actions/setup-node@v2 41 | with: 42 | node-version: '14.x' 43 | 44 | - run: npm install -g yarn 45 | 46 | - id: yarn-cache 47 | run: echo "::set-output name=dir::$(yarn cache dir)" 48 | 49 | - uses: actions/cache@v2 50 | with: 51 | path: ${{ steps.yarn-cache.outputs.dir }} 52 | key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }} 53 | restore-keys: | 54 | ${{ runner.os }}-yarn- 55 | 56 | - run: yarn 57 | - run: yarn test 58 | 59 | coverage: 60 | runs-on: ubuntu-latest 61 | steps: 62 | - uses: actions/checkout@v2 63 | 64 | - uses: actions/setup-node@v2 65 | with: 66 | node-version: 14.x 67 | 68 | - run: npm install -g yarn 69 | 70 | - id: yarn-cache 71 | run: echo "::set-output name=dir::$(yarn cache dir)" 72 | 73 | - uses: actions/cache@v2 74 | with: 75 | path: ${{ steps.yarn-cache.outputs.dir }} 76 | key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }} 77 | restore-keys: | 78 | ${{ runner.os }}-yarn- 79 | 80 | - run: yarn 81 | - run: yarn coverage 82 | 83 | - name: Coveralls 84 | uses: coverallsapp/github-action@v1.1.2 85 | with: 86 | github-token: ${{ secrets.GITHUB_TOKEN }} 87 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | artifacts 2 | cache 3 | node_modules 4 | coverage 5 | coverage.json 6 | build 7 | .coverage_contracts 8 | .coverage_artifacts 9 | .env 10 | .idea 11 | -------------------------------------------------------------------------------- /.solcover.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | skipFiles: [ 3 | 'mocks' 4 | ] 5 | } 6 | -------------------------------------------------------------------------------- /.solhint.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "solhint:recommended", 3 | "rules": { 4 | "compiler-version": ["error", "^0.8.0"], 5 | "private-vars-leading-underscore": "error", 6 | "func-visibility": ["error", { "ignoreConstructors": true }] 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 ZumZoom 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Fixed Rate Swap 2 | 3 | Simple AMM that allows trade with fixed rate and variable fee. 4 | 5 | [![Build Status](https://github.com/1inch/fixed-rate-swap/workflows/CI/badge.svg)](https://github.com/1inch/fixed-rate-swap/actions) 6 | [![Coverage Status](https://coveralls.io/repos/github/1inch/fixed-rate-swap/badge.svg?branch=master)](https://coveralls.io/github/1inch/fixed-rate-swap?branch=master) 7 | -------------------------------------------------------------------------------- /contracts/FixedRateSwap.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: MIT 2 | 3 | pragma solidity 0.8.10; 4 | 5 | import "@openzeppelin/contracts/utils/math/Math.sol"; 6 | import "@openzeppelin/contracts/utils/math/SafeCast.sol"; 7 | import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; 8 | import "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol"; 9 | import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; 10 | 11 | 12 | /** 13 | * @dev AMM that is designed for assets with stable price to each other e.g. USDC and USDT. 14 | * It utilizes constant sum price curve x + y = const but fee is variable depending on the token balances. 15 | * In most cases fee is equal to 1 bip. But when balances are at extreme ends it either lowers to 0 16 | * or increases to 20 bip. 17 | * Fee calculations are explained in more details in `getReturn` method. 18 | * Note that AMM does not support token with fees. 19 | * Note that tokens decimals are required to be the same. 20 | */ 21 | contract FixedRateSwap is ERC20 { 22 | using SafeERC20 for IERC20; 23 | using SafeCast for uint256; 24 | 25 | event Swap( 26 | address indexed trader, 27 | int256 token0Amount, 28 | int256 token1Amount 29 | ); 30 | 31 | event Deposit( 32 | address indexed user, 33 | uint256 token0Amount, 34 | uint256 token1Amount, 35 | uint256 share 36 | ); 37 | 38 | event Withdrawal( 39 | address indexed user, 40 | uint256 token0Amount, 41 | uint256 token1Amount, 42 | uint256 share 43 | ); 44 | 45 | IERC20 immutable public token0; 46 | IERC20 immutable public token1; 47 | 48 | uint8 immutable private _decimals; 49 | 50 | uint256 constant private _ONE = 1e18; 51 | uint256 constant private _C1 = 0.9999e18; 52 | uint256 constant private _C2 = 3.382712334998325432e18; 53 | uint256 constant private _C3 = 0.456807350974663119e18; 54 | uint256 constant private _THRESHOLD = 1; 55 | uint256 constant private _LOWER_BOUND_NUMERATOR = 998; 56 | uint256 constant private _UPPER_BOUND_NUMERATOR = 1002; 57 | uint256 constant private _DENOMINATOR = 1000; 58 | 59 | constructor( 60 | IERC20 _token0, 61 | IERC20 _token1, 62 | string memory name, 63 | string memory symbol, 64 | uint8 decimals_ 65 | ) 66 | ERC20(name, symbol) 67 | { 68 | token0 = _token0; 69 | token1 = _token1; 70 | _decimals = decimals_; 71 | require(IERC20Metadata(address(_token0)).decimals() == decimals_, "token0 decimals mismatch"); 72 | require(IERC20Metadata(address(_token1)).decimals() == decimals_, "token1 decimals mismatch"); 73 | } 74 | 75 | function decimals() public view override returns(uint8) { 76 | return _decimals; 77 | } 78 | 79 | /** 80 | * @notice estimates return value of the swap 81 | * @param tokenFrom token that user wants to sell 82 | * @param tokenTo token that user wants to buy 83 | * @param inputAmount amount of `tokenFrom` that user wants to sell 84 | * @return outputAmount amount of `tokenTo` that user will receive after the trade 85 | * 86 | * @dev 87 | * `rate` at point `x = inputBalance / (inputBalance + outputBalance)`: 88 | * `rate(x) = 0.9999 + (0.5817091329374359 - x * 1.2734233188154198)^17` 89 | * When balance is changed from `inputBalance` to `inputBalance + amount` we should take 90 | * integral of `rate` to calculate proper output amount: 91 | * `getReturn(x0, x1) = (integral (0.9999 + (0.5817091329374359 - x * 1.2734233188154198)^17) dx from x=x0 to x=x1) / (x1 - x0)` 92 | * `getReturn(x0, x1) = (0.9999 * x - 3.382712334998325432 * (x - 0.456807350974663119) ** 18 from x=x0 to x=x1) / (x1 - x0)` 93 | * `getReturn(x0, x1) = (0.9999 * (x1 - x0) + 3.382712334998325432 * ((x0 - 0.456807350974663119) ** 18 - (x1 - 0.456807350974663119) ** 18)) / (x1 - x0)` 94 | * C0 = 0.9999 95 | * C2 = 3.382712334998325432 96 | * C3 = 0.456807350974663119 97 | * `getReturn(x0, x1) = (C0 * (x1 - x0) + C2 * ((x0 - C3) ** 18 - (x1 - C3) ** 18)) / (x1 - x0)` 98 | */ 99 | function getReturn(IERC20 tokenFrom, IERC20 tokenTo, uint256 inputAmount) public view returns(uint256 outputAmount) { 100 | require(inputAmount > 0, "Input amount should be > 0"); 101 | uint256 fromBalance = tokenFrom.balanceOf(address(this)); 102 | uint256 toBalance = tokenTo.balanceOf(address(this)); 103 | // require is needed to be sure that _getReturn math won't overflow 104 | require(inputAmount <= toBalance, "Input amount is too big"); 105 | outputAmount = _getReturn(fromBalance, toBalance, inputAmount); 106 | } 107 | 108 | /** 109 | * @notice makes a deposit of both tokens to the AMM 110 | * @param token0Amount amount of token0 to deposit 111 | * @param token1Amount amount of token1 to deposit 112 | * @param minShare minimal required amount of LP tokens received 113 | * @return share amount of LP tokens received 114 | */ 115 | function deposit(uint256 token0Amount, uint256 token1Amount, uint256 minShare) external returns(uint256 share) { 116 | share = depositFor(token0Amount, token1Amount, msg.sender, minShare); 117 | } 118 | 119 | /** 120 | * @notice makes a deposit of both tokens to the AMM and transfers LP tokens to the specified address 121 | * @param token0Amount amount of token0 to deposit 122 | * @param token1Amount amount of token1 to deposit 123 | * @param to address that will receive tokens 124 | * @param minShare minimal required amount of LP tokens received 125 | * @return share amount of LP tokens received 126 | * 127 | * @dev fully balanced deposit happens when ratio of amounts of deposit matches ratio of balances. 128 | * To make a fair deposit when ratios do not match the contract finds the amount that is needed to swap to 129 | * equalize ratios and makes that swap virtually to capture the swap fees. Then final share is calculated from 130 | * fair deposit of virtual amounts. 131 | */ 132 | function depositFor(uint256 token0Amount, uint256 token1Amount, address to, uint256 minShare) public returns(uint256 share) { 133 | uint256 token0Balance = token0.balanceOf(address(this)); 134 | uint256 token1Balance = token1.balanceOf(address(this)); 135 | (uint256 token0VirtualAmount, uint256 token1VirtualAmount) = _getVirtualAmountsForDeposit(token0Amount, token1Amount, token0Balance, token1Balance); 136 | 137 | uint256 inputAmount = token0VirtualAmount + token1VirtualAmount; 138 | require(inputAmount > 0, "Empty deposit is not allowed"); 139 | require(to != address(this), "Deposit to this is forbidden"); 140 | // _mint also checks require(to != address(0)) 141 | 142 | uint256 _totalSupply = totalSupply(); 143 | if (_totalSupply > 0) { 144 | uint256 totalBalance = token0Balance + token1Balance + token0Amount + token1Amount - inputAmount; 145 | share = inputAmount * _totalSupply / totalBalance; 146 | } else { 147 | share = inputAmount; 148 | } 149 | 150 | require(share >= minShare, "Share is not enough"); 151 | _mint(to, share); 152 | emit Deposit(to, token0Amount, token1Amount, share); 153 | 154 | if (token0Amount > 0) { 155 | token0.safeTransferFrom(msg.sender, address(this), token0Amount); 156 | } 157 | if (token1Amount > 0) { 158 | token1.safeTransferFrom(msg.sender, address(this), token1Amount); 159 | } 160 | } 161 | 162 | /** 163 | * @notice makes a proportional withdrawal of both tokens 164 | * @param amount amount of LP tokens to burn 165 | * @param minToken0Amount minimal required amount of token0 166 | * @param minToken1Amount minimal required amount of token1 167 | * @return token0Amount amount of token0 received 168 | * @return token1Amount amount of token1 received 169 | */ 170 | function withdraw(uint256 amount, uint256 minToken0Amount, uint256 minToken1Amount) external returns(uint256 token0Amount, uint256 token1Amount) { 171 | (token0Amount, token1Amount) = withdrawFor(amount, msg.sender, minToken0Amount, minToken1Amount); 172 | } 173 | 174 | /** 175 | * @notice makes a proportional withdrawal of both tokens and transfers them to the specified address 176 | * @param amount amount of LP tokens to burn 177 | * @param to address that will receive tokens 178 | * @param minToken0Amount minimal required amount of token0 179 | * @param minToken1Amount minimal required amount of token1 180 | * @return token0Amount amount of token0 received 181 | * @return token1Amount amount of token1 received 182 | */ 183 | function withdrawFor(uint256 amount, address to, uint256 minToken0Amount, uint256 minToken1Amount) public returns(uint256 token0Amount, uint256 token1Amount) { 184 | require(amount > 0, "Empty withdrawal is not allowed"); 185 | require(to != address(this), "Withdrawal to this is forbidden"); 186 | require(to != address(0), "Withdrawal to zero is forbidden"); 187 | 188 | uint256 _totalSupply = totalSupply(); 189 | _burn(msg.sender, amount); 190 | token0Amount = token0.balanceOf(address(this)) * amount / _totalSupply; 191 | token1Amount = token1.balanceOf(address(this)) * amount / _totalSupply; 192 | _handleWithdraw(to, amount, token0Amount, token1Amount, minToken0Amount, minToken1Amount); 193 | } 194 | 195 | /** 196 | * @notice makes a withdrawal with custom ratio 197 | * @param amount amount of LP tokens to burn 198 | * @param token0Share percentage of token0 to receive with 100% equals to 1e18 199 | * @param minToken0Amount minimal required amount of token0 200 | * @param minToken1Amount minimal required amount of token1 201 | * @return token0Amount amount of token0 received 202 | * @return token1Amount amount of token1 received 203 | */ 204 | function withdrawWithRatio(uint256 amount, uint256 token0Share, uint256 minToken0Amount, uint256 minToken1Amount) external returns(uint256 token0Amount, uint256 token1Amount) { 205 | return withdrawForWithRatio(amount, msg.sender, token0Share, minToken0Amount, minToken1Amount); 206 | } 207 | 208 | /** 209 | * @notice makes a withdrawal with custom ratio and transfers tokens to the specified address 210 | * @param amount amount of LP tokens to burn 211 | * @param to address that will receive tokens 212 | * @param token0Share percentage of token0 to receive with 100% equals to 1e18 213 | * @param minToken0Amount minimal required amount of token0 214 | * @param minToken1Amount minimal required amount of token1 215 | * @return token0Amount amount of token0 received 216 | * @return token1Amount amount of token1 received 217 | * 218 | * @dev withdrawal with custom ratio is semantically equal to proportional withdrawal with extra swap afterwards to 219 | * get to the specified ratio. The contract does exactly this by making virtual proportional withdrawal and then 220 | * finds the amount needed for an extra virtual swap to achieve specified ratio. 221 | */ 222 | function withdrawForWithRatio(uint256 amount, address to, uint256 token0Share, uint256 minToken0Amount, uint256 minToken1Amount) public returns(uint256 token0Amount, uint256 token1Amount) { 223 | require(amount > 0, "Empty withdrawal is not allowed"); 224 | require(to != address(this), "Withdrawal to this is forbidden"); 225 | require(to != address(0), "Withdrawal to zero is forbidden"); 226 | require(token0Share <= _ONE, "Ratio should be in [0, 1]"); 227 | 228 | uint256 _totalSupply = totalSupply(); 229 | // burn happens before amounts calculations intentionally — to validate amount 230 | _burn(msg.sender, amount); 231 | (token0Amount, token1Amount) = _getRealAmountsForWithdraw(amount, token0Share, _totalSupply); 232 | _handleWithdraw(to, amount, token0Amount, token1Amount, minToken0Amount, minToken1Amount); 233 | } 234 | 235 | /** 236 | * @notice swaps token0 for token1 237 | * @param inputAmount amount of token0 to sell 238 | * @param minReturnAmount minimal required amount of token1 to buy 239 | * @return outputAmount amount of token1 bought 240 | */ 241 | function swap0To1(uint256 inputAmount, uint256 minReturnAmount) external returns(uint256 outputAmount) { 242 | outputAmount = _swap(token0, token1, inputAmount, msg.sender, minReturnAmount); 243 | emit Swap(msg.sender, inputAmount.toInt256(), -outputAmount.toInt256()); 244 | } 245 | 246 | /** 247 | * @notice swaps token1 for token0 248 | * @param inputAmount amount of token1 to sell 249 | * @param minReturnAmount minimal required amount of token0 to buy 250 | * @return outputAmount amount of token0 bought 251 | */ 252 | function swap1To0(uint256 inputAmount, uint256 minReturnAmount) external returns(uint256 outputAmount) { 253 | outputAmount = _swap(token1, token0, inputAmount, msg.sender, minReturnAmount); 254 | emit Swap(msg.sender, -outputAmount.toInt256(), inputAmount.toInt256()); 255 | } 256 | 257 | /** 258 | * @notice swaps token0 for token1 and transfers them to specified receiver address 259 | * @param inputAmount amount of token0 to sell 260 | * @param to address that will receive tokens 261 | * @param minReturnAmount minimal required amount of token1 to buy 262 | * @return outputAmount amount of token1 bought 263 | */ 264 | function swap0To1For(uint256 inputAmount, address to, uint256 minReturnAmount) external returns(uint256 outputAmount) { 265 | require(to != address(this), "Swap to this is forbidden"); 266 | require(to != address(0), "Swap to zero is forbidden"); 267 | 268 | outputAmount = _swap(token0, token1, inputAmount, to, minReturnAmount); 269 | emit Swap(msg.sender, inputAmount.toInt256(), -outputAmount.toInt256()); 270 | } 271 | 272 | /** 273 | * @notice swaps token1 for token0 and transfers them to specified receiver address 274 | * @param inputAmount amount of token1 to sell 275 | * @param to address that will receive tokens 276 | * @param minReturnAmount minimal required amount of token0 to buy 277 | * @return outputAmount amount of token0 bought 278 | */ 279 | function swap1To0For(uint256 inputAmount, address to, uint256 minReturnAmount) external returns(uint256 outputAmount) { 280 | require(to != address(this), "Swap to this is forbidden"); 281 | require(to != address(0), "Swap to zero is forbidden"); 282 | 283 | outputAmount = _swap(token1, token0, inputAmount, to, minReturnAmount); 284 | emit Swap(msg.sender, -outputAmount.toInt256(), inputAmount.toInt256()); 285 | } 286 | 287 | function _getVirtualAmountsForDeposit(uint256 token0Amount, uint256 token1Amount, uint256 token0Balance, uint256 token1Balance) 288 | private pure returns(uint256 token0VirtualAmount, uint256 token1VirtualAmount) 289 | { 290 | int256 shift = _checkVirtualAmountsFormula(token0Amount, token1Amount, token0Balance, token1Balance); 291 | if (shift > 0) { 292 | (token0VirtualAmount, token1VirtualAmount) = _getVirtualAmountsForDepositImpl(token0Amount, token1Amount, token0Balance, token1Balance); 293 | } else if (shift < 0) { 294 | (token1VirtualAmount, token0VirtualAmount) = _getVirtualAmountsForDepositImpl(token1Amount, token0Amount, token1Balance, token0Balance); 295 | } else { 296 | (token0VirtualAmount, token1VirtualAmount) = (token0Amount, token1Amount); 297 | } 298 | } 299 | 300 | function _getRealAmountsForWithdraw(uint256 amount, uint256 token0Share, uint256 _totalSupply) private view returns(uint256 token0RealAmount, uint256 token1RealAmount) { 301 | uint256 token0Balance = token0.balanceOf(address(this)); 302 | uint256 token1Balance = token1.balanceOf(address(this)); 303 | uint256 token0VirtualAmount = token0Balance * amount / _totalSupply; 304 | uint256 token1VirtualAmount = token1Balance * amount / _totalSupply; 305 | 306 | uint256 currentToken0Share = token0VirtualAmount * _ONE / (token0VirtualAmount + token1VirtualAmount); 307 | if (token0Share < currentToken0Share) { 308 | (token0RealAmount, token1RealAmount) = _getRealAmountsForWithdrawImpl(token0VirtualAmount, token1VirtualAmount, token0Balance - token0VirtualAmount, token1Balance - token1VirtualAmount, token0Share); 309 | } else if (token0Share > currentToken0Share) { 310 | (token1RealAmount, token0RealAmount) = _getRealAmountsForWithdrawImpl(token1VirtualAmount, token0VirtualAmount, token1Balance - token1VirtualAmount, token0Balance - token0VirtualAmount, _ONE - token0Share); 311 | } else { 312 | (token0RealAmount, token1RealAmount) = (token0VirtualAmount, token1VirtualAmount); 313 | } 314 | } 315 | 316 | function _getReturn(uint256 fromBalance, uint256 toBalance, uint256 inputAmount) private pure returns(uint256 outputAmount) { 317 | unchecked { 318 | uint256 totalBalance = fromBalance + toBalance; 319 | uint256 x0 = _ONE * fromBalance / totalBalance; 320 | uint256 x1 = _ONE * (fromBalance + inputAmount) / totalBalance; 321 | uint256 scaledInputAmount = _ONE * inputAmount; 322 | uint256 amountMultiplier = ( 323 | _C1 * scaledInputAmount / totalBalance + 324 | _C2 * _powerHelper(x0) - 325 | _C2 * _powerHelper(x1) 326 | ) * totalBalance / scaledInputAmount; 327 | outputAmount = inputAmount * Math.min(amountMultiplier, _ONE) / _ONE; 328 | } 329 | } 330 | 331 | function _handleWithdraw(address to, uint256 amount, uint256 token0Amount, uint256 token1Amount, uint256 minToken0Amount, uint256 minToken1Amount) private { 332 | require(token0Amount >= minToken0Amount, "Min token0Amount is not reached"); 333 | require(token1Amount >= minToken1Amount, "Min token1Amount is not reached"); 334 | 335 | emit Withdrawal(msg.sender, token0Amount, token1Amount, amount); 336 | if (token0Amount > 0) { 337 | token0.safeTransfer(to, token0Amount); 338 | } 339 | if (token1Amount > 0) { 340 | token1.safeTransfer(to, token1Amount); 341 | } 342 | } 343 | 344 | function _swap(IERC20 tokenFrom, IERC20 tokenTo, uint256 inputAmount, address to, uint256 minReturnAmount) private returns(uint256 outputAmount) { 345 | outputAmount = getReturn(tokenFrom, tokenTo, inputAmount); 346 | require(outputAmount > 0, "Empty swap is not allowed"); 347 | require(outputAmount >= minReturnAmount, "Min return not reached"); 348 | tokenFrom.safeTransferFrom(msg.sender, address(this), inputAmount); 349 | tokenTo.safeTransfer(to, outputAmount); 350 | } 351 | 352 | /** 353 | * @dev We utilize binary search to find proper to swap 354 | * 355 | * Inital approximation of dx is taken from the same equation by assuming dx ~ dy 356 | * 357 | * x - dx xBalance + dx 358 | * ------ = ------------ 359 | * y + dx yBalance - dx 360 | * 361 | * dx = (x * yBalance - xBalance * y) / (xBalance + yBalance + x + y) 362 | */ 363 | function _getVirtualAmountsForDepositImpl(uint256 x, uint256 y, uint256 xBalance, uint256 yBalance) private pure returns(uint256, uint256) { 364 | uint256 dx = (x * yBalance - y * xBalance) / (xBalance + yBalance + x + y); 365 | if (dx == 0) { 366 | return (x, y); 367 | } 368 | uint256 dy; 369 | uint256 left = dx * _LOWER_BOUND_NUMERATOR / _DENOMINATOR; 370 | uint256 right = Math.min(Math.min(dx * _UPPER_BOUND_NUMERATOR / _DENOMINATOR, yBalance), x); 371 | 372 | while (left + _THRESHOLD < right) { 373 | dy = _getReturn(xBalance, yBalance, dx); 374 | int256 shift = _checkVirtualAmountsFormula(x - dx, y + dy, xBalance + dx, yBalance - dy); 375 | if (shift > 0) { 376 | left = dx; 377 | dx = (dx + right) / 2; 378 | } else if (shift < 0) { 379 | right = dx; 380 | dx = (left + dx) / 2; 381 | } else { 382 | break; 383 | } 384 | } 385 | 386 | return (x - dx, y + dy); 387 | } 388 | 389 | /** 390 | * @dev We utilize binary search to find proper amount to swap 391 | * 392 | * Inital approximation of dx is taken from the same equation by assuming dx ~ dy 393 | * 394 | * x - dx firstTokenShare 395 | * ------ = ---------------------- 396 | * y + dx _ONE - firstTokenShare 397 | * 398 | * dx = (x * (_ONE - firstTokenShare) - y * firstTokenShare) / _ONE 399 | */ 400 | function _getRealAmountsForWithdrawImpl(uint256 virtualX, uint256 virtualY, uint256 balanceX, uint256 balanceY, uint256 firstTokenShare) private pure returns(uint256, uint256) { 401 | require(balanceX != 0 || balanceY != 0, "Amount exceeds total balance"); 402 | if (firstTokenShare == 0) { 403 | return (0, virtualY + _getReturn(balanceX, balanceY, virtualX)); 404 | } 405 | 406 | uint256 secondTokenShare = _ONE - firstTokenShare; 407 | uint256 dx = (virtualX * secondTokenShare - virtualY * firstTokenShare) / _ONE; 408 | uint256 dy; 409 | uint256 left = dx * _LOWER_BOUND_NUMERATOR / _DENOMINATOR; 410 | uint256 right = Math.min(dx * _UPPER_BOUND_NUMERATOR / _DENOMINATOR, virtualX); 411 | 412 | while (left + _THRESHOLD < right) { 413 | dy = _getReturn(balanceX, balanceY, dx); 414 | int256 shift = _checkVirtualAmountsFormula(virtualX - dx, virtualY + dy, firstTokenShare, secondTokenShare); 415 | if (shift > 0) { 416 | left = dx; 417 | dx = (dx + right) / 2; 418 | } else if (shift < 0) { 419 | right = dx; 420 | dx = (left + dx) / 2; 421 | } else { 422 | break; 423 | } 424 | } 425 | 426 | return (virtualX - dx, virtualY + dy); 427 | } 428 | 429 | /** 430 | * @dev 431 | * 432 | * Equilibrium is when ratio of amounts equals to ratio of balances 433 | * 434 | * x xBalance 435 | * --- == ---------- 436 | * y yBalance 437 | * 438 | */ 439 | function _checkVirtualAmountsFormula(uint256 x, uint256 y, uint256 xBalance, uint256 yBalance) private pure returns(int256) { 440 | unchecked { 441 | return int256(x * yBalance - y * xBalance); 442 | } 443 | } 444 | 445 | function _powerHelper(uint256 x) private pure returns(uint256 p) { 446 | unchecked { 447 | if (x > _C3) { 448 | p = x - _C3; 449 | } else { 450 | p = _C3 - x; 451 | } 452 | p = p * p / _ONE; // p ^ 2 453 | uint256 pp = p * p / _ONE; // p ^ 4 454 | pp = pp * pp / _ONE; // p ^ 8 455 | pp = pp * pp / _ONE; // p ^ 16 456 | p = p * pp / _ONE; // p ^ 18 457 | } 458 | } 459 | } 460 | -------------------------------------------------------------------------------- /deploy/deploy.js: -------------------------------------------------------------------------------- 1 | const hre = require('hardhat'); 2 | const { getChainId } = hre; 3 | 4 | // mainnet 5 | const USDC = '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48'; 6 | const USDT = '0xdAC17F958D2ee523a2206206994597C13D831ec7'; 7 | const DECIMALS = 6; 8 | // bsc 9 | // const USDC = '0x8AC76a51cc950d9822D68b83fE1Ad97B32Cd580d'; 10 | // const USDT = '0x55d398326f99059fF775485246999027B3197955'; 11 | // const DECIMALS = 18; 12 | // matic 13 | // const USDC = '0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174'; 14 | // const USDT = '0xc2132D05D31c914a87C6611C10748AEb04B58e8F'; 15 | // const DECIMALS = 6; 16 | // arbitrum 17 | // const USDC = '0xFF970A61A04b1cA14834A43f5dE4533eBDDB5CC8'; 18 | // const USDT = '0xFd086bC7CD5C481DCC9C85ebE478A1C0b69FCbb9'; 19 | // const DECIMALS = 6; 20 | // optimistic 21 | // const USDC = '0x7F5c764cBc14f9669B88837ca1490cCa17c31607'; 22 | // const USDT = '0x94b008aA00579c1307B0EF2c499aD98a8ce58e58'; 23 | // const DECIMALS = 6; 24 | 25 | module.exports = async ({ deployments, getNamedAccounts }) => { 26 | console.log('running deploy script'); 27 | console.log('network id ', await getChainId()); 28 | 29 | const { deploy } = deployments; 30 | const { deployer } = await getNamedAccounts(); 31 | 32 | const args = [USDC, USDT, 'FixedRateSwap', 'FRS', DECIMALS]; 33 | const FixedRateSwap = await deploy('FixedRateSwap', { 34 | args: args, 35 | from: deployer, 36 | skipIfAlreadyDeployed: true, 37 | }); 38 | 39 | console.log('FixedRateSwap deployed to:', FixedRateSwap.address); 40 | 41 | if (await getChainId() !== '31337') { 42 | await hre.run('verify:verify', { 43 | address: FixedRateSwap.address, 44 | constructorArguments: args, 45 | }); 46 | } 47 | }; 48 | 49 | module.exports.skip = async () => true; 50 | -------------------------------------------------------------------------------- /deployments/arbitrum/.chainId: -------------------------------------------------------------------------------- 1 | 42161 -------------------------------------------------------------------------------- /deployments/bsc/.chainId: -------------------------------------------------------------------------------- 1 | 56 -------------------------------------------------------------------------------- /deployments/bsc/FixedRateSwap.json: -------------------------------------------------------------------------------- 1 | { 2 | "address": "0xA83fCeA9229C7f1e02Acb46ABe8D6889259339e8", 3 | "abi": [ 4 | { 5 | "inputs": [ 6 | { 7 | "internalType": "contract IERC20", 8 | "name": "_token0", 9 | "type": "address" 10 | }, 11 | { 12 | "internalType": "contract IERC20", 13 | "name": "_token1", 14 | "type": "address" 15 | }, 16 | { 17 | "internalType": "string", 18 | "name": "name", 19 | "type": "string" 20 | }, 21 | { 22 | "internalType": "string", 23 | "name": "symbol", 24 | "type": "string" 25 | }, 26 | { 27 | "internalType": "uint8", 28 | "name": "decimals_", 29 | "type": "uint8" 30 | } 31 | ], 32 | "stateMutability": "nonpayable", 33 | "type": "constructor" 34 | }, 35 | { 36 | "anonymous": false, 37 | "inputs": [ 38 | { 39 | "indexed": true, 40 | "internalType": "address", 41 | "name": "owner", 42 | "type": "address" 43 | }, 44 | { 45 | "indexed": true, 46 | "internalType": "address", 47 | "name": "spender", 48 | "type": "address" 49 | }, 50 | { 51 | "indexed": false, 52 | "internalType": "uint256", 53 | "name": "value", 54 | "type": "uint256" 55 | } 56 | ], 57 | "name": "Approval", 58 | "type": "event" 59 | }, 60 | { 61 | "anonymous": false, 62 | "inputs": [ 63 | { 64 | "indexed": true, 65 | "internalType": "address", 66 | "name": "user", 67 | "type": "address" 68 | }, 69 | { 70 | "indexed": false, 71 | "internalType": "uint256", 72 | "name": "token0Amount", 73 | "type": "uint256" 74 | }, 75 | { 76 | "indexed": false, 77 | "internalType": "uint256", 78 | "name": "token1Amount", 79 | "type": "uint256" 80 | }, 81 | { 82 | "indexed": false, 83 | "internalType": "uint256", 84 | "name": "share", 85 | "type": "uint256" 86 | } 87 | ], 88 | "name": "Deposit", 89 | "type": "event" 90 | }, 91 | { 92 | "anonymous": false, 93 | "inputs": [ 94 | { 95 | "indexed": true, 96 | "internalType": "address", 97 | "name": "trader", 98 | "type": "address" 99 | }, 100 | { 101 | "indexed": false, 102 | "internalType": "int256", 103 | "name": "token0Amount", 104 | "type": "int256" 105 | }, 106 | { 107 | "indexed": false, 108 | "internalType": "int256", 109 | "name": "token1Amount", 110 | "type": "int256" 111 | } 112 | ], 113 | "name": "Swap", 114 | "type": "event" 115 | }, 116 | { 117 | "anonymous": false, 118 | "inputs": [ 119 | { 120 | "indexed": true, 121 | "internalType": "address", 122 | "name": "from", 123 | "type": "address" 124 | }, 125 | { 126 | "indexed": true, 127 | "internalType": "address", 128 | "name": "to", 129 | "type": "address" 130 | }, 131 | { 132 | "indexed": false, 133 | "internalType": "uint256", 134 | "name": "value", 135 | "type": "uint256" 136 | } 137 | ], 138 | "name": "Transfer", 139 | "type": "event" 140 | }, 141 | { 142 | "anonymous": false, 143 | "inputs": [ 144 | { 145 | "indexed": true, 146 | "internalType": "address", 147 | "name": "user", 148 | "type": "address" 149 | }, 150 | { 151 | "indexed": false, 152 | "internalType": "uint256", 153 | "name": "token0Amount", 154 | "type": "uint256" 155 | }, 156 | { 157 | "indexed": false, 158 | "internalType": "uint256", 159 | "name": "token1Amount", 160 | "type": "uint256" 161 | }, 162 | { 163 | "indexed": false, 164 | "internalType": "uint256", 165 | "name": "share", 166 | "type": "uint256" 167 | } 168 | ], 169 | "name": "Withdrawal", 170 | "type": "event" 171 | }, 172 | { 173 | "inputs": [ 174 | { 175 | "internalType": "address", 176 | "name": "owner", 177 | "type": "address" 178 | }, 179 | { 180 | "internalType": "address", 181 | "name": "spender", 182 | "type": "address" 183 | } 184 | ], 185 | "name": "allowance", 186 | "outputs": [ 187 | { 188 | "internalType": "uint256", 189 | "name": "", 190 | "type": "uint256" 191 | } 192 | ], 193 | "stateMutability": "view", 194 | "type": "function" 195 | }, 196 | { 197 | "inputs": [ 198 | { 199 | "internalType": "address", 200 | "name": "spender", 201 | "type": "address" 202 | }, 203 | { 204 | "internalType": "uint256", 205 | "name": "amount", 206 | "type": "uint256" 207 | } 208 | ], 209 | "name": "approve", 210 | "outputs": [ 211 | { 212 | "internalType": "bool", 213 | "name": "", 214 | "type": "bool" 215 | } 216 | ], 217 | "stateMutability": "nonpayable", 218 | "type": "function" 219 | }, 220 | { 221 | "inputs": [ 222 | { 223 | "internalType": "address", 224 | "name": "account", 225 | "type": "address" 226 | } 227 | ], 228 | "name": "balanceOf", 229 | "outputs": [ 230 | { 231 | "internalType": "uint256", 232 | "name": "", 233 | "type": "uint256" 234 | } 235 | ], 236 | "stateMutability": "view", 237 | "type": "function" 238 | }, 239 | { 240 | "inputs": [], 241 | "name": "decimals", 242 | "outputs": [ 243 | { 244 | "internalType": "uint8", 245 | "name": "", 246 | "type": "uint8" 247 | } 248 | ], 249 | "stateMutability": "view", 250 | "type": "function" 251 | }, 252 | { 253 | "inputs": [ 254 | { 255 | "internalType": "address", 256 | "name": "spender", 257 | "type": "address" 258 | }, 259 | { 260 | "internalType": "uint256", 261 | "name": "subtractedValue", 262 | "type": "uint256" 263 | } 264 | ], 265 | "name": "decreaseAllowance", 266 | "outputs": [ 267 | { 268 | "internalType": "bool", 269 | "name": "", 270 | "type": "bool" 271 | } 272 | ], 273 | "stateMutability": "nonpayable", 274 | "type": "function" 275 | }, 276 | { 277 | "inputs": [ 278 | { 279 | "internalType": "uint256", 280 | "name": "token0Amount", 281 | "type": "uint256" 282 | }, 283 | { 284 | "internalType": "uint256", 285 | "name": "token1Amount", 286 | "type": "uint256" 287 | }, 288 | { 289 | "internalType": "uint256", 290 | "name": "minShare", 291 | "type": "uint256" 292 | } 293 | ], 294 | "name": "deposit", 295 | "outputs": [ 296 | { 297 | "internalType": "uint256", 298 | "name": "share", 299 | "type": "uint256" 300 | } 301 | ], 302 | "stateMutability": "nonpayable", 303 | "type": "function" 304 | }, 305 | { 306 | "inputs": [ 307 | { 308 | "internalType": "uint256", 309 | "name": "token0Amount", 310 | "type": "uint256" 311 | }, 312 | { 313 | "internalType": "uint256", 314 | "name": "token1Amount", 315 | "type": "uint256" 316 | }, 317 | { 318 | "internalType": "address", 319 | "name": "to", 320 | "type": "address" 321 | }, 322 | { 323 | "internalType": "uint256", 324 | "name": "minShare", 325 | "type": "uint256" 326 | } 327 | ], 328 | "name": "depositFor", 329 | "outputs": [ 330 | { 331 | "internalType": "uint256", 332 | "name": "share", 333 | "type": "uint256" 334 | } 335 | ], 336 | "stateMutability": "nonpayable", 337 | "type": "function" 338 | }, 339 | { 340 | "inputs": [ 341 | { 342 | "internalType": "contract IERC20", 343 | "name": "tokenFrom", 344 | "type": "address" 345 | }, 346 | { 347 | "internalType": "contract IERC20", 348 | "name": "tokenTo", 349 | "type": "address" 350 | }, 351 | { 352 | "internalType": "uint256", 353 | "name": "inputAmount", 354 | "type": "uint256" 355 | } 356 | ], 357 | "name": "getReturn", 358 | "outputs": [ 359 | { 360 | "internalType": "uint256", 361 | "name": "outputAmount", 362 | "type": "uint256" 363 | } 364 | ], 365 | "stateMutability": "view", 366 | "type": "function" 367 | }, 368 | { 369 | "inputs": [ 370 | { 371 | "internalType": "address", 372 | "name": "spender", 373 | "type": "address" 374 | }, 375 | { 376 | "internalType": "uint256", 377 | "name": "addedValue", 378 | "type": "uint256" 379 | } 380 | ], 381 | "name": "increaseAllowance", 382 | "outputs": [ 383 | { 384 | "internalType": "bool", 385 | "name": "", 386 | "type": "bool" 387 | } 388 | ], 389 | "stateMutability": "nonpayable", 390 | "type": "function" 391 | }, 392 | { 393 | "inputs": [], 394 | "name": "name", 395 | "outputs": [ 396 | { 397 | "internalType": "string", 398 | "name": "", 399 | "type": "string" 400 | } 401 | ], 402 | "stateMutability": "view", 403 | "type": "function" 404 | }, 405 | { 406 | "inputs": [ 407 | { 408 | "internalType": "uint256", 409 | "name": "inputAmount", 410 | "type": "uint256" 411 | }, 412 | { 413 | "internalType": "uint256", 414 | "name": "minReturnAmount", 415 | "type": "uint256" 416 | } 417 | ], 418 | "name": "swap0To1", 419 | "outputs": [ 420 | { 421 | "internalType": "uint256", 422 | "name": "outputAmount", 423 | "type": "uint256" 424 | } 425 | ], 426 | "stateMutability": "nonpayable", 427 | "type": "function" 428 | }, 429 | { 430 | "inputs": [ 431 | { 432 | "internalType": "uint256", 433 | "name": "inputAmount", 434 | "type": "uint256" 435 | }, 436 | { 437 | "internalType": "address", 438 | "name": "to", 439 | "type": "address" 440 | }, 441 | { 442 | "internalType": "uint256", 443 | "name": "minReturnAmount", 444 | "type": "uint256" 445 | } 446 | ], 447 | "name": "swap0To1For", 448 | "outputs": [ 449 | { 450 | "internalType": "uint256", 451 | "name": "outputAmount", 452 | "type": "uint256" 453 | } 454 | ], 455 | "stateMutability": "nonpayable", 456 | "type": "function" 457 | }, 458 | { 459 | "inputs": [ 460 | { 461 | "internalType": "uint256", 462 | "name": "inputAmount", 463 | "type": "uint256" 464 | }, 465 | { 466 | "internalType": "uint256", 467 | "name": "minReturnAmount", 468 | "type": "uint256" 469 | } 470 | ], 471 | "name": "swap1To0", 472 | "outputs": [ 473 | { 474 | "internalType": "uint256", 475 | "name": "outputAmount", 476 | "type": "uint256" 477 | } 478 | ], 479 | "stateMutability": "nonpayable", 480 | "type": "function" 481 | }, 482 | { 483 | "inputs": [ 484 | { 485 | "internalType": "uint256", 486 | "name": "inputAmount", 487 | "type": "uint256" 488 | }, 489 | { 490 | "internalType": "address", 491 | "name": "to", 492 | "type": "address" 493 | }, 494 | { 495 | "internalType": "uint256", 496 | "name": "minReturnAmount", 497 | "type": "uint256" 498 | } 499 | ], 500 | "name": "swap1To0For", 501 | "outputs": [ 502 | { 503 | "internalType": "uint256", 504 | "name": "outputAmount", 505 | "type": "uint256" 506 | } 507 | ], 508 | "stateMutability": "nonpayable", 509 | "type": "function" 510 | }, 511 | { 512 | "inputs": [], 513 | "name": "symbol", 514 | "outputs": [ 515 | { 516 | "internalType": "string", 517 | "name": "", 518 | "type": "string" 519 | } 520 | ], 521 | "stateMutability": "view", 522 | "type": "function" 523 | }, 524 | { 525 | "inputs": [], 526 | "name": "token0", 527 | "outputs": [ 528 | { 529 | "internalType": "contract IERC20", 530 | "name": "", 531 | "type": "address" 532 | } 533 | ], 534 | "stateMutability": "view", 535 | "type": "function" 536 | }, 537 | { 538 | "inputs": [], 539 | "name": "token1", 540 | "outputs": [ 541 | { 542 | "internalType": "contract IERC20", 543 | "name": "", 544 | "type": "address" 545 | } 546 | ], 547 | "stateMutability": "view", 548 | "type": "function" 549 | }, 550 | { 551 | "inputs": [], 552 | "name": "totalSupply", 553 | "outputs": [ 554 | { 555 | "internalType": "uint256", 556 | "name": "", 557 | "type": "uint256" 558 | } 559 | ], 560 | "stateMutability": "view", 561 | "type": "function" 562 | }, 563 | { 564 | "inputs": [ 565 | { 566 | "internalType": "address", 567 | "name": "recipient", 568 | "type": "address" 569 | }, 570 | { 571 | "internalType": "uint256", 572 | "name": "amount", 573 | "type": "uint256" 574 | } 575 | ], 576 | "name": "transfer", 577 | "outputs": [ 578 | { 579 | "internalType": "bool", 580 | "name": "", 581 | "type": "bool" 582 | } 583 | ], 584 | "stateMutability": "nonpayable", 585 | "type": "function" 586 | }, 587 | { 588 | "inputs": [ 589 | { 590 | "internalType": "address", 591 | "name": "sender", 592 | "type": "address" 593 | }, 594 | { 595 | "internalType": "address", 596 | "name": "recipient", 597 | "type": "address" 598 | }, 599 | { 600 | "internalType": "uint256", 601 | "name": "amount", 602 | "type": "uint256" 603 | } 604 | ], 605 | "name": "transferFrom", 606 | "outputs": [ 607 | { 608 | "internalType": "bool", 609 | "name": "", 610 | "type": "bool" 611 | } 612 | ], 613 | "stateMutability": "nonpayable", 614 | "type": "function" 615 | }, 616 | { 617 | "inputs": [ 618 | { 619 | "internalType": "uint256", 620 | "name": "amount", 621 | "type": "uint256" 622 | }, 623 | { 624 | "internalType": "uint256", 625 | "name": "minToken0Amount", 626 | "type": "uint256" 627 | }, 628 | { 629 | "internalType": "uint256", 630 | "name": "minToken1Amount", 631 | "type": "uint256" 632 | } 633 | ], 634 | "name": "withdraw", 635 | "outputs": [ 636 | { 637 | "internalType": "uint256", 638 | "name": "token0Amount", 639 | "type": "uint256" 640 | }, 641 | { 642 | "internalType": "uint256", 643 | "name": "token1Amount", 644 | "type": "uint256" 645 | } 646 | ], 647 | "stateMutability": "nonpayable", 648 | "type": "function" 649 | }, 650 | { 651 | "inputs": [ 652 | { 653 | "internalType": "uint256", 654 | "name": "amount", 655 | "type": "uint256" 656 | }, 657 | { 658 | "internalType": "address", 659 | "name": "to", 660 | "type": "address" 661 | }, 662 | { 663 | "internalType": "uint256", 664 | "name": "minToken0Amount", 665 | "type": "uint256" 666 | }, 667 | { 668 | "internalType": "uint256", 669 | "name": "minToken1Amount", 670 | "type": "uint256" 671 | } 672 | ], 673 | "name": "withdrawFor", 674 | "outputs": [ 675 | { 676 | "internalType": "uint256", 677 | "name": "token0Amount", 678 | "type": "uint256" 679 | }, 680 | { 681 | "internalType": "uint256", 682 | "name": "token1Amount", 683 | "type": "uint256" 684 | } 685 | ], 686 | "stateMutability": "nonpayable", 687 | "type": "function" 688 | }, 689 | { 690 | "inputs": [ 691 | { 692 | "internalType": "uint256", 693 | "name": "amount", 694 | "type": "uint256" 695 | }, 696 | { 697 | "internalType": "address", 698 | "name": "to", 699 | "type": "address" 700 | }, 701 | { 702 | "internalType": "uint256", 703 | "name": "token0Share", 704 | "type": "uint256" 705 | }, 706 | { 707 | "internalType": "uint256", 708 | "name": "minToken0Amount", 709 | "type": "uint256" 710 | }, 711 | { 712 | "internalType": "uint256", 713 | "name": "minToken1Amount", 714 | "type": "uint256" 715 | } 716 | ], 717 | "name": "withdrawForWithRatio", 718 | "outputs": [ 719 | { 720 | "internalType": "uint256", 721 | "name": "token0Amount", 722 | "type": "uint256" 723 | }, 724 | { 725 | "internalType": "uint256", 726 | "name": "token1Amount", 727 | "type": "uint256" 728 | } 729 | ], 730 | "stateMutability": "nonpayable", 731 | "type": "function" 732 | }, 733 | { 734 | "inputs": [ 735 | { 736 | "internalType": "uint256", 737 | "name": "amount", 738 | "type": "uint256" 739 | }, 740 | { 741 | "internalType": "uint256", 742 | "name": "token0Share", 743 | "type": "uint256" 744 | }, 745 | { 746 | "internalType": "uint256", 747 | "name": "minToken0Amount", 748 | "type": "uint256" 749 | }, 750 | { 751 | "internalType": "uint256", 752 | "name": "minToken1Amount", 753 | "type": "uint256" 754 | } 755 | ], 756 | "name": "withdrawWithRatio", 757 | "outputs": [ 758 | { 759 | "internalType": "uint256", 760 | "name": "token0Amount", 761 | "type": "uint256" 762 | }, 763 | { 764 | "internalType": "uint256", 765 | "name": "token1Amount", 766 | "type": "uint256" 767 | } 768 | ], 769 | "stateMutability": "nonpayable", 770 | "type": "function" 771 | } 772 | ], 773 | "transactionHash": "0xcf1f9e2f0504d3b1121add079050f7513a695da046a6fa0bdb72b3be07b1e8d5", 774 | "receipt": { 775 | "to": null, 776 | "from": "0x11799622F4D98A24514011E8527B969f7488eF47", 777 | "contractAddress": "0xA83fCeA9229C7f1e02Acb46ABe8D6889259339e8", 778 | "transactionIndex": 29, 779 | "gasUsed": "2878143", 780 | "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", 781 | "blockHash": "0x854c37c80e5a22cf189a0ac72b7a8fc18abd86723b4e54b5d73c7a3f6f7ed097", 782 | "transactionHash": "0xcf1f9e2f0504d3b1121add079050f7513a695da046a6fa0bdb72b3be07b1e8d5", 783 | "logs": [], 784 | "blockNumber": 12970860, 785 | "cumulativeGasUsed": "3715665", 786 | "status": 1, 787 | "byzantium": true 788 | }, 789 | "args": [ 790 | "0x8AC76a51cc950d9822D68b83fE1Ad97B32Cd580d", 791 | "0x55d398326f99059fF775485246999027B3197955", 792 | "FixedRateSwap", 793 | "FRS", 794 | 18 795 | ], 796 | "solcInputHash": "2445d7b873241221d1d800222d2189f8", 797 | "metadata": "{\"compiler\":{\"version\":\"0.8.10+commit.fc410830\"},\"language\":\"Solidity\",\"output\":{\"abi\":[{\"inputs\":[{\"internalType\":\"contract IERC20\",\"name\":\"_token0\",\"type\":\"address\"},{\"internalType\":\"contract IERC20\",\"name\":\"_token1\",\"type\":\"address\"},{\"internalType\":\"string\",\"name\":\"name\",\"type\":\"string\"},{\"internalType\":\"string\",\"name\":\"symbol\",\"type\":\"string\"},{\"internalType\":\"uint8\",\"name\":\"decimals_\",\"type\":\"uint8\"}],\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"address\",\"name\":\"owner\",\"type\":\"address\"},{\"indexed\":true,\"internalType\":\"address\",\"name\":\"spender\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"value\",\"type\":\"uint256\"}],\"name\":\"Approval\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"address\",\"name\":\"user\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"token0Amount\",\"type\":\"uint256\"},{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"token1Amount\",\"type\":\"uint256\"},{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"share\",\"type\":\"uint256\"}],\"name\":\"Deposit\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"address\",\"name\":\"trader\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"int256\",\"name\":\"token0Amount\",\"type\":\"int256\"},{\"indexed\":false,\"internalType\":\"int256\",\"name\":\"token1Amount\",\"type\":\"int256\"}],\"name\":\"Swap\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"address\",\"name\":\"from\",\"type\":\"address\"},{\"indexed\":true,\"internalType\":\"address\",\"name\":\"to\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"value\",\"type\":\"uint256\"}],\"name\":\"Transfer\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"address\",\"name\":\"user\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"token0Amount\",\"type\":\"uint256\"},{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"token1Amount\",\"type\":\"uint256\"},{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"share\",\"type\":\"uint256\"}],\"name\":\"Withdrawal\",\"type\":\"event\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"owner\",\"type\":\"address\"},{\"internalType\":\"address\",\"name\":\"spender\",\"type\":\"address\"}],\"name\":\"allowance\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"spender\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"amount\",\"type\":\"uint256\"}],\"name\":\"approve\",\"outputs\":[{\"internalType\":\"bool\",\"name\":\"\",\"type\":\"bool\"}],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"account\",\"type\":\"address\"}],\"name\":\"balanceOf\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"decimals\",\"outputs\":[{\"internalType\":\"uint8\",\"name\":\"\",\"type\":\"uint8\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"spender\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"subtractedValue\",\"type\":\"uint256\"}],\"name\":\"decreaseAllowance\",\"outputs\":[{\"internalType\":\"bool\",\"name\":\"\",\"type\":\"bool\"}],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"uint256\",\"name\":\"token0Amount\",\"type\":\"uint256\"},{\"internalType\":\"uint256\",\"name\":\"token1Amount\",\"type\":\"uint256\"},{\"internalType\":\"uint256\",\"name\":\"minShare\",\"type\":\"uint256\"}],\"name\":\"deposit\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"share\",\"type\":\"uint256\"}],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"uint256\",\"name\":\"token0Amount\",\"type\":\"uint256\"},{\"internalType\":\"uint256\",\"name\":\"token1Amount\",\"type\":\"uint256\"},{\"internalType\":\"address\",\"name\":\"to\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"minShare\",\"type\":\"uint256\"}],\"name\":\"depositFor\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"share\",\"type\":\"uint256\"}],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"contract IERC20\",\"name\":\"tokenFrom\",\"type\":\"address\"},{\"internalType\":\"contract IERC20\",\"name\":\"tokenTo\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"inputAmount\",\"type\":\"uint256\"}],\"name\":\"getReturn\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"outputAmount\",\"type\":\"uint256\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"spender\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"addedValue\",\"type\":\"uint256\"}],\"name\":\"increaseAllowance\",\"outputs\":[{\"internalType\":\"bool\",\"name\":\"\",\"type\":\"bool\"}],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"name\",\"outputs\":[{\"internalType\":\"string\",\"name\":\"\",\"type\":\"string\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"uint256\",\"name\":\"inputAmount\",\"type\":\"uint256\"},{\"internalType\":\"uint256\",\"name\":\"minReturnAmount\",\"type\":\"uint256\"}],\"name\":\"swap0To1\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"outputAmount\",\"type\":\"uint256\"}],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"uint256\",\"name\":\"inputAmount\",\"type\":\"uint256\"},{\"internalType\":\"address\",\"name\":\"to\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"minReturnAmount\",\"type\":\"uint256\"}],\"name\":\"swap0To1For\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"outputAmount\",\"type\":\"uint256\"}],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"uint256\",\"name\":\"inputAmount\",\"type\":\"uint256\"},{\"internalType\":\"uint256\",\"name\":\"minReturnAmount\",\"type\":\"uint256\"}],\"name\":\"swap1To0\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"outputAmount\",\"type\":\"uint256\"}],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"uint256\",\"name\":\"inputAmount\",\"type\":\"uint256\"},{\"internalType\":\"address\",\"name\":\"to\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"minReturnAmount\",\"type\":\"uint256\"}],\"name\":\"swap1To0For\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"outputAmount\",\"type\":\"uint256\"}],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"symbol\",\"outputs\":[{\"internalType\":\"string\",\"name\":\"\",\"type\":\"string\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"token0\",\"outputs\":[{\"internalType\":\"contract IERC20\",\"name\":\"\",\"type\":\"address\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"token1\",\"outputs\":[{\"internalType\":\"contract IERC20\",\"name\":\"\",\"type\":\"address\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"totalSupply\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"recipient\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"amount\",\"type\":\"uint256\"}],\"name\":\"transfer\",\"outputs\":[{\"internalType\":\"bool\",\"name\":\"\",\"type\":\"bool\"}],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"sender\",\"type\":\"address\"},{\"internalType\":\"address\",\"name\":\"recipient\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"amount\",\"type\":\"uint256\"}],\"name\":\"transferFrom\",\"outputs\":[{\"internalType\":\"bool\",\"name\":\"\",\"type\":\"bool\"}],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"uint256\",\"name\":\"amount\",\"type\":\"uint256\"},{\"internalType\":\"uint256\",\"name\":\"minToken0Amount\",\"type\":\"uint256\"},{\"internalType\":\"uint256\",\"name\":\"minToken1Amount\",\"type\":\"uint256\"}],\"name\":\"withdraw\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"token0Amount\",\"type\":\"uint256\"},{\"internalType\":\"uint256\",\"name\":\"token1Amount\",\"type\":\"uint256\"}],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"uint256\",\"name\":\"amount\",\"type\":\"uint256\"},{\"internalType\":\"address\",\"name\":\"to\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"minToken0Amount\",\"type\":\"uint256\"},{\"internalType\":\"uint256\",\"name\":\"minToken1Amount\",\"type\":\"uint256\"}],\"name\":\"withdrawFor\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"token0Amount\",\"type\":\"uint256\"},{\"internalType\":\"uint256\",\"name\":\"token1Amount\",\"type\":\"uint256\"}],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"uint256\",\"name\":\"amount\",\"type\":\"uint256\"},{\"internalType\":\"address\",\"name\":\"to\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"token0Share\",\"type\":\"uint256\"},{\"internalType\":\"uint256\",\"name\":\"minToken0Amount\",\"type\":\"uint256\"},{\"internalType\":\"uint256\",\"name\":\"minToken1Amount\",\"type\":\"uint256\"}],\"name\":\"withdrawForWithRatio\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"token0Amount\",\"type\":\"uint256\"},{\"internalType\":\"uint256\",\"name\":\"token1Amount\",\"type\":\"uint256\"}],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"uint256\",\"name\":\"amount\",\"type\":\"uint256\"},{\"internalType\":\"uint256\",\"name\":\"token0Share\",\"type\":\"uint256\"},{\"internalType\":\"uint256\",\"name\":\"minToken0Amount\",\"type\":\"uint256\"},{\"internalType\":\"uint256\",\"name\":\"minToken1Amount\",\"type\":\"uint256\"}],\"name\":\"withdrawWithRatio\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"token0Amount\",\"type\":\"uint256\"},{\"internalType\":\"uint256\",\"name\":\"token1Amount\",\"type\":\"uint256\"}],\"stateMutability\":\"nonpayable\",\"type\":\"function\"}],\"devdoc\":{\"details\":\"AMM that is designed for assets with stable price to each other e.g. USDC and USDT. It utilizes constant sum price curve x + y = const but fee is variable depending on the token balances. In most cases fee is equal to 1 bip. But when balances are at extreme ends it either lowers to 0 or increases to 20 bip. Fee calculations are explained in more details in `getReturn` method. Note that AMM does not support token with fees. Note that tokens decimals are required to be the same.\",\"kind\":\"dev\",\"methods\":{\"allowance(address,address)\":{\"details\":\"See {IERC20-allowance}.\"},\"approve(address,uint256)\":{\"details\":\"See {IERC20-approve}. Requirements: - `spender` cannot be the zero address.\"},\"balanceOf(address)\":{\"details\":\"See {IERC20-balanceOf}.\"},\"decimals()\":{\"details\":\"Returns the number of decimals used to get its user representation. For example, if `decimals` equals `2`, a balance of `505` tokens should be displayed to a user as `5.05` (`505 / 10 ** 2`). Tokens usually opt for a value of 18, imitating the relationship between Ether and Wei. This is the value {ERC20} uses, unless this function is overridden; NOTE: This information is only used for _display_ purposes: it in no way affects any of the arithmetic of the contract, including {IERC20-balanceOf} and {IERC20-transfer}.\"},\"decreaseAllowance(address,uint256)\":{\"details\":\"Atomically decreases the allowance granted to `spender` by the caller. This is an alternative to {approve} that can be used as a mitigation for problems described in {IERC20-approve}. Emits an {Approval} event indicating the updated allowance. Requirements: - `spender` cannot be the zero address. - `spender` must have allowance for the caller of at least `subtractedValue`.\"},\"deposit(uint256,uint256,uint256)\":{\"params\":{\"minShare\":\"minimal required amount of LP tokens received\",\"token0Amount\":\"amount of token0 to deposit\",\"token1Amount\":\"amount of token1 to deposit\"},\"returns\":{\"share\":\"amount of LP tokens received\"}},\"depositFor(uint256,uint256,address,uint256)\":{\"details\":\"fully balanced deposit happens when ratio of amounts of deposit matches ratio of balances. To make a fair deposit when ratios do not match the contract finds the amount that is needed to swap to equalize ratios and makes that swap virtually to capture the swap fees. Then final share is calculated from fair deposit of virtual amounts.\",\"params\":{\"minShare\":\"minimal required amount of LP tokens received\",\"to\":\"address that will receive tokens\",\"token0Amount\":\"amount of token0 to deposit\",\"token1Amount\":\"amount of token1 to deposit\"},\"returns\":{\"share\":\"amount of LP tokens received\"}},\"getReturn(address,address,uint256)\":{\"details\":\"`getReturn` at point `x = inputBalance / (inputBalance + outputBalance)`: `getReturn(x) = 0.9999 + (0.5817091329374359 - x * 1.2734233188154198)^17` When balance is changed from `inputBalance` to `inputBalance + amount` we should take integral of getReturn to calculate proper amount: `getReturn(x0, x1) = (integral (0.9999 + (0.5817091329374359 - x * 1.2734233188154198)^17) dx from x=x0 to x=x1) / (x1 - x0)` `getReturn(x0, x1) = (0.9999 * x - 3.3827123349983306 * (x - 0.4568073509746632) ** 18 from x=x0 to x=x1) / (x1 - x0)` `getReturn(x0, x1) = (0.9999 * (x1 - x0) + 3.3827123349983306 * ((x0 - 0.4568073509746632) ** 18 - (x1 - 0.4568073509746632) ** 18)) / (x1 - x0)` C0 = 0.9999 C2 = 3.3827123349983306 C3 = 0.4568073509746632 `getReturn(x0, x1) = (C0 * (x1 - x0) + C2 * ((x0 - C3) ** 18 - (x1 - C3) ** 18)) / (x1 - x0)`\",\"params\":{\"inputAmount\":\"amount of `tokenFrom` that user wants to sell\",\"tokenFrom\":\"token that user wants to sell\",\"tokenTo\":\"token that user wants to buy\"},\"returns\":{\"outputAmount\":\"amount of `tokenTo` that user will receive after the trade\"}},\"increaseAllowance(address,uint256)\":{\"details\":\"Atomically increases the allowance granted to `spender` by the caller. This is an alternative to {approve} that can be used as a mitigation for problems described in {IERC20-approve}. Emits an {Approval} event indicating the updated allowance. Requirements: - `spender` cannot be the zero address.\"},\"name()\":{\"details\":\"Returns the name of the token.\"},\"swap0To1(uint256,uint256)\":{\"params\":{\"inputAmount\":\"amount of token0 to sell\",\"minReturnAmount\":\"minimal required amount of token1 to buy\"},\"returns\":{\"outputAmount\":\"amount of token1 bought\"}},\"swap0To1For(uint256,address,uint256)\":{\"params\":{\"inputAmount\":\"amount of token0 to sell\",\"minReturnAmount\":\"minimal required amount of token1 to buy\",\"to\":\"address that will receive tokens\"},\"returns\":{\"outputAmount\":\"amount of token1 bought\"}},\"swap1To0(uint256,uint256)\":{\"params\":{\"inputAmount\":\"amount of token1 to sell\",\"minReturnAmount\":\"minimal required amount of token0 to buy\"},\"returns\":{\"outputAmount\":\"amount of token0 bought\"}},\"swap1To0For(uint256,address,uint256)\":{\"params\":{\"inputAmount\":\"amount of token1 to sell\",\"minReturnAmount\":\"minimal required amount of token0 to buy\",\"to\":\"address that will receive tokens\"},\"returns\":{\"outputAmount\":\"amount of token0 bought\"}},\"symbol()\":{\"details\":\"Returns the symbol of the token, usually a shorter version of the name.\"},\"totalSupply()\":{\"details\":\"See {IERC20-totalSupply}.\"},\"transfer(address,uint256)\":{\"details\":\"See {IERC20-transfer}. Requirements: - `recipient` cannot be the zero address. - the caller must have a balance of at least `amount`.\"},\"transferFrom(address,address,uint256)\":{\"details\":\"See {IERC20-transferFrom}. Emits an {Approval} event indicating the updated allowance. This is not required by the EIP. See the note at the beginning of {ERC20}. Requirements: - `sender` and `recipient` cannot be the zero address. - `sender` must have a balance of at least `amount`. - the caller must have allowance for ``sender``'s tokens of at least `amount`.\"},\"withdraw(uint256,uint256,uint256)\":{\"params\":{\"amount\":\"amount of LP tokens to burn\",\"minToken0Amount\":\"minimal required amount of token0\",\"minToken1Amount\":\"minimal required amount of token1\"},\"returns\":{\"token0Amount\":\"amount of token0 received\",\"token1Amount\":\"amount of token1 received\"}},\"withdrawFor(uint256,address,uint256,uint256)\":{\"params\":{\"amount\":\"amount of LP tokens to burn\",\"minToken0Amount\":\"minimal required amount of token0\",\"minToken1Amount\":\"minimal required amount of token1\",\"to\":\"address that will receive tokens\"},\"returns\":{\"token0Amount\":\"amount of token0 received\",\"token1Amount\":\"amount of token1 received\"}},\"withdrawForWithRatio(uint256,address,uint256,uint256,uint256)\":{\"details\":\"withdrawal with custom ratio is semantically equal to proportional withdrawal with extra swap afterwards to get to the specified ratio. The contract does exactly this by making virtual proportional withdrawal and then finds the amount needed for an extra virtual swap to achieve specified ratio.\",\"params\":{\"amount\":\"amount of LP tokens to burn\",\"minToken0Amount\":\"minimal required amount of token0\",\"minToken1Amount\":\"minimal required amount of token1\",\"to\":\"address that will receive tokens\",\"token0Share\":\"percentage of token0 to receive with 100% equals to 1e18\"},\"returns\":{\"token0Amount\":\"amount of token0 received\",\"token1Amount\":\"amount of token1 received\"}},\"withdrawWithRatio(uint256,uint256,uint256,uint256)\":{\"params\":{\"amount\":\"amount of LP tokens to burn\",\"minToken0Amount\":\"minimal required amount of token0\",\"minToken1Amount\":\"minimal required amount of token1\",\"token0Share\":\"percentage of token0 to receive with 100% equals to 1e18\"},\"returns\":{\"token0Amount\":\"amount of token0 received\",\"token1Amount\":\"amount of token1 received\"}}},\"version\":1},\"userdoc\":{\"kind\":\"user\",\"methods\":{\"deposit(uint256,uint256,uint256)\":{\"notice\":\"makes a deposit of both tokens to the AMM\"},\"depositFor(uint256,uint256,address,uint256)\":{\"notice\":\"makes a deposit of both tokens to the AMM and transfers LP tokens to the specified address\"},\"getReturn(address,address,uint256)\":{\"notice\":\"estimates return value of the swap\"},\"swap0To1(uint256,uint256)\":{\"notice\":\"swaps token0 for token1\"},\"swap0To1For(uint256,address,uint256)\":{\"notice\":\"swaps token0 for token1 and transfers them to specified receiver address\"},\"swap1To0(uint256,uint256)\":{\"notice\":\"swaps token1 for token0\"},\"swap1To0For(uint256,address,uint256)\":{\"notice\":\"swaps token1 for token0 and transfers them to specified receiver address\"},\"withdraw(uint256,uint256,uint256)\":{\"notice\":\"makes a proportional withdrawal of both tokens\"},\"withdrawFor(uint256,address,uint256,uint256)\":{\"notice\":\"makes a proportional withdrawal of both tokens and transfers them to the specified address\"},\"withdrawForWithRatio(uint256,address,uint256,uint256,uint256)\":{\"notice\":\"makes a withdrawal with custom ratio and transfers tokens to the specified address\"},\"withdrawWithRatio(uint256,uint256,uint256,uint256)\":{\"notice\":\"makes a withdrawal with custom ratio\"}},\"version\":1}},\"settings\":{\"compilationTarget\":{\"contracts/FixedRateSwap.sol\":\"FixedRateSwap\"},\"evmVersion\":\"london\",\"libraries\":{},\"metadata\":{\"bytecodeHash\":\"ipfs\",\"useLiteralContent\":true},\"optimizer\":{\"enabled\":true,\"runs\":1000000},\"remappings\":[]},\"sources\":{\"@openzeppelin/contracts/token/ERC20/ERC20.sol\":{\"content\":\"// SPDX-License-Identifier: MIT\\n// OpenZeppelin Contracts v4.4.0 (token/ERC20/ERC20.sol)\\n\\npragma solidity ^0.8.0;\\n\\nimport \\\"./IERC20.sol\\\";\\nimport \\\"./extensions/IERC20Metadata.sol\\\";\\nimport \\\"../../utils/Context.sol\\\";\\n\\n/**\\n * @dev Implementation of the {IERC20} interface.\\n *\\n * This implementation is agnostic to the way tokens are created. This means\\n * that a supply mechanism has to be added in a derived contract using {_mint}.\\n * For a generic mechanism see {ERC20PresetMinterPauser}.\\n *\\n * TIP: For a detailed writeup see our guide\\n * https://forum.zeppelin.solutions/t/how-to-implement-erc20-supply-mechanisms/226[How\\n * to implement supply mechanisms].\\n *\\n * We have followed general OpenZeppelin Contracts guidelines: functions revert\\n * instead returning `false` on failure. This behavior is nonetheless\\n * conventional and does not conflict with the expectations of ERC20\\n * applications.\\n *\\n * Additionally, an {Approval} event is emitted on calls to {transferFrom}.\\n * This allows applications to reconstruct the allowance for all accounts just\\n * by listening to said events. Other implementations of the EIP may not emit\\n * these events, as it isn't required by the specification.\\n *\\n * Finally, the non-standard {decreaseAllowance} and {increaseAllowance}\\n * functions have been added to mitigate the well-known issues around setting\\n * allowances. See {IERC20-approve}.\\n */\\ncontract ERC20 is Context, IERC20, IERC20Metadata {\\n mapping(address => uint256) private _balances;\\n\\n mapping(address => mapping(address => uint256)) private _allowances;\\n\\n uint256 private _totalSupply;\\n\\n string private _name;\\n string private _symbol;\\n\\n /**\\n * @dev Sets the values for {name} and {symbol}.\\n *\\n * The default value of {decimals} is 18. To select a different value for\\n * {decimals} you should overload it.\\n *\\n * All two of these values are immutable: they can only be set once during\\n * construction.\\n */\\n constructor(string memory name_, string memory symbol_) {\\n _name = name_;\\n _symbol = symbol_;\\n }\\n\\n /**\\n * @dev Returns the name of the token.\\n */\\n function name() public view virtual override returns (string memory) {\\n return _name;\\n }\\n\\n /**\\n * @dev Returns the symbol of the token, usually a shorter version of the\\n * name.\\n */\\n function symbol() public view virtual override returns (string memory) {\\n return _symbol;\\n }\\n\\n /**\\n * @dev Returns the number of decimals used to get its user representation.\\n * For example, if `decimals` equals `2`, a balance of `505` tokens should\\n * be displayed to a user as `5.05` (`505 / 10 ** 2`).\\n *\\n * Tokens usually opt for a value of 18, imitating the relationship between\\n * Ether and Wei. This is the value {ERC20} uses, unless this function is\\n * overridden;\\n *\\n * NOTE: This information is only used for _display_ purposes: it in\\n * no way affects any of the arithmetic of the contract, including\\n * {IERC20-balanceOf} and {IERC20-transfer}.\\n */\\n function decimals() public view virtual override returns (uint8) {\\n return 18;\\n }\\n\\n /**\\n * @dev See {IERC20-totalSupply}.\\n */\\n function totalSupply() public view virtual override returns (uint256) {\\n return _totalSupply;\\n }\\n\\n /**\\n * @dev See {IERC20-balanceOf}.\\n */\\n function balanceOf(address account) public view virtual override returns (uint256) {\\n return _balances[account];\\n }\\n\\n /**\\n * @dev See {IERC20-transfer}.\\n *\\n * Requirements:\\n *\\n * - `recipient` cannot be the zero address.\\n * - the caller must have a balance of at least `amount`.\\n */\\n function transfer(address recipient, uint256 amount) public virtual override returns (bool) {\\n _transfer(_msgSender(), recipient, amount);\\n return true;\\n }\\n\\n /**\\n * @dev See {IERC20-allowance}.\\n */\\n function allowance(address owner, address spender) public view virtual override returns (uint256) {\\n return _allowances[owner][spender];\\n }\\n\\n /**\\n * @dev See {IERC20-approve}.\\n *\\n * Requirements:\\n *\\n * - `spender` cannot be the zero address.\\n */\\n function approve(address spender, uint256 amount) public virtual override returns (bool) {\\n _approve(_msgSender(), spender, amount);\\n return true;\\n }\\n\\n /**\\n * @dev See {IERC20-transferFrom}.\\n *\\n * Emits an {Approval} event indicating the updated allowance. This is not\\n * required by the EIP. See the note at the beginning of {ERC20}.\\n *\\n * Requirements:\\n *\\n * - `sender` and `recipient` cannot be the zero address.\\n * - `sender` must have a balance of at least `amount`.\\n * - the caller must have allowance for ``sender``'s tokens of at least\\n * `amount`.\\n */\\n function transferFrom(\\n address sender,\\n address recipient,\\n uint256 amount\\n ) public virtual override returns (bool) {\\n _transfer(sender, recipient, amount);\\n\\n uint256 currentAllowance = _allowances[sender][_msgSender()];\\n require(currentAllowance >= amount, \\\"ERC20: transfer amount exceeds allowance\\\");\\n unchecked {\\n _approve(sender, _msgSender(), currentAllowance - amount);\\n }\\n\\n return true;\\n }\\n\\n /**\\n * @dev Atomically increases the allowance granted to `spender` by the caller.\\n *\\n * This is an alternative to {approve} that can be used as a mitigation for\\n * problems described in {IERC20-approve}.\\n *\\n * Emits an {Approval} event indicating the updated allowance.\\n *\\n * Requirements:\\n *\\n * - `spender` cannot be the zero address.\\n */\\n function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) {\\n _approve(_msgSender(), spender, _allowances[_msgSender()][spender] + addedValue);\\n return true;\\n }\\n\\n /**\\n * @dev Atomically decreases the allowance granted to `spender` by the caller.\\n *\\n * This is an alternative to {approve} that can be used as a mitigation for\\n * problems described in {IERC20-approve}.\\n *\\n * Emits an {Approval} event indicating the updated allowance.\\n *\\n * Requirements:\\n *\\n * - `spender` cannot be the zero address.\\n * - `spender` must have allowance for the caller of at least\\n * `subtractedValue`.\\n */\\n function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) {\\n uint256 currentAllowance = _allowances[_msgSender()][spender];\\n require(currentAllowance >= subtractedValue, \\\"ERC20: decreased allowance below zero\\\");\\n unchecked {\\n _approve(_msgSender(), spender, currentAllowance - subtractedValue);\\n }\\n\\n return true;\\n }\\n\\n /**\\n * @dev Moves `amount` of tokens from `sender` to `recipient`.\\n *\\n * This internal function is equivalent to {transfer}, and can be used to\\n * e.g. implement automatic token fees, slashing mechanisms, etc.\\n *\\n * Emits a {Transfer} event.\\n *\\n * Requirements:\\n *\\n * - `sender` cannot be the zero address.\\n * - `recipient` cannot be the zero address.\\n * - `sender` must have a balance of at least `amount`.\\n */\\n function _transfer(\\n address sender,\\n address recipient,\\n uint256 amount\\n ) internal virtual {\\n require(sender != address(0), \\\"ERC20: transfer from the zero address\\\");\\n require(recipient != address(0), \\\"ERC20: transfer to the zero address\\\");\\n\\n _beforeTokenTransfer(sender, recipient, amount);\\n\\n uint256 senderBalance = _balances[sender];\\n require(senderBalance >= amount, \\\"ERC20: transfer amount exceeds balance\\\");\\n unchecked {\\n _balances[sender] = senderBalance - amount;\\n }\\n _balances[recipient] += amount;\\n\\n emit Transfer(sender, recipient, amount);\\n\\n _afterTokenTransfer(sender, recipient, amount);\\n }\\n\\n /** @dev Creates `amount` tokens and assigns them to `account`, increasing\\n * the total supply.\\n *\\n * Emits a {Transfer} event with `from` set to the zero address.\\n *\\n * Requirements:\\n *\\n * - `account` cannot be the zero address.\\n */\\n function _mint(address account, uint256 amount) internal virtual {\\n require(account != address(0), \\\"ERC20: mint to the zero address\\\");\\n\\n _beforeTokenTransfer(address(0), account, amount);\\n\\n _totalSupply += amount;\\n _balances[account] += amount;\\n emit Transfer(address(0), account, amount);\\n\\n _afterTokenTransfer(address(0), account, amount);\\n }\\n\\n /**\\n * @dev Destroys `amount` tokens from `account`, reducing the\\n * total supply.\\n *\\n * Emits a {Transfer} event with `to` set to the zero address.\\n *\\n * Requirements:\\n *\\n * - `account` cannot be the zero address.\\n * - `account` must have at least `amount` tokens.\\n */\\n function _burn(address account, uint256 amount) internal virtual {\\n require(account != address(0), \\\"ERC20: burn from the zero address\\\");\\n\\n _beforeTokenTransfer(account, address(0), amount);\\n\\n uint256 accountBalance = _balances[account];\\n require(accountBalance >= amount, \\\"ERC20: burn amount exceeds balance\\\");\\n unchecked {\\n _balances[account] = accountBalance - amount;\\n }\\n _totalSupply -= amount;\\n\\n emit Transfer(account, address(0), amount);\\n\\n _afterTokenTransfer(account, address(0), amount);\\n }\\n\\n /**\\n * @dev Sets `amount` as the allowance of `spender` over the `owner` s tokens.\\n *\\n * This internal function is equivalent to `approve`, and can be used to\\n * e.g. set automatic allowances for certain subsystems, etc.\\n *\\n * Emits an {Approval} event.\\n *\\n * Requirements:\\n *\\n * - `owner` cannot be the zero address.\\n * - `spender` cannot be the zero address.\\n */\\n function _approve(\\n address owner,\\n address spender,\\n uint256 amount\\n ) internal virtual {\\n require(owner != address(0), \\\"ERC20: approve from the zero address\\\");\\n require(spender != address(0), \\\"ERC20: approve to the zero address\\\");\\n\\n _allowances[owner][spender] = amount;\\n emit Approval(owner, spender, amount);\\n }\\n\\n /**\\n * @dev Hook that is called before any transfer of tokens. This includes\\n * minting and burning.\\n *\\n * Calling conditions:\\n *\\n * - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens\\n * will be transferred to `to`.\\n * - when `from` is zero, `amount` tokens will be minted for `to`.\\n * - when `to` is zero, `amount` of ``from``'s tokens will be burned.\\n * - `from` and `to` are never both zero.\\n *\\n * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].\\n */\\n function _beforeTokenTransfer(\\n address from,\\n address to,\\n uint256 amount\\n ) internal virtual {}\\n\\n /**\\n * @dev Hook that is called after any transfer of tokens. This includes\\n * minting and burning.\\n *\\n * Calling conditions:\\n *\\n * - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens\\n * has been transferred to `to`.\\n * - when `from` is zero, `amount` tokens have been minted for `to`.\\n * - when `to` is zero, `amount` of ``from``'s tokens have been burned.\\n * - `from` and `to` are never both zero.\\n *\\n * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].\\n */\\n function _afterTokenTransfer(\\n address from,\\n address to,\\n uint256 amount\\n ) internal virtual {}\\n}\\n\",\"keccak256\":\"0x53a0bb51b8a505e04aaf065de27c0e31cadf38194f8a9a6ec92b7bcd3c5826e6\",\"license\":\"MIT\"},\"@openzeppelin/contracts/token/ERC20/IERC20.sol\":{\"content\":\"// SPDX-License-Identifier: MIT\\n// OpenZeppelin Contracts v4.4.0 (token/ERC20/IERC20.sol)\\n\\npragma solidity ^0.8.0;\\n\\n/**\\n * @dev Interface of the ERC20 standard as defined in the EIP.\\n */\\ninterface IERC20 {\\n /**\\n * @dev Returns the amount of tokens in existence.\\n */\\n function totalSupply() external view returns (uint256);\\n\\n /**\\n * @dev Returns the amount of tokens owned by `account`.\\n */\\n function balanceOf(address account) external view returns (uint256);\\n\\n /**\\n * @dev Moves `amount` tokens from the caller's account to `recipient`.\\n *\\n * Returns a boolean value indicating whether the operation succeeded.\\n *\\n * Emits a {Transfer} event.\\n */\\n function transfer(address recipient, uint256 amount) external returns (bool);\\n\\n /**\\n * @dev Returns the remaining number of tokens that `spender` will be\\n * allowed to spend on behalf of `owner` through {transferFrom}. This is\\n * zero by default.\\n *\\n * This value changes when {approve} or {transferFrom} are called.\\n */\\n function allowance(address owner, address spender) external view returns (uint256);\\n\\n /**\\n * @dev Sets `amount` as the allowance of `spender` over the caller's tokens.\\n *\\n * Returns a boolean value indicating whether the operation succeeded.\\n *\\n * IMPORTANT: Beware that changing an allowance with this method brings the risk\\n * that someone may use both the old and the new allowance by unfortunate\\n * transaction ordering. One possible solution to mitigate this race\\n * condition is to first reduce the spender's allowance to 0 and set the\\n * desired value afterwards:\\n * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729\\n *\\n * Emits an {Approval} event.\\n */\\n function approve(address spender, uint256 amount) external returns (bool);\\n\\n /**\\n * @dev Moves `amount` tokens from `sender` to `recipient` using the\\n * allowance mechanism. `amount` is then deducted from the caller's\\n * allowance.\\n *\\n * Returns a boolean value indicating whether the operation succeeded.\\n *\\n * Emits a {Transfer} event.\\n */\\n function transferFrom(\\n address sender,\\n address recipient,\\n uint256 amount\\n ) external returns (bool);\\n\\n /**\\n * @dev Emitted when `value` tokens are moved from one account (`from`) to\\n * another (`to`).\\n *\\n * Note that `value` may be zero.\\n */\\n event Transfer(address indexed from, address indexed to, uint256 value);\\n\\n /**\\n * @dev Emitted when the allowance of a `spender` for an `owner` is set by\\n * a call to {approve}. `value` is the new allowance.\\n */\\n event Approval(address indexed owner, address indexed spender, uint256 value);\\n}\\n\",\"keccak256\":\"0xc1452b054778f1926419196ef12ae200758a4ee728df69ae1cd13e5c16ca7df7\",\"license\":\"MIT\"},\"@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol\":{\"content\":\"// SPDX-License-Identifier: MIT\\n// OpenZeppelin Contracts v4.4.0 (token/ERC20/extensions/IERC20Metadata.sol)\\n\\npragma solidity ^0.8.0;\\n\\nimport \\\"../IERC20.sol\\\";\\n\\n/**\\n * @dev Interface for the optional metadata functions from the ERC20 standard.\\n *\\n * _Available since v4.1._\\n */\\ninterface IERC20Metadata is IERC20 {\\n /**\\n * @dev Returns the name of the token.\\n */\\n function name() external view returns (string memory);\\n\\n /**\\n * @dev Returns the symbol of the token.\\n */\\n function symbol() external view returns (string memory);\\n\\n /**\\n * @dev Returns the decimals places of the token.\\n */\\n function decimals() external view returns (uint8);\\n}\\n\",\"keccak256\":\"0x842c66d5965ed0bf77f274732c2a93a7e2223d53171ec9cccc473bde75104ead\",\"license\":\"MIT\"},\"@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol\":{\"content\":\"// SPDX-License-Identifier: MIT\\n// OpenZeppelin Contracts v4.4.0 (token/ERC20/utils/SafeERC20.sol)\\n\\npragma solidity ^0.8.0;\\n\\nimport \\\"../IERC20.sol\\\";\\nimport \\\"../../../utils/Address.sol\\\";\\n\\n/**\\n * @title SafeERC20\\n * @dev Wrappers around ERC20 operations that throw on failure (when the token\\n * contract returns false). Tokens that return no value (and instead revert or\\n * throw on failure) are also supported, non-reverting calls are assumed to be\\n * successful.\\n * To use this library you can add a `using SafeERC20 for IERC20;` statement to your contract,\\n * which allows you to call the safe operations as `token.safeTransfer(...)`, etc.\\n */\\nlibrary SafeERC20 {\\n using Address for address;\\n\\n function safeTransfer(\\n IERC20 token,\\n address to,\\n uint256 value\\n ) internal {\\n _callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));\\n }\\n\\n function safeTransferFrom(\\n IERC20 token,\\n address from,\\n address to,\\n uint256 value\\n ) internal {\\n _callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value));\\n }\\n\\n /**\\n * @dev Deprecated. This function has issues similar to the ones found in\\n * {IERC20-approve}, and its usage is discouraged.\\n *\\n * Whenever possible, use {safeIncreaseAllowance} and\\n * {safeDecreaseAllowance} instead.\\n */\\n function safeApprove(\\n IERC20 token,\\n address spender,\\n uint256 value\\n ) internal {\\n // safeApprove should only be called when setting an initial allowance,\\n // or when resetting it to zero. To increase and decrease it, use\\n // 'safeIncreaseAllowance' and 'safeDecreaseAllowance'\\n require(\\n (value == 0) || (token.allowance(address(this), spender) == 0),\\n \\\"SafeERC20: approve from non-zero to non-zero allowance\\\"\\n );\\n _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));\\n }\\n\\n function safeIncreaseAllowance(\\n IERC20 token,\\n address spender,\\n uint256 value\\n ) internal {\\n uint256 newAllowance = token.allowance(address(this), spender) + value;\\n _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));\\n }\\n\\n function safeDecreaseAllowance(\\n IERC20 token,\\n address spender,\\n uint256 value\\n ) internal {\\n unchecked {\\n uint256 oldAllowance = token.allowance(address(this), spender);\\n require(oldAllowance >= value, \\\"SafeERC20: decreased allowance below zero\\\");\\n uint256 newAllowance = oldAllowance - value;\\n _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));\\n }\\n }\\n\\n /**\\n * @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement\\n * on the return value: the return value is optional (but if data is returned, it must not be false).\\n * @param token The token targeted by the call.\\n * @param data The call data (encoded using abi.encode or one of its variants).\\n */\\n function _callOptionalReturn(IERC20 token, bytes memory data) private {\\n // We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since\\n // we're implementing it ourselves. We use {Address.functionCall} to perform this call, which verifies that\\n // the target address contains contract code and also asserts for success in the low-level call.\\n\\n bytes memory returndata = address(token).functionCall(data, \\\"SafeERC20: low-level call failed\\\");\\n if (returndata.length > 0) {\\n // Return data is optional\\n require(abi.decode(returndata, (bool)), \\\"SafeERC20: ERC20 operation did not succeed\\\");\\n }\\n }\\n}\\n\",\"keccak256\":\"0x671741933530f343f023a40e58e61bc09d62494b96c6f3e39e647f315facd519\",\"license\":\"MIT\"},\"@openzeppelin/contracts/utils/Address.sol\":{\"content\":\"// SPDX-License-Identifier: MIT\\n// OpenZeppelin Contracts v4.4.0 (utils/Address.sol)\\n\\npragma solidity ^0.8.0;\\n\\n/**\\n * @dev Collection of functions related to the address type\\n */\\nlibrary Address {\\n /**\\n * @dev Returns true if `account` is a contract.\\n *\\n * [IMPORTANT]\\n * ====\\n * It is unsafe to assume that an address for which this function returns\\n * false is an externally-owned account (EOA) and not a contract.\\n *\\n * Among others, `isContract` will return false for the following\\n * types of addresses:\\n *\\n * - an externally-owned account\\n * - a contract in construction\\n * - an address where a contract will be created\\n * - an address where a contract lived, but was destroyed\\n * ====\\n */\\n function isContract(address account) internal view returns (bool) {\\n // This method relies on extcodesize, which returns 0 for contracts in\\n // construction, since the code is only stored at the end of the\\n // constructor execution.\\n\\n uint256 size;\\n assembly {\\n size := extcodesize(account)\\n }\\n return size > 0;\\n }\\n\\n /**\\n * @dev Replacement for Solidity's `transfer`: sends `amount` wei to\\n * `recipient`, forwarding all available gas and reverting on errors.\\n *\\n * https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost\\n * of certain opcodes, possibly making contracts go over the 2300 gas limit\\n * imposed by `transfer`, making them unable to receive funds via\\n * `transfer`. {sendValue} removes this limitation.\\n *\\n * https://diligence.consensys.net/posts/2019/09/stop-using-soliditys-transfer-now/[Learn more].\\n *\\n * IMPORTANT: because control is transferred to `recipient`, care must be\\n * taken to not create reentrancy vulnerabilities. Consider using\\n * {ReentrancyGuard} or the\\n * https://solidity.readthedocs.io/en/v0.5.11/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].\\n */\\n function sendValue(address payable recipient, uint256 amount) internal {\\n require(address(this).balance >= amount, \\\"Address: insufficient balance\\\");\\n\\n (bool success, ) = recipient.call{value: amount}(\\\"\\\");\\n require(success, \\\"Address: unable to send value, recipient may have reverted\\\");\\n }\\n\\n /**\\n * @dev Performs a Solidity function call using a low level `call`. A\\n * plain `call` is an unsafe replacement for a function call: use this\\n * function instead.\\n *\\n * If `target` reverts with a revert reason, it is bubbled up by this\\n * function (like regular Solidity function calls).\\n *\\n * Returns the raw returned data. To convert to the expected return value,\\n * use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`].\\n *\\n * Requirements:\\n *\\n * - `target` must be a contract.\\n * - calling `target` with `data` must not revert.\\n *\\n * _Available since v3.1._\\n */\\n function functionCall(address target, bytes memory data) internal returns (bytes memory) {\\n return functionCall(target, data, \\\"Address: low-level call failed\\\");\\n }\\n\\n /**\\n * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], but with\\n * `errorMessage` as a fallback revert reason when `target` reverts.\\n *\\n * _Available since v3.1._\\n */\\n function functionCall(\\n address target,\\n bytes memory data,\\n string memory errorMessage\\n ) internal returns (bytes memory) {\\n return functionCallWithValue(target, data, 0, errorMessage);\\n }\\n\\n /**\\n * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],\\n * but also transferring `value` wei to `target`.\\n *\\n * Requirements:\\n *\\n * - the calling contract must have an ETH balance of at least `value`.\\n * - the called Solidity function must be `payable`.\\n *\\n * _Available since v3.1._\\n */\\n function functionCallWithValue(\\n address target,\\n bytes memory data,\\n uint256 value\\n ) internal returns (bytes memory) {\\n return functionCallWithValue(target, data, value, \\\"Address: low-level call with value failed\\\");\\n }\\n\\n /**\\n * @dev Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[`functionCallWithValue`], but\\n * with `errorMessage` as a fallback revert reason when `target` reverts.\\n *\\n * _Available since v3.1._\\n */\\n function functionCallWithValue(\\n address target,\\n bytes memory data,\\n uint256 value,\\n string memory errorMessage\\n ) internal returns (bytes memory) {\\n require(address(this).balance >= value, \\\"Address: insufficient balance for call\\\");\\n require(isContract(target), \\\"Address: call to non-contract\\\");\\n\\n (bool success, bytes memory returndata) = target.call{value: value}(data);\\n return verifyCallResult(success, returndata, errorMessage);\\n }\\n\\n /**\\n * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],\\n * but performing a static call.\\n *\\n * _Available since v3.3._\\n */\\n function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {\\n return functionStaticCall(target, data, \\\"Address: low-level static call failed\\\");\\n }\\n\\n /**\\n * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],\\n * but performing a static call.\\n *\\n * _Available since v3.3._\\n */\\n function functionStaticCall(\\n address target,\\n bytes memory data,\\n string memory errorMessage\\n ) internal view returns (bytes memory) {\\n require(isContract(target), \\\"Address: static call to non-contract\\\");\\n\\n (bool success, bytes memory returndata) = target.staticcall(data);\\n return verifyCallResult(success, returndata, errorMessage);\\n }\\n\\n /**\\n * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],\\n * but performing a delegate call.\\n *\\n * _Available since v3.4._\\n */\\n function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) {\\n return functionDelegateCall(target, data, \\\"Address: low-level delegate call failed\\\");\\n }\\n\\n /**\\n * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],\\n * but performing a delegate call.\\n *\\n * _Available since v3.4._\\n */\\n function functionDelegateCall(\\n address target,\\n bytes memory data,\\n string memory errorMessage\\n ) internal returns (bytes memory) {\\n require(isContract(target), \\\"Address: delegate call to non-contract\\\");\\n\\n (bool success, bytes memory returndata) = target.delegatecall(data);\\n return verifyCallResult(success, returndata, errorMessage);\\n }\\n\\n /**\\n * @dev Tool to verifies that a low level call was successful, and revert if it wasn't, either by bubbling the\\n * revert reason using the provided one.\\n *\\n * _Available since v4.3._\\n */\\n function verifyCallResult(\\n bool success,\\n bytes memory returndata,\\n string memory errorMessage\\n ) internal pure returns (bytes memory) {\\n if (success) {\\n return returndata;\\n } else {\\n // Look for revert reason and bubble it up if present\\n if (returndata.length > 0) {\\n // The easiest way to bubble the revert reason is using memory via assembly\\n\\n assembly {\\n let returndata_size := mload(returndata)\\n revert(add(32, returndata), returndata_size)\\n }\\n } else {\\n revert(errorMessage);\\n }\\n }\\n }\\n}\\n\",\"keccak256\":\"0x9944d1038f27dcebff810d7ba16b3b8058b967173d76874fb72dd7cd84129656\",\"license\":\"MIT\"},\"@openzeppelin/contracts/utils/Context.sol\":{\"content\":\"// SPDX-License-Identifier: MIT\\n// OpenZeppelin Contracts v4.4.0 (utils/Context.sol)\\n\\npragma solidity ^0.8.0;\\n\\n/**\\n * @dev Provides information about the current execution context, including the\\n * sender of the transaction and its data. While these are generally available\\n * via msg.sender and msg.data, they should not be accessed in such a direct\\n * manner, since when dealing with meta-transactions the account sending and\\n * paying for execution may not be the actual sender (as far as an application\\n * is concerned).\\n *\\n * This contract is only required for intermediate, library-like contracts.\\n */\\nabstract contract Context {\\n function _msgSender() internal view virtual returns (address) {\\n return msg.sender;\\n }\\n\\n function _msgData() internal view virtual returns (bytes calldata) {\\n return msg.data;\\n }\\n}\\n\",\"keccak256\":\"0x7736c187e6f1358c1ea9350a2a21aa8528dec1c2f43b374a9067465a3a51f5d3\",\"license\":\"MIT\"},\"@openzeppelin/contracts/utils/math/Math.sol\":{\"content\":\"// SPDX-License-Identifier: MIT\\n// OpenZeppelin Contracts v4.4.0 (utils/math/Math.sol)\\n\\npragma solidity ^0.8.0;\\n\\n/**\\n * @dev Standard math utilities missing in the Solidity language.\\n */\\nlibrary Math {\\n /**\\n * @dev Returns the largest of two numbers.\\n */\\n function max(uint256 a, uint256 b) internal pure returns (uint256) {\\n return a >= b ? a : b;\\n }\\n\\n /**\\n * @dev Returns the smallest of two numbers.\\n */\\n function min(uint256 a, uint256 b) internal pure returns (uint256) {\\n return a < b ? a : b;\\n }\\n\\n /**\\n * @dev Returns the average of two numbers. The result is rounded towards\\n * zero.\\n */\\n function average(uint256 a, uint256 b) internal pure returns (uint256) {\\n // (a + b) / 2 can overflow.\\n return (a & b) + (a ^ b) / 2;\\n }\\n\\n /**\\n * @dev Returns the ceiling of the division of two numbers.\\n *\\n * This differs from standard division with `/` in that it rounds up instead\\n * of rounding down.\\n */\\n function ceilDiv(uint256 a, uint256 b) internal pure returns (uint256) {\\n // (a + b - 1) / b can overflow on addition, so we distribute.\\n return a / b + (a % b == 0 ? 0 : 1);\\n }\\n}\\n\",\"keccak256\":\"0xe936fc79332de2ca7b1c06a70f81345aa2466958aab00f463e312ca0585e85cf\",\"license\":\"MIT\"},\"@openzeppelin/contracts/utils/math/SafeCast.sol\":{\"content\":\"// SPDX-License-Identifier: MIT\\n// OpenZeppelin Contracts v4.4.0 (utils/math/SafeCast.sol)\\n\\npragma solidity ^0.8.0;\\n\\n/**\\n * @dev Wrappers over Solidity's uintXX/intXX casting operators with added overflow\\n * checks.\\n *\\n * Downcasting from uint256/int256 in Solidity does not revert on overflow. This can\\n * easily result in undesired exploitation or bugs, since developers usually\\n * assume that overflows raise errors. `SafeCast` restores this intuition by\\n * reverting the transaction when such an operation overflows.\\n *\\n * Using this library instead of the unchecked operations eliminates an entire\\n * class of bugs, so it's recommended to use it always.\\n *\\n * Can be combined with {SafeMath} and {SignedSafeMath} to extend it to smaller types, by performing\\n * all math on `uint256` and `int256` and then downcasting.\\n */\\nlibrary SafeCast {\\n /**\\n * @dev Returns the downcasted uint224 from uint256, reverting on\\n * overflow (when the input is greater than largest uint224).\\n *\\n * Counterpart to Solidity's `uint224` operator.\\n *\\n * Requirements:\\n *\\n * - input must fit into 224 bits\\n */\\n function toUint224(uint256 value) internal pure returns (uint224) {\\n require(value <= type(uint224).max, \\\"SafeCast: value doesn't fit in 224 bits\\\");\\n return uint224(value);\\n }\\n\\n /**\\n * @dev Returns the downcasted uint128 from uint256, reverting on\\n * overflow (when the input is greater than largest uint128).\\n *\\n * Counterpart to Solidity's `uint128` operator.\\n *\\n * Requirements:\\n *\\n * - input must fit into 128 bits\\n */\\n function toUint128(uint256 value) internal pure returns (uint128) {\\n require(value <= type(uint128).max, \\\"SafeCast: value doesn't fit in 128 bits\\\");\\n return uint128(value);\\n }\\n\\n /**\\n * @dev Returns the downcasted uint96 from uint256, reverting on\\n * overflow (when the input is greater than largest uint96).\\n *\\n * Counterpart to Solidity's `uint96` operator.\\n *\\n * Requirements:\\n *\\n * - input must fit into 96 bits\\n */\\n function toUint96(uint256 value) internal pure returns (uint96) {\\n require(value <= type(uint96).max, \\\"SafeCast: value doesn't fit in 96 bits\\\");\\n return uint96(value);\\n }\\n\\n /**\\n * @dev Returns the downcasted uint64 from uint256, reverting on\\n * overflow (when the input is greater than largest uint64).\\n *\\n * Counterpart to Solidity's `uint64` operator.\\n *\\n * Requirements:\\n *\\n * - input must fit into 64 bits\\n */\\n function toUint64(uint256 value) internal pure returns (uint64) {\\n require(value <= type(uint64).max, \\\"SafeCast: value doesn't fit in 64 bits\\\");\\n return uint64(value);\\n }\\n\\n /**\\n * @dev Returns the downcasted uint32 from uint256, reverting on\\n * overflow (when the input is greater than largest uint32).\\n *\\n * Counterpart to Solidity's `uint32` operator.\\n *\\n * Requirements:\\n *\\n * - input must fit into 32 bits\\n */\\n function toUint32(uint256 value) internal pure returns (uint32) {\\n require(value <= type(uint32).max, \\\"SafeCast: value doesn't fit in 32 bits\\\");\\n return uint32(value);\\n }\\n\\n /**\\n * @dev Returns the downcasted uint16 from uint256, reverting on\\n * overflow (when the input is greater than largest uint16).\\n *\\n * Counterpart to Solidity's `uint16` operator.\\n *\\n * Requirements:\\n *\\n * - input must fit into 16 bits\\n */\\n function toUint16(uint256 value) internal pure returns (uint16) {\\n require(value <= type(uint16).max, \\\"SafeCast: value doesn't fit in 16 bits\\\");\\n return uint16(value);\\n }\\n\\n /**\\n * @dev Returns the downcasted uint8 from uint256, reverting on\\n * overflow (when the input is greater than largest uint8).\\n *\\n * Counterpart to Solidity's `uint8` operator.\\n *\\n * Requirements:\\n *\\n * - input must fit into 8 bits.\\n */\\n function toUint8(uint256 value) internal pure returns (uint8) {\\n require(value <= type(uint8).max, \\\"SafeCast: value doesn't fit in 8 bits\\\");\\n return uint8(value);\\n }\\n\\n /**\\n * @dev Converts a signed int256 into an unsigned uint256.\\n *\\n * Requirements:\\n *\\n * - input must be greater than or equal to 0.\\n */\\n function toUint256(int256 value) internal pure returns (uint256) {\\n require(value >= 0, \\\"SafeCast: value must be positive\\\");\\n return uint256(value);\\n }\\n\\n /**\\n * @dev Returns the downcasted int128 from int256, reverting on\\n * overflow (when the input is less than smallest int128 or\\n * greater than largest int128).\\n *\\n * Counterpart to Solidity's `int128` operator.\\n *\\n * Requirements:\\n *\\n * - input must fit into 128 bits\\n *\\n * _Available since v3.1._\\n */\\n function toInt128(int256 value) internal pure returns (int128) {\\n require(value >= type(int128).min && value <= type(int128).max, \\\"SafeCast: value doesn't fit in 128 bits\\\");\\n return int128(value);\\n }\\n\\n /**\\n * @dev Returns the downcasted int64 from int256, reverting on\\n * overflow (when the input is less than smallest int64 or\\n * greater than largest int64).\\n *\\n * Counterpart to Solidity's `int64` operator.\\n *\\n * Requirements:\\n *\\n * - input must fit into 64 bits\\n *\\n * _Available since v3.1._\\n */\\n function toInt64(int256 value) internal pure returns (int64) {\\n require(value >= type(int64).min && value <= type(int64).max, \\\"SafeCast: value doesn't fit in 64 bits\\\");\\n return int64(value);\\n }\\n\\n /**\\n * @dev Returns the downcasted int32 from int256, reverting on\\n * overflow (when the input is less than smallest int32 or\\n * greater than largest int32).\\n *\\n * Counterpart to Solidity's `int32` operator.\\n *\\n * Requirements:\\n *\\n * - input must fit into 32 bits\\n *\\n * _Available since v3.1._\\n */\\n function toInt32(int256 value) internal pure returns (int32) {\\n require(value >= type(int32).min && value <= type(int32).max, \\\"SafeCast: value doesn't fit in 32 bits\\\");\\n return int32(value);\\n }\\n\\n /**\\n * @dev Returns the downcasted int16 from int256, reverting on\\n * overflow (when the input is less than smallest int16 or\\n * greater than largest int16).\\n *\\n * Counterpart to Solidity's `int16` operator.\\n *\\n * Requirements:\\n *\\n * - input must fit into 16 bits\\n *\\n * _Available since v3.1._\\n */\\n function toInt16(int256 value) internal pure returns (int16) {\\n require(value >= type(int16).min && value <= type(int16).max, \\\"SafeCast: value doesn't fit in 16 bits\\\");\\n return int16(value);\\n }\\n\\n /**\\n * @dev Returns the downcasted int8 from int256, reverting on\\n * overflow (when the input is less than smallest int8 or\\n * greater than largest int8).\\n *\\n * Counterpart to Solidity's `int8` operator.\\n *\\n * Requirements:\\n *\\n * - input must fit into 8 bits.\\n *\\n * _Available since v3.1._\\n */\\n function toInt8(int256 value) internal pure returns (int8) {\\n require(value >= type(int8).min && value <= type(int8).max, \\\"SafeCast: value doesn't fit in 8 bits\\\");\\n return int8(value);\\n }\\n\\n /**\\n * @dev Converts an unsigned uint256 into a signed int256.\\n *\\n * Requirements:\\n *\\n * - input must be less than or equal to maxInt256.\\n */\\n function toInt256(uint256 value) internal pure returns (int256) {\\n // Note: Unsafe cast below is okay because `type(int256).max` is guaranteed to be positive\\n require(value <= uint256(type(int256).max), \\\"SafeCast: value doesn't fit in an int256\\\");\\n return int256(value);\\n }\\n}\\n\",\"keccak256\":\"0x47c0131bd8a972c31596958aa86752ea18d60e33f1cd94d412b9e29fd6ab25a6\",\"license\":\"MIT\"},\"contracts/FixedRateSwap.sol\":{\"content\":\"// SPDX-License-Identifier: MIT\\n\\npragma solidity 0.8.10;\\n\\nimport \\\"@openzeppelin/contracts/utils/math/Math.sol\\\";\\nimport \\\"@openzeppelin/contracts/utils/math/SafeCast.sol\\\";\\nimport \\\"@openzeppelin/contracts/token/ERC20/ERC20.sol\\\";\\nimport \\\"@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol\\\";\\nimport \\\"@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol\\\";\\n\\n\\n/**\\n * @dev AMM that is designed for assets with stable price to each other e.g. USDC and USDT.\\n * It utilizes constant sum price curve x + y = const but fee is variable depending on the token balances.\\n * In most cases fee is equal to 1 bip. But when balances are at extreme ends it either lowers to 0\\n * or increases to 20 bip.\\n * Fee calculations are explained in more details in `getReturn` method.\\n * Note that AMM does not support token with fees.\\n * Note that tokens decimals are required to be the same.\\n */\\ncontract FixedRateSwap is ERC20 {\\n using SafeERC20 for IERC20;\\n using SafeCast for uint256;\\n\\n event Swap(\\n address indexed trader,\\n int256 token0Amount,\\n int256 token1Amount\\n );\\n\\n event Deposit(\\n address indexed user,\\n uint256 token0Amount,\\n uint256 token1Amount,\\n uint256 share\\n );\\n\\n event Withdrawal(\\n address indexed user,\\n uint256 token0Amount,\\n uint256 token1Amount,\\n uint256 share\\n );\\n\\n IERC20 immutable public token0;\\n IERC20 immutable public token1;\\n\\n uint8 immutable private _decimals;\\n\\n uint256 constant private _ONE = 1e18;\\n uint256 constant private _C1 = 0.9999e18;\\n uint256 constant private _C2 = 3.382712334998325432e18;\\n uint256 constant private _C3 = 0.456807350974663119e18;\\n uint256 constant private _THRESHOLD = 1;\\n uint256 constant private _LOWER_BOUND_NUMERATOR = 998;\\n uint256 constant private _UPPER_BOUND_NUMERATOR = 1002;\\n uint256 constant private _DENOMINATOR = 1000;\\n\\n constructor(\\n IERC20 _token0,\\n IERC20 _token1,\\n string memory name,\\n string memory symbol,\\n uint8 decimals_\\n )\\n ERC20(name, symbol)\\n {\\n token0 = _token0;\\n token1 = _token1;\\n _decimals = decimals_;\\n require(IERC20Metadata(address(_token0)).decimals() == decimals_, \\\"token0 decimals mismatch\\\");\\n require(IERC20Metadata(address(_token1)).decimals() == decimals_, \\\"token1 decimals mismatch\\\");\\n }\\n\\n function decimals() public view override returns(uint8) {\\n return _decimals;\\n }\\n\\n /**\\n * @notice estimates return value of the swap\\n * @param tokenFrom token that user wants to sell\\n * @param tokenTo token that user wants to buy\\n * @param inputAmount amount of `tokenFrom` that user wants to sell\\n * @return outputAmount amount of `tokenTo` that user will receive after the trade\\n *\\n * @dev\\n * `getReturn` at point `x = inputBalance / (inputBalance + outputBalance)`:\\n * `getReturn(x) = 0.9999 + (0.5817091329374359 - x * 1.2734233188154198)^17`\\n * When balance is changed from `inputBalance` to `inputBalance + amount` we should take\\n * integral of getReturn to calculate proper amount:\\n * `getReturn(x0, x1) = (integral (0.9999 + (0.5817091329374359 - x * 1.2734233188154198)^17) dx from x=x0 to x=x1) / (x1 - x0)`\\n * `getReturn(x0, x1) = (0.9999 * x - 3.3827123349983306 * (x - 0.4568073509746632) ** 18 from x=x0 to x=x1) / (x1 - x0)`\\n * `getReturn(x0, x1) = (0.9999 * (x1 - x0) + 3.3827123349983306 * ((x0 - 0.4568073509746632) ** 18 - (x1 - 0.4568073509746632) ** 18)) / (x1 - x0)`\\n * C0 = 0.9999\\n * C2 = 3.3827123349983306\\n * C3 = 0.4568073509746632\\n * `getReturn(x0, x1) = (C0 * (x1 - x0) + C2 * ((x0 - C3) ** 18 - (x1 - C3) ** 18)) / (x1 - x0)`\\n */\\n function getReturn(IERC20 tokenFrom, IERC20 tokenTo, uint256 inputAmount) public view returns(uint256 outputAmount) {\\n require(inputAmount > 0, \\\"Input amount should be > 0\\\");\\n uint256 fromBalance = tokenFrom.balanceOf(address(this));\\n uint256 toBalance = tokenTo.balanceOf(address(this));\\n // require is needed to be sure that _getReturn math won't overflow\\n require(inputAmount <= toBalance, \\\"Input amount is too big\\\");\\n outputAmount = _getReturn(fromBalance, toBalance, inputAmount);\\n }\\n\\n /**\\n * @notice makes a deposit of both tokens to the AMM\\n * @param token0Amount amount of token0 to deposit\\n * @param token1Amount amount of token1 to deposit\\n * @param minShare minimal required amount of LP tokens received\\n * @return share amount of LP tokens received\\n */\\n function deposit(uint256 token0Amount, uint256 token1Amount, uint256 minShare) external returns(uint256 share) {\\n share = depositFor(token0Amount, token1Amount, msg.sender, minShare);\\n }\\n\\n /**\\n * @notice makes a deposit of both tokens to the AMM and transfers LP tokens to the specified address\\n * @param token0Amount amount of token0 to deposit\\n * @param token1Amount amount of token1 to deposit\\n * @param to address that will receive tokens\\n * @param minShare minimal required amount of LP tokens received\\n * @return share amount of LP tokens received\\n *\\n * @dev fully balanced deposit happens when ratio of amounts of deposit matches ratio of balances.\\n * To make a fair deposit when ratios do not match the contract finds the amount that is needed to swap to\\n * equalize ratios and makes that swap virtually to capture the swap fees. Then final share is calculated from\\n * fair deposit of virtual amounts.\\n */\\n function depositFor(uint256 token0Amount, uint256 token1Amount, address to, uint256 minShare) public returns(uint256 share) {\\n uint256 token0Balance = token0.balanceOf(address(this));\\n uint256 token1Balance = token1.balanceOf(address(this));\\n (uint256 token0VirtualAmount, uint256 token1VirtualAmount) = _getVirtualAmountsForDeposit(token0Amount, token1Amount, token0Balance, token1Balance);\\n\\n uint256 inputAmount = token0VirtualAmount + token1VirtualAmount;\\n require(inputAmount > 0, \\\"Empty deposit is not allowed\\\");\\n require(to != address(this), \\\"Deposit to this is forbidden\\\");\\n // _mint also checks require(to != address(0))\\n\\n uint256 _totalSupply = totalSupply();\\n if (_totalSupply > 0) {\\n uint256 totalBalance = token0Balance + token1Balance + token0Amount + token1Amount - inputAmount;\\n share = inputAmount * _totalSupply / totalBalance;\\n } else {\\n share = inputAmount;\\n }\\n\\n require(share >= minShare, \\\"Share is not enough\\\");\\n _mint(to, share);\\n emit Deposit(to, token0Amount, token1Amount, share);\\n\\n if (token0Amount > 0) {\\n token0.safeTransferFrom(msg.sender, address(this), token0Amount);\\n }\\n if (token1Amount > 0) {\\n token1.safeTransferFrom(msg.sender, address(this), token1Amount);\\n }\\n }\\n\\n /**\\n * @notice makes a proportional withdrawal of both tokens\\n * @param amount amount of LP tokens to burn\\n * @param minToken0Amount minimal required amount of token0\\n * @param minToken1Amount minimal required amount of token1\\n * @return token0Amount amount of token0 received\\n * @return token1Amount amount of token1 received\\n */\\n function withdraw(uint256 amount, uint256 minToken0Amount, uint256 minToken1Amount) external returns(uint256 token0Amount, uint256 token1Amount) {\\n (token0Amount, token1Amount) = withdrawFor(amount, msg.sender, minToken0Amount, minToken1Amount);\\n }\\n\\n /**\\n * @notice makes a proportional withdrawal of both tokens and transfers them to the specified address\\n * @param amount amount of LP tokens to burn\\n * @param to address that will receive tokens\\n * @param minToken0Amount minimal required amount of token0\\n * @param minToken1Amount minimal required amount of token1\\n * @return token0Amount amount of token0 received\\n * @return token1Amount amount of token1 received\\n */\\n function withdrawFor(uint256 amount, address to, uint256 minToken0Amount, uint256 minToken1Amount) public returns(uint256 token0Amount, uint256 token1Amount) {\\n require(amount > 0, \\\"Empty withdrawal is not allowed\\\");\\n require(to != address(this), \\\"Withdrawal to this is forbidden\\\");\\n require(to != address(0), \\\"Withdrawal to zero is forbidden\\\");\\n\\n uint256 _totalSupply = totalSupply();\\n _burn(msg.sender, amount);\\n token0Amount = token0.balanceOf(address(this)) * amount / _totalSupply;\\n token1Amount = token1.balanceOf(address(this)) * amount / _totalSupply;\\n _handleWithdraw(to, amount, token0Amount, token1Amount, minToken0Amount, minToken1Amount);\\n }\\n\\n /**\\n * @notice makes a withdrawal with custom ratio\\n * @param amount amount of LP tokens to burn\\n * @param token0Share percentage of token0 to receive with 100% equals to 1e18\\n * @param minToken0Amount minimal required amount of token0\\n * @param minToken1Amount minimal required amount of token1\\n * @return token0Amount amount of token0 received\\n * @return token1Amount amount of token1 received\\n */\\n function withdrawWithRatio(uint256 amount, uint256 token0Share, uint256 minToken0Amount, uint256 minToken1Amount) external returns(uint256 token0Amount, uint256 token1Amount) {\\n return withdrawForWithRatio(amount, msg.sender, token0Share, minToken0Amount, minToken1Amount);\\n }\\n\\n /**\\n * @notice makes a withdrawal with custom ratio and transfers tokens to the specified address\\n * @param amount amount of LP tokens to burn\\n * @param to address that will receive tokens\\n * @param token0Share percentage of token0 to receive with 100% equals to 1e18\\n * @param minToken0Amount minimal required amount of token0\\n * @param minToken1Amount minimal required amount of token1\\n * @return token0Amount amount of token0 received\\n * @return token1Amount amount of token1 received\\n *\\n * @dev withdrawal with custom ratio is semantically equal to proportional withdrawal with extra swap afterwards to\\n * get to the specified ratio. The contract does exactly this by making virtual proportional withdrawal and then\\n * finds the amount needed for an extra virtual swap to achieve specified ratio.\\n */\\n function withdrawForWithRatio(uint256 amount, address to, uint256 token0Share, uint256 minToken0Amount, uint256 minToken1Amount) public returns(uint256 token0Amount, uint256 token1Amount) {\\n require(amount > 0, \\\"Empty withdrawal is not allowed\\\");\\n require(to != address(this), \\\"Withdrawal to this is forbidden\\\");\\n require(to != address(0), \\\"Withdrawal to zero is forbidden\\\");\\n require(token0Share <= _ONE, \\\"Ratio should be in [0, 1]\\\");\\n\\n uint256 _totalSupply = totalSupply();\\n // burn happens before amounts calculations intentionally \\u2014 to validate amount\\n _burn(msg.sender, amount);\\n (token0Amount, token1Amount) = _getRealAmountsForWithdraw(amount, token0Share, _totalSupply);\\n _handleWithdraw(to, amount, token0Amount, token1Amount, minToken0Amount, minToken1Amount);\\n }\\n\\n /**\\n * @notice swaps token0 for token1\\n * @param inputAmount amount of token0 to sell\\n * @param minReturnAmount minimal required amount of token1 to buy\\n * @return outputAmount amount of token1 bought\\n */\\n function swap0To1(uint256 inputAmount, uint256 minReturnAmount) external returns(uint256 outputAmount) {\\n outputAmount = _swap(token0, token1, inputAmount, msg.sender, minReturnAmount);\\n emit Swap(msg.sender, inputAmount.toInt256(), -outputAmount.toInt256());\\n }\\n\\n /**\\n * @notice swaps token1 for token0\\n * @param inputAmount amount of token1 to sell\\n * @param minReturnAmount minimal required amount of token0 to buy\\n * @return outputAmount amount of token0 bought\\n */\\n function swap1To0(uint256 inputAmount, uint256 minReturnAmount) external returns(uint256 outputAmount) {\\n outputAmount = _swap(token1, token0, inputAmount, msg.sender, minReturnAmount);\\n emit Swap(msg.sender, -outputAmount.toInt256(), inputAmount.toInt256());\\n }\\n\\n /**\\n * @notice swaps token0 for token1 and transfers them to specified receiver address\\n * @param inputAmount amount of token0 to sell\\n * @param to address that will receive tokens\\n * @param minReturnAmount minimal required amount of token1 to buy\\n * @return outputAmount amount of token1 bought\\n */\\n function swap0To1For(uint256 inputAmount, address to, uint256 minReturnAmount) external returns(uint256 outputAmount) {\\n require(to != address(this), \\\"Swap to this is forbidden\\\");\\n require(to != address(0), \\\"Swap to zero is forbidden\\\");\\n\\n outputAmount = _swap(token0, token1, inputAmount, to, minReturnAmount);\\n emit Swap(msg.sender, inputAmount.toInt256(), -outputAmount.toInt256());\\n }\\n\\n /**\\n * @notice swaps token1 for token0 and transfers them to specified receiver address\\n * @param inputAmount amount of token1 to sell\\n * @param to address that will receive tokens\\n * @param minReturnAmount minimal required amount of token0 to buy\\n * @return outputAmount amount of token0 bought\\n */\\n function swap1To0For(uint256 inputAmount, address to, uint256 minReturnAmount) external returns(uint256 outputAmount) {\\n require(to != address(this), \\\"Swap to this is forbidden\\\");\\n require(to != address(0), \\\"Swap to zero is forbidden\\\");\\n\\n outputAmount = _swap(token1, token0, inputAmount, to, minReturnAmount);\\n emit Swap(msg.sender, -outputAmount.toInt256(), inputAmount.toInt256());\\n }\\n\\n function _getVirtualAmountsForDeposit(uint256 token0Amount, uint256 token1Amount, uint256 token0Balance, uint256 token1Balance)\\n private pure returns(uint256 token0VirtualAmount, uint256 token1VirtualAmount)\\n {\\n int256 shift = _checkVirtualAmountsFormula(token0Amount, token1Amount, token0Balance, token1Balance);\\n if (shift > 0) {\\n (token0VirtualAmount, token1VirtualAmount) = _getVirtualAmountsForDepositImpl(token0Amount, token1Amount, token0Balance, token1Balance);\\n } else if (shift < 0) {\\n (token1VirtualAmount, token0VirtualAmount) = _getVirtualAmountsForDepositImpl(token1Amount, token0Amount, token1Balance, token0Balance);\\n } else {\\n (token0VirtualAmount, token1VirtualAmount) = (token0Amount, token1Amount);\\n }\\n }\\n\\n function _getRealAmountsForWithdraw(uint256 amount, uint256 token0Share, uint256 _totalSupply) private view returns(uint256 token0RealAmount, uint256 token1RealAmount) {\\n uint256 token0Balance = token0.balanceOf(address(this));\\n uint256 token1Balance = token1.balanceOf(address(this));\\n uint256 token0VirtualAmount = token0Balance * amount / _totalSupply;\\n uint256 token1VirtualAmount = token1Balance * amount / _totalSupply;\\n\\n uint256 currentToken0Share = token0VirtualAmount * _ONE / (token0VirtualAmount + token1VirtualAmount);\\n if (token0Share < currentToken0Share) {\\n (token0RealAmount, token1RealAmount) = _getRealAmountsForWithdrawImpl(token0VirtualAmount, token1VirtualAmount, token0Balance - token0VirtualAmount, token1Balance - token1VirtualAmount, token0Share);\\n } else if (token0Share > currentToken0Share) {\\n (token1RealAmount, token0RealAmount) = _getRealAmountsForWithdrawImpl(token1VirtualAmount, token0VirtualAmount, token1Balance - token1VirtualAmount, token0Balance - token0VirtualAmount, _ONE - token0Share);\\n } else {\\n (token0RealAmount, token1RealAmount) = (token0VirtualAmount, token1VirtualAmount);\\n }\\n }\\n\\n function _getReturn(uint256 fromBalance, uint256 toBalance, uint256 inputAmount) private pure returns(uint256 outputAmount) {\\n unchecked {\\n uint256 totalBalance = fromBalance + toBalance;\\n uint256 x0 = _ONE * fromBalance / totalBalance;\\n uint256 x1 = _ONE * (fromBalance + inputAmount) / totalBalance;\\n uint256 scaledInputAmount = _ONE * inputAmount;\\n uint256 amountMultiplier = (\\n _C1 * scaledInputAmount / totalBalance +\\n _C2 * _powerHelper(x0) -\\n _C2 * _powerHelper(x1)\\n ) * totalBalance / scaledInputAmount;\\n outputAmount = inputAmount * Math.min(amountMultiplier, _ONE) / _ONE;\\n }\\n }\\n\\n function _handleWithdraw(address to, uint256 amount, uint256 token0Amount, uint256 token1Amount, uint256 minToken0Amount, uint256 minToken1Amount) private {\\n require(token0Amount >= minToken0Amount, \\\"Min token0Amount is not reached\\\");\\n require(token1Amount >= minToken1Amount, \\\"Min token1Amount is not reached\\\");\\n\\n emit Withdrawal(msg.sender, token0Amount, token1Amount, amount);\\n if (token0Amount > 0) {\\n token0.safeTransfer(to, token0Amount);\\n }\\n if (token1Amount > 0) {\\n token1.safeTransfer(to, token1Amount);\\n }\\n }\\n\\n function _swap(IERC20 tokenFrom, IERC20 tokenTo, uint256 inputAmount, address to, uint256 minReturnAmount) private returns(uint256 outputAmount) {\\n outputAmount = getReturn(tokenFrom, tokenTo, inputAmount);\\n require(outputAmount > 0, \\\"Empty swap is not allowed\\\");\\n require(outputAmount >= minReturnAmount, \\\"Min return not reached\\\");\\n tokenFrom.safeTransferFrom(msg.sender, address(this), inputAmount);\\n tokenTo.safeTransfer(to, outputAmount);\\n }\\n\\n /**\\n * @dev We utilize binary search to find proper to swap\\n *\\n * Inital approximation of dx is taken from the same equation by assuming dx ~ dy\\n *\\n * x - dx xBalance + dx\\n * ------ = ------------\\n * y + dx yBalance - dx\\n *\\n * dx = (x * yBalance - xBalance * y) / (xBalance + yBalance + x + y)\\n */\\n function _getVirtualAmountsForDepositImpl(uint256 x, uint256 y, uint256 xBalance, uint256 yBalance) private pure returns(uint256, uint256) {\\n uint256 dx = (x * yBalance - y * xBalance) / (xBalance + yBalance + x + y);\\n if (dx == 0) {\\n return (x, y);\\n }\\n uint256 dy;\\n uint256 left = dx * _LOWER_BOUND_NUMERATOR / _DENOMINATOR;\\n uint256 right = Math.min(Math.min(dx * _UPPER_BOUND_NUMERATOR / _DENOMINATOR, yBalance), x);\\n\\n while (left + _THRESHOLD < right) {\\n dy = _getReturn(xBalance, yBalance, dx);\\n int256 shift = _checkVirtualAmountsFormula(x - dx, y + dy, xBalance + dx, yBalance - dy);\\n if (shift > 0) {\\n left = dx;\\n dx = (dx + right) / 2;\\n } else if (shift < 0) {\\n right = dx;\\n dx = (left + dx) / 2;\\n } else {\\n break;\\n }\\n }\\n\\n return (x - dx, y + dy);\\n }\\n\\n /**\\n * @dev We utilize binary search to find proper amount to swap\\n *\\n * Inital approximation of dx is taken from the same equation by assuming dx ~ dy\\n *\\n * x - dx firstTokenShare\\n * ------ = ----------------------\\n * y + dx _ONE - firstTokenShare\\n *\\n * dx = (x * (_ONE - firstTokenShare) - y * firstTokenShare) / _ONE\\n */\\n function _getRealAmountsForWithdrawImpl(uint256 virtualX, uint256 virtualY, uint256 balanceX, uint256 balanceY, uint256 firstTokenShare) private pure returns(uint256, uint256) {\\n require(balanceX != 0 || balanceY != 0, \\\"Amount exceeds total balance\\\");\\n if (firstTokenShare == 0) {\\n return (0, virtualY + _getReturn(balanceX, balanceY, virtualX));\\n }\\n\\n uint256 secondTokenShare = _ONE - firstTokenShare;\\n uint256 dx = (virtualX * secondTokenShare - virtualY * firstTokenShare) / _ONE;\\n uint256 dy;\\n uint256 left = dx * _LOWER_BOUND_NUMERATOR / _DENOMINATOR;\\n uint256 right = Math.min(dx * _UPPER_BOUND_NUMERATOR / _DENOMINATOR, virtualX);\\n\\n while (left + _THRESHOLD < right) {\\n dy = _getReturn(balanceX, balanceY, dx);\\n int256 shift = _checkVirtualAmountsFormula(virtualX - dx, virtualY + dy, firstTokenShare, secondTokenShare);\\n if (shift > 0) {\\n left = dx;\\n dx = (dx + right) / 2;\\n } else if (shift < 0) {\\n right = dx;\\n dx = (left + dx) / 2;\\n } else {\\n break;\\n }\\n }\\n\\n return (virtualX - dx, virtualY + dy);\\n }\\n\\n /**\\n * @dev\\n *\\n * Equilibrium is when ratio of amounts equals to ratio of balances\\n *\\n * x xBalance\\n * --- == ----------\\n * y yBalance\\n *\\n */\\n function _checkVirtualAmountsFormula(uint256 x, uint256 y, uint256 xBalance, uint256 yBalance) private pure returns(int256) {\\n unchecked {\\n return int256(x * yBalance - y * xBalance);\\n }\\n }\\n\\n function _powerHelper(uint256 x) private pure returns(uint256 p) {\\n unchecked {\\n if (x > _C3) {\\n p = x - _C3;\\n } else {\\n p = _C3 - x;\\n }\\n p = p * p / _ONE; // p ^ 2\\n uint256 pp = p * p / _ONE; // p ^ 4\\n pp = pp * pp / _ONE; // p ^ 8\\n pp = pp * pp / _ONE; // p ^ 16\\n p = p * pp / _ONE; // p ^ 18\\n }\\n }\\n}\\n\",\"keccak256\":\"0x41fb07d1babf09440f9110e672a0056354ca4d8059ad904c09f137cdd61f37e0\",\"license\":\"MIT\"}},\"version\":1}", 798 | "bytecode": "0x60e06040523480156200001157600080fd5b50604051620037a6380380620037a68339810160408190526200003491620003a8565b8251839083906200004d90600390602085019062000206565b5080516200006390600490602084019062000206565b5050506001600160a01b03808616608081905290851660a05260ff821660c08190526040805163313ce56760e01b8152905191929163313ce567916004808201926020929091908290030181865afa158015620000c4573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190620000ea91906200044b565b60ff1614620001405760405162461bcd60e51b815260206004820152601860248201527f746f6b656e3020646563696d616c73206d69736d61746368000000000000000060448201526064015b60405180910390fd5b8060ff16846001600160a01b031663313ce5676040518163ffffffff1660e01b8152600401602060405180830381865afa15801562000183573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190620001a991906200044b565b60ff1614620001fb5760405162461bcd60e51b815260206004820152601860248201527f746f6b656e3120646563696d616c73206d69736d617463680000000000000000604482015260640162000137565b5050505050620004ad565b828054620002149062000470565b90600052602060002090601f01602090048101928262000238576000855562000283565b82601f106200025357805160ff191683800117855562000283565b8280016001018555821562000283579182015b828111156200028357825182559160200191906001019062000266565b506200029192915062000295565b5090565b5b8082111562000291576000815560010162000296565b80516001600160a01b0381168114620002c457600080fd5b919050565b634e487b7160e01b600052604160045260246000fd5b600082601f830112620002f157600080fd5b81516001600160401b03808211156200030e576200030e620002c9565b604051601f8301601f19908116603f01168101908282118183101715620003395762000339620002c9565b816040528381526020925086838588010111156200035657600080fd5b600091505b838210156200037a57858201830151818301840152908201906200035b565b838211156200038c5760008385830101525b9695505050505050565b805160ff81168114620002c457600080fd5b600080600080600060a08688031215620003c157600080fd5b620003cc86620002ac565b9450620003dc60208701620002ac565b60408701519094506001600160401b0380821115620003fa57600080fd5b6200040889838a01620002df565b945060608801519150808211156200041f57600080fd5b506200042e88828901620002df565b9250506200043f6080870162000396565b90509295509295909350565b6000602082840312156200045e57600080fd5b620004698262000396565b9392505050565b600181811c908216806200048557607f821691505b60208210811415620004a757634e487b7160e01b600052602260045260246000fd5b50919050565b60805160a05160c05161324b6200055b600039600061027b0152600081816103bb0152818161095201528181610a2001528181610ba001528181610eb101528181610ffd015281816111a9015281816116da015281816123d201526126a20152600081816101ff01528181610973015281816109ff01528181610aec01528181610e690152818161101e01528181611188015281816116110152818161231e015261265b015261324b6000f3fe608060405234801561001057600080fd5b50600436106101975760003560e01c80638882eb62116100e3578063c755836b1161008c578063db5dadbf11610066578063db5dadbf146103dd578063dd62ed3e146103f0578063eac38e0c1461043657600080fd5b8063c755836b14610390578063cdc4e51d146103a3578063d21220a7146103b657600080fd5b8063a457c2d7116100bd578063a457c2d714610357578063a9059cbb1461036a578063c0a535111461037d57600080fd5b80638882eb621461031457806395d89b4114610327578063a41fe49f1461032f57600080fd5b806323b872dd116101455780635a7e9a5f1161011f5780635a7e9a5f146102b857806360336508146102cb57806370a08231146102de57600080fd5b806323b872dd14610261578063313ce5671461027457806339509351146102a557600080fd5b80630dfe1681116101765780630dfe1681146101fa57806318160ddd146102465780631e1401f81461024e57600080fd5b8062aeef8a1461019c57806306fdde03146101c2578063095ea7b3146101d7575b600080fd5b6101af6101aa366004612d5c565b610449565b6040519081526020015b60405180910390f35b6101ca61045f565b6040516101b99190612db4565b6101ea6101e5366004612e2a565b6104f1565b60405190151581526020016101b9565b6102217f000000000000000000000000000000000000000000000000000000000000000081565b60405173ffffffffffffffffffffffffffffffffffffffff90911681526020016101b9565b6002546101af565b6101af61025c366004612e56565b610507565b6101ea61026f366004612e56565b61071d565b60405160ff7f00000000000000000000000000000000000000000000000000000000000000001681526020016101b9565b6101ea6102b3366004612e2a565b610805565b6101af6102c6366004612e97565b61084e565b6101af6102d9366004612ebe565b6109f8565b6101af6102ec366004612ee0565b73ffffffffffffffffffffffffffffffffffffffff1660009081526020819052604090205490565b6101af610322366004612efd565b610aa4565b6101ca610ee7565b61034261033d366004612d5c565b610ef6565b604080519283526020830191909152016101b9565b6101ea610365366004612e2a565b610f11565b6101ea610378366004612e2a565b610fe9565b6101af61038b366004612ebe565b610ff6565b6101af61039e366004612e97565b611084565b6103426103b1366004612f3c565b61120f565b6102217f000000000000000000000000000000000000000000000000000000000000000081565b6103426103eb366004612f84565b61142b565b6101af6103fe366004612fb6565b73ffffffffffffffffffffffffffffffffffffffff918216600090815260016020908152604080832093909416825291909152205490565b610342610444366004612fef565b611449565b600061045784843385610aa4565b949350505050565b60606003805461046e9061302c565b80601f016020809104026020016040519081016040528092919081815260200182805461049a9061302c565b80156104e75780601f106104bc576101008083540402835291602001916104e7565b820191906000526020600020905b8154815290600101906020018083116104ca57829003601f168201915b5050505050905090565b60006104fe338484611773565b50600192915050565b6000808211610577576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601a60248201527f496e70757420616d6f756e742073686f756c64206265203e203000000000000060448201526064015b60405180910390fd5b6040517f70a0823100000000000000000000000000000000000000000000000000000000815230600482015260009073ffffffffffffffffffffffffffffffffffffffff8616906370a0823190602401602060405180830381865afa1580156105e4573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906106089190613080565b6040517f70a0823100000000000000000000000000000000000000000000000000000000815230600482015290915060009073ffffffffffffffffffffffffffffffffffffffff8616906370a0823190602401602060405180830381865afa158015610678573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061069c9190613080565b905080841115610708576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601760248201527f496e70757420616d6f756e7420697320746f6f20626967000000000000000000604482015260640161056e565b610713828286611927565b9695505050505050565b600061072a848484611a03565b73ffffffffffffffffffffffffffffffffffffffff84166000908152600160209081526040808320338452909152902054828110156107eb576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602860248201527f45524332303a207472616e7366657220616d6f756e742065786365656473206160448201527f6c6c6f77616e6365000000000000000000000000000000000000000000000000606482015260840161056e565b6107f88533858403611773565b60019150505b9392505050565b33600081815260016020908152604080832073ffffffffffffffffffffffffffffffffffffffff8716845290915281205490916104fe9185906108499086906130c8565b611773565b600073ffffffffffffffffffffffffffffffffffffffff83163014156108d0576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601960248201527f5377617020746f207468697320697320666f7262696464656e00000000000000604482015260640161056e565b73ffffffffffffffffffffffffffffffffffffffff831661094d576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601960248201527f5377617020746f207a65726f20697320666f7262696464656e00000000000000604482015260640161056e565b61099a7f00000000000000000000000000000000000000000000000000000000000000007f0000000000000000000000000000000000000000000000000000000000000000868686611cb8565b9050337f803540962ed9acbf87226c32486d71e1c86c2bdb208e771bab2fd8a626f61e896109c783611de7565b6109d0906130e0565b6109d987611de7565b6040805192835260208301919091520160405180910390a29392505050565b6000610a477f00000000000000000000000000000000000000000000000000000000000000007f0000000000000000000000000000000000000000000000000000000000000000853386611cb8565b9050337f803540962ed9acbf87226c32486d71e1c86c2bdb208e771bab2fd8a626f61e89610a7485611de7565b610a7d84611de7565b610a86906130e0565b6040805192835260208301919091520160405180910390a292915050565b6040517f70a08231000000000000000000000000000000000000000000000000000000008152306004820152600090819073ffffffffffffffffffffffffffffffffffffffff7f000000000000000000000000000000000000000000000000000000000000000016906370a0823190602401602060405180830381865afa158015610b33573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610b579190613080565b6040517f70a0823100000000000000000000000000000000000000000000000000000000815230600482015290915060009073ffffffffffffffffffffffffffffffffffffffff7f000000000000000000000000000000000000000000000000000000000000000016906370a0823190602401602060405180830381865afa158015610be7573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610c0b9190613080565b9050600080610c1c89898686611e9d565b90925090506000610c2d82846130c8565b905060008111610c99576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601c60248201527f456d707479206465706f736974206973206e6f7420616c6c6f77656400000000604482015260640161056e565b73ffffffffffffffffffffffffffffffffffffffff8816301415610d19576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601c60248201527f4465706f73697420746f207468697320697320666f7262696464656e00000000604482015260640161056e565b6000610d2460025490565b90508015610d78576000828b8d610d3b898b6130c8565b610d4591906130c8565b610d4f91906130c8565b610d599190613119565b905080610d668385613130565b610d70919061319c565b975050610d7c565b8196505b87871015610de6576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601360248201527f5368617265206973206e6f7420656e6f75676800000000000000000000000000604482015260640161056e565b610df08988611eee565b604080518c8152602081018c905290810188905273ffffffffffffffffffffffffffffffffffffffff8a16907f36af321ec8d3c75236829c5317affd40ddb308863a1236d2d277a4025cccee1e9060600160405180910390a28a15610e9157610e9173ffffffffffffffffffffffffffffffffffffffff7f00000000000000000000000000000000000000000000000000000000000000001633308e61200e565b8915610ed957610ed973ffffffffffffffffffffffffffffffffffffffff7f00000000000000000000000000000000000000000000000000000000000000001633308d61200e565b505050505050949350505050565b60606004805461046e9061302c565b600080610f0585338686611449565b90969095509350505050565b33600090815260016020908152604080832073ffffffffffffffffffffffffffffffffffffffff8616845290915281205482811015610fd2576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602560248201527f45524332303a2064656372656173656420616c6c6f77616e63652062656c6f7760448201527f207a65726f000000000000000000000000000000000000000000000000000000606482015260840161056e565b610fdf3385858403611773565b5060019392505050565b60006104fe338484611a03565b60006110457f00000000000000000000000000000000000000000000000000000000000000007f0000000000000000000000000000000000000000000000000000000000000000853386611cb8565b9050337f803540962ed9acbf87226c32486d71e1c86c2bdb208e771bab2fd8a626f61e8961107283611de7565b61107b906130e0565b610a8686611de7565b600073ffffffffffffffffffffffffffffffffffffffff8316301415611106576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601960248201527f5377617020746f207468697320697320666f7262696464656e00000000000000604482015260640161056e565b73ffffffffffffffffffffffffffffffffffffffff8316611183576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601960248201527f5377617020746f207a65726f20697320666f7262696464656e00000000000000604482015260640161056e565b6111d07f00000000000000000000000000000000000000000000000000000000000000007f0000000000000000000000000000000000000000000000000000000000000000868686611cb8565b9050337f803540962ed9acbf87226c32486d71e1c86c2bdb208e771bab2fd8a626f61e896111fd86611de7565b61120684611de7565b6109d9906130e0565b6000806000871161127c576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601f60248201527f456d707479207769746864726177616c206973206e6f7420616c6c6f77656400604482015260640161056e565b73ffffffffffffffffffffffffffffffffffffffff86163014156112fc576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601f60248201527f5769746864726177616c20746f207468697320697320666f7262696464656e00604482015260640161056e565b73ffffffffffffffffffffffffffffffffffffffff8616611379576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601f60248201527f5769746864726177616c20746f207a65726f20697320666f7262696464656e00604482015260640161056e565b670de0b6b3a76400008511156113eb576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601960248201527f526174696f2073686f756c6420626520696e205b302c20315d00000000000000604482015260640161056e565b60006113f660025490565b905061140233896120ea565b61140d8887836122d4565b9093509150611420878985858989612524565b509550959350505050565b60008061143b863387878761120f565b915091505b94509492505050565b600080600086116114b6576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601f60248201527f456d707479207769746864726177616c206973206e6f7420616c6c6f77656400604482015260640161056e565b73ffffffffffffffffffffffffffffffffffffffff8516301415611536576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601f60248201527f5769746864726177616c20746f207468697320697320666f7262696464656e00604482015260640161056e565b73ffffffffffffffffffffffffffffffffffffffff85166115b3576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601f60248201527f5769746864726177616c20746f207a65726f20697320666f7262696464656e00604482015260640161056e565b60006115be60025490565b90506115ca33886120ea565b6040517f70a082310000000000000000000000000000000000000000000000000000000081523060048201528190889073ffffffffffffffffffffffffffffffffffffffff7f000000000000000000000000000000000000000000000000000000000000000016906370a0823190602401602060405180830381865afa158015611658573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061167c9190613080565b6116869190613130565b611690919061319c565b6040517f70a082310000000000000000000000000000000000000000000000000000000081523060048201529093508190889073ffffffffffffffffffffffffffffffffffffffff7f000000000000000000000000000000000000000000000000000000000000000016906370a0823190602401602060405180830381865afa158015611721573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906117459190613080565b61174f9190613130565b611759919061319c565b9150611769868885858989612524565b5094509492505050565b73ffffffffffffffffffffffffffffffffffffffff8316611815576040517f08c379a0000000000000000000000000000000000000000000000000000000008152602060048201526024808201527f45524332303a20617070726f76652066726f6d20746865207a65726f2061646460448201527f7265737300000000000000000000000000000000000000000000000000000000606482015260840161056e565b73ffffffffffffffffffffffffffffffffffffffff82166118b8576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602260248201527f45524332303a20617070726f766520746f20746865207a65726f20616464726560448201527f7373000000000000000000000000000000000000000000000000000000000000606482015260840161056e565b73ffffffffffffffffffffffffffffffffffffffff83811660008181526001602090815260408083209487168084529482529182902085905590518481527f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b92591015b60405180910390a3505050565b60008383018181670de0b6b3a76400008702816119465761194661316d565b049050600082858801670de0b6b3a764000002816119665761196661316d565b049050670de0b6b3a7640000850260008185611981856126d1565b672ef1cef240e848b802611994876126d1565b672ef1cef240e848b8028886670de05bc096e9c00002816119b7576119b761316d565b04010302816119c8576119c861316d565b049050670de0b6b3a76400006119e682670de0b6b3a7640000612740565b8802816119f5576119f561316d565b049998505050505050505050565b73ffffffffffffffffffffffffffffffffffffffff8316611aa6576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602560248201527f45524332303a207472616e736665722066726f6d20746865207a65726f20616460448201527f6472657373000000000000000000000000000000000000000000000000000000606482015260840161056e565b73ffffffffffffffffffffffffffffffffffffffff8216611b49576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602360248201527f45524332303a207472616e7366657220746f20746865207a65726f206164647260448201527f6573730000000000000000000000000000000000000000000000000000000000606482015260840161056e565b73ffffffffffffffffffffffffffffffffffffffff831660009081526020819052604090205481811015611bff576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602660248201527f45524332303a207472616e7366657220616d6f756e742065786365656473206260448201527f616c616e63650000000000000000000000000000000000000000000000000000606482015260840161056e565b73ffffffffffffffffffffffffffffffffffffffff808516600090815260208190526040808220858503905591851681529081208054849290611c439084906130c8565b925050819055508273ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef84604051611ca991815260200190565b60405180910390a35b50505050565b6000611cc5868686610507565b905060008111611d31576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601960248201527f456d7074792073776170206973206e6f7420616c6c6f77656400000000000000604482015260640161056e565b81811015611d9b576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601660248201527f4d696e2072657475726e206e6f74207265616368656400000000000000000000604482015260640161056e565b611dbd73ffffffffffffffffffffffffffffffffffffffff871633308761200e565b611dde73ffffffffffffffffffffffffffffffffffffffff86168483612756565b95945050505050565b60007f7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff821115611e99576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602860248201527f53616665436173743a2076616c756520646f65736e27742066697420696e206160448201527f6e20696e74323536000000000000000000000000000000000000000000000000606482015260840161056e565b5090565b6000808484028684020381811315611ec557611ebb878787876127ac565b9093509150611769565b6000811215611ee357611eda868886886127ac565b93509150611769565b509495939450505050565b73ffffffffffffffffffffffffffffffffffffffff8216611f6b576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601f60248201527f45524332303a206d696e7420746f20746865207a65726f206164647265737300604482015260640161056e565b8060026000828254611f7d91906130c8565b909155505073ffffffffffffffffffffffffffffffffffffffff821660009081526020819052604081208054839290611fb79084906130c8565b909155505060405181815273ffffffffffffffffffffffffffffffffffffffff8316906000907fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef9060200160405180910390a35050565b60405173ffffffffffffffffffffffffffffffffffffffff80851660248301528316604482015260648101829052611cb29085907f23b872dd00000000000000000000000000000000000000000000000000000000906084015b604080517fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe08184030181529190526020810180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff167fffffffff0000000000000000000000000000000000000000000000000000000090931692909217909152612921565b73ffffffffffffffffffffffffffffffffffffffff821661218d576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602160248201527f45524332303a206275726e2066726f6d20746865207a65726f2061646472657360448201527f7300000000000000000000000000000000000000000000000000000000000000606482015260840161056e565b73ffffffffffffffffffffffffffffffffffffffff821660009081526020819052604090205481811015612243576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602260248201527f45524332303a206275726e20616d6f756e7420657863656564732062616c616e60448201527f6365000000000000000000000000000000000000000000000000000000000000606482015260840161056e565b73ffffffffffffffffffffffffffffffffffffffff8316600090815260208190526040812083830390556002805484929061227f908490613119565b909155505060405182815260009073ffffffffffffffffffffffffffffffffffffffff8516907fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef9060200161191a565b505050565b6040517f70a082310000000000000000000000000000000000000000000000000000000081523060048201526000908190819073ffffffffffffffffffffffffffffffffffffffff7f000000000000000000000000000000000000000000000000000000000000000016906370a0823190602401602060405180830381865afa158015612365573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906123899190613080565b6040517f70a0823100000000000000000000000000000000000000000000000000000000815230600482015290915060009073ffffffffffffffffffffffffffffffffffffffff7f000000000000000000000000000000000000000000000000000000000000000016906370a0823190602401602060405180830381865afa158015612419573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061243d9190613080565b905060008561244c8985613130565b612456919061319c565b90506000866124658a85613130565b61246f919061319c565b9050600061247d82846130c8565b61248f670de0b6b3a764000085613130565b612499919061319c565b9050808910156124cc576124c283836124b28289613119565b6124bc8689613119565b8d612a2d565b9097509550612517565b8089111561250d5761250482846124e38288613119565b6124ed878a613119565b6124ff8e670de0b6b3a7640000613119565b612a2d565b97509550612517565b9195509350849084905b5050505050935093915050565b8184101561258e576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601f60248201527f4d696e20746f6b656e30416d6f756e74206973206e6f74207265616368656400604482015260640161056e565b808310156125f8576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601f60248201527f4d696e20746f6b656e31416d6f756e74206973206e6f74207265616368656400604482015260640161056e565b604080518581526020810185905290810186905233907f650fdf669e93aa6c8ff3defe2da9c12b64f1548e5e1e54e803f4c1beb6466c8e9060600160405180910390a283156126825761268273ffffffffffffffffffffffffffffffffffffffff7f0000000000000000000000000000000000000000000000000000000000000000168786612756565b82156126c9576126c973ffffffffffffffffffffffffffffffffffffffff7f0000000000000000000000000000000000000000000000000000000000000000168785612756565b505050505050565b6000670656e7dd8da1c9cf82111561270c57507ffffffffffffffffffffffffffffffffffffffffffffffffff9a91822725e3631810161271a565b81670656e7dd8da1c9cf0390505b670de0b6b3a7640000908002819004808002829004800282900480028290040204919050565b600081831061274f57816107fe565b5090919050565b60405173ffffffffffffffffffffffffffffffffffffffff83166024820152604481018290526122cf9084907fa9059cbb0000000000000000000000000000000000000000000000000000000090606401612068565b6000808085876127bc86886130c8565b6127c691906130c8565b6127d091906130c8565b6127da8688613130565b6127e4868a613130565b6127ee9190613119565b6127f8919061319c565b90508061280b5786869250925050611440565b6000806103e861281d6103e685613130565b612827919061319c565b905060006128566128506103e86128406103ea88613130565b61284a919061319c565b89612740565b8b612740565b90505b806128656001846130c8565b10156128fc57612876888886611927565b925060006128ad612887868d613119565b612891868d6130c8565b61289b888d6130c8565b6128a5888d613119565b910291020390565b905060008113156128d85784925060026128c783856130c8565b6128d1919061319c565b94506128f6565b60008112156128f05784915060026128c783856130c8565b506128fc565b50612859565b612906848b613119565b612910848b6130c8565b955095505050505094509492505050565b6000612983826040518060400160405280602081526020017f5361666545524332303a206c6f772d6c6576656c2063616c6c206661696c65648152508573ffffffffffffffffffffffffffffffffffffffff16612c109092919063ffffffff16565b8051909150156122cf57808060200190518101906129a191906131d7565b6122cf576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602a60248201527f5361666545524332303a204552433230206f7065726174696f6e20646964206e60448201527f6f74207375636365656400000000000000000000000000000000000000000000606482015260840161056e565b60008084151580612a3d57508315155b612aa3576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601c60248201527f416d6f756e74206578636565647320746f74616c2062616c616e636500000000604482015260640161056e565b82612ac8576000612ab586868a611927565b612abf90886130c8565b91509150612c06565b6000612adc84670de0b6b3a7640000613119565b90506000670de0b6b3a7640000612af3868a613130565b612afd848c613130565b612b079190613119565b612b11919061319c565b90506000806103e8612b256103e685613130565b612b2f919061319c565b90506000612b556103e8612b456103ea87613130565b612b4f919061319c565b8d612740565b90505b80612b646001846130c8565b1015612be857612b758a8a86611927565b92506000612b99612b86868f613119565b612b90868f6130c8565b8b029088020390565b90506000811315612bc4578492506002612bb383856130c8565b612bbd919061319c565b9450612be2565b6000811215612bdc578491506002612bb383856130c8565b50612be8565b50612b58565b612bf2848d613119565b612bfc848d6130c8565b9650965050505050505b9550959350505050565b6060610457848460008585843b612c83576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601d60248201527f416464726573733a2063616c6c20746f206e6f6e2d636f6e7472616374000000604482015260640161056e565b6000808673ffffffffffffffffffffffffffffffffffffffff168587604051612cac91906131f9565b60006040518083038185875af1925050503d8060008114612ce9576040519150601f19603f3d011682016040523d82523d6000602084013e612cee565b606091505b5091509150612cfe828286612d09565b979650505050505050565b60608315612d185750816107fe565b825115612d285782518084602001fd5b816040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161056e9190612db4565b600080600060608486031215612d7157600080fd5b505081359360208301359350604090920135919050565b60005b83811015612da3578181015183820152602001612d8b565b83811115611cb25750506000910152565b6020815260008251806020840152612dd3816040850160208701612d88565b601f017fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0169190910160400192915050565b73ffffffffffffffffffffffffffffffffffffffff81168114612e2757600080fd5b50565b60008060408385031215612e3d57600080fd5b8235612e4881612e05565b946020939093013593505050565b600080600060608486031215612e6b57600080fd5b8335612e7681612e05565b92506020840135612e8681612e05565b929592945050506040919091013590565b600080600060608486031215612eac57600080fd5b833592506020840135612e8681612e05565b60008060408385031215612ed157600080fd5b50508035926020909101359150565b600060208284031215612ef257600080fd5b81356107fe81612e05565b60008060008060808587031215612f1357600080fd5b84359350602085013592506040850135612f2c81612e05565b9396929550929360600135925050565b600080600080600060a08688031215612f5457600080fd5b853594506020860135612f6681612e05565b94979496505050506040830135926060810135926080909101359150565b60008060008060808587031215612f9a57600080fd5b5050823594602084013594506040840135936060013592509050565b60008060408385031215612fc957600080fd5b8235612fd481612e05565b91506020830135612fe481612e05565b809150509250929050565b6000806000806080858703121561300557600080fd5b84359350602085013561301781612e05565b93969395505050506040820135916060013590565b600181811c9082168061304057607f821691505b6020821081141561307a577f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b50919050565b60006020828403121561309257600080fd5b5051919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b600082198211156130db576130db613099565b500190565b60007f800000000000000000000000000000000000000000000000000000000000000082141561311257613112613099565b5060000390565b60008282101561312b5761312b613099565b500390565b6000817fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff048311821515161561316857613168613099565b500290565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601260045260246000fd5b6000826131d2577f4e487b7100000000000000000000000000000000000000000000000000000000600052601260045260246000fd5b500490565b6000602082840312156131e957600080fd5b815180151581146107fe57600080fd5b6000825161320b818460208701612d88565b919091019291505056fea264697066735822122086221e39f307408e9dd6b3ce95278b2cef177599a09d35c1cc582c2d10fb611464736f6c634300080a0033", 799 | "deployedBytecode": "0x608060405234801561001057600080fd5b50600436106101975760003560e01c80638882eb62116100e3578063c755836b1161008c578063db5dadbf11610066578063db5dadbf146103dd578063dd62ed3e146103f0578063eac38e0c1461043657600080fd5b8063c755836b14610390578063cdc4e51d146103a3578063d21220a7146103b657600080fd5b8063a457c2d7116100bd578063a457c2d714610357578063a9059cbb1461036a578063c0a535111461037d57600080fd5b80638882eb621461031457806395d89b4114610327578063a41fe49f1461032f57600080fd5b806323b872dd116101455780635a7e9a5f1161011f5780635a7e9a5f146102b857806360336508146102cb57806370a08231146102de57600080fd5b806323b872dd14610261578063313ce5671461027457806339509351146102a557600080fd5b80630dfe1681116101765780630dfe1681146101fa57806318160ddd146102465780631e1401f81461024e57600080fd5b8062aeef8a1461019c57806306fdde03146101c2578063095ea7b3146101d7575b600080fd5b6101af6101aa366004612d5c565b610449565b6040519081526020015b60405180910390f35b6101ca61045f565b6040516101b99190612db4565b6101ea6101e5366004612e2a565b6104f1565b60405190151581526020016101b9565b6102217f000000000000000000000000000000000000000000000000000000000000000081565b60405173ffffffffffffffffffffffffffffffffffffffff90911681526020016101b9565b6002546101af565b6101af61025c366004612e56565b610507565b6101ea61026f366004612e56565b61071d565b60405160ff7f00000000000000000000000000000000000000000000000000000000000000001681526020016101b9565b6101ea6102b3366004612e2a565b610805565b6101af6102c6366004612e97565b61084e565b6101af6102d9366004612ebe565b6109f8565b6101af6102ec366004612ee0565b73ffffffffffffffffffffffffffffffffffffffff1660009081526020819052604090205490565b6101af610322366004612efd565b610aa4565b6101ca610ee7565b61034261033d366004612d5c565b610ef6565b604080519283526020830191909152016101b9565b6101ea610365366004612e2a565b610f11565b6101ea610378366004612e2a565b610fe9565b6101af61038b366004612ebe565b610ff6565b6101af61039e366004612e97565b611084565b6103426103b1366004612f3c565b61120f565b6102217f000000000000000000000000000000000000000000000000000000000000000081565b6103426103eb366004612f84565b61142b565b6101af6103fe366004612fb6565b73ffffffffffffffffffffffffffffffffffffffff918216600090815260016020908152604080832093909416825291909152205490565b610342610444366004612fef565b611449565b600061045784843385610aa4565b949350505050565b60606003805461046e9061302c565b80601f016020809104026020016040519081016040528092919081815260200182805461049a9061302c565b80156104e75780601f106104bc576101008083540402835291602001916104e7565b820191906000526020600020905b8154815290600101906020018083116104ca57829003601f168201915b5050505050905090565b60006104fe338484611773565b50600192915050565b6000808211610577576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601a60248201527f496e70757420616d6f756e742073686f756c64206265203e203000000000000060448201526064015b60405180910390fd5b6040517f70a0823100000000000000000000000000000000000000000000000000000000815230600482015260009073ffffffffffffffffffffffffffffffffffffffff8616906370a0823190602401602060405180830381865afa1580156105e4573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906106089190613080565b6040517f70a0823100000000000000000000000000000000000000000000000000000000815230600482015290915060009073ffffffffffffffffffffffffffffffffffffffff8616906370a0823190602401602060405180830381865afa158015610678573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061069c9190613080565b905080841115610708576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601760248201527f496e70757420616d6f756e7420697320746f6f20626967000000000000000000604482015260640161056e565b610713828286611927565b9695505050505050565b600061072a848484611a03565b73ffffffffffffffffffffffffffffffffffffffff84166000908152600160209081526040808320338452909152902054828110156107eb576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602860248201527f45524332303a207472616e7366657220616d6f756e742065786365656473206160448201527f6c6c6f77616e6365000000000000000000000000000000000000000000000000606482015260840161056e565b6107f88533858403611773565b60019150505b9392505050565b33600081815260016020908152604080832073ffffffffffffffffffffffffffffffffffffffff8716845290915281205490916104fe9185906108499086906130c8565b611773565b600073ffffffffffffffffffffffffffffffffffffffff83163014156108d0576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601960248201527f5377617020746f207468697320697320666f7262696464656e00000000000000604482015260640161056e565b73ffffffffffffffffffffffffffffffffffffffff831661094d576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601960248201527f5377617020746f207a65726f20697320666f7262696464656e00000000000000604482015260640161056e565b61099a7f00000000000000000000000000000000000000000000000000000000000000007f0000000000000000000000000000000000000000000000000000000000000000868686611cb8565b9050337f803540962ed9acbf87226c32486d71e1c86c2bdb208e771bab2fd8a626f61e896109c783611de7565b6109d0906130e0565b6109d987611de7565b6040805192835260208301919091520160405180910390a29392505050565b6000610a477f00000000000000000000000000000000000000000000000000000000000000007f0000000000000000000000000000000000000000000000000000000000000000853386611cb8565b9050337f803540962ed9acbf87226c32486d71e1c86c2bdb208e771bab2fd8a626f61e89610a7485611de7565b610a7d84611de7565b610a86906130e0565b6040805192835260208301919091520160405180910390a292915050565b6040517f70a08231000000000000000000000000000000000000000000000000000000008152306004820152600090819073ffffffffffffffffffffffffffffffffffffffff7f000000000000000000000000000000000000000000000000000000000000000016906370a0823190602401602060405180830381865afa158015610b33573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610b579190613080565b6040517f70a0823100000000000000000000000000000000000000000000000000000000815230600482015290915060009073ffffffffffffffffffffffffffffffffffffffff7f000000000000000000000000000000000000000000000000000000000000000016906370a0823190602401602060405180830381865afa158015610be7573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610c0b9190613080565b9050600080610c1c89898686611e9d565b90925090506000610c2d82846130c8565b905060008111610c99576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601c60248201527f456d707479206465706f736974206973206e6f7420616c6c6f77656400000000604482015260640161056e565b73ffffffffffffffffffffffffffffffffffffffff8816301415610d19576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601c60248201527f4465706f73697420746f207468697320697320666f7262696464656e00000000604482015260640161056e565b6000610d2460025490565b90508015610d78576000828b8d610d3b898b6130c8565b610d4591906130c8565b610d4f91906130c8565b610d599190613119565b905080610d668385613130565b610d70919061319c565b975050610d7c565b8196505b87871015610de6576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601360248201527f5368617265206973206e6f7420656e6f75676800000000000000000000000000604482015260640161056e565b610df08988611eee565b604080518c8152602081018c905290810188905273ffffffffffffffffffffffffffffffffffffffff8a16907f36af321ec8d3c75236829c5317affd40ddb308863a1236d2d277a4025cccee1e9060600160405180910390a28a15610e9157610e9173ffffffffffffffffffffffffffffffffffffffff7f00000000000000000000000000000000000000000000000000000000000000001633308e61200e565b8915610ed957610ed973ffffffffffffffffffffffffffffffffffffffff7f00000000000000000000000000000000000000000000000000000000000000001633308d61200e565b505050505050949350505050565b60606004805461046e9061302c565b600080610f0585338686611449565b90969095509350505050565b33600090815260016020908152604080832073ffffffffffffffffffffffffffffffffffffffff8616845290915281205482811015610fd2576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602560248201527f45524332303a2064656372656173656420616c6c6f77616e63652062656c6f7760448201527f207a65726f000000000000000000000000000000000000000000000000000000606482015260840161056e565b610fdf3385858403611773565b5060019392505050565b60006104fe338484611a03565b60006110457f00000000000000000000000000000000000000000000000000000000000000007f0000000000000000000000000000000000000000000000000000000000000000853386611cb8565b9050337f803540962ed9acbf87226c32486d71e1c86c2bdb208e771bab2fd8a626f61e8961107283611de7565b61107b906130e0565b610a8686611de7565b600073ffffffffffffffffffffffffffffffffffffffff8316301415611106576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601960248201527f5377617020746f207468697320697320666f7262696464656e00000000000000604482015260640161056e565b73ffffffffffffffffffffffffffffffffffffffff8316611183576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601960248201527f5377617020746f207a65726f20697320666f7262696464656e00000000000000604482015260640161056e565b6111d07f00000000000000000000000000000000000000000000000000000000000000007f0000000000000000000000000000000000000000000000000000000000000000868686611cb8565b9050337f803540962ed9acbf87226c32486d71e1c86c2bdb208e771bab2fd8a626f61e896111fd86611de7565b61120684611de7565b6109d9906130e0565b6000806000871161127c576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601f60248201527f456d707479207769746864726177616c206973206e6f7420616c6c6f77656400604482015260640161056e565b73ffffffffffffffffffffffffffffffffffffffff86163014156112fc576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601f60248201527f5769746864726177616c20746f207468697320697320666f7262696464656e00604482015260640161056e565b73ffffffffffffffffffffffffffffffffffffffff8616611379576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601f60248201527f5769746864726177616c20746f207a65726f20697320666f7262696464656e00604482015260640161056e565b670de0b6b3a76400008511156113eb576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601960248201527f526174696f2073686f756c6420626520696e205b302c20315d00000000000000604482015260640161056e565b60006113f660025490565b905061140233896120ea565b61140d8887836122d4565b9093509150611420878985858989612524565b509550959350505050565b60008061143b863387878761120f565b915091505b94509492505050565b600080600086116114b6576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601f60248201527f456d707479207769746864726177616c206973206e6f7420616c6c6f77656400604482015260640161056e565b73ffffffffffffffffffffffffffffffffffffffff8516301415611536576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601f60248201527f5769746864726177616c20746f207468697320697320666f7262696464656e00604482015260640161056e565b73ffffffffffffffffffffffffffffffffffffffff85166115b3576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601f60248201527f5769746864726177616c20746f207a65726f20697320666f7262696464656e00604482015260640161056e565b60006115be60025490565b90506115ca33886120ea565b6040517f70a082310000000000000000000000000000000000000000000000000000000081523060048201528190889073ffffffffffffffffffffffffffffffffffffffff7f000000000000000000000000000000000000000000000000000000000000000016906370a0823190602401602060405180830381865afa158015611658573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061167c9190613080565b6116869190613130565b611690919061319c565b6040517f70a082310000000000000000000000000000000000000000000000000000000081523060048201529093508190889073ffffffffffffffffffffffffffffffffffffffff7f000000000000000000000000000000000000000000000000000000000000000016906370a0823190602401602060405180830381865afa158015611721573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906117459190613080565b61174f9190613130565b611759919061319c565b9150611769868885858989612524565b5094509492505050565b73ffffffffffffffffffffffffffffffffffffffff8316611815576040517f08c379a0000000000000000000000000000000000000000000000000000000008152602060048201526024808201527f45524332303a20617070726f76652066726f6d20746865207a65726f2061646460448201527f7265737300000000000000000000000000000000000000000000000000000000606482015260840161056e565b73ffffffffffffffffffffffffffffffffffffffff82166118b8576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602260248201527f45524332303a20617070726f766520746f20746865207a65726f20616464726560448201527f7373000000000000000000000000000000000000000000000000000000000000606482015260840161056e565b73ffffffffffffffffffffffffffffffffffffffff83811660008181526001602090815260408083209487168084529482529182902085905590518481527f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b92591015b60405180910390a3505050565b60008383018181670de0b6b3a76400008702816119465761194661316d565b049050600082858801670de0b6b3a764000002816119665761196661316d565b049050670de0b6b3a7640000850260008185611981856126d1565b672ef1cef240e848b802611994876126d1565b672ef1cef240e848b8028886670de05bc096e9c00002816119b7576119b761316d565b04010302816119c8576119c861316d565b049050670de0b6b3a76400006119e682670de0b6b3a7640000612740565b8802816119f5576119f561316d565b049998505050505050505050565b73ffffffffffffffffffffffffffffffffffffffff8316611aa6576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602560248201527f45524332303a207472616e736665722066726f6d20746865207a65726f20616460448201527f6472657373000000000000000000000000000000000000000000000000000000606482015260840161056e565b73ffffffffffffffffffffffffffffffffffffffff8216611b49576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602360248201527f45524332303a207472616e7366657220746f20746865207a65726f206164647260448201527f6573730000000000000000000000000000000000000000000000000000000000606482015260840161056e565b73ffffffffffffffffffffffffffffffffffffffff831660009081526020819052604090205481811015611bff576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602660248201527f45524332303a207472616e7366657220616d6f756e742065786365656473206260448201527f616c616e63650000000000000000000000000000000000000000000000000000606482015260840161056e565b73ffffffffffffffffffffffffffffffffffffffff808516600090815260208190526040808220858503905591851681529081208054849290611c439084906130c8565b925050819055508273ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef84604051611ca991815260200190565b60405180910390a35b50505050565b6000611cc5868686610507565b905060008111611d31576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601960248201527f456d7074792073776170206973206e6f7420616c6c6f77656400000000000000604482015260640161056e565b81811015611d9b576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601660248201527f4d696e2072657475726e206e6f74207265616368656400000000000000000000604482015260640161056e565b611dbd73ffffffffffffffffffffffffffffffffffffffff871633308761200e565b611dde73ffffffffffffffffffffffffffffffffffffffff86168483612756565b95945050505050565b60007f7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff821115611e99576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602860248201527f53616665436173743a2076616c756520646f65736e27742066697420696e206160448201527f6e20696e74323536000000000000000000000000000000000000000000000000606482015260840161056e565b5090565b6000808484028684020381811315611ec557611ebb878787876127ac565b9093509150611769565b6000811215611ee357611eda868886886127ac565b93509150611769565b509495939450505050565b73ffffffffffffffffffffffffffffffffffffffff8216611f6b576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601f60248201527f45524332303a206d696e7420746f20746865207a65726f206164647265737300604482015260640161056e565b8060026000828254611f7d91906130c8565b909155505073ffffffffffffffffffffffffffffffffffffffff821660009081526020819052604081208054839290611fb79084906130c8565b909155505060405181815273ffffffffffffffffffffffffffffffffffffffff8316906000907fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef9060200160405180910390a35050565b60405173ffffffffffffffffffffffffffffffffffffffff80851660248301528316604482015260648101829052611cb29085907f23b872dd00000000000000000000000000000000000000000000000000000000906084015b604080517fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe08184030181529190526020810180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff167fffffffff0000000000000000000000000000000000000000000000000000000090931692909217909152612921565b73ffffffffffffffffffffffffffffffffffffffff821661218d576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602160248201527f45524332303a206275726e2066726f6d20746865207a65726f2061646472657360448201527f7300000000000000000000000000000000000000000000000000000000000000606482015260840161056e565b73ffffffffffffffffffffffffffffffffffffffff821660009081526020819052604090205481811015612243576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602260248201527f45524332303a206275726e20616d6f756e7420657863656564732062616c616e60448201527f6365000000000000000000000000000000000000000000000000000000000000606482015260840161056e565b73ffffffffffffffffffffffffffffffffffffffff8316600090815260208190526040812083830390556002805484929061227f908490613119565b909155505060405182815260009073ffffffffffffffffffffffffffffffffffffffff8516907fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef9060200161191a565b505050565b6040517f70a082310000000000000000000000000000000000000000000000000000000081523060048201526000908190819073ffffffffffffffffffffffffffffffffffffffff7f000000000000000000000000000000000000000000000000000000000000000016906370a0823190602401602060405180830381865afa158015612365573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906123899190613080565b6040517f70a0823100000000000000000000000000000000000000000000000000000000815230600482015290915060009073ffffffffffffffffffffffffffffffffffffffff7f000000000000000000000000000000000000000000000000000000000000000016906370a0823190602401602060405180830381865afa158015612419573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061243d9190613080565b905060008561244c8985613130565b612456919061319c565b90506000866124658a85613130565b61246f919061319c565b9050600061247d82846130c8565b61248f670de0b6b3a764000085613130565b612499919061319c565b9050808910156124cc576124c283836124b28289613119565b6124bc8689613119565b8d612a2d565b9097509550612517565b8089111561250d5761250482846124e38288613119565b6124ed878a613119565b6124ff8e670de0b6b3a7640000613119565b612a2d565b97509550612517565b9195509350849084905b5050505050935093915050565b8184101561258e576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601f60248201527f4d696e20746f6b656e30416d6f756e74206973206e6f74207265616368656400604482015260640161056e565b808310156125f8576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601f60248201527f4d696e20746f6b656e31416d6f756e74206973206e6f74207265616368656400604482015260640161056e565b604080518581526020810185905290810186905233907f650fdf669e93aa6c8ff3defe2da9c12b64f1548e5e1e54e803f4c1beb6466c8e9060600160405180910390a283156126825761268273ffffffffffffffffffffffffffffffffffffffff7f0000000000000000000000000000000000000000000000000000000000000000168786612756565b82156126c9576126c973ffffffffffffffffffffffffffffffffffffffff7f0000000000000000000000000000000000000000000000000000000000000000168785612756565b505050505050565b6000670656e7dd8da1c9cf82111561270c57507ffffffffffffffffffffffffffffffffffffffffffffffffff9a91822725e3631810161271a565b81670656e7dd8da1c9cf0390505b670de0b6b3a7640000908002819004808002829004800282900480028290040204919050565b600081831061274f57816107fe565b5090919050565b60405173ffffffffffffffffffffffffffffffffffffffff83166024820152604481018290526122cf9084907fa9059cbb0000000000000000000000000000000000000000000000000000000090606401612068565b6000808085876127bc86886130c8565b6127c691906130c8565b6127d091906130c8565b6127da8688613130565b6127e4868a613130565b6127ee9190613119565b6127f8919061319c565b90508061280b5786869250925050611440565b6000806103e861281d6103e685613130565b612827919061319c565b905060006128566128506103e86128406103ea88613130565b61284a919061319c565b89612740565b8b612740565b90505b806128656001846130c8565b10156128fc57612876888886611927565b925060006128ad612887868d613119565b612891868d6130c8565b61289b888d6130c8565b6128a5888d613119565b910291020390565b905060008113156128d85784925060026128c783856130c8565b6128d1919061319c565b94506128f6565b60008112156128f05784915060026128c783856130c8565b506128fc565b50612859565b612906848b613119565b612910848b6130c8565b955095505050505094509492505050565b6000612983826040518060400160405280602081526020017f5361666545524332303a206c6f772d6c6576656c2063616c6c206661696c65648152508573ffffffffffffffffffffffffffffffffffffffff16612c109092919063ffffffff16565b8051909150156122cf57808060200190518101906129a191906131d7565b6122cf576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602a60248201527f5361666545524332303a204552433230206f7065726174696f6e20646964206e60448201527f6f74207375636365656400000000000000000000000000000000000000000000606482015260840161056e565b60008084151580612a3d57508315155b612aa3576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601c60248201527f416d6f756e74206578636565647320746f74616c2062616c616e636500000000604482015260640161056e565b82612ac8576000612ab586868a611927565b612abf90886130c8565b91509150612c06565b6000612adc84670de0b6b3a7640000613119565b90506000670de0b6b3a7640000612af3868a613130565b612afd848c613130565b612b079190613119565b612b11919061319c565b90506000806103e8612b256103e685613130565b612b2f919061319c565b90506000612b556103e8612b456103ea87613130565b612b4f919061319c565b8d612740565b90505b80612b646001846130c8565b1015612be857612b758a8a86611927565b92506000612b99612b86868f613119565b612b90868f6130c8565b8b029088020390565b90506000811315612bc4578492506002612bb383856130c8565b612bbd919061319c565b9450612be2565b6000811215612bdc578491506002612bb383856130c8565b50612be8565b50612b58565b612bf2848d613119565b612bfc848d6130c8565b9650965050505050505b9550959350505050565b6060610457848460008585843b612c83576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601d60248201527f416464726573733a2063616c6c20746f206e6f6e2d636f6e7472616374000000604482015260640161056e565b6000808673ffffffffffffffffffffffffffffffffffffffff168587604051612cac91906131f9565b60006040518083038185875af1925050503d8060008114612ce9576040519150601f19603f3d011682016040523d82523d6000602084013e612cee565b606091505b5091509150612cfe828286612d09565b979650505050505050565b60608315612d185750816107fe565b825115612d285782518084602001fd5b816040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161056e9190612db4565b600080600060608486031215612d7157600080fd5b505081359360208301359350604090920135919050565b60005b83811015612da3578181015183820152602001612d8b565b83811115611cb25750506000910152565b6020815260008251806020840152612dd3816040850160208701612d88565b601f017fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0169190910160400192915050565b73ffffffffffffffffffffffffffffffffffffffff81168114612e2757600080fd5b50565b60008060408385031215612e3d57600080fd5b8235612e4881612e05565b946020939093013593505050565b600080600060608486031215612e6b57600080fd5b8335612e7681612e05565b92506020840135612e8681612e05565b929592945050506040919091013590565b600080600060608486031215612eac57600080fd5b833592506020840135612e8681612e05565b60008060408385031215612ed157600080fd5b50508035926020909101359150565b600060208284031215612ef257600080fd5b81356107fe81612e05565b60008060008060808587031215612f1357600080fd5b84359350602085013592506040850135612f2c81612e05565b9396929550929360600135925050565b600080600080600060a08688031215612f5457600080fd5b853594506020860135612f6681612e05565b94979496505050506040830135926060810135926080909101359150565b60008060008060808587031215612f9a57600080fd5b5050823594602084013594506040840135936060013592509050565b60008060408385031215612fc957600080fd5b8235612fd481612e05565b91506020830135612fe481612e05565b809150509250929050565b6000806000806080858703121561300557600080fd5b84359350602085013561301781612e05565b93969395505050506040820135916060013590565b600181811c9082168061304057607f821691505b6020821081141561307a577f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b50919050565b60006020828403121561309257600080fd5b5051919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b600082198211156130db576130db613099565b500190565b60007f800000000000000000000000000000000000000000000000000000000000000082141561311257613112613099565b5060000390565b60008282101561312b5761312b613099565b500390565b6000817fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff048311821515161561316857613168613099565b500290565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601260045260246000fd5b6000826131d2577f4e487b7100000000000000000000000000000000000000000000000000000000600052601260045260246000fd5b500490565b6000602082840312156131e957600080fd5b815180151581146107fe57600080fd5b6000825161320b818460208701612d88565b919091019291505056fea264697066735822122086221e39f307408e9dd6b3ce95278b2cef177599a09d35c1cc582c2d10fb611464736f6c634300080a0033", 800 | "devdoc": { 801 | "details": "AMM that is designed for assets with stable price to each other e.g. USDC and USDT. It utilizes constant sum price curve x + y = const but fee is variable depending on the token balances. In most cases fee is equal to 1 bip. But when balances are at extreme ends it either lowers to 0 or increases to 20 bip. Fee calculations are explained in more details in `getReturn` method. Note that AMM does not support token with fees. Note that tokens decimals are required to be the same.", 802 | "kind": "dev", 803 | "methods": { 804 | "allowance(address,address)": { 805 | "details": "See {IERC20-allowance}." 806 | }, 807 | "approve(address,uint256)": { 808 | "details": "See {IERC20-approve}. Requirements: - `spender` cannot be the zero address." 809 | }, 810 | "balanceOf(address)": { 811 | "details": "See {IERC20-balanceOf}." 812 | }, 813 | "decimals()": { 814 | "details": "Returns the number of decimals used to get its user representation. For example, if `decimals` equals `2`, a balance of `505` tokens should be displayed to a user as `5.05` (`505 / 10 ** 2`). Tokens usually opt for a value of 18, imitating the relationship between Ether and Wei. This is the value {ERC20} uses, unless this function is overridden; NOTE: This information is only used for _display_ purposes: it in no way affects any of the arithmetic of the contract, including {IERC20-balanceOf} and {IERC20-transfer}." 815 | }, 816 | "decreaseAllowance(address,uint256)": { 817 | "details": "Atomically decreases the allowance granted to `spender` by the caller. This is an alternative to {approve} that can be used as a mitigation for problems described in {IERC20-approve}. Emits an {Approval} event indicating the updated allowance. Requirements: - `spender` cannot be the zero address. - `spender` must have allowance for the caller of at least `subtractedValue`." 818 | }, 819 | "deposit(uint256,uint256,uint256)": { 820 | "params": { 821 | "minShare": "minimal required amount of LP tokens received", 822 | "token0Amount": "amount of token0 to deposit", 823 | "token1Amount": "amount of token1 to deposit" 824 | }, 825 | "returns": { 826 | "share": "amount of LP tokens received" 827 | } 828 | }, 829 | "depositFor(uint256,uint256,address,uint256)": { 830 | "details": "fully balanced deposit happens when ratio of amounts of deposit matches ratio of balances. To make a fair deposit when ratios do not match the contract finds the amount that is needed to swap to equalize ratios and makes that swap virtually to capture the swap fees. Then final share is calculated from fair deposit of virtual amounts.", 831 | "params": { 832 | "minShare": "minimal required amount of LP tokens received", 833 | "to": "address that will receive tokens", 834 | "token0Amount": "amount of token0 to deposit", 835 | "token1Amount": "amount of token1 to deposit" 836 | }, 837 | "returns": { 838 | "share": "amount of LP tokens received" 839 | } 840 | }, 841 | "getReturn(address,address,uint256)": { 842 | "details": "`getReturn` at point `x = inputBalance / (inputBalance + outputBalance)`: `getReturn(x) = 0.9999 + (0.5817091329374359 - x * 1.2734233188154198)^17` When balance is changed from `inputBalance` to `inputBalance + amount` we should take integral of getReturn to calculate proper amount: `getReturn(x0, x1) = (integral (0.9999 + (0.5817091329374359 - x * 1.2734233188154198)^17) dx from x=x0 to x=x1) / (x1 - x0)` `getReturn(x0, x1) = (0.9999 * x - 3.3827123349983306 * (x - 0.4568073509746632) ** 18 from x=x0 to x=x1) / (x1 - x0)` `getReturn(x0, x1) = (0.9999 * (x1 - x0) + 3.3827123349983306 * ((x0 - 0.4568073509746632) ** 18 - (x1 - 0.4568073509746632) ** 18)) / (x1 - x0)` C0 = 0.9999 C2 = 3.3827123349983306 C3 = 0.4568073509746632 `getReturn(x0, x1) = (C0 * (x1 - x0) + C2 * ((x0 - C3) ** 18 - (x1 - C3) ** 18)) / (x1 - x0)`", 843 | "params": { 844 | "inputAmount": "amount of `tokenFrom` that user wants to sell", 845 | "tokenFrom": "token that user wants to sell", 846 | "tokenTo": "token that user wants to buy" 847 | }, 848 | "returns": { 849 | "outputAmount": "amount of `tokenTo` that user will receive after the trade" 850 | } 851 | }, 852 | "increaseAllowance(address,uint256)": { 853 | "details": "Atomically increases the allowance granted to `spender` by the caller. This is an alternative to {approve} that can be used as a mitigation for problems described in {IERC20-approve}. Emits an {Approval} event indicating the updated allowance. Requirements: - `spender` cannot be the zero address." 854 | }, 855 | "name()": { 856 | "details": "Returns the name of the token." 857 | }, 858 | "swap0To1(uint256,uint256)": { 859 | "params": { 860 | "inputAmount": "amount of token0 to sell", 861 | "minReturnAmount": "minimal required amount of token1 to buy" 862 | }, 863 | "returns": { 864 | "outputAmount": "amount of token1 bought" 865 | } 866 | }, 867 | "swap0To1For(uint256,address,uint256)": { 868 | "params": { 869 | "inputAmount": "amount of token0 to sell", 870 | "minReturnAmount": "minimal required amount of token1 to buy", 871 | "to": "address that will receive tokens" 872 | }, 873 | "returns": { 874 | "outputAmount": "amount of token1 bought" 875 | } 876 | }, 877 | "swap1To0(uint256,uint256)": { 878 | "params": { 879 | "inputAmount": "amount of token1 to sell", 880 | "minReturnAmount": "minimal required amount of token0 to buy" 881 | }, 882 | "returns": { 883 | "outputAmount": "amount of token0 bought" 884 | } 885 | }, 886 | "swap1To0For(uint256,address,uint256)": { 887 | "params": { 888 | "inputAmount": "amount of token1 to sell", 889 | "minReturnAmount": "minimal required amount of token0 to buy", 890 | "to": "address that will receive tokens" 891 | }, 892 | "returns": { 893 | "outputAmount": "amount of token0 bought" 894 | } 895 | }, 896 | "symbol()": { 897 | "details": "Returns the symbol of the token, usually a shorter version of the name." 898 | }, 899 | "totalSupply()": { 900 | "details": "See {IERC20-totalSupply}." 901 | }, 902 | "transfer(address,uint256)": { 903 | "details": "See {IERC20-transfer}. Requirements: - `recipient` cannot be the zero address. - the caller must have a balance of at least `amount`." 904 | }, 905 | "transferFrom(address,address,uint256)": { 906 | "details": "See {IERC20-transferFrom}. Emits an {Approval} event indicating the updated allowance. This is not required by the EIP. See the note at the beginning of {ERC20}. Requirements: - `sender` and `recipient` cannot be the zero address. - `sender` must have a balance of at least `amount`. - the caller must have allowance for ``sender``'s tokens of at least `amount`." 907 | }, 908 | "withdraw(uint256,uint256,uint256)": { 909 | "params": { 910 | "amount": "amount of LP tokens to burn", 911 | "minToken0Amount": "minimal required amount of token0", 912 | "minToken1Amount": "minimal required amount of token1" 913 | }, 914 | "returns": { 915 | "token0Amount": "amount of token0 received", 916 | "token1Amount": "amount of token1 received" 917 | } 918 | }, 919 | "withdrawFor(uint256,address,uint256,uint256)": { 920 | "params": { 921 | "amount": "amount of LP tokens to burn", 922 | "minToken0Amount": "minimal required amount of token0", 923 | "minToken1Amount": "minimal required amount of token1", 924 | "to": "address that will receive tokens" 925 | }, 926 | "returns": { 927 | "token0Amount": "amount of token0 received", 928 | "token1Amount": "amount of token1 received" 929 | } 930 | }, 931 | "withdrawForWithRatio(uint256,address,uint256,uint256,uint256)": { 932 | "details": "withdrawal with custom ratio is semantically equal to proportional withdrawal with extra swap afterwards to get to the specified ratio. The contract does exactly this by making virtual proportional withdrawal and then finds the amount needed for an extra virtual swap to achieve specified ratio.", 933 | "params": { 934 | "amount": "amount of LP tokens to burn", 935 | "minToken0Amount": "minimal required amount of token0", 936 | "minToken1Amount": "minimal required amount of token1", 937 | "to": "address that will receive tokens", 938 | "token0Share": "percentage of token0 to receive with 100% equals to 1e18" 939 | }, 940 | "returns": { 941 | "token0Amount": "amount of token0 received", 942 | "token1Amount": "amount of token1 received" 943 | } 944 | }, 945 | "withdrawWithRatio(uint256,uint256,uint256,uint256)": { 946 | "params": { 947 | "amount": "amount of LP tokens to burn", 948 | "minToken0Amount": "minimal required amount of token0", 949 | "minToken1Amount": "minimal required amount of token1", 950 | "token0Share": "percentage of token0 to receive with 100% equals to 1e18" 951 | }, 952 | "returns": { 953 | "token0Amount": "amount of token0 received", 954 | "token1Amount": "amount of token1 received" 955 | } 956 | } 957 | }, 958 | "version": 1 959 | }, 960 | "userdoc": { 961 | "kind": "user", 962 | "methods": { 963 | "deposit(uint256,uint256,uint256)": { 964 | "notice": "makes a deposit of both tokens to the AMM" 965 | }, 966 | "depositFor(uint256,uint256,address,uint256)": { 967 | "notice": "makes a deposit of both tokens to the AMM and transfers LP tokens to the specified address" 968 | }, 969 | "getReturn(address,address,uint256)": { 970 | "notice": "estimates return value of the swap" 971 | }, 972 | "swap0To1(uint256,uint256)": { 973 | "notice": "swaps token0 for token1" 974 | }, 975 | "swap0To1For(uint256,address,uint256)": { 976 | "notice": "swaps token0 for token1 and transfers them to specified receiver address" 977 | }, 978 | "swap1To0(uint256,uint256)": { 979 | "notice": "swaps token1 for token0" 980 | }, 981 | "swap1To0For(uint256,address,uint256)": { 982 | "notice": "swaps token1 for token0 and transfers them to specified receiver address" 983 | }, 984 | "withdraw(uint256,uint256,uint256)": { 985 | "notice": "makes a proportional withdrawal of both tokens" 986 | }, 987 | "withdrawFor(uint256,address,uint256,uint256)": { 988 | "notice": "makes a proportional withdrawal of both tokens and transfers them to the specified address" 989 | }, 990 | "withdrawForWithRatio(uint256,address,uint256,uint256,uint256)": { 991 | "notice": "makes a withdrawal with custom ratio and transfers tokens to the specified address" 992 | }, 993 | "withdrawWithRatio(uint256,uint256,uint256,uint256)": { 994 | "notice": "makes a withdrawal with custom ratio" 995 | } 996 | }, 997 | "version": 1 998 | }, 999 | "storageLayout": { 1000 | "storage": [ 1001 | { 1002 | "astId": 120, 1003 | "contract": "contracts/FixedRateSwap.sol:FixedRateSwap", 1004 | "label": "_balances", 1005 | "offset": 0, 1006 | "slot": "0", 1007 | "type": "t_mapping(t_address,t_uint256)" 1008 | }, 1009 | { 1010 | "astId": 126, 1011 | "contract": "contracts/FixedRateSwap.sol:FixedRateSwap", 1012 | "label": "_allowances", 1013 | "offset": 0, 1014 | "slot": "1", 1015 | "type": "t_mapping(t_address,t_mapping(t_address,t_uint256))" 1016 | }, 1017 | { 1018 | "astId": 128, 1019 | "contract": "contracts/FixedRateSwap.sol:FixedRateSwap", 1020 | "label": "_totalSupply", 1021 | "offset": 0, 1022 | "slot": "2", 1023 | "type": "t_uint256" 1024 | }, 1025 | { 1026 | "astId": 130, 1027 | "contract": "contracts/FixedRateSwap.sol:FixedRateSwap", 1028 | "label": "_name", 1029 | "offset": 0, 1030 | "slot": "3", 1031 | "type": "t_string_storage" 1032 | }, 1033 | { 1034 | "astId": 132, 1035 | "contract": "contracts/FixedRateSwap.sol:FixedRateSwap", 1036 | "label": "_symbol", 1037 | "offset": 0, 1038 | "slot": "4", 1039 | "type": "t_string_storage" 1040 | } 1041 | ], 1042 | "types": { 1043 | "t_address": { 1044 | "encoding": "inplace", 1045 | "label": "address", 1046 | "numberOfBytes": "20" 1047 | }, 1048 | "t_mapping(t_address,t_mapping(t_address,t_uint256))": { 1049 | "encoding": "mapping", 1050 | "key": "t_address", 1051 | "label": "mapping(address => mapping(address => uint256))", 1052 | "numberOfBytes": "32", 1053 | "value": "t_mapping(t_address,t_uint256)" 1054 | }, 1055 | "t_mapping(t_address,t_uint256)": { 1056 | "encoding": "mapping", 1057 | "key": "t_address", 1058 | "label": "mapping(address => uint256)", 1059 | "numberOfBytes": "32", 1060 | "value": "t_uint256" 1061 | }, 1062 | "t_string_storage": { 1063 | "encoding": "bytes", 1064 | "label": "string", 1065 | "numberOfBytes": "32" 1066 | }, 1067 | "t_uint256": { 1068 | "encoding": "inplace", 1069 | "label": "uint256", 1070 | "numberOfBytes": "32" 1071 | } 1072 | } 1073 | } 1074 | } -------------------------------------------------------------------------------- /deployments/mainnet/.chainId: -------------------------------------------------------------------------------- 1 | 1 -------------------------------------------------------------------------------- /deployments/matic/.chainId: -------------------------------------------------------------------------------- 1 | 137 -------------------------------------------------------------------------------- /deployments/optimistic/.chainId: -------------------------------------------------------------------------------- 1 | 10 -------------------------------------------------------------------------------- /hardhat.config.js: -------------------------------------------------------------------------------- 1 | require('@nomiclabs/hardhat-etherscan'); 2 | require('@nomiclabs/hardhat-truffle5'); 3 | require('dotenv').config(); 4 | require('hardhat-dependency-compiler'); 5 | require('hardhat-deploy'); 6 | require('hardhat-gas-reporter'); 7 | require('solidity-coverage'); 8 | 9 | const networks = require('./hardhat.networks'); 10 | 11 | module.exports = { 12 | solidity: { 13 | version: '0.8.10', 14 | settings: { 15 | optimizer: { 16 | enabled: true, 17 | runs: 1000000, 18 | }, 19 | }, 20 | }, 21 | networks, 22 | namedAccounts: { 23 | deployer: { 24 | default: 0, 25 | }, 26 | }, 27 | etherscan: { 28 | apiKey: process.env.MAINNET_ETHERSCAN_KEY, 29 | }, 30 | gasReporter: { 31 | enable: true, 32 | currency: 'USD', 33 | }, 34 | dependencyCompiler: { 35 | paths: [ 36 | '@1inch/solidity-utils/contracts/mocks/TokenMock.sol', 37 | ], 38 | }, 39 | }; 40 | -------------------------------------------------------------------------------- /hardhat.networks.js: -------------------------------------------------------------------------------- 1 | const networks = {}; 2 | 3 | if (process.env.MAINNET_RPC_URL && process.env.MAINNET_PRIVATE_KEY) { 4 | networks.mainnet = { 5 | url: process.env.MAINNET_RPC_URL, 6 | chainId: 1, 7 | accounts: [process.env.MAINNET_PRIVATE_KEY], 8 | }; 9 | } 10 | 11 | if (process.env.BSC_RPC_URL && process.env.BSC_PRIVATE_KEY) { 12 | networks.bsc = { 13 | url: process.env.BSC_RPC_URL, 14 | chainId: 56, 15 | accounts: [process.env.BSC_PRIVATE_KEY], 16 | }; 17 | } 18 | 19 | if (process.env.MATIC_RPC_URL && process.env.MATIC_PRIVATE_KEY) { 20 | networks.matic = { 21 | url: process.env.MATIC_RPC_URL, 22 | chainId: 137, 23 | accounts: [process.env.MATIC_PRIVATE_KEY], 24 | }; 25 | } 26 | 27 | if (process.env.ARBITRUM_RPC_URL && process.env.ARBITRUM_PRIVATE_KEY) { 28 | networks.arbitrum = { 29 | url: process.env.ARBITRUM_RPC_URL, 30 | chainId: 42161, 31 | accounts: [process.env.ARBITRUM_PRIVATE_KEY], 32 | }; 33 | } 34 | 35 | if (process.env.OPTIMISTIC_RPC_URL && process.env.OPTIMISTIC_PRIVATE_KEY) { 36 | networks.optimistic = { 37 | url: process.env.OPTIMISTIC_RPC_URL, 38 | chainId: 10, 39 | accounts: [process.env.OPTIMISTIC_PRIVATE_KEY], 40 | }; 41 | } 42 | 43 | module.exports = networks; 44 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "solidity-template", 3 | "version": "0.0.1", 4 | "description": "", 5 | "repository": { 6 | "type": "git", 7 | "url": "git@github.com:zumzoom/solidity-template.git" 8 | }, 9 | "license": "MIT", 10 | "dependencies": { 11 | "@openzeppelin/contracts": "^4.4.0" 12 | }, 13 | "devDependencies": { 14 | "@1inch/solidity-utils": "2.0.23", 15 | "@nomiclabs/hardhat-etherscan": "^2.1.8", 16 | "@nomiclabs/hardhat-truffle5": "^2.0.2", 17 | "@nomiclabs/hardhat-web3": "^2.0.0", 18 | "@openzeppelin/test-helpers": "^0.5.15", 19 | "chai": "^4.3.4", 20 | "dotenv": "^10.0.0", 21 | "eslint": "^8.3.0", 22 | "eslint-config-standard": "^16.0.3", 23 | "eslint-plugin-import": "^2.25.3", 24 | "eslint-plugin-node": "^11.1.0", 25 | "eslint-plugin-promise": "^5.1.1", 26 | "eslint-plugin-standard": "^5.0.0", 27 | "hardhat": "^2.7.0", 28 | "hardhat-dependency-compiler": "1.1.3", 29 | "hardhat-deploy": "^0.9.12", 30 | "hardhat-gas-reporter": "^1.0.4", 31 | "rimraf": "^3.0.2", 32 | "solc": "^0.8.10", 33 | "solhint": "^3.3.6", 34 | "solidity-coverage": "^0.7.17" 35 | }, 36 | "scripts": { 37 | "clean": "rimraf artifacts cache coverage coverage.json", 38 | "coverage": "hardhat coverage", 39 | "deploy": "hardhat deploy --network", 40 | "deploy:test": "hardhat deploy", 41 | "lint": "yarn run lint:js && yarn run lint:sol", 42 | "lint:fix": "yarn run lint:js:fix && yarn run lint:sol:fix", 43 | "lint:js": "eslint .", 44 | "lint:js:fix": "eslint . --fix", 45 | "lint:sol": "solhint --max-warnings 0 \"contracts/**/*.sol\"", 46 | "lint:sol:fix": "solhint --max-warnings 0 \"contracts/**/*.sol\" --fix", 47 | "test": "hardhat test" 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /test/FixedRateSwap.js: -------------------------------------------------------------------------------- 1 | const { ether, expectRevert } = require('@openzeppelin/test-helpers'); 2 | const { expect } = require('chai'); 3 | 4 | const { gasspectEVM } = require('./helpers/profileEVM'); 5 | const { assertRoughlyEqualValues, toBN } = require('./helpers/utils'); 6 | 7 | const FixedRateSwap = artifacts.require('FixedRateSwap'); 8 | const TokenMock = artifacts.require('TokenMock'); 9 | 10 | contract('FixedRateSwap', function ([wallet1, wallet2]) { 11 | const arbitraryPrecision = '0.0000000000000001'; // 1e-16 12 | const precision = '0.001'; 13 | const swapAmount = ether('1'); 14 | const feeAmount = toBN('214674003683125'); 15 | 16 | before(async function () { 17 | this.USDT = await TokenMock.new('USDT', 'USDT'); 18 | this.USDC = await TokenMock.new('USDC', 'USDC'); 19 | this.fixedRateSwap = await FixedRateSwap.new(this.USDT.address, this.USDC.address, 'FixedRateSwap', 'FRS', 18); 20 | }); 21 | 22 | beforeEach(async function () { 23 | for (const addr of [wallet1, wallet2]) { 24 | for (const token of [this.USDT, this.USDC]) { 25 | await token.mint(addr, ether('1000')); 26 | await token.approve(this.fixedRateSwap.address, ether('1000'), { from: addr }); 27 | } 28 | } 29 | }); 30 | 31 | afterEach(async function () { 32 | for (const addr of [wallet1, wallet2]) { 33 | const p = await this.fixedRateSwap.balanceOf(addr); 34 | if (!p.isZero()) { 35 | await this.fixedRateSwap.withdraw(p, '0', '0', { from: addr }); 36 | } 37 | for (const token of [this.USDT, this.USDC]) { 38 | const b = await token.balanceOf(addr); 39 | if (!b.isZero()) { 40 | await token.burn(addr, b); 41 | } 42 | } 43 | } 44 | }); 45 | 46 | describe('Arbitrary withdrawal', async function () { 47 | beforeEach(async function () { 48 | await this.fixedRateSwap.deposit(ether('1'), ether('1'), '0'); 49 | }); 50 | 51 | it('should be equal to (withdraw + swap) {1:9}', async function () { 52 | // arbitrary withdraw 53 | const arbitraryBalances = await this.fixedRateSwap.contract.methods.withdrawWithRatio(ether('1'), ether('0.1'), '0', '0').call(); 54 | 55 | // withdraw + swap 56 | const balances = await this.fixedRateSwap.contract.methods.withdraw(ether('1'), '0', '0').call(); 57 | await this.fixedRateSwap.withdraw(ether('1'), '0', '0'); 58 | 59 | const swapAmount = ether('0.400004147274109535'); 60 | const token1Amount = await this.fixedRateSwap.contract.methods.swap0To1(swapAmount, '0').call(); 61 | 62 | assertRoughlyEqualValues(toBN(arbitraryBalances.token0Amount), toBN(balances.token0Amount).sub(swapAmount), arbitraryPrecision); 63 | assertRoughlyEqualValues(toBN(arbitraryBalances.token1Amount), toBN(balances.token1Amount).add(toBN(token1Amount)), arbitraryPrecision); 64 | assertRoughlyEqualValues( 65 | toBN(1e18).mul(toBN(balances.token0Amount).sub(swapAmount)).div(toBN(balances.token1Amount).add(toBN(token1Amount))), 66 | toBN(1e18).muln(1).divn(9), 67 | arbitraryPrecision, 68 | ); 69 | }); 70 | 71 | it('should be equal to (withdraw + swap) {9:1}', async function () { 72 | // arbitrary withdraw 73 | const arbitraryBalances = await this.fixedRateSwap.contract.methods.withdrawWithRatio(ether('1'), ether('0.9'), '0', '0').call(); 74 | 75 | // withdraw + swap 76 | const balances = await this.fixedRateSwap.contract.methods.withdraw(ether('1'), '0', '0').call(); 77 | await this.fixedRateSwap.withdraw(ether('1'), '0', '0'); 78 | 79 | const swapAmount = ether('0.400004147274109535'); 80 | const token0Amount = await this.fixedRateSwap.contract.methods.swap1To0(swapAmount, '0').call(); 81 | 82 | assertRoughlyEqualValues(toBN(arbitraryBalances.token0Amount), toBN(balances.token0Amount).add(toBN(token0Amount)), arbitraryPrecision); 83 | assertRoughlyEqualValues(toBN(arbitraryBalances.token1Amount), toBN(balances.token1Amount).sub(swapAmount), arbitraryPrecision); 84 | assertRoughlyEqualValues( 85 | toBN(1e18).mul(toBN(balances.token0Amount).add(toBN(token0Amount))).div(toBN(balances.token1Amount).sub(swapAmount)), 86 | toBN(1e18).muln(9).divn(1), 87 | arbitraryPrecision, 88 | ); 89 | }); 90 | 91 | it('should be equal to (withdraw + swap) {0:1}', async function () { 92 | // arbitrary withdraw 93 | const arbitraryBalances = await this.fixedRateSwap.contract.methods.withdrawWithRatio(ether('1'), ether('0'), '0', '0').call(); 94 | 95 | // withdraw + swap 96 | const balances = await this.fixedRateSwap.contract.methods.withdraw(ether('1'), '0', '0').call(); 97 | await this.fixedRateSwap.withdraw(ether('1'), '0', '0'); 98 | 99 | const swapAmount = ether('0.5'); 100 | const token1Amount = await this.fixedRateSwap.contract.methods.swap0To1(swapAmount, '0').call(); 101 | 102 | assertRoughlyEqualValues(toBN(arbitraryBalances.token0Amount), toBN(balances.token0Amount).sub(swapAmount), arbitraryPrecision); 103 | assertRoughlyEqualValues(toBN(arbitraryBalances.token1Amount), toBN(balances.token1Amount).add(toBN(token1Amount)), arbitraryPrecision); 104 | assertRoughlyEqualValues(toBN(arbitraryBalances.token0Amount), toBN('0'), arbitraryPrecision); 105 | }); 106 | 107 | it('should be equal to (withdraw + swap) {1:1}', async function () { 108 | // arbitrary withdraw 109 | const arbitraryBalances = await this.fixedRateSwap.contract.methods.withdrawWithRatio(ether('1'), ether('0.5'), '0', '0').call(); 110 | 111 | // withdraw 112 | const balances = await this.fixedRateSwap.contract.methods.withdraw(ether('1'), '0', '0').call(); 113 | 114 | expect(arbitraryBalances.token0Amount).to.bignumber.equal(balances.token0Amount); 115 | expect(arbitraryBalances.token1Amount).to.bignumber.equal(balances.token1Amount); 116 | }); 117 | }); 118 | 119 | describe('Arbitrary deposit', async function () { 120 | async function swap0to1Test (token0Balance, token1Balance, token0Deposit, token1Deposit, swapAmount) { 121 | await this.fixedRateSwap.deposit(token0Balance, token1Balance, '0'); 122 | expect(await this.USDT.balanceOf(this.fixedRateSwap.address)).to.be.bignumber.equal(token0Balance); 123 | expect(await this.USDC.balanceOf(this.fixedRateSwap.address)).to.be.bignumber.equal(token1Balance); 124 | 125 | // arbitrary deposit 126 | const lpBalanceWallet = await this.fixedRateSwap.contract.methods.deposit(token0Deposit, token1Deposit, '0').call(); 127 | const arbitraryLpBalance = toBN(lpBalanceWallet); 128 | 129 | // swap + deposit 130 | const outputAmount = await this.fixedRateSwap.contract.methods.swap0To1(swapAmount, '0').call(); 131 | await this.fixedRateSwap.contract.methods.swap0To1(swapAmount, '0').send({ from: wallet1 }); 132 | 133 | const balance0 = await this.USDT.balanceOf(this.fixedRateSwap.address); 134 | const balance1 = await this.USDC.balanceOf(this.fixedRateSwap.address); 135 | assertRoughlyEqualValues( 136 | toBN(1e18).mul(toBN(outputAmount)).div(token0Deposit.sub(swapAmount)), 137 | toBN(1e18).mul(balance1).div(balance0), 138 | arbitraryPrecision, 139 | ); 140 | 141 | const lpBalance = await this.fixedRateSwap.contract.methods.deposit(token0Deposit.sub(swapAmount), outputAmount, '0').call(); 142 | assertRoughlyEqualValues(arbitraryLpBalance, lpBalance, arbitraryPrecision); 143 | }; 144 | 145 | async function swap1to0Test (token0Balance, token1Balance, token0Deposit, token1Deposit, swapAmount) { 146 | await this.fixedRateSwap.deposit(token0Balance, token1Balance, '0'); 147 | expect(await this.USDT.balanceOf(this.fixedRateSwap.address)).to.be.bignumber.equal(token0Balance); 148 | expect(await this.USDC.balanceOf(this.fixedRateSwap.address)).to.be.bignumber.equal(token1Balance); 149 | 150 | // arbitrary deposit 151 | const lpBalanceWallet = await this.fixedRateSwap.contract.methods.deposit(token0Deposit, token1Deposit, '0').call(); 152 | const arbitraryLpBalance = web3.utils.toBN(lpBalanceWallet); 153 | 154 | // swap + deposit 155 | const outputAmount = await this.fixedRateSwap.contract.methods.swap1To0(swapAmount, '0').call(); 156 | await this.fixedRateSwap.contract.methods.swap1To0(swapAmount, '0').send({ from: wallet1 }); 157 | 158 | const balance0 = await this.USDT.balanceOf(this.fixedRateSwap.address); 159 | const balance1 = await this.USDC.balanceOf(this.fixedRateSwap.address); 160 | assertRoughlyEqualValues( 161 | toBN(1e18).mul(toBN(outputAmount)).div(token1Deposit.sub(swapAmount)), 162 | toBN(1e18).mul(balance0).div(balance1), 163 | arbitraryPrecision, 164 | ); 165 | 166 | const lpBalance = await this.fixedRateSwap.contract.methods.deposit(outputAmount, token1Deposit.sub(swapAmount), '0').call(); 167 | assertRoughlyEqualValues(arbitraryLpBalance, lpBalance, arbitraryPrecision); 168 | }; 169 | 170 | it('should be equal to (swap + deposit) {balances, deposit} = {(0,100), (1,0)}', async function () { 171 | await swap0to1Test.call(this, ether('0'), ether('100'), ether('1'), ether('0'), ether('0.990099171224324018')); 172 | }); 173 | 174 | it('should be equal to (swap + deposit) {balances, deposit} = {(100,0), (0,1)}', async function () { 175 | await swap1to0Test.call(this, ether('100'), ether('0'), ether('0'), ether('1'), ether('0.990099171224324018')); 176 | }); 177 | 178 | it('should be equal to (swap + deposit) {balances, deposit} = {(100,100), (0,1)}', async function () { 179 | await swap1to0Test.call(this, ether('100'), ether('100'), ether('0'), ether('1'), ether('0.497537438448399645')); 180 | }); 181 | 182 | it('should be equal to (swap + deposit) {balances, deposit} = {(100,100), (1,0)}', async function () { 183 | await swap0to1Test.call(this, ether('100'), ether('100'), ether('1'), ether('0'), ether('0.497537438448399645')); 184 | }); 185 | }); 186 | 187 | describe('Deposits', async function () { 188 | it.skip('should be cheap', async function () { 189 | await this.fixedRateSwap.deposit(ether('1'), ether('1'), '0'); 190 | await this.fixedRateSwap.deposit(ether('0.5'), ether('1'), '0'); 191 | await this.fixedRateSwap.deposit(ether('1'), ether('0.5'), '0'); 192 | await this.fixedRateSwap.deposit(ether('1'), ether('0'), '0'); 193 | const receipt = await this.fixedRateSwap.deposit(ether('0'), ether('1'), '0'); 194 | gasspectEVM(receipt.tx); 195 | }); 196 | 197 | describe('mint lp', async function () { 198 | const tests = [ 199 | ['0', '100', '0', '1', '1'], 200 | ['0', '100', '1', '0', '1'], 201 | ['0', '100', '1', '1', '2'], 202 | ['100', '0', '1', '0', '1'], 203 | ['100', '0', '0', '1', '1'], 204 | ['100', '0', '1', '1', '2'], 205 | ['100', '100', '1', '0', '1'], 206 | ['100', '100', '0', '1', '1'], 207 | ['100', '100', '1', '1', '2'], 208 | ]; 209 | 210 | tests.forEach(test => { 211 | it(`should mint ${test[4]} lp when {balances, deposit} = {(${test[0]}, ${test[1]}), (${test[2]}, ${test[3]})}`, async function () { 212 | const usdtBalance = ether(test[0]); 213 | const usdcBalance = ether(test[1]); 214 | const usdtDeposit = ether(test[2]); 215 | const usdcDeposit = ether(test[3]); 216 | const lpResult = ether(test[4]); 217 | 218 | await this.fixedRateSwap.deposit(usdtBalance, usdcBalance, '0'); 219 | expect(await this.USDT.balanceOf(this.fixedRateSwap.address)).to.be.bignumber.equal(usdtBalance); 220 | expect(await this.USDC.balanceOf(this.fixedRateSwap.address)).to.be.bignumber.equal(usdcBalance); 221 | 222 | await this.fixedRateSwap.deposit(usdtDeposit, usdcDeposit, '0', { from: wallet2 }); 223 | assertRoughlyEqualValues(await this.fixedRateSwap.balanceOf(wallet2), lpResult, precision); 224 | }); 225 | }); 226 | }); 227 | 228 | it('should be denied for zero amount', async function () { 229 | await expectRevert( 230 | this.fixedRateSwap.deposit(ether('0'), ether('0'), '0'), 231 | 'Empty deposit is not allowed', 232 | ); 233 | }); 234 | 235 | it('should allow low unbalanced deposit', async function () { 236 | await this.fixedRateSwap.deposit(ether('1'), '0', '0'); 237 | await this.fixedRateSwap.contract.methods.deposit('100000', '100000', '0').call({ from: wallet2 }); 238 | await this.fixedRateSwap.contract.methods.deposit('100000', '0', '0').call({ from: wallet2 }); 239 | await this.fixedRateSwap.contract.methods.deposit('0', '100000', '0').call({ from: wallet2 }); 240 | }); 241 | 242 | it('should give the same shares for the same deposits', async function () { 243 | await this.fixedRateSwap.deposit(ether('1'), ether('1'), '0'); 244 | expect(await this.fixedRateSwap.balanceOf(wallet1)).to.be.bignumber.equal(ether('2')); 245 | await this.fixedRateSwap.deposit(ether('1'), ether('1'), '0'); 246 | expect(await this.fixedRateSwap.balanceOf(wallet1)).to.be.bignumber.equal(ether('4')); 247 | }); 248 | }); 249 | 250 | describe('Withdrawals', async function () { 251 | beforeEach(async function () { 252 | this.usdtBalanceBefore = await this.USDT.balanceOf(wallet1); 253 | this.usdcBalanceBefore = await this.USDC.balanceOf(wallet1); 254 | await this.fixedRateSwap.deposit(ether('1'), ether('1'), '0'); 255 | }); 256 | 257 | it('should be able to withdraw fully', async function () { 258 | await this.fixedRateSwap.withdraw(ether('2'), '0', '0'); 259 | expect(await this.fixedRateSwap.balanceOf(wallet1)).to.be.bignumber.equal('0'); 260 | expect(await this.USDT.balanceOf(this.fixedRateSwap.address)).to.be.bignumber.equal('0'); 261 | expect(await this.USDC.balanceOf(this.fixedRateSwap.address)).to.be.bignumber.equal('0'); 262 | expect(await this.USDT.balanceOf(wallet1)).to.be.bignumber.equal(this.usdtBalanceBefore); 263 | expect(await this.USDC.balanceOf(wallet1)).to.be.bignumber.equal(this.usdcBalanceBefore); 264 | }); 265 | 266 | it('should be able to withdraw partially', async function () { 267 | await this.fixedRateSwap.withdraw(ether('1'), '0', '0'); 268 | expect(await this.fixedRateSwap.balanceOf(wallet1)).to.be.bignumber.equal(ether('1')); 269 | expect(await this.USDT.balanceOf(this.fixedRateSwap.address)).to.be.bignumber.equal(ether('0.5')); 270 | expect(await this.USDC.balanceOf(this.fixedRateSwap.address)).to.be.bignumber.equal(ether('0.5')); 271 | expect(await this.USDT.balanceOf(wallet1)).to.be.bignumber.equal(this.usdtBalanceBefore.sub(ether('0.5'))); 272 | expect(await this.USDC.balanceOf(wallet1)).to.be.bignumber.equal(this.usdcBalanceBefore.sub(ether('0.5'))); 273 | }); 274 | 275 | describe('ratio withdrawals', async function () { 276 | const tests = [ 277 | ['1', '1', '0', '0'], 278 | ['1', '9', '4147274109535', '37325466985822'], 279 | ['9', '1', '37325466985822', '4147274109535'], 280 | ['1', '0', '107337001841563', '0'], 281 | ['0', '1', '0', '107337001841563'], 282 | ]; 283 | const withdrawalAmount = ether('1'); 284 | 285 | tests.forEach(test => { 286 | it(`should be able to withdraw with ${test[0]}:${test[1]}`, async function () { 287 | const token0Amount = ether(test[0]).div(toBN(test[0]).add(toBN(test[1]))); 288 | const token1Amount = ether(test[1]).div(toBN(test[0]).add(toBN(test[1]))); 289 | await this.fixedRateSwap.withdrawWithRatio(withdrawalAmount, token0Amount, '0', '0'); 290 | expect(await this.fixedRateSwap.balanceOf(wallet1)).to.be.bignumber.equal(withdrawalAmount); 291 | const token0Fee = toBN(test[2]); 292 | const token1Fee = toBN(test[3]); 293 | expect(await this.USDT.balanceOf(this.fixedRateSwap.address)).to.bignumber.equal(ether('1').sub(token0Amount).add(token0Fee)); 294 | expect(await this.USDC.balanceOf(this.fixedRateSwap.address)).to.bignumber.equal(ether('1').sub(token1Amount).add(token1Fee)); 295 | }); 296 | }); 297 | }); 298 | 299 | it('should revert when withdraw too much', async function () { 300 | await expectRevert( 301 | this.fixedRateSwap.withdraw(ether('3'), '0', '0'), 302 | 'ERC20: burn amount exceeds balance', 303 | ); 304 | }); 305 | 306 | it('should revert when withdrawWithRatio too much', async function () { 307 | await expectRevert( 308 | this.fixedRateSwap.withdrawWithRatio(ether('3'), '0', '0', '0'), 309 | 'ERC20: burn amount exceeds balance', 310 | ); 311 | }); 312 | 313 | it('should revert when withdraw too much in one token', async function () { 314 | await expectRevert( 315 | this.fixedRateSwap.withdrawWithRatio(ether('2'), ether('0'), '0', '0'), 316 | 'Amount exceeds total balance', 317 | ); 318 | }); 319 | 320 | it('should withdraw all after swap', async function () { 321 | await this.fixedRateSwap.swap0To1(swapAmount, '0', { from: wallet2 }); 322 | await this.fixedRateSwap.withdraw(ether('2'), '0', '0'); 323 | expect(await this.USDT.balanceOf(wallet1)).to.be.bignumber.equal(this.usdtBalanceBefore.add(swapAmount)); 324 | expect(await this.USDC.balanceOf(wallet1)).to.be.bignumber.equal(this.usdcBalanceBefore.sub(swapAmount).add(feeAmount)); 325 | }); 326 | }); 327 | 328 | describe('Swaps', async function () { 329 | beforeEach(async function () { 330 | await this.fixedRateSwap.deposit(ether('1'), ether('1'), '0'); 331 | this.usdtBalanceBefore = await this.USDT.balanceOf(wallet1); 332 | this.usdcBalanceBefore = await this.USDC.balanceOf(wallet1); 333 | }); 334 | 335 | it('should swap directly', async function () { 336 | await this.fixedRateSwap.swap0To1(swapAmount, '0'); 337 | expect(await this.USDT.balanceOf(wallet1)).to.bignumber.equal(this.usdtBalanceBefore.sub(swapAmount)); 338 | expect(await this.USDC.balanceOf(wallet1)).to.bignumber.equal(this.usdcBalanceBefore.add(swapAmount).sub(feeAmount)); 339 | }); 340 | 341 | it('should swap inversly', async function () { 342 | await this.fixedRateSwap.swap1To0(swapAmount, '0'); 343 | expect(await this.USDC.balanceOf(wallet1)).to.bignumber.equal(this.usdcBalanceBefore.sub(swapAmount)); 344 | expect(await this.USDT.balanceOf(wallet1)).to.bignumber.equal(this.usdtBalanceBefore.add(swapAmount).sub(feeAmount)); 345 | }); 346 | 347 | it('should error with "input amount is too big"', async function () { 348 | await expectRevert( 349 | this.fixedRateSwap.swap1To0(ether('2'), '0'), 350 | 'Input amount is too big', 351 | ); 352 | }); 353 | 354 | it('should be error input amount is too big {balances, deposit} = {(100,0), (1,0)}', async function () { 355 | await expectRevert( 356 | this.fixedRateSwap.swap0To1(ether('2'), '0'), 357 | 'Input amount is too big', 358 | ); 359 | }); 360 | }); 361 | 362 | it('deposit/withdraw should be more expensive than swap', async function () { 363 | await this.fixedRateSwap.deposit(ether('100'), ether('100'), '0'); 364 | const swapResult = await this.fixedRateSwap.contract.methods.swap0To1(swapAmount, '0').call({ from: wallet2 }); 365 | const depositResult = await this.fixedRateSwap.contract.methods.deposit(swapAmount, '0', '0').call({ from: wallet2 }); 366 | await this.fixedRateSwap.deposit(swapAmount, '0', '0', { from: wallet2 }); 367 | const withdrawResult = await this.fixedRateSwap.contract.methods.withdrawWithRatio(depositResult, ether('0'), '0', '0').call({ from: wallet2 }); 368 | expect(withdrawResult.token1Amount).to.be.bignumber.lt(swapResult); 369 | }); 370 | }); 371 | -------------------------------------------------------------------------------- /test/helpers/profileEVM.js: -------------------------------------------------------------------------------- 1 | const { promisify } = require('util'); 2 | // const fs = require('fs').promises; 3 | 4 | function _normalizeOp (op) { 5 | if (op.op === 'STATICCALL') { 6 | if (op.stack.length > 8 && op.stack[op.stack.length - 8] === '0000000000000000000000000000000000000000000000000000000000000001') { 7 | op.gasCost = 700 + 3000; 8 | op.op = 'STATICCALL-ECRECOVER'; 9 | } else if (op.stack.length > 8 && op.stack[op.stack.length - 8] <= '00000000000000000000000000000000000000000000000000000000000000FF') { 10 | op.gasCost = 700; 11 | op.op = 'STATICCALL-' + op.stack[op.stack.length - 8].substr(62, 2); 12 | } else { 13 | op.gasCost = 700; 14 | } 15 | } 16 | if (['CALL', 'DELEGATECALL', 'CALLCODE'].indexOf(op.op) !== -1) { 17 | op.gasCost = 700; 18 | } 19 | if (['RETURN', 'REVERT', 'INVALID'].indexOf(op.op) !== -1) { 20 | op.gasCost = 3; 21 | } 22 | } 23 | 24 | async function profileEVM (txHash, instruction) { 25 | const trace = await promisify(web3.currentProvider.send.bind(web3.currentProvider))({ 26 | jsonrpc: '2.0', 27 | method: 'debug_traceTransaction', 28 | params: [txHash, {}], 29 | id: new Date().getTime(), 30 | }); 31 | 32 | const str = JSON.stringify(trace); 33 | 34 | // await fs.writeFile("./trace.json", str); 35 | 36 | if (Array.isArray(instruction)) { 37 | return instruction.map(instr => { 38 | return str.split('"' + instr.toUpperCase() + '"').length - 1; 39 | }); 40 | } 41 | 42 | return str.split('"' + instruction.toUpperCase() + '"').length - 1; 43 | } 44 | 45 | async function gasspectEVM (txHash) { 46 | const trace = await promisify(web3.currentProvider.send.bind(web3.currentProvider))({ 47 | jsonrpc: '2.0', 48 | method: 'debug_traceTransaction', 49 | params: [txHash, {}], 50 | id: new Date().getTime(), 51 | }); 52 | 53 | const ops = trace.result.structLogs; 54 | 55 | const traceAddress = [0, -1]; 56 | for (const op of ops) { 57 | op.traceAddress = traceAddress.slice(0, traceAddress.length - 1); 58 | _normalizeOp(op); 59 | 60 | if (op.depth + 2 > traceAddress.length) { 61 | traceAddress[traceAddress.length - 1] += 1; 62 | traceAddress.push(-1); 63 | } 64 | 65 | if (op.depth + 2 < traceAddress.length) { 66 | traceAddress.pop(); 67 | } 68 | } 69 | 70 | console.log(ops.filter(op => op.gasCost > 300).map(op => op.traceAddress.join('-') + '-' + op.op + ' = ' + op.gasCost)); 71 | 72 | // await fs.writeFile("./trace-3.json", JSON.stringify(ops)); 73 | 74 | // const res = ops.reduce((dict, op) => { 75 | // const key = op.traceAddress.join('-') + '-' + op.op; 76 | // dict[key] = (dict[key] || 0) + op.gasCost; 77 | // return dict; 78 | // }, {}); 79 | 80 | // const entries = Object.keys(res).map(k => [k, res[k]]); 81 | // entries.sort((a,b) => b[1].gasCost - a[1].gasCost); 82 | // console.log(entries.map(([k, op]) => k + ' = ' + op.gasCost)); 83 | } 84 | 85 | module.exports = { 86 | profileEVM, 87 | gasspectEVM, 88 | }; 89 | -------------------------------------------------------------------------------- /test/helpers/utils.js: -------------------------------------------------------------------------------- 1 | const { expect } = require('chai'); 2 | 3 | function assertRoughlyEqualValues (expected, actual, relativeDiff) { 4 | const expectedBN = toBN(expected); 5 | const actualBN = toBN(actual); 6 | 7 | let multiplerNumerator = relativeDiff; 8 | let multiplerDenominator = toBN('1'); 9 | while (!Number.isInteger(multiplerNumerator)) { 10 | multiplerDenominator = multiplerDenominator.mul(toBN('10')); 11 | multiplerNumerator *= 10; 12 | } 13 | const diff = expectedBN.sub(actualBN).abs(); 14 | const treshold = expectedBN.mul(toBN(multiplerNumerator)).div(multiplerDenominator); 15 | if (!diff.lte(treshold)) { 16 | expect(actualBN).to.be.bignumber.equal(expectedBN, `${actualBN} != ${expectedBN} with ${relativeDiff} precision`); 17 | } 18 | } 19 | 20 | function toBN (val) { 21 | return web3.utils.toBN(val); 22 | } 23 | 24 | module.exports = { 25 | assertRoughlyEqualValues, 26 | toBN, 27 | }; 28 | --------------------------------------------------------------------------------