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

Add filters for get_transaction_count API #15502

Merged
merged 1 commit into from
Jun 14, 2023
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
7 changes: 6 additions & 1 deletion chia/rpc/wallet_rpc_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -923,7 +923,12 @@ async def get_transactions(self, request: Dict) -> EndpointResult:

async def get_transaction_count(self, request: Dict) -> EndpointResult:
wallet_id = int(request["wallet_id"])
count = await self.service.wallet_state_manager.tx_store.get_transaction_count_for_wallet(wallet_id)
type_filter = None
if "type_filter" in request:
type_filter = TransactionTypeFilter.from_json_dict(request["type_filter"])
count = await self.service.wallet_state_manager.tx_store.get_transaction_count_for_wallet(
wallet_id, confirmed=request.get("confirmed", None), type_filter=type_filter
)
return {
"count": count,
"wallet_id": wallet_id,
Expand Down
13 changes: 9 additions & 4 deletions chia/rpc/wallet_rpc_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,16 @@ async def get_transactions(
async def get_transaction_count(
self,
wallet_id: int,
confirmed: Optional[bool] = None,
type_filter: Optional[TransactionTypeFilter] = None,
) -> List[TransactionRecord]:
res = await self.fetch(
"get_transaction_count",
{"wallet_id": wallet_id},
)
request: Dict[str, Any] = {"wallet_id": wallet_id}
if type_filter is not None:
request["type_filter"] = type_filter.to_json_dict()

if confirmed is not None:
request["confirmed"] = confirmed
res = await self.fetch("get_transaction_count", request)
return res["count"]

async def get_next_address(self, wallet_id: int, new_address: bool) -> str:
Expand Down
23 changes: 21 additions & 2 deletions chia/wallet/wallet_transaction_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,10 +309,29 @@ async def get_transactions_between(

return [TransactionRecord.from_bytes(row[0]) for row in rows]

async def get_transaction_count_for_wallet(self, wallet_id) -> int:
async def get_transaction_count_for_wallet(
self,
wallet_id: int,
confirmed: Optional[bool] = None,
type_filter: Optional[TransactionTypeFilter] = None,
) -> int:
confirmed_str = ""
if confirmed is not None:
confirmed_str = f"AND confirmed={int(confirmed)}"

if type_filter is None:
type_filter_str = ""
else:
type_filter_str = (
f"AND type {'' if type_filter.mode == FilterMode.include else 'NOT'} "
f"IN ({','.join([str(x) for x in type_filter.values])})"
)
async with self.db_wrapper.reader_no_transaction() as conn:
rows = list(
await conn.execute_fetchall("SELECT COUNT(*) FROM transaction_record where wallet_id=?", (wallet_id,))
await conn.execute_fetchall(
f"SELECT COUNT(*) FROM transaction_record where wallet_id=? {type_filter_str} {confirmed_str}",
(wallet_id,),
)
)
return 0 if len(rows) == 0 else rows[0][0]

Expand Down
7 changes: 7 additions & 0 deletions tests/wallet/rpc/test_wallet_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,13 @@ async def test_get_transaction_count(wallet_rpc_environment: WalletRpcTestEnviro
assert len(all_transactions) > 0
transaction_count = await client.get_transaction_count(1)
assert transaction_count == len(all_transactions)
assert await client.get_transaction_count(1, confirmed=False) == 0
assert (
await client.get_transaction_count(
1, type_filter=TransactionTypeFilter.include([TransactionType.INCOMING_CLAWBACK_SEND])
)
== 0
)


@pytest.mark.asyncio
Expand Down
18 changes: 18 additions & 0 deletions tests/wallet/test_transaction_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,24 @@ async def test_transaction_count_for_wallet() -> None:

assert await store.get_transaction_count_for_wallet(1) == 5
assert await store.get_transaction_count_for_wallet(2) == 2
assert (
await store.get_transaction_count_for_wallet(
1, True, type_filter=TransactionTypeFilter.include([TransactionType.OUTGOING_TX])
)
== 0
)
assert (
await store.get_transaction_count_for_wallet(
1, False, type_filter=TransactionTypeFilter.include([TransactionType.OUTGOING_CLAWBACK])
)
== 0
)
assert (
await store.get_transaction_count_for_wallet(
1, False, type_filter=TransactionTypeFilter.include([TransactionType.OUTGOING_TX])
)
== 5
)


@pytest.mark.asyncio
Expand Down