Skip to content

dex.blue Smart Contract source code

Please read the Introduction first.

For coverage of the smart contract methods useful for trading, please refer to the Smart Contract Methods page.

Verify the Smart Contract on Etherscan.

Contract ABI

1
[{"constant":false,"inputs":[{"internalType":"bool","name":"state","type":"bool"}],"name":"setMarketActiveState","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"makerReserve","type":"address"},{"internalType":"address payable","name":"takerReserve","type":"address"},{"components":[{"internalType":"address","name":"makerToken","type":"address"},{"internalType":"address","name":"takerToken","type":"address"},{"internalType":"uint256","name":"makerAmount","type":"uint256"},{"internalType":"uint256","name":"takerAmount","type":"uint256"},{"internalType":"uint256","name":"makerFee","type":"uint256"},{"internalType":"uint256","name":"takerFee","type":"uint256"},{"internalType":"uint256","name":"gasLimit","type":"uint256"}],"internalType":"struct dexBlueStructs.ReserveReserveTrade","name":"trade","type":"tuple"},{"internalType":"bytes32[]","name":"makerData","type":"bytes32[]"},{"internalType":"bytes32[]","name":"takerData","type":"bytes32[]"}],"name":"executeReserveReserveTradeWithData","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"components":[{"internalType":"bytes32","name":"packedInput1","type":"bytes32"},{"internalType":"bytes32","name":"packedInput2","type":"bytes32"},{"internalType":"bytes32","name":"packedInput3","type":"bytes32"}],"internalType":"struct dexBlueStructs.TradeInputPacked","name":"tradeInput","type":"tuple"},{"internalType":"bytes32[]","name":"makerData","type":"bytes32[]"},{"internalType":"bytes32[]","name":"takerData","type":"bytes32[]"}],"name":"settleReserveReserveTradeWithData","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"components":[{"internalType":"bytes32","name":"packedInput1","type":"bytes32"},{"internalType":"bytes32","name":"packedInput2","type":"bytes32"},{"internalType":"bytes32","name":"packedInput3","type":"bytes32"}],"internalType":"struct dexBlueStructs.TradeInputPacked","name":"tradeInput","type":"tuple"}],"name":"settleReserveReserveTrade","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"getReserves","outputs":[{"internalType":"address[]","name":"","type":"address[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"getFeeCollector","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"reserve","type":"address"}],"name":"removePublicReserveAddress","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"components":[{"internalType":"bytes32","name":"packedInput1","type":"bytes32"},{"internalType":"bytes32","name":"packedInput2","type":"bytes32"}],"internalType":"struct dexBlueStructs.RingTradeInputPacked","name":"packed","type":"tuple"}],"name":"ringTradeFromInput","outputs":[{"components":[{"internalType":"bool","name":"isReserve","type":"bool"},{"internalType":"uint256","name":"identifier","type":"uint256"},{"internalType":"address","name":"giveToken","type":"address"},{"internalType":"uint256","name":"giveAmount","type":"uint256"},{"internalType":"uint256","name":"fee","type":"uint256"},{"internalType":"uint256","name":"rebate","type":"uint256"},{"internalType":"uint256","name":"gasLimit","type":"uint256"}],"internalType":"struct dexBlueStructs.RingTrade","name":"","type":"tuple"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"address payable","name":"collector","type":"address"}],"name":"nominateFeeCollector","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"depositToken","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"directWithdrawal","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"uint64","name":"nonce","type":"uint64"},{"internalType":"uint8","name":"v","type":"uint8"},{"internalType":"bytes32","name":"r","type":"bytes32"},{"internalType":"bytes32","name":"s","type":"bytes32"},{"internalType":"address","name":"receiving_address","type":"address"}],"name":"multiSigTransfer","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"delegate","type":"address"}],"name":"delegateAddress","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"components":[{"internalType":"bytes32","name":"packedInput1","type":"bytes32"},{"internalType":"bytes32","name":"packedInput2","type":"bytes32"},{"internalType":"bytes32","name":"r","type":"bytes32"},{"internalType":"bytes32","name":"s","type":"bytes32"}],"internalType":"struct dexBlueStructs.OrderInputPacked","name":"orderInput","type":"tuple"}],"name":"orderFromInput","outputs":[{"components":[{"internalType":"address","name":"sellToken","type":"address"},{"internalType":"uint256","name":"sellAmount","type":"uint256"},{"internalType":"address","name":"buyToken","type":"address"},{"internalType":"uint256","name":"buyAmount","type":"uint256"},{"internalType":"uint256","name":"expiry","type":"uint256"},{"internalType":"bytes32","name":"hash","type":"bytes32"},{"internalType":"address","name":"signee","type":"address"}],"internalType":"struct dexBlueStructs.Order","name":"","type":"tuple"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"components":[{"internalType":"bytes32","name":"packedInput1","type":"bytes32"},{"internalType":"bytes32","name":"packedInput2","type":"bytes32"},{"internalType":"bytes32","name":"r","type":"bytes32"},{"internalType":"bytes32","name":"s","type":"bytes32"}],"internalType":"struct dexBlueStructs.OrderInputPacked[]","name":"orderInput","type":"tuple[]"},{"components":[{"internalType":"bytes32","name":"packedInput1","type":"bytes32"},{"internalType":"bytes32","name":"packedInput2","type":"bytes32"},{"internalType":"bytes32","name":"packedInput3","type":"bytes32"}],"internalType":"struct dexBlueStructs.TradeInputPacked[]","name":"tradeInput","type":"tuple[]"}],"name":"batchSettleTrades","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"token","type":"address"}],"name":"removeTokenAddressFromArr","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"uint64","name":"nonce","type":"uint64"},{"internalType":"uint8","name":"v","type":"uint8"},{"internalType":"bytes32","name":"r","type":"bytes32"},{"internalType":"bytes32","name":"s","type":"bytes32"},{"internalType":"address payable","name":"receiving_address","type":"address"}],"name":"multiSigSend","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"makerReserve","type":"address"},{"internalType":"address payable","name":"takerReserve","type":"address"},{"components":[{"internalType":"address","name":"makerToken","type":"address"},{"internalType":"address","name":"takerToken","type":"address"},{"internalType":"uint256","name":"makerAmount","type":"uint256"},{"internalType":"uint256","name":"takerAmount","type":"uint256"},{"internalType":"uint256","name":"makerFee","type":"uint256"},{"internalType":"uint256","name":"takerFee","type":"uint256"},{"internalType":"uint256","name":"gasLimit","type":"uint256"}],"internalType":"struct dexBlueStructs.ReserveReserveTrade","name":"trade","type":"tuple"}],"name":"executeReserveReserveTrade","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"sell_token","type":"address"},{"internalType":"uint256","name":"sell_amount","type":"uint256"},{"internalType":"address","name":"buy_token","type":"address"},{"internalType":"uint256","name":"min_output","type":"uint256"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swap","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":true,"stateMutability":"payable","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"index","type":"uint256"}],"name":"cacheTokenAddress","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"internalType":"address","name":"sell_token","type":"address"},{"internalType":"uint256","name":"sell_amount","type":"uint256"},{"internalType":"address","name":"buy_token","type":"address"}],"name":"getBestReserve","outputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"address","name":"token","type":"address"}],"name":"getTokenIndex","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"arbiter","type":"address"},{"internalType":"bool","name":"status","type":"bool"}],"name":"nominateArbiter","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"components":[{"internalType":"bytes32","name":"packedInput1","type":"bytes32"},{"internalType":"bytes32","name":"packedInput2","type":"bytes32"},{"internalType":"bytes32","name":"packedInput3","type":"bytes32"}],"internalType":"struct dexBlueStructs.TradeInputPacked","name":"packed","type":"tuple"}],"name":"reserveTradeFromInput","outputs":[{"components":[{"internalType":"uint256","name":"orderAmount","type":"uint256"},{"internalType":"uint256","name":"reserveAmount","type":"uint256"},{"internalType":"uint256","name":"orderFee","type":"uint256"},{"internalType":"uint256","name":"reserveFee","type":"uint256"},{"internalType":"uint256","name":"orderRebate","type":"uint256"},{"internalType":"uint256","name":"reserveRebate","type":"uint256"},{"internalType":"bool","name":"orderIsMaker","type":"bool"},{"internalType":"uint256","name":"gasLimit","type":"uint256"}],"internalType":"struct dexBlueStructs.ReserveTrade","name":"","type":"tuple"}],"payable":false,"stateMutability":"pure","type":"function"},{"constant":false,"inputs":[],"name":"lockFeeCollector","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"sellToken","type":"address"},{"internalType":"uint256","name":"sellAmount","type":"uint256"},{"internalType":"address","name":"buyToken","type":"address"},{"internalType":"uint256","name":"buyAmount","type":"uint256"},{"internalType":"address","name":"reserve","type":"address"}],"name":"executeReserveTrade","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"sell_token","type":"address"},{"internalType":"uint256","name":"sell_amount","type":"uint256"},{"internalType":"address","name":"buy_token","type":"address"},{"internalType":"uint256","name":"min_output","type":"uint256"},{"internalType":"address","name":"reserve","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapWithReserve","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":true,"stateMutability":"payable","type":"function"},{"constant":true,"inputs":[{"internalType":"address","name":"user","type":"address"}],"name":"getLastBlockedTimestamp","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"bytes32[]","name":"orderHashes","type":"bytes32[]"},{"internalType":"uint8","name":"v","type":"uint8"},{"internalType":"bytes32","name":"r","type":"bytes32"},{"internalType":"bytes32","name":"s","type":"bytes32"}],"name":"multiSigOrderBatchCancel","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"components":[{"internalType":"bytes32","name":"packedInput1","type":"bytes32"},{"internalType":"bytes32","name":"packedInput2","type":"bytes32"},{"internalType":"bytes32","name":"r","type":"bytes32"},{"internalType":"bytes32","name":"s","type":"bytes32"}],"internalType":"struct dexBlueStructs.OrderInputPacked","name":"orderInput","type":"tuple"},{"components":[{"internalType":"bytes32","name":"packedInput1","type":"bytes32"},{"internalType":"bytes32","name":"packedInput2","type":"bytes32"},{"internalType":"bytes32","name":"packedInput3","type":"bytes32"}],"internalType":"struct dexBlueStructs.TradeInputPacked","name":"tradeInput","type":"tuple"},{"internalType":"bytes32[]","name":"data","type":"bytes32[]"}],"name":"settleReserveTradeWithData","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"components":[{"internalType":"bytes32","name":"packedInput1","type":"bytes32"},{"internalType":"bytes32","name":"packedInput2","type":"bytes32"},{"internalType":"bytes32","name":"packedInput3","type":"bytes32"}],"internalType":"struct dexBlueStructs.TradeInputPacked","name":"packed","type":"tuple"}],"name":"reserveReserveTradeFromInput","outputs":[{"components":[{"internalType":"address","name":"makerToken","type":"address"},{"internalType":"address","name":"takerToken","type":"address"},{"internalType":"uint256","name":"makerAmount","type":"uint256"},{"internalType":"uint256","name":"takerAmount","type":"uint256"},{"internalType":"uint256","name":"makerFee","type":"uint256"},{"internalType":"uint256","name":"takerFee","type":"uint256"},{"internalType":"uint256","name":"gasLimit","type":"uint256"}],"internalType":"struct dexBlueStructs.ReserveReserveTrade","name":"","type":"tuple"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"blockFundsForSingleSigWithdrawal","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"depositWrappedEther","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[],"name":"depositEther","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"constant":true,"inputs":[{"internalType":"uint256","name":"index","type":"uint256"}],"name":"getTokenFromIndex","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"bytes32","name":"packedInput1","type":"bytes32"},{"internalType":"bytes32","name":"packedInput2","type":"bytes32"},{"internalType":"bytes32","name":"r","type":"bytes32"},{"internalType":"bytes32","name":"s","type":"bytes32"}],"name":"userSigWithdrawal","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"uint64","name":"nonce","type":"uint64"},{"internalType":"uint8","name":"v","type":"uint8"},{"internalType":"bytes32","name":"r","type":"bytes32"},{"internalType":"bytes32","name":"s","type":"bytes32"}],"name":"multiSigWithdrawal","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"initiateSingleSigWithdrawal","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"components":[{"internalType":"bytes32","name":"packedInput1","type":"bytes32"},{"internalType":"bytes32","name":"packedInput2","type":"bytes32"},{"internalType":"bytes32","name":"r","type":"bytes32"},{"internalType":"bytes32","name":"s","type":"bytes32"}],"internalType":"struct dexBlueStructs.OrderInputPacked[]","name":"orderInput","type":"tuple[]"},{"components":[{"internalType":"bytes32","name":"packedInput1","type":"bytes32"},{"internalType":"bytes32","name":"packedInput2","type":"bytes32"}],"internalType":"struct dexBlueStructs.RingTradeInputPacked[]","name":"tradeInput","type":"tuple[]"}],"name":"settleRingTrade","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"internalType":"uint256","name":"index","type":"uint256"}],"name":"getReserveFromIndex","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"address payable","name":"new_owner","type":"address"}],"name":"changeOwner","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"getTokens","outputs":[{"internalType":"address[]","name":"","type":"address[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"uint256","name":"waiting_period","type":"uint256"}],"name":"changeSingleSigWaitingPeriod","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"components":[{"internalType":"bytes32","name":"packedInput1","type":"bytes32"},{"internalType":"bytes32","name":"packedInput2","type":"bytes32"},{"internalType":"bytes32","name":"packedInput3","type":"bytes32"}],"internalType":"struct dexBlueStructs.TradeInputPacked","name":"packed","type":"tuple"}],"name":"tradeFromInput","outputs":[{"components":[{"internalType":"uint256","name":"makerAmount","type":"uint256"},{"internalType":"uint256","name":"takerAmount","type":"uint256"},{"internalType":"uint256","name":"makerFee","type":"uint256"},{"internalType":"uint256","name":"takerFee","type":"uint256"},{"internalType":"uint256","name":"makerRebate","type":"uint256"}],"internalType":"struct dexBlueStructs.Trade","name":"","type":"tuple"}],"payable":false,"stateMutability":"pure","type":"function"},{"constant":false,"inputs":[{"internalType":"address payable","name":"reserve","type":"address"},{"internalType":"uint256","name":"index","type":"uint256"},{"internalType":"bool","name":"is_public","type":"bool"}],"name":"cacheReserveAddress","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"components":[{"internalType":"bytes32","name":"packedInput1","type":"bytes32"},{"internalType":"bytes32","name":"packedInput2","type":"bytes32"},{"internalType":"bytes32","name":"r","type":"bytes32"},{"internalType":"bytes32","name":"s","type":"bytes32"}],"internalType":"struct dexBlueStructs.OrderInputPacked","name":"makerOrderInput","type":"tuple"},{"components":[{"internalType":"bytes32","name":"packedInput1","type":"bytes32"},{"internalType":"bytes32","name":"packedInput2","type":"bytes32"},{"internalType":"bytes32","name":"r","type":"bytes32"},{"internalType":"bytes32","name":"s","type":"bytes32"}],"internalType":"struct dexBlueStructs.OrderInputPacked","name":"takerOrderInput","type":"tuple"},{"components":[{"internalType":"bytes32","name":"packedInput1","type":"bytes32"},{"internalType":"bytes32","name":"packedInput2","type":"bytes32"},{"internalType":"bytes32","name":"packedInput3","type":"bytes32"}],"internalType":"struct dexBlueStructs.TradeInputPacked","name":"tradeInput","type":"tuple"}],"name":"settleTrade","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"internalType":"address","name":"sell_token","type":"address"},{"internalType":"uint256","name":"sell_amount","type":"uint256"},{"internalType":"address","name":"buy_token","type":"address"}],"name":"getSwapOutput","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"mintGasTokens","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"address","name":"holder","type":"address"}],"name":"getBalance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"address","name":"reserve","type":"address"}],"name":"getReserveIndex","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"components":[{"internalType":"bytes32","name":"packedInput1","type":"bytes32"},{"internalType":"bytes32","name":"packedInput2","type":"bytes32"},{"internalType":"bytes32","name":"r","type":"bytes32"},{"internalType":"bytes32","name":"s","type":"bytes32"}],"internalType":"struct dexBlueStructs.OrderInputPacked[]","name":"orderInput","type":"tuple[]"},{"components":[{"internalType":"bytes32","name":"packedInput1","type":"bytes32"},{"internalType":"bytes32","name":"packedInput2","type":"bytes32"}],"internalType":"struct dexBlueStructs.RingTradeInputPacked[]","name":"tradeInput","type":"tuple[]"},{"internalType":"bytes32[][]","name":"data","type":"bytes32[][]"}],"name":"settleRingTradeWithData","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"getAvailableGasTokens","outputs":[{"internalType":"uint256","name":"amount","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"delegate","type":"address"},{"internalType":"uint8","name":"v","type":"uint8"},{"internalType":"bytes32","name":"r","type":"bytes32"},{"internalType":"bytes32","name":"s","type":"bytes32"}],"name":"revokeDelegation","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"address","name":"holder","type":"address"}],"name":"getBlocked","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"address","name":"sellToken","type":"address"},{"internalType":"uint256","name":"sellAmount","type":"uint256"},{"internalType":"address","name":"buyToken","type":"address"},{"internalType":"uint256","name":"buyAmount","type":"uint256"},{"internalType":"address","name":"reserve","type":"address"},{"internalType":"bytes32[]","name":"data","type":"bytes32[]"}],"name":"executeReserveTradeWithData","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"components":[{"internalType":"bytes32","name":"packedInput1","type":"bytes32"},{"internalType":"bytes32","name":"packedInput2","type":"bytes32"},{"internalType":"bytes32","name":"r","type":"bytes32"},{"internalType":"bytes32","name":"s","type":"bytes32"}],"internalType":"struct dexBlueStructs.OrderInputPacked","name":"orderInput","type":"tuple"},{"components":[{"internalType":"bytes32","name":"packedInput1","type":"bytes32"},{"internalType":"bytes32","name":"packedInput2","type":"bytes32"},{"internalType":"bytes32","name":"packedInput3","type":"bytes32"}],"internalType":"struct dexBlueStructs.TradeInputPacked","name":"tradeInput","type":"tuple"}],"name":"settleReserveTrade","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"payable":true,"stateMutability":"payable","type":"fallback"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"makerAsset","type":"address"},{"indexed":false,"internalType":"uint256","name":"makerAmount","type":"uint256"},{"indexed":false,"internalType":"address","name":"takerAsset","type":"address"},{"indexed":false,"internalType":"uint256","name":"takerAmount","type":"uint256"}],"name":"LogTrade","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"soldAsset","type":"address"},{"indexed":false,"internalType":"uint256","name":"soldAmount","type":"uint256"},{"indexed":false,"internalType":"address","name":"boughtAsset","type":"address"},{"indexed":false,"internalType":"uint256","name":"boughtAmount","type":"uint256"}],"name":"LogSwap","type":"event"},{"anonymous":false,"inputs":[],"name":"LogTradeFailed","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"account","type":"address"},{"indexed":false,"internalType":"address","name":"token","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"LogDeposit","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"account","type":"address"},{"indexed":false,"internalType":"address","name":"token","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"LogWithdrawal","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"account","type":"address"},{"indexed":false,"internalType":"address","name":"token","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"LogDirectWithdrawal","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"account","type":"address"},{"indexed":false,"internalType":"address","name":"token","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"LogBlockedForSingleSigWithdrawal","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"account","type":"address"},{"indexed":false,"internalType":"address","name":"token","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"LogSingleSigWithdrawal","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"bytes32","name":"hash","type":"bytes32"}],"name":"LogOrderCanceled","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"delegator","type":"address"},{"indexed":false,"internalType":"address","name":"delegate","type":"address"},{"indexed":false,"internalType":"bool","name":"status","type":"bool"}],"name":"LogDelegateStatus","type":"event"}]

