예제 #1
0
def _execute_deposit(base_url, deposit, priv_key_wif):
    """This is the second endpoint required to execute a deposit. After using the Create Deposit endpoint,
    you will receive a response which requires additional signing.
    The signature should then be attached as the signature parameter in the request payload.

    Note that a sha256 parameter is provided for convenience to be used directly as part of the ECDSA signature
    process. In production mode, this should be recalculated for additional security.

    Args:
        base_url (str)     : This paramter governs whether to connect to test or mainnet..
        deposit (json)     : The correct json response returned from create_deposit function (json.loads)
        priv_key_wif (str) : The private key WIF of the user.

    Returns:
        If response from the server is HTTP_OK (200) then this returns the requests.response object

    """
    logger.debug("Deposit transaction is {0}".format(
        utils.jsonify(deposit["transaction"])))

    pk = get_private_key_from_wif(priv_key_wif)
    # signature is Signed response from create deposit endpoint.
    # signable_params_json_str = utils.jsonify(deposit["transaction"])

    signature = sign_msg(serialize_transaction(deposit["transaction"], False),
                         pk)

    url = utils.format_urls(base_url,
                            deposits.EXECUTE_DEPOSIT.format(id=deposit["id"]))
    resp = requests.post(url, json={"signature": signature})
    return utils.response_else_exception(resp)
예제 #2
0
def _get_contract_tokens_info(base_url):
    """Fetch updated hashes of contracts deployed by Switcheo along with their precision.

    Args:
        base_url (str): This paramter governs whether to connect to test or mainnet.

    Returns:
        If response from the server is HTTP_OK (200) then this returns the requests.response object

        Example response
        {
          "NEO": {
            "hash": "c56f33fc6ecfcd0c225c4ab356fee59390af8560be0e930faebe74a6daff7c9b",
            "decimals": 8
          },
          "GAS": {
            "hash": "602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de7",
            "decimals": 8
          },
          "SWTH": {
            "hash": "ab38352559b8b203bde5fddfa0b07d8b2525e132",
            "decimals": 8
          },
          ...
        }
    """
    url = utils.format_urls(base_url, exchange.GET_TOKENS)
    resp = requests.get(url)
    return utils.response_else_exception(resp)
예제 #3
0
def _list_offers(base_url, blockchain, pair, contract_hash):
    """Retrieves the best 70 offers (per side) on the offer book.

    Args:
        base_url (str): This paramter governs whether to connect to test or mainnet..
        blockchain (str)   : Only return offers from this blockchain. Possible values are neo.
        pair (str)         : Only return offers from this pair, for eg. SWTH_NEO
        contract_hash (str): Only return offers for the contract hash. e.g. eed0d2e14b0027f5f30ade45f2b23dc57dd54ad2

    Returns:
        If response from the server is HTTP_OK (200) then this returns the requests.response object

        Example response:
        [
            {
             "id": "b3a91e19-3726-4d09-8488-7c22eca76fc0",
             "offer_asset": "SWTH",
             "want_asset": "NEO",
             "available_amount": 2550000013,
             "offer_amount": 4000000000,
             "want_amount": 320000000
            }
        ]
    """
    params = {
        "blockchain": blockchain,
        "pair": pair,
        "contract_hash": contract_hash
    }
    url = utils.format_urls(base_url, LIST_OFFERS)
    resp = requests.get(url, params=params)
    return utils.response_else_exception(resp)
