// Vulnerability label: TOD1
pragma solidity^0.4.24;

/**
                    RUSSIAN ROULETTE
                   https://m2d.win/rr 
    
    .................................. LAUGHING MAN sssyyhddmN..........................................
    ..........................Nmdyyso+/:--.``` :`  `-`:--:/+ossyhdmN....................................
    ......................Ndhyso/:.`   --.     o.  /+`o::` `` `-:+osyh..................................
    ..................MNdyso/-` /-`/:+./:/..`  +.  //.o +.+::+ -`  `-/sshdN.............................
    ................Ndyso:` ` --:+`o//.-:-```  ...  ``` - /::::/ +..-` ./osh............................
    ..............Nhso/. .-.:/`o--:``   `..-:::oss+::--.``    .:/::/`+-`/../sydN........................
    ............mhso-``-:+./:-:.   .-/+osssssssssssssssssso+:-`  -//o::+:/` .:oyhN......................
    ..........Nhso:`  .+-./ `  .:+sssssso+//:-------:://+ossssso/---.`-`/:-o/ `:syd.....................
    ........Mdyo- +/../`-`  ./osssso/-.`                 ``.:+ossss+:`  `-+`  ` `/sy....................
    ......MNys/` -:-/:    -+ssss+-`                           `.:+ssss/.  `  -+-. .osh..................
    ......mys-  :-/+-`  :osss+-`                                  .:osss+.  `//o:- `/syN................
    ....Mdso. --:-/-  -osss+.                                       `-osss+`  :--://`-sy................
    ....dso-. ++:+  `/sss+.                                           `:osss:  `:.-+  -sy...............
    ..Mdso``+///.` .osss:                                               `/sss+`  :/-.. -syN.............
    ..mss` `+::/  .ssso.                                                  :sss+` `+:/+  -syN............
    ..ys-   ```  .ssso`                                                    -sss+` `:::+:`/sh............
    Mds+ `:/..  `osso`                                                      -sss+  -:`.` `ssN...........
    Mys. `/+::  +sss/........................................................+sss:.....-::+sy..NN.......
    ds+  :-/-  .ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssyyhdN...
    hs: `/+::   :/+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ossssyhNM
    ss. `:::`                    ````                        ```                               ``-+sssyN
    ss` /:-+` `o++:           .:+oooo+/.                 `-/ooooo+-`                               -sssy
    ss  `:/:  `sss/          :ooo++/++os/`              .oso++/++oso.                               osss
    ss``/:--  `sss/         ./.`      `.::              /-.`     ``-/`                             -sssy
    ss.:::-:.  ssso         `            `                                                    ``.-+sssyN
    hs:`:/:/.  /sss.   .++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++oossssyhNM
    ds+ ``     .sss/   -ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssyyyyhmN...
    Nss.:::::.  +sss.   +sss/........................................osss:...+sss:......../shmNNN.......
    Mds+..-:::` `osso`  `+sss:                                     `+sss:   -sss+  .:-.` `ssN...........
    ..ys- .+.::  .ssso`  `/sss+.                                  -osss:   -sss+` `:++-` /sh............
    ..mss` .-.    .ssso.   :osss/`                              .+ssso.   :sss+` `.:+:` -syN............
    ..Mdso`  `--:` .osss:   `/ssss/.`                        `-+ssso:`  `/sss+` `++.-. -syN.............
    ....dso` -//+-` `/sss+.   ./ossso/-``                `.:+sssso:`  `:osss:  .::+/. -sy...............
    ....Mdso. `-//-`  -osss+.   `-+ssssso+/:-.`````..-:/+osssso/.   `-osss+.` -///-  -sy................
    ......mys- `/://.`  :osss+-`   `-/+osssssssssssssssssso+:.    .:osss+.  .:`..-``/syN................
    ......MNys/` ..+-/:   -+ssss+-`    `.-://++oooo++/:-.`    `.:+ssss/.  .`      .osh..................
    ........Mdyo- `::/.  `  ./osssso/-.`                 ``.:+ossss+:` `  .//`  `/sy....................
    ..........Nhso-     :+:.`  .:+sssssso+//:--------://+ossssso/:.  `::/: --/.:syd.....................
    ............mhso-` ./+--+-:    .-/+osssssssssssssssssso+/-.  .+` `//-/ `::oyhN......................
    ..............Nhso/`   +/:--+.-`    `..-:::////::--.``    .`:/-o`  ./`./sydN........................
    ................Ndys+:` ``--+++-  .:  `.``      `` -.`/:/`.o./::.  ./osh............................
    ..................MNdyso/-` ` :`  +-  :+.o`s ::-/++`s`+/o.-:`  `-/sshdN.............................
    ......................Ndhyso/:.` .+   +/:/ +:/-./:-`+: `` `.:+osyh..................................
    ..........................Nmdyyso+/:--/.``      ``..-:/+ossyhdmN....................................
    ..............................MN..dhhyyssssssssssssyyhddmN..........................................
 */

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);
            }
        }
    }
}

contract DSA {
    function canCall(
        address src, address dst, bytes4 sig
    ) public view returns (bool);
}

contract DSAuthEvents {
    event LogSetOrcl (address indexed authority);
    event LogSetOwner     (address indexed owner);
}

contract DSAuth is DSAuthEvents {
    DSA  public  a;
    address      public  owner;

    constructor() public {
        owner = msg.sender;
        emit LogSetOwner(msg.sender);
    }

    function setOwner(address owner_)
        public
        auth
    {
        owner = owner_;
        emit LogSetOwner(owner);
    }

    function setOrcl(DSA a_)
        public
        auth
    {
        a = a_;
        emit LogSetOrcl(a);
    }

    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 (a == DSA(0)) {
            return false;
        } else {
            return a.canCall(src, this, sig);
        }
    }
}

