Ejemplo n.º 1
0
def remove_order_by_id(request, order_id):
    result, tx_unconfirmed, tx_failed, tx_hash = smart_contract.invoke_single(
        "deleteOrder", [order_id], True)
    return {
        "result": byte_to_int(result),
        "tx_unconfirmed": tx_unconfirmed,
        "tx_failed": tx_failed,
        "tx_hash": tx_hash
    }
Ejemplo n.º 2
0
def insert_order(request, user_adr):
    try:
        body = json.loads(request.content.read().decode("utf-8"))
    except JSONDecodeError as e:
        request.setResponseCode(400)
        return build_error(STATUS_ERROR_JSON, "JSON Error: %s" % str(e))

    if len(user_adr) != 34:
        request.setResponseCode(400)
        return build_error(STATUS_ERROR_JSON, "Address not 34 characters")

    if "record_id_list" not in body:
        request.setResponseCode(400)
        return build_error(STATUS_ERROR_JSON, "Missing record_id_list")
    record_id_list = body["record_id_list"]

    if isinstance(record_id_list, str):
        request.setResponseCode(400)
        return build_error(STATUS_ERROR_JSON,
                           "Should be a list record_id_list")

    record_id_list_str = ""
    for record_id in record_id_list:
        record_id_list_str += str(int(record_id))
        record_id_list_str += ":"

    if len(record_id_list_str) == 0:
        request.setResponseCode(400)
        return build_error(STATUS_ERROR_JSON, "Empty list record_id_list")
    else:
        record_id_list_str = record_id_list_str[0:len(record_id_list_str) - 1]

    if "price" not in body:
        request.setResponseCode(400)
        return build_error(STATUS_ERROR_JSON, "Missing price")

    price = int(body["price"])
    if price < 0:
        request.setResponseCode(400)
        return build_error(STATUS_ERROR_JSON, "Price can not be negative")

    result, tx_unconfirmed, tx_failed, tx_hash = smart_contract.invoke_single(
        "createOrder", [user_adr, record_id_list_str, price], True)
    return {
        "result": byte_to_int(result),
        "tx_unconfirmed": tx_unconfirmed,
        "tx_failed": tx_failed,
        "tx_hash": tx_hash
    }
Ejemplo n.º 3
0
def purchase_order_by_id(request, order_id):
    try:
        body = json.loads(request.content.read().decode("utf-8"))
    except JSONDecodeError as e:
        request.setResponseCode(400)
        return build_error(STATUS_ERROR_JSON, "JSON Error: %s" % str(e))

    if "pub_key" not in body:
        request.setResponseCode(400)
        return build_error(STATUS_ERROR_JSON, "Missing pub_key")
    pub_key = body["pub_key"]

    if "attach_neo" not in body:
        request.setResponseCode(400)
        return build_error(STATUS_ERROR_JSON, "Missing attach_neo")

    attach_neo = int(body["attach_neo"])
    if attach_neo < 0:
        request.setResponseCode(400)
        return build_error(STATUS_ERROR_JSON, "attach_neo can not be negative")

    order, tx_unconfirmed, tx_failed, tx_hash = smart_contract.invoke_single(
        "getOrder", [order_id])
    if len(order) == 4:
        order[0] = bytes_to_address(order[0])
        order[1] = parse_id_list(bytestr_to_str(str(order[1])))
        order[2] = int.from_bytes(order[2], byteorder='little')
        order[3] = bytestr_to_str(order[3])
    else:
        request.setResponseCode(400)
        return build_error(STATUS_ERROR_JSON, "Order doesn't exist")

    if order[3] != '\\x00' and order[3] != '':
        request.setResponseCode(400)
        return build_error(STATUS_ERROR_JSON, "Already purchased")

    if attach_neo < order[2]:
        request.setResponseCode(400)
        return build_error(STATUS_ERROR_JSON, "NEO required: " + str(order[2]))

    result, tx_unconfirmed, tx_failed, tx_hash = smart_contract.invoke_single(
        "purchaseData", [order_id, pub_key], True, attach_neo)
    return {
        "result": byte_to_int(result),
        "tx_unconfirmed": tx_unconfirmed,
        "tx_failed": tx_failed,
        "tx_hash": tx_hash
    }
Ejemplo n.º 4
0
def set_pubkey_by_user_id(request, user_adr):
    try:
        body = json.loads(request.content.read().decode("utf-8"))
    except JSONDecodeError as e:
        request.setResponseCode(400)
        return build_error(STATUS_ERROR_JSON, "JSON Error: %s" % str(e))

    if len(user_adr) != 34:
        request.setResponseCode(400)
        return build_error(STATUS_ERROR_JSON, "Address not 34 characters")

    if "pub_key" not in body:
        request.setResponseCode(400)
        return build_error(STATUS_ERROR_JSON, "Missing creator_adr")
    pub_key = body["pub_key"]
    result, tx_unconfirmed, tx_failed, tx_hash = smart_contract.invoke_single(
        "setUserPubKey", [user_adr, pub_key], True)
    return {
        "result": byte_to_int(result),
        "tx_unconfirmed": tx_unconfirmed,
        "tx_failed": tx_failed,
        "tx_hash": tx_hash
    }