@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,566 @@
1
+ pragma solidity ^0.5.16;
2
+
3
+ import "@openzeppelin/contracts/math/SafeMath.sol";
4
+ import "@openzeppelin/contracts/token/ERC20/SafeERC20.sol";
5
+
6
+ import "./libraries/SafeETH.sol";
7
+ import "./libraries/UniswapV2Library.sol";
8
+
9
+ import "./interfaces/IUniswapV2Factory.sol";
10
+ import "./interfaces/IWETH.sol";
11
+
12
+
13
+ // solium-disable function-order
14
+ contract UniswapV2Router02 {
15
+ using SafeMath for uint;
16
+
17
+ address public factory;
18
+ address public WETH;
19
+
20
+ modifier ensure(uint deadline) {
21
+ require(deadline >= block.timestamp, "UniswapV2Router: EXPIRED");
22
+ _;
23
+ }
24
+
25
+ constructor(address _factory, address _WETH) public {
26
+ factory = _factory;
27
+ WETH = _WETH;
28
+ }
29
+
30
+ function() external payable {
31
+ assert(msg.sender == WETH); // only accept ETH via fallback from the WETH contract
32
+ }
33
+
34
+ function getPairInitCodeHash() external pure returns (bytes32) {
35
+ return UniswapV2Library.getPairInitCodeHash();
36
+ }
37
+
38
+ function addLiquidity(
39
+ address tokenA,
40
+ address tokenB,
41
+ uint amountADesired,
42
+ uint amountBDesired,
43
+ uint amountAMin,
44
+ uint amountBMin,
45
+ address to,
46
+ uint deadline
47
+ ) external ensure(deadline) returns (uint amountA, uint amountB, uint liquidity) {
48
+ (amountA, amountB) = _addLiquidity(
49
+ tokenA,
50
+ tokenB,
51
+ amountADesired,
52
+ amountBDesired,
53
+ amountAMin,
54
+ amountBMin
55
+ );
56
+ address pair = UniswapV2Library.pairFor(factory, tokenA, tokenB);
57
+ SafeERC20.safeTransferFrom(
58
+ IERC20(tokenA),
59
+ msg.sender,
60
+ pair,
61
+ amountA
62
+ );
63
+ SafeERC20.safeTransferFrom(
64
+ IERC20(tokenB),
65
+ msg.sender,
66
+ pair,
67
+ amountB
68
+ );
69
+ liquidity = IUniswapV2Pair(pair).mint(to);
70
+ }
71
+
72
+ function addLiquidityETH(
73
+ address token,
74
+ uint amountTokenDesired,
75
+ uint amountTokenMin,
76
+ uint amountETHMin,
77
+ address to,
78
+ uint deadline
79
+ ) external payable ensure(deadline) returns (uint amountToken, uint amountETH, uint liquidity) {
80
+ (amountToken, amountETH) = _addLiquidity(
81
+ token,
82
+ WETH,
83
+ amountTokenDesired,
84
+ msg.value,
85
+ amountTokenMin,
86
+ amountETHMin
87
+ );
88
+ address pair = UniswapV2Library.pairFor(factory, token, WETH);
89
+ SafeERC20.safeTransferFrom(
90
+ IERC20(token),
91
+ msg.sender,
92
+ pair,
93
+ amountToken
94
+ );
95
+ IWETH(WETH).deposit.value(amountETH)();
96
+ assert(IWETH(WETH).transfer(pair, amountETH));
97
+ liquidity = IUniswapV2Pair(pair).mint(to);
98
+ // refund dust eth, if any
99
+ if (msg.value > amountETH) {
100
+ SafeETH.safeTransferETH(msg.sender, msg.value - amountETH);
101
+ }
102
+ }
103
+
104
+ function removeLiquidity(
105
+ address tokenA,
106
+ address tokenB,
107
+ uint liquidity,
108
+ uint amountAMin,
109
+ uint amountBMin,
110
+ address to,
111
+ uint deadline
112
+ ) public ensure(deadline) returns (uint amountA, uint amountB) {
113
+ address pair = UniswapV2Library.pairFor(factory, tokenA, tokenB);
114
+ IUniswapV2Pair(pair).transferFrom(msg.sender, pair, liquidity); // send liquidity to pair
115
+ (uint amount0, uint amount1) = IUniswapV2Pair(pair).burn(to);
116
+ (address token0,) = UniswapV2Library.sortTokens(tokenA, tokenB);
117
+ (amountA, amountB) = tokenA == token0 ? (amount0, amount1) : (amount1, amount0);
118
+ require(amountA >= amountAMin, "UniswapV2Router: INSUFFICIENT_A_AMOUNT");
119
+ require(amountB >= amountBMin, "UniswapV2Router: INSUFFICIENT_B_AMOUNT");
120
+ }
121
+
122
+ function removeLiquidityETH(
123
+ address token,
124
+ uint liquidity,
125
+ uint amountTokenMin,
126
+ uint amountETHMin,
127
+ address to,
128
+ uint deadline
129
+ ) public ensure(deadline) returns (uint amountToken, uint amountETH) {
130
+ (amountToken, amountETH) = removeLiquidity(
131
+ token,
132
+ WETH,
133
+ liquidity,
134
+ amountTokenMin,
135
+ amountETHMin,
136
+ address(this),
137
+ deadline
138
+ );
139
+ SafeERC20.safeTransfer(IERC20(token), to, amountToken);
140
+ IWETH(WETH).withdraw(amountETH);
141
+ SafeETH.safeTransferETH(to, amountETH);
142
+ }
143
+
144
+ function removeLiquidityWithPermit(
145
+ address tokenA,
146
+ address tokenB,
147
+ uint liquidity,
148
+ uint amountAMin,
149
+ uint amountBMin,
150
+ address to,
151
+ uint deadline,
152
+ bool approveMax, uint8 v, bytes32 r, bytes32 s
153
+ ) external returns (uint amountA, uint amountB) {
154
+ address pair = UniswapV2Library.pairFor(factory, tokenA, tokenB);
155
+ uint value = approveMax ? uint(-1) : liquidity;
156
+
157
+ IUniswapV2Pair(pair).permit(
158
+ msg.sender,
159
+ address(this),
160
+ value,
161
+ deadline,
162
+ v,
163
+ r,
164
+ s
165
+ );
166
+
167
+ (amountA, amountB) = removeLiquidity(
168
+ tokenA,
169
+ tokenB,
170
+ liquidity,
171
+ amountAMin,
172
+ amountBMin,
173
+ to,
174
+ deadline
175
+ );
176
+ }
177
+
178
+ function removeLiquidityETHWithPermit(
179
+ address token,
180
+ uint liquidity,
181
+ uint amountTokenMin,
182
+ uint amountETHMin,
183
+ address to,
184
+ uint deadline,
185
+ bool approveMax, uint8 v, bytes32 r, bytes32 s
186
+ ) external returns (uint amountToken, uint amountETH) {
187
+ address pair = UniswapV2Library.pairFor(factory, token, WETH);
188
+ uint value = approveMax ? uint(-1) : liquidity;
189
+
190
+ IUniswapV2Pair(pair).permit(
191
+ msg.sender,
192
+ address(this),
193
+ value,
194
+ deadline,
195
+ v,
196
+ r,
197
+ s
198
+ );
199
+
200
+ (amountToken, amountETH) = removeLiquidityETH(
201
+ token,
202
+ liquidity,
203
+ amountTokenMin,
204
+ amountETHMin,
205
+ to,
206
+ deadline
207
+ );
208
+ }
209
+
210
+ // **** REMOVE LIQUIDITY (supporting fee-on-transfer tokens) ****
211
+ function removeLiquidityETHSupportingFeeOnTransferTokens(
212
+ address token,
213
+ uint liquidity,
214
+ uint amountTokenMin,
215
+ uint amountETHMin,
216
+ address to,
217
+ uint deadline
218
+ ) public ensure(deadline) returns (uint amountETH) {
219
+ (, amountETH) = removeLiquidity(
220
+ token,
221
+ WETH,
222
+ liquidity,
223
+ amountTokenMin,
224
+ amountETHMin,
225
+ address(this),
226
+ deadline
227
+ );
228
+ SafeERC20.safeTransfer(IERC20(token), to, IERC20(token).balanceOf(address(this)));
229
+ IWETH(WETH).withdraw(amountETH);
230
+ SafeETH.safeTransferETH(to, amountETH);
231
+ }
232
+
233
+ function removeLiquidityETHWithPermitSupportingFeeOnTransferTokens(
234
+ address token,
235
+ uint liquidity,
236
+ uint amountTokenMin,
237
+ uint amountETHMin,
238
+ address to,
239
+ uint deadline,
240
+ bool approveMax,
241
+ uint8 v,
242
+ bytes32 r,
243
+ bytes32 s
244
+ )
245
+ external
246
+ returns (uint amountETH) {
247
+ address pair = UniswapV2Library.pairFor(factory, token, WETH);
248
+ uint value = approveMax ? uint(-1) : liquidity;
249
+
250
+ IUniswapV2Pair(pair).permit(
251
+ msg.sender,
252
+ address(this),
253
+ value,
254
+ deadline,
255
+ v,
256
+ r,
257
+ s
258
+ );
259
+
260
+ amountETH = removeLiquidityETHSupportingFeeOnTransferTokens(
261
+ token, liquidity, amountTokenMin, amountETHMin, to, deadline
262
+ );
263
+ }
264
+
265
+ function swapExactTokensForTokens(
266
+ uint amountIn,
267
+ uint amountOutMin,
268
+ address[] calldata path,
269
+ address to,
270
+ uint deadline
271
+ )
272
+ external
273
+ ensure(deadline)
274
+ returns (uint[] memory amounts) {
275
+ amounts = UniswapV2Library.getAmountsOut(factory, amountIn, path);
276
+ require(amounts[amounts.length - 1] >= amountOutMin, "UniswapV2Router: INSUFFICIENT_OUTPUT_AMOUNT");
277
+ SafeERC20.safeTransferFrom(
278
+ IERC20(path[0]),
279
+ msg.sender,
280
+ UniswapV2Library.pairFor(factory, path[0], path[1]),
281
+ amounts[0]
282
+ );
283
+ _swap(amounts, path, to);
284
+ }
285
+
286
+ function swapTokensForExactTokens(
287
+ uint amountOut,
288
+ uint amountInMax,
289
+ address[] calldata path,
290
+ address to,
291
+ uint deadline
292
+ )
293
+ external
294
+ ensure(deadline)
295
+ returns (uint[] memory amounts) {
296
+ amounts = UniswapV2Library.getAmountsIn(factory, amountOut, path);
297
+ require(amounts[0] <= amountInMax, "UniswapV2Router: EXCESSIVE_INPUT_AMOUNT");
298
+ SafeERC20.safeTransferFrom(
299
+ IERC20(path[0]),
300
+ msg.sender,
301
+ UniswapV2Library.pairFor(factory, path[0], path[1]),
302
+ amounts[0]
303
+ );
304
+ _swap(amounts, path, to);
305
+ }
306
+
307
+ function swapExactETHForTokens(
308
+ uint amountOutMin,
309
+ address[] calldata path,
310
+ address to,
311
+ uint deadline
312
+ )
313
+ external
314
+ payable
315
+ ensure(deadline)
316
+ returns (uint[] memory amounts) {
317
+ require(path[0] == WETH, "UniswapV2Router: INVALID_PATH");
318
+ amounts = UniswapV2Library.getAmountsOut(factory, msg.value, path);
319
+ require(amounts[amounts.length - 1] >= amountOutMin, "UniswapV2Router: INSUFFICIENT_OUTPUT_AMOUNT");
320
+ IWETH(WETH).deposit.value(amounts[0])();
321
+ assert(IWETH(WETH).transfer(UniswapV2Library.pairFor(factory, path[0], path[1]), amounts[0]));
322
+ _swap(amounts, path, to);
323
+ }
324
+
325
+ function swapTokensForExactETH(
326
+ uint amountOut,
327
+ uint amountInMax,
328
+ address[] calldata path,
329
+ address to,
330
+ uint deadline
331
+ )
332
+ external
333
+ ensure(deadline)
334
+ returns (uint[] memory amounts) {
335
+ require(path[path.length - 1] == WETH, "UniswapV2Router: INVALID_PATH");
336
+ amounts = UniswapV2Library.getAmountsIn(factory, amountOut, path);
337
+ require(amounts[0] <= amountInMax, "UniswapV2Router: EXCESSIVE_INPUT_AMOUNT");
338
+ SafeERC20.safeTransferFrom(
339
+ IERC20(path[0]),
340
+ msg.sender,
341
+ UniswapV2Library.pairFor(factory, path[0], path[1]),
342
+ amounts[0]
343
+ );
344
+ _swap(amounts, path, address(this));
345
+ IWETH(WETH).withdraw(amounts[amounts.length - 1]);
346
+ SafeETH.safeTransferETH(to, amounts[amounts.length - 1]);
347
+ }
348
+
349
+ function swapExactTokensForETH(
350
+ uint amountIn,
351
+ uint amountOutMin,
352
+ address[] calldata path,
353
+ address to,
354
+ uint deadline
355
+ )
356
+ external
357
+ ensure(deadline)
358
+ returns (uint[] memory amounts) {
359
+ require(path[path.length - 1] == WETH, "UniswapV2Router: INVALID_PATH");
360
+ amounts = UniswapV2Library.getAmountsOut(factory, amountIn, path);
361
+ require(amounts[amounts.length - 1] >= amountOutMin, "UniswapV2Router: INSUFFICIENT_OUTPUT_AMOUNT");
362
+ SafeERC20.safeTransferFrom(
363
+ IERC20(path[0]),
364
+ msg.sender,
365
+ UniswapV2Library.pairFor(factory, path[0], path[1]),
366
+ amounts[0]
367
+ );
368
+ _swap(amounts, path, address(this));
369
+ IWETH(WETH).withdraw(amounts[amounts.length - 1]);
370
+ SafeETH.safeTransferETH(to, amounts[amounts.length - 1]);
371
+ }
372
+
373
+ function swapETHForExactTokens(
374
+ uint amountOut,
375
+ address[] calldata path,
376
+ address to,
377
+ uint deadline
378
+ )
379
+ external
380
+ payable
381
+ ensure(deadline)
382
+ returns (uint[] memory amounts) {
383
+ require(path[0] == WETH, "UniswapV2Router: INVALID_PATH");
384
+ amounts = UniswapV2Library.getAmountsIn(factory, amountOut, path);
385
+ require(amounts[0] <= msg.value, "UniswapV2Router: EXCESSIVE_INPUT_AMOUNT");
386
+ IWETH(WETH).deposit.value(amounts[0])();
387
+ assert(IWETH(WETH).transfer(UniswapV2Library.pairFor(factory, path[0], path[1]), amounts[0]));
388
+ _swap(amounts, path, to);
389
+ // refund dust eth, if any
390
+ if (msg.value > amounts[0]) SafeETH.safeTransferETH(msg.sender, msg.value - amounts[0]);
391
+ }
392
+
393
+ function swapExactTokensForTokensSupportingFeeOnTransferTokens(
394
+ uint amountIn,
395
+ uint amountOutMin,
396
+ address[] calldata path,
397
+ address to,
398
+ uint deadline
399
+ ) external ensure(deadline) {
400
+ SafeERC20.safeTransferFrom(
401
+ IERC20(path[0]), msg.sender, UniswapV2Library.pairFor(factory, path[0], path[1]), amountIn
402
+ );
403
+ uint balanceBefore = IERC20(path[path.length - 1]).balanceOf(to);
404
+ _swapSupportingFeeOnTransferTokens(path, to);
405
+ require(
406
+ IERC20(path[path.length - 1]).balanceOf(to).sub(balanceBefore) >= amountOutMin,
407
+ "UniswapV2Router: INSUFFICIENT_OUTPUT_AMOUNT"
408
+ );
409
+ }
410
+
411
+ function swapExactETHForTokensSupportingFeeOnTransferTokens(
412
+ uint amountOutMin,
413
+ address[] calldata path,
414
+ address to,
415
+ uint deadline
416
+ )
417
+ external
418
+ payable
419
+ ensure(deadline)
420
+ {
421
+ require(path[0] == WETH, "UniswapV2Router: INVALID_PATH");
422
+ uint amountIn = msg.value;
423
+ IWETH(WETH).deposit.value(amountIn)();
424
+ assert(IWETH(WETH).transfer(UniswapV2Library.pairFor(factory, path[0], path[1]), amountIn));
425
+ uint balanceBefore = IERC20(path[path.length - 1]).balanceOf(to);
426
+ _swapSupportingFeeOnTransferTokens(path, to);
427
+ require(
428
+ IERC20(path[path.length - 1]).balanceOf(to).sub(balanceBefore) >= amountOutMin,
429
+ "UniswapV2Router: INSUFFICIENT_OUTPUT_AMOUNT"
430
+ );
431
+ }
432
+
433
+ function swapExactTokensForETHSupportingFeeOnTransferTokens(
434
+ uint amountIn,
435
+ uint amountOutMin,
436
+ address[] calldata path,
437
+ address to,
438
+ uint deadline
439
+ )
440
+ external
441
+ ensure(deadline)
442
+ {
443
+ require(path[path.length - 1] == WETH, "UniswapV2Router: INVALID_PATH");
444
+ SafeERC20.safeTransferFrom(
445
+ IERC20(path[0]), msg.sender, UniswapV2Library.pairFor(factory, path[0], path[1]), amountIn
446
+ );
447
+ _swapSupportingFeeOnTransferTokens(path, address(this));
448
+ uint amountOut = IERC20(WETH).balanceOf(address(this));
449
+ require(amountOut >= amountOutMin, "UniswapV2Router: INSUFFICIENT_OUTPUT_AMOUNT");
450
+ IWETH(WETH).withdraw(amountOut);
451
+ SafeETH.safeTransferETH(to, amountOut);
452
+ }
453
+
454
+ // **** LIBRARY FUNCTIONS ****
455
+ function quote(uint amountA, uint reserveA, uint reserveB) public pure returns (uint amountB) {
456
+ return UniswapV2Library.quote(amountA, reserveA, reserveB);
457
+ }
458
+
459
+ function getAmountOut(uint amountIn, uint reserveIn, uint reserveOut)
460
+ public
461
+ pure
462
+ returns (uint amountOut)
463
+ {
464
+ return UniswapV2Library.getAmountOut(amountIn, reserveIn, reserveOut);
465
+ }
466
+
467
+ function getAmountIn(uint amountOut, uint reserveIn, uint reserveOut)
468
+ public
469
+ pure
470
+ returns (uint amountIn)
471
+ {
472
+ return UniswapV2Library.getAmountIn(amountOut, reserveIn, reserveOut);
473
+ }
474
+
475
+ function getAmountsOut(uint amountIn, address[] memory path)
476
+ public
477
+ view
478
+ returns (uint[] memory amounts)
479
+ {
480
+ return UniswapV2Library.getAmountsOut(factory, amountIn, path);
481
+ }
482
+
483
+ function getAmountsIn(uint amountOut, address[] memory path)
484
+ public
485
+ view
486
+ returns (uint[] memory amounts)
487
+ {
488
+ return UniswapV2Library.getAmountsIn(factory, amountOut, path);
489
+ }
490
+
491
+ function _addLiquidity(
492
+ address tokenA,
493
+ address tokenB,
494
+ uint amountADesired,
495
+ uint amountBDesired,
496
+ uint amountAMin,
497
+ uint amountBMin
498
+ ) internal returns (uint amountA, uint amountB) {
499
+ // create the pair if it doesn"t exist yet
500
+ if (IUniswapV2Factory(factory).getPair(tokenA, tokenB) == address(0)) {
501
+ IUniswapV2Factory(factory).createPair(tokenA, tokenB);
502
+ }
503
+ (uint reserveA, uint reserveB) = UniswapV2Library.getReserves(factory, tokenA, tokenB);
504
+ if (reserveA == 0 && reserveB == 0) {
505
+ (amountA, amountB) = (amountADesired, amountBDesired);
506
+ } else {
507
+ uint amountBOptimal = UniswapV2Library.quote(amountADesired, reserveA, reserveB);
508
+ if (amountBOptimal <= amountBDesired) {
509
+ require(amountBOptimal >= amountBMin, "UniswapV2Router: INSUFFICIENT_B_AMOUNT");
510
+ (amountA, amountB) = (amountADesired, amountBOptimal);
511
+ } else {
512
+ uint amountAOptimal = UniswapV2Library.quote(amountBDesired, reserveB, reserveA);
513
+ assert(amountAOptimal <= amountADesired);
514
+ require(amountAOptimal >= amountAMin, "UniswapV2Router: INSUFFICIENT_A_AMOUNT");
515
+ (amountA, amountB) = (amountAOptimal, amountBDesired);
516
+ }
517
+ }
518
+ }
519
+
520
+ // **** SWAP ****
521
+ // requires the initial amount to have already been sent to the first pair
522
+ function _swap(uint[] memory amounts, address[] memory path, address _to) internal {
523
+ for (uint i; i < path.length - 1; i++) {
524
+ (address input, address output) = (path[i], path[i + 1]);
525
+ (address token0,) = UniswapV2Library.sortTokens(input, output);
526
+ uint amountOut = amounts[i + 1];
527
+ (uint amount0Out, uint amount1Out) = input == token0 ? (uint(0), amountOut) : (amountOut, uint(0));
528
+ address to = i < path.length - 2 ? UniswapV2Library.pairFor(factory, output, path[i + 2]) : _to;
529
+ IUniswapV2Pair(UniswapV2Library.pairFor(factory, input, output)).swap(
530
+ amount0Out,
531
+ amount1Out,
532
+ to,
533
+ new bytes(0)
534
+ );
535
+ }
536
+ }
537
+
538
+ // **** SWAP (supporting fee-on-transfer tokens) ****
539
+ // requires the initial amount to have already been sent to the first pair
540
+ function _swapSupportingFeeOnTransferTokens(address[] memory path, address _to) internal {
541
+ for (uint i; i < path.length - 1; i++) {
542
+ (address input, address output) = (path[i], path[i + 1]);
543
+ (address token0,) = UniswapV2Library.sortTokens(input, output);
544
+ IUniswapV2Pair pair = IUniswapV2Pair(UniswapV2Library.pairFor(factory, input, output));
545
+ uint amountInput;
546
+ uint amountOutput;
547
+ // solium-disable indentation
548
+ {
549
+ // scope to avoid stack too deep errors
550
+ (uint reserve0, uint reserve1,) = pair.getReserves();
551
+ (uint reserveInput, uint reserveOutput) = input == token0 ? (reserve0, reserve1) : (reserve1, reserve0);
552
+ amountInput = IERC20(input).balanceOf(address(pair)).sub(reserveInput);
553
+ amountOutput = UniswapV2Library.getAmountOut(amountInput, reserveInput, reserveOutput);
554
+ }
555
+ // solium-enable indentation
556
+ (uint amount0Out, uint amount1Out) = input == token0 ? (uint(0), amountOutput) : (amountOutput, uint(0));
557
+ address to = i < path.length - 2 ? UniswapV2Library.pairFor(factory, output, path[i + 2]) : _to;
558
+ pair.swap(
559
+ amount0Out,
560
+ amount1Out,
561
+ to,
562
+ new bytes(0)
563
+ );
564
+ }
565
+ }
566
+ }
@@ -0,0 +1,13 @@
1
+ pragma solidity >=0.5.0;
2
+
3
+
4
+ interface IUniswapV2Callee {
5
+
6
+ function uniswapV2Call(
7
+ address sender,
8
+ uint amount0,
9
+ uint amount1,
10
+ bytes calldata data
11
+ )
12
+ external;
13
+ }
@@ -0,0 +1,18 @@
1
+ pragma solidity >=0.5.0;
2
+
3
+ interface IUniswapV2Factory {
4
+ event PairCreated(address indexed token0, address indexed token1, address pair, uint);
5
+
6
+ function feeTo() external view returns (address);
7
+ function feeToSetter() external view returns (address);
8
+
9
+ function getPair(address tokenA, address tokenB) external view returns (address pair);
10
+ function allPairs(uint) external view returns (address pair);
11
+ function allPairsLength() external view returns (uint);
12
+ function getPairInitCodeHash() external pure returns (bytes32);
13
+
14
+ function createPair(address tokenA, address tokenB) external returns (address pair);
15
+
16
+ function setFeeTo(address) external;
17
+ function setFeeToSetter(address) external;
18
+ }
@@ -0,0 +1,67 @@
1
+ pragma solidity >=0.5.0;
2
+
3
+ interface IUniswapV2Pair {
4
+ event Approval(address indexed owner, address indexed spender, uint value);
5
+ event Transfer(address indexed from, address indexed to, uint value);
6
+
7
+ function name() external pure returns (string memory);
8
+ function symbol() external pure returns (string memory);
9
+ function decimals() external pure returns (uint8);
10
+ function totalSupply() external view returns (uint);
11
+ function balanceOf(address owner) external view returns (uint);
12
+ function allowance(address owner, address spender) external view returns (uint);
13
+
14
+ function approve(address spender, uint value) external returns (bool);
15
+ function transfer(address to, uint value) external returns (bool);
16
+ function transferFrom(address from, address to, uint value) external returns (bool);
17
+
18
+ function DOMAIN_SEPARATOR() external view returns (bytes32);
19
+ function PERMIT_TYPEHASH() external pure returns (bytes32);
20
+ function nonces(address owner) external view returns (uint);
21
+
22
+ function permit(
23
+ address owner,
24
+ address spender,
25
+ uint value,
26
+ uint deadline,
27
+ uint8 v,
28
+ bytes32 r,
29
+ bytes32 s
30
+ )
31
+ external;
32
+
33
+ event Mint(address indexed sender, uint amount0, uint amount1);
34
+ event Burn(address indexed sender, uint amount0, uint amount1, address indexed to);
35
+ event Swap(
36
+ address indexed sender,
37
+ uint amount0In,
38
+ uint amount1In,
39
+ uint amount0Out,
40
+ uint amount1Out,
41
+ address indexed to
42
+ );
43
+ event Sync(uint112 reserve0, uint112 reserve1);
44
+
45
+ function MINIMUM_LIQUIDITY() external pure returns (uint);
46
+ function factory() external view returns (address);
47
+ function token0() external view returns (address);
48
+ function token1() external view returns (address);
49
+ function getReserves() external view returns (uint112 reserve0, uint112 reserve1, uint32 blockTimestampLast);
50
+ function price0CumulativeLast() external view returns (uint);
51
+ function price1CumulativeLast() external view returns (uint);
52
+ function kLast() external view returns (uint);
53
+
54
+ function mint(address to) external returns (uint liquidity);
55
+ function burn(address to) external returns (uint amount0, uint amount1);
56
+ function swap(
57
+ uint amount0Out,
58
+ uint amount1Out,
59
+ address to,
60
+ bytes calldata data
61
+ )
62
+ external;
63
+ function skim(address to) external;
64
+ function sync() external;
65
+
66
+ function initialize(address, address) external;
67
+ }
@@ -0,0 +1,7 @@
1
+ pragma solidity ^0.5.4;
2
+
3
+ interface IWETH {
4
+ function deposit() external payable;
5
+ function transfer(address to, uint value) external returns (bool);
6
+ function withdraw(uint) external;
7
+ }
@@ -0,0 +1,29 @@
1
+ /*
2
+
3
+ Copyright 2021 Dolomite.
4
+
5
+ Licensed under the Apache License, Version 2.0 (the "License");
6
+ you may not use this file except in compliance with the License.
7
+ You may obtain a copy of the License at
8
+
9
+ http://www.apache.org/licenses/LICENSE-2.0
10
+
11
+ Unless required by applicable law or agreed to in writing, software
12
+ distributed under the License is distributed on an "AS IS" BASIS,
13
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ See the License for the specific language governing permissions and
15
+ limitations under the License.
16
+
17
+ */
18
+
19
+ pragma solidity ^0.5.4;
20
+
21
+
22
+ library SafeETH {
23
+
24
+ function safeTransferETH(address to, uint value) internal {
25
+ // solium-disable-next-line security/no-call-value
26
+ (bool success,) = to.call.value(value)(new bytes(0));
27
+ require(success, "SafeETH: ETH_TRANSFER_FAILED");
28
+ }
29
+ }