contract OraclizeI {
    address public cbAddress;
    function query(uint _timestamp, string _datasource, string _arg) external payable returns (bytes32 _id);
    function query_withGasLimit(uint _timestamp, string _datasource, string _arg, uint _gaslimit) external payable returns (bytes32 _id);
    function query2(uint _timestamp, string _datasource, string _arg1, string _arg2) public payable returns (bytes32 _id);
    function query2_withGasLimit(uint _timestamp, string _datasource, string _arg1, string _arg2, uint _gaslimit) external payable returns (bytes32 _id);
    function queryN(uint _timestamp, string _datasource, bytes _argN) public payable returns (bytes32 _id);
    function queryN_withGasLimit(uint _timestamp, string _datasource, bytes _argN, uint _gaslimit) external payable returns (bytes32 _id);
    function getPrice(string _datasource) public returns (uint _dsprice);
    function getPrice(string _datasource, uint gaslimit) public returns (uint _dsprice);
    function setProofType(byte _proofType) external;
    function setCustomGasPrice(uint _gasPrice) external;
    function randomDS_getSessionPubKeyHash() external constant returns(bytes32);
}

contract OraclizeAddrResolverI {
    function getAddress() public returns (address _addr);
}

/*
Begin solidity-cborutils

https://github.com/smartcontractkit/solidity-cborutils

MIT License

Copyright (c) 2018 SmartContract ChainLink, Ltd.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
 */
// solium-disable security/no-inline-assembly
library Buffer {
    struct buffer {
        bytes buf;
        uint capacity;
    }

    function init(buffer memory buf, uint _capacity) internal pure {
        uint capacity = _capacity;
        if(capacity % 32 != 0) capacity += 32 - (capacity % 32);
        // Allocate space for the buffer data
        buf.capacity = capacity;
        assembly {
            let ptr := mload(0x40)
            mstore(buf, ptr)
            mstore(ptr, 0)
            mstore(0x40, add(ptr, capacity))
        }
    }

    function resize(buffer memory buf, uint capacity) private pure {
        bytes memory oldbuf = buf.buf;
        init(buf, capacity);
        append(buf, oldbuf);
    }

    function max(uint a, uint b) private pure returns(uint) {
        if(a > b) {
            return a;
        }
        return b;
    }

    /**
     * @dev Appends a byte array to the end of the buffer. Resizes if doing so
     *      would exceed the capacity of the buffer.
     * @param buf The buffer to append to.
     * @param data The data to append.
     * @return The original buffer.
     */
    function append(buffer memory buf, bytes data) internal pure returns(buffer memory) {
        if(data.length + buf.buf.length > buf.capacity) {
            resize(buf, max(buf.capacity, data.length) * 2);
        }

        uint dest;
        uint src;
        uint len = data.length;
        assembly {
            // Memory address of the buffer data
            let bufptr := mload(buf)
            // Length of existing buffer data
            let buflen := mload(bufptr)
            // Start address = buffer address + buffer length + sizeof(buffer length)
            dest := add(add(bufptr, buflen), 32)
            // Update buffer length
            mstore(bufptr, add(buflen, mload(data)))
            src := add(data, 32)
        }

        // Copy word-length chunks while possible
        for(; len >= 32; len -= 32) {
            assembly {
                mstore(dest, mload(src))
            }
            dest += 32;
            src += 32;
        }

        // Copy remaining bytes
        uint mask = 256 ** (32 - len) - 1;
        assembly {
            let srcpart := and(mload(src), not(mask))
            let destpart := and(mload(dest), mask)
            mstore(dest, or(destpart, srcpart))
        }

        return buf;
    }

    /**
     * @dev Appends a byte to the end of the buffer. Resizes if doing so would
     * exceed the capacity of the buffer.
     * @param buf The buffer to append to.
     * @param data The data to append.
     * @return The original buffer.
     */
    function append(buffer memory buf, uint8 data) internal pure {
        if(buf.buf.length + 1 > buf.capacity) {
            resize(buf, buf.capacity * 2);
        }

        assembly {
            // Memory address of the buffer data
            let bufptr := mload(buf)
            // Length of existing buffer data
            let buflen := mload(bufptr)
            // Address = buffer address + buffer length + sizeof(buffer length)
            let dest := add(add(bufptr, buflen), 32)
            mstore8(dest, data)
            // Update buffer length
            mstore(bufptr, add(buflen, 1))
        }
    }

    /**
     * @dev Appends a byte to the end of the buffer. Resizes if doing so would
     * exceed the capacity of the buffer.
     * @param buf The buffer to append to.
     * @param data The data to append.
     * @return The original buffer.
     */
    function appendInt(buffer memory buf, uint data, uint len) internal pure returns(buffer memory) {
        if(len + buf.buf.length > buf.capacity) {
            resize(buf, max(buf.capacity, len) * 2);
        }

        uint mask = 256 ** len - 1;
        assembly {
            // Memory address of the buffer data
            let bufptr := mload(buf)
            // Length of existing buffer data
            let buflen := mload(bufptr)
            // Address = buffer address + buffer length + sizeof(buffer length) + len
            let dest := add(add(bufptr, buflen), len)
            mstore(dest, or(and(mload(dest), not(mask)), data))
            // Update buffer length
            mstore(bufptr, add(buflen, len))
        }
        return buf;
    }
}