예제 #4
0
def _execute_withdrawal(base_url, withdrawal, priv_key_wif):
    """This is the second endpoint required to execute a withdrawal.

    After using the Create Withdrawal endpoint, you will receive a response which requires additional signing.

    Note that a sha256 parameter is provided for convenience to be used directly as part of the ECDSA signature
    process. In production mode, this should be recalculated for additional security.

    Args:
        base_url (str)    : This paramter governs whether to connect to test or mainnet.
        withdrawal (dict) : Withdrawal is the json object returned after executing create_withdrawal end point.
        priv_key_wif (str)    : The private key of the user. (wif)

    Returns:
        If response from the server is HTTP_OK (200) then this returns the requests.response object

    """
    logger.debug("Withdrawal invoke transaction is {0}".format(utils.jsonify(withdrawal)))

    pk = get_private_key_from_wif(priv_key_wif)

    signable_params = {
        "id": withdrawal["id"],
        "timestamp": utils.get_current_epoch_milli()
    }
    signable_params_json_str = utils.jsonify(signable_params)
    encoded_signable_params = encode_msg(signable_params_json_str)
    signature = sign_msg(encoded_signable_params, pk)

    params = {"signature": signature, **signable_params}
    logger.debug("Final params being sent to execute withdrawal {0}".format(params))
    url = utils.format_urls(base_url, withdrawals.EXECUTE_WITHDRAWAL.format(id=withdrawal["id"]))
    resp = requests.post(url, json=params)
    return utils.response_else_exception(resp)
예제 #5
0
def _get_last_twenty_four_hours(base_url):
    """Get 24-hour data for all pairs and markets.

    Args:
        base_url (str)  : This paramter governs whether to connect to test or mainnet.
    Returns:
        If response from the server is HTTP_OK (200) then this returns the requests.response object

    """
    url = utils.format_urls(base_url, tickers.LAST_TWENTY_FOUR_HOURS)
    return __get_prices(url)
예제 #6
0
def _get_last_price(base_url, symbols=None):
    """Get last price of given symbol(s). Defaults to all symbols.

    Args:
        base_url (str)       : This paramter governs whether to connect to test or mainnet..
        symbols (list[str])  : Return the price for these symbols for eg. ["SWTH"]

    Returns:
        If response from the server is HTTP_OK (200) then this returns the requests.response object

    """
    url = utils.format_urls(base_url, tickers.LAST_PRICE)
    return __get_prices(url, symbols)
예제 #7
0
def _create_deposit(base_url,
                    priv_key_wif,
                    asset_id,
                    amount,
                    contract_hash,
                    blockchain="NEO"):
    """This endpoint creates a deposit which can be executed through Execute Deposit.

    To be able to make a deposit, sufficient funds are required in the depositing wallet.
    NOTE: After calling this endpoint, the Execute Deposit endpoint has to be called for the deposit to be executed.

    Args:
        base_url (str)      : This paramter governs whether to connect to test or mainnet..
        priv_key_wif (str)  : The private key wif of the user.
        asset_id (str)      : The asset symbol or ID to deposit. for eg. SWTH
        amount (int)        : Amount of tokens to deposit.
        contract_hash (str) : Switcheo Exchange contract hash to execute the deposit on.
        blockchain (str)    : Blockchain that the token to deposit is on. Possible values are: neo.

    Returns:
        If response from the server is HTTP_OK (200) then this returns the requests.response object
        Check schemas.CREATE_DEPOSIT_SCHEMA
    """

    signable_params = {
        "blockchain": blockchain,
        "asset_id": asset_id,
        "amount": utils.convert_to_neo_asset_amount(amount, asset_id,
                                                    base_url),
        "timestamp": utils.get_current_epoch_milli(),
        "contract_hash": contract_hash,
    }
    signable_params_json_str = utils.jsonify(signable_params)

    # The depositer's address. Do not include this in the parameters to be signed.
    # This needs to be the script hash of the public key
    script_hash = get_script_hash_from_wif(priv_key_wif)

    pk = get_private_key_from_wif(priv_key_wif)
    encoded_msg = encode_msg(signable_params_json_str)
    signature = sign_msg(encoded_msg, pk)

    params = {
        **signable_params, "signature": signature,
        "address": script_hash
    }

    logger.debug("Params being sent to create deposit: {0}".format(params))
    url = utils.format_urls(base_url, deposits.CREATE_DEPOSIT)
    resp = requests.post(url, json=params)
    return utils.response_else_exception(resp)