Source Code

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
pragma solidity 0.5.11;
pragma experimental ABIEncoderV2;

contract dexBlueEvents{
    // Events

    /** @notice Emitted when a trade is settled
      * @param  makerAsset  The address of the token the maker gave
      * @param  makerAmount The amount of makerAsset the maker gave
      * @param  takerAsset  The address of the token the taker gave
      * @param  takerAmount The amount of takerAsset the taker gave
      */
    event LogTrade(address makerAsset, uint256 makerAmount, address takerAsset, uint256 takerAmount);

    /** @notice Emitted when a simple token swap against a reserve is settled
      * @param  soldAsset    The address of the token the maker gave
      * @param  soldAmount   The amount of makerAsset the maker gave
      * @param  boughtAsset  The address of the token the taker gave
      * @param  boughtAmount The amount of takerAsset the taker gave
      */
    event LogSwap(address soldAsset, uint256 soldAmount, address boughtAsset, uint256 boughtAmount);

    /** @notice Emitted when a trade settlement failed
      */
    event LogTradeFailed();

    /** @notice Emitted after a successful deposit of ETH or token
      * @param  account  The address, which deposited the asset
      * @param  token    The address of the deposited token (ETH is address(0))
      * @param  amount   The amount deposited in this transaction 
      */
    event LogDeposit(address account, address token, uint256 amount);

    /** @notice Emitted after a successful multi-sig withdrawal of deposited ETH or token
      * @param  account  The address, which initiated the withdrawal
      * @param  token    The address of the token which is withdrawn (ETH is address(0))
      * @param  amount   The amount withdrawn in this transaction 
      */
    event LogWithdrawal(address account, address token, uint256 amount);

    /** @notice Emitted after a successful direct withdrawal of deposited ETH or token
      * @param  account  The address, which initiated the withdrawal
      * @param  token    The address of the token which is withdrawn (ETH is address(0))
      * @param  amount   The amount withdrawn in this transaction 
      */
    event LogDirectWithdrawal(address account, address token, uint256 amount);

    /** @notice Emitted after a user successfully blocked tokens or ETH for a single signature withdrawal
      * @param  account  The address controlling the tokens
      * @param  token    The address of the token which is blocked (ETH is address(0))
      * @param  amount   The amount blocked in this transaction 
      */
    event LogBlockedForSingleSigWithdrawal(address account, address token, uint256 amount);

    /** @notice Emitted after a successful single-sig withdrawal of deposited ETH or token
      * @param  account  The address, which initiated the withdrawal
      * @param  token    The address of the token which is withdrawn (ETH is address(0))
      * @param  amount   The amount withdrawn in this transaction 
      */
    event LogSingleSigWithdrawal(address account, address token, uint256 amount);

    /** @notice Emitted once an on-chain cancellation of an order was performed
      * @param  hash    The invalidated orders hash 
      */
    event LogOrderCanceled(bytes32 hash);

    /** @notice Emitted once a address delegation or dedelegation was performed
      * @param  delegator The delegating address,
      * @param  delegate  The delegated address,
      * @param  status    whether the transaction delegated an address (true) or inactivated an active delegation (false) 
      */
    event LogDelegateStatus(address delegator, address delegate, bool status);
}

contract dexBlueStorage{
    // Storage Variables

    mapping(address => mapping(address => uint256)) balances;                           // Users balances (token address > user address > balance amount) (ETH is address(0))
    mapping(address => mapping(address => uint256)) blocked_for_single_sig_withdrawal;  // Users balances, blocked to withdraw without arbiters multi-sig (token address > user address > blocked amount) (ETH is address(0))
    mapping(address => uint256) last_blocked_timestamp;                                 // The last timestamp a user blocked tokens at, to withdraw with single-sig

    mapping(bytes32 => bool) processed_withdrawals;                                     // Processed withdrawal hashes
    mapping(bytes32 => uint256) matched;                                                // Orders matched sell amounts to prevent multiple-/over- matches of the same orders

    mapping(address => address) delegates;                                              // Registered Delegated Signing Key addresses

    mapping(uint256 => address) tokens;                                                 // Cached token index > address mapping
    mapping(address => uint256) token_indices;                                          // Cached token addresses > index mapping
    address[] token_arr;                                                                // Array of cached token addresses

    mapping(uint256 => address payable) reserves;                                       // Reserve index > reserve address mapping
    mapping(address => uint256) reserve_indices;                                        // Reserve address > reserve index mapping
    mapping(address => bool) public_reserves;                                           // Reserves publicly accessible through swap() & swapWithReserve()
    address[] public_reserve_arr;                                                       // Array of the publicly accessible reserves

    address payable owner;                      // Contract owner address (has the right to nominate arbiters and feeCollector addresses)   
    mapping(address => bool) arbiters;          // Mapping of arbiters
    bool marketActive = true;                   // Make it possible to pause the market
    address payable feeCollector;               // feeCollector address
    bool feeCollectorLocked = false;            // Make it possible to lock the feeCollector address (to allow to change the feeCollector to a fee distribution contract)
    uint256 single_sig_waiting_period = 86400;  // waiting period for single sig withdrawas, default (and max) is one day
}

contract dexBlueUtils is dexBlueStorage{
    /** @notice Get the balance of a user for a specific token
      * @param  token  The token address (ETH is address(0))
      * @param  holder The address holding the token
      * @return The amount of the specified token held by the user 
      */
    function getBalance(address token, address holder) view public returns(uint256){
        return balances[token][holder];
    }

    /** @notice Get index of a cached token address
      * @param  token  The token address (ETH is address(0))
      * @return The index of the token
      */
    function getTokenIndex(address token) view public returns(uint256){
        return token_indices[token];
    }

    /** @notice Get a cached token address from an index
      * @param  index  The index of the token
      * @return The address of the token
      */
    function getTokenFromIndex(uint256 index) view public returns(address){
        return tokens[index];
    }

    /** @notice Get the array containing all indexed token addresses
      * @return The array of all indexed token addresses
      */
    function getTokens() view public returns(address[] memory){
        return token_arr;
    }

    /** @notice Get index of a cached reserve address
      * @param  reserve  The reserve address
      * @return The index of the reserve
      */
    function getReserveIndex(address reserve) view public returns(uint256){
        return reserve_indices[reserve];
    }

    /** @notice Get a cached reserve address from an index
      * @param  index  The index of the reserve
      * @return The address of the reserve
      */
    function getReserveFromIndex(uint256 index) view public returns(address){
        return reserves[index];
    }

    /** @notice Get the array containing all publicly available reserve addresses
      * @return The array of addresses of all publicly available reserves
      */
    function getReserves() view public returns(address[] memory){
        return public_reserve_arr;
    }

    /** @notice Get the balance a user blocked for a single-signature withdrawal (ETH is address(0))
      * @param  token  The token address (ETH is address(0))
      * @param  holder The address holding the token
      * @return The amount of the specified token blocked by the user 
      */
    function getBlocked(address token, address holder) view public returns(uint256){
        return blocked_for_single_sig_withdrawal[token][holder];
    }

    /** @notice Returns the timestamp of the last blocked balance
      * @param  user  Address of the user which blocked funds
      * @return The last unix timestamp the user blocked funds at, which starts the waiting period for single-sig withdrawals 
      */
    function getLastBlockedTimestamp(address user) view public returns(uint256){
        return last_blocked_timestamp[user];
    }

    /** @notice We have to check returndatasize after ERC20 tokens transfers, as some tokens are implemented badly (dont return a boolean)
      * @return Whether the last ERC20 transfer failed or succeeded
      */
    function checkERC20TransferSuccess() pure internal returns(bool){
        uint256 success = 0;

        assembly {
            switch returndatasize               // Check the number of bytes the token contract returned
                case 0 {                        // Nothing returned, but contract did not throw > assume our transfer succeeded
                    success := 1
                }
                case 32 {                       // 32 bytes returned, result is the returned bool
                    returndatacopy(0, 0, 32)
                    success := mload(0)
                }
        }

        return success != 0;
    }
}

