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

Enable searching bulk transfer uploads by token address #705

Merged
merged 1 commit into from
Oct 3, 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
1 change: 1 addition & 0 deletions app/model/schema/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
IbetShareBulkTransferRequest,
IbetStraightBondBulkTransferRequest,
ListBulkTransferQuery,
ListBulkTransferUploadQuery,
)
from .e2e_messaging import (
E2EMessagingAccountChangeEOAPasswordRequest,
Expand Down
14 changes: 14 additions & 0 deletions app/model/schema/bulk_transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,20 @@ class IbetShareBulkTransferRequest(BaseModel):
)


@dataclass
class ListBulkTransferUploadQuery:
token_address: Annotated[
Optional[str],
Query(description="Token address (**this affects total number**)"),
] = None
offset: Annotated[
Optional[NonNegativeInt], Query(description="Offset for pagination")
] = None
limit: Annotated[
Optional[NonNegativeInt], Query(description="Limit for pagination")
] = None


@dataclass
class ListBulkTransferQuery:
offset: Annotated[
Expand Down
15 changes: 14 additions & 1 deletion app/routers/issuer/bond.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
cast,
column,
desc,
distinct,
func,
literal,
literal_column,
Expand Down Expand Up @@ -138,6 +139,7 @@
ListBatchIssueRedeemUploadResponse,
ListBatchRegisterPersonalInfoUploadResponse,
ListBulkTransferQuery,
ListBulkTransferUploadQuery,
ListRedeemHistoryQuery,
ListTokenOperationLogHistoryQuery,
ListTokenOperationLogHistoryResponse,
Expand Down Expand Up @@ -4132,7 +4134,7 @@ async def bulk_transfer_bond_token_ownership(
async def list_bond_token_bulk_transfers(
db: DBAsyncSession,
issuer_address: Optional[str] = Header(None),
get_query: ListBulkTransferQuery = Depends(),
get_query: ListBulkTransferUploadQuery = Depends(),
):
"""List bond token bulk transfers"""

Expand All @@ -4154,6 +4156,17 @@ async def list_bond_token_bulk_transfers(
)
)

if get_query.token_address is not None:
upload_id_subquery = (
select(distinct(BulkTransfer.upload_id).label("upload_id"))
.where(BulkTransfer.token_address == get_query.token_address)
.subquery()
)
stmt = stmt.join(
upload_id_subquery,
upload_id_subquery.c.upload_id == BulkTransferUpload.upload_id,
)

total = await db.scalar(select(func.count()).select_from(stmt.subquery()))
count = total

Expand Down
15 changes: 14 additions & 1 deletion app/routers/issuer/share.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
cast,
column,
desc,
distinct,
func,
literal,
literal_column,
Expand Down Expand Up @@ -139,6 +140,7 @@
ListBatchIssueRedeemUploadResponse,
ListBatchRegisterPersonalInfoUploadResponse,
ListBulkTransferQuery,
ListBulkTransferUploadQuery,
ListRedeemHistoryQuery,
ListTokenOperationLogHistoryQuery,
ListTokenOperationLogHistoryResponse,
Expand Down Expand Up @@ -4061,7 +4063,7 @@ async def bulk_transfer_share_token_ownership(
async def list_share_token_bulk_transfers(
db: DBAsyncSession,
issuer_address: Optional[str] = Header(None),
get_query: ListBulkTransferQuery = Depends(),
get_query: ListBulkTransferUploadQuery = Depends(),
):
"""List share token bulk transfers"""

Expand All @@ -4083,6 +4085,17 @@ async def list_share_token_bulk_transfers(
)
)

if get_query.token_address is not None:
upload_id_subquery = (
select(distinct(BulkTransfer.upload_id).label("upload_id"))
.where(BulkTransfer.token_address == get_query.token_address)
.subquery()
)
stmt = stmt.join(
upload_id_subquery,
upload_id_subquery.c.upload_id == BulkTransferUpload.upload_id,
)

total = await db.scalar(select(func.count()).select_from(stmt.subquery()))
count = total

Expand Down
20 changes: 20 additions & 0 deletions docs/ibet_prime.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3290,6 +3290,16 @@ paths:
description: List bond token bulk transfers
operationId: ListBondTokenBulkTransfers
parameters:
- name: token_address
in: query
required: false
schema:
anyOf:
- type: string
- type: 'null'
description: Token address (**this affects total number**)
title: Token Address
description: Token address (**this affects total number**)
- name: offset
in: query
required: false
Expand Down Expand Up @@ -7596,6 +7606,16 @@ paths:
description: List share token bulk transfers
operationId: ListShareTokenBulkTransfers
parameters:
- name: token_address
in: query
required: false
schema:
anyOf:
- type: string
- type: 'null'
description: Token address (**this affects total number**)
title: Token Address
description: Token address (**this affects total number**)
- name: offset
in: query
required: false
Expand Down
122 changes: 115 additions & 7 deletions tests/app/test_bond_bulk_transfer_ListBondTokenBulkTransfers.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import pytest
import pytz

from app.model.db import Account, BulkTransferUpload, TokenType
from app.model.db import Account, BulkTransfer, BulkTransferUpload, TokenType
from config import TZ
from tests.account_config import config_eth_account

Expand Down Expand Up @@ -60,10 +60,11 @@ class TestListBondTokenBulkTransfers:
# Normal Case
###########################################################################

# <Normal_1>
# Issuer specified
# <Normal_1_1>
# Search all
# - Header: issuer address is set
@pytest.mark.freeze_time("2021-05-20 12:34:56")
def test_normal_1(self, client, db):
def test_normal_1_1(self, client, db):
# prepare data : Account(Issuer)
for _issuer in self.upload_issuer_list:
account = Account()
Expand Down Expand Up @@ -110,10 +111,11 @@ def test_normal_1(self, client, db):
],
}