예제 #8
0
def _list_trades(base_url,
                 contract_hash,
                 pair,
                 from_time=None,
                 to_time=None,
                 limit=None):
    """Retrieve trades that have already occurred on Switcheo Exchange filtered by the request parameters.

    Args:
        base_url (str): This paramter governs whether to connect to test or mainnet.
        contract_hash (str) : Only return trades for this contract hash.
        pair (str)          : Only return trades for this pair.
        from_time (int)     : Only return trades after this time in epoch seconds.
        to_time (int)       : Only return trades before this time in epoch seconds.
        limit (int)         : Only return this number of trades (min: 1, max: 10000, default: 5000).

    Returns:
        If response from the server is HTTP_OK (200) then this returns the requests.response object

        Example response:
        [
          {
            "id": "712a5019-3a23-463e-b0e1-80e9f0ad4f91",
            "fill_amount": 9122032316,
            "take_amount": 20921746,
            "event_time": "2018-06-08T11:32:03.219Z",
            "is_buy": false
          },
          {
            "id": "5d7e42a2-a8f3-40a9-bce5-7304921ff691",
            "fill_amount": 280477933,
            "take_amount": 4207169,
            "event_time": "2018-06-08T11:31:42.200Z",
            "is_buy": false
          },
          ...
        ]
    """
    url = utils.format_urls(base_url, trades.LIST_TRADES)
    params = {
        "contract_hash": contract_hash,
        "pair": pair,
        "from": from_time,
        "to": to_time,
        "limit": limit,
    }
    resp = requests.get(url, params=params)
    return utils.response_else_exception(resp)
예제 #9
0
def _get_candle_sticks(base_url, pair, start_time, end_time, interval):
    """Get candlestick chart data filtered by url parameters.

    Args:
        base_url (str)     : This paramter governs whether to connect to test or mainnet.
        pair (str)	       : Show chart data of this trading pair
        start_time (int)   : Start of time range for data in epoch seconds
        end_time (int)	   : End of time range for data in epoch seconds
        interval (int)	   : Candlestick period in minutes Possible values are: 1, 5, 30, 60, 360, 1440


    Returns:
        If response from the server is HTTP_OK (200) then this returns the requests.response object

        Example response:
        [
          {
            "time": "1531215240",
            "open": "0.00049408",
            "close": "0.00049238",
            "high": "0.000497",
            "low": "0.00048919",
            "volume": "110169445.0",
            "quote_volume": "222900002152.0"
          },
          {
            "time": "1531219800",
            "open": "0.00050366",
            "close": "0.00049408",
            "high": "0.00050366",
            "low": "0.00049408",
            "volume": "102398958.0",
            "quote_volume": "205800003323.0"
          },
          ...
        ]
    """
    url = utils.format_urls(base_url, tickers.CANDLE_STICKS)
    params = {
        "pair": pair,
        "start_time": start_time,
        "end_time": end_time,
        "interval": interval,
    }
    resp = requests.get(url, params=params)
    return utils.response_else_exception(resp)
예제 #10
0
def _create_withdrawal(base_url, priv_key_wif, asset_id, amount, contract_hash, blockchain="NEO"):
    """Creates a withdrawal which can be executed later through execute_withdrawal.

    To be able to make a withdrawal, sufficient funds are required in the contract balance.
    A signature of the request payload has to be provided for this API call.

    Args:
        base_url (str)      : This paramter governs whether to connect to test or mainnet..
        priv_key_wif (str)      : The private key wif of the user.
        asset_id (str)      : The asset symbol or ID to withdraw. for eg. SWTH
        amount (int)        : Amount of tokens to withdraw.
        contract_hash (str) : Switcheo Exchange contract hash to execute the withdraw on.
        blockchain (str)    : Blockchain that the token to withdraw is on. Possible values are: neo.

    Returns:
        An id representing this transaction
        Example response:
        {
          "id": "e0f56e23-2e11-4848-b749-a147c872cbe6"
        }

    """
    signable_params = {
        "blockchain": blockchain,
        "asset_id": asset_id,
        "amount": utils.convert_to_neo_asset_amount(amount, asset_id, base_url),
        "timestamp": utils.get_current_epoch_milli(),
        "contract_hash": contract_hash,
    }
    signable_params_json_str = utils.jsonify(signable_params)

    # The withdrawer's address. Do not include this in the parameters to be signed.
    script_hash = get_script_hash_from_wif(priv_key_wif)

    pk = get_private_key_from_wif(priv_key_wif)
    encoded_msg = encode_msg(signable_params_json_str)
    signature = sign_msg(encoded_msg, pk)

    params = {**signable_params, "signature": signature, "address": script_hash}

    logger.debug("Params being sent to create withdrawal: {0}".format(params))
    url = utils.format_urls(base_url, withdrawals.CREATE_WITHDRAWAL)
    resp = requests.post(url, json=params)
    return utils.response_else_exception(resp)