library CBOR {
    using Buffer for Buffer.buffer;

    uint8 private constant MAJOR_TYPE_INT = 0;
    uint8 private constant MAJOR_TYPE_NEGATIVE_INT = 1;
    uint8 private constant MAJOR_TYPE_BYTES = 2;
    uint8 private constant MAJOR_TYPE_STRING = 3;
    uint8 private constant MAJOR_TYPE_ARRAY = 4;
    uint8 private constant MAJOR_TYPE_MAP = 5;
    uint8 private constant MAJOR_TYPE_CONTENT_FREE = 7;

    function encodeType(Buffer.buffer memory buf, uint8 major, uint value) private pure {
        if(value <= 23) {
            buf.append(uint8((major << 5) | value));
        } else if(value <= 0xFF) {
            buf.append(uint8((major << 5) | 24));
            buf.appendInt(value, 1);
        } else if(value <= 0xFFFF) {
            buf.append(uint8((major << 5) | 25));
            buf.appendInt(value, 2);
        } else if(value <= 0xFFFFFFFF) {
            buf.append(uint8((major << 5) | 26));
            buf.appendInt(value, 4);
        } else if(value <= 0xFFFFFFFFFFFFFFFF) {
            buf.append(uint8((major << 5) | 27));
            buf.appendInt(value, 8);
        }
    }

    function encodeIndefiniteLengthType(Buffer.buffer memory buf, uint8 major) private pure {
        buf.append(uint8((major << 5) | 31));
    }

    function encodeUInt(Buffer.buffer memory buf, uint value) internal pure {
        encodeType(buf, MAJOR_TYPE_INT, value);
    }

    function encodeInt(Buffer.buffer memory buf, int value) internal pure {
        if(value >= 0) {
            encodeType(buf, MAJOR_TYPE_INT, uint(value));
        } else {
            encodeType(buf, MAJOR_TYPE_NEGATIVE_INT, uint(-1 - value));
        }
    }

    function encodeBytes(Buffer.buffer memory buf, bytes value) internal pure {
        encodeType(buf, MAJOR_TYPE_BYTES, value.length);
        buf.append(value);
    }

    function encodeString(Buffer.buffer memory buf, string value) internal pure {
        encodeType(buf, MAJOR_TYPE_STRING, bytes(value).length);
        buf.append(bytes(value));
    }

    function startArray(Buffer.buffer memory buf) internal pure {
        encodeIndefiniteLengthType(buf, MAJOR_TYPE_ARRAY);
    }

    function startMap(Buffer.buffer memory buf) internal pure {
        encodeIndefiniteLengthType(buf, MAJOR_TYPE_MAP);
    }

    function endSequence(Buffer.buffer memory buf) internal pure {
        encodeIndefiniteLengthType(buf, MAJOR_TYPE_CONTENT_FREE);
    }
}

/*
End solidity-cborutils
 */

