Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix get asset dispensers query #2208

Merged
merged 1 commit into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions counterparty-core/counterpartycore/lib/api/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,8 @@
OrderMatchesStatus = Literal["all", "pending", "completed", "expired"]
BetStatus = Literal["cancelled", "dropped", "expired", "filled", "open"]
DispenserStatus = Literal["all", "open", "closed", "closing", "open_empty_address"]
DispenserStatusNumber = {
"open": 0,
"closed": 10,
"closing": 11,
"open_empty_address": 1,
}
DispenserStatusNumber = {"open": 0, "closed": 10, "closing": 11, "open_empty_address": 1}
DispenserStatusNumberInverted = {value: key for key, value in DispenserStatusNumber.items()}

BetMatchesStatus = Literal[
"dropped",
Expand Down Expand Up @@ -1711,18 +1707,24 @@ def get_receive_by_address_and_asset(
)


def preprare_dispenser_where(status, other_conditions=None):
def prepare_dispenser_where(status, other_conditions=None):
where = []
statuses = status.split(",")
for status in statuses:
if status == "all":
for s in statuses:
if s.isdigit():
s = DispenserStatusNumberInverted.get(int(s), "all") # noqa: PLW2901

if s == "all":
where = other_conditions or {}
break
if status in DispenserStatusNumber:
where_status = {"status": DispenserStatusNumber[status]}

if s in DispenserStatusNumber:
where_status = {"status": DispenserStatusNumber[s]}
if other_conditions:
where_status.update(other_conditions)

where.append(where_status)

return where


Expand All @@ -1740,7 +1742,7 @@ def get_dispensers(
return select_rows(
db,
"dispensers",
where=preprare_dispenser_where(status),
where=prepare_dispenser_where(status),
last_cursor=cursor,
limit=limit,
offset=offset,
Expand All @@ -1766,7 +1768,7 @@ def get_dispensers_by_address(
return select_rows(
db,
"dispensers",
where=preprare_dispenser_where(status, {"source": address}),
where=prepare_dispenser_where(status, {"source": address}),
last_cursor=cursor,
limit=limit,
offset=offset,
Expand All @@ -1792,7 +1794,7 @@ def get_dispensers_by_asset(
return select_rows(
db,
"dispensers",
where=preprare_dispenser_where(status, {"asset": asset.upper()}),
where=prepare_dispenser_where(status, {"asset": asset.upper()}),
last_cursor=cursor,
limit=limit,
offset=offset,
Expand Down
65 changes: 64 additions & 1 deletion counterparty-core/counterpartycore/test/api_v2_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ def test_new_get_balances_by_asset():
"utxo": None,
"utxo_address": None,
"asset": "XCP",
"quantity": 92999130360,
"quantity": 92949130360,
},
{
"address": "mwtPsLQxW9xpm7gdLmwWvJK5ABdPUVJm42",
Expand Down Expand Up @@ -444,3 +444,66 @@ def test_new_get_order_matches():
"status": "expired",
"confirmed": True,
}


@pytest.mark.usefixtures("api_server_v2")
def test_asset_dispensers():
asset = "XCP"

url = f"{API_ROOT}/v2/assets/{asset}/dispensers?status=1"
result = requests.get(url) # noqa: S113
assert result.json()["result"] == []

url = f"{API_ROOT}/v2/assets/{asset}/dispensers?status=0"
result = requests.get(url) # noqa: S113
assert result.json()["result"] == [
{
"tx_index": 108,
"tx_hash": "9834219d2825b4d85ca7ee0d75a5372d9d42ce75eb9144951fca1af5a25915ec",
"block_index": 310107,
"source": "munimLLHjPhGeSU5rYB2HN79LJa8bRZr5b",
"asset": "XCP",
"give_quantity": 100,
"escrow_quantity": 100,
"satoshirate": 100,
"status": 0,
"give_remaining": 100,
"oracle_address": None,
"last_status_tx_hash": None,
"origin": "munimLLHjPhGeSU5rYB2HN79LJa8bRZr5b",
"dispense_count": 0,
"last_status_tx_source": None,
"close_block_index": None,
"confirmed": True,
}
]

asset = "TESTDISP"

url = f"{API_ROOT}/v2/assets/{asset}/dispensers?status=1"
result = requests.get(url) # noqa: S113
assert result.json()["result"] == []

url = f"{API_ROOT}/v2/assets/{asset}/dispensers?status=0"
result = requests.get(url) # noqa: S113
assert result.json()["result"] == [
{
"tx_index": 511,
"tx_hash": "af67f6762d4b00b4bf5fb93a9d556e007a147aa80fbf6a84410df05a0182da9e",
"block_index": 310510,
"source": "munimLLHjPhGeSU5rYB2HN79LJa8bRZr5b",
"asset": "TESTDISP",
"give_quantity": 100,
"escrow_quantity": 100,
"satoshirate": 100,
"status": 0,
"give_remaining": 100,
"oracle_address": None,
"last_status_tx_hash": None,
"origin": "munimLLHjPhGeSU5rYB2HN79LJa8bRZr5b",
"dispense_count": 0,
"last_status_tx_source": None,
"close_block_index": None,
"confirmed": True,
}
]
Loading
Loading