ETH Price: $3,116.52 (+0.61%)
Gas: 4 Gwei

Transaction Decoder

Block:
8566467 at Sep-17-2019 10:40:32 AM +UTC
Transaction Fee:
0.001692075 ETH $5.27
Gas Used:
51,275 Gas / 33 Gwei

Emitted Events:

49 DaiPriceOracle.UpdatePrice( newPrice=997892007467167827 )

Account State Difference:

  Address   Before After State Difference Code
0xD7518704...1cCBc16ea
4.9418306007 Eth
Nonce: 53
4.9401385257 Eth
Nonce: 54
0.001692075
(Ethermine)
452.53904198469690498 Eth452.54073405969690498 Eth0.001692075
0xeB1f1A28...36E09EAA8

Execution Trace

DaiPriceOracle.CALL( )
  • Medianizer.STATICCALL( )
  • MatchingMarket.STATICCALL( )
  • MatchingMarket.STATICCALL( )
  • MatchingMarket.STATICCALL( )
  • MatchingMarket.getBuyAmount( buy_gem=0x89d24A6b4CcB1B6fAA2625fE562bDD9a23260359, pay_gem=0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2, pay_amt=10000000000000000000 ) => ( fill_amt=1989999999999999999988 )
  • MatchingMarket.getPayAmount( pay_gem=0x89d24A6b4CcB1B6fAA2625fE562bDD9a23260359, buy_gem=0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2, buy_amt=10000000000000000000 ) => ( fill_amt=1990090000000000000000 )
    File 1 of 3: DaiPriceOracle
    /*
    
        Copyright 2019 The Hydro Protocol Foundation
    
        Licensed under the Apache License, Version 2.0 (the "License");
        you may not use this file except in compliance with the License.
        You may obtain a copy of the License at
    
            http://www.apache.org/licenses/LICENSE-2.0
    
        Unless required by applicable law or agreed to in writing, software
        distributed under the License is distributed on an "AS IS" BASIS,
        WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
        See the License for the specific language governing permissions and
        limitations under the License.
    
    */
    
    pragma solidity 0.5.8;
    pragma experimental ABIEncoderV2;
    
    interface IEth2Dai{
        function isClosed()
            external
            view
            returns (bool);
    
        function buyEnabled()
            external
            view
            returns (bool);
    
        function matchingEnabled()
            external
            view
            returns (bool);
    
        function getBuyAmount(
            address buy_gem,
            address pay_gem,
            uint256 pay_amt
        )
            external
            view
            returns (uint256);
    
        function getPayAmount(
            address pay_gem,
            address buy_gem,
            uint256 buy_amt
        )
            external
            view
            returns (uint256);
    }
    
    interface IMakerDaoOracle{
        function peek()
            external
            view
            returns (bytes32, bool);
    }
    
    interface IStandardToken {
        function transfer(
            address _to,
            uint256 _amount
        )
            external
            returns (bool);
    
        function balanceOf(
            address _owner)
            external
            view
            returns (uint256 balance);
    
        function transferFrom(
            address _from,
            address _to,
            uint256 _amount
        )
            external
            returns (bool);
    
        function approve(
            address _spender,
            uint256 _amount
        )
            external
            returns (bool);
    
        function allowance(
            address _owner,
            address _spender
        )
            external
            view
            returns (uint256);
    }
    
    contract Ownable {
        address private _owner;
    
        event OwnershipTransferred(
            address indexed previousOwner,
            address indexed newOwner
        );
    
        /** @dev The Ownable constructor sets the original `owner` of the contract to the sender account. */
        constructor()
            internal
        {
            _owner = msg.sender;
            emit OwnershipTransferred(address(0), _owner);
        }
    
        /** @return the address of the owner. */
        function owner()
            public
            view
            returns(address)
        {
            return _owner;
        }
    
        /** @dev Throws if called by any account other than the owner. */
        modifier onlyOwner() {
            require(isOwner(), "NOT_OWNER");
            _;
        }
    
        /** @return true if `msg.sender` is the owner of the contract. */
        function isOwner()
            public
            view
            returns(bool)
        {
            return msg.sender == _owner;
        }
    
        /** @dev Allows the current owner to relinquish control of the contract.
         * @notice Renouncing to ownership will leave the contract without an owner.
         * It will not be possible to call the functions with the `onlyOwner`
         * modifier anymore.
         */
        function renounceOwnership()
            public
            onlyOwner
        {
            emit OwnershipTransferred(_owner, address(0));
            _owner = address(0);
        }
    
        /** @dev Allows the current owner to transfer control of the contract to a newOwner.
         * @param newOwner The address to transfer ownership to.
         */
        function transferOwnership(
            address newOwner
        )
            public
            onlyOwner
        {
            require(newOwner != address(0), "INVALID_OWNER");
            emit OwnershipTransferred(_owner, newOwner);
            _owner = newOwner;
        }
    }
    
    library SafeMath {
    
        // Multiplies two numbers, reverts on overflow.
        function mul(
            uint256 a,
            uint256 b
        )
            internal
            pure
            returns (uint256)
        {
            if (a == 0) {
                return 0;
            }
    
            uint256 c = a * b;
            require(c / a == b, "MUL_ERROR");
    
            return c;
        }
    
        // Integer division of two numbers truncating the quotient, reverts on division by zero.
        function div(
            uint256 a,
            uint256 b
        )
            internal
            pure
            returns (uint256)
        {
            require(b > 0, "DIVIDING_ERROR");
            return a / b;
        }
    
        function divCeil(
            uint256 a,
            uint256 b
        )
            internal
            pure
            returns (uint256)
        {
            uint256 quotient = div(a, b);
            uint256 remainder = a - quotient * b;
            if (remainder > 0) {
                return quotient + 1;
            } else {
                return quotient;
            }
        }
    
        // Subtracts two numbers, reverts on overflow (i.e. if subtrahend is greater than minuend).
        function sub(
            uint256 a,
            uint256 b
        )
            internal
            pure
            returns (uint256)
        {
            require(b <= a, "SUB_ERROR");
            return a - b;
        }
    
        function sub(
            int256 a,
            uint256 b
        )
            internal
            pure
            returns (int256)
        {
            require(b <= 2**255-1, "INT256_SUB_ERROR");
            int256 c = a - int256(b);
            require(c <= a, "INT256_SUB_ERROR");
            return c;
        }
    
        // Adds two numbers, reverts on overflow.
        function add(
            uint256 a,
            uint256 b
        )
            internal
            pure
            returns (uint256)
        {
            uint256 c = a + b;
            require(c >= a, "ADD_ERROR");
            return c;
        }
    
        function add(
            int256 a,
            uint256 b
        )
            internal
            pure
            returns (int256)
        {
            require(b <= 2**255 - 1, "INT256_ADD_ERROR");
            int256 c = a + int256(b);
            require(c >= a, "INT256_ADD_ERROR");
            return c;
        }
    
        // Divides two numbers and returns the remainder (unsigned integer modulo), reverts when dividing by zero.
        function mod(
            uint256 a,
            uint256 b
        )
            internal
            pure
            returns (uint256)
        {
            require(b != 0, "MOD_ERROR");
            return a % b;
        }
    
        /**
         * Check the amount of precision lost by calculating multiple * (numerator / denominator). To
         * do this, we check the remainder and make sure it's proportionally less than 0.1%. So we have:
         *
         *     ((numerator * multiple) % denominator)     1
         *     -------------------------------------- < ----
         *              numerator * multiple            1000
         *
         * To avoid further division, we can move the denominators to the other sides and we get:
         *
         *     ((numerator * multiple) % denominator) * 1000 < numerator * multiple
         *
         * Since we want to return true if there IS a rounding error, we simply flip the sign and our
         * final equation becomes:
         *
         *     ((numerator * multiple) % denominator) * 1000 >= numerator * multiple
         *
         * @param numerator The numerator of the proportion
         * @param denominator The denominator of the proportion
         * @param multiple The amount we want a proportion of
         * @return Boolean indicating if there is a rounding error when calculating the proportion
         */
        function isRoundingError(
            uint256 numerator,
            uint256 denominator,
            uint256 multiple
        )
            internal
            pure
            returns (bool)
        {
            // numerator.mul(multiple).mod(denominator).mul(1000) >= numerator.mul(multiple)
            return mul(mod(mul(numerator, multiple), denominator), 1000) >= mul(numerator, multiple);
        }
    
        /**
         * Takes an amount (multiple) and calculates a proportion of it given a numerator/denominator
         * pair of values. The final value will be rounded down to the nearest integer value.
         *
         * This function will revert the transaction if rounding the final value down would lose more
         * than 0.1% precision.
         *
         * @param numerator The numerator of the proportion
         * @param denominator The denominator of the proportion
         * @param multiple The amount we want a proportion of
         * @return The final proportion of multiple rounded down
         */
        function getPartialAmountFloor(
            uint256 numerator,
            uint256 denominator,
            uint256 multiple
        )
            internal
            pure
            returns (uint256)
        {
            require(!isRoundingError(numerator, denominator, multiple), "ROUNDING_ERROR");
            // numerator.mul(multiple).div(denominator)
            return div(mul(numerator, multiple), denominator);
        }
    
        /**
         * Returns the smaller integer of the two passed in.
         *
         * @param a Unsigned integer
         * @param b Unsigned integer
         * @return The smaller of the two integers
         */
        function min(
            uint256 a,
            uint256 b
        )
            internal
            pure
            returns (uint256)
        {
            return a < b ? a : b;
        }
    }
    
    contract DaiPriceOracle is Ownable{
        using SafeMath for uint256;
    
        uint256 public price;
    
        uint256 constant ONE = 10**18;
    
        IMakerDaoOracle public constant makerDaoOracle = IMakerDaoOracle(0x729D19f657BD0614b4985Cf1D82531c67569197B);
        IStandardToken public constant DAI = IStandardToken(0x89d24A6b4CcB1B6fAA2625fE562bDD9a23260359);
        IEth2Dai public constant Eth2Dai = IEth2Dai(0x39755357759cE0d7f32dC8dC45414CCa409AE24e);
    
        address public constant UNISWAP = 0x09cabEC1eAd1c0Ba254B09efb3EE13841712bE14;
        address public constant WETH = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
        uint256 public constant eth2daiETHAmount = 10 ether;
        uint256 public constant eth2daiMaxSpread = 2 * ONE / 100; // 2.00%
        uint256 public constant uniswapMinETHAmount = 2000 ether;
    
        event UpdatePrice(uint256 newPrice);
    
        function getPrice(
            address asset
        )
            external
            view
            returns (uint256)
        {
            require(asset == address(DAI), "ASSET_NOT_MATCH");
            return price;
        }
    
        function adminSetPrice(
            uint256 _price
        )
            external
            onlyOwner
        {
            if (!updatePrice()){
                price = _price;
            }
    
            emit UpdatePrice(price);
        }
    
        function updatePrice()
            public
            returns (bool)
        {
            uint256 _price = peek();
    
            if (_price != 0) {
                price = _price;
                emit UpdatePrice(price);
                return true;
            } else {
                return false;
            }
        }
    
        function peek()
            public
            view
            returns (uint256 _price)
        {
            uint256 makerDaoPrice = getMakerDaoPrice();
    
            if (makerDaoPrice == 0) {
                return _price;
            }
    
            uint256 eth2daiPrice = getEth2DaiPrice();
    
            if (eth2daiPrice > 0) {
                _price = makerDaoPrice.mul(ONE).div(eth2daiPrice);
                return _price;
            }
    
            uint256 uniswapPrice = getUniswapPrice();
    
            if (uniswapPrice > 0) {
                _price = makerDaoPrice.mul(ONE).div(uniswapPrice);
                return _price;
            }
    
            return _price;
        }
    
        function getEth2DaiPrice()
            public
            view
            returns (uint256)
        {
            if (Eth2Dai.isClosed() || !Eth2Dai.buyEnabled() || !Eth2Dai.matchingEnabled()) {
                return 0;
            }
    
            uint256 bidDai = Eth2Dai.getBuyAmount(address(DAI), WETH, eth2daiETHAmount);
            uint256 askDai = Eth2Dai.getPayAmount(address(DAI), WETH, eth2daiETHAmount);
    
            uint256 bidPrice = bidDai.mul(ONE).div(eth2daiETHAmount);
            uint256 askPrice = askDai.mul(ONE).div(eth2daiETHAmount);
    
            uint256 spread = askPrice.mul(ONE).div(bidPrice).sub(ONE);
    
            if (spread > eth2daiMaxSpread) {
                return 0;
            } else {
                return bidPrice.add(askPrice).div(2);
            }
        }
    
        function getUniswapPrice()
            public
            view
            returns (uint256)
        {
            uint256 ethAmount = UNISWAP.balance;
            uint256 daiAmount = DAI.balanceOf(UNISWAP);
            uint256 uniswapPrice = daiAmount.mul(10**18).div(ethAmount);
    
            if (ethAmount < uniswapMinETHAmount) {
                return 0;
            } else {
                return uniswapPrice;
            }
        }
    
        function getMakerDaoPrice()
            public
            view
            returns (uint256)
        {
            (bytes32 value, bool has) = makerDaoOracle.peek();
    
            if (has) {
                return uint256(value);
            } else {
                return 0;
            }
        }
    }

    File 2 of 3: Medianizer
    /// return median value of feeds
    
    // Copyright (C) 2017  DappHub, LLC
    
    // Licensed under the Apache License, Version 2.0 (the "License").
    // You may not use this file except in compliance with the License.
    
    // Unless required by applicable law or agreed to in writing, software
    // distributed under the License is distributed on an "AS IS" BASIS,
    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND (express or implied).
    
    pragma solidity ^0.4.8;
    
    contract DSAuthority {
        function canCall(
            address src, address dst, bytes4 sig
        ) constant returns (bool);
    }
    
    contract DSAuthEvents {
        event LogSetAuthority (address indexed authority);
        event LogSetOwner     (address indexed owner);
    }
    
    contract DSAuth is DSAuthEvents {
        DSAuthority  public  authority;
        address      public  owner;
    
        function DSAuth() {
            owner = msg.sender;
            LogSetOwner(msg.sender);
        }
    
        function setOwner(address owner_)
            auth
        {
            owner = owner_;
            LogSetOwner(owner);
        }
    
        function setAuthority(DSAuthority authority_)
            auth
        {
            authority = authority_;
            LogSetAuthority(authority);
        }
    
        modifier auth {
            assert(isAuthorized(msg.sender, msg.sig));
            _;
        }
    
        modifier authorized(bytes4 sig) {
            assert(isAuthorized(msg.sender, sig));
            _;
        }
    
        function isAuthorized(address src, bytes4 sig) internal returns (bool) {
            if (src == address(this)) {
                return true;
            } else if (src == owner) {
                return true;
            } else if (authority == DSAuthority(0)) {
                return false;
            } else {
                return authority.canCall(src, this, sig);
            }
        }
    
        function assert(bool x) internal {
            if (!x) throw;
        }
    }
    
    contract DSNote {
        event LogNote(
            bytes4   indexed  sig,
            address  indexed  guy,
            bytes32  indexed  foo,
            bytes32  indexed  bar,
    	uint	 	  wad,
            bytes             fax
        ) anonymous;
    
        modifier note {
            bytes32 foo;
            bytes32 bar;
    
            assembly {
                foo := calldataload(4)
                bar := calldataload(36)
            }
    
            LogNote(msg.sig, msg.sender, foo, bar, msg.value, msg.data);
    
            _;
        }
    }
    
    contract DSMath {
        
        /*
        standard uint256 functions
         */
    
        function add(uint256 x, uint256 y) constant internal returns (uint256 z) {
            assert((z = x + y) >= x);
        }
    
        function sub(uint256 x, uint256 y) constant internal returns (uint256 z) {
            assert((z = x - y) <= x);
        }
    
        function mul(uint256 x, uint256 y) constant internal returns (uint256 z) {
            assert((z = x * y) >= x);
        }
    
        function div(uint256 x, uint256 y) constant internal returns (uint256 z) {
            z = x / y;
        }
    
        function min(uint256 x, uint256 y) constant internal returns (uint256 z) {
            return x <= y ? x : y;
        }
        function max(uint256 x, uint256 y) constant internal returns (uint256 z) {
            return x >= y ? x : y;
        }
    
        /*
        uint128 functions (h is for half)
         */
    
    
        function hadd(uint128 x, uint128 y) constant internal returns (uint128 z) {
            assert((z = x + y) >= x);
        }
    
        function hsub(uint128 x, uint128 y) constant internal returns (uint128 z) {
            assert((z = x - y) <= x);
        }
    
        function hmul(uint128 x, uint128 y) constant internal returns (uint128 z) {
            assert((z = x * y) >= x);
        }
    
        function hdiv(uint128 x, uint128 y) constant internal returns (uint128 z) {
            z = x / y;
        }
    
        function hmin(uint128 x, uint128 y) constant internal returns (uint128 z) {
            return x <= y ? x : y;
        }
        function hmax(uint128 x, uint128 y) constant internal returns (uint128 z) {
            return x >= y ? x : y;
        }
    
    
        /*
        int256 functions
         */
    
        function imin(int256 x, int256 y) constant internal returns (int256 z) {
            return x <= y ? x : y;
        }
        function imax(int256 x, int256 y) constant internal returns (int256 z) {
            return x >= y ? x : y;
        }
    
        /*
        WAD math
         */
    
        uint128 constant WAD = 10 ** 18;
    
        function wadd(uint128 x, uint128 y) constant internal returns (uint128) {
            return hadd(x, y);
        }
    
        function wsub(uint128 x, uint128 y) constant internal returns (uint128) {
            return hsub(x, y);
        }
    
        function wmul(uint128 x, uint128 y) constant internal returns (uint128 z) {
            z = cast((uint256(x) * y + WAD / 2) / WAD);
        }
    
        function wdiv(uint128 x, uint128 y) constant internal returns (uint128 z) {
            z = cast((uint256(x) * WAD + y / 2) / y);
        }
    
        function wmin(uint128 x, uint128 y) constant internal returns (uint128) {
            return hmin(x, y);
        }
        function wmax(uint128 x, uint128 y) constant internal returns (uint128) {
            return hmax(x, y);
        }
    
        /*
        RAY math
         */
    
        uint128 constant RAY = 10 ** 27;
    
        function radd(uint128 x, uint128 y) constant internal returns (uint128) {
            return hadd(x, y);
        }
    
        function rsub(uint128 x, uint128 y) constant internal returns (uint128) {
            return hsub(x, y);
        }
    
        function rmul(uint128 x, uint128 y) constant internal returns (uint128 z) {
            z = cast((uint256(x) * y + RAY / 2) / RAY);
        }
    
        function rdiv(uint128 x, uint128 y) constant internal returns (uint128 z) {
            z = cast((uint256(x) * RAY + y / 2) / y);
        }
    
        function rpow(uint128 x, uint64 n) constant internal returns (uint128 z) {
            // This famous algorithm is called "exponentiation by squaring"
            // and calculates x^n with x as fixed-point and n as regular unsigned.
            //
            // It's O(log n), instead of O(n) for naive repeated multiplication.
            //
            // These facts are why it works:
            //
            //  If n is even, then x^n = (x^2)^(n/2).
            //  If n is odd,  then x^n = x * x^(n-1),
            //   and applying the equation for even x gives
            //    x^n = x * (x^2)^((n-1) / 2).
            //
            //  Also, EVM division is flooring and
            //    floor[(n-1) / 2] = floor[n / 2].
    
            z = n % 2 != 0 ? x : RAY;
    
            for (n /= 2; n != 0; n /= 2) {
                x = rmul(x, x);
    
                if (n % 2 != 0) {
                    z = rmul(z, x);
                }
            }
        }
    
        function rmin(uint128 x, uint128 y) constant internal returns (uint128) {
            return hmin(x, y);
        }
        function rmax(uint128 x, uint128 y) constant internal returns (uint128) {
            return hmax(x, y);
        }
    
        function cast(uint256 x) constant internal returns (uint128 z) {
            assert((z = uint128(x)) == x);
        }
    
    }
    
    contract DSThing is DSAuth, DSNote, DSMath {
    }
    
    contract DSValue is DSThing {
        bool    has;
        bytes32 val;
        function peek() constant returns (bytes32, bool) {
            return (val,has);
        }
        function read() constant returns (bytes32) {
            var (wut, has) = peek();
            assert(has);
            return wut;
        }
        function poke(bytes32 wut) note auth {
            val = wut;
            has = true;
        }
        function void() note auth { // unset the value
            has = false;
        }
    }
    
    contract Medianizer is DSValue {
        mapping (bytes12 => address) public values;
        mapping (address => bytes12) public indexes;
        bytes12 public next = 0x1;
    
        uint96 public min = 0x1;
    
        function set(address wat) auth {
            bytes12 nextId = bytes12(uint96(next) + 1);
            assert(nextId != 0x0);
            set(next, wat);
            next = nextId;
        }
    
        function set(bytes12 pos, address wat) note auth {
            if (pos == 0x0) throw;
    
            if (wat != 0 && indexes[wat] != 0) throw;
    
            indexes[values[pos]] = 0; // Making sure to remove a possible existing address in that position
    
            if (wat != 0) {
                indexes[wat] = pos;
            }
    
            values[pos] = wat;
        }
    
        function setMin(uint96 min_) note auth {
            if (min_ == 0x0) throw;
            min = min_;
        }
    
        function setNext(bytes12 next_) note auth {
            if (next_ == 0x0) throw;
            next = next_;
        }
    
        function unset(bytes12 pos) {
            set(pos, 0);
        }
    
        function unset(address wat) {
            set(indexes[wat], 0);
        }
    
        function poke() {
            poke(0);
        }
    
        function poke(bytes32) note {
            (val, has) = compute();
        }
    
        function compute() constant returns (bytes32, bool) {
            bytes32[] memory wuts = new bytes32[](uint96(next) - 1);
            uint96 ctr = 0;
            for (uint96 i = 1; i < uint96(next); i++) {
                if (values[bytes12(i)] != 0) {
                    var (wut, wuz) = DSValue(values[bytes12(i)]).peek();
                    if (wuz) {
                        if (ctr == 0 || wut >= wuts[ctr - 1]) {
                            wuts[ctr] = wut;
                        } else {
                            uint96 j = 0;
                            while (wut >= wuts[j]) {
                                j++;
                            }
                            for (uint96 k = ctr; k > j; k--) {
                                wuts[k] = wuts[k - 1];
                            }
                            wuts[j] = wut;
                        }
                        ctr++;
                    }
                }
            }
    
            if (ctr < min) return (val, false);
    
            bytes32 value;
            if (ctr % 2 == 0) {
                uint128 val1 = uint128(wuts[(ctr / 2) - 1]);
                uint128 val2 = uint128(wuts[ctr / 2]);
                value = bytes32(wdiv(hadd(val1, val2), 2 ether));
            } else {
                value = wuts[(ctr - 1) / 2];
            }
    
            return (value, true);
        }
    
    }

    File 3 of 3: MatchingMarket
    /// matching_market.sol
    
    //
    // This program is free software: you can redistribute it and/or modify
    // it under the terms of the GNU Affero General Public License as published by
    // the Free Software Foundation, either version 3 of the License, or
    // (at your option) any later version.
    //
    // This program is distributed in the hope that it will be useful,
    // but WITHOUT ANY WARRANTY; without even the implied warranty of
    // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    // GNU Affero General Public License for more details.
    //
    // You should have received a copy of the GNU Affero General Public License
    // along with this program.  If not, see <https://www.gnu.org/licenses/>.
    
    pragma solidity ^0.4.18;
    
    /// expiring_market.sol
    
    //
    // This program is free software: you can redistribute it and/or modify
    // it under the terms of the GNU Affero General Public License as published by
    // the Free Software Foundation, either version 3 of the License, or
    // (at your option) any later version.
    //
    // This program is distributed in the hope that it will be useful,
    // but WITHOUT ANY WARRANTY; without even the implied warranty of
    // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    // GNU Affero General Public License for more details.
    //
    // You should have received a copy of the GNU Affero General Public License
    // along with this program.  If not, see <https://www.gnu.org/licenses/>.
    
    pragma solidity ^0.4.18;
    
    // This program is free software: you can redistribute it and/or modify
    // it under the terms of the GNU General Public License as published by
    // the Free Software Foundation, either version 3 of the License, or
    // (at your option) any later version.
    
    // This program is distributed in the hope that it will be useful,
    // but WITHOUT ANY WARRANTY; without even the implied warranty of
    // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    // GNU General Public License for more details.
    
    // You should have received a copy of the GNU General Public License
    // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
    pragma solidity ^0.4.13;
    
    contract DSAuthority {
        function canCall(
            address src, address dst, bytes4 sig
        ) public view returns (bool);
    }
    
    contract DSAuthEvents {
        event LogSetAuthority (address indexed authority);
        event LogSetOwner     (address indexed owner);
    }
    
    contract DSAuth is DSAuthEvents {
        DSAuthority  public  authority;
        address      public  owner;
    
        function DSAuth() public {
            owner = msg.sender;
            LogSetOwner(msg.sender);
        }
    
        function setOwner(address owner_)
            public
            auth
        {
            owner = owner_;
            LogSetOwner(owner);
        }
    
        function setAuthority(DSAuthority authority_)
            public
            auth
        {
            authority = authority_;
            LogSetAuthority(authority);
        }
    
        modifier auth {
            require(isAuthorized(msg.sender, msg.sig));
            _;
        }
    
        function isAuthorized(address src, bytes4 sig) internal view returns (bool) {
            if (src == address(this)) {
                return true;
            } else if (src == owner) {
                return true;
            } else if (authority == DSAuthority(0)) {
                return false;
            } else {
                return authority.canCall(src, this, sig);
            }
        }
    }
    
    /// simple_market.sol
    
    //
    // This program is free software: you can redistribute it and/or modify
    // it under the terms of the GNU Affero General Public License as published by
    // the Free Software Foundation, either version 3 of the License, or
    // (at your option) any later version.
    //
    // This program is distributed in the hope that it will be useful,
    // but WITHOUT ANY WARRANTY; without even the implied warranty of
    // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    // GNU Affero General Public License for more details.
    //
    // You should have received a copy of the GNU Affero General Public License
    // along with this program.  If not, see <https://www.gnu.org/licenses/>.
    
    pragma solidity ^0.4.18;
    
    /// math.sol -- mixin for inline numerical wizardry
    
    // This program is free software: you can redistribute it and/or modify
    // it under the terms of the GNU General Public License as published by
    // the Free Software Foundation, either version 3 of the License, or
    // (at your option) any later version.
    
    // This program is distributed in the hope that it will be useful,
    // but WITHOUT ANY WARRANTY; without even the implied warranty of
    // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    // GNU General Public License for more details.
    
    // You should have received a copy of the GNU General Public License
    // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
    pragma solidity ^0.4.13;
    
    contract DSMath {
        function add(uint x, uint y) internal pure returns (uint z) {
            require((z = x + y) >= x);
        }
        function sub(uint x, uint y) internal pure returns (uint z) {
            require((z = x - y) <= x);
        }
        function mul(uint x, uint y) internal pure returns (uint z) {
            require(y == 0 || (z = x * y) / y == x);
        }
    
        function min(uint x, uint y) internal pure returns (uint z) {
            return x <= y ? x : y;
        }
        function max(uint x, uint y) internal pure returns (uint z) {
            return x >= y ? x : y;
        }
        function imin(int x, int y) internal pure returns (int z) {
            return x <= y ? x : y;
        }
        function imax(int x, int y) internal pure returns (int z) {
            return x >= y ? x : y;
        }
    
        uint constant WAD = 10 ** 18;
        uint constant RAY = 10 ** 27;
    
        function wmul(uint x, uint y) internal pure returns (uint z) {
            z = add(mul(x, y), WAD / 2) / WAD;
        }
        function rmul(uint x, uint y) internal pure returns (uint z) {
            z = add(mul(x, y), RAY / 2) / RAY;
        }
        function wdiv(uint x, uint y) internal pure returns (uint z) {
            z = add(mul(x, WAD), y / 2) / y;
        }
        function rdiv(uint x, uint y) internal pure returns (uint z) {
            z = add(mul(x, RAY), y / 2) / y;
        }
    
        // This famous algorithm is called "exponentiation by squaring"
        // and calculates x^n with x as fixed-point and n as regular unsigned.
        //
        // It's O(log n), instead of O(n) for naive repeated multiplication.
        //
        // These facts are why it works:
        //
        //  If n is even, then x^n = (x^2)^(n/2).
        //  If n is odd,  then x^n = x * x^(n-1),
        //   and applying the equation for even x gives
        //    x^n = x * (x^2)^((n-1) / 2).
        //
        //  Also, EVM division is flooring and
        //    floor[(n-1) / 2] = floor[n / 2].
        //
        function rpow(uint x, uint n) internal pure returns (uint z) {
            z = n % 2 != 0 ? x : RAY;
    
            for (n /= 2; n != 0; n /= 2) {
                x = rmul(x, x);
    
                if (n % 2 != 0) {
                    z = rmul(z, x);
                }
            }
        }
    }
    
    /// erc20.sol -- API for the ERC20 token standard
    
    // See <https://github.com/ethereum/EIPs/issues/20>.
    
    // This file likely does not meet the threshold of originality
    // required for copyright to apply.  As a result, this is free and
    // unencumbered software belonging to the public domain.
    
    pragma solidity ^0.4.8;
    
    contract ERC20Events {
        event Approval(address indexed src, address indexed guy, uint wad);
        event Transfer(address indexed src, address indexed dst, uint wad);
    }
    
    contract ERC20 is ERC20Events {
        function totalSupply() public view returns (uint);
        function balanceOf(address guy) public view returns (uint);
        function allowance(address src, address guy) public view returns (uint);
    
        function approve(address guy, uint wad) public returns (bool);
        function transfer(address dst, uint wad) public returns (bool);
        function transferFrom(
            address src, address dst, uint wad
        ) public returns (bool);
    }
    
    contract EventfulMarket {
        event LogItemUpdate(uint id);
        event LogTrade(uint pay_amt, address indexed pay_gem,
                       uint buy_amt, address indexed buy_gem);
    
        event LogMake(
            bytes32  indexed  id,
            bytes32  indexed  pair,
            address  indexed  maker,
            ERC20             pay_gem,
            ERC20             buy_gem,
            uint128           pay_amt,
            uint128           buy_amt,
            uint64            timestamp
        );
    
        event LogBump(
            bytes32  indexed  id,
            bytes32  indexed  pair,
            address  indexed  maker,
            ERC20             pay_gem,
            ERC20             buy_gem,
            uint128           pay_amt,
            uint128           buy_amt,
            uint64            timestamp
        );
    
        event LogTake(
            bytes32           id,
            bytes32  indexed  pair,
            address  indexed  maker,
            ERC20             pay_gem,
            ERC20             buy_gem,
            address  indexed  taker,
            uint128           take_amt,
            uint128           give_amt,
            uint64            timestamp
        );
    
        event LogKill(
            bytes32  indexed  id,
            bytes32  indexed  pair,
            address  indexed  maker,
            ERC20             pay_gem,
            ERC20             buy_gem,
            uint128           pay_amt,
            uint128           buy_amt,
            uint64            timestamp
        );
    }
    
    contract SimpleMarket is EventfulMarket, DSMath {
    
        uint public last_offer_id;
    
        mapping (uint => OfferInfo) public offers;
    
        bool locked;
    
        struct OfferInfo {
            uint     pay_amt;
            ERC20    pay_gem;
            uint     buy_amt;
            ERC20    buy_gem;
            address  owner;
            uint64   timestamp;
        }
    
        modifier can_buy(uint id) {
            require(isActive(id));
            _;
        }
    
        modifier can_cancel(uint id) {
            require(isActive(id));
            require(getOwner(id) == msg.sender);
            _;
        }
    
        modifier can_offer {
            _;
        }
    
        modifier synchronized {
            require(!locked);
            locked = true;
            _;
            locked = false;
        }
    
        function isActive(uint id) public constant returns (bool active) {
            return offers[id].timestamp > 0;
        }
    
        function getOwner(uint id) public constant returns (address owner) {
            return offers[id].owner;
        }
    
        function getOffer(uint id) public constant returns (uint, ERC20, uint, ERC20) {
          var offer = offers[id];
          return (offer.pay_amt, offer.pay_gem,
                  offer.buy_amt, offer.buy_gem);
        }
    
        // ---- Public entrypoints ---- //
    
        function bump(bytes32 id_)
            public
            can_buy(uint256(id_))
        {
            var id = uint256(id_);
            LogBump(
                id_,
                keccak256(offers[id].pay_gem, offers[id].buy_gem),
                offers[id].owner,
                offers[id].pay_gem,
                offers[id].buy_gem,
                uint128(offers[id].pay_amt),
                uint128(offers[id].buy_amt),
                offers[id].timestamp
            );
        }
    
        // Accept given `quantity` of an offer. Transfers funds from caller to
        // offer maker, and from market to caller.
        function buy(uint id, uint quantity)
            public
            can_buy(id)
            synchronized
            returns (bool)
        {
            OfferInfo memory offer = offers[id];
            uint spend = mul(quantity, offer.buy_amt) / offer.pay_amt;
    
            require(uint128(spend) == spend);
            require(uint128(quantity) == quantity);
    
            // For backwards semantic compatibility.
            if (quantity == 0 || spend == 0 ||
                quantity > offer.pay_amt || spend > offer.buy_amt)
            {
                return false;
            }
    
            offers[id].pay_amt = sub(offer.pay_amt, quantity);
            offers[id].buy_amt = sub(offer.buy_amt, spend);
            require( offer.buy_gem.transferFrom(msg.sender, offer.owner, spend) );
            require( offer.pay_gem.transfer(msg.sender, quantity) );
    
            LogItemUpdate(id);
            LogTake(
                bytes32(id),
                keccak256(offer.pay_gem, offer.buy_gem),
                offer.owner,
                offer.pay_gem,
                offer.buy_gem,
                msg.sender,
                uint128(quantity),
                uint128(spend),
                uint64(now)
            );
            LogTrade(quantity, offer.pay_gem, spend, offer.buy_gem);
    
            if (offers[id].pay_amt == 0) {
              delete offers[id];
            }
    
            return true;
        }
    
        // Cancel an offer. Refunds offer maker.
        function cancel(uint id)
            public
            can_cancel(id)
            synchronized
            returns (bool success)
        {
            // read-only offer. Modify an offer by directly accessing offers[id]
            OfferInfo memory offer = offers[id];
            delete offers[id];
    
            require( offer.pay_gem.transfer(offer.owner, offer.pay_amt) );
    
            LogItemUpdate(id);
            LogKill(
                bytes32(id),
                keccak256(offer.pay_gem, offer.buy_gem),
                offer.owner,
                offer.pay_gem,
                offer.buy_gem,
                uint128(offer.pay_amt),
                uint128(offer.buy_amt),
                uint64(now)
            );
    
            success = true;
        }
    
        function kill(bytes32 id)
            public
        {
            require(cancel(uint256(id)));
        }
    
        function make(
            ERC20    pay_gem,
            ERC20    buy_gem,
            uint128  pay_amt,
            uint128  buy_amt
        )
            public
            returns (bytes32 id)
        {
            return bytes32(offer(pay_amt, pay_gem, buy_amt, buy_gem));
        }
    
        // Make a new offer. Takes funds from the caller into market escrow.
        function offer(uint pay_amt, ERC20 pay_gem, uint buy_amt, ERC20 buy_gem)
            public
            can_offer
            synchronized
            returns (uint id)
        {
            require(uint128(pay_amt) == pay_amt);
            require(uint128(buy_amt) == buy_amt);
            require(pay_amt > 0);
            require(pay_gem != ERC20(0x0));
            require(buy_amt > 0);
            require(buy_gem != ERC20(0x0));
            require(pay_gem != buy_gem);
    
            OfferInfo memory info;
            info.pay_amt = pay_amt;
            info.pay_gem = pay_gem;
            info.buy_amt = buy_amt;
            info.buy_gem = buy_gem;
            info.owner = msg.sender;
            info.timestamp = uint64(now);
            id = _next_id();
            offers[id] = info;
    
            require( pay_gem.transferFrom(msg.sender, this, pay_amt) );
    
            LogItemUpdate(id);
            LogMake(
                bytes32(id),
                keccak256(pay_gem, buy_gem),
                msg.sender,
                pay_gem,
                buy_gem,
                uint128(pay_amt),
                uint128(buy_amt),
                uint64(now)
            );
        }
    
        function take(bytes32 id, uint128 maxTakeAmount)
            public
        {
            require(buy(uint256(id), maxTakeAmount));
        }
    
        function _next_id()
            internal
            returns (uint)
        {
            last_offer_id++; return last_offer_id;
        }
    }
    
    // Simple Market with a market lifetime. When the close_time has been reached,
    // offers can only be cancelled (offer and buy will throw).
    
    contract ExpiringMarket is DSAuth, SimpleMarket {
        uint64 public close_time;
        bool public stopped;
    
        // after close_time has been reached, no new offers are allowed
        modifier can_offer {
            require(!isClosed());
            _;
        }
    
        // after close, no new buys are allowed
        modifier can_buy(uint id) {
            require(isActive(id));
            require(!isClosed());
            _;
        }
    
        // after close, anyone can cancel an offer
        modifier can_cancel(uint id) {
            require(isActive(id));
            require((msg.sender == getOwner(id)) || isClosed());
            _;
        }
    
        function ExpiringMarket(uint64 _close_time)
            public
        {
            close_time = _close_time;
        }
    
        function isClosed() public constant returns (bool closed) {
            return stopped || getTime() > close_time;
        }
    
        function getTime() public constant returns (uint64) {
            return uint64(now);
        }
    
        function stop() public auth {
            stopped = true;
        }
    }
    
    /// note.sol -- the `note' modifier, for logging calls as events
    
    // This program is free software: you can redistribute it and/or modify
    // it under the terms of the GNU General Public License as published by
    // the Free Software Foundation, either version 3 of the License, or
    // (at your option) any later version.
    
    // This program is distributed in the hope that it will be useful,
    // but WITHOUT ANY WARRANTY; without even the implied warranty of
    // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    // GNU General Public License for more details.
    
    // You should have received a copy of the GNU General Public License
    // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
    pragma solidity ^0.4.13;
    
    contract DSNote {
        event LogNote(
            bytes4   indexed  sig,
            address  indexed  guy,
            bytes32  indexed  foo,
            bytes32  indexed  bar,
            uint              wad,
            bytes             fax
        ) anonymous;
    
        modifier note {
            bytes32 foo;
            bytes32 bar;
    
            assembly {
                foo := calldataload(4)
                bar := calldataload(36)
            }
    
            LogNote(msg.sig, msg.sender, foo, bar, msg.value, msg.data);
    
            _;
        }
    }
    
    contract MatchingEvents {
        event LogBuyEnabled(bool isEnabled);
        event LogMinSell(address pay_gem, uint min_amount);
        event LogMatchingEnabled(bool isEnabled);
        event LogUnsortedOffer(uint id);
        event LogSortedOffer(uint id);
        event LogInsert(address keeper, uint id);
        event LogDelete(address keeper, uint id);
    }
    
    contract MatchingMarket is MatchingEvents, ExpiringMarket, DSNote {
        bool public buyEnabled = true;      //buy enabled
        bool public matchingEnabled = true; //true: enable matching,
                                             //false: revert to expiring market
        struct sortInfo {
            uint next;  //points to id of next higher offer
            uint prev;  //points to id of previous lower offer
            uint delb;  //the blocknumber where this entry was marked for delete
        }
        mapping(uint => sortInfo) public _rank;                     //doubly linked lists of sorted offer ids
        mapping(address => mapping(address => uint)) public _best;  //id of the highest offer for a token pair
        mapping(address => mapping(address => uint)) public _span;  //number of offers stored for token pair in sorted orderbook
        mapping(address => uint) public _dust;                      //minimum sell amount for a token to avoid dust offers
        mapping(uint => uint) public _near;         //next unsorted offer id
        uint _head;                                 //first unsorted offer id
        uint public dustId;                         // id of the latest offer marked as dust
    
    
        function MatchingMarket(uint64 close_time) ExpiringMarket(close_time) public {
        }
    
        // After close, anyone can cancel an offer
        modifier can_cancel(uint id) {
            require(isActive(id), "Offer was deleted or taken, or never existed.");
            require(
                isClosed() || msg.sender == getOwner(id) || id == dustId,
                "Offer can not be cancelled because user is not owner, and market is open, and offer sells required amount of tokens."
            );
            _;
        }
    
        // ---- Public entrypoints ---- //
    
        function make(
            ERC20    pay_gem,
            ERC20    buy_gem,
            uint128  pay_amt,
            uint128  buy_amt
        )
            public
            returns (bytes32)
        {
            return bytes32(offer(pay_amt, pay_gem, buy_amt, buy_gem));
        }
    
        function take(bytes32 id, uint128 maxTakeAmount) public {
            require(buy(uint256(id), maxTakeAmount));
        }
    
        function kill(bytes32 id) public {
            require(cancel(uint256(id)));
        }
    
        // Make a new offer. Takes funds from the caller into market escrow.
        //
        // If matching is enabled:
        //     * creates new offer without putting it in
        //       the sorted list.
        //     * available to authorized contracts only!
        //     * keepers should call insert(id,pos)
        //       to put offer in the sorted list.
        //
        // If matching is disabled:
        //     * calls expiring market's offer().
        //     * available to everyone without authorization.
        //     * no sorting is done.
        //
        function offer(
            uint pay_amt,    //maker (ask) sell how much
            ERC20 pay_gem,   //maker (ask) sell which token
            uint buy_amt,    //taker (ask) buy how much
            ERC20 buy_gem    //taker (ask) buy which token
        )
            public
            returns (uint)
        {
            require(!locked, "Reentrancy attempt");
            var fn = matchingEnabled ? _offeru : super.offer;
            return fn(pay_amt, pay_gem, buy_amt, buy_gem);
        }
    
        // Make a new offer. Takes funds from the caller into market escrow.
        function offer(
            uint pay_amt,    //maker (ask) sell how much
            ERC20 pay_gem,   //maker (ask) sell which token
            uint buy_amt,    //maker (ask) buy how much
            ERC20 buy_gem,   //maker (ask) buy which token
            uint pos         //position to insert offer, 0 should be used if unknown
        )
            public
            can_offer
            returns (uint)
        {
            return offer(pay_amt, pay_gem, buy_amt, buy_gem, pos, true);
        }
    
        function offer(
            uint pay_amt,    //maker (ask) sell how much
            ERC20 pay_gem,   //maker (ask) sell which token
            uint buy_amt,    //maker (ask) buy how much
            ERC20 buy_gem,   //maker (ask) buy which token
            uint pos,        //position to insert offer, 0 should be used if unknown
            bool rounding    //match "close enough" orders?
        )
            public
            can_offer
            returns (uint)
        {
            require(!locked, "Reentrancy attempt");
            require(_dust[pay_gem] <= pay_amt);
    
            if (matchingEnabled) {
              return _matcho(pay_amt, pay_gem, buy_amt, buy_gem, pos, rounding);
            }
            return super.offer(pay_amt, pay_gem, buy_amt, buy_gem);
        }
    
        //Transfers funds from caller to offer maker, and from market to caller.
        function buy(uint id, uint amount)
            public
            can_buy(id)
            returns (bool)
        {
            require(!locked, "Reentrancy attempt");
            var fn = matchingEnabled ? _buys : super.buy;
            return fn(id, amount);
        }
    
        // Cancel an offer. Refunds offer maker.
        function cancel(uint id)
            public
            can_cancel(id)
            returns (bool success)
        {
            require(!locked, "Reentrancy attempt");
            if (matchingEnabled) {
                if (isOfferSorted(id)) {
                    require(_unsort(id));
                } else {
                    require(_hide(id));
                }
            }
            return super.cancel(id);    //delete the offer.
        }
    
        //insert offer into the sorted list
        //keepers need to use this function
        function insert(
            uint id,   //maker (ask) id
            uint pos   //position to insert into
        )
            public
            returns (bool)
        {
            require(!locked, "Reentrancy attempt");
            require(!isOfferSorted(id));    //make sure offers[id] is not yet sorted
            require(isActive(id));          //make sure offers[id] is active
    
            _hide(id);                      //remove offer from unsorted offers list
            _sort(id, pos);                 //put offer into the sorted offers list
            LogInsert(msg.sender, id);
            return true;
        }
    
        //deletes _rank [id]
        //  Function should be called by keepers.
        function del_rank(uint id)
            public
            returns (bool)
        {
            require(!locked, "Reentrancy attempt");
            require(!isActive(id) && _rank[id].delb != 0 && _rank[id].delb < block.number - 10);
            delete _rank[id];
            LogDelete(msg.sender, id);
            return true;
        }
    
        //set the minimum sell amount for a token
        //    Function is used to avoid "dust offers" that have
        //    very small amount of tokens to sell, and it would
        //    cost more gas to accept the offer, than the value
        //    of tokens received.
        function setMinSell(
            ERC20 pay_gem,     //token to assign minimum sell amount to
            uint dust          //maker (ask) minimum sell amount
        )
            public
            auth
            note
            returns (bool)
        {
            _dust[pay_gem] = dust;
            LogMinSell(pay_gem, dust);
            return true;
        }
    
        //returns the minimum sell amount for an offer
        function getMinSell(
            ERC20 pay_gem      //token for which minimum sell amount is queried
        )
            public
            constant
            returns (uint)
        {
            return _dust[pay_gem];
        }
    
        //set buy functionality enabled/disabled
        function setBuyEnabled(bool buyEnabled_) public auth returns (bool) {
            buyEnabled = buyEnabled_;
            LogBuyEnabled(buyEnabled);
            return true;
        }
    
        //set matching enabled/disabled
        //    If matchingEnabled true(default), then inserted offers are matched.
        //    Except the ones inserted by contracts, because those end up
        //    in the unsorted list of offers, that must be later sorted by
        //    keepers using insert().
        //    If matchingEnabled is false then MatchingMarket is reverted to ExpiringMarket,
        //    and matching is not done, and sorted lists are disabled.
        function setMatchingEnabled(bool matchingEnabled_) public auth returns (bool) {
            matchingEnabled = matchingEnabled_;
            LogMatchingEnabled(matchingEnabled);
            return true;
        }
    
        //return the best offer for a token pair
        //      the best offer is the lowest one if it's an ask,
        //      and highest one if it's a bid offer
        function getBestOffer(ERC20 sell_gem, ERC20 buy_gem) public constant returns(uint) {
            return _best[sell_gem][buy_gem];
        }
    
        //return the next worse offer in the sorted list
        //      the worse offer is the higher one if its an ask,
        //      a lower one if its a bid offer,
        //      and in both cases the newer one if they're equal.
        function getWorseOffer(uint id) public constant returns(uint) {
            return _rank[id].prev;
        }
    
        //return the next better offer in the sorted list
        //      the better offer is in the lower priced one if its an ask,
        //      the next higher priced one if its a bid offer
        //      and in both cases the older one if they're equal.
        function getBetterOffer(uint id) public constant returns(uint) {
    
            return _rank[id].next;
        }
    
        //return the amount of better offers for a token pair
        function getOfferCount(ERC20 sell_gem, ERC20 buy_gem) public constant returns(uint) {
            return _span[sell_gem][buy_gem];
        }
    
        //get the first unsorted offer that was inserted by a contract
        //      Contracts can't calculate the insertion position of their offer because it is not an O(1) operation.
        //      Their offers get put in the unsorted list of offers.
        //      Keepers can calculate the insertion position offchain and pass it to the insert() function to insert
        //      the unsorted offer into the sorted list. Unsorted offers will not be matched, but can be bought with buy().
        function getFirstUnsortedOffer() public constant returns(uint) {
            return _head;
        }
    
        //get the next unsorted offer
        //      Can be used to cycle through all the unsorted offers.
        function getNextUnsortedOffer(uint id) public constant returns(uint) {
            return _near[id];
        }
    
        function isOfferSorted(uint id) public constant returns(bool) {
            return _rank[id].next != 0
                   || _rank[id].prev != 0
                   || _best[offers[id].pay_gem][offers[id].buy_gem] == id;
        }
    
        function sellAllAmount(ERC20 pay_gem, uint pay_amt, ERC20 buy_gem, uint min_fill_amount)
            public
            returns (uint fill_amt)
        {
            require(!locked, "Reentrancy attempt");
            uint offerId;
            while (pay_amt > 0) {                           //while there is amount to sell
                offerId = getBestOffer(buy_gem, pay_gem);   //Get the best offer for the token pair
                require(offerId != 0);                      //Fails if there are not more offers
    
                // There is a chance that pay_amt is smaller than 1 wei of the other token
                if (pay_amt * 1 ether < wdiv(offers[offerId].buy_amt, offers[offerId].pay_amt)) {
                    break;                                  //We consider that all amount is sold
                }
                if (pay_amt >= offers[offerId].buy_amt) {                       //If amount to sell is higher or equal than current offer amount to buy
                    fill_amt = add(fill_amt, offers[offerId].pay_amt);          //Add amount bought to acumulator
                    pay_amt = sub(pay_amt, offers[offerId].buy_amt);            //Decrease amount to sell
                    take(bytes32(offerId), uint128(offers[offerId].pay_amt));   //We take the whole offer
                } else { // if lower
                    var baux = rmul(pay_amt * 10 ** 9, rdiv(offers[offerId].pay_amt, offers[offerId].buy_amt)) / 10 ** 9;
                    fill_amt = add(fill_amt, baux);         //Add amount bought to acumulator
                    take(bytes32(offerId), uint128(baux));  //We take the portion of the offer that we need
                    pay_amt = 0;                            //All amount is sold
                }
            }
            require(fill_amt >= min_fill_amount);
        }
    
        function buyAllAmount(ERC20 buy_gem, uint buy_amt, ERC20 pay_gem, uint max_fill_amount)
            public
            returns (uint fill_amt)
        {
            require(!locked, "Reentrancy attempt");
            uint offerId;
            while (buy_amt > 0) {                           //Meanwhile there is amount to buy
                offerId = getBestOffer(buy_gem, pay_gem);   //Get the best offer for the token pair
                require(offerId != 0);
    
                // There is a chance that buy_amt is smaller than 1 wei of the other token
                if (buy_amt * 1 ether < wdiv(offers[offerId].pay_amt, offers[offerId].buy_amt)) {
                    break;                                  //We consider that all amount is sold
                }
                if (buy_amt >= offers[offerId].pay_amt) {                       //If amount to buy is higher or equal than current offer amount to sell
                    fill_amt = add(fill_amt, offers[offerId].buy_amt);          //Add amount sold to acumulator
                    buy_amt = sub(buy_amt, offers[offerId].pay_amt);            //Decrease amount to buy
                    take(bytes32(offerId), uint128(offers[offerId].pay_amt));   //We take the whole offer
                } else {                                                        //if lower
                    fill_amt = add(fill_amt, rmul(buy_amt * 10 ** 9, rdiv(offers[offerId].buy_amt, offers[offerId].pay_amt)) / 10 ** 9); //Add amount sold to acumulator
                    take(bytes32(offerId), uint128(buy_amt));                   //We take the portion of the offer that we need
                    buy_amt = 0;                                                //All amount is bought
                }
            }
            require(fill_amt <= max_fill_amount);
        }
    
        function getBuyAmount(ERC20 buy_gem, ERC20 pay_gem, uint pay_amt) public constant returns (uint fill_amt) {
            var offerId = getBestOffer(buy_gem, pay_gem);           //Get best offer for the token pair
            while (pay_amt > offers[offerId].buy_amt) {
                fill_amt = add(fill_amt, offers[offerId].pay_amt);  //Add amount to buy accumulator
                pay_amt = sub(pay_amt, offers[offerId].buy_amt);    //Decrease amount to pay
                if (pay_amt > 0) {                                  //If we still need more offers
                    offerId = getWorseOffer(offerId);               //We look for the next best offer
                    require(offerId != 0);                          //Fails if there are not enough offers to complete
                }
            }
            fill_amt = add(fill_amt, rmul(pay_amt * 10 ** 9, rdiv(offers[offerId].pay_amt, offers[offerId].buy_amt)) / 10 ** 9); //Add proportional amount of last offer to buy accumulator
        }
    
        function getPayAmount(ERC20 pay_gem, ERC20 buy_gem, uint buy_amt) public constant returns (uint fill_amt) {
            var offerId = getBestOffer(buy_gem, pay_gem);           //Get best offer for the token pair
            while (buy_amt > offers[offerId].pay_amt) {
                fill_amt = add(fill_amt, offers[offerId].buy_amt);  //Add amount to pay accumulator
                buy_amt = sub(buy_amt, offers[offerId].pay_amt);    //Decrease amount to buy
                if (buy_amt > 0) {                                  //If we still need more offers
                    offerId = getWorseOffer(offerId);               //We look for the next best offer
                    require(offerId != 0);                          //Fails if there are not enough offers to complete
                }
            }
            fill_amt = add(fill_amt, rmul(buy_amt * 10 ** 9, rdiv(offers[offerId].buy_amt, offers[offerId].pay_amt)) / 10 ** 9); //Add proportional amount of last offer to pay accumulator
        }
    
        // ---- Internal Functions ---- //
    
        function _buys(uint id, uint amount)
            internal
            returns (bool)
        {
            require(buyEnabled);
            if (amount == offers[id].pay_amt) {
                if (isOfferSorted(id)) {
                    //offers[id] must be removed from sorted list because all of it is bought
                    _unsort(id);
                }else{
                    _hide(id);
                }
            }
            require(super.buy(id, amount));
            // If offer has become dust during buy, we cancel it
            if (isActive(id) && offers[id].pay_amt < _dust[offers[id].pay_gem]) {
                dustId = id; //enable current msg.sender to call cancel(id)
                cancel(id);
            }
            return true;
        }
    
        //find the id of the next higher offer after offers[id]
        function _find(uint id)
            internal
            view
            returns (uint)
        {
            require( id > 0 );
    
            address buy_gem = address(offers[id].buy_gem);
            address pay_gem = address(offers[id].pay_gem);
            uint top = _best[pay_gem][buy_gem];
            uint old_top = 0;
    
            // Find the larger-than-id order whose successor is less-than-id.
            while (top != 0 && _isPricedLtOrEq(id, top)) {
                old_top = top;
                top = _rank[top].prev;
            }
            return old_top;
        }
    
        //find the id of the next higher offer after offers[id]
        function _findpos(uint id, uint pos)
            internal
            view
            returns (uint)
        {
            require(id > 0);
    
            // Look for an active order.
            while (pos != 0 && !isActive(pos)) {
                pos = _rank[pos].prev;
            }
    
            if (pos == 0) {
                //if we got to the end of list without a single active offer
                return _find(id);
    
            } else {
                // if we did find a nearby active offer
                // Walk the order book down from there...
                if(_isPricedLtOrEq(id, pos)) {
                    uint old_pos;
    
                    // Guaranteed to run at least once because of
                    // the prior if statements.
                    while (pos != 0 && _isPricedLtOrEq(id, pos)) {
                        old_pos = pos;
                        pos = _rank[pos].prev;
                    }
                    return old_pos;
    
                // ...or walk it up.
                } else {
                    while (pos != 0 && !_isPricedLtOrEq(id, pos)) {
                        pos = _rank[pos].next;
                    }
                    return pos;
                }
            }
        }
    
        //return true if offers[low] priced less than or equal to offers[high]
        function _isPricedLtOrEq(
            uint low,   //lower priced offer's id
            uint high   //higher priced offer's id
        )
            internal
            view
            returns (bool)
        {
            return mul(offers[low].buy_amt, offers[high].pay_amt)
              >= mul(offers[high].buy_amt, offers[low].pay_amt);
        }
    
        //these variables are global only because of solidity local variable limit
    
        //match offers with taker offer, and execute token transactions
        function _matcho(
            uint t_pay_amt,    //taker sell how much
            ERC20 t_pay_gem,   //taker sell which token
            uint t_buy_amt,    //taker buy how much
            ERC20 t_buy_gem,   //taker buy which token
            uint pos,          //position id
            bool rounding      //match "close enough" orders?
        )
            internal
            returns (uint id)
        {
            uint best_maker_id;    //highest maker id
            uint t_buy_amt_old;    //taker buy how much saved
            uint m_buy_amt;        //maker offer wants to buy this much token
            uint m_pay_amt;        //maker offer wants to sell this much token
    
            // there is at least one offer stored for token pair
            while (_best[t_buy_gem][t_pay_gem] > 0) {
                best_maker_id = _best[t_buy_gem][t_pay_gem];
                m_buy_amt = offers[best_maker_id].buy_amt;
                m_pay_amt = offers[best_maker_id].pay_amt;
    
                // Ugly hack to work around rounding errors. Based on the idea that
                // the furthest the amounts can stray from their "true" values is 1.
                // Ergo the worst case has t_pay_amt and m_pay_amt at +1 away from
                // their "correct" values and m_buy_amt and t_buy_amt at -1.
                // Since (c - 1) * (d - 1) > (a + 1) * (b + 1) is equivalent to
                // c * d > a * b + a + b + c + d, we write...
                if (mul(m_buy_amt, t_buy_amt) > mul(t_pay_amt, m_pay_amt) +
                    (rounding ? m_buy_amt + t_buy_amt + t_pay_amt + m_pay_amt : 0))
                {
                    break;
                }
                // ^ The `rounding` parameter is a compromise borne of a couple days
                // of discussion.
                buy(best_maker_id, min(m_pay_amt, t_buy_amt));
                t_buy_amt_old = t_buy_amt;
                t_buy_amt = sub(t_buy_amt, min(m_pay_amt, t_buy_amt));
                t_pay_amt = mul(t_buy_amt, t_pay_amt) / t_buy_amt_old;
    
                if (t_pay_amt == 0 || t_buy_amt == 0) {
                    break;
                }
            }
    
            if (t_buy_amt > 0 && t_pay_amt > 0 && t_pay_amt >= _dust[t_pay_gem]) {
                //new offer should be created
                id = super.offer(t_pay_amt, t_pay_gem, t_buy_amt, t_buy_gem);
                //insert offer into the sorted list
                _sort(id, pos);
            }
        }
    
        // Make a new offer without putting it in the sorted list.
        // Takes funds from the caller into market escrow.
        // ****Available to authorized contracts only!**********
        // Keepers should call insert(id,pos) to put offer in the sorted list.
        function _offeru(
            uint pay_amt,      //maker (ask) sell how much
            ERC20 pay_gem,     //maker (ask) sell which token
            uint buy_amt,      //maker (ask) buy how much
            ERC20 buy_gem      //maker (ask) buy which token
        )
            internal
            returns (uint id)
        {
            require(_dust[pay_gem] <= pay_amt);
            id = super.offer(pay_amt, pay_gem, buy_amt, buy_gem);
            _near[id] = _head;
            _head = id;
            LogUnsortedOffer(id);
        }
    
        //put offer into the sorted list
        function _sort(
            uint id,    //maker (ask) id
            uint pos    //position to insert into
        )
            internal
        {
            require(isActive(id));
    
            address buy_gem = address(offers[id].buy_gem);
            address pay_gem = address(offers[id].pay_gem);
            uint prev_id;                                      //maker (ask) id
    
            pos = pos == 0 || offers[pos].pay_gem != pay_gem || offers[pos].buy_gem != buy_gem || !isOfferSorted(pos)
            ?
                _find(id)
            :
                _findpos(id, pos);
    
            if (pos != 0) {                                    //offers[id] is not the highest offer
                //requirement below is satisfied by statements above
                //require(_isPricedLtOrEq(id, pos));
                prev_id = _rank[pos].prev;
                _rank[pos].prev = id;
                _rank[id].next = pos;
            } else {                                           //offers[id] is the highest offer
                prev_id = _best[pay_gem][buy_gem];
                _best[pay_gem][buy_gem] = id;
            }
    
            if (prev_id != 0) {                               //if lower offer does exist
                //requirement below is satisfied by statements above
                //require(!_isPricedLtOrEq(id, prev_id));
                _rank[prev_id].next = id;
                _rank[id].prev = prev_id;
            }
    
            _span[pay_gem][buy_gem]++;
            LogSortedOffer(id);
        }
    
        // Remove offer from the sorted list (does not cancel offer)
        function _unsort(
            uint id    //id of maker (ask) offer to remove from sorted list
        )
            internal
            returns (bool)
        {
            address buy_gem = address(offers[id].buy_gem);
            address pay_gem = address(offers[id].pay_gem);
            require(_span[pay_gem][buy_gem] > 0);
    
            require(_rank[id].delb == 0 &&                    //assert id is in the sorted list
                     isOfferSorted(id));
    
            if (id != _best[pay_gem][buy_gem]) {              // offers[id] is not the highest offer
                require(_rank[_rank[id].next].prev == id);
                _rank[_rank[id].next].prev = _rank[id].prev;
            } else {                                          //offers[id] is the highest offer
                _best[pay_gem][buy_gem] = _rank[id].prev;
            }
    
            if (_rank[id].prev != 0) {                        //offers[id] is not the lowest offer
                require(_rank[_rank[id].prev].next == id);
                _rank[_rank[id].prev].next = _rank[id].next;
            }
    
            _span[pay_gem][buy_gem]--;
            _rank[id].delb = block.number;                    //mark _rank[id] for deletion
            return true;
        }
    
        //Hide offer from the unsorted order book (does not cancel offer)
        function _hide(
            uint id     //id of maker offer to remove from unsorted list
        )
            internal
            returns (bool)
        {
            uint uid = _head;               //id of an offer in unsorted offers list
            uint pre = uid;                 //id of previous offer in unsorted offers list
    
            require(!isOfferSorted(id));    //make sure offer id is not in sorted offers list
    
            if (_head == id) {              //check if offer is first offer in unsorted offers list
                _head = _near[id];          //set head to new first unsorted offer
                _near[id] = 0;              //delete order from unsorted order list
                return true;
            }
            while (uid > 0 && uid != id) {  //find offer in unsorted order list
                pre = uid;
                uid = _near[uid];
            }
            if (uid != id) {                //did not find offer id in unsorted offers list
                return false;
            }
            _near[pre] = _near[id];         //set previous unsorted offer to point to offer after offer id
            _near[id] = 0;                  //delete order from unsorted order list
            return true;
        }
    }