|
| 1 | +pragma experimental ABIEncoderV2; |
| 2 | +pragma solidity ^0.5.16; |
| 3 | +import "@uniswap/v2-core/contracts/interfaces/IUniswapV2Pair.sol"; |
| 4 | +import "@uniswap/v2-core/contracts/interfaces/IUniswapV2Factory.sol"; |
| 5 | + |
| 6 | +import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; |
| 7 | +import "@uniswap/v2-core/contracts/libraries/Math.sol"; |
| 8 | +import "@openzeppelin/contracts/math/SafeMath.sol"; |
| 9 | + |
| 10 | +contract PreAMMBatcher { |
| 11 | + using SafeMath for uint256; |
| 12 | + IUniswapV2Factory uniswapFactory; |
| 13 | + |
| 14 | + bytes32 public constant DOMAIN_SEPARATOR = keccak256("preBatcher-V1"); |
| 15 | + mapping(address => uint8) public nonces; // probably a nonce per tokenpair would be better |
| 16 | + |
| 17 | + struct Order { |
| 18 | + uint256 sellAmount; |
| 19 | + uint256 buyAmount; |
| 20 | + address sellToken; |
| 21 | + address buyToken; |
| 22 | + address owner; |
| 23 | + } |
| 24 | + |
| 25 | + struct Fraction { |
| 26 | + uint256 numerator; |
| 27 | + uint256 denominator; |
| 28 | + } |
| 29 | + |
| 30 | + event BatchSettlement( |
| 31 | + address token0, |
| 32 | + address token1, |
| 33 | + uint256 sellAmountToken0, |
| 34 | + uint256 sellAmountToken1 |
| 35 | + ); |
| 36 | + |
| 37 | + constructor(IUniswapV2Factory _uniswapFactory) public { |
| 38 | + uniswapFactory = _uniswapFactory; |
| 39 | + } |
| 40 | + |
| 41 | + function preBatchTrade(bytes memory order0bytes, bytes memory order1bytes) |
| 42 | + public |
| 43 | + returns (Fraction memory clearingPrice) |
| 44 | + { |
| 45 | + Order memory sellOrderToken0 = parseOrderBytes(order0bytes); |
| 46 | + Order memory sellOrderToken1 = parseOrderBytes(order1bytes); |
| 47 | + require( |
| 48 | + orderChecks(sellOrderToken0, sellOrderToken1), |
| 49 | + "orders-checks are not succesful" |
| 50 | + ); |
| 51 | + receiveTradeAmounts(sellOrderToken0, sellOrderToken1); |
| 52 | + IUniswapV2Pair uniswapPool = IUniswapV2Pair( |
| 53 | + uniswapFactory.getPair( |
| 54 | + sellOrderToken0.sellToken, |
| 55 | + sellOrderToken1.sellToken |
| 56 | + ) |
| 57 | + ); |
| 58 | + clearingPrice = calculateSettlementPrice( |
| 59 | + sellOrderToken0, |
| 60 | + sellOrderToken1, |
| 61 | + uniswapPool |
| 62 | + ); |
| 63 | + uint256 unmatchedAmountToken0 = sellOrderToken0.sellAmount.sub( |
| 64 | + sellOrderToken1.sellAmount.mul(clearingPrice.numerator).div( |
| 65 | + clearingPrice.denominator |
| 66 | + ) |
| 67 | + ); |
| 68 | + settleUnmatchedAmountsToUniswap( |
| 69 | + unmatchedAmountToken0, |
| 70 | + clearingPrice, |
| 71 | + sellOrderToken0, |
| 72 | + uniswapPool |
| 73 | + ); |
| 74 | + payOutTradeProceedings(sellOrderToken0, sellOrderToken1); |
| 75 | + emit BatchSettlement( |
| 76 | + sellOrderToken0.sellToken, |
| 77 | + sellOrderToken1.sellToken, |
| 78 | + sellOrderToken0.sellAmount.sub(unmatchedAmountToken0), |
| 79 | + sellOrderToken1.sellAmount |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + function orderChecks( |
| 84 | + Order memory sellOrderToken0, |
| 85 | + Order memory sellOrderToken1 |
| 86 | + ) public pure returns (bool) { |
| 87 | + // later the signature verification should happen here as well |
| 88 | + return |
| 89 | + sellOrderToken0.sellToken == sellOrderToken1.buyToken && |
| 90 | + sellOrderToken1.sellToken == sellOrderToken0.buyToken; |
| 91 | + } |
| 92 | + |
| 93 | + function parseOrderBytes(bytes memory orderBytes) |
| 94 | + public |
| 95 | + returns (Order memory order) |
| 96 | + { |
| 97 | + ( |
| 98 | + uint256 sellAmount, |
| 99 | + uint256 buyAmount, |
| 100 | + address sellToken, |
| 101 | + address buyToken, |
| 102 | + address owner, |
| 103 | + uint8 nonce, |
| 104 | + uint8 v, |
| 105 | + bytes32 r, |
| 106 | + bytes32 s |
| 107 | + ) = abi.decode( |
| 108 | + orderBytes, |
| 109 | + ( |
| 110 | + uint256, |
| 111 | + uint256, |
| 112 | + address, |
| 113 | + address, |
| 114 | + address, |
| 115 | + uint8, |
| 116 | + uint8, |
| 117 | + bytes32, |
| 118 | + bytes32 |
| 119 | + ) |
| 120 | + ); |
| 121 | + bytes32 digest = keccak256( |
| 122 | + abi.encode( |
| 123 | + DOMAIN_SEPARATOR, |
| 124 | + sellAmount, |
| 125 | + buyAmount, |
| 126 | + sellToken, |
| 127 | + buyToken, |
| 128 | + owner, |
| 129 | + nonce |
| 130 | + ) |
| 131 | + ); |
| 132 | + address recoveredAddress = ecrecover(digest, v, r, s); |
| 133 | + require( |
| 134 | + recoveredAddress != address(0) && recoveredAddress == owner, |
| 135 | + "invalid_signature" |
| 136 | + ); |
| 137 | + require(nonces[owner] < nonce, "nonce already used"); |
| 138 | + nonces[owner] = nonce; |
| 139 | + order = Order({ |
| 140 | + sellAmount: sellAmount, |
| 141 | + buyAmount: buyAmount, |
| 142 | + buyToken: buyToken, |
| 143 | + sellToken: sellToken, |
| 144 | + owner: owner |
| 145 | + }); |
| 146 | + } |
| 147 | + |
| 148 | + function calculateSettlementPrice( |
| 149 | + Order memory sellOrderToken0, |
| 150 | + Order memory sellOrderToken1, |
| 151 | + IUniswapV2Pair uniswapPool |
| 152 | + ) public view returns (Fraction memory clearingPrice) { |
| 153 | + (uint112 reserve0, uint112 reserve1, ) = uniswapPool.getReserves(); |
| 154 | + uint256 uniswapK = uint256(reserve0).mul(reserve1); |
| 155 | + // if deltaUniswapToken0 will be > 0 |
| 156 | + // if(sellOrderToken1.sellAmount * serve0 / reserve1 > sellToken2Order.sellAmount) |
| 157 | + uint256 p = uniswapK.div( |
| 158 | + uint256(2).mul(uint256(reserve1).add(sellOrderToken1.sellAmount)) |
| 159 | + ); |
| 160 | + uint256 newReserve0 = p.add( |
| 161 | + Math.sqrt( |
| 162 | + p.mul(p).add( |
| 163 | + uniswapK.mul(sellOrderToken0.sellAmount).div( |
| 164 | + uint256(reserve1).add(sellOrderToken1.sellAmount) |
| 165 | + ) |
| 166 | + ) |
| 167 | + ) |
| 168 | + ); |
| 169 | + uint256 newReserve1 = uniswapK.div(newReserve0); |
| 170 | + clearingPrice = Fraction({ |
| 171 | + numerator: newReserve0, |
| 172 | + denominator: newReserve1 |
| 173 | + }); |
| 174 | + require( |
| 175 | + clearingPrice.numerator.mul(sellOrderToken0.sellAmount) >= |
| 176 | + clearingPrice.denominator.mul(sellOrderToken0.buyAmount), |
| 177 | + "sellOrderToken0 price violations" |
| 178 | + ); |
| 179 | + require( |
| 180 | + clearingPrice.numerator.mul(sellOrderToken1.sellAmount) > |
| 181 | + clearingPrice.denominator.mul(sellOrderToken1.buyAmount), |
| 182 | + "sellOrderToken1 price violations" |
| 183 | + ); |
| 184 | + } |
| 185 | + |
| 186 | + function settleUnmatchedAmountsToUniswap( |
| 187 | + uint256 unsettledDirectAmountToken0, |
| 188 | + Fraction memory clearingPrice, |
| 189 | + Order memory sellOrderToken0, |
| 190 | + IUniswapV2Pair uniswapPool |
| 191 | + ) internal { |
| 192 | + require( |
| 193 | + IERC20(sellOrderToken0.sellToken).transfer( |
| 194 | + address(uniswapPool), |
| 195 | + unsettledDirectAmountToken0 |
| 196 | + ), |
| 197 | + "transfer to uniswap failed" |
| 198 | + ); |
| 199 | + uniswapPool.swap( |
| 200 | + 0, |
| 201 | + unsettledDirectAmountToken0 |
| 202 | + .mul(clearingPrice.denominator) |
| 203 | + .mul(997) |
| 204 | + .div(clearingPrice.numerator) |
| 205 | + .div(1000), |
| 206 | + address(this), |
| 207 | + "" |
| 208 | + ); |
| 209 | + } |
| 210 | + |
| 211 | + function receiveTradeAmounts( |
| 212 | + Order memory sellOrderToken0, |
| 213 | + Order memory sellOrderToken1 |
| 214 | + ) internal { |
| 215 | + require( |
| 216 | + IERC20(sellOrderToken0.sellToken).transferFrom( |
| 217 | + sellOrderToken0.owner, |
| 218 | + address(this), |
| 219 | + sellOrderToken0.sellAmount |
| 220 | + ), |
| 221 | + "transferFrom for token0 was not succesful" |
| 222 | + ); |
| 223 | + require( |
| 224 | + IERC20(sellOrderToken1.sellToken).transferFrom( |
| 225 | + sellOrderToken1.owner, |
| 226 | + address(this), |
| 227 | + sellOrderToken1.sellAmount |
| 228 | + ), |
| 229 | + "transferFrom for token1 was not succesful" |
| 230 | + ); |
| 231 | + } |
| 232 | + |
| 233 | + function payOutTradeProceedings( |
| 234 | + Order memory sellOrderToken0, |
| 235 | + Order memory sellOrderToken1 |
| 236 | + ) internal { |
| 237 | + require( |
| 238 | + IERC20(sellOrderToken0.sellToken).transfer( |
| 239 | + sellOrderToken1.owner, |
| 240 | + IERC20(sellOrderToken0.sellToken).balanceOf(address(this)) |
| 241 | + ), |
| 242 | + "final token transfer failed" |
| 243 | + ); |
| 244 | + require( |
| 245 | + IERC20(sellOrderToken0.buyToken).transfer( |
| 246 | + sellOrderToken0.owner, |
| 247 | + IERC20(sellOrderToken0.buyToken).balanceOf(address(this)) |
| 248 | + ), |
| 249 | + "final token1 transfer failed" |
| 250 | + ); |
| 251 | + } |
| 252 | +} |
0 commit comments