contract dexBlueStructs is dexBlueStorage{

    // EIP712 Domain
    struct EIP712_Domain {
        string  name;
        string  version;
        uint256 chainId;
        address verifyingContract;
    }
    bytes32 constant EIP712_DOMAIN_TYPEHASH = keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)");
    bytes32          EIP712_DOMAIN_SEPARATOR;
    // Order typehash
    bytes32 constant EIP712_ORDER_TYPEHASH = keccak256("Order(address sellTokenAddress,uint128 sellTokenAmount,address buyTokenAddress,uint128 buyTokenAmount,uint32 expiry,uint64 nonce)");
    // Withdrawal typehash
    bytes32 constant EIP712_WITHDRAWAL_TYPEHASH = keccak256("Withdrawal(address token,uint256 amount,uint64 nonce)");


    struct Order{
        address     sellToken;     // The token, the order signee wants to sell
        uint256     sellAmount;    // The total amount the signee wants to give for the amount he wants to buy (the orders "rate" is implied by the ratio between the two amounts)
        address     buyToken;      // The token, the order signee wants to buy
        uint256     buyAmount;     // The total amount the signee wants to buy
        uint256     expiry;        // The expiry time of the order (after which it is not longer valid)
        bytes32     hash;          // The orders hash
        address     signee;        // The orders signee
    }

    struct OrderInputPacked{
        /*
            BITMASK                                                            | BYTE RANGE | DESCRIPTION
            -------------------------------------------------------------------|------------|----------------------------------
            0xffffffffffffffffffffffffffffffff00000000000000000000000000000000 |  0 - 15    | sell amount
            0x00000000000000000000000000000000ffffffffffffffffffffffffffffffff | 16 - 31    | buy amount
        */  
        bytes32     packedInput1;
        /*
            BITMASK                                                            | BYTE RANGE | DESCRIPTION
            -------------------------------------------------------------------|------------|----------------------------------
            0xffff000000000000000000000000000000000000000000000000000000000000 |  0 -  1    | sell token identifier
            0x0000ffff00000000000000000000000000000000000000000000000000000000 |  2 -  3    | buy token identifier
            0x00000000ffffffff000000000000000000000000000000000000000000000000 |  4 -  7    | expiry
            0x0000000000000000ffffffffffffffff00000000000000000000000000000000 |  8 - 15    | nonce
            0x00000000000000000000000000000000ff000000000000000000000000000000 | 16 - 16    | v
            0x0000000000000000000000000000000000ff0000000000000000000000000000 | 17 - 17    | signing scheme 0x00 = personal.sign, 0x01 = EIP712
            0x000000000000000000000000000000000000ff00000000000000000000000000 | 18 - 18    | signed by delegate
        */
        bytes32     packedInput2;

        bytes32     r;                          // Signature r
        bytes32     s;                          // Signature s
    }

    /** @notice Helper function parse an Order struct from an OrderInputPacked struct
      * @param   orderInput  The OrderInputPacked struct to parse
      * @return The parsed Order struct
      */
    function orderFromInput(OrderInputPacked memory orderInput) view public returns(Order memory){
        // Parse packed input
        Order memory order = Order({
            sellToken  : tokens[uint256(orderInput.packedInput2 >> 240)],
            sellAmount : uint256(orderInput.packedInput1 >> 128),
            buyToken   : tokens[uint256((orderInput.packedInput2 & 0x0000ffff00000000000000000000000000000000000000000000000000000000) >> 224)],
            buyAmount  : uint256(orderInput.packedInput1 & 0x00000000000000000000000000000000ffffffffffffffffffffffffffffffff),
            expiry     : uint256((orderInput.packedInput2 & 0x00000000ffffffff000000000000000000000000000000000000000000000000) >> 192), 
            hash       : 0x0,
            signee     : address(0x0)
        });

        // Restore order hash
        if(
            orderInput.packedInput2[17] == byte(0x00)   // Signing scheme
        ){                                              // Order is hashed after signature scheme personal.sign()
            order.hash = keccak256(abi.encodePacked(    // Restore the hash of this order
                "\x19Ethereum Signed Message:\n32",
                keccak256(abi.encodePacked(
                    order.sellToken,
                    uint128(order.sellAmount),
                    order.buyToken,
                    uint128(order.buyAmount),
                    uint32(order.expiry), 
                    uint64(uint256((orderInput.packedInput2 & 0x0000000000000000ffffffffffffffff00000000000000000000000000000000) >> 128)), // nonce     
                    address(this)                       // This contract's address
                ))
            ));
        }else{                                          // Order is hashed after EIP712
            order.hash = keccak256(abi.encodePacked(
                "\x19\x01",
                EIP712_DOMAIN_SEPARATOR,
                keccak256(abi.encode(
                    EIP712_ORDER_TYPEHASH,
                    order.sellToken,
                    order.sellAmount,
                    order.buyToken,
                    order.buyAmount,
                    order.expiry, 
                    uint256((orderInput.packedInput2 & 0x0000000000000000ffffffffffffffff00000000000000000000000000000000) >> 128) // nonce   
                ))
            ));
        }

        // Restore the signee of this order
        order.signee = ecrecover(
            order.hash,                             // Order hash
            uint8(orderInput.packedInput2[16]),     // Signature v
            orderInput.r,                           // Signature r
            orderInput.s                            // Signature s
        );

        // When the signature was delegated restore delegating address
        if(
            orderInput.packedInput2[18] == byte(0x01)  // Is delegated
        ){
            order.signee = delegates[order.signee];
        }

        return order;
    }

    struct Trade{
        uint256 makerAmount;
        uint256 takerAmount; 
        uint256 makerFee; 
        uint256 takerFee;
        uint256 makerRebate;
    }

    struct ReserveReserveTrade{
        address makerToken;
        address takerToken; 
        uint256 makerAmount;
        uint256 takerAmount; 
        uint256 makerFee; 
        uint256 takerFee;
        uint256 gasLimit;
    }

    struct ReserveTrade{
        uint256 orderAmount;
        uint256 reserveAmount; 
        uint256 orderFee; 
        uint256 reserveFee;
        uint256 orderRebate;
        uint256 reserveRebate;
        bool    orderIsMaker;
        uint256 gasLimit;
    }

    struct TradeInputPacked{
        /* 
            BITMASK                                                            | BYTE RANGE | DESCRIPTION
            -------------------------------------------------------------------|------------|----------------------------------
            0xffffffffffffffffffffffffffffffff00000000000000000000000000000000 |  0 - 15    | maker amount
            0x00000000000000000000000000000000ffffffffffffffffffffffffffffffff | 16 - 31    | taker amount
        */
        bytes32     packedInput1;  
        /*
            BITMASK                                                            | BYTE RANGE | DESCRIPTION
            -------------------------------------------------------------------|------------|----------------------------------
            0xffffffffffffffffffffffffffffffff00000000000000000000000000000000 |  0-15      | maker fee
            0x00000000000000000000000000000000ffffffffffffffffffffffffffffffff | 16-31      | taker fee
        */
        bytes32     packedInput2; 
        /*
            BITMASK                                                            | BYTE RANGE | DESCRIPTION
            -------------------------------------------------------------------|------------|----------------------------------
            0xffffffffffffffffffffffffffffffff00000000000000000000000000000000 |  0 - 15    | maker rebate           (optional)
            0x00000000000000000000000000000000ff000000000000000000000000000000 | 16 - 16    | counterparty types:
                                                                               |            |   0x11 : maker=order,   taker=order, 
                                                                               |            |   0x10 : maker=order,   taker=reserve, 
                                                                               |            |   0x01 : maker=reserve, taker=order
                                                                               |            |   0x00 : maker=reserve, taker=reserve
            0x0000000000000000000000000000000000ffff00000000000000000000000000 | 17 - 18    | maker_identifier
            0x00000000000000000000000000000000000000ffff0000000000000000000000 | 19 - 20    | taker_identifier
            0x000000000000000000000000000000000000000000ffff000000000000000000 | 21 - 22    | maker_token_identifier (optional)
            0x0000000000000000000000000000000000000000000000ffff00000000000000 | 23 - 24    | taker_token_identifier (optional)
            0x00000000000000000000000000000000000000000000000000ffffff00000000 | 25 - 27    | gas_limit              (optional)
            0x00000000000000000000000000000000000000000000000000000000ff000000 | 28 - 28    | burn_gas_tokens        (optional)
        */
        bytes32     packedInput3; 
    }

    /** @notice Helper function parse an Trade struct from an TradeInputPacked struct
      * @param  packed      The TradeInputPacked struct to parse
      * @return The parsed Trade struct
      */
    function tradeFromInput(TradeInputPacked memory packed) public pure returns (Trade memory){
        return Trade({
            makerAmount : uint256(packed.packedInput1 >> 128),
            takerAmount : uint256(packed.packedInput1 & 0x00000000000000000000000000000000ffffffffffffffffffffffffffffffff),
            makerFee    : uint256(packed.packedInput2 >> 128),
            takerFee    : uint256(packed.packedInput2 & 0x00000000000000000000000000000000ffffffffffffffffffffffffffffffff),
            makerRebate : uint256(packed.packedInput3 >> 128)
        });
    }

    /** @notice Helper function parse an ReserveTrade struct from an TradeInputPacked struct
      * @param  packed      The TradeInputPacked struct to parse
      * @return The parsed ReserveTrade struct
      */
    function reserveTradeFromInput(TradeInputPacked memory packed) public pure returns (ReserveTrade memory){
        if(packed.packedInput3[16] == byte(0x10)){
            // maker is order, taker is reserve
            return ReserveTrade({
                orderAmount   : uint256( packed.packedInput1 >> 128),
                reserveAmount : uint256( packed.packedInput1 & 0x00000000000000000000000000000000ffffffffffffffffffffffffffffffff),
                orderFee      : uint256( packed.packedInput2 >> 128),
                reserveFee    : uint256( packed.packedInput2 & 0x00000000000000000000000000000000ffffffffffffffffffffffffffffffff),
                orderRebate   : uint256((packed.packedInput3 & 0xffffffffffffffffffffffffffffffff00000000000000000000000000000000) >> 128),
                reserveRebate : 0,
                orderIsMaker  : true,
                gasLimit      : uint256((packed.packedInput3 & 0x00000000000000000000000000000000000000000000000000ffffff00000000) >> 32)
            });
        }else{
            // taker is order, maker is reserve
            return ReserveTrade({
                orderAmount   : uint256( packed.packedInput1 & 0x00000000000000000000000000000000ffffffffffffffffffffffffffffffff),
                reserveAmount : uint256( packed.packedInput1 >> 128),
                orderFee      : uint256( packed.packedInput2 & 0x00000000000000000000000000000000ffffffffffffffffffffffffffffffff),
                reserveFee    : uint256( packed.packedInput2 >> 128),
                orderRebate   : 0,
                reserveRebate : uint256((packed.packedInput3 & 0xffffffffffffffffffffffffffffffff00000000000000000000000000000000) >> 128),
                orderIsMaker  : false,
                gasLimit      : uint256((packed.packedInput3 & 0x00000000000000000000000000000000000000000000000000ffffff00000000) >> 32)
            });
        }
    }

    /** @notice Helper function parse an ReserveReserveTrade struct from an TradeInputPacked struct
      * @param  packed      The TradeInputPacked struct to parse
      * @return The parsed ReserveReserveTrade struct
      */
    function reserveReserveTradeFromInput(TradeInputPacked memory packed) public view returns (ReserveReserveTrade memory){
        return ReserveReserveTrade({
            makerToken    : tokens[uint256((packed.packedInput3 & 0x000000000000000000000000000000000000000000ffff000000000000000000) >> 72)],
            takerToken    : tokens[uint256((packed.packedInput3 & 0x0000000000000000000000000000000000000000000000ffff00000000000000) >> 56)],
            makerAmount   : uint256( packed.packedInput1 >> 128),
            takerAmount   : uint256( packed.packedInput1 & 0x00000000000000000000000000000000ffffffffffffffffffffffffffffffff),
            makerFee      : uint256( packed.packedInput2 >> 128),
            takerFee      : uint256( packed.packedInput2 & 0x00000000000000000000000000000000ffffffffffffffffffffffffffffffff),
            gasLimit      : uint256((packed.packedInput3 & 0x00000000000000000000000000000000000000000000000000ffffff00000000) >> 32)
        });
    }

    struct RingTrade {
        bool    isReserve;      // 1 if this trade is from a reserve, 0 when from an order
        uint256 identifier;     // identifier of the reserve or order
        address giveToken;      // the token this trade gives, the receive token is the givetoken of the previous ring element
        uint256 giveAmount;     // the amount of giveToken, this ring element is giving for the amount it reeives from the previous element
        uint256 fee;            // the fee this ring element has to pay on the giveToken giveAmount of the previous ring element
        uint256 rebate;         // the rebate on giveAmount this element receives
        uint256 gasLimit;       // the gas limit for the reserve call (if the element is a reserve)
    }

    struct RingTradeInputPacked{
        /* 
            BITMASK                                                            | BYTE RANGE | DESCRIPTION
            -------------------------------------------------------------------|------------|----------------------------------
            0xffffffffffffffffffffffffffffffff00000000000000000000000000000000 |  0 - 15    | give amount
            0x00000000000000000000000000000000ffffffffffffffffffffffffffffffff | 16 - 31    | fee
        */
        bytes32     packedInput1;    
        /* 
            BITMASK                                                            | BYTE RANGE | DESCRIPTION
            -------------------------------------------------------------------|------------|----------------------------------
            0xffffffffffffffffffffffffffffffff00000000000000000000000000000000 |  0 - 15    | rebate
            0x00000000000000000000000000000000ff000000000000000000000000000000 | 16 - 16    | is reserve
            0x0000000000000000000000000000000000ffff00000000000000000000000000 | 17 - 18    | identifier
            0x00000000000000000000000000000000000000ffff0000000000000000000000 | 19 - 20    | giveToken identifier
            0x000000000000000000000000000000000000000000ffffff0000000000000000 | 21 - 23    | gas_limit
            0x000000000000000000000000000000000000000000000000ff00000000000000 | 24 - 24    | burn_gas_tokens
        */
        bytes32     packedInput2;   
    }

    /** @notice Helper function parse an RingTrade struct from an RingTradeInputPacked struct
      * @param  packed  The RingTradeInputPacked struct to parse
      * @return The parsed RingTrade struct
      */
    function ringTradeFromInput(RingTradeInputPacked memory packed) view public returns(RingTrade memory){
        return RingTrade({
            isReserve     : (packed.packedInput2[16] == bytes1(0x01)),
            identifier    : uint256((       packed.packedInput2 & 0x0000000000000000000000000000000000ffff00000000000000000000000000) >> 104),
            giveToken     : tokens[uint256((packed.packedInput2 & 0x00000000000000000000000000000000000000ffff0000000000000000000000) >> 88)],
            giveAmount    : uint256(        packed.packedInput1                                                                       >> 128),
            fee           : uint256(        packed.packedInput1 & 0x00000000000000000000000000000000ffffffffffffffffffffffffffffffff),
            rebate        : uint256(        packed.packedInput2                                                                       >> 128),
            gasLimit      : uint256((       packed.packedInput2 & 0x000000000000000000000000000000000000000000ffffff0000000000000000) >> 64)
        });
    }
}

