xian-tech-py 0.4.8__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
xian_py/__init__.py ADDED
@@ -0,0 +1,96 @@
1
+ from xian_runtime_types.time import to_contract_time
2
+
3
+ from xian_py.application_clients import (
4
+ AsyncContractClient,
5
+ AsyncEventClient,
6
+ AsyncStateKeyClient,
7
+ AsyncTokenClient,
8
+ ContractClient,
9
+ EventClient,
10
+ StateKeyClient,
11
+ TokenClient,
12
+ )
13
+ from xian_py.config import (
14
+ RetryPolicy,
15
+ SubmissionConfig,
16
+ TransportConfig,
17
+ WatcherConfig,
18
+ XianClientConfig,
19
+ )
20
+ from xian_py.exception import (
21
+ AbciError,
22
+ RpcError,
23
+ SimulationError,
24
+ TransactionError,
25
+ TransportError,
26
+ TxTimeoutError,
27
+ XianException,
28
+ )
29
+ from xian_py.models import (
30
+ BdsStatus,
31
+ DeveloperRewardSummary,
32
+ IndexedBlock,
33
+ IndexedEvent,
34
+ IndexedTransaction,
35
+ NodeStatus,
36
+ PerformanceStatus,
37
+ StateEntry,
38
+ TransactionReceipt,
39
+ TransactionSubmission,
40
+ )
41
+ from xian_py.projectors import (
42
+ EventProjector,
43
+ EventProjectorError,
44
+ EventSource,
45
+ SQLiteProjectionState,
46
+ indexed_event_sort_key,
47
+ merged_event_payload,
48
+ )
49
+ from xian_py.run_sync import run_sync
50
+ from xian_py.wallet import Wallet
51
+ from xian_py.xian import Xian
52
+ from xian_py.xian_async import XianAsync
53
+
54
+ __all__ = [
55
+ "AbciError",
56
+ "AsyncContractClient",
57
+ "AsyncEventClient",
58
+ "AsyncStateKeyClient",
59
+ "AsyncTokenClient",
60
+ "BdsStatus",
61
+ "DeveloperRewardSummary",
62
+ "ContractClient",
63
+ "EventClient",
64
+ "EventProjector",
65
+ "EventProjectorError",
66
+ "EventSource",
67
+ "IndexedBlock",
68
+ "IndexedEvent",
69
+ "IndexedTransaction",
70
+ "NodeStatus",
71
+ "PerformanceStatus",
72
+ "RpcError",
73
+ "RetryPolicy",
74
+ "SimulationError",
75
+ "SQLiteProjectionState",
76
+ "StateEntry",
77
+ "SubmissionConfig",
78
+ "TransactionError",
79
+ "TransactionReceipt",
80
+ "TransactionSubmission",
81
+ "TransportConfig",
82
+ "TransportError",
83
+ "TxTimeoutError",
84
+ "WatcherConfig",
85
+ "Xian",
86
+ "XianAsync",
87
+ "XianClientConfig",
88
+ "StateKeyClient",
89
+ "TokenClient",
90
+ "Wallet",
91
+ "XianException",
92
+ "indexed_event_sort_key",
93
+ "merged_event_payload",
94
+ "run_sync",
95
+ "to_contract_time",
96
+ ]
@@ -0,0 +1,470 @@
1
+ from __future__ import annotations
2
+
3
+ from dataclasses import dataclass
4
+ from typing import Any
5
+
6
+ from xian_runtime_types.decimal import ContractingDecimal
7
+
8
+ from xian_py.models import IndexedEvent, StateEntry, TransactionSubmission
9
+
10
+
11
+ def _merge_call_kwargs(
12
+ kwargs: dict[str, Any] | None,
13
+ extra_kwargs: dict[str, Any],
14
+ ) -> dict[str, Any]:
15
+ if kwargs is None:
16
+ return dict(extra_kwargs)
17
+ if extra_kwargs:
18
+ merged = dict(kwargs)
19
+ merged.update(extra_kwargs)
20
+ return merged
21
+ return dict(kwargs)
22
+
23
+
24
+ @dataclass(frozen=True)
25
+ class AsyncEventClient:
26
+ client: Any
27
+ contract: str
28
+ event: str
29
+
30
+ async def list(
31
+ self,
32
+ *,
33
+ limit: int = 100,
34
+ offset: int = 0,
35
+ after_id: int | None = None,
36
+ ) -> list[IndexedEvent]:
37
+ return await self.client.list_events(
38
+ self.contract,
39
+ self.event,
40
+ limit=limit,
41
+ offset=offset,
42
+ after_id=after_id,
43
+ )
44
+
45
+ async def watch(
46
+ self,
47
+ *,
48
+ after_id: int | None = None,
49
+ limit: int | None = None,
50
+ poll_interval_seconds: float | None = None,
51
+ ):
52
+ async for item in self.client.watch_events(
53
+ self.contract,
54
+ self.event,
55
+ after_id=after_id,
56
+ limit=limit,
57
+ poll_interval_seconds=poll_interval_seconds,
58
+ ):
59
+ yield item
60
+
61
+
62
+ @dataclass(frozen=True)
63
+ class EventClient:
64
+ client: Any
65
+ contract: str
66
+ event: str
67
+
68
+ def list(
69
+ self,
70
+ *,
71
+ limit: int = 100,
72
+ offset: int = 0,
73
+ after_id: int | None = None,
74
+ ) -> list[IndexedEvent]:
75
+ return self.client.list_events(
76
+ self.contract,
77
+ self.event,
78
+ limit=limit,
79
+ offset=offset,
80
+ after_id=after_id,
81
+ )
82
+
83
+ def watch(
84
+ self,
85
+ *,
86
+ after_id: int | None = None,
87
+ limit: int | None = None,
88
+ poll_interval_seconds: float | None = None,
89
+ ):
90
+ return self.client.watch_events(
91
+ self.contract,
92
+ self.event,
93
+ after_id=after_id,
94
+ limit=limit,
95
+ poll_interval_seconds=poll_interval_seconds,
96
+ )
97
+
98
+
99
+ @dataclass(frozen=True)
100
+ class AsyncStateKeyClient:
101
+ client: Any
102
+ contract: str
103
+ variable: str
104
+ keys: tuple[str, ...]
105
+
106
+ @property
107
+ def full_key(self) -> str:
108
+ suffix = f":{':'.join(self.keys)}" if self.keys else ""
109
+ return f"{self.contract}.{self.variable}{suffix}"
110
+
111
+ async def get(self) -> Any:
112
+ return await self.client.get_state(
113
+ self.contract, self.variable, *self.keys
114
+ )
115
+
116
+ async def history(
117
+ self,
118
+ *,
119
+ limit: int = 100,
120
+ offset: int = 0,
121
+ ) -> list[StateEntry]:
122
+ return await self.client.get_state_history(
123
+ self.full_key,
124
+ limit=limit,
125
+ offset=offset,
126
+ )
127
+
128
+
129
+ @dataclass(frozen=True)
130
+ class StateKeyClient:
131
+ client: Any
132
+ contract: str
133
+ variable: str
134
+ keys: tuple[str, ...]
135
+
136
+ @property
137
+ def full_key(self) -> str:
138
+ suffix = f":{':'.join(self.keys)}" if self.keys else ""
139
+ return f"{self.contract}.{self.variable}{suffix}"
140
+
141
+ def get(self) -> Any:
142
+ return self.client.get_state(self.contract, self.variable, *self.keys)
143
+
144
+ def history(
145
+ self,
146
+ *,
147
+ limit: int = 100,
148
+ offset: int = 0,
149
+ ) -> list[StateEntry]:
150
+ return self.client.get_state_history(
151
+ self.full_key,
152
+ limit=limit,
153
+ offset=offset,
154
+ )
155
+
156
+
157
+ @dataclass(frozen=True)
158
+ class AsyncContractClient:
159
+ client: Any
160
+ name: str
161
+
162
+ async def get_state(self, variable: str, *keys: str) -> Any:
163
+ return await self.client.get_state(self.name, variable, *keys)
164
+
165
+ def state_key(self, variable: str, *keys: str) -> AsyncStateKeyClient:
166
+ return AsyncStateKeyClient(
167
+ self.client, self.name, variable, tuple(keys)
168
+ )
169
+
170
+ async def simulate(
171
+ self,
172
+ function: str,
173
+ *,
174
+ kwargs: dict[str, Any] | None = None,
175
+ **contract_kwargs: Any,
176
+ ) -> dict[str, Any]:
177
+ return await self.client.simulate(
178
+ self.name,
179
+ function,
180
+ _merge_call_kwargs(kwargs, contract_kwargs),
181
+ )
182
+
183
+ async def call(
184
+ self,
185
+ function: str,
186
+ *,
187
+ kwargs: dict[str, Any] | None = None,
188
+ **contract_kwargs: Any,
189
+ ) -> Any:
190
+ return await self.client.call(
191
+ self.name,
192
+ function,
193
+ _merge_call_kwargs(kwargs, contract_kwargs),
194
+ )
195
+
196
+ async def send(
197
+ self,
198
+ function: str,
199
+ *,
200
+ kwargs: dict[str, Any] | None = None,
201
+ stamps: int | None = None,
202
+ nonce: int | None = None,
203
+ chain_id: str | None = None,
204
+ mode: str | None = None,
205
+ wait_for_tx: bool | None = None,
206
+ timeout_seconds: float | None = None,
207
+ poll_interval_seconds: float | None = None,
208
+ stamp_margin: float | None = None,
209
+ min_stamp_headroom: int | None = None,
210
+ **contract_kwargs: Any,
211
+ ) -> TransactionSubmission:
212
+ return await self.client.send_tx(
213
+ contract=self.name,
214
+ function=function,
215
+ kwargs=_merge_call_kwargs(kwargs, contract_kwargs),
216
+ stamps=stamps,
217
+ nonce=nonce,
218
+ chain_id=chain_id,
219
+ mode=mode,
220
+ wait_for_tx=wait_for_tx,
221
+ timeout_seconds=timeout_seconds,
222
+ poll_interval_seconds=poll_interval_seconds,
223
+ stamp_margin=stamp_margin,
224
+ min_stamp_headroom=min_stamp_headroom,
225
+ )
226
+
227
+ async def get_source(self) -> str | None:
228
+ return await self.client.get_contract(self.name)
229
+
230
+ async def get_code(self) -> str | None:
231
+ return await self.client.get_contract_code(self.name)
232
+
233
+ def events(self, event: str) -> AsyncEventClient:
234
+ return AsyncEventClient(self.client, self.name, event)
235
+
236
+
237
+ @dataclass(frozen=True)
238
+ class ContractClient:
239
+ client: Any
240
+ name: str
241
+
242
+ def get_state(self, variable: str, *keys: str) -> Any:
243
+ return self.client.get_state(self.name, variable, *keys)
244
+
245
+ def state_key(self, variable: str, *keys: str) -> StateKeyClient:
246
+ return StateKeyClient(self.client, self.name, variable, tuple(keys))
247
+
248
+ def simulate(
249
+ self,
250
+ function: str,
251
+ *,
252
+ kwargs: dict[str, Any] | None = None,
253
+ **contract_kwargs: Any,
254
+ ) -> dict[str, Any]:
255
+ return self.client.simulate(
256
+ self.name,
257
+ function,
258
+ _merge_call_kwargs(kwargs, contract_kwargs),
259
+ )
260
+
261
+ def call(
262
+ self,
263
+ function: str,
264
+ *,
265
+ kwargs: dict[str, Any] | None = None,
266
+ **contract_kwargs: Any,
267
+ ) -> Any:
268
+ return self.client.call(
269
+ self.name,
270
+ function,
271
+ _merge_call_kwargs(kwargs, contract_kwargs),
272
+ )
273
+
274
+ def send(
275
+ self,
276
+ function: str,
277
+ *,
278
+ kwargs: dict[str, Any] | None = None,
279
+ stamps: int | None = None,
280
+ nonce: int | None = None,
281
+ chain_id: str | None = None,
282
+ mode: str | None = None,
283
+ wait_for_tx: bool | None = None,
284
+ timeout_seconds: float | None = None,
285
+ poll_interval_seconds: float | None = None,
286
+ stamp_margin: float | None = None,
287
+ min_stamp_headroom: int | None = None,
288
+ **contract_kwargs: Any,
289
+ ) -> TransactionSubmission:
290
+ return self.client.send_tx(
291
+ contract=self.name,
292
+ function=function,
293
+ kwargs=_merge_call_kwargs(kwargs, contract_kwargs),
294
+ stamps=stamps,
295
+ nonce=nonce,
296
+ chain_id=chain_id,
297
+ mode=mode,
298
+ wait_for_tx=wait_for_tx,
299
+ timeout_seconds=timeout_seconds,
300
+ poll_interval_seconds=poll_interval_seconds,
301
+ stamp_margin=stamp_margin,
302
+ min_stamp_headroom=min_stamp_headroom,
303
+ )
304
+
305
+ def get_source(self) -> str | None:
306
+ return self.client.get_contract(self.name)
307
+
308
+ def get_code(self) -> str | None:
309
+ return self.client.get_contract_code(self.name)
310
+
311
+ def events(self, event: str) -> EventClient:
312
+ return EventClient(self.client, self.name, event)
313
+
314
+
315
+ @dataclass(frozen=True)
316
+ class AsyncTokenClient(AsyncContractClient):
317
+ async def balance_of(
318
+ self,
319
+ address: str | None = None,
320
+ ) -> int | ContractingDecimal:
321
+ return await self.client.get_balance(
322
+ address=address, contract=self.name
323
+ )
324
+
325
+ async def transfer(
326
+ self,
327
+ to_address: str,
328
+ amount: int | float | str | ContractingDecimal,
329
+ *,
330
+ stamps: int | None = None,
331
+ mode: str | None = None,
332
+ wait_for_tx: bool | None = None,
333
+ timeout_seconds: float | None = None,
334
+ poll_interval_seconds: float | None = None,
335
+ stamp_margin: float | None = None,
336
+ min_stamp_headroom: int | None = None,
337
+ ) -> TransactionSubmission:
338
+ return await self.client.send(
339
+ amount=amount,
340
+ to_address=to_address,
341
+ token=self.name,
342
+ stamps=stamps,
343
+ mode=mode,
344
+ wait_for_tx=wait_for_tx,
345
+ timeout_seconds=timeout_seconds,
346
+ poll_interval_seconds=poll_interval_seconds,
347
+ stamp_margin=stamp_margin,
348
+ min_stamp_headroom=min_stamp_headroom,
349
+ )
350
+
351
+ async def approve(
352
+ self,
353
+ spender: str,
354
+ *,
355
+ amount: int | float | str | ContractingDecimal = 999999999999,
356
+ stamps: int | None = None,
357
+ mode: str | None = None,
358
+ wait_for_tx: bool | None = None,
359
+ timeout_seconds: float | None = None,
360
+ poll_interval_seconds: float | None = None,
361
+ stamp_margin: float | None = None,
362
+ min_stamp_headroom: int | None = None,
363
+ ) -> TransactionSubmission:
364
+ return await self.client.approve(
365
+ contract=spender,
366
+ token=self.name,
367
+ amount=amount,
368
+ stamps=stamps,
369
+ mode=mode,
370
+ wait_for_tx=wait_for_tx,
371
+ timeout_seconds=timeout_seconds,
372
+ poll_interval_seconds=poll_interval_seconds,
373
+ stamp_margin=stamp_margin,
374
+ min_stamp_headroom=min_stamp_headroom,
375
+ )
376
+
377
+ async def allowance(
378
+ self,
379
+ spender: str,
380
+ *,
381
+ owner: str | None = None,
382
+ ) -> int | ContractingDecimal:
383
+ value = await self.client.get_state(
384
+ self.name,
385
+ "approvals",
386
+ owner or self.client.wallet.public_key,
387
+ spender,
388
+ )
389
+ return 0 if value is None else value
390
+
391
+ def transfers(self) -> AsyncEventClient:
392
+ return self.events("Transfer")
393
+
394
+
395
+ @dataclass(frozen=True)
396
+ class TokenClient(ContractClient):
397
+ def balance_of(
398
+ self,
399
+ address: str | None = None,
400
+ ) -> int | ContractingDecimal:
401
+ return self.client.get_balance(address=address, contract=self.name)
402
+
403
+ def transfer(
404
+ self,
405
+ to_address: str,
406
+ amount: int | float | str | ContractingDecimal,
407
+ *,
408
+ stamps: int | None = None,
409
+ mode: str | None = None,
410
+ wait_for_tx: bool | None = None,
411
+ timeout_seconds: float | None = None,
412
+ poll_interval_seconds: float | None = None,
413
+ stamp_margin: float | None = None,
414
+ min_stamp_headroom: int | None = None,
415
+ ) -> TransactionSubmission:
416
+ return self.client.send(
417
+ amount=amount,
418
+ to_address=to_address,
419
+ token=self.name,
420
+ stamps=stamps,
421
+ mode=mode,
422
+ wait_for_tx=wait_for_tx,
423
+ timeout_seconds=timeout_seconds,
424
+ poll_interval_seconds=poll_interval_seconds,
425
+ stamp_margin=stamp_margin,
426
+ min_stamp_headroom=min_stamp_headroom,
427
+ )
428
+
429
+ def approve(
430
+ self,
431
+ spender: str,
432
+ *,
433
+ amount: int | float | str | ContractingDecimal = 999999999999,
434
+ stamps: int | None = None,
435
+ mode: str | None = None,
436
+ wait_for_tx: bool | None = None,
437
+ timeout_seconds: float | None = None,
438
+ poll_interval_seconds: float | None = None,
439
+ stamp_margin: float | None = None,
440
+ min_stamp_headroom: int | None = None,
441
+ ) -> TransactionSubmission:
442
+ return self.client.approve(
443
+ contract=spender,
444
+ token=self.name,
445
+ amount=amount,
446
+ stamps=stamps,
447
+ mode=mode,
448
+ wait_for_tx=wait_for_tx,
449
+ timeout_seconds=timeout_seconds,
450
+ poll_interval_seconds=poll_interval_seconds,
451
+ stamp_margin=stamp_margin,
452
+ min_stamp_headroom=min_stamp_headroom,
453
+ )
454
+
455
+ def allowance(
456
+ self,
457
+ spender: str,
458
+ *,
459
+ owner: str | None = None,
460
+ ) -> int | ContractingDecimal:
461
+ value = self.client.get_state(
462
+ self.name,
463
+ "approvals",
464
+ owner or self.client.wallet.public_key,
465
+ spender,
466
+ )
467
+ return 0 if value is None else value
468
+
469
+ def transfers(self) -> EventClient:
470
+ return self.events("Transfer")
xian_py/async_utils.py ADDED
@@ -0,0 +1,54 @@
1
+ """
2
+ Utilities for async/sync compatibility
3
+ """
4
+
5
+ import asyncio
6
+ import functools
7
+ from typing import Any, Callable, Coroutine, TypeVar
8
+
9
+ T = TypeVar("T")
10
+
11
+
12
+ def sync_wrapper(
13
+ async_func: Callable[..., Coroutine[Any, Any, T]],
14
+ ) -> Callable[..., T]:
15
+ """
16
+ Wrap an async method with a synchronous facade.
17
+
18
+ - Raises if called while an event loop is running (use the async API).
19
+ - Advanced users can import `run_sync()` from `xian_py.run_sync` and
20
+ opt-in to thread-backed execution if they know what they're doing.
21
+
22
+ This allows async functions to be called from synchronous code by
23
+ automatically managing the event loop.
24
+ """
25
+
26
+ @functools.wraps(async_func)
27
+ def wrapper(*args, **kwargs) -> T:
28
+ try:
29
+ # Try to get the running event loop
30
+ asyncio.get_running_loop()
31
+ except RuntimeError:
32
+ # No event loop is running, so we can safely use asyncio.run
33
+ return asyncio.run(async_func(*args, **kwargs))
34
+ else:
35
+ # We're already in an async context, which shouldn't happen
36
+ # for a sync wrapper, but handle it gracefully
37
+ raise RuntimeError(
38
+ f"Cannot call sync wrapper for {async_func.__name__} from within "
39
+ "an async context. Use the async version directly."
40
+ )
41
+
42
+ # Preserve the original async function as an attribute
43
+ wrapper._async = async_func
44
+ return wrapper
45
+
46
+
47
+ def create_sync_version(
48
+ async_func: Callable[..., Coroutine[Any, Any, T]],
49
+ ) -> Callable[..., T]:
50
+ """
51
+ Creates a synchronous version of an async function.
52
+ This is an alias for sync_wrapper for clarity.
53
+ """
54
+ return sync_wrapper(async_func)
xian_py/config.py ADDED
@@ -0,0 +1,47 @@
1
+ from __future__ import annotations
2
+
3
+ from dataclasses import dataclass, field
4
+ from typing import Literal
5
+
6
+
7
+ @dataclass(frozen=True)
8
+ class TransportConfig:
9
+ total_timeout_seconds: float = 15.0
10
+ connect_timeout_seconds: float = 3.0
11
+ read_timeout_seconds: float = 10.0
12
+ connection_limit: int = 100
13
+ dns_cache_ttl_seconds: int = 300
14
+
15
+
16
+ @dataclass(frozen=True)
17
+ class RetryPolicy:
18
+ max_attempts: int = 3
19
+ initial_delay_seconds: float = 0.25
20
+ max_delay_seconds: float = 2.0
21
+ backoff_multiplier: float = 2.0
22
+ retry_transport_errors: bool = True
23
+ retry_rpc_errors: bool = False
24
+
25
+
26
+ @dataclass(frozen=True)
27
+ class SubmissionConfig:
28
+ mode: Literal["async", "checktx", "commit"] = "checktx"
29
+ wait_for_tx: bool = False
30
+ timeout_seconds: float = 30.0
31
+ poll_interval_seconds: float = 0.25
32
+ stamp_margin: float = 0.10
33
+ min_stamp_headroom: int = 10
34
+
35
+
36
+ @dataclass(frozen=True)
37
+ class WatcherConfig:
38
+ poll_interval_seconds: float = 1.0
39
+ batch_limit: int = 100
40
+
41
+
42
+ @dataclass(frozen=True)
43
+ class XianClientConfig:
44
+ transport: TransportConfig = field(default_factory=TransportConfig)
45
+ retry: RetryPolicy = field(default_factory=RetryPolicy)
46
+ submission: SubmissionConfig = field(default_factory=SubmissionConfig)
47
+ watcher: WatcherConfig = field(default_factory=WatcherConfig)