contract usingOraclize is DSAuth {
    byte constant proofType_NONE = 0x00;
    byte constant proofType_TLSNotary = 0x10;
    byte constant proofType_Ledger = 0x30;
    byte constant proofType_Android = 0x40;
    byte constant proofType_Native = 0xF0;
    byte constant proofStorage_IPFS = 0x01;
    uint8 constant networkID_auto = 0;
    uint8 constant networkID_mainnet = 1;
    uint8 constant networkID_testnet = 2;
    uint8 constant networkID_morden = 2;
    uint8 constant networkID_consensys = 161;

    OraclizeAddrResolverI OAR;

    OraclizeI oraclize;

    modifier oraclizeAPI {
        if((address(OAR)==0)||(getCodeSize(address(OAR))==0))
            oraclize_setNetwork(networkID_auto);

        if(address(oraclize) != OAR.getAddress())
            oraclize = OraclizeI(OAR.getAddress());
        _;
    }
    modifier coupon(string code){
        oraclize = OraclizeI(OAR.getAddress());
        _;
    }

    function oraclize_setNetwork(uint8 networkID) internal returns(bool){
        return oraclize_setNetwork();
        // solium-disable security/no-unreachable-code
        networkID;
    }

    function oraclize_setNetwork() internal returns(bool){
        if (getCodeSize(0x1d3B2638a7cC9f2CB3D298A3DA7a90B67E5506ed)>0){ //mainnet
            OAR = OraclizeAddrResolverI(0x1d3B2638a7cC9f2CB3D298A3DA7a90B67E5506ed);
            oraclize_setNetworkName("eth_mainnet");
            return true;
        }
        if (getCodeSize(0xc03A2615D5efaf5F49F60B7BB6583eaec212fdf1)>0){ //ropsten testnet
            OAR = OraclizeAddrResolverI(0xc03A2615D5efaf5F49F60B7BB6583eaec212fdf1);
            oraclize_setNetworkName("eth_ropsten3");
            return true;
        }
        
        return false;
    }

    function oraclize_cbAddress() internal oraclizeAPI returns (address){
        return oraclize.cbAddress();
    }

    function __callback(bytes32 myid, string result) public {
        __callback(myid, result, new bytes(0));
    }
    
    function __callback(bytes32 myid, string result, bytes proof) public;

    function oraclize_getPrice(string datasource) internal oraclizeAPI returns (uint){
        return oraclize.getPrice(datasource);
    }

    function oraclize_getPrice(string datasource, uint gaslimit) internal oraclizeAPI returns (uint){
        return oraclize.getPrice(datasource, gaslimit);
    }

    function oraclize_query(string datasource, bytes[] argN, uint gaslimit) internal oraclizeAPI returns (bytes32 id){
        uint price = oraclize.getPrice(datasource, gaslimit);
        if (price > 1 ether + tx.gasprice*gaslimit) return 0; // unexpectedly high price
        bytes memory args = ba2cbor(argN);
        return oraclize.queryN_withGasLimit.value(price)(0, datasource, args, gaslimit);
    }

    function oraclize_query(string datasource, bytes[4] args, uint gaslimit) internal oraclizeAPI returns (bytes32 id) {
        bytes[] memory dynargs = new bytes[](4);
        dynargs[0] = args[0];
        dynargs[1] = args[1];
        dynargs[2] = args[2];
        dynargs[3] = args[3];
        return oraclize_query(datasource, dynargs, gaslimit);
    }

    function oraclize_setProof(byte proofP) internal oraclizeAPI {
        return oraclize.setProofType(proofP);
    }
    function oraclize_setCustomGasPrice(uint gasPrice) internal oraclizeAPI {
        return oraclize.setCustomGasPrice(gasPrice);
    }

    function oraclize_randomDS_getSessionPubKeyHash() internal oraclizeAPI returns (bytes32){
        return oraclize.randomDS_getSessionPubKeyHash();
    }

    function getCodeSize(address _addr) internal view returns(uint _size) {
        assembly {
            _size := extcodesize(_addr)
        }
    }

    using CBOR for Buffer.buffer;

    function ba2cbor(bytes[] arr) internal pure returns (bytes) {
        safeMemoryCleaner();
        Buffer.buffer memory buf;
        Buffer.init(buf, 1024);
        buf.startArray();
        for (uint i = 0; i < arr.length; i++) {
            buf.encodeBytes(arr[i]);
        }
        buf.endSequence();
        return buf.buf;
    }

    string oraclize_network_name;
    function oraclize_setNetworkName(string _network_name) internal {
        oraclize_network_name = _network_name;
    }

    function oraclize_getNetworkName() internal view returns (string) {
        return oraclize_network_name;
    }

    // solium-disable security/no-assign-params
    function oraclize_newRandomDSQuery(uint _delay, uint _nbytes, uint _customGasLimit) internal returns (bytes32) {
        require((_nbytes > 0) && (_nbytes <= 32), "Requested bytes out of range!");
        // Convert from seconds to ledger timer ticks
        _delay *= 10;
        bytes memory nbytes = new bytes(1);
        nbytes[0] = byte(_nbytes);
        bytes memory unonce = new bytes(32);
        bytes memory sessionKeyHash = new bytes(32);
        bytes32 sessionKeyHash_bytes32 = oraclize_randomDS_getSessionPubKeyHash();
        assembly {
            
            mstore(unonce, 0x20)
            mstore(add(unonce, 0x20), xor(blockhash(sub(number, 1)), xor(caller, callvalue)))
            mstore(sessionKeyHash, 0x20)
            mstore(add(sessionKeyHash, 0x20), sessionKeyHash_bytes32)
        }
        bytes memory delay = new bytes(32);
        assembly {
            mstore(add(delay, 0x20), _delay)
        }

        bytes memory delay_bytes8 = new bytes(8);
        copyBytes(delay, 24, 8, delay_bytes8, 0);

        bytes[4] memory args = [unonce, nbytes, sessionKeyHash, delay];
        bytes32 queryId = oraclize_query("random", args, _customGasLimit);

        bytes memory delay_bytes8_left = new bytes(8);

        assembly {
            let x := mload(add(delay_bytes8, 0x20))
            mstore8(add(delay_bytes8_left, 0x27), div(x, 0x100000000000000000000000000000000000000000000000000000000000000))
            mstore8(add(delay_bytes8_left, 0x26), div(x, 0x1000000000000000000000000000000000000000000000000000000000000))
            mstore8(add(delay_bytes8_left, 0x25), div(x, 0x10000000000000000000000000000000000000000000000000000000000))
            mstore8(add(delay_bytes8_left, 0x24), div(x, 0x100000000000000000000000000000000000000000000000000000000))
            mstore8(add(delay_bytes8_left, 0x23), div(x, 0x1000000000000000000000000000000000000000000000000000000))
            mstore8(add(delay_bytes8_left, 0x22), div(x, 0x10000000000000000000000000000000000000000000000000000))
            mstore8(add(delay_bytes8_left, 0x21), div(x, 0x100000000000000000000000000000000000000000000000000))
            mstore8(add(delay_bytes8_left, 0x20), div(x, 0x1000000000000000000000000000000000000000000000000))

        }

        oraclize_randomDS_setCommitment(queryId, keccak256(abi.encodePacked(delay_bytes8_left, args[1], sha256(args[0]), args[2])));
        return queryId;
    }

    // New function with 0 delay
    function oraclize_newRandomDSQuery(uint _nbytes, uint _customGasLimit) internal returns (bytes32){
        require((_nbytes > 0) && (_nbytes <= 32), "Requested bytes out of range!");
        // Convert from seconds to ledger timer ticks
        
        bytes memory nbytes = new bytes(1);
        nbytes[0] = byte(_nbytes);
        bytes memory unonce = new bytes(32);
        bytes memory sessionKeyHash = new bytes(32);
        bytes32 sessionKeyHash_bytes32 = oraclize_randomDS_getSessionPubKeyHash();
        assembly {
            mstore(unonce, 0x20)
            // Slightly different commitment
            mstore(add(unonce, 0x20), xor(blockhash(sub(number, 1)), xor(caller, callvalue)))
            mstore(sessionKeyHash, 0x20)
            mstore(add(sessionKeyHash, 0x20), sessionKeyHash_bytes32)
        }
        bytes memory delay = new bytes(32);
        
        bytes[4] memory args = [unonce, nbytes, sessionKeyHash, delay];
        bytes32 queryId = oraclize_query("random", args, _customGasLimit);

        bytes memory delay_bytes8_left = new bytes(8);

        oraclize_randomDS_setCommitment(queryId, keccak256(abi.encodePacked(delay_bytes8_left, args[1], sha256(args[0]), args[2])));
        return queryId;
    }

    function oraclize_randomDS_setCommitment(bytes32 queryId, bytes32 commitment) internal {
        oraclize_randomDS_args[queryId] = commitment;
    }

    mapping(bytes32=>bytes32) oraclize_randomDS_args;
    mapping(bytes32=>bool) oraclize_randomDS_sessionKeysHashVerified;

    function verifySig(bytes32 tosignh, bytes dersig, bytes pubkey) internal returns (bool){
        bool sigok;
        address signer;

        bytes32 sigr;
        bytes32 sigs;

        bytes memory sigr_ = new bytes(32);
        uint offset = 4+(uint(dersig[3]) - 0x20);
        sigr_ = copyBytes(dersig, offset, 32, sigr_, 0);
        bytes memory sigs_ = new bytes(32);
        offset += 32 + 2;
        sigs_ = copyBytes(dersig, offset+(uint(dersig[offset-1]) - 0x20), 32, sigs_, 0);

        assembly {
            sigr := mload(add(sigr_, 32))
            sigs := mload(add(sigs_, 32))
        }


        (sigok, signer) = safer_ecrecover(tosignh, 27, sigr, sigs);
        if (address(keccak256(pubkey)) == signer) return true;
        else {
            (sigok, signer) = safer_ecrecover(tosignh, 28, sigr, sigs);
            return (address(keccak256(pubkey)) == signer);
        }
    }

    function oraclize_randomDS_proofVerify__sessionKeyValidity(bytes proof, uint sig2offset) internal returns (bool) {
        bool sigok;

        // Step 6: verify the attestation signature, APPKEY1 must sign the sessionKey from the correct ledger app (CODEHASH)
        bytes memory sig2 = new bytes(uint(proof[sig2offset+1])+2);
        copyBytes(proof, sig2offset, sig2.length, sig2, 0);

        bytes memory appkey1_pubkey = new bytes(64);
        copyBytes(proof, 3+1, 64, appkey1_pubkey, 0);

        bytes memory tosign2 = new bytes(1+65+32);
        tosign2[0] = byte(1); //role
        copyBytes(proof, sig2offset-65, 65, tosign2, 1);
        bytes memory CODEHASH = hex"fd94fa71bc0ba10d39d464d0d8f465efeef0a2764e3887fcc9df41ded20f505c";
        copyBytes(CODEHASH, 0, 32, tosign2, 1+65);
        sigok = verifySig(sha256(tosign2), sig2, appkey1_pubkey);

        if (sigok == false) return false;


        // Step 7: verify the APPKEY1 provenance (must be signed by Ledger)
        // solium-disable max-len
        bytes memory LEDGERKEY = hex"7fb956469c5c9b89840d55b43537e66a98dd4811ea0a27224272c2e5622911e8537a2f8e86a46baec82864e98dd01e9ccc2f8bc5dfc9cbe5a91a290498dd96e4";

        bytes memory tosign3 = new bytes(1+65);
        tosign3[0] = 0xFE;
        copyBytes(proof, 3, 65, tosign3, 1);

        bytes memory sig3 = new bytes(uint(proof[3+65+1])+2);
        copyBytes(proof, 3+65, sig3.length, sig3, 0);

        sigok = verifySig(sha256(tosign3), sig3, LEDGERKEY);

        return sigok;
    }

    modifier oraclize_randomDS_proofVerify(bytes32 _queryId, string _result, bytes _proof) {
        // Step 1: the prefix has to match 'LP\x01' (Ledger Proof version 1)
        require((_proof[0] == "L") && (_proof[1] == "P") && (_proof[2] == 1));

        bool proofVerified = oraclize_randomDS_proofVerify__main(_proof, _queryId, bytes(_result), oraclize_getNetworkName());
        require(proofVerified);

        _;
    }

    function oraclize_randomDS_proofVerify__returnCode(bytes32 _queryId, string _result, bytes _proof) internal returns (uint8){
        // Step 1: the prefix has to match 'LP\x01' (Ledger Proof version 1)
        if ((_proof[0] != "L")||(_proof[1] != "P")||(_proof[2] != 1)) return 1;

        bool proofVerified = oraclize_randomDS_proofVerify__main(_proof, _queryId, bytes(_result), oraclize_getNetworkName());
        if (proofVerified == false) return 2;

        return 0;
    }

    function matchBytes32Prefix(bytes32 content, bytes prefix, uint n_random_bytes) internal pure returns (bool){
        bool match_ = true;

        require(prefix.length == n_random_bytes);

        for (uint256 i=0; i< n_random_bytes; i++) {
            if (content[i] != prefix[i]) match_ = false;
        }

        return match_;
    }

    function oraclize_randomDS_proofVerify__main(bytes proof, bytes32 queryId, bytes result, string context_name) internal returns (bool){

        // Step 2: the unique keyhash has to match with the sha256 of (context name + queryId)
        uint ledgerProofLength = 3+65+(uint(proof[3+65+1])+2)+32;
        bytes memory keyhash = new bytes(32);
        copyBytes(proof, ledgerProofLength, 32, keyhash, 0);
        if (!(keccak256(keyhash) == keccak256(abi.encodePacked(sha256(abi.encodePacked(context_name, queryId)))))) return false;

        bytes memory sig1 = new bytes(uint(proof[ledgerProofLength+(32+8+1+32)+1])+2);
        copyBytes(proof, ledgerProofLength+(32+8+1+32), sig1.length, sig1, 0);

        // Step 3: we assume sig1 is valid (it will be verified during step 5) and we verify if 'result' is the prefix of sha256(sig1)
        if (!matchBytes32Prefix(sha256(sig1), result, uint(proof[ledgerProofLength+32+8]))) return false;

        // Step 4: commitment match verification, keccak256(delay, nbytes, unonce, sessionKeyHash) == commitment in storage.
        // This is to verify that the computed args match with the ones specified in the query.
        bytes memory commitmentSlice1 = new bytes(8+1+32);
        copyBytes(proof, ledgerProofLength+32, 8+1+32, commitmentSlice1, 0);

        bytes memory sessionPubkey = new bytes(64);
        uint sig2offset = ledgerProofLength+32+(8+1+32)+sig1.length+65;
        copyBytes(proof, sig2offset-64, 64, sessionPubkey, 0);

        bytes32 sessionPubkeyHash = sha256(sessionPubkey);
        if (oraclize_randomDS_args[queryId] == keccak256(abi.encodePacked(commitmentSlice1, sessionPubkeyHash))){ //unonce, nbytes and sessionKeyHash match
            delete oraclize_randomDS_args[queryId];
        } else return false;


        // Step 5: validity verification for sig1 (keyhash and args signed with the sessionKey)
        bytes memory tosign1 = new bytes(32+8+1+32);
        copyBytes(proof, ledgerProofLength, 32+8+1+32, tosign1, 0);
        if (!verifySig(sha256(tosign1), sig1, sessionPubkey)) return false;

        // verify if sessionPubkeyHash was verified already, if not.. let's do it!
        if (oraclize_randomDS_sessionKeysHashVerified[sessionPubkeyHash] == false){
            oraclize_randomDS_sessionKeysHashVerified[sessionPubkeyHash] = oraclize_randomDS_proofVerify__sessionKeyValidity(proof, sig2offset);
        }

        return oraclize_randomDS_sessionKeysHashVerified[sessionPubkeyHash];
    }

    // the following function has been written by Alex Beregszaszi (@axic), use it under the terms of the MIT license
    function copyBytes(bytes from, uint fromOffset, uint length, bytes to, uint toOffset) internal pure returns (bytes) {
        uint minLength = length + toOffset;

        // Buffer too small
        require(to.length >= minLength); // Should be a better way?

        // NOTE: the offset 32 is added to skip the `size` field of both bytes variables
        uint i = 32 + fromOffset;
        uint j = 32 + toOffset;

        while (i < (32 + fromOffset + length)) {
            assembly {
                let tmp := mload(add(from, i))
                mstore(add(to, j), tmp)
            }
            i += 32;
            j += 32;
        }

        return to;
    }

    // the following function has been written by Alex Beregszaszi (@axic), use it under the terms of the MIT license
    // Duplicate Solidity's ecrecover, but catching the CALL return value
    function safer_ecrecover(bytes32 hash, uint8 v, bytes32 r, bytes32 s) internal returns (bool, address) {
        // We do our own memory management here. Solidity uses memory offset
        // 0x40 to store the current end of memory. We write past it (as
        // writes are memory extensions), but don't update the offset so
        // Solidity will reuse it. The memory used here is only needed for
        // this context.

        // FIXME: inline assembly can't access return values
        bool ret;
        address addr;

        assembly {
            let size := mload(0x40)
            mstore(size, hash)
            mstore(add(size, 32), v)
            mstore(add(size, 64), r)
            mstore(add(size, 96), s)

            // NOTE: we can reuse the request memory because we deal with
            //       the return code
            ret := call(3000, 1, 0, size, 128, size, 32)
            addr := mload(size)
        }

        return (ret, addr);
    }

    // the following function has been written by Alex Beregszaszi (@axic), use it under the terms of the MIT license
    function ecrecovery(bytes32 hash, bytes sig) internal returns (bool, address) {
        bytes32 r;
        bytes32 s;
        uint8 v;

        if (sig.length != 65)
          return (false, 0);

        // The signature format is a compact form of:
        //   {bytes32 r}{bytes32 s}{uint8 v}
        // Compact means, uint8 is not padded to 32 bytes.
        assembly {
            r := mload(add(sig, 32))
            s := mload(add(sig, 64))

            // Here we are loading the last 32 bytes. We exploit the fact that
            // 'mload' will pad with zeroes if we overread.
            // There is no 'mload8' to do this, but that would be nicer.
            v := byte(0, mload(add(sig, 96)))

            // Alternative solution:
            // 'byte' is not working due to the Solidity parser, so lets
            // use the second best option, 'and'
            // v := and(mload(add(sig, 65)), 255)
        }

        // albeit non-transactional signatures are not specified by the YP, one would expect it
        // to match the YP range of [27, 28]
        //
        // geth uses [0, 1] and some clients have followed. This might change, see:
        //  https://github.com/ethereum/go-ethereum/issues/2053
        if (v < 27)
          v += 27;

        if (v != 27 && v != 28)
            return (false, 0);

        return safer_ecrecover(hash, v, r, s);
    }

    function safeMemoryCleaner() internal pure {
        assembly {
            let fmem := mload(0x40)
            codecopy(fmem, codesize, sub(msize, fmem))
        }
    }

}
// </ORACLIZE_API>

