-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathapp_factory.py
791 lines (690 loc) · 32 KB
/
app_factory.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
import base64
from collections.abc import Callable
from dataclasses import asdict, dataclass, replace
from typing import Any, TypeVar
from algosdk import transaction
from algosdk.abi import Method
from algosdk.atomic_transaction_composer import TransactionSigner
from algosdk.source_map import SourceMap
from algosdk.transaction import OnComplete, Transaction
from typing_extensions import Self
from algokit_utils._legacy_v2.application_specification import ApplicationSpecification
from algokit_utils.applications.abi import (
ABIReturn,
ABIStruct,
ABIValue,
Arc56ReturnValueType,
get_abi_decoded_value,
get_abi_tuple_from_abi_struct,
)
from algokit_utils.applications.app_client import (
AppClient,
AppClientBareCallParams,
AppClientCompilationParams,
AppClientCompilationResult,
AppClientMethodCallParams,
AppClientParams,
)
from algokit_utils.applications.app_deployer import (
AppDeployMetaData,
AppDeployParams,
AppDeployResponse,
AppLookup,
AppMetaData,
OnSchemaBreak,
OnUpdate,
OperationPerformed,
)
from algokit_utils.applications.app_manager import DELETABLE_TEMPLATE_NAME, UPDATABLE_TEMPLATE_NAME
from algokit_utils.applications.app_spec.arc56 import Arc56Contract
from algokit_utils.models.application import (
AppSourceMaps,
)
from algokit_utils.models.state import TealTemplateParams
from algokit_utils.models.transaction import SendParams
from algokit_utils.protocols.client import AlgorandClientProtocol
from algokit_utils.transactions.transaction_composer import (
AppCreateMethodCallParams,
AppCreateParams,
AppDeleteMethodCallParams,
AppDeleteParams,
AppUpdateMethodCallParams,
AppUpdateParams,
BuiltTransactions,
)
from algokit_utils.transactions.transaction_sender import (
SendAppCreateTransactionResult,
SendAppTransactionResult,
SendAppUpdateTransactionResult,
SendSingleTransactionResult,
)
T = TypeVar("T")
__all__ = [
"AppFactory",
"AppFactoryCreateMethodCallParams",
"AppFactoryCreateMethodCallResult",
"AppFactoryCreateMethodCallWithSendParams",
"AppFactoryCreateParams",
"AppFactoryCreateWithSendParams",
"AppFactoryDeployResponse",
"AppFactoryParams",
"SendAppCreateFactoryTransactionResult",
"SendAppFactoryTransactionResult",
"SendAppUpdateFactoryTransactionResult",
]
@dataclass(kw_only=True, frozen=True)
class AppFactoryParams:
algorand: AlgorandClientProtocol
app_spec: Arc56Contract | ApplicationSpecification | str
app_name: str | None = None
default_sender: str | bytes | None = None
default_signer: TransactionSigner | None = None
version: str | None = None
updatable: bool | None = None
deletable: bool | None = None
deploy_time_params: TealTemplateParams | None = None
@dataclass(kw_only=True, frozen=True)
class AppFactoryCreateParams(AppClientBareCallParams, AppClientCompilationParams):
on_complete: transaction.OnComplete | None = None
schema: dict[str, int] | None = None
extra_program_pages: int | None = None
@dataclass(kw_only=True, frozen=True)
class AppFactoryCreateWithSendParams(AppFactoryCreateParams, SendParams):
pass
@dataclass(kw_only=True, frozen=True)
class AppFactoryCreateMethodCallParams(AppClientMethodCallParams, AppClientCompilationParams):
on_complete: transaction.OnComplete | None = None
schema: dict[str, int] | None = None
extra_program_pages: int | None = None
@dataclass(frozen=True, kw_only=True)
class AppFactoryCreateMethodCallResult(SendSingleTransactionResult):
app_id: int
app_address: str
compiled_approval: Any | None = None
compiled_clear: Any | None = None
abi_return: ABIValue | ABIStruct | None = None
@dataclass(kw_only=True, frozen=True)
class AppFactoryCreateMethodCallWithSendParams(AppFactoryCreateMethodCallParams, SendParams):
pass
@dataclass(frozen=True)
class SendAppFactoryTransactionResult(SendAppTransactionResult):
abi_value: Arc56ReturnValueType | None = None
@dataclass(frozen=True)
class SendAppUpdateFactoryTransactionResult(SendAppUpdateTransactionResult):
abi_value: Arc56ReturnValueType | None = None
@dataclass(frozen=True, kw_only=True)
class SendAppCreateFactoryTransactionResult(SendAppCreateTransactionResult):
abi_value: Arc56ReturnValueType | None = None
@dataclass(frozen=True)
class AppFactoryDeployResponse:
"""Result from deploying an application via AppFactory"""
app: AppMetaData
operation_performed: OperationPerformed
create_response: SendAppCreateFactoryTransactionResult | None = None
update_response: SendAppUpdateFactoryTransactionResult | None = None
delete_response: SendAppFactoryTransactionResult | None = None
@classmethod
def from_deploy_response(
cls,
response: AppDeployResponse,
deploy_params: AppDeployParams,
app_spec: Arc56Contract,
app_compilation_data: AppClientCompilationResult | None = None,
) -> Self:
def to_factory_response(
response_data: SendAppTransactionResult
| SendAppCreateTransactionResult
| SendAppUpdateTransactionResult
| None,
params: Any, # noqa: ANN401
) -> Any | None: # noqa: ANN401
if not response_data:
return None
abi_value = None
abi_return = response_data.abi_return
if abi_return and abi_return.method:
abi_value = abi_return.get_arc56_value(params.method, app_spec.structs)
match response_data:
case SendAppCreateTransactionResult():
return SendAppCreateFactoryTransactionResult(**asdict(response_data), abi_value=abi_value)
case SendAppUpdateTransactionResult():
raw_response = asdict(response_data)
raw_response["compiled_approval"] = (
app_compilation_data.compiled_approval if app_compilation_data else None
)
raw_response["compiled_clear"] = (
app_compilation_data.compiled_clear if app_compilation_data else None
)
return SendAppUpdateFactoryTransactionResult(**raw_response, abi_value=abi_value)
case SendAppTransactionResult():
return SendAppFactoryTransactionResult(**asdict(response_data), abi_value=abi_value)
return cls(
app=response.app,
operation_performed=response.operation_performed,
create_response=to_factory_response(
response.create_response,
deploy_params.create_params,
),
update_response=to_factory_response(
response.update_response,
deploy_params.update_params,
),
delete_response=to_factory_response(
response.delete_response,
deploy_params.delete_params,
),
)
class _AppFactoryBareParamsAccessor:
def __init__(self, factory: "AppFactory") -> None:
self._factory = factory
self._algorand = factory._algorand
def create(self, params: AppFactoryCreateParams | None = None) -> AppCreateParams:
base_params = params or AppFactoryCreateParams()
compiled = self._factory.compile(base_params)
return AppCreateParams(
approval_program=compiled.approval_program,
clear_state_program=compiled.clear_state_program,
schema=base_params.schema
or {
"global_bytes": self._factory._app_spec.state.schema.global_state.bytes,
"global_ints": self._factory._app_spec.state.schema.global_state.ints,
"local_bytes": self._factory._app_spec.state.schema.local_state.bytes,
"local_ints": self._factory._app_spec.state.schema.local_state.ints,
},
sender=self._factory._get_sender(base_params.sender),
signer=self._factory._get_signer(base_params.sender, base_params.signer),
on_complete=base_params.on_complete or OnComplete.NoOpOC,
extra_program_pages=base_params.extra_program_pages,
)
def deploy_update(self, params: AppClientBareCallParams | None = None) -> AppUpdateParams:
return AppUpdateParams(
app_id=0,
approval_program="",
clear_state_program="",
sender=self._factory._get_sender(params.sender if params else None),
on_complete=OnComplete.UpdateApplicationOC,
signer=self._factory._get_signer(params.sender if params else None, params.signer if params else None),
note=params.note if params else None,
lease=params.lease if params else None,
rekey_to=params.rekey_to if params else None,
account_references=params.account_references if params else None,
app_references=params.app_references if params else None,
asset_references=params.asset_references if params else None,
box_references=params.box_references if params else None,
)
def deploy_delete(self, params: AppClientBareCallParams | None = None) -> AppDeleteParams:
return AppDeleteParams(
app_id=0,
sender=self._factory._get_sender(params.sender if params else None),
signer=self._factory._get_signer(params.sender if params else None, params.signer if params else None),
on_complete=OnComplete.DeleteApplicationOC,
note=params.note if params else None,
lease=params.lease if params else None,
rekey_to=params.rekey_to if params else None,
account_references=params.account_references if params else None,
app_references=params.app_references if params else None,
asset_references=params.asset_references if params else None,
box_references=params.box_references if params else None,
)
class _AppFactoryParamsAccessor:
def __init__(self, factory: "AppFactory") -> None:
self._factory = factory
self._bare = _AppFactoryBareParamsAccessor(factory)
@property
def bare(self) -> _AppFactoryBareParamsAccessor:
return self._bare
def create(self, params: AppFactoryCreateMethodCallParams) -> AppCreateMethodCallParams:
compiled = self._factory.compile(params)
return AppCreateMethodCallParams(
app_id=0,
approval_program=compiled.approval_program,
clear_state_program=compiled.clear_state_program,
schema=params.schema
or {
"global_bytes": self._factory._app_spec.state.schema.global_state.bytes,
"global_ints": self._factory._app_spec.state.schema.global_state.ints,
"local_bytes": self._factory._app_spec.state.schema.local_state.bytes,
"local_ints": self._factory._app_spec.state.schema.local_state.ints,
},
sender=self._factory._get_sender(params.sender),
signer=self._factory._get_signer(params.sender if params else None, params.signer if params else None),
method=self._factory._app_spec.get_arc56_method(params.method).to_abi_method(),
args=self._factory._get_create_abi_args_with_default_values(params.method, params.args),
on_complete=params.on_complete or OnComplete.NoOpOC,
note=params.note,
lease=params.lease,
rekey_to=params.rekey_to,
)
def deploy_update(self, params: AppClientMethodCallParams) -> AppUpdateMethodCallParams:
return AppUpdateMethodCallParams(
app_id=0,
approval_program="",
clear_state_program="",
sender=self._factory._get_sender(params.sender),
signer=self._factory._get_signer(params.sender if params else None, params.signer if params else None),
method=self._factory._app_spec.get_arc56_method(params.method).to_abi_method(),
args=self._factory._get_create_abi_args_with_default_values(params.method, params.args),
on_complete=OnComplete.UpdateApplicationOC,
note=params.note,
lease=params.lease,
rekey_to=params.rekey_to,
)
def deploy_delete(self, params: AppClientMethodCallParams) -> AppDeleteMethodCallParams:
return AppDeleteMethodCallParams(
app_id=0,
sender=self._factory._get_sender(params.sender),
signer=self._factory._get_signer(params.sender if params else None, params.signer if params else None),
method=self._factory.app_spec.get_arc56_method(params.method).to_abi_method(),
args=self._factory._get_create_abi_args_with_default_values(params.method, params.args),
on_complete=OnComplete.DeleteApplicationOC,
note=params.note,
lease=params.lease,
rekey_to=params.rekey_to,
)
class _AppFactoryBareCreateTransactionAccessor:
def __init__(self, factory: "AppFactory") -> None:
self._factory = factory
def create(self, params: AppFactoryCreateParams | None = None) -> Transaction:
return self._factory._algorand.create_transaction.app_create(self._factory.params.bare.create(params))
class _AppFactoryCreateTransactionAccessor:
def __init__(self, factory: "AppFactory") -> None:
self._factory = factory
self._bare = _AppFactoryBareCreateTransactionAccessor(factory)
@property
def bare(self) -> _AppFactoryBareCreateTransactionAccessor:
return self._bare
def create(self, params: AppFactoryCreateMethodCallParams) -> BuiltTransactions:
return self._factory._algorand.create_transaction.app_create_method_call(self._factory.params.create(params))
class _AppFactoryBareSendAccessor:
def __init__(self, factory: "AppFactory") -> None:
self._factory = factory
self._algorand = factory._algorand
def create(
self, params: AppFactoryCreateWithSendParams | None = None
) -> tuple[AppClient, SendAppCreateTransactionResult]:
base_params = params or AppFactoryCreateWithSendParams()
# Use replace() to create new instance with overridden values
create_params = replace(
base_params,
updatable=base_params.updatable if base_params.updatable is not None else self._factory._updatable,
deletable=base_params.deletable if base_params.deletable is not None else self._factory._deletable,
deploy_time_params=(
base_params.deploy_time_params
if base_params.deploy_time_params is not None
else self._factory._deploy_time_params
),
)
compiled = self._factory.compile(
AppClientCompilationParams(
deploy_time_params=create_params.deploy_time_params,
updatable=create_params.updatable,
deletable=create_params.deletable,
)
)
result = self._factory._handle_call_errors(
lambda: self._algorand.send.app_create(self._factory.params.bare.create(create_params))
)
return (
self._factory.get_app_client_by_id(
app_id=result.app_id,
),
SendAppCreateTransactionResult(
transaction=result.transaction,
confirmation=result.confirmation,
app_id=result.app_id,
app_address=result.app_address,
compiled_approval=compiled.compiled_approval if compiled else None,
compiled_clear=compiled.compiled_clear if compiled else None,
group_id=result.group_id,
tx_ids=result.tx_ids,
transactions=result.transactions,
confirmations=result.confirmations,
),
)
class _AppFactorySendAccessor:
def __init__(self, factory: "AppFactory") -> None:
self._factory = factory
self._algorand = factory._algorand
self._bare = _AppFactoryBareSendAccessor(factory)
@property
def bare(self) -> _AppFactoryBareSendAccessor:
return self._bare
def create(self, params: AppFactoryCreateMethodCallParams) -> tuple[AppClient, AppFactoryCreateMethodCallResult]:
create_params = replace(
params,
updatable=params.updatable if params.updatable is not None else self._factory._updatable,
deletable=params.deletable if params.deletable is not None else self._factory._deletable,
deploy_time_params=(
params.deploy_time_params
if params.deploy_time_params is not None
else self._factory._deploy_time_params
),
)
compiled = self._factory.compile(
AppClientCompilationParams(
deploy_time_params=create_params.deploy_time_params,
updatable=create_params.updatable,
deletable=create_params.deletable,
)
)
result = self._factory._handle_call_errors(
lambda: self._factory._parse_method_call_return(
lambda: self._algorand.send.app_create_method_call(self._factory.params.create(create_params)),
self._factory._app_spec.get_arc56_method(params.method).to_abi_method(),
)
)
return (
self._factory.get_app_client_by_id(
app_id=result.app_id,
),
AppFactoryCreateMethodCallResult(
transaction=result.transaction,
confirmation=result.confirmation,
tx_id=result.tx_id,
app_id=result.app_id,
app_address=result.app_address,
abi_return=result.abi_return,
compiled_approval=compiled.compiled_approval if compiled else None,
compiled_clear=compiled.compiled_clear if compiled else None,
group_id=result.group_id,
tx_ids=result.tx_ids,
transactions=result.transactions,
confirmations=result.confirmations,
returns=result.returns,
),
)
class AppFactory:
def __init__(self, params: AppFactoryParams) -> None:
self._app_spec = AppClient.normalise_app_spec(params.app_spec)
self._app_name = params.app_name or self._app_spec.name
self._algorand = params.algorand
self._version = params.version or "1.0"
self._default_sender = params.default_sender
self._default_signer = params.default_signer
self._deploy_time_params = params.deploy_time_params
self._updatable = params.updatable
self._deletable = params.deletable
self._approval_source_map: SourceMap | None = None
self._clear_source_map: SourceMap | None = None
self._params_accessor = _AppFactoryParamsAccessor(self)
self._send_accessor = _AppFactorySendAccessor(self)
self._create_transaction_accessor = _AppFactoryCreateTransactionAccessor(self)
@property
def app_name(self) -> str:
return self._app_name
@property
def app_spec(self) -> Arc56Contract:
return self._app_spec
@property
def algorand(self) -> AlgorandClientProtocol:
return self._algorand
@property
def params(self) -> _AppFactoryParamsAccessor:
return self._params_accessor
@property
def send(self) -> _AppFactorySendAccessor:
return self._send_accessor
@property
def create_transaction(self) -> _AppFactoryCreateTransactionAccessor:
return self._create_transaction_accessor
def deploy( # noqa: PLR0913
self,
*,
deploy_time_params: TealTemplateParams | None = None,
on_update: OnUpdate = OnUpdate.Fail,
on_schema_break: OnSchemaBreak = OnSchemaBreak.Fail,
create_params: AppClientMethodCallParams | AppClientBareCallParams | None = None,
update_params: AppClientMethodCallParams | AppClientBareCallParams | None = None,
delete_params: AppClientMethodCallParams | AppClientBareCallParams | None = None,
existing_deployments: AppLookup | None = None,
ignore_cache: bool = False,
updatable: bool | None = None,
deletable: bool | None = None,
app_name: str | None = None,
max_rounds_to_wait: int | None = None,
suppress_log: bool = False,
populate_app_call_resources: bool = False,
) -> tuple[AppClient, AppFactoryDeployResponse]:
"""Deploy the application with the specified parameters."""
# Resolve control parameters with factory defaults
resolved_updatable = (
updatable if updatable is not None else self._updatable or self._get_deploy_time_control("updatable")
)
resolved_deletable = (
deletable if deletable is not None else self._deletable or self._get_deploy_time_control("deletable")
)
resolved_deploy_time_params = deploy_time_params or self._deploy_time_params
def prepare_create_args() -> AppCreateMethodCallParams | AppCreateParams:
"""Prepare create arguments based on parameter type."""
if create_params and isinstance(create_params, AppClientMethodCallParams):
return self.params.create(
AppFactoryCreateMethodCallParams(
**asdict(create_params),
updatable=resolved_updatable,
deletable=resolved_deletable,
deploy_time_params=resolved_deploy_time_params,
)
)
base_params = create_params or AppClientBareCallParams()
return self.params.bare.create(
AppFactoryCreateParams(
**asdict(base_params) if base_params else {},
updatable=resolved_updatable,
deletable=resolved_deletable,
deploy_time_params=resolved_deploy_time_params,
)
)
def prepare_update_args() -> AppUpdateMethodCallParams | AppUpdateParams:
"""Prepare update arguments based on parameter type."""
return (
self.params.deploy_update(update_params)
if isinstance(update_params, AppClientMethodCallParams)
else self.params.bare.deploy_update(update_params)
)
def prepare_delete_args() -> AppDeleteMethodCallParams | AppDeleteParams:
"""Prepare delete arguments based on parameter type."""
return (
self.params.deploy_delete(delete_params)
if isinstance(delete_params, AppClientMethodCallParams)
else self.params.bare.deploy_delete(delete_params)
)
# Execute deployment
deploy_params = AppDeployParams(
deploy_time_params=resolved_deploy_time_params,
on_schema_break=on_schema_break,
on_update=on_update,
existing_deployments=existing_deployments,
ignore_cache=ignore_cache,
create_params=prepare_create_args(),
update_params=prepare_update_args(),
delete_params=prepare_delete_args(),
metadata=AppDeployMetaData(
name=app_name or self._app_name,
version=self._version,
updatable=resolved_updatable,
deletable=resolved_deletable,
),
suppress_log=suppress_log,
max_rounds_to_wait=max_rounds_to_wait,
populate_app_call_resources=populate_app_call_resources,
)
deploy_response = self._algorand.app_deployer.deploy(deploy_params)
# Prepare app client and factory deploy response
app_client = self.get_app_client_by_id(
app_id=deploy_response.app.app_id,
app_name=app_name,
default_sender=self._default_sender,
default_signer=self._default_signer,
)
factory_deploy_response = AppFactoryDeployResponse.from_deploy_response(
response=deploy_response,
deploy_params=deploy_params,
app_spec=app_client.app_spec,
app_compilation_data=self.compile(
AppClientCompilationParams(
deploy_time_params=resolved_deploy_time_params,
updatable=resolved_updatable,
deletable=resolved_deletable,
)
),
)
return app_client, factory_deploy_response
def get_app_client_by_id(
self,
app_id: int,
app_name: str | None = None,
default_sender: str | bytes | None = None, # Address can be string or bytes
default_signer: TransactionSigner | None = None,
approval_source_map: SourceMap | None = None,
clear_source_map: SourceMap | None = None,
) -> AppClient:
return AppClient(
AppClientParams(
app_id=app_id,
algorand=self._algorand,
app_spec=self._app_spec,
app_name=app_name or self._app_name,
default_sender=default_sender or self._default_sender,
default_signer=default_signer or self._default_signer,
approval_source_map=approval_source_map or self._approval_source_map,
clear_source_map=clear_source_map or self._clear_source_map,
)
)
def get_app_client_by_creator_and_name(
self,
creator_address: str,
app_name: str,
default_sender: str | bytes | None = None,
default_signer: TransactionSigner | None = None,
ignore_cache: bool | None = None,
app_lookup_cache: AppLookup | None = None,
approval_source_map: SourceMap | None = None,
clear_source_map: SourceMap | None = None,
) -> AppClient:
return AppClient.from_creator_and_name(
creator_address=creator_address,
app_name=app_name or self._app_name,
default_sender=default_sender or self._default_sender,
default_signer=default_signer or self._default_signer,
approval_source_map=approval_source_map or self._approval_source_map,
clear_source_map=clear_source_map or self._clear_source_map,
ignore_cache=ignore_cache,
app_lookup_cache=app_lookup_cache,
app_spec=self._app_spec,
algorand=self._algorand,
)
def export_source_maps(self) -> AppSourceMaps:
if not self._approval_source_map or not self._clear_source_map:
raise ValueError(
"Unable to export source maps; they haven't been loaded into this client - "
"you need to call create, update, or deploy first"
)
return AppSourceMaps(
approval_source_map=self._approval_source_map,
clear_source_map=self._clear_source_map,
)
def import_source_maps(self, source_maps: AppSourceMaps) -> None:
self._approval_source_map = source_maps.approval_source_map
self._clear_source_map = source_maps.clear_source_map
def compile(self, compilation: AppClientCompilationParams | None = None) -> AppClientCompilationResult:
result = AppClient.compile(
app_spec=self._app_spec,
app_manager=self._algorand.app,
deploy_time_params=compilation.deploy_time_params if compilation else None,
updatable=compilation.updatable if compilation else None,
deletable=compilation.deletable if compilation else None,
)
if result.compiled_approval:
self._approval_source_map = result.compiled_approval.source_map
if result.compiled_clear:
self._clear_source_map = result.compiled_clear.source_map
return result
def _expose_logic_error(self, e: Exception, is_clear_state_program: bool = False) -> Exception: # noqa: FBT002 FBT001
return AppClient._expose_logic_error_static(
e=e,
app_spec=self._app_spec,
is_clear_state_program=is_clear_state_program,
approval_source_map=self._approval_source_map,
clear_source_map=self._clear_source_map,
program=None,
approval_source_info=(self._app_spec.source_info.approval if self._app_spec.source_info else None),
clear_source_info=(self._app_spec.source_info.clear if self._app_spec.source_info else None),
)
def _get_deploy_time_control(self, control: str) -> bool | None:
approval = self._app_spec.source.get_decoded_approval() if self._app_spec.source else None
template_name = UPDATABLE_TEMPLATE_NAME if control == "updatable" else DELETABLE_TEMPLATE_NAME
if not approval or template_name not in approval:
return None
on_complete = "UpdateApplication" if control == "updatable" else "DeleteApplication"
return on_complete in self._app_spec.bare_actions.call or any(
on_complete in m.actions.call for m in self._app_spec.methods if m.actions and m.actions.call
)
def _get_sender(self, sender: str | bytes | None) -> str:
if not sender and not self._default_sender:
raise Exception(
f"No sender provided and no default sender present in app client for call to app {self._app_name}"
)
return str(sender or self._default_sender)
def _get_signer(self, sender: str | None, signer: TransactionSigner | None) -> TransactionSigner | None:
return signer or (self._default_signer if not sender or sender == self._default_sender else None)
def _handle_call_errors(self, call: Callable[[], T]) -> T:
try:
return call()
except Exception as e:
raise self._expose_logic_error(e) from None
def _parse_method_call_return(
self,
result: Callable[
[], SendAppTransactionResult | SendAppCreateTransactionResult | SendAppUpdateTransactionResult
],
method: Method,
) -> AppFactoryCreateMethodCallResult:
result_value = result()
return AppFactoryCreateMethodCallResult(
**{
**result_value.__dict__,
"abi_return": result_value.abi_return.get_arc56_value(method, self._app_spec.structs)
if isinstance(result_value.abi_return, ABIReturn)
else None,
}
)
def _get_create_abi_args_with_default_values(
self,
method_name_or_signature: str,
user_args: list[Any] | None,
) -> list[Any]:
"""
Builds a list of ABI argument values for creation calls, applying default
argument values when not provided.
"""
method = self._app_spec.get_arc56_method(method_name_or_signature)
results: list[Any] = []
for i, param in enumerate(method.args):
if user_args and i < len(user_args):
arg_value = user_args[i]
if param.struct and isinstance(arg_value, dict):
arg_value = get_abi_tuple_from_abi_struct(
arg_value,
self._app_spec.structs[param.struct],
self._app_spec.structs,
)
results.append(arg_value)
continue
default_value = getattr(param, "default_value", None)
if default_value:
if default_value.source == "literal":
raw_value = base64.b64decode(default_value.data)
value_type = default_value.type or str(param.type)
decoded_value = get_abi_decoded_value(raw_value, value_type, self._app_spec.structs)
results.append(decoded_value)
else:
raise ValueError(
f"Cannot provide default value from source={default_value.source} "
"for a contract creation call."
)
else:
param_name = param.name or f"arg{i + 1}"
raise ValueError(
f"No value provided for required argument {param_name} " f"in call to method {method.name}"
)
return results