contract dexBlueSettlementModule is dexBlueStorage, dexBlueEvents, dexBlueUtils, dexBlueStructs{

    /** @notice Internal helper function to settle a trade between two orders
      * @param  makerOrder  The maker order
      * @param  takerOrder  The taker order
      * @param  trade       The trade to settle between the two
      * @return Whether the trade succeeded or failed
      */
    function matchOrders(
        Order memory makerOrder,
        Order memory takerOrder,
        Trade memory trade
    ) internal returns (bool){
        // Load the orders previously matched amounts into memory
        uint makerOrderMatched = matched[makerOrder.hash];
        uint takerOrderMatched = matched[takerOrder.hash];

        if( // Check if the arbiter has matched following the conditions of the two order signees
            // Do maker and taker want to trade the same tokens with each other
               makerOrder.buyToken == takerOrder.sellToken
            && takerOrder.buyToken == makerOrder.sellToken

            // Are both of the orders still valid
            && makerOrder.expiry > block.timestamp
            && takerOrder.expiry > block.timestamp 

            // Do maker and taker hold the required balances
            && balances[makerOrder.sellToken][makerOrder.signee] >= trade.makerAmount - trade.makerRebate
            && balances[takerOrder.sellToken][takerOrder.signee] >= trade.takerAmount

            // Are they both matched at a rate better or equal to the one they signed
            && trade.makerAmount - trade.makerRebate <= makerOrder.sellAmount * trade.takerAmount / makerOrder.buyAmount + 1  // Check maker doesn't overpay (+ 1 to deal with rouding errors for very smal amounts)
            && trade.takerAmount                     <= takerOrder.sellAmount * trade.makerAmount / takerOrder.buyAmount + 1  // Check taker doesn't overpay (+ 1 to deal with rouding errors for very smal amounts)

            // Check if the order was cancelled
            && makerOrder.sellAmount > makerOrderMatched
            && takerOrder.sellAmount > takerOrderMatched

            // Check if the matched amount + previously matched trades doesn't exceed the amount specified by the order signee
            && trade.makerAmount - trade.makerRebate + makerOrderMatched <= makerOrder.sellAmount
            && trade.takerAmount                     + takerOrderMatched <= takerOrder.sellAmount

            // Check if the charged fee is not too high
            && trade.makerFee <= trade.takerAmount / 20
            && trade.takerFee <= trade.makerAmount / 20

            // Check if maker_rebate is smaller than or equal to the taker's fee which compensates it
            && trade.makerRebate <= trade.takerFee
        ){
            // Settle the trade:

            // Substract sold amounts
            balances[makerOrder.sellToken][makerOrder.signee] -= trade.makerAmount - trade.makerRebate;     // Substract maker's sold amount minus the makers rebate
            balances[takerOrder.sellToken][takerOrder.signee] -= trade.takerAmount;                         // Substract taker's sold amount

            // Add bought amounts
            balances[makerOrder.buyToken][makerOrder.signee] += trade.takerAmount - trade.makerFee;         // Give the maker his bought amount minus the fee
            balances[takerOrder.buyToken][takerOrder.signee] += trade.makerAmount - trade.takerFee;         // Give the taker his bought amount minus the fee

            // Save sold amounts to prevent double matching
            matched[makerOrder.hash] += trade.makerAmount - trade.makerRebate;                              // Prevent maker order from being reused
            matched[takerOrder.hash] += trade.takerAmount;                                                  // Prevent taker order from being reused

            // Give fee to feeCollector
            balances[takerOrder.buyToken][feeCollector] += trade.takerFee - trade.makerRebate;              // Give the feeColletor the taker fee minus the maker rebate 
            balances[makerOrder.buyToken][feeCollector] += trade.makerFee;                                  // Give the feeColletor the maker fee

            // Set potential previous blocking of these funds to 0
            blocked_for_single_sig_withdrawal[makerOrder.sellToken][makerOrder.signee] = 0;                 // If the maker tried to block funds which he/she used in this order we have to unblock them
            blocked_for_single_sig_withdrawal[takerOrder.sellToken][takerOrder.signee] = 0;                 // If the taker tried to block funds which he/she used in this order we have to unblock them

            emit LogTrade(makerOrder.sellToken, trade.makerAmount, takerOrder.sellToken, trade.takerAmount);

            return true;                                                                         
        }else{
            return false;                                                                                   
        }
    }

    /** @notice Internal helper function to settle a trade between an order and a reserve
      * @param  order    The order
      * @param  reserve  The reserve
      * @param  trade    The trade to settle between the two
      * @return Whether the trade succeeded or failed
      */
    function matchOrderWithReserve(
        Order memory order,
        address      reserve,
        ReserveTrade memory trade
    ) internal returns(bool){
        // Load the orders previously matched amount into memory
        uint orderMatched = matched[order.hash];

        if( // Check if the arbiter matched the conditions of the order signee
            // Does the order signee has the required balances deposited
            balances[order.sellToken][order.signee] >= trade.orderAmount - trade.orderRebate

            // Is the order still valid
            && order.expiry > block.timestamp 

            // Is the order matched at a rate better or equal to the one specified by the signee
            && trade.orderAmount - trade.orderRebate <= order.sellAmount * trade.reserveAmount / order.buyAmount + 1  // + 1 to deal with rouding errors

            // Check if the order was cancelled
            && order.sellAmount > orderMatched

            // Check if the matched amount + previously matched trades doesn't exceed the amount specified by the order signee
            && trade.orderAmount - trade.orderRebate + orderMatched <= order.sellAmount

            // Check if the charged fee is not too high
            && trade.orderFee   <= trade.reserveAmount / 20
            && trade.reserveFee <= trade.orderAmount   / 20

            // Check if the rebates can be compensated by the fees
            && trade.orderRebate   <= trade.reserveFee
            && trade.reserveRebate <= trade.orderFee
        ){
            balances[order.sellToken][order.signee] -= trade.orderAmount - trade.orderRebate;  // Substract users's sold amount minus the makers rebate

            (bool txSuccess, bytes memory returnData) = address(this).call.gas(
                    trade.gasLimit                                              // The gas limit for the call
                )(
                    abi.encodePacked(                                           // This encodes the function to call and the parameters we are passing to the settlement function
                        dexBlue(address(0)).executeReserveTrade.selector,       // This function executes the call to the reserve
                        abi.encode(                            
                            order.sellToken,                                    // The token the order signee wants to exchange with the reserve
                            trade.orderAmount   - trade.reserveFee,             // The reserve receives the sold amount minus the fee
                            order.buyToken,                                     // The token the order signee wants to receive from the reserve
                            trade.reserveAmount - trade.reserveRebate,          // The reserve has to return the amount the order want to receive minus
                            reserve                                             // The reserve the trade is settled with
                        )
                    )
                );

            if(
               txSuccess                                    // The call to the reserve did not fail
               && abi.decode(returnData, (bool))            // The call returned true (we are sure its a contract we called)
               // executeReserveTrade checks whether the reserve deposited the funds
            ){
                // Substract the deposited amount from reserves balance
                balances[order.buyToken][reserve]      -= trade.reserveAmount - trade.reserveRebate;    // Substract reserves's sold amount

                // The amount to the order signees balance
                balances[order.buyToken][order.signee] += trade.reserveAmount - trade.orderFee;         // Give the users his bought amount minus the fee

                // Save sold amounts to prevent double matching
                matched[order.hash] += trade.orderAmount - trade.orderRebate;                           // Prevent maker order from being reused

                // Give fee to feeCollector
                balances[order.buyToken][feeCollector]  += trade.orderFee   - trade.reserveRebate;      // Give the feeColletor the fee minus the maker rebate
                balances[order.sellToken][feeCollector] += trade.reserveFee - trade.orderRebate;        // Give the feeColletor the fee minus the maker rebate

                // Set potential previous blocking of these funds to 0
                blocked_for_single_sig_withdrawal[order.sellToken][order.signee] = 0;                   // If the user blocked funds which he/she used in this order we have to unblock them

                if(trade.orderIsMaker){
                    emit LogTrade(order.sellToken, trade.orderAmount, order.buyToken, trade.reserveAmount);
                }else{
                    emit LogTrade(order.buyToken, trade.reserveAmount, order.sellToken, trade.orderAmount);
                }
                emit LogDirectWithdrawal(reserve, order.sellToken, trade.orderAmount - trade.reserveFee);

                return true;
            }else{
                balances[order.sellToken][order.signee] += trade.orderAmount - trade.orderRebate;  // Refund substracted amount

                return false;
            }
        }else{
            return false;
        }
    }

    /** @notice Internal helper function to settle a trade between an order and a reserve, passing some additional data to the reserve
      * @param  order    The order
      * @param  reserve  The reserve
      * @param  trade    The trade to settle between the two
      * @param  data     The data to pass along to the reserve
      * @return Whether the trade succeeded or failed
      */
    function matchOrderWithReserveWithData(
        Order        memory order,
        address      reserve,
        ReserveTrade memory trade,
        bytes32[]    memory data
    ) internal returns(bool){
        // Load the orders previously matched amount into memory
        uint orderMatched = matched[order.hash];

        if( // Check if the arbiter matched the conditions of the order signee
            // Does the order signee has the required balances deposited
            balances[order.sellToken][order.signee] >= trade.orderAmount - trade.orderRebate

            // Is the order still valid
            && order.expiry > block.timestamp 

            // Is the order matched at a rate better or equal to the one specified by the signee
            && trade.orderAmount - trade.orderRebate <= order.sellAmount * trade.reserveAmount / order.buyAmount + 1  // + 1 to deal with rouding errors

            // Check if the order was cancelled
            && order.sellAmount > orderMatched

            // Check if the matched amount + previously matched trades doesn't exceed the amount specified by the order signee
            && trade.orderAmount - trade.orderRebate + orderMatched <= order.sellAmount

            // Check if the charged fee is not too high
            && trade.orderFee   <= trade.reserveAmount / 20
            && trade.reserveFee <= trade.orderAmount   / 20

            // Check if the rebates can be compensated by the fees
            && trade.orderRebate   <= trade.reserveFee
            && trade.reserveRebate <= trade.orderFee
        ){
            balances[order.sellToken][order.signee] -= trade.orderAmount - trade.orderRebate;  // Substract users's sold amount minus the makers rebate

            (bool txSuccess, bytes memory returnData) = address(this).call.gas(
                    trade.gasLimit                                                  // The gas limit for the call
                )(
                    abi.encodePacked(                                               // This encodes the function to call and the parameters we are passing to the settlement function
                        dexBlue(address(0)).executeReserveTradeWithData.selector,   // This function executes the call to the reserve
                        abi.encode(                            
                            order.sellToken,                                        // The token the order signee wants to exchange with the reserve
                            trade.orderAmount   - trade.reserveFee,                 // The reserve receives the sold amount minus the fee
                            order.buyToken,                                         // The token the order signee wants to receive from the reserve
                            trade.reserveAmount - trade.reserveRebate,              // The reserve has to return the amount the order want to receive minus
                            reserve,                                                // The reserve the trade is settled with
                            data                                                    // The data passed on to the reserve
                        )
                    )
                );

            if(
               txSuccess                                    // The call to the reserve did not fail
               && abi.decode(returnData, (bool))            // The call returned true (we are sure its a contract we called)
               // executeReserveTrade checks whether the reserve deposited the funds
            ){
                // substract the deposited amount from reserves balance
                balances[order.buyToken][reserve]      -= trade.reserveAmount - trade.reserveRebate;    // Substract reserves's sold amount

                // the amount to the order signees balance
                balances[order.buyToken][order.signee] += trade.reserveAmount - trade.orderFee;         // Give the users his bought amount minus the fee

                // Save sold amounts to prevent double matching
                matched[order.hash] += trade.orderAmount - trade.orderRebate;                           // Prevent maker order from being reused

                // Give fee to feeCollector
                balances[order.buyToken][feeCollector]  += trade.orderFee   - trade.reserveRebate;      // Give the feeColletor the fee minus the maker rebate
                balances[order.sellToken][feeCollector] += trade.reserveFee - trade.orderRebate;        // Give the feeColletor the fee minus the maker rebate

                // Set potential previous blocking of these funds to 0
                blocked_for_single_sig_withdrawal[order.sellToken][order.signee] = 0;                   // If the user blocked funds which he/she used in this order we have to unblock them

                if(trade.orderIsMaker){
                    emit LogTrade(order.sellToken, trade.orderAmount, order.buyToken, trade.reserveAmount);
                }else{
                    emit LogTrade(order.buyToken, trade.reserveAmount, order.sellToken, trade.orderAmount);
                }
                emit LogDirectWithdrawal(reserve, order.sellToken, trade.orderAmount - trade.reserveFee);

                return true;
            }else{
                balances[order.sellToken][order.signee] += trade.orderAmount - trade.orderRebate;  // Refund substracted amount

                return false;
            }
        }else{
            return false;
        }
    }

    /** @notice internal helper function to settle a trade between two reserves
      * @param  makerReserve  The maker reserve
      * @param  takerReserve  The taker reserve
      * @param  trade         The trade to settle between the two
      * @return Whether the trade succeeded or failed
      */
    function matchReserveWithReserve(
        address             makerReserve,
        address             takerReserve,
        ReserveReserveTrade memory trade
    ) internal returns(bool){

        (bool txSuccess, bytes memory returnData) = address(this).call.gas(
            trade.gasLimit                                                      // The gas limit for the call
        )(
            abi.encodePacked(                                                   // This encodes the function to call and the parameters we are passing to the settlement function
                dexBlue(address(0)).executeReserveReserveTrade.selector,     // This function executes the call to the reserves
                abi.encode(                            
                    makerReserve,
                    takerReserve,
                    trade
                )
            )
        );

        return (
            txSuccess                                    // The call to the reserve did not fail
            && abi.decode(returnData, (bool))            // The call returned true (we are sure its a contract we called)
        );
    }


    /** @notice internal helper function to settle a trade between two reserves
      * @param  makerReserve  The maker reserve
      * @param  takerReserve  The taker reserve
      * @param  trade         The trade to settle between the two
      * @param  makerData     The data to pass on to the maker reserve
      * @param  takerData     The data to pass on to the taker reserve
      * @return Whether the trade succeeded or failed
      */
    function matchReserveWithReserveWithData(
        address             makerReserve,
        address             takerReserve,
        ReserveReserveTrade memory trade,
        bytes32[] memory    makerData,
        bytes32[] memory    takerData
    ) internal returns(bool){

        (bool txSuccess, bytes memory returnData) = address(this).call.gas(
            trade.gasLimit                                                       // The gas limit for the call
        )(
            abi.encodePacked(                                                    // This encodes the function to call and the parameters we are passing to the settlement function
                dexBlue(address(0)).executeReserveReserveTradeWithData.selector, // This function executes the call to the reserves
                abi.encode(                            
                    makerReserve,
                    takerReserve,
                    trade,
                    makerData,
                    takerData
                )
            )
        );

        return (
            txSuccess                                    // The call to the reserve did not fail
            && abi.decode(returnData, (bool))            // The call returned true (we are sure its a contract we called)
        );
    }

    /** @notice Allows an arbiter to settle multiple trades between multiple orders and reserves
      * @param  orderInput     Array of all orders involved in the transactions
      * @param  tradeInput     Array of the trades to be settled
      */   
    function batchSettleTrades(OrderInputPacked[] calldata orderInput, TradeInputPacked[] calldata tradeInput) external {
        require(arbiters[msg.sender] && marketActive);      // Check if msg.sender is an arbiter and the market is active

        Order[] memory orders = new Order[](orderInput.length);
        uint256 i = orderInput.length;

        while(i-- != 0){                                // Loop through the orderInput array, to parse the infos and restore all signees
            orders[i] = orderFromInput(orderInput[i]);  // Parse this orders infos
        }

        uint256 makerIdentifier;
        uint256 takerIdentifier;

        for(i = 0; i < tradeInput.length; i++){
            makerIdentifier = uint256((tradeInput[i].packedInput3 & 0x0000000000000000000000000000000000ffff00000000000000000000000000) >> 104);
            takerIdentifier = uint256((tradeInput[i].packedInput3 & 0x00000000000000000000000000000000000000ffff0000000000000000000000) >> 88);

            if(tradeInput[i].packedInput3[16] == byte(0x11)){       // Both are orders
                if(!matchOrders(
                    orders[makerIdentifier],
                    orders[takerIdentifier],
                    tradeFromInput(tradeInput[i])
                )){
                    emit LogTradeFailed();      
                }
            }else if(tradeInput[i].packedInput3[16] == byte(0x10)){ // Maker is order, taker is reserve
                if(!matchOrderWithReserve(
                    orders[makerIdentifier],
                    reserves[takerIdentifier],
                    reserveTradeFromInput(tradeInput[i])
                )){
                    emit LogTradeFailed();      
                }
            }else if(tradeInput[i].packedInput3[16] == byte(0x01)){ // Taker is order, maker is reserve
                if(!matchOrderWithReserve(
                    orders[takerIdentifier],
                    reserves[makerIdentifier],
                    reserveTradeFromInput(tradeInput[i])
                )){
                    emit LogTradeFailed();      
                }
            }else{                                                  // Both are reserves
                if(!matchReserveWithReserve(
                    reserves[makerIdentifier],
                    reserves[takerIdentifier],
                    reserveReserveTradeFromInput(tradeInput[i])
                )){
                    emit LogTradeFailed();      
                }
            }
        }
    }

    /** @notice Allows an arbiter to settle a trade between two orders
      * @param  makerOrderInput  The packed maker order input
      * @param  takerOrderInput  The packed taker order input
      * @param  tradeInput       The packed trade to settle between the two
      */ 
    function settleTrade(OrderInputPacked calldata makerOrderInput, OrderInputPacked calldata takerOrderInput, TradeInputPacked calldata tradeInput) external {
        require(arbiters[msg.sender] && marketActive);      // Check if msg.sender is an arbiter and the market is active

        if(!matchOrders(
            orderFromInput(makerOrderInput),
            orderFromInput(takerOrderInput),
            tradeFromInput(tradeInput)
        )){
            emit LogTradeFailed();      
        }
    }

    /** @notice Allows an arbiter to settle a trade between an order and a reserve
      * @param  orderInput  The packed maker order input
      * @param  tradeInput  The packed trade to settle between the two
      */ 
    function settleReserveTrade(OrderInputPacked calldata orderInput, TradeInputPacked calldata tradeInput) external {
        require(arbiters[msg.sender] && marketActive);      // Check if msg.sender is an arbiter and the market is active

        if(!matchOrderWithReserve(
            orderFromInput(orderInput),
            reserves[
                tradeInput.packedInput3[16] == byte(0x01) ? // is maker reserve
                    // maker is reserve
                    uint256((tradeInput.packedInput3 & 0x0000000000000000000000000000000000ffff00000000000000000000000000) >> 104) :
                    // taker is reserve
                    uint256((tradeInput.packedInput3 & 0x00000000000000000000000000000000000000ffff0000000000000000000000) >> 88)
            ],
            reserveTradeFromInput(tradeInput)
        )){
            emit LogTradeFailed();      
        }
    }

    /** @notice Allows an arbiter to settle a trade between an order and a reserve
      * @param  orderInput  The packed maker order input
      * @param  tradeInput  The packed trade to settle between the two
      * @param  data        The data to pass on to the reserve
      */ 
    function settleReserveTradeWithData(
        OrderInputPacked calldata orderInput, 
        TradeInputPacked calldata tradeInput,
        bytes32[] calldata        data
    ) external {
        require(arbiters[msg.sender] && marketActive);      // Check if msg.sender is an arbiter and the market is active

        if(!matchOrderWithReserveWithData(
            orderFromInput(orderInput),
            reserves[
                tradeInput.packedInput3[16] == byte(0x01) ? // Is maker reserve
                    // maker is reserve
                    uint256((tradeInput.packedInput3 & 0x0000000000000000000000000000000000ffff00000000000000000000000000) >> 104) :
                    // taker is reserve
                    uint256((tradeInput.packedInput3 & 0x00000000000000000000000000000000000000ffff0000000000000000000000) >> 88)
            ],
            reserveTradeFromInput(tradeInput),
            data
        )){
            emit LogTradeFailed();      
        }
    }

    /** @notice Allows an arbiter to settle a trade between two reserves
      * @param  tradeInput  The packed trade to settle between the two
      */ 
    function settleReserveReserveTrade(
        TradeInputPacked calldata tradeInput
    ) external {
        require(arbiters[msg.sender] && marketActive);      // Check if msg.sender is an arbiter and the market is active

        if(!matchReserveWithReserve(
            reserves[uint256((tradeInput.packedInput3 & 0x0000000000000000000000000000000000ffff00000000000000000000000000) >> 104)],
            reserves[uint256((tradeInput.packedInput3 & 0x00000000000000000000000000000000000000ffff0000000000000000000000) >> 88)],
            reserveReserveTradeFromInput(tradeInput)
        )){
            emit LogTradeFailed();      
        }
    }

    /** @notice Allows an arbiter to settle a trade between two reserves
      * @param  tradeInput  The packed trade to settle between the two
      * @param  makerData   The data to pass on to the maker reserve
      * @param  takerData   The data to pass on to the taker reserve
      */ 
    function settleReserveReserveTradeWithData(
        TradeInputPacked calldata tradeInput,
        bytes32[] calldata        makerData,
        bytes32[] calldata        takerData
    ) external {
        require(arbiters[msg.sender] && marketActive);      // Check if msg.sender is an arbiter and the market is active

        if(!matchReserveWithReserveWithData(
            reserves[uint256((tradeInput.packedInput3 & 0x0000000000000000000000000000000000ffff00000000000000000000000000) >> 104)],
            reserves[uint256((tradeInput.packedInput3 & 0x00000000000000000000000000000000000000ffff0000000000000000000000) >> 88)],
            reserveReserveTradeFromInput(tradeInput),
            makerData,
            takerData
        )){
            emit LogTradeFailed();      
        }
    }

    /** @notice Allow arbiters to settle a ring of order and reserve trades
      * @param  orderInput Array of OrderInputPacked structs
      * @param  tradeInput Array of RingTradeInputPacked structs
      */
    function settleRingTrade(OrderInputPacked[] calldata orderInput, RingTradeInputPacked[] calldata tradeInput) external {
        require(arbiters[msg.sender] && marketActive);      // Check if msg.sender is an arbiter and the market is active

        // Parse Orders from packed input
        uint256 i = orderInput.length;
        Order[] memory orders = new Order[](i);
        while(i-- != 0){
            orders[i] = orderFromInput(orderInput[i]);
        }

        // Parse RingTrades from packed input
        i = tradeInput.length;
        RingTrade[] memory trades = new RingTrade[](i);
        while(i-- != 0){
            trades[i] = ringTradeFromInput(tradeInput[i]);
        }

        uint256 prev = trades.length - 1;
        uint256 next = 1;
         // Loop through the RingTrades array and settle each participants trade
        for(i = 0; i < trades.length; i++){

            require(
                // Check if the charged fee is not too high
                trades[i].fee       <= trades[prev].giveAmount / 20

                // Check if maker_rebate is smaller than or equal to the taker's fee which compensates it
                && trades[i].rebate <= trades[next].fee
            );

            if(trades[i].isReserve){ // Ring element is a reserve
                address reserve = reserves[trades[i].identifier];

                if(i == 0){
                    require(
                        dexBlueReserve(reserve).offer(
                            trades[i].giveToken,                                   // The token the reserve would sell
                            trades[i].giveAmount - trades[i].rebate,               // The amount the reserve would sell
                            trades[prev].giveToken,                                // The token the reserve would receive
                            trades[prev].giveAmount - trades[i].fee                // The amount the reserve would receive
                        )
                        && balances[trades[i].giveToken][reserve] >= trades[i].giveAmount
                    );
                }else{
                    uint256 receiveAmount = trades[prev].giveAmount - trades[i].fee;

                    if(trades[prev].giveToken != address(0)){
                        Token(trades[prev].giveToken).transfer(reserve, receiveAmount);  // Send collateral to reserve
                        require(                                                         // Revert if the send failed
                            checkERC20TransferSuccess(),
                            "ERC20 token transfer failed."
                        );
                    }

                    require(
                        dexBlueReserve(reserve).trade.value(
                            trades[prev].giveToken == address(0) ? receiveAmount : 0
                        )(             
                            trades[prev].giveToken,
                            receiveAmount,                                      // Reserve gets the reserve_buy_amount minus the fee
                            trades[i].giveToken,    
                            trades[i].giveAmount - trades[i].rebate             // Reserve has to give reserve_sell_amount minus the rebate
                        )
                    );
                }

                // Substract deposited amount from reserves balance
                balances[trades[i].giveToken][reserve] -= trades[i].giveAmount - trades[i].rebate;

                emit LogDirectWithdrawal(reserve, trades[prev].giveToken, trades[prev].giveAmount - trades[i].fee);
            }else{ // Ring element is an order

                Order memory order = orders[trades[i].identifier];  // Cache order

                uint256 orderMatched = matched[order.hash];

                require(
                    // Does the order signee want to trade the last elements giveToken and this elements giveToken
                       order.buyToken  == trades[prev].giveToken
                    && order.sellToken == trades[i].giveToken

                    // Is the order still valid
                    && order.expiry > block.timestamp

                    // Does the order signee hold the required balances
                    && balances[order.sellToken][order.signee] >= trades[i].giveAmount - trades[i].rebate

                    // Is the order matched at a rate better or equal to the one the order signee signed
                    && trades[i].giveAmount - trades[i].rebate <= order.sellAmount * trades[prev].giveAmount / order.buyAmount + 1  // Check order doesn't overpay (+ 1 to deal with rouding errors for very smal amounts)

                    // Check if the order was cancelled
                    && order.sellAmount > orderMatched

                    // Do the matched amount + previously matched trades not exceed the amount specified by the order signee
                    && trades[i].giveAmount - trades[i].rebate + orderMatched <= order.sellAmount
                );

                // Substract the sold amounts
                balances[order.sellToken       ][order.signee] -= trades[i].giveAmount - trades[i].rebate;      // Substract sold amount minus the makers rebate from order signees balance

                // Add bought amounts
                balances[trades[prev].giveToken][order.signee] += trades[prev].giveAmount - trades[i].fee;      // Give the order signee his bought amount minus the fee

                // Save sold amounts to prevent double matching
                matched[order.hash] += trades[i].giveAmount - trades[i].rebate;                                 // Prevent order from being reused

                // Set potential previous blocking of these funds to 0
                blocked_for_single_sig_withdrawal[order.sellToken][order.signee] = 0;                           // If the order signee tried to block funds which he/she used in this order we have to unblock them
            }

            emit LogTrade(trades[prev].giveToken, trades[prev].giveAmount, trades[i].giveToken, trades[i].giveAmount);

            // Give fee to feeCollector
            balances[trades[prev].giveToken][feeCollector] += trades[i].fee - trades[prev].rebate;              // Give the feeColletor the fee minus the maker rebate 

            prev = i;
            if(i == trades.length - 2){
                next = 0;
            }else{
                next = i + 2;
            }
        }

        if(trades[0].isReserve){
            address payable reserve = reserves[trades[0].identifier];
            prev = trades.length - 1;

            if(trades[prev].giveToken == address(0)){                                                       // Is the withdrawal token ETH
                require(
                    reserve.send(trades[prev].giveAmount - trades[0].fee),                                  // Withdraw ETH
                    "Sending of ETH failed."
                );
            }else{
                Token(trades[prev].giveToken).transfer(reserve, trades[prev].giveAmount - trades[0].fee);   // Withdraw ERC20
                require(                                                                                    // Revert if the withdrawal failed
                    checkERC20TransferSuccess(),
                    "ERC20 token transfer failed."
                );
            }

            // Notify the reserve, that the offer got executed
            dexBlueReserve(reserve).offerExecuted(
                trades[0].giveToken,                                   // The token the reserve sold
                trades[0].giveAmount - trades[0].rebate,               // The amount the reserve sold
                trades[prev].giveToken,                                // The token the reserve received
                trades[prev].giveAmount - trades[0].fee                // The amount the reserve received
            );
        }
    }


    /** @notice Allow arbiters to settle a ring of order and reserve trades, passing on some data to the reserves
      * @param  orderInput Array of OrderInputPacked structs
      * @param  tradeInput Array of RingTradeInputPacked structs
      * @param  data       Array of data to pass along to the reserves
      */
    function settleRingTradeWithData(
        OrderInputPacked[]     calldata orderInput,
        RingTradeInputPacked[] calldata tradeInput,
        bytes32[][]            calldata data
    ) external {
        require(arbiters[msg.sender] && marketActive);      // Check if msg.sender is an arbiter and the market is active

        // Parse Orders from packed input
        uint256 i = orderInput.length;
        Order[] memory orders = new Order[](i);
        while(i-- != 0){
            orders[i] = orderFromInput(orderInput[i]);
        }

        // Parse RingTrades from packed input
        i = tradeInput.length;
        RingTrade[] memory trades = new RingTrade[](i);
        while(i-- != 0){
            trades[i] = ringTradeFromInput(tradeInput[i]);
        }

        uint256 prev = trades.length - 1;
        uint256 next = 1;
         // Loop through the RingTrades array and settle each participants trade
        for(i = 0; i < trades.length; i++){

            require(
                // Check if the charged fee is not too high
                trades[i].fee       <= trades[prev].giveAmount / 20

                // Check if maker_rebate is smaller than or equal to the taker's fee which compensates it
                && trades[i].rebate <= trades[next].fee
            );

            if(trades[i].isReserve){ // ring element is a reserve
                address reserve = reserves[trades[i].identifier];

                if(i == 0){
                    require(
                        dexBlueReserve(reserve).offerWithData(
                            trades[i].giveToken,                                   // The token the reserve would sell
                            trades[i].giveAmount - trades[i].rebate,               // The amount the reserve would sell
                            trades[prev].giveToken,                                // The token the reserve would receive
                            trades[prev].giveAmount - trades[i].fee,               // The amount the reserve would receive
                            data[i]                                                // The data to pass along to the reserve
                        )
                        && balances[trades[i].giveToken][reserve] >= trades[i].giveAmount
                    );
                }else{
                    uint256 receiveAmount = trades[prev].giveAmount - trades[i].fee;

                    if(trades[prev].giveToken != address(0)){
                        Token(trades[prev].giveToken).transfer(reserve, receiveAmount);  // Send collateral to reserve
                        require(                                                         // Revert if the send failed
                            checkERC20TransferSuccess(),
                            "ERC20 token transfer failed."
                        );
                    }

                    require(
                        dexBlueReserve(reserve).tradeWithData.value(
                            trades[prev].giveToken == address(0) ? receiveAmount : 0
                        )(             
                            trades[prev].giveToken,
                            receiveAmount,                                      // Reserve gets the reserve_buy_amount minus the fee
                            trades[i].giveToken,    
                            trades[i].giveAmount - trades[i].rebate,            // Reserve has to give reserve_sell_amount minus the reserve rebate
                            data[i]                                             // The data to pass along to the reserve
                        )
                    );
                }

                // Substract deposited amount from reserves balance
                balances[trades[i].giveToken][reserve] -= trades[i].giveAmount - trades[i].rebate;

                emit LogDirectWithdrawal(reserve, trades[prev].giveToken, trades[prev].giveAmount - trades[i].fee);
            }else{ // Ring element is an order

                Order memory order = orders[trades[i].identifier];  // Cache order

                uint256 orderMatched = matched[order.hash];

                require(
                    // Does the order signee want to trade the last elements giveToken and this elements giveToken
                       order.buyToken  == trades[prev].giveToken
                    && order.sellToken == trades[i].giveToken

                    // Is the order still valid
                    && order.expiry > block.timestamp

                    // Does the order signee hold the required balances
                    && balances[order.sellToken][order.signee] >= trades[i].giveAmount - trades[i].rebate

                    // Is the order matched at a rate better or equal to the one the order signee signed
                    && trades[i].giveAmount - trades[i].rebate <= order.sellAmount * trades[prev].giveAmount / order.buyAmount + 1  // Check order doesn't overpay (+ 1 to deal with rouding errors for very smal amounts)

                    // Check if the order was cancelled
                    && order.sellAmount > orderMatched

                    // Do the matched amount + previously matched trades not exceed the amount specified by the order signee
                    && trades[i].giveAmount - trades[i].rebate + orderMatched <= order.sellAmount
                );

                // Substract the sold amounts
                balances[order.sellToken       ][order.signee] -= trades[i].giveAmount - trades[i].rebate;      // Substract sold amount minus the makers rebate from order signees balance

                // Add bought amounts
                balances[trades[prev].giveToken][order.signee] += trades[prev].giveAmount - trades[i].fee;      // Give the order signee his bought amount minus the fee

                // Save sold amounts to prevent double matching
                matched[order.hash] += trades[i].giveAmount - trades[i].rebate;                                 // Prevent order from being reused

                // Set potential previous blocking of these funds to 0
                blocked_for_single_sig_withdrawal[order.sellToken][order.signee] = 0;                           // If the order signee tried to block funds which he/she used in this order we have to unblock them
            }

            emit LogTrade(trades[prev].giveToken, trades[prev].giveAmount, trades[i].giveToken, trades[i].giveAmount);

            // Give fee to feeCollector
            balances[trades[prev].giveToken][feeCollector] += trades[i].fee - trades[prev].rebate;              // Give the feeColletor the fee minus the maker rebate 

            prev = i;
            if(i == trades.length - 2){
                next = 0;
            }else{
                next = i + 2;
            }
        }

        if(trades[0].isReserve){
            address payable reserve = reserves[trades[0].identifier];
            prev = trades.length - 1;

            if(trades[prev].giveToken == address(0)){                                                       // Is the withdrawal token ETH
                require(
                    reserve.send(trades[prev].giveAmount - trades[0].fee),                                  // Withdraw ETH
                    "Sending of ETH failed."
                );
            }else{
                Token(trades[prev].giveToken).transfer(reserve, trades[prev].giveAmount - trades[0].fee);   // Withdraw ERC20
                require(                                                                                    // Revert if the withdrawal failed
                    checkERC20TransferSuccess(),
                    "ERC20 token transfer failed."
                );
            }

            // Notify the reserve, that the offer got executed
            dexBlueReserve(reserve).offerExecuted(
                trades[0].giveToken,                                   // The token the reserve sold
                trades[0].giveAmount - trades[0].rebate,               // The amount the reserve sold
                trades[prev].giveToken,                                // The token the reserve received
                trades[prev].giveAmount - trades[0].fee                // The amount the reserve received
            );
        }
    }


    // Swapping functions

    /** @notice Queries best output for a trade currently available from the reserves
      * @param  sell_token   The token the user wants to sell (ETH is address(0))
      * @param  sell_amount  The amount of sell_token to sell
      * @param  buy_token    The token the user wants to acquire (ETH is address(0))
      * @return The output amount the reserve with the best price offers
    */
    function getSwapOutput(address sell_token, uint256 sell_amount, address buy_token) public view returns (uint256){
        (, uint256 output) = getBestReserve(sell_token, sell_amount, buy_token);
        return output;
    }

    /** @notice Queries the reserve address and output of trade, of the reserve which offers the best deal on a trade
      * @param  sell_token   The token the user wants to sell (ETH is address(0))
      * @param  sell_amount  The amount of sell_token to sell
      * @param  buy_token    The token the user wants to acquire (ETH is address(0))
      * @return The address of the reserve offering the best deal and the expected output of the trade
    */
    function getBestReserve(address sell_token, uint256 sell_amount, address buy_token) public view returns (address, uint256){
        address bestReserve;
        uint256 bestOutput = 0;
        uint256 output;

        for(uint256 i = 0; i < public_reserve_arr.length; i++){
            output = dexBlueReserve(public_reserve_arr[i]).getSwapOutput(sell_token, sell_amount, buy_token);
            if(output > bestOutput){
                bestOutput  = output;
                bestReserve = public_reserve_arr[i];
            }
        }

        return (bestReserve, bestOutput);
    }

    /** @notice Allows users to swap a token or ETH with the reserve offering the best price for his trade
      * @param  sell_token   The token the user wants to sell (ETH is address(0))
      * @param  sell_amount  The amount of sell_token to sell
      * @param  buy_token    The token the user wants to acquire (ETH is address(0))
      * @param  min_output   The minimum amount of buy_token, the trade should result in 
      * @param  deadline     The timestamp after which the transaction should not be executed
      * @return The amount of buy_token the user receives
    */
    function swap(address sell_token, uint256 sell_amount, address buy_token,  uint256 min_output, uint256 deadline) external payable returns(uint256){        
        require(
            (
                deadline == 0                               // No deadline is set         
                || deadline > block.timestamp               // Deadline is met
            ),                                              // Check whether the deadline is met
            "Call deadline exceeded."
        );

        (address reserve, uint256 amount) = getBestReserve(sell_token, sell_amount, buy_token);     // Check which reserve offers the best deal on the trade

        require(
            amount >= min_output,                                                                   // Check whether the best reserves deal is good enough
            "Too much slippage"
        );

        return swapWithReserve(sell_token, sell_amount, buy_token,  min_output, reserve, deadline); // Execute the swap with the best reserve
    }

    /** @notice Allows users to swap a token or ETH with a specified reserve
      * @param  sell_token   The token the user wants to sell (ETH is address(0))
      * @param  sell_amount  The amount of sell_token to sell
      * @param  buy_token    The token the user wants to acquire (ETH is address(0))
      * @param  min_output   The minimum amount of buy_token, the trade should result in 
      * @param  reserve      The address of the reserve to trade with
      * @param  deadline     The timestamp after which the transaction should not be executed
    */
    function swapWithReserve(address sell_token, uint256 sell_amount, address buy_token,  uint256 min_output, address reserve, uint256 deadline) public payable returns (uint256){
        require(
            (
                deadline == 0                               // No deadline is set         
                || deadline > block.timestamp               // Deadline is met
            ),
            "Call deadline exceeded."
        );

        require(
            public_reserves[reserve],                       // Check whether the reserve is registered
            "Unknown reserve."
        );

        if(sell_token == address(0)){                       // Caller wants to swap ETH
            require(
                msg.value == sell_amount,                   // Check whether the caller sent the required ETH
                "ETH amount not sent with the call."
            );
        }else{                                              // Caller wants to swap a token
            require(
                msg.value == 0,                             // Check the caller hasn't sent any ETH with the call
                "Don't send ETH when swapping a token."
            );

            Token(sell_token).transferFrom(msg.sender, reserve, sell_amount);   // Deposit ERC20 into the reserve

            require(
                checkERC20TransferSuccess(),                // Check whether the ERC20 token transfer was successful
                "ERC20 token transfer failed."
            );
        }

        // Execute the swap with the reserve
        uint256 output = dexBlueReserve(reserve).swap.value(msg.value)(
            sell_token,
            sell_amount,
            buy_token,
            min_output
        );

        if(
            output >= min_output                                // Check whether the output amount is sufficient 
            && balances[buy_token][reserve] >= output           // Check whether the reserve deposited the output amount
        ){
            balances[buy_token][reserve] -= output;             // Substract the amount from the reserves balance

            if(buy_token == address(0)){                        // Is the bought asset ETH
                require(
                    msg.sender.send(output),                    // Send the output ETH of the swap to msg.sender
                    "Sending of ETH failed."
                );
            }else{
                Token(buy_token).transfer(msg.sender, output);  // Transfer the output token of the swap msg.sender
                require(                                        // Revert if the transfer failed
                    checkERC20TransferSuccess(),
                    "ERC20 token transfer failed."
                );
            }

            emit LogSwap(sell_token, sell_amount, buy_token, output);

            return output;
        }else{
            revert("Too much slippage.");
        }
    }
}