contract UsingOraclizeRandom is usingOraclize {
    uint public oraclizeCallbackGas = 200000;
    uint public oraclizeGasPrice = 20000000000; //20 gwei

    constructor() public {
        a = DSA(0xdbf98a75f521Cb1BD421c03F2b6A6a617f4240F1);
    }

    // function _requestRandom(uint delay) internal returns(bytes32 qID);

    // function _onRandom(uint _rand, bytes32 _queryId) internal;

    // function _onRandomFailed(bytes32 _queryId) internal;

    function setOraclizeGasLimit(uint _newLimit) public auth {
        oraclizeCallbackGas = _newLimit;
    }

    function setOraclizeGasPrice(uint _newGasPrice) public auth {
        oraclizeGasPrice = _newGasPrice;
        oraclize_setCustomGasPrice(_newGasPrice);
    }

}

interface MobiusToken {
    function disburseDividends() external payable;
    function approve(address, uint) external returns (bool);
    function transfer(address, uint) external returns (bool);
    function transferFrom(address, address, uint) external returns (bool);
}

contract MobiusRandom is UsingOraclizeRandom, DSMath {
    
    // Constants
    uint24 constant public SECONDAY_MODULO = 1000000;// 1 million
    uint16 constant public MAX_UNDER2 = 150;
    uint constant public BET_EXPIRY = 6 hours;
    uint constant public HOUSE_EDGE_DIVISOR = 70; //1.43% in Mobius mode
    uint constant public HOUSE_EDGE_DIVISOR_CLASSIC = 125;//0.8% in classic mode
    uint constant public SECONDARY_JACKPOT_DIVISOR = 400;//0.25% in Mobius mode, only if you are eligible for the jackpot
    uint constant public UNLUCK_RATE = 1 * WAD / 6 hours;   // 1 level decline per 6 hours
    MobiusToken constant public TOKEN = MobiusToken(0x54cdC9D889c28f55F59f6b136822868c7d4726fC);
    
    // Accounting
    uint public pendingBets;
    uint public secondaryPot;

    // Other parameters - note that these can be changed
    uint public minHouse = 0.4 finney;
    uint public minHouseClassic = 1.5 finney;
    uint public minSecondaryAmount = 100 finney;
    uint public maxProfit = 2 ether;
    uint public luckPrice = 10 * WAD;   // 10 tokens per level

    // Stats:
    uint public dividendsPaid;

    struct Bet {
        uint props;
        address player;
    }

    mapping(bytes32 => Bet) public bets;
    mapping(address => uint) public luck;

    event BetPlaced(bytes32 queryId, address indexed player, uint props);
    event BetFinalized(bytes32 queryId, address indexed player, uint props, uint amountToSend);
    event SecondaryJackpotWon(bytes32 queryId, address indexed player, uint amount);
    event FailedPayment(bytes32 queryId, uint amount);
    event RefundAttempt(bytes32 queryId);
    event RandomFailed(bytes32 queryId);

    constructor() public {
        
    }

    function () public payable {
        // used to top up bankroll
    }

    function placeBet(uint16 modulo, uint16 rollUnder, bool classic) external payable {
        bytes32 queryId = oraclize_newRandomDSQuery(32, oraclizeCallbackGas);
        address player = msg.sender;
        uint128 amount = uint128(msg.value);
        uint props;
        // Props is a single storage variable that stores multiple properties of the bet to save gas:
        // | amount (128 bits) | timestamp (64 bits) | modulo (16 bits) | roll under (16 bits)| roll under 2 (16 bits) | classic (16bits) |
        require(_getBetAmount(queryId) == 0, "Invalid query ID!");
        
        // Set storage
        Bet storage newBet = bets[queryId];
        newBet.player = player;
        props = amount;
        props = props << 64;
        // solium-disable security/no-block-members
        props |= uint64(now);        
        props = props << 16;
        props |= modulo;
        props = props << 16;
        props |= rollUnder;
        props = props << 16; 

        uint win;
        uint jackpotFee;
        (win,jackpotFee) = _winAmount(amount, modulo, rollUnder, classic);
        require(win <= amount + maxProfit, "Potential profit exceeds maximum!");

        if(!classic) {
            if(amount >= minSecondaryAmount) {
                uint lucky = getLuck(player);
                props |= uint16(min(MAX_UNDER2, 1 + (lucky * 25) / WAD));
                secondaryPot += jackpotFee;
            }
            // if you play in Mobius mode, any bet you place tops up your luck, 
            // even if you aren't eligible for the jackpot
            _addLuck(player, amount);
        }
        props = props << 16;
        props |= uint16(classic ? 1 : 0);
        newBet.props = props;

        pendingBets += win;

        require(secondaryPot + pendingBets <= address(this).balance, "Can't cover bet!");
        emit BetPlaced(queryId, player, props);
    }

    function topUpLuck(uint level) external {
        address player = msg.sender;
        require(level <= 4 * WAD, "Can't top up more than 4 levels!");
        require(TOKEN.transferFrom(player, address(this), wmul(level, luckPrice)), "Token transfer failed!");
        _addLuck(player, level);
    }

    function refundExpiredBet(bytes32 queryId) external {

        require(_getBetTimestamp(queryId) + BET_EXPIRY < now, "Bet not expired!");
        require(_getBetAmount(queryId) > 0, "Bet invalid!");

        _processRefund(queryId);
    }

    /// Dev only functions

    function initOraclize() external auth {
        oraclizeCallbackGas = 200000;
        if(oraclize_setNetwork()){
            oraclize_setProof(proofType_Ledger);
        }
    }

    function setMinHouse(uint newValue) external auth {
        minHouse = newValue;
    }
    
    function setMinHouseClassic(uint newValue) external auth {
        minHouseClassic = newValue;
    }

    function setMinSecondaryAmount(uint newValue) external auth {
        minSecondaryAmount = newValue;
    }

    function setLuckPrice(uint newValue) external auth {
        luckPrice = newValue;
    }

    function setMaxProfit(uint newValue) external auth {
        maxProfit = newValue;
    }

    function destroy() external auth {
        // Only check if there are less than 0.1 ETH pending bets, to allow for some rounding errors and
        // discrepancies from changing min house profit.
        require (pendingBets < 100 finney, "There are pending bets!");
        selfdestruct(msg.sender);
    }

    function feedSecondaryPot(uint amount) external auth {
        require (amount <= address(this).balance, "Nonsense amount!");
        require (secondaryPot + pendingBets + amount <= address(this).balance, "Can't use what you don't own!");
        secondaryPot += amount;
    }

    function withdraw(uint amount) external auth {
        require (amount <= address(this).balance, "Nonsense amount!"); // do this check to make sure we don't overflow
        require (secondaryPot + pendingBets + amount <= address(this).balance, "Can't withdraw what you don't own!");
        msg.sender.transfer(amount);
    }

    function withdrawTokens(address to, uint amount) external auth {
        require(TOKEN.transfer(to, amount), "Token transfer failed!");
    }

    function disburseDividends(uint amount) external auth {
        require (amount <= address(this).balance, "Nonsense amount!"); // do this check to make sure we don't overflow
        require (secondaryPot + pendingBets + amount <= address(this).balance, "Can't send what you don't own!");
        TOKEN.disburseDividends.value(amount)();
        dividendsPaid += amount;
    }

    /// Oraclize callback function
    function __callback(bytes32 _queryId, string _result, bytes _proof) public {
        
        require(msg.sender == oraclize_cbAddress(), "You can't do that!");
        
        if (oraclize_randomDS_proofVerify__returnCode(_queryId, _result, _proof) != 0) {
            _onRandomFailed(_queryId);
        } else {
            uint randomNumber = uint(keccak256(abi.encode(_result)));
            _onRandom(randomNumber, _queryId);
        }
    }

    function getLuck(address player) public view returns(uint) {
        uint lastTime;
        uint lastLvl;
        (lastLvl, lastTime) = getLuckProps(player);
        uint elapsed = (now - lastTime) * UNLUCK_RATE;
        if(lastLvl > elapsed) {
            return lastLvl - elapsed;
        } else {
            return 0;
        }
    }

    function getContractProps() public view 
    returns(
        uint secondaryPot_,
        uint minHouseEdge,
        uint minHouseEdgeClassic,
        uint maxProfit_,
        uint luckPrice_
    ) {
        secondaryPot_ = secondaryPot;
        minHouseEdge = _minHouseEdge();
        minHouseEdgeClassic = _minHouseEdgeClassic();
        maxProfit_ = maxProfit;
        luckPrice_ = luckPrice;
    }

    function getBetProps(bytes32 queryId) public view 
    returns(
        uint128 amount,
        uint64 time,
        uint16 modulo,
        uint16 rollUnder,
        uint16 rollUnder2,
        bool classicMode
    ) {
        uint props = bets[queryId].props;
        return decodeProps(props);
    }

    function decodeProps(uint props) public pure 
    returns(
        uint128 amount,
        uint64 time,
        uint16 modulo,
        uint16 rollUnder,
        uint16 rollUnder2,
        bool classicMode
    ) {
        classicMode = uint16(props) == 1 ? true : false;
        rollUnder2 = uint16(props >> 16);
        rollUnder = uint16(props >> 32);
        modulo = uint16(props >> 48);
        time = uint64(props >> 64);
        amount = uint128(props >> 128);
    }

    function getLuckProps(address player) public view returns(uint128 lastLevel, uint64 lastToppedUp) {
        uint props = luck[player];
        // Luck is another single storage variable that stores two effective values
        // | Level (128 bits) | last topped up timestamp (64 bits) |
        lastToppedUp = uint64(props);
        lastLevel = uint128(props >> 64);
    }

    /// Internal Functions

    function _addLuck(address player, uint amount) internal {
        // Luck can be level 5 at most
        uint props = min(getLuck(player) + amount, 5 * WAD);
        props = props << 64;
        props |= uint64(now);
        luck[player] = props;
    }

    function _onRandom(uint _rand, bytes32 queryId) internal {
        Bet storage bet = bets[queryId];
        address player = bet.player;
        uint props = bet.props;
        uint128 amount;
        uint64 time;
        uint16 modulo;
        uint16 rollUnder;
        uint16 rollUnder2;
        bool classicMode;

        (amount, time, modulo, rollUnder, rollUnder2, classicMode) = decodeProps(props);

        require(time + BET_EXPIRY >= now, "Bet already expired");
        require(amount > 0, "Invalid query ID!");
        
        uint win; // Potential win
        uint won; // Actually won
        (win,) = _winAmount(amount, modulo, rollUnder, classicMode);

        if(uint16(_rand) % modulo < rollUnder) {
            won += win;
        }

        if(!classicMode) {
            if(amount >= minSecondaryAmount) {           
                if(uint16(_rand >> 16) % SECONDAY_MODULO < rollUnder2) {
                    won += secondaryPot / 2;
                    secondaryPot /= 2;
                    emit SecondaryJackpotWon(queryId, player, secondaryPot);
                }
            }
        }

        _finalizeBet(queryId, player, props, won);

        // Since we can change the min house profit while there are pending bets,
        // there's a small chance of an underflow, so check if that's the case here
        if(pendingBets >= win) {
            pendingBets -= win;
        } else {
            pendingBets = 0;
        }
    }

    function _finalizeBet(bytes32 queryId, address player, uint props, uint amountToSend) internal {
        uint _props = props << 128;
        bets[queryId].props = _props >> 128;
        if(amountToSend > 0) {
            // solium-disable security/no-send
            if(!player.send(amountToSend)) {
                emit FailedPayment(queryId, amountToSend);
            }            
        }
        emit BetFinalized(queryId, player, props, amountToSend);
    }

    function _processRefund(bytes32 queryId) internal {    
        emit RefundAttempt(queryId); 
        uint props = bets[queryId].props;
        uint128 amount;
        uint16 modulo;
        uint16 rollUnder;
        bool classicMode;

        (amount, , modulo, rollUnder, , classicMode) = decodeProps(props);

        uint win;
        (win,) = _winAmount(amount, modulo, rollUnder, classicMode);

        _finalizeBet(queryId, bets[queryId].player, props, amount);// refund full bet amount
        
        // And here
        if(pendingBets >= win) {
            pendingBets -= win;
        } else {
            pendingBets = 0;
        }
    }

    function _onRandomFailed(bytes32 queryId) internal {
        emit RandomFailed(queryId);
        _processRefund(queryId);
    }

    function _winAmount(uint128 betSize, uint16 modulo, uint16 rollUnder, bool classic) 
    internal 
    view 
    returns(uint reward, uint secondaryJackpotFee){
        require(rollUnder > 0 && rollUnder <= modulo, "Nonsense bet!");
       
        uint houseEdge;
        if(!classic) {
            houseEdge = max(betSize / HOUSE_EDGE_DIVISOR, _minHouseEdge());
            if(betSize >= minSecondaryAmount){
                secondaryJackpotFee = betSize / SECONDARY_JACKPOT_DIVISOR;
            }            
        } else {
            houseEdge = max(betSize / HOUSE_EDGE_DIVISOR_CLASSIC, _minHouseEdgeClassic());
        }

        reward = (betSize - houseEdge - secondaryJackpotFee) * modulo / rollUnder;
        require(betSize >= houseEdge + secondaryJackpotFee, "Bet doesn't cover minimum fee!");
    }

    function _minHouseEdge() internal view returns(uint) {
        return oraclizeGasPrice * oraclizeCallbackGas + minHouse;
    }

    function _minHouseEdgeClassic() internal view returns(uint) {
        return oraclizeGasPrice * oraclizeCallbackGas + minHouseClassic;
    }

    function _getBetAmount(bytes32 queryId) internal view returns(uint128 amount) {
        uint props = bets[queryId].props;
        amount = uint128(props >> 128);
    }

    function _getBetTimestamp(bytes32 queryId) internal view returns(uint64 timestamp) {
        uint props = bets[queryId].props;
        timestamp = uint64(props >> 64);
    }
}