Skip to content

Commit

Permalink
Merge pull request #2859 from CounterpartyXCP/apitweaks
Browse files Browse the repository at this point in the history
API Tweaks
  • Loading branch information
ouziel-slama authored Dec 17, 2024
2 parents 00348e0 + c83a532 commit 4f6b5b1
Show file tree
Hide file tree
Showing 9 changed files with 4,574 additions and 4,353 deletions.
4,754 changes: 2,415 additions & 2,339 deletions apiary.apib

Large diffs are not rendered by default.

43 changes: 37 additions & 6 deletions counterparty-core/counterpartycore/lib/api/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -2644,6 +2644,8 @@ def prepare_order_matches_where(status, other_conditions=None):
SELECT_ORDERS += "COALESCE((get_quantity * 1.0) / (give_quantity * 1.0), 0) AS give_price, "
SELECT_ORDERS += "COALESCE((give_quantity * 1.0) / (get_quantity * 1.0), 0) AS get_price"

SELECT_ORDER_MATCHES = SELECT_ORDERS.replace("get_", "forward_").replace("give_", "backward_")


def get_orders(
state_db,
Expand Down Expand Up @@ -2687,6 +2689,8 @@ def get_orders_by_asset(
state_db,
asset: str,
status: OrderStatus = "all",
get_asset: str = None,
give_asset: str = None,
cursor: str = None,
limit: int = 100,
offset: int = None,
Expand All @@ -2696,14 +2700,25 @@ def get_orders_by_asset(
Returns the orders of an asset
:param str asset: The asset to return (e.g. XCP)
:param str status: The status of the orders to return
:param str get_asset: The get asset to return
:param str give_asset: The give asset to return
:param str cursor: The last index of the orders to return
:param int limit: The maximum number of orders to return (e.g. 5)
:param int offset: The number of lines to skip before returning results (overrides the `cursor` parameter)
:param str sort: The sort order of the orders to return (overrides the `cursor` parameter) (e.g. expiration:desc)
"""
where = prepare_order_where(status, {"give_asset": asset.upper()}) + prepare_order_where(
status, {"get_asset": asset.upper()}
)
if get_asset:
where = prepare_order_where(
status, {"get_asset": get_asset.upper(), "give_asset": asset.upper()}
)
elif give_asset:
where = prepare_order_where(
status, {"give_asset": give_asset.upper(), "get_asset": asset.upper()}
)
else:
where = prepare_order_where(status, {"give_asset": asset.upper()}) + prepare_order_where(
status, {"get_asset": asset.upper()}
)

return select_rows(
state_db,
Expand Down Expand Up @@ -2857,6 +2872,7 @@ def get_all_order_matches(
limit=limit,
offset=offset,
sort=sort,
select=SELECT_ORDER_MATCHES,
)


Expand Down Expand Up @@ -2889,13 +2905,16 @@ def get_order_matches_by_order(
limit=limit,
offset=offset,
sort=sort,
select=SELECT_ORDER_MATCHES,
)


def get_order_matches_by_asset(
state_db,
asset: str,
status: OrderMatchesStatus = "all",
forward_asset: str = None,
backward_asset: str = None,
cursor: str = None,
limit: int = 100,
offset: int = None,
Expand All @@ -2905,14 +2924,25 @@ def get_order_matches_by_asset(
Returns the orders of an asset
:param str asset: The asset to return (e.g. XCP)
:param str status: The status of the order matches to return
:param str forward_asset: The forward asset to return
:param str backward_asset: The backward asset to return
:param str cursor: The last index of the order matches to return
:param int limit: The maximum number of order matches to return (e.g. 5)
:param int offset: The number of lines to skip before returning results (overrides the `cursor` parameter)
:param str sort: The sort order of the order matches to return (overrides the `cursor` parameter) (e.g. forward_quantity:desc)
"""
where = prepare_order_matches_where(
status, {"forward_asset": asset.upper()}
) + prepare_order_matches_where(status, {"backward_asset": asset.upper()})
if forward_asset:
where = prepare_order_matches_where(
status, {"forward_asset": forward_asset.upper(), "backward_asset": asset.upper()}
)
elif backward_asset:
where = prepare_order_matches_where(
status, {"forward_asset": asset.upper(), "backward_asset": backward_asset.upper()}
)
else:
where = prepare_order_matches_where(
status, {"forward_asset": asset.upper()}
) + prepare_order_matches_where(status, {"backward_asset": asset.upper()})

return select_rows(
state_db,
Expand All @@ -2922,6 +2952,7 @@ def get_order_matches_by_asset(
limit=limit,
offset=offset,
sort=sort,
select=SELECT_ORDER_MATCHES,
)


Expand Down
4 changes: 3 additions & 1 deletion counterparty-core/counterpartycore/lib/api/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ def inject_normalized_quantity(item, field_name, asset_info):
return item

if item[field_name] is not None:
if field_name in ["give_price", "get_price", "price"]:
if field_name in ["give_price", "get_price", "forward_price", "backward_price", "price"]:
# use 16 decimal places for prices
item[field_name + "_normalized"] = normalize_price(item[field_name])
else:
Expand Down Expand Up @@ -483,6 +483,8 @@ def inject_normalized_quantities(result_list):
"paid_quantity": {"asset_field": "asset_info", "divisible": None},
"give_price": {"asset_field": "give_asset_info", "divisible": None},
"get_price": {"asset_field": "get_asset_info", "divisible": None},
"forward_price": {"asset_field": "forward_asset_info", "divisible": None},
"backward_price": {"asset_field": "backward_asset_info", "divisible": None},
}

enriched_result_list = []
Expand Down
2 changes: 1 addition & 1 deletion counterparty-core/counterpartycore/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def initialise_config(
if not cache_dir:
cache_dir = appdirs.user_cache_dir(appauthor=config.XCP_NAME, appname=config.APP_NAME)
if not os.path.isdir(cache_dir):
os.makedirs(config.CACHE_DIR, mode=0o755)
os.makedirs(cache_dir, mode=0o755)
config.CACHE_DIR = cache_dir

# testnet
Expand Down
2 changes: 2 additions & 0 deletions counterparty-core/counterpartycore/test/api_v2_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,8 +474,10 @@ def test_new_get_order_matches():
"tx1_hash": "1b294dd8592e76899b1c106782e4c96e63114abd8e3fa09ab6d2d52496b5bf81",
"tx1_address": "mtQheFaSfWELRB2MyMBaiWjdDm6ux9Ezns",
"forward_asset": "XCP",
"forward_price": 0.008,
"forward_quantity": 100000000,
"backward_asset": "BTC",
"backward_price": 125.0,
"backward_quantity": 800000,
"tx0_block_index": 310491,
"tx1_block_index": 310492,
Expand Down
5 changes: 4 additions & 1 deletion counterparty-core/counterpartycore/test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ def server_db(request, cp_server, api_server):
cursor = db.cursor()
cursor.execute("""BEGIN""")
util_test.reset_current_block_index(db)
config.CACHE_DIR = os.path.dirname(request.module.FIXTURE_DB)

request.addfinalizer(lambda: cursor.execute("""ROLLBACK"""))
request.addfinalizer(lambda: util_test.reset_current_block_index(db))
Expand All @@ -245,6 +246,7 @@ def api_server(request, cp_server):

config.RPC_PORT = TEST_RPC_PORT = TEST_RPC_PORT + 1
server.configure_rpc(config.RPC_PASSWORD)
config.CACHE_DIR = os.path.dirname(request.module.FIXTURE_DB)

print("api_server", config.DATABASE, config.STATE_DATABASE)

Expand Down Expand Up @@ -323,7 +325,7 @@ def api_server_v2(request, cp_server):
"max_log_file_rotations": 20,
"log_exclude_filters": None,
"log_include_filters": None,
"cache_dir": None,
"cache_dir": os.path.dirname(request.module.FIXTURE_DB),
}
server_config = (
default_config
Expand All @@ -336,6 +338,7 @@ def api_server_v2(request, cp_server):
)

config.STATE_DATABASE = config.STATE_DATABASE.replace(".testnet.db", ".db")
config.CACHE_DIR = os.path.dirname(request.module.FIXTURE_DB)

if os.path.exists(config.STATE_DATABASE):
os.unlink(config.STATE_DATABASE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7907,6 +7907,8 @@
"match_expire_index": 310512,
"fee_paid": 7200,
"status": "expired",
"backward_price": 125.0,
"forward_price": 0.008,
"block_time": 310513000,
"forward_asset_info": {
"asset_longname": null,
Expand All @@ -7924,7 +7926,9 @@
},
"forward_quantity_normalized": "1.00000000",
"backward_quantity_normalized": "0.00800000",
"fee_paid_normalized": "0.00007200"
"fee_paid_normalized": "0.00007200",
"forward_price_normalized": "0.0080000000000000",
"backward_price_normalized": "125.0000000000000000"
}
],
"next_cursor": null,
Expand Down Expand Up @@ -7967,6 +7971,8 @@
"match_expire_index": 310512,
"fee_paid": 7200,
"status": "expired",
"backward_price": 125.0,
"forward_price": 0.008,
"block_time": 310513000,
"forward_asset_info": {
"asset_longname": null,
Expand All @@ -7984,7 +7990,9 @@
},
"forward_quantity_normalized": "1.00000000",
"backward_quantity_normalized": "0.00800000",
"fee_paid_normalized": "0.00007200"
"fee_paid_normalized": "0.00007200",
"forward_price_normalized": "0.0080000000000000",
"backward_price_normalized": "125.0000000000000000"
}
],
"next_cursor": null,
Expand Down Expand Up @@ -16964,6 +16972,20 @@
],
"description": "The status of the orders to return"
},
{
"name": "get_asset",
"default": null,
"required": false,
"type": "str",
"description": "The get asset to return"
},
{
"name": "give_asset",
"default": null,
"required": false,
"type": "str",
"description": "The give asset to return"
},
{
"name": "cursor",
"default": null,
Expand Down Expand Up @@ -17024,6 +17046,20 @@
],
"description": "The status of the order matches to return"
},
{
"name": "forward_asset",
"default": null,
"required": false,
"type": "str",
"description": "The forward asset to return"
},
{
"name": "backward_asset",
"default": null,
"required": false,
"type": "str",
"description": "The backward asset to return"
},
{
"name": "cursor",
"default": null,
Expand Down
Loading

0 comments on commit 4f6b5b1

Please sign in to comment.