contract dexBlue is dexBlueStorage, dexBlueEvents, dexBlueUtils, dexBlueStructs{
    // Hardcode settlement module contract:
    address constant settlementModuleAddress = 0x9e3d5C6ffACA00cAf136609680b536DC0Eb20c66;

    // Deposit functions:

    /** @notice Deposit Ether into the smart contract 
      */
    function depositEther() public payable{
        balances[address(0)][msg.sender] += msg.value;          // Add the received ETH to the users balance
        emit LogDeposit(msg.sender, address(0), msg.value);     // emit LogDeposit event
    }

    /** @notice Fallback function to credit ETH sent to the contract without data 
      */
    function() external payable{
        if(msg.sender != wrappedEtherContract){     // ETH sends from WETH contract are handled in the depositWrappedEther() function
            depositEther();                 // Call the deposit function to credit ETH sent in this transaction
        }
    }

    /** @notice Deposit Wrapped Ether (remember to set allowance in the token contract first)
      * @param  amount  The amount of WETH to deposit 
      */
    address constant wrappedEtherContract = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2; // We hardcode the address, to prevent misbehaviour through custom contracts (reentrancy etc)
    function depositWrappedEther(uint256 amount) external {

        Token(wrappedEtherContract).transferFrom(msg.sender, address(this), amount);    // Transfer WETH to this contract

        require(
            checkERC20TransferSuccess(),                                        // Check whether the ERC20 token transfer was successful
            "WETH deposit failed."
        );

        uint balanceBefore = address(this).balance;                             // Remember ETH balance before the call

        WETH(wrappedEtherContract).withdraw(amount);                            // Unwrap the WETH

        require(balanceBefore + amount == address(this).balance);               // Check whether the ETH was deposited

        balances[address(0)][msg.sender] += amount;                             // Credit the deposited eth to users balance

        emit LogDeposit(msg.sender, address(0), amount);                        // emit LogDeposit event
    }

    /** @notice Deposit ERC20 tokens into the smart contract (remember to set allowance in the token contract first)
      * @param  token   The address of the token to deposit
      * @param  amount  The amount of tokens to deposit 
      */
    function depositToken(address token, uint256 amount) external {
        Token(token).transferFrom(msg.sender, address(this), amount);    // Deposit ERC20
        require(
            checkERC20TransferSuccess(),                                 // Check whether the ERC20 token transfer was successful
            "ERC20 token transfer failed."
        );
        balances[token][msg.sender] += amount;                           // Credit the deposited token to users balance
        emit LogDeposit(msg.sender, token, amount);                      // emit LogDeposit event
    }

    // Multi-sig withdrawal functions:

    /** @notice User-submitted withdrawal with arbiters signature, which withdraws to the users address
      * @param  token   The token to withdraw (ETH is address(address(0)))
      * @param  amount  The amount of tokens to withdraw
      * @param  nonce   The nonce (to salt the hash)
      * @param  v       Multi-signature v
      * @param  r       Multi-signature r
      * @param  s       Multi-signature s 
      */
    function multiSigWithdrawal(address token, uint256 amount, uint64 nonce, uint8 v, bytes32 r, bytes32 s) external {
        multiSigSend(token, amount, nonce, v, r, s, msg.sender); // Call multiSigSend to send funds to msg.sender
    }    

    /** @notice User-submitted withdrawal with arbiters signature, which sends tokens to specified address
      * @param  token              The token to withdraw (ETH is address(address(0)))
      * @param  amount             The amount of tokens to withdraw
      * @param  nonce              The nonce (to salt the hash)
      * @param  v                  Multi-signature v
      * @param  r                  Multi-signature r
      * @param  s                  Multi-signature s
      * @param  receiving_address  The address to send the withdrawn token/ETH to
      */
    function multiSigSend(address token, uint256 amount, uint64 nonce, uint8 v, bytes32 r, bytes32 s, address payable receiving_address) public {
        bytes32 hash = keccak256(abi.encodePacked(                      // Calculate the withdrawal hash from the parameters 
            "\x19Ethereum Signed Message:\n32", 
            keccak256(abi.encodePacked(
                msg.sender,
                token,
                amount,
                nonce,
                address(this)
            ))
        ));
        if(
            !processed_withdrawals[hash]                                // Check if the withdrawal was initiated before
            && arbiters[ecrecover(hash, v,r,s)]                         // Check if the multi-sig is valid
            && balances[token][msg.sender] >= amount                    // Check if the user holds the required balance
        ){
            processed_withdrawals[hash]  = true;                        // Mark this withdrawal as processed
            balances[token][msg.sender] -= amount;                      // Substract the withdrawn balance from the users balance

            if(token == address(0)){                                    // Process an ETH withdrawal
                require(
                    receiving_address.send(amount),
                    "Sending of ETH failed."
                );
            }else{                                                      // Withdraw an ERC20 token
                Token(token).transfer(receiving_address, amount);       // Transfer the ERC20 token
                require(
                    checkERC20TransferSuccess(),                        // Check whether the ERC20 token transfer was successful
                    "ERC20 token transfer failed."
                );
            }

            blocked_for_single_sig_withdrawal[token][msg.sender] = 0;   // Set potential previous blocking of these funds to 0

            emit LogWithdrawal(msg.sender,token,amount);                // emit LogWithdrawal event
        }else{
            revert();                                                   // Revert the transaction if checks fail
        }
    }

    /** @notice User-submitted transfer with arbiters signature, which sends tokens to another addresses account in the smart contract
      * @param  token              The token to transfer (ETH is address(address(0)))
      * @param  amount             The amount of tokens to transfer
      * @param  nonce              The nonce (to salt the hash)
      * @param  v                  Multi-signature v
      * @param  r                  Multi-signature r
      * @param  s                  Multi-signature s
      * @param  receiving_address  The address to transfer the token/ETH to
      */
    function multiSigTransfer(address token, uint256 amount, uint64 nonce, uint8 v, bytes32 r, bytes32 s, address receiving_address) external {
        bytes32 hash = keccak256(abi.encodePacked(                      // Calculate the withdrawal/transfer hash from the parameters 
            "\x19Ethereum Signed Message:\n32", 
            keccak256(abi.encodePacked(
                msg.sender,
                token,
                amount,
                nonce,
                address(this)
            ))
        ));
        if(
            !processed_withdrawals[hash]                                // Check if the withdrawal was initiated before
            && arbiters[ecrecover(hash, v,r,s)]                         // Check if the multi-sig is valid
            && balances[token][msg.sender] >= amount                    // Check if the user holds the required balance
        ){
            processed_withdrawals[hash]         = true;                 // Mark this withdrawal as processed
            balances[token][msg.sender]        -= amount;               // Substract the balance from the withdrawing account
            balances[token][receiving_address] += amount;               // Add the balance to the receiving account

            blocked_for_single_sig_withdrawal[token][msg.sender] = 0;   // Set potential previous blocking of these funds to 0

            emit LogWithdrawal(msg.sender,token,amount);                // emit LogWithdrawal event
            emit LogDeposit(receiving_address,token,amount);            // emit LogDeposit event
        }else{
            revert();                                                   // Revert the transaction if checks fail
        }
    }

    /** @notice Arbiter submitted withdrawal with users multi-sig to users address
      * @param  packedInput1 tightly packed input arguments:
      *             amount  The amount of tokens to withdraw
      *             fee     The fee, covering the gas cost of the arbiter
      * @param  packedInput2 tightly packed input arguments:
      *             token           The token to withdraw (ETH is address(address(0)))
      *             nonce           The nonce (to salt the hash)
      *             v               Multi-signature v (either 27 or 28. To identify the different signing schemes an offset of 10 is applied for EIP712)
      *             signing_scheme  The signing scheme of the users signature
      *             burn_gas_tokens The amount of gas tokens to burn
      * @param  r       Multi-signature r
      * @param  s       Multi-signature s
      */
    function userSigWithdrawal(bytes32 packedInput1, bytes32 packedInput2, bytes32 r, bytes32 s) external {
        /* 
            BITMASK packedInput1                                               | BYTE RANGE | DESCRIPTION
            -------------------------------------------------------------------|------------|----------------------------------
            0xffffffffffffffffffffffffffffffff00000000000000000000000000000000 |  0-15      | amount
            0x00000000000000000000000000000000ffffffffffffffffffffffffffffffff | 16-31      | gas fee

            BITMASK packedInput2                                               | BYTE RANGE | DESCRIPTION
            -------------------------------------------------------------------|------------|----------------------------------
            0xffff000000000000000000000000000000000000000000000000000000000000 |  0- 1      | token identifier
            0x0000ffffffffffffffff00000000000000000000000000000000000000000000 |  2- 9      | nonce
            0x00000000000000000000ff000000000000000000000000000000000000000000 | 10-10      | v
            0x0000000000000000000000ff0000000000000000000000000000000000000000 | 11-11      | signing scheme 0x00 = personal.sign, 0x01 = EIP712
            0x000000000000000000000000ff00000000000000000000000000000000000000 | 12-12      | burn_gas_tokens
        */
        // parse the packed input parameters
        uint256 amount = uint256(packedInput1 >> 128);
        uint256 fee    = uint256(packedInput1 & 0x00000000000000000000000000000000ffffffffffffffffffffffffffffffff);
        address token  = tokens[uint256(packedInput2 >> 240)];
        uint64  nonce  = uint64(uint256((packedInput2 & 0x0000ffffffffffffffff00000000000000000000000000000000000000000000) >> 176));
        uint8   v      = uint8(packedInput2[10]);

        bytes32 hash;
        if(packedInput2[11] == byte(0x00)){                             // Standard signing scheme (personal.sign())
            hash = keccak256(abi.encodePacked(                          // Restore multi-sig hash
                "\x19Ethereum Signed Message:\n32",
                keccak256(abi.encodePacked(
                    token,
                    amount,
                    nonce,
                    address(this)
                ))
            ));
        }else{                                                          // EIP712 signing scheme
            hash = keccak256(abi.encodePacked(                          // Restore multi-sig hash
                "\x19\x01",
                EIP712_DOMAIN_SEPARATOR,
                keccak256(abi.encode(
                    EIP712_WITHDRAWAL_TYPEHASH,
                    token,
                    amount,
                    nonce
                ))
            ));
        }

        address payable account = address(uint160(ecrecover(hash, v, r, s)));   // Restore signing address

        if(
            !processed_withdrawals[hash]                                // Check if the withdrawal was initiated before
            && arbiters[msg.sender]                                     // Check if transaction comes from arbiter
            && fee <= amount / 20                                       // Check if fee is not too big
            && balances[token][account] >= amount                       // Check if the user holds the required tokens
        ){
            processed_withdrawals[hash]    = true;                      // Mark the withdrawal as processed
            balances[token][account]      -= amount;                    // Deduct the withdrawn tokens from the users balance
            balances[token][feeCollector] += fee;                       // Fee to cover gas costs for the withdrawal

            if(token == address(0)){                                    // Send ETH
                require(
                    account.send(amount - fee),
                    "Sending of ETH failed."
                );
            }else{
                Token(token).transfer(account, amount - fee);           // Withdraw ERC20
                require(
                    checkERC20TransferSuccess(),                        // Check if the transfer was successful
                    "ERC20 token transfer failed."
                );
            }

            blocked_for_single_sig_withdrawal[token][account] = 0;      // Set potential previous blocking of these funds to 0

            emit LogWithdrawal(account,token,amount);                   // emit LogWithdrawal event

            // burn gas tokens
            if(packedInput2[12] != byte(0x00)){
                spendGasTokens(uint8(packedInput2[12]));
            }
        }else{
            revert();                                                   // Revert the transaction is checks fail
        }
    }

    // Single-sig withdrawal functions:

    /** @notice Allows user to block funds for single-sig withdrawal after 24h waiting period 
      *         (This period is necessary to ensure all trades backed by these funds will be settled.)
      * @param  token   The address of the token to block (ETH is address(address(0)))
      * @param  amount  The amount of the token to block
      */
    function blockFundsForSingleSigWithdrawal(address token, uint256 amount) external {
        if (balances[token][msg.sender] - blocked_for_single_sig_withdrawal[token][msg.sender] >= amount){  // Check if the user holds the required funds
            blocked_for_single_sig_withdrawal[token][msg.sender] += amount;                                 // Block funds for manual withdrawal
            last_blocked_timestamp[msg.sender] = block.timestamp;                                           // Start waiting period
            emit LogBlockedForSingleSigWithdrawal(msg.sender, token, amount);                               // emit LogBlockedForSingleSigWithdrawal event
        }else{
            revert();                                                                                       // Revert the transaction if the user does not hold the required balance
        }
    }

    /** @notice Allows user to withdraw blocked funds without multi-sig after the waiting period
      * @param  token   The address of the token to withdraw (ETH is address(address(0)))
      * @param  amount  The amount of the token to withdraw
      */
    function initiateSingleSigWithdrawal(address token, uint256 amount) external {
        if (
            balances[token][msg.sender] >= amount                                   // Check if the user holds the funds
            && (
                (
                    blocked_for_single_sig_withdrawal[token][msg.sender] >= amount                          // Check if these funds are blocked
                    && last_blocked_timestamp[msg.sender] + single_sig_waiting_period <= block.timestamp    // Check if the waiting period has passed
                )
                || single_sig_waiting_period == 0                                                           // or the waiting period is disabled
            )
        ){
            balances[token][msg.sender] -= amount;                                  // Substract the tokens from users balance

            if(blocked_for_single_sig_withdrawal[token][msg.sender] >= amount){
                blocked_for_single_sig_withdrawal[token][msg.sender] = 0;     // Substract the tokens from users blocked balance
            }

            if(token == address(0)){                                                // Withdraw ETH
                require(
                    msg.sender.send(amount),
                    "Sending of ETH failed."
                );
            }else{                                                                  // Withdraw ERC20 tokens
                Token(token).transfer(msg.sender, amount);                          // Transfer the ERC20 tokens
                require(
                    checkERC20TransferSuccess(),                                    // Check if the transfer was successful
                    "ERC20 token transfer failed."
                );
            }

            emit LogSingleSigWithdrawal(msg.sender, token, amount);                 // emit LogSingleSigWithdrawal event
        }else{
            revert();                                                               // Revert the transaction if the required checks fail
        }
    } 

    //Trade settlement structs and function

    /** @notice Allows an arbiter to settle a trade between two orders
      * @param  makerOrderInput  The packed maker order input
      * @param  takerOrderInput  The packed taker order input
      * @param  tradeInput       The packed trade to settle between the two
      */ 
    function settleTrade(OrderInputPacked calldata makerOrderInput, OrderInputPacked calldata takerOrderInput, TradeInputPacked calldata tradeInput) external {
        require(arbiters[msg.sender] && marketActive);   // Check if msg.sender is an arbiter and the market is active

        settlementModuleAddress.delegatecall(msg.data);  // delegate the call to the settlement module

        // burn gas tokens
        if(tradeInput.packedInput3[28] != byte(0x00)){
            spendGasTokens(uint8(tradeInput.packedInput3[28]));
        }
    }

    /** @notice Allows an arbiter to settle a trade between an order and a reserve
      * @param  orderInput  The packed maker order input
      * @param  tradeInput  The packed trade to settle between the two
      */ 
    function settleReserveTrade(OrderInputPacked calldata orderInput, TradeInputPacked calldata tradeInput) external {
        require(arbiters[msg.sender] && marketActive);   // Check if msg.sender is an arbiter and the market is active

        settlementModuleAddress.delegatecall(msg.data);  // delegate the call to the settlement module

        // burn gas tokens
        if(tradeInput.packedInput3[28] != byte(0x00)){
            spendGasTokens(uint8(tradeInput.packedInput3[28]));
        }
    }

    /** @notice Allows an arbiter to settle a trade between an order and a reserve, passing some additional data to the reserve
      * @param  orderInput  The packed maker order input
      * @param  tradeInput  The packed trade to settle between the two
      * @param  data        The data to pass on to the reserve
      */ 
    function settleReserveTradeWithData(OrderInputPacked calldata orderInput, TradeInputPacked calldata tradeInput, bytes32[] calldata data) external {
        require(arbiters[msg.sender] && marketActive);      // Check if msg.sender is an arbiter and the market is active

        settlementModuleAddress.delegatecall(msg.data);  // delegate the call to the settlement module

        // burn gas tokens
        if(tradeInput.packedInput3[28] != byte(0x00)){
            spendGasTokens(uint8(tradeInput.packedInput3[28]));
        }
    }

    /** @notice Allows an arbiter to settle a trade between two reserves
      * @param  tradeInput  The packed trade to settle between the two
      */ 
    function settleReserveReserveTrade(TradeInputPacked calldata tradeInput) external {
        require(arbiters[msg.sender] && marketActive);          // Check if msg.sender is an arbiter and the market is active

        settlementModuleAddress.delegatecall(msg.data);  // delegate the call to the settlement module

        // burn gas tokens
        if(tradeInput.packedInput3[28] != byte(0x00)){
            spendGasTokens(uint8(tradeInput.packedInput3[28]));
        }
    }

    /** @notice Allows an arbiter to settle a trade between two reserves
      * @param  tradeInput  The packed trade to settle between the two
      * @param  makerData   The data to pass on to the maker reserve
      * @param  takerData   The data to pass on to the taker reserve
      */ 
    function settleReserveReserveTradeWithData(TradeInputPacked calldata tradeInput, bytes32[] calldata makerData, bytes32[] calldata takerData) external {
        require(arbiters[msg.sender] && marketActive);      // Check if msg.sender is an arbiter and the market is active

        settlementModuleAddress.delegatecall(msg.data);     // delegate the call to the settlement module

        // burn gas tokens
        if(tradeInput.packedInput3[28] != byte(0x00)){
            spendGasTokens(uint8(tradeInput.packedInput3[28]));
        }
    }


    /** @notice Allows an arbiter to settle multiple trades between multiple orders and reserves
      * @param  orderInput     Array of all orders involved in the transactions
      * @param  tradeInput     Array of the trades to be settled
      */   
    function batchSettleTrades(OrderInputPacked[] calldata orderInput, TradeInputPacked[] calldata tradeInput) external {
        require(arbiters[msg.sender] && marketActive);          // Check if msg.sender is an arbiter and the market is active

        settlementModuleAddress.delegatecall(msg.data);  // delegate the call to the settlement module

        // Loop through the trades and calc the gasToken sum
        uint256 i = tradeInput.length;        
        uint256 gasTokenSum;
        while(i-- != 0){
            gasTokenSum += uint8(tradeInput[i].packedInput3[28]);
        }

        // burn gas tokens
        if(gasTokenSum > 0){
            spendGasTokens(gasTokenSum);
        }
    }

    /** @notice Allow arbiters to settle a ring of order and reserve trades
      * @param  orderInput Array of OrderInputPacked structs
      * @param  tradeInput Array of RingTradeInputPacked structs
      */
    function settleRingTrade(OrderInputPacked[] calldata orderInput, RingTradeInputPacked[] calldata tradeInput) external {
        require(arbiters[msg.sender] && marketActive);      // Check if msg.sender is an arbiter and the market is active

        settlementModuleAddress.delegatecall(msg.data);

        // Loop through the trades and calc the gasToken sum
        uint256 i = tradeInput.length;        
        uint256 gasTokenSum;
        while(i-- != 0){
            gasTokenSum += uint8(tradeInput[i].packedInput2[24]);
        }

        // burn gas tokens
        if(gasTokenSum > 0){
            spendGasTokens(gasTokenSum);
        }
    }

    /** @notice Allow arbiters to settle a ring of order and reserve trades, passing on some data to the reserves
      * @param  orderInput Array of OrderInputPacked structs
      * @param  tradeInput Array of RingTradeInputPacked structs
      * @param  data       Array of data to pass along to the reserves
      */
    function settleRingTradeWithData(OrderInputPacked[] calldata orderInput, RingTradeInputPacked[] calldata tradeInput, bytes32[][] calldata data) external {
        require(arbiters[msg.sender] && marketActive);      // Check if msg.sender is an arbiter and the market is active

        settlementModuleAddress.delegatecall(msg.data);

        // Loop through the trades and calc the gasToken sum
        uint256 i = tradeInput.length;        
        uint256 gasTokenSum;
        while(i-- != 0){
            gasTokenSum += uint8(tradeInput[i].packedInput2[24]);
        }

        // burn gas tokens
        if(gasTokenSum > 0){
            spendGasTokens(gasTokenSum);
        }
    }


    /** @notice Helper function, callable only by the contract itself, to execute a trade between two reserves
      * @param  makerReserve  The maker reserve
      * @param  takerReserve  The taker reserve
      * @param  trade         The trade to settle between the two
      * @return Whether the trade succeeded or failed
      */
    function executeReserveReserveTrade(
        address             makerReserve,
        address payable     takerReserve,
        ReserveReserveTrade calldata trade
    ) external returns(bool){
        // this method is only callable from the contract itself
        // a call is used vs a jump, to be able to revert the sending of funds to the reserve without throwing the entire transaction
        require(msg.sender == address(this));                       // Check that the caller is the contract itself

        // Check whether the taker reserve accepts the trade
        require(
            dexBlueReserve(takerReserve).offer(                     
                trade.takerToken,                                   // The token we offer the reserve to sell
                trade.takerAmount,                                  // The amount the reserve could sell
                trade.makerToken,                                   // The token the reserve would receive
                trade.makerAmount - trade.takerFee                  // The amount the reserve would receive
            )
            && balances[trade.takerToken][takerReserve] >= trade.takerAmount    // Check whether the taker reserve deposited the collateral
        );

        balances[trade.takerToken][takerReserve] -= trade.takerAmount;          // Substract the deposited amount from the taker reserve

        if(trade.takerToken != address(0)){
            Token(trade.takerToken).transfer(makerReserve, trade.takerAmount - trade.makerFee);     // Send the taker reserves collateral to the maker reserve
            require(                                                                                // Revert if the send failed
                checkERC20TransferSuccess(),
                "ERC20 token transfer failed."
            );
        }

        // Check whether the maker reserve accepts the trade
        require(
            dexBlueReserve(makerReserve).trade.value(               // Execute the trade in the maker reserve
                trade.takerToken == address(0) ? 
                    trade.takerAmount - trade.makerFee              // Send the taker reserves collateral to the maker reserve
                    : 0
            )(
                trade.takerToken,                                   // The token the taker reserve is selling
                trade.takerAmount - trade.makerFee,                 // The amount of sellToken the taker reserve wants to sell
                trade.makerToken,                                   // The token the taker reserve wants in return
                trade.makerAmount                                   // The amount of token the taker reserve wants in return
            )
            && balances[trade.makerToken][makerReserve] >= trade.makerAmount  // Check whether the maker reserve deposited the collateral
        );

        balances[trade.makerToken][makerReserve] -= trade.makerAmount;                              // Substract the maker reserves's sold amount

        // Send the acquired amount to the taker reserve
        if(trade.makerToken == address(0)){                                                         // Is the acquired token ETH
            require(
                takerReserve.send(trade.makerAmount - trade.takerFee),                              // Send ETH
                "Sending of ETH failed."
            );
        }else{
            Token(trade.makerToken).transfer(takerReserve, trade.makerAmount - trade.takerFee);     // Transfer ERC20
            require(                                                                                // Revert if the transfer failed
                checkERC20TransferSuccess(),
                "ERC20 token transfer failed."
            );
        }

        // Notify the reserve, that the offer got executed
        dexBlueReserve(takerReserve).offerExecuted(                     
            trade.takerToken,                                   // The token the reserve sold
            trade.takerAmount,                                  // The amount the reserve sold
            trade.makerToken,                                   // The token the reserve received
            trade.makerAmount - trade.takerFee                  // The amount the reserve received
        );

        // Give fee to feeCollector
        balances[trade.makerToken][feeCollector] += trade.takerFee;  // Give feeColletor the taker fee
        balances[trade.takerToken][feeCollector] += trade.makerFee;  // Give feeColletor the maker fee

        emit LogTrade(trade.makerToken, trade.makerAmount, trade.takerToken, trade.takerAmount);

        emit LogDirectWithdrawal(makerReserve, trade.takerToken, trade.takerAmount - trade.makerFee);
        emit LogDirectWithdrawal(takerReserve, trade.makerToken, trade.makerAmount - trade.takerFee);

        return true;
    }

    /** @notice Helper function, callable only by the contract itself, to execute a trade between two reserves
      * @param  makerReserve  The maker reserve
      * @param  takerReserve  The taker reserve
      * @param  trade         The trade to settle between the two
      * @param  makerData     The data to pass on to the maker reserve
      * @param  takerData     The data to pass on to the taker reserve
      * @return Whether the trade succeeded or failed
      */
    function executeReserveReserveTradeWithData(
        address             makerReserve,
        address payable     takerReserve,
        ReserveReserveTrade calldata trade,
        bytes32[] calldata  makerData,
        bytes32[] calldata  takerData
    ) external returns(bool){
        // this method is only callable from the contract itself
        // a call is used vs a jump, to be able to revert the sending of funds to the reserve without throwing the entire transaction
        require(msg.sender == address(this));                       // Check that the caller is the contract itself

        // Check whether the taker reserve accepts the trade
        require(
            dexBlueReserve(takerReserve).offerWithData(                     
                trade.takerToken,                                   // The token we offer the reserve to sell
                trade.takerAmount,                                  // The amount the reserve could sell
                trade.makerToken,                                   // The token the reserve would receive
                trade.makerAmount - trade.takerFee,                 // The amount the reserve would receive
                takerData
            )
            && balances[trade.takerToken][takerReserve] >= trade.takerAmount    // Check whether the taker reserve deposited the collateral
        );

        balances[trade.takerToken][takerReserve] -= trade.takerAmount;          // Substract the deposited amount from the taker reserve

        if(trade.takerToken != address(0)){
            Token(trade.takerToken).transfer(makerReserve, trade.takerAmount - trade.makerFee);     // Send the taker reserves collateral to the maker reserve
            require(                                                                                // Revert if the send failed
                checkERC20TransferSuccess(),
                "ERC20 token transfer failed."
            );
        }

        // Check whether the maker reserve accepts the trade
        require(
            dexBlueReserve(makerReserve).tradeWithData.value(       // Execute the trade in the maker reserve
                trade.takerToken == address(0) ? 
                    trade.takerAmount - trade.makerFee              // Send the taker reserves collateral to the maker reserve
                    : 0
            )(
                trade.takerToken,                                   // The token the taker reserve is selling
                trade.takerAmount - trade.makerFee,                 // The amount of sellToken the taker reserve wants to sell
                trade.makerToken,                                   // The token the taker reserve wants in return
                trade.makerAmount,                                  // The amount of token the taker reserve wants in return
                makerData
            )
            && balances[trade.makerToken][makerReserve] >= trade.makerAmount  // Check whether the maker reserve deposited the collateral
        );

        balances[trade.makerToken][makerReserve] -= trade.makerAmount;                              // Substract the maker reserves's sold amount

        // Send the acquired amount to the taker reserve
        if(trade.makerToken == address(0)){                                                         // Is the acquired token ETH
            require(
                takerReserve.send(trade.makerAmount - trade.takerFee),                              // Send ETH
                "Sending of ETH failed."
            );
        }else{
            Token(trade.makerToken).transfer(takerReserve, trade.makerAmount - trade.takerFee);     // Transfer ERC20
            require(                                                                                // Revert if the transfer failed
                checkERC20TransferSuccess(),
                "ERC20 token transfer failed."
            );
        }

        // Notify the reserve, that the offer got executed
        dexBlueReserve(takerReserve).offerExecuted(                     
            trade.takerToken,                                   // The token the reserve sold
            trade.takerAmount,                                  // The amount the reserve sold
            trade.makerToken,                                   // The token the reserve received
            trade.makerAmount - trade.takerFee                  // The amount the reserve received
        );

        // Give fee to feeCollector
        balances[trade.makerToken][feeCollector] += trade.takerFee;  // Give feeColletor the taker fee
        balances[trade.takerToken][feeCollector] += trade.makerFee;  // Give feeColletor the maker fee

        emit LogTrade(trade.makerToken, trade.makerAmount, trade.takerToken, trade.takerAmount);

        emit LogDirectWithdrawal(makerReserve, trade.takerToken, trade.takerAmount - trade.makerFee);
        emit LogDirectWithdrawal(takerReserve, trade.makerToken, trade.makerAmount - trade.takerFee);

        return true;
    }

    /** @notice Helper function, callable only by the contract itself, to execute a trade with a reserve contract
      * @param  sellToken   The address of the token we want to sell (ETH is address(address(0)))
      * @param  sellAmount  The amount of sellToken we want to sell
      * @param  buyToken    The address of the token we want to buy (ETH is address(address(0)))
      * @param  buyAmount   The amount of buyToken we want in exchange for sellAmount
      * @param  reserve     The address of the reserve, we want to trade with
      */
    function executeReserveTrade(
        address    sellToken,
        uint256    sellAmount,
        address    buyToken,
        uint256    buyAmount,
        address    reserve
    ) external returns(bool){
        // this method is only callable from the contract itself
        // a call is used vs a jump, to be able to revert the sending of funds to the reserve without throwing the entire transaction
        require(msg.sender == address(this));                   // check that the caller is the contract itself

        if(sellToken == address(0)){
            require(dexBlueReserve(reserve).trade.value(        // execute the trade in the reserve
                                                                // if the reserve accepts the trade, it will deposit the buyAmount and return true
                sellAmount                                      // send collateral to reserve
            )(
                sellToken,                                      // the token we want to sell
                sellAmount,                                     // the amount of sellToken we want to exchange
                buyToken,                                       // the token we want to receive
                buyAmount                                       // the quantity of buyToken we demand in return
            ));
        }else{
            Token(sellToken).transfer(reserve, sellAmount);     // send collateral to reserve
            require(                                            // revert if the send failed
                checkERC20TransferSuccess(),
                "ERC20 token transfer failed."
            );

            require(dexBlueReserve(reserve).trade(              // execute the trade in the reserve
                sellToken,                                      // the token we want to sell
                sellAmount,                                     // the amount of sellToken we want to exchange
                buyToken,                                       // the token we want to receive
                buyAmount                                       // the quantity of buyToken we demand in return
            ));
        }

        require(balances[buyToken][reserve] >= buyAmount);      // check if the reserve delivered on the request, else revert

        return true;                                            // return true if all checks are passed and the trade was executed successfully
    }

    /** @notice private function to execute a trade with a reserve contract
      * @param  sellToken   The address of the token we want to sell (ETH is address(address(0)))
      * @param  sellAmount  The amount of sellToken we want to sell
      * @param  buyToken    The address of the token we want to buy (ETH is address(address(0)))
      * @param  buyAmount   The amount of buyToken we want in exchange for sellAmount
      * @param  reserve     The address of the reserve, we want to trade with
      * @param  data        The data passed on to the reserve
      */
    function executeReserveTradeWithData(
        address    sellToken,
        uint256    sellAmount,
        address    buyToken,
        uint256    buyAmount,
        address    reserve,
        bytes32[]  calldata data
    ) external returns(bool){
        // this method is only callable from the contract itself
        // a call is used vs a jump, to be able to revert the sending of funds to the reserve without throwing the entire transaction
        require(msg.sender == address(this));                   // check that the caller is the contract itself

        if(sellToken == address(0)){
            require(dexBlueReserve(reserve).tradeWithData.value(// execute the trade in the reserve
                                                                // if the reserve accepts the trade, it will deposit the buyAmount and return true
                sellAmount                                      // send collateral to reserve
            )(
                sellToken,                                      // the token we want to sell
                sellAmount,                                     // the amount of sellToken we want to exchange
                buyToken,                                       // the token we want to receive
                buyAmount,                                      // the quantity of buyToken we demand in return
                data                                            // the data passed on to the reserve
            ));
        }else{
            Token(sellToken).transfer(reserve, sellAmount);     // send collateral to reserve
            require(                                            // revert if the send failed
                checkERC20TransferSuccess(),
                "ERC20 token transfer failed."
            );
            require(dexBlueReserve(reserve).tradeWithData(      // execute the trade in the reserve
                sellToken,                                      // the token we want to sell
                sellAmount,                                     // the amount of sellToken we want to exchange
                buyToken,                                       // the token we want to receive
                buyAmount,                                      // the quantity of buyToken we demand in return
                data                                            // the data passed on to the reserve
            ));
        }

        require(balances[buyToken][reserve] >= buyAmount);      // check if the reserve delivered on the request, else revert

        return true;                                            // return true if all checks are passed and the trade was executed successfully
    }


    // Token swapping functionality

    /** @notice Queries best output for a trade currently available from the reserves
      * @param  sell_token   The token the user wants to sell (ETH is address(0))
      * @param  sell_amount  The amount of sell_token to sell
      * @param  buy_token    The token the user wants to acquire (ETH is address(0))
      * @return The output amount the reserve with the best price offers
    */
    function getSwapOutput(address sell_token, uint256 sell_amount, address buy_token) public view returns (uint256){
        (, uint256 output) = getBestReserve(sell_token, sell_amount, buy_token);
        return output;
    }

    /** @notice Queries the reserve address and output of trade, of the reserve which offers the best deal on a trade
      * @param  sell_token   The token the user wants to sell (ETH is address(0))
      * @param  sell_amount  The amount of sell_token to sell
      * @param  buy_token    The token the user wants to acquire (ETH is address(0))
      * @return The address of the reserve offering the best deal and the expected output of the trade
    */
    function getBestReserve(address sell_token, uint256 sell_amount, address buy_token) public view returns (address, uint256){
        address bestReserve;
        uint256 bestOutput = 0;
        uint256 output;

        for(uint256 i = 0; i < public_reserve_arr.length; i++){
            output = dexBlueReserve(public_reserve_arr[i]).getSwapOutput(sell_token, sell_amount, buy_token);
            if(output > bestOutput){
                bestOutput  = output;
                bestReserve = public_reserve_arr[i];
            }
        }

        return (bestReserve, bestOutput);
    }

    /** @notice Allows users to swap a token or ETH with the reserve offering the best price for his trade
      * @param  sell_token   The token the user wants to sell (ETH is address(0))
      * @param  sell_amount  The amount of sell_token to sell
      * @param  buy_token    The token the user wants to acquire (ETH is address(0))
      * @param  min_output   The minimum amount of buy_token, the trade should result in 
      * @param  deadline     The timestamp after which the transaction should not be executed
      * @return The amount of buy_token the user receives
    */
    function swap(address sell_token, uint256 sell_amount, address buy_token,  uint256 min_output, uint256 deadline) external payable returns(uint256){

        (bool success, bytes memory returnData) = settlementModuleAddress.delegatecall(msg.data);  // delegate the call to the settlement module

        require(success);

        return abi.decode(returnData, (uint256));
    }

    /** @notice Allows users to swap a token or ETH with a specified reserve
      * @param  sell_token   The token the user wants to sell (ETH is address(0))
      * @param  sell_amount  The amount of sell_token to sell
      * @param  buy_token    The token the user wants to acquire (ETH is address(0))
      * @param  min_output   The minimum amount of buy_token, the trade should result in 
      * @param  reserve      The address of the reserve to trade with
      * @param  deadline     The timestamp after which the transaction should not be executed
    */
    function swapWithReserve(address sell_token, uint256 sell_amount, address buy_token,  uint256 min_output, address reserve, uint256 deadline) public payable returns (uint256){

        (bool success, bytes memory returnData) = settlementModuleAddress.delegatecall(msg.data);  // delegate the call to the settlement module

        require(success);

        return abi.decode(returnData, (uint256));
    }


    // Order cancellation functions

    /** @notice Give the user the option to perform multiple on-chain cancellations of orders at once with arbiters multi-sig
      * @param  orderHashes Array of orderHashes of the orders to be canceled
      * @param  v           Multi-sig v
      * @param  r           Multi-sig r
      * @param  s           Multi-sig s
      */
    function multiSigOrderBatchCancel(bytes32[] calldata orderHashes, uint8 v, bytes32 r, bytes32 s) external {
        if(
            arbiters[                                               // Check if the signee is an arbiter
                ecrecover(                                          // Restore the signing address
                    keccak256(abi.encodePacked(                     // Restore the signed hash (hash of all orderHashes)
                        "\x19Ethereum Signed Message:\n32", 
                        keccak256(abi.encodePacked(orderHashes))
                    )),
                    v, r, s
                )
            ]
        ){
            uint256 len = orderHashes.length;
            for(uint256 i = 0; i < len; i++){
                matched[orderHashes[i]] = 2**256 - 1;               // Set the matched amount of all orders to the maximum
                emit LogOrderCanceled(orderHashes[i]);              // emit LogOrderCanceled event
            }
        }else{
            revert();
        }
    }


    // Gastoken functionality

    // This is heavily inspired and based on the work of the gastoken.io team @ initc3.org, kudos!
    // Why not use their implementation?
    // We can safe even more gas through: having a even shorter contract address (1 byte less), saving the call to their contract, their token logic, and other minor optimisations

    uint256 gas_token_nonce_head;
    uint256 gas_token_nonce_tail;

    /** @notice Get the available amount of gasTokens
      * @return The array of all indexed token addresses
      */
    function getAvailableGasTokens() view public returns (uint256 amount){
        return gas_token_nonce_head - gas_token_nonce_tail;
    }

    /** @notice Mint new gasTokens
      * @param  amount  The amount of gasTokens to mint
      */
    function mintGasTokens(uint amount) public {
        gas_token_nonce_head += amount;
        while(amount-- > 0){
            createChildContract();   
        }
    }

    /** @notice internal function to burn gasTokens
      * @param  amount  The amount of gasTokens to burn
      */
    function spendGasTokens(uint256 amount) internal {
        uint256 tail = gas_token_nonce_tail;

        if(amount <= gas_token_nonce_head - tail){

            // tail points to slot behind the last contract in the queue
            for (uint256 i = tail + 1; i <= tail + amount; i++) {
                restoreChildContractAddress(i).call("");
            }

            gas_token_nonce_tail = tail + amount;
        }
    }

    /** @notice internal helper function to create a child contract
      * @return The address of the created contract
      */
    function createChildContract() internal returns (address addr) {
        assembly {
            let solidity_free_mem_ptr := mload(0x40)
            mstore(solidity_free_mem_ptr, 0x746d541e251335090ac5b47176af4f7e3318585733ff6000526015600bf3) // Load contract bytecode into memory
            addr := create(0, add(solidity_free_mem_ptr, 2), 30)                                          // Create child contract
        }
    }

    /** @notice internal helper function to restore the address of a child contract for a given nonce
      * @param  nonce   The nonce of the child contract
      * @return The address of the child contract
      */
    function restoreChildContractAddress(uint256 nonce) view internal returns (address) {
        require(nonce <= 256**9 - 1);

        uint256 encoded;
        uint256 tot_bytes;

        if (nonce < 128) {
            // RLP(nonce) = nonce
            // add the encoded nonce to the encoded word
            encoded = nonce * 256**9;

            // [address_length(1) address(20) nonce_length(0) nonce(1)]
            tot_bytes = 22;
        } else {
            // RLP(nonce) = [num_bytes_in_nonce nonce]
            uint nonce_bytes = 1;
            // count nonce bytes
            uint mask = 256;
            while (nonce >= mask) {
                nonce_bytes += 1;
                mask        *= 256;
            }

            // add the encoded nonce to the word
            encoded = ((128 + nonce_bytes) * 256**9) +  // nonce length
                      (nonce * 256**(9 - nonce_bytes)); // nonce

            // [address_length(1) address(20) nonce_length(1) nonce(1-9)]
            tot_bytes = 22 + nonce_bytes;
        }

        // add the prefix and encoded address to the encoded word
        encoded += ((192 + tot_bytes) * 256**31) +     // total length
                   ((128 + 20) * 256**30) +            // address length
                   (uint256(address(this)) * 256**10); // address(this)

        uint256 hash;

        assembly {
            let mem_start := mload(0x40)        // get a pointer to free memory
            mstore(0x40, add(mem_start, 0x20))  // update the pointer

            mstore(mem_start, encoded)          // store the rlp encoding
            hash := keccak256(mem_start,
                         add(tot_bytes, 1))     // hash the rlp encoding
        }

        // interpret hash as address (20 least significant bytes)
        return address(hash);
    }


    // Signature delegation

    /** @notice delegate an address to allow it to sign orders on your behalf
      * @param delegate  The address to delegate
      */
    function delegateAddress(address delegate) external {
        // set as delegate
        require(delegates[delegate] == address(0), "Address is already a delegate");
        delegates[delegate] = msg.sender;

        emit LogDelegateStatus(msg.sender, delegate, true);
    }

    /** @notice revoke the delegation of an address
      * @param  delegate  The delegated address
      * @param  v         Multi-sig v
      * @param  r         Multi-sig r
      * @param  s         Multi-sig s
      */
    function revokeDelegation(address delegate, uint8 v, bytes32 r, bytes32 s) external {
        bytes32 hash = keccak256(abi.encodePacked(              // Restore the signed hash
            "\x19Ethereum Signed Message:\n32", 
            keccak256(abi.encodePacked(
                delegate,
                msg.sender,
                address(this)
            ))
        ));

        require(
            arbiters[ecrecover(hash, v, r, s)],     // Check if signee is an arbiter
            "MultiSig is not from known arbiter"
        );

        delegates[delegate] = address(1);           // Set to 1 not 0 to prevent double delegation, which would make old signed orders valid for the new delegator

        emit LogDelegateStatus(msg.sender, delegate, false);
    }


    // Management functions:

    /** @notice Constructor function. Sets initial roles and creates EIP712 Domain.
      */
    constructor() public {
        owner = msg.sender;             // Nominate sender to be the contract owner

        // create EIP712 domain seperator
        EIP712_Domain memory eip712Domain = EIP712_Domain({
            name              : "dex.blue",
            version           : "1",
            chainId           : 1,
            verifyingContract : address(this)
        });
        EIP712_DOMAIN_SEPARATOR = keccak256(abi.encode(
            EIP712_DOMAIN_TYPEHASH,
            keccak256(bytes(eip712Domain.name)),
            keccak256(bytes(eip712Domain.version)),
            eip712Domain.chainId,
            eip712Domain.verifyingContract
        ));
    }

    /** @notice Allows the owner to change / disable the waiting period for a single sig withdrawal
      * @param  waiting_period The new waiting period
      */
    function changeSingleSigWaitingPeriod(uint256 waiting_period) external {
        require(
            msg.sender == owner             // only owner can set waiting period
            && waiting_period <= 86400      // max period owner can set is one day
        );

        single_sig_waiting_period = waiting_period;
    }

    /** @notice Allows the owner to handle over the ownership to another address
      * @param  new_owner The new owner address
      */
    function changeOwner(address payable new_owner) external {
        require(msg.sender == owner);
        owner = new_owner;
    }

    /** @notice Allows the owner to register & cache a new reserve address in the smart conract
      * @param  reserve   The address of the reserve to add
      * @param  index     The index under which the reserve should be indexed
      * @param  is_public Whether the reserve should publicly available through swap() & swapWithReserve()
      */
    function cacheReserveAddress(address payable reserve, uint256 index, bool is_public) external {
        require(arbiters[msg.sender]);

        reserves[index] = reserve;
        reserve_indices[reserve] = index;

        if(is_public){
            public_reserves[reserve] = true;
            public_reserve_arr.push(reserve);  // append the reserve to the reserve array
        }
    }

    /** @notice Allows the owner to remove a reserve from the array swap() and getSwapOutput() need to loop through
      * @param  reserve The address of the reserve to remove
      */
    function removePublicReserveAddress(address reserve) external {
        require(arbiters[msg.sender]);

        public_reserves[reserve] = false;

        for(uint256 i = 0; i < public_reserve_arr.length; i++){
            if(public_reserve_arr[i] == reserve){
                public_reserve_arr[i] = public_reserve_arr[public_reserve_arr.length - 1]; // array order does not matter, so we just move the last element in the slot of the element we are removing

                delete public_reserve_arr[public_reserve_arr.length-1];                    // delete the last element of the array
                public_reserve_arr.length--;                             

                return;
            }
        }
    }

    /** @notice Allows an arbiterto cache a new token address
      * @param  token   The address of the token to add
      * @param  index   The index under which the token should be indexed
      */
    function cacheTokenAddress(address token, uint256 index) external {
        require(arbiters[msg.sender]);

        tokens[index]        = token;
        token_indices[token] = index;

        token_arr.push(token);  // append the token to the array
    }

    /** @notice Allows arbiters to remove a token from the token array
      * @param  token The address of the token to remove
      */
    function removeTokenAddressFromArr(address token) external {
        require(arbiters[msg.sender]);

        for(uint256 i = 0; i < token_arr.length; i++){
            if(token_arr[i] == token){
                token_arr[i] = token_arr[token_arr.length - 1]; // array order does not matter, so we just move the last element in the slot of the element we are removing

                delete token_arr[token_arr.length-1];           // delete the last element of the array
                token_arr.length--;                             

                return;
            }
        }
    }

    /** @notice Allows the owner to nominate or denominate trade arbiting addresses
      * @param  arbiter The arbiter whose status to change
      * @param  status  whether the address should be an arbiter (true) or not (false)
      */
    function nominateArbiter(address arbiter, bool status) external {
        require(msg.sender == owner);                           // Check if sender is owner
        arbiters[arbiter] = status;                             // Update address status
    }

    /** @notice Allows the owner to pause / unpause the market
      * @param  state  whether the the market should be active (true) or paused (false)
      */
    function setMarketActiveState(bool state) external {
        require(msg.sender == owner);                           // Check if sender is owner
        marketActive = state;                                   // pause / unpause market
    }

    /** @notice Allows the owner to nominate the feeCollector address
      * @param  collector The address to nominate as feeCollector
      */
    function nominateFeeCollector(address payable collector) external {
        require(msg.sender == owner && !feeCollectorLocked);    // Check if sender is owner and feeCollector address is not locked
        feeCollector = collector;                               // Update feeCollector address
    }

    /** @notice Allows the owner to lock the feeCollector address
    */
    function lockFeeCollector() external {
        require(msg.sender == owner);                           // Check if sender is owner
        feeCollectorLocked = true;                              // Lock feeCollector address
    }

    /** @notice Get the feeCollectors address
      * @return The feeCollectors address
      */
    function getFeeCollector() public view returns (address){
        return feeCollector;
    }

    /** @notice Allows an arbiter or feeCollector to directly withdraw his own funds (would allow e.g. a fee distribution contract the withdrawal of collected fees)
      * @param  token   The token to withdraw
      * @param  amount  The amount of tokens to withdraw
    */
    function directWithdrawal(address token, uint256 amount) external returns(bool){
        if (
            (
                msg.sender == feeCollector                        // Check if the sender is the feeCollector
                || arbiters[msg.sender]                           // Check if the sender is an arbiter
            )
            && balances[token][msg.sender] >= amount              // Check if feeCollector has the sufficient balance
        ){
            balances[token][msg.sender] -= amount;                // Substract the feeCollectors balance

            if(token == address(0)){                              // Is the withdrawal token ETH
                require(
                    msg.sender.send(amount),                      // Withdraw ETH
                    "Sending of ETH failed."
                );
            }else{
                Token(token).transfer(msg.sender, amount);        // Withdraw ERC20
                require(                                          // Revert if the withdrawal failed
                    checkERC20TransferSuccess(),
                    "ERC20 token transfer failed."
                );
            }

            emit LogDirectWithdrawal(msg.sender, token, amount);     // emit LogDirectWithdrawal event
            return true;
        }else{
            return false;
        }
    }
}

