@dolomite-exchange/dolomite-margin 0.2.6 → 0.2.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (200) hide show
  1. package/build/contracts/Account.json +11 -11
  2. package/build/contracts/Actions.json +11 -11
  3. package/build/contracts/Address.json +1 -1
  4. package/build/contracts/Admin.json +23 -23
  5. package/build/contracts/AdminImpl.json +35 -307
  6. package/build/contracts/AdvancedMath.json +7 -7
  7. package/build/contracts/AmmRebalancerProxy.json +577 -621
  8. package/build/contracts/AmmRebalancerProxyV1.json +78 -32
  9. package/build/contracts/Babylonian.json +1 -1
  10. package/build/contracts/Bits.json +9 -9
  11. package/build/contracts/Cache.json +13 -13
  12. package/build/contracts/ChainlinkPriceOracleV1.json +16 -16
  13. package/build/contracts/Context.json +1 -1
  14. package/build/contracts/CustomTestToken.json +9 -9
  15. package/build/contracts/Decimal.json +9 -9
  16. package/build/contracts/DelayedMultiSig.json +9 -9
  17. package/build/contracts/DolomiteAmmERC20.json +11 -11
  18. package/build/contracts/DolomiteAmmFactory.json +14 -52
  19. package/build/contracts/DolomiteAmmLibrary.json +24 -24
  20. package/build/contracts/DolomiteAmmPair.json +27 -27
  21. package/build/contracts/DolomiteAmmRouterProxy.json +30 -363
  22. package/build/contracts/DolomiteMargin.json +21 -67
  23. package/build/contracts/DoubleExponentInterestSetter.json +15 -21
  24. package/build/contracts/EnumerableSet.json +7 -7
  25. package/build/contracts/ErroringOmiseToken.json +9 -9
  26. package/build/contracts/ErroringToken.json +10 -10
  27. package/build/contracts/Events.json +17 -17
  28. package/build/contracts/ExcessivelySafeCall.json +35 -35
  29. package/build/contracts/Exchange.json +15 -15
  30. package/build/contracts/Expiry.json +36 -36
  31. package/build/contracts/Getters.json +33 -33
  32. package/build/contracts/IAutoTrader.json +9 -9
  33. package/build/contracts/ICallee.json +7 -7
  34. package/build/contracts/IChainlinkAggregator.json +5 -5
  35. package/build/contracts/IDolomiteAmmERC20.json +5 -5
  36. package/build/contracts/IDolomiteAmmFactory.json +9 -9
  37. package/build/contracts/IDolomiteAmmPair.json +9 -9
  38. package/build/contracts/IDolomiteMargin.json +23 -23
  39. package/build/contracts/IERC20.json +1 -1
  40. package/build/contracts/IERC20Detailed.json +5 -5
  41. package/build/contracts/IExchangeWrapper.json +5 -5
  42. package/build/contracts/IExpiry.json +9 -9
  43. package/build/contracts/IInterestSetter.json +7 -7
  44. package/build/contracts/ILiquidationCallback.json +7 -7
  45. package/build/contracts/IMakerOracle.json +5 -5
  46. package/build/contracts/IOasisDex.json +5 -5
  47. package/build/contracts/IPriceOracle.json +7 -7
  48. package/build/contracts/IRecyclable.json +9 -9
  49. package/build/contracts/ITransferProxy.json +5 -5
  50. package/build/contracts/IUniswapV2Callee.json +5 -5
  51. package/build/contracts/IUniswapV2Factory.json +5 -5
  52. package/build/contracts/IUniswapV2Pair.json +5 -5
  53. package/build/contracts/IUniswapV2Router.json +5 -5
  54. package/build/contracts/IWETH.json +5 -5
  55. package/build/contracts/Interest.json +15 -15
  56. package/build/contracts/LiquidateOrVaporizeImpl.json +32 -83
  57. package/build/contracts/LiquidatorProxyHelper.json +23 -23
  58. package/build/contracts/LiquidatorProxyV1.json +30 -36
  59. package/build/contracts/LiquidatorProxyV1WithAmm.json +49 -105
  60. package/build/contracts/Math.json +1 -1
  61. package/build/contracts/Migrations.json +8 -14
  62. package/build/contracts/Monetary.json +7 -7
  63. package/build/contracts/MultiCall.json +8 -14
  64. package/build/contracts/MultiSig.json +7 -7
  65. package/build/contracts/OmiseToken.json +8 -8
  66. package/build/contracts/OnlyDolomiteMargin.json +11 -11
  67. package/build/contracts/Operation.json +15 -15
  68. package/build/contracts/OperationImpl.json +39 -47
  69. package/build/contracts/Ownable.json +1 -1
  70. package/build/contracts/PartiallyDelayedMultiSig.json +9 -9
  71. package/build/contracts/PayableProxy.json +17 -23
  72. package/build/contracts/Permission.json +11 -11
  73. package/build/contracts/PolynomialInterestSetter.json +13 -13
  74. package/build/contracts/RecyclableTokenProxy.json +25 -25
  75. package/build/contracts/ReentrancyGuard.json +1 -1
  76. package/build/contracts/Require.json +7 -7
  77. package/build/contracts/SafeERC20.json +1 -1
  78. package/build/contracts/SafeETH.json +7 -7
  79. package/build/contracts/SafeLiquidationCallback.json +17 -17
  80. package/build/contracts/SafeMath.json +1 -1
  81. package/build/contracts/SignedOperationProxy.json +21 -104
  82. package/build/contracts/SimpleFeeOwner.json +18 -63
  83. package/build/contracts/State.json +9 -9
  84. package/build/contracts/Storage.json +37 -37
  85. package/build/contracts/TestAmmRebalancerProxy.json +32 -58
  86. package/build/contracts/TestAutoTrader.json +22 -22
  87. package/build/contracts/TestBtcUsdChainlinkAggregator.json +10 -10
  88. package/build/contracts/TestCallee.json +19 -19
  89. package/build/contracts/TestChainlinkPriceOracleV1.json +10 -94
  90. package/build/contracts/TestCounter.json +7 -7
  91. package/build/contracts/TestDaiUsdChainlinkAggregator.json +11 -11
  92. package/build/contracts/TestDolomiteMargin.json +22 -22
  93. package/build/contracts/TestDoubleExponentInterestSetter.json +10 -10
  94. package/build/contracts/TestEthUsdChainlinkAggregator.json +11 -11
  95. package/build/contracts/TestExchangeWrapper.json +28 -28
  96. package/build/contracts/TestInterestSetter.json +13 -13
  97. package/build/contracts/TestLib.json +22 -22
  98. package/build/contracts/TestLinkUsdChainlinkAggregator.json +11 -11
  99. package/build/contracts/TestLiquidateCallback.json +719 -719
  100. package/build/contracts/TestLiquidationCallback.json +13 -13
  101. package/build/contracts/TestLrcEthChainlinkAggregator.json +11 -11
  102. package/build/contracts/TestMakerOracle.json +10 -10
  103. package/build/contracts/TestMaticUsdChainlinkAggregator.json +10 -10
  104. package/build/contracts/TestOasisDex.json +12 -12
  105. package/build/contracts/TestOperationImpl.json +14 -14
  106. package/build/contracts/TestPolynomialInterestSetter.json +10 -10
  107. package/build/contracts/TestPriceAggregator.json +9 -9
  108. package/build/contracts/TestPriceOracle.json +13 -13
  109. package/build/contracts/TestRecyclableToken.json +9 -9
  110. package/build/contracts/TestSimpleCallee.json +15 -15
  111. package/build/contracts/TestToken.json +9 -9
  112. package/build/contracts/TestTrader.json +17 -17
  113. package/build/contracts/TestUniswapAmmRebalancerProxy.json +16 -16
  114. package/build/contracts/TestUsdcUsdChainlinkAggregator.json +11 -11
  115. package/build/contracts/TestWETH.json +9 -9
  116. package/build/contracts/Time.json +9 -9
  117. package/build/contracts/Token.json +9 -9
  118. package/build/contracts/TokenA.json +10 -10
  119. package/build/contracts/TokenB.json +10 -10
  120. package/build/contracts/TokenC.json +10 -10
  121. package/build/contracts/TokenD.json +10 -10
  122. package/build/contracts/TokenE.json +10 -10
  123. package/build/contracts/TokenF.json +10 -10
  124. package/build/contracts/TransferProxy.json +22 -28
  125. package/build/contracts/TypedSignature.json +9 -9
  126. package/build/contracts/Types.json +9 -9
  127. package/build/contracts/UQ112x112.json +7 -7
  128. package/build/contracts/UniswapV2ERC20.json +7 -7
  129. package/build/contracts/UniswapV2Factory.json +12 -12
  130. package/build/contracts/UniswapV2Library.json +20 -20
  131. package/build/contracts/UniswapV2Pair.json +17 -17
  132. package/build/contracts/UniswapV2Router02.json +16 -16
  133. package/build/contracts/WETH9.json +1 -1
  134. package/contracts/external/amm/DolomiteAmmERC20.sol +135 -0
  135. package/contracts/external/amm/DolomiteAmmFactory.sol +122 -0
  136. package/contracts/external/amm/DolomiteAmmPair.sol +573 -0
  137. package/contracts/external/amm/SimpleFeeOwner.sol +107 -0
  138. package/contracts/external/helpers/LiquidatorProxyHelper.sol +252 -0
  139. package/contracts/external/helpers/OnlyDolomiteMargin.sol +63 -0
  140. package/contracts/external/interestsetters/DoubleExponentInterestSetter.sol +212 -0
  141. package/contracts/external/interestsetters/PolynomialInterestSetter.sol +205 -0
  142. package/contracts/external/interfaces/IChainlinkAggregator.sol +33 -0
  143. package/contracts/external/interfaces/IDolomiteAmmERC20.sol +52 -0
  144. package/contracts/external/interfaces/IDolomiteAmmFactory.sol +42 -0
  145. package/contracts/external/interfaces/IDolomiteAmmPair.sol +116 -0
  146. package/contracts/external/interfaces/IExpiry.sol +70 -0
  147. package/contracts/external/interfaces/IMakerOracle.sol +52 -0
  148. package/contracts/external/interfaces/IOasisDex.sol +326 -0
  149. package/contracts/external/interfaces/ITransferProxy.sol +53 -0
  150. package/contracts/external/interfaces/IUniswapV2Router.sol +134 -0
  151. package/contracts/external/lib/AdvancedMath.sol +23 -0
  152. package/contracts/external/lib/DolomiteAmmLibrary.sol +323 -0
  153. package/contracts/external/lib/TypedSignature.sol +120 -0
  154. package/contracts/external/lib/UQ112x112.sol +22 -0
  155. package/contracts/external/multisig/DelayedMultiSig.sol +206 -0
  156. package/contracts/external/multisig/MultiSig.sol +571 -0
  157. package/contracts/external/multisig/PartiallyDelayedMultiSig.sol +174 -0
  158. package/contracts/external/oracles/ChainlinkPriceOracleV1.sol +197 -0
  159. package/contracts/external/oracles/TestChainlinkPriceOracleV1.sol +98 -0
  160. package/contracts/external/proxies/AmmRebalancerProxyV1.sol +465 -0
  161. package/contracts/external/proxies/DolomiteAmmRouterProxy.sol +877 -0
  162. package/contracts/external/proxies/LiquidatorProxyV1.sol +507 -0
  163. package/contracts/external/proxies/LiquidatorProxyV1WithAmm.sol +574 -0
  164. package/contracts/external/proxies/PayableProxy.sol +146 -0
  165. package/contracts/external/proxies/RecyclableTokenProxy.sol +463 -0
  166. package/contracts/external/proxies/SignedOperationProxy.sol +553 -0
  167. package/contracts/external/proxies/TransferProxy.sol +207 -0
  168. package/contracts/external/traders/Expiry.sol +532 -0
  169. package/contracts/external/uniswap-v2/UniswapV2ERC20.sol +118 -0
  170. package/contracts/external/uniswap-v2/UniswapV2Factory.sol +67 -0
  171. package/contracts/external/uniswap-v2/UniswapV2Pair.sol +283 -0
  172. package/contracts/external/uniswap-v2/UniswapV2Router02.sol +566 -0
  173. package/contracts/external/uniswap-v2/interfaces/IUniswapV2Callee.sol +13 -0
  174. package/contracts/external/uniswap-v2/interfaces/IUniswapV2Factory.sol +18 -0
  175. package/contracts/external/uniswap-v2/interfaces/IUniswapV2Pair.sol +67 -0
  176. package/contracts/external/uniswap-v2/interfaces/IWETH.sol +7 -0
  177. package/contracts/external/uniswap-v2/libraries/SafeETH.sol +29 -0
  178. package/contracts/external/uniswap-v2/libraries/UniswapV2Library.sol +117 -0
  179. package/contracts/external/utils/MultiCall.sol +95 -0
  180. package/contracts/protocol/impl/artifacts/OperationImpl.json +80 -0
  181. package/contracts/protocol/impl/artifacts/OperationImpl_metadata.json +193 -0
  182. package/dist/build/published_contracts/AdminImpl.json +2 -2
  183. package/dist/build/published_contracts/AmmRebalancerProxyV1.json +5 -0
  184. package/dist/build/published_contracts/ChainlinkPriceOracleV1.json +1 -1
  185. package/dist/build/published_contracts/DolomiteAmmFactory.json +1 -1
  186. package/dist/build/published_contracts/DolomiteAmmRouterProxy.json +1 -1
  187. package/dist/build/published_contracts/DoubleExponentInterestSetter.json +2 -2
  188. package/dist/build/published_contracts/Expiry.json +1 -1
  189. package/dist/build/published_contracts/LiquidatorProxyV1.json +1 -1
  190. package/dist/build/published_contracts/LiquidatorProxyV1WithAmm.json +1 -1
  191. package/dist/build/published_contracts/MultiCall.json +1 -1
  192. package/dist/build/published_contracts/PayableProxy.json +2 -2
  193. package/dist/build/published_contracts/SignedOperationProxy.json +2 -2
  194. package/dist/build/published_contracts/SimpleFeeOwner.json +1 -1
  195. package/dist/build/published_contracts/TestUniswapAmmRebalancerProxy.json +140 -0
  196. package/dist/build/published_contracts/TransferProxy.json +1 -1
  197. package/dist/src/lib/Contracts.d.ts +3 -1
  198. package/dist/src/lib/Contracts.js +7 -3
  199. package/dist/src/lib/Contracts.js.map +1 -1
  200. package/package.json +2 -1
