Skip to content

Commit a88e369

Browse files
authored
fix: make start_ledger optional and mutually exclusive with cursor (#1032)
* chore: gitignore .vscode * fix: make start_ledger optional and mutually exclusive with cursor * chore: rename PRC_URL to RPC_URL * chore: update changelog for #1032 * chore: sort imports * fix: make start_ledger optional in GetTransactionsRequest and GetLedgersRequest * deps: import Self from typing_extensions
1 parent 08622be commit a88e369

9 files changed

+390
-225
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,4 @@ venv.bak/
105105

106106
# IDE
107107
.idea/
108+
.vscode/

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Release History
55

66
#### Update
77
- fix: fix the issue where sending assets using `Transaction.append_payment_to_contract_op` fails when the sender's account is the same as the asset issuer's account. ([#1029](https://github.com/StellarCN/py-stellar-base/pull/1029))
8+
- fix: allow `SorobanServer.get_events()`, `.get_transactions()`, and `.get_ledgers()` to be paginated by making the `start_ledger` argument optional. ([#1032](https://github.com/StellarCN/py-stellar-base/pull/1032))
89

910
### Version 12.2.0
1011

poetry.lock

+5-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ toml = "^0.10.2"
5858
pydantic = "^2.5.2"
5959
xdrlib3 = "^0.1.1"
6060
requests-sse = ">=0.3,<0.6"
61+
typing-extensions = "^4.13.2"
6162

6263
[tool.poetry.extras]
6364
aiohttp = ["aiohttp", "aiohttp-sse-client"]

stellar_sdk/soroban_rpc.py

+20-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
from enum import Enum
33
from typing import Any, Dict, Generic, List, Optional, Sequence, TypeVar, Union
44

5-
from pydantic import BaseModel, ConfigDict, Field
5+
from pydantic import BaseModel, ConfigDict, Field, model_validator
6+
from typing_extensions import Self
67

78
T = TypeVar("T")
89

@@ -75,16 +76,27 @@ class PaginationOptions(BaseModel):
7576
limit: Optional[int] = None
7677

7778

78-
class GetEventsRequest(BaseModel):
79+
class PaginationMixin:
80+
pagination: Optional[PaginationOptions] = None
81+
82+
@model_validator(mode="after")
83+
def verify_ledger_or_cursor(self) -> Self:
84+
if self.pagination and (
85+
getattr(self, "start_ledger") and self.pagination.cursor
86+
):
87+
raise ValueError("start_ledger and cursor cannot both be set")
88+
return self
89+
90+
91+
class GetEventsRequest(PaginationMixin, BaseModel):
7992
"""Response for JSON-RPC method getEvents.
8093
8194
See `getEvents documentation <https://developers.stellar.org/docs/data/rpc/api-reference/methods/getEvents>`__ for
8295
more information.
8396
"""
8497

85-
start_ledger: int = Field(alias="startLedger")
98+
start_ledger: Optional[int] = Field(alias="startLedger", default=None)
8699
filters: Optional[Sequence[EventFilter]] = None
87-
pagination: Optional[PaginationOptions] = None
88100

89101

90102
class GetEventsResponse(BaseModel):
@@ -376,14 +388,13 @@ class GetFeeStatsResponse(BaseModel):
376388

377389

378390
# get_transactions
379-
class GetTransactionsRequest(BaseModel):
391+
class GetTransactionsRequest(PaginationMixin, BaseModel):
380392
"""Request for JSON-RPC method getTransactions.
381393
382394
See `getTransactions documentation <https://developers.stellar.org/docs/data/rpc/api-reference/methods/getTransactions>`__ for
383395
more information."""
384396

385-
start_ledger: int = Field(alias="startLedger")
386-
pagination: Optional[PaginationOptions] = None
397+
start_ledger: Optional[int] = Field(alias="startLedger", default=None)
387398

388399

389400
class Transaction(BaseModel):
@@ -430,14 +441,13 @@ class GetVersionInfoResponse(BaseModel):
430441

431442

432443
# get_ledgers
433-
class GetLedgersRequest(BaseModel):
444+
class GetLedgersRequest(PaginationMixin, BaseModel):
434445
"""Request for JSON-RPC method getLedgers.
435446
436447
See `getLedgers documentation <https://developers.stellar.org/docs/data/rpc/api-reference/methods/getLedgers>`__ for
437448
more information."""
438449

439-
start_ledger: int = Field(alias="startLedger")
440-
pagination: Optional[PaginationOptions] = None
450+
start_ledger: Optional[int] = Field(alias="startLedger", default=None)
441451

442452

443453
class LedgerInfo(BaseModel):

stellar_sdk/soroban_server.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def get_health(self) -> GetHealthResponse:
6767

6868
def get_events(
6969
self,
70-
start_ledger: int,
70+
start_ledger: int = None,
7171
filters: Sequence[EventFilter] = None,
7272
cursor: str = None,
7373
limit: int = None,
@@ -85,7 +85,7 @@ def get_events(
8585
"""
8686
pagination = PaginationOptions(cursor=cursor, limit=limit)
8787
data = GetEventsRequest(
88-
startLedger=str(start_ledger),
88+
startLedger=start_ledger,
8989
filters=filters,
9090
pagination=pagination,
9191
)
@@ -243,7 +243,7 @@ def get_fee_stats(self) -> GetFeeStatsResponse:
243243

244244
def get_transactions(
245245
self,
246-
start_ledger: int,
246+
start_ledger: int = None,
247247
cursor: str = None,
248248
limit: int = None,
249249
) -> GetTransactionsResponse:
@@ -260,7 +260,7 @@ def get_transactions(
260260
"""
261261
pagination = PaginationOptions(cursor=cursor, limit=limit)
262262
data = GetTransactionsRequest(
263-
startLedger=str(start_ledger),
263+
startLedger=start_ledger,
264264
pagination=pagination,
265265
)
266266
request: Request = Request[GetTransactionsRequest](
@@ -269,7 +269,10 @@ def get_transactions(
269269
return self._post(request, GetTransactionsResponse)
270270

271271
def get_ledgers(
272-
self, start_ledger: int, cursor: str = None, limit: int = None
272+
self,
273+
start_ledger: int = None,
274+
cursor: str = None,
275+
limit: int = None,
273276
) -> GetLedgersResponse:
274277
"""Fetch a detailed list of ledgers starting from the user specified starting point that you can paginate
275278
as long as the pages fall within the history retention of their corresponding RPC provider.
@@ -284,7 +287,7 @@ def get_ledgers(
284287
"""
285288
pagination = PaginationOptions(cursor=cursor, limit=limit)
286289
data = GetLedgersRequest(
287-
startLedger=str(start_ledger),
290+
startLedger=start_ledger,
288291
pagination=pagination,
289292
)
290293
request: Request = Request[GetLedgersRequest](

stellar_sdk/soroban_server_async.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ async def get_health(self) -> GetHealthResponse:
6767

6868
async def get_events(
6969
self,
70-
start_ledger: int,
70+
start_ledger: int = None,
7171
filters: Sequence[EventFilter] = None,
7272
cursor: str = None,
7373
limit: int = None,
@@ -85,7 +85,7 @@ async def get_events(
8585
"""
8686
pagination = PaginationOptions(cursor=cursor, limit=limit)
8787
data = GetEventsRequest(
88-
startLedger=str(start_ledger),
88+
startLedger=start_ledger,
8989
filters=filters,
9090
pagination=pagination,
9191
)
@@ -242,7 +242,7 @@ async def get_fee_stats(self) -> GetFeeStatsResponse:
242242

243243
async def get_transactions(
244244
self,
245-
start_ledger: int,
245+
start_ledger: int = None,
246246
cursor: str = None,
247247
limit: int = None,
248248
) -> GetTransactionsResponse:
@@ -259,7 +259,7 @@ async def get_transactions(
259259
"""
260260
pagination = PaginationOptions(cursor=cursor, limit=limit)
261261
data = GetTransactionsRequest(
262-
startLedger=str(start_ledger),
262+
startLedger=start_ledger,
263263
pagination=pagination,
264264
)
265265
request: Request = Request[GetTransactionsRequest](
@@ -268,7 +268,10 @@ async def get_transactions(
268268
return await self._post(request, GetTransactionsResponse)
269269

270270
async def get_ledgers(
271-
self, start_ledger: int, cursor: str = None, limit: int = None
271+
self,
272+
start_ledger: int = None,
273+
cursor: str = None,
274+
limit: int = None,
272275
) -> GetLedgersResponse:
273276
"""Fetch a detailed list of ledgers starting from the user specified starting point that you can paginate
274277
as long as the pages fall within the history retention of their corresponding RPC provider.
@@ -283,7 +286,7 @@ async def get_ledgers(
283286
"""
284287
pagination = PaginationOptions(cursor=cursor, limit=limit)
285288
data = GetLedgersRequest(
286-
startLedger=str(start_ledger),
289+
startLedger=start_ledger,
287290
pagination=pagination,
288291
)
289292
request: Request = Request[GetLedgersRequest](

0 commit comments

Comments
 (0)