// dexBlueReserve
contract dexBlueReserve{
    // insured trade function with fixed outcome
    function trade(address sell_token, uint256 sell_amount, address buy_token,  uint256 buy_amount) public payable returns(bool success){}

    // insured trade function with fixed outcome, passes additional data to the reserve
    function tradeWithData(address sell_token, uint256 sell_amount, address buy_token,  uint256 buy_amount, bytes32[] memory data) public payable returns(bool success){}

    // offer the reserve to enter a trade a a taker
    function offer(address sell_token, uint256 sell_amount, address buy_token,  uint256 buy_amount) public returns(bool accept){}

    // offer the reserve to enter a trade a a taker, passes additional data to the reserve
    function offerWithData(address sell_token, uint256 sell_amount, address buy_token,  uint256 buy_amount, bytes32[] memory data) public returns(bool accept){}

    // callback function, to inform the reserve that an offer has been accepted by the maker reserve
    function offerExecuted(address sell_token, uint256 sell_amount, address buy_token,  uint256 buy_amount) public{}

    // uninsured swap
    function swap(address sell_token, uint256 sell_amount, address buy_token,  uint256 min_output) public payable returns(uint256 output){}

    // get output amount of swap
    function getSwapOutput(address sell_token, uint256 sell_amount, address buy_token) public view returns(uint256 output){}
}