@@ -0,0 +1,283 @@
1
+ pragma solidity ^0.5.16;
2
+
3
+ import "@openzeppelin/contracts/math/Math.sol";
4
+ import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
5
+
6
+ import "./interfaces/IUniswapV2Factory.sol";
7
+ import "./interfaces/IUniswapV2Callee.sol";
8
+
9
+ import "../lib/UQ112x112.sol";
10
+ import "../lib/AdvancedMath.sol";
11
+
12
+ import "./UniswapV2ERC20.sol";
13
+
14
+
15
+ contract UniswapV2Pair is UniswapV2ERC20 {
16
+ using SafeMath for uint;
17
+ using UQ112x112 for uint224;
18
+
19
+ uint public constant MINIMUM_LIQUIDITY = 10**3;
20
+ bytes4 private constant SELECTOR = bytes4(keccak256(bytes("transfer(address,uint256)")));
21
+
22
+ address public factory;
23
+ address public token0;
24
+ address public token1;
25
+
26
+ uint112 private reserve0; // uses single storage slot, accessible via getReserves
27
+ uint112 private reserve1; // uses single storage slot, accessible via getReserves
28
+ uint32 private blockTimestampLast; // uses single storage slot, accessible via getReserves
29
+
30
+ uint public price0CumulativeLast;
31
+ uint public price1CumulativeLast;
32
+ uint public kLast; // reserve0 * reserve1, as of immediately after the most recent liquidity event
33
+
34
+ uint private unlocked = 1;
35
+ modifier lock() {
36
+ require(unlocked == 1, "UniswapV2: LOCKED");
37
+ unlocked = 0;
38
+ _;
39
+ unlocked = 1;
40
+ }
41
+
42
+ event Mint(address indexed sender, uint amount0, uint amount1);
43
+ event Burn(
44
+ address indexed sender,
45
+ uint amount0,
46
+ uint amount1,
47
+ address indexed to
48
+ );
49
+ event Swap(
50
+ address indexed sender,
51
+ uint amount0In,
52
+ uint amount1In,
53
+ uint amount0Out,
54
+ uint amount1Out,
55
+ address indexed to
56
+ );
57
+ event Sync(uint112 reserve0, uint112 reserve1);
58
+
59
+ constructor() public {
60
+ factory = msg.sender;
61
+ }
62
+
63
+ // called once by the factory at time of deployment
64
+ function initialize(address _token0, address _token1) external {
65
+ require(msg.sender == factory, "UniswapV2: FORBIDDEN"); // sufficient check
66
+ token0 = _token0;
67
+ token1 = _token1;
68
+ }
69
+
70
+ // this low-level function should be called from a contract which performs important safety checks
71
+ function mint(address to) external lock returns (uint liquidity) {
72
+ (uint112 _reserve0, uint112 _reserve1,) = getReserves(); // gas savings
73
+ uint balance0 = IERC20(token0).balanceOf(address(this));
74
+ uint balance1 = IERC20(token1).balanceOf(address(this));
75
+ uint amount0 = balance0.sub(_reserve0);
76
+ uint amount1 = balance1.sub(_reserve1);
77
+
78
+ bool feeOn = _mintFee(_reserve0, _reserve1);
79
+ uint _totalSupply = totalSupply; // gas savings, must be defined here since totalSupply can update in _mintFee
80
+ if (_totalSupply == 0) {
81
+ liquidity = AdvancedMath.sqrt(amount0.mul(amount1)).sub(MINIMUM_LIQUIDITY);
82
+ _mint(address(0), MINIMUM_LIQUIDITY); // permanently lock the first MINIMUM_LIQUIDITY tokens
83
+ } else {
84
+ liquidity = Math.min(amount0.mul(_totalSupply) / _reserve0, amount1.mul(_totalSupply) / _reserve1);
85
+ }
86
+ require(liquidity > 0, "UniswapV2: INSUFFICIENT_LIQUIDITY_MINTED");
87
+ _mint(to, liquidity);
88
+
89
+ _update(
90
+ balance0,
91
+ balance1,
92
+ _reserve0,
93
+ _reserve1
94
+ );
95
+
96
+ if (feeOn) {
97
+ kLast = uint(reserve0).mul(reserve1); // reserve0 and reserve1 are up-to-date
98
+ }
99
+ emit Mint(msg.sender, amount0, amount1);
100
+ }
101
+
102
+ // this low-level function should be called from a contract which performs important safety checks
103
+ function burn(address to) external lock returns (uint amount0, uint amount1) {
104
+ (uint112 _reserve0, uint112 _reserve1,) = getReserves(); // gas savings
105
+ address _token0 = token0; // gas savings
106
+ address _token1 = token1; // gas savings
107
+ uint balance0 = IERC20(_token0).balanceOf(address(this));
108
+ uint balance1 = IERC20(_token1).balanceOf(address(this));
109
+ uint liquidity = balanceOf[address(this)];
110
+
111
+ bool feeOn = _mintFee(_reserve0, _reserve1);
112
+ uint _totalSupply = totalSupply; // gas savings, must be defined here since totalSupply can update in _mintFee
113
+ amount0 = liquidity.mul(balance0) / _totalSupply; // using balances ensures pro-rata distribution
114
+ amount1 = liquidity.mul(balance1) / _totalSupply; // using balances ensures pro-rata distribution
115
+ require(amount0 > 0 && amount1 > 0, "UniswapV2: INSUFFICIENT_LIQUIDITY_BURNED");
116
+ _burn(address(this), liquidity);
117
+ _safeTransfer(_token0, to, amount0);
118
+ _safeTransfer(_token1, to, amount1);
119
+ balance0 = IERC20(_token0).balanceOf(address(this));
120
+ balance1 = IERC20(_token1).balanceOf(address(this));
121
+
122
+ _update(
123
+ balance0,
124
+ balance1,
125
+ _reserve0,
126
+ _reserve1
127
+ );
128
+
129
+ if (feeOn) {
130
+ kLast = uint(reserve0).mul(reserve1); // reserve0 and reserve1 are up-to-date
131
+ }
132
+ emit Burn(
133
+ msg.sender,
134
+ amount0,
135
+ amount1,
136
+ to
137
+ );
138
+ }
139
+
140
+ // this low-level function should be called from a contract which performs important safety checks
141
+ function swap(
142
+ uint amount0Out,
143
+ uint amount1Out,
144
+ address to,
145
+ bytes calldata data
146
+ )
147
+ external
148
+ lock {
149
+ require(amount0Out > 0 || amount1Out > 0, "UniswapV2: INSUFFICIENT_OUTPUT_AMOUNT");
150
+ (uint112 _reserve0, uint112 _reserve1,) = getReserves(); // gas savings
151
+ require(amount0Out < _reserve0 && amount1Out < _reserve1, "UniswapV2: INSUFFICIENT_LIQUIDITY");
152
+
153
+ uint balance0;
154
+ uint balance1;
155
+ // solium-disable indentation
156
+ {
157
+ // scope for _token{0,1}, avoids stack too deep errors
158
+ address _token0 = token0;
159
+ address _token1 = token1;
160
+ require(to != _token0 && to != _token1, "UniswapV2: INVALID_TO");
161
+ if (amount0Out > 0) {
162
+ _safeTransfer(_token0, to, amount0Out); // optimistically transfer tokens
163
+ }
164
+ if (amount1Out > 0) {
165
+ _safeTransfer(_token1, to, amount1Out); // optimistically transfer tokens
166
+ }
167
+ if (data.length > 0) {
168
+ IUniswapV2Callee(to).uniswapV2Call(
169
+ msg.sender,
170
+ amount0Out,
171
+ amount1Out,
172
+ data
173
+ );
174
+ }
175
+ balance0 = IERC20(_token0).balanceOf(address(this));
176
+ balance1 = IERC20(_token1).balanceOf(address(this));
177
+ }
178
+ // solium-enable indentation
179
+ uint amount0In = balance0 > _reserve0 - amount0Out ? balance0 - (_reserve0 - amount0Out) : 0;
180
+ uint amount1In = balance1 > _reserve1 - amount1Out ? balance1 - (_reserve1 - amount1Out) : 0;
181
+ require(amount0In > 0 || amount1In > 0, "UniswapV2: INSUFFICIENT_INPUT_AMOUNT");
182
+ // solium-disable indentation
183
+ {
184
+ // scope for reserve{0,1}Adjusted, avoids stack too deep errors
185
+ uint balance0Adjusted = balance0.mul(1000).sub(amount0In.mul(3));
186
+ uint balance1Adjusted = balance1.mul(1000).sub(amount1In.mul(3));
187
+ require(
188
+ balance0Adjusted.mul(balance1Adjusted) >= uint(_reserve0).mul(_reserve1).mul(1000**2),
189
+ "UniswapV2: K"
190
+ );
191
+ }
192
+ // solium-enable indentation
193
+
194
+ _update(
195
+ balance0,
196
+ balance1,
197
+ _reserve0,
198
+ _reserve1
199
+ );
200
+ emit Swap(
201
+ msg.sender,
202
+ amount0In,
203
+ amount1In,
204
+ amount0Out,
205
+ amount1Out,
206
+ to
207
+ );
208
+ }
209
+
210
+ // force balances to match reserves
211
+ function skim(address to) external lock {
212
+ address _token0 = token0; // gas savings
213
+ address _token1 = token1; // gas savings
214
+ _safeTransfer(_token0, to, IERC20(_token0).balanceOf(address(this)).sub(reserve0));
215
+ _safeTransfer(_token1, to, IERC20(_token1).balanceOf(address(this)).sub(reserve1));
216
+ }
217
+
218
+ // force reserves to match balances
219
+ function sync() external lock {
220
+ _update(
221
+ IERC20(token0).balanceOf(address(this)),
222
+ IERC20(token1).balanceOf(address(this)),
223
+ reserve0,
224
+ reserve1
225
+ );
226
+ }
227
+
228
+ function getReserves() public view returns (uint112 _reserve0, uint112 _reserve1, uint32 _blockTimestampLast) {
229
+ _reserve0 = reserve0;
230
+ _reserve1 = reserve1;
231
+ _blockTimestampLast = blockTimestampLast;
232
+ }
233
+
234
+ // update reserves and, on the first call per block, price accumulators
235
+ function _update(
236
+ uint balance0,
237
+ uint balance1,
238
+ uint112 _reserve0,
239
+ uint112 _reserve1
240
+ )
241
+ private {
242
+ require(balance0 <= uint112(-1) && balance1 <= uint112(-1), "UniswapV2: OVERFLOW");
243
+ uint32 blockTimestamp = uint32(block.timestamp % 2**32);
244
+ uint32 timeElapsed = blockTimestamp - blockTimestampLast; // overflow is desired
245
+ if (timeElapsed > 0 && _reserve0 != 0 && _reserve1 != 0) {
246
+ // * never overflows, and + overflow is desired
247
+ price0CumulativeLast += uint(UQ112x112.encode(_reserve1).uqdiv(_reserve0)) * timeElapsed;
248
+ price1CumulativeLast += uint(UQ112x112.encode(_reserve0).uqdiv(_reserve1)) * timeElapsed;
249
+ }
250
+ reserve0 = uint112(balance0);
251
+ reserve1 = uint112(balance1);
252
+ blockTimestampLast = blockTimestamp;
253
+ emit Sync(reserve0, reserve1);
254
+ }
255
+
256
+ // if fee is on, mint liquidity equivalent to 1/6th of the growth in sqrt(k)
257
+ function _mintFee(uint112 _reserve0, uint112 _reserve1) private returns (bool feeOn) {
258
+ address feeTo = IUniswapV2Factory(factory).feeTo();
259
+ feeOn = feeTo != address(0);
260
+ uint _kLast = kLast; // gas savings
261
+ if (feeOn) {
262
+ if (_kLast != 0) {
263
+ uint rootK = AdvancedMath.sqrt(uint(_reserve0).mul(_reserve1));
264
+ uint rootKLast = AdvancedMath.sqrt(_kLast);
265
+ if (rootK > rootKLast) {
266
+ uint numerator = totalSupply.mul(rootK.sub(rootKLast));
267
+ uint denominator = rootK.mul(5).add(rootKLast);
268
+ uint liquidity = numerator / denominator;
269
+ if (liquidity > 0) _mint(feeTo, liquidity);
270
+ }
271
+ }
272
+ } else if (_kLast != 0) {
273
+ kLast = 0;
274
+ }
275
+ }
276
+
277
+ function _safeTransfer(address token, address to, uint value) private {
278
+ // solium-disable-next-line security/no-low-level-calls
279
+ (bool success, bytes memory data) = token.call(abi.encodeWithSelector(SELECTOR, to, value));
280
+ require(success && (data.length == 0 || abi.decode(data, (bool))), "UniswapV2: TRANSFER_FAILED");
281
+ }
282
+
283
+ }