예제 #11
0
def _list_balances(base_url, addresses, contract_hashes):
    """List contract balances of the given address and contract hashes.

    NOTE: The address gets converted to scripthash internally
    Args:
        base_url (str)               : This paramter governs whether to connect to test or mainnet..
        addresses (list[str])        : Only return balances for these addresses.
        contract_hashes (list[str])  : Only return balances from these contract hashes.

    Returns:
        If response from the server is HTTP_OK (200) then this returns the requests.response object

        Example response:
        {
          "confirming": {
            "GAS": [
              {
                "event_type": "withdrawal",
                "asset_id": "602c79718b16e442de58778de6b7b16cee7969282de7",
                "amount": -100000000,
                "transaction_hash": null,
                "created_at": "2018-07-12T10:48:48.866Z"
              }
            ]
          },
          "confirmed": {
            "GAS": "47320000000.0",
            "SWTH": "421549852102.0",
            "NEO": "50269113921.0"
          },
          "locked": {
            "GAS": "500000000.0",
            "NEO": "1564605000.0"
          }
        }
    """
    addresses = [
        get_script_hash_from_address(address) for address in addresses
    ]
    params = {"addresses": addresses, "contract_hashes": contract_hashes}
    url = utils.format_urls(base_url, LIST_BALANCES)
    resp = requests.get(url, params=params)
    return utils.response_else_exception(resp)
예제 #12
0
def _get_exchange_timestamp(base_url):
    """Returns the current timestamp in the exchange.

    This value should be fetched and used when a timestamp parameter is required for API requests.
    If the timestamp used for your API request is not within an acceptable range of the exchange's
    timestamp then an invalid signature error will be returned. The acceptable range might vary, but it
    should be less than one minute.

    Returns:
        If response from the server is HTTP_OK (200) then this returns the requests.response object

        Example response
        {
          "timestamp": 1534392760908
        }
    """
    url = utils.format_urls(base_url, exchange.GET_TIMESTAMP)
    resp = requests.get(url)
    return utils.response_else_exception(resp)
예제 #13
0
def _list_contracts(base_url):
    """Fetch updated hashes of contracts deployed by Switcheo.

    Args:
        base_url (str): This paramter governs whether to connect to test or mainnet.

    Returns:
        If response from the server is HTTP_OK (200) then this returns the requests.response object

        Example response
        {
          "NEO":
          {
            "V1": "0ec5712e0f7c63e4b0fea31029a28cea5e9d551f",
            "V1_5": "c41d8b0c30252ce7e8b6d95e9ce13fdd68d2a5a8",
            "V2": "48756743d524af03aa75729e911651ffd3cbe7d8"
          }
        }
    """
    url = utils.format_urls(base_url, exchange.LIST_CONTRACTS)
    resp = requests.get(url)
    return utils.response_else_exception(resp)
예제 #14
0
def _list_currency_pairs(base_url, bases=None):
    """Fetch available currency pairs on Switcheo Exchange filtered by the base parameter. Defaults to all pairs.

    Args:
        base_url (str)    : This paramter governs whether to connect to test or mainnet..
        bases (list[str]) : Provides pairs for these base symbols. Possible values are NEO, GAS, SWTH, USD.

    Returns:
        If response from the server is HTTP_OK (200) then this returns the requests.response object

        Example response
        [
          "GAS_NEO",
          "SWTH_NEO"
        ]
    """
    params = {}
    if bases:
        params["bases"] = bases

    url = utils.format_urls(base_url, exchange.LIST_CURRENCY_PAIRS)
    resp = requests.get(url, params=params)
    return utils.response_else_exception(resp)
예제 #15
0
def test_format_urls(base_url, test_end_point, expected):
    """Formatting urls test."""
    actual = format_urls(base_url, test_end_point)
    assert expected == actual