// Standart ERC20 token interface to interact with ERC20 token contracts
// To support badly implemented tokens (which dont return a boolean on the transfer functions)
// we have to expect a badly implemented token and then check with checkERC20TransferSuccess() whether the transfer succeeded

contract Token {
    /** @return total amount of tokens
      */
    function totalSupply() view public returns (uint256 supply) {}

    /** @param _owner The address from which the balance will be retrieved
      * @return The balance
      */
    function balanceOf(address _owner) view public returns (uint256 balance) {}

    /** @notice send `_value` token to `_to` from `msg.sender`
      * @param  _to     The address of the recipient
      * @param  _value  The amount of tokens to be transferred
      * @return whether the transfer was successful or not
      */
    function transfer(address _to, uint256 _value) public {}

    /** @notice send `_value` token to `_to` from `_from` on the condition it is approved by `_from`
      * @param  _from   The address of the sender
      * @param  _to     The address of the recipient
      * @param  _value  The amount of tokens to be transferred
      * @return whether the transfer was successful or not
      */
    function transferFrom(address _from, address _to, uint256 _value)  public {}

    /** @notice `msg.sender` approves `_addr` to spend `_value` tokens
      * @param  _spender The address of the account able to transfer the tokens
      * @param  _value   The amount of wei to be approved for transfer
      * @return whether the approval was successful or not
      */
    function approve(address _spender, uint256 _value) public returns (bool success) {}

    /** @param  _owner   The address of the account owning tokens
      * @param  _spender The address of the account able to transfer the tokens
      * @return Amount of remaining tokens allowed to spend
      */
    function allowance(address _owner, address _spender) view public returns (uint256 remaining) {}

    event Transfer(address indexed _from, address indexed _to, uint256 _value);
    event Approval(address indexed _owner, address indexed _spender, uint256 _value);

    uint256 public decimals;
    string public name;
}

// Wrapped Ether interface
contract WETH is Token{
    function deposit() public payable {}
    function withdraw(uint256 amount) public {}
}