# <Normal_2>
# No issuer specified
# <Normal_1_2>
# Search all
# - Header: issuer address is not set
@pytest.mark.freeze_time("2021-05-20 12:34:56")
def test_normal_2(self, client, db):
def test_normal_1_2(self, client, db):
# prepare data : BulkTransferUpload
utc_now = datetime.now(UTC).replace(tzinfo=None)
for i in range(0, 3):
Expand Down Expand Up @@ -161,6 +163,112 @@ def test_normal_2(self, client, db):
)
assert sorted_resp == sorted_assumed

# <Normal_2>
# Search by token_address
@pytest.mark.freeze_time("2021-05-20 12:34:56")
def test_normal_2(self, client, db):
# prepare data : Account(Issuer)
for _issuer in self.upload_issuer_list:
account = Account()
account.issuer_address = _issuer["address"]
account.keyfile = _issuer["keyfile"]
db.add(account)

# prepare data : BulkTransferUpload
utc_now = datetime.now(UTC).replace(tzinfo=None)
for i in range(0, 3):
bulk_transfer_upload = BulkTransferUpload()
bulk_transfer_upload.issuer_address = self.upload_issuer_list[0]["address"]
bulk_transfer_upload.upload_id = self.upload_id_list[i]
bulk_transfer_upload.token_type = TokenType.IBET_STRAIGHT_BOND.value
bulk_transfer_upload.status = i
bulk_transfer_upload.created = utc_now
db.add(bulk_transfer_upload)

# prepare data : BulkTransfer
bulk_transfer_0_0 = BulkTransfer()
bulk_transfer_0_0.issuer_address = self.upload_issuer_list[0]["address"]
bulk_transfer_0_0.upload_id = self.upload_id_list[0]
bulk_transfer_0_0.token_type = TokenType.IBET_STRAIGHT_BOND.value
bulk_transfer_0_0.token_address = "test_token_address_1" # 抽出対象
bulk_transfer_0_0.from_address = "test_from_address_1"
bulk_transfer_0_0.to_address = "test_to_address_1"
bulk_transfer_0_0.amount = 10
bulk_transfer_0_0.status = 1
db.add(bulk_transfer_0_0)

bulk_transfer_0_1 = BulkTransfer()
bulk_transfer_0_1.issuer_address = self.upload_issuer_list[0]["address"]
bulk_transfer_0_1.upload_id = self.upload_id_list[0]
bulk_transfer_0_1.token_type = TokenType.IBET_STRAIGHT_BOND.value
bulk_transfer_0_1.token_address = "test_token_address_1" # 抽出対象
bulk_transfer_0_1.from_address = "test_from_address_2"
bulk_transfer_0_1.to_address = "test_to_address_2"
bulk_transfer_0_1.amount = 10
bulk_transfer_0_1.status = 1
db.add(bulk_transfer_0_1)

bulk_transfer_1_0 = BulkTransfer()
bulk_transfer_1_0.issuer_address = self.upload_issuer_list[0]["address"]
bulk_transfer_1_0.upload_id = self.upload_id_list[1]
bulk_transfer_1_0.token_type = TokenType.IBET_STRAIGHT_BOND.value
bulk_transfer_1_0.token_address = "test_token_address_other" # 抽出対象外
bulk_transfer_1_0.from_address = "test_from_address_2"
bulk_transfer_1_0.to_address = "test_to_address_2"
bulk_transfer_1_0.amount = 10
bulk_transfer_1_0.status = 1
db.add(bulk_transfer_1_0)

bulk_transfer_2_0 = BulkTransfer()
bulk_transfer_2_0.issuer_address = self.upload_issuer_list[0]["address"]
bulk_transfer_2_0.upload_id = self.upload_id_list[2]
bulk_transfer_2_0.token_type = TokenType.IBET_STRAIGHT_BOND.value
bulk_transfer_2_0.token_address = "test_token_address_1" # 抽出対象
bulk_transfer_2_0.from_address = "test_from_address_3"
bulk_transfer_2_0.to_address = "test_to_address_3"
bulk_transfer_2_0.amount = 10
bulk_transfer_2_0.status = 1
db.add(bulk_transfer_2_0)

db.commit()

# request target API
resp = client.get(
self.test_url,
headers={"issuer-address": self.upload_issuer_list[0]["address"]},
params={"token_address": "test_token_address_1"},
)

# assertion
assert resp.status_code == 200
assert resp.json() == {
"result_set": {"count": 2, "offset": None, "limit": None, "total": 2},
"bulk_transfer_uploads": [
{
"issuer_address": self.upload_issuer_list[0]["address"],
"token_type": TokenType.IBET_STRAIGHT_BOND.value,
"token_address": None,
"upload_id": self.upload_id_list[0],
"status": 0,
"created": pytz.timezone("UTC")
.localize(utc_now)
.astimezone(local_tz)
.isoformat(),
},
{
"issuer_address": self.upload_issuer_list[0]["address"],
"token_type": TokenType.IBET_STRAIGHT_BOND.value,
"token_address": None,
"upload_id": self.upload_id_list[2],
"status": 2,
"created": pytz.timezone("UTC")
.localize(utc_now)
.astimezone(local_tz)
.isoformat(),
},
],
}

# <Normal_3>
# offset / limit
@pytest.mark.freeze_time("2021-05-20 12:34:56")
Expand Down
Loading
Loading