forked from algorandlabs/smart-asa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
smart_asa_test.py
2510 lines (2283 loc) · 86.1 KB
/
smart_asa_test.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
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Smart ASA test suite
"""
__author__ = "Cosimo Bassi, Stefano De Angelis"
__email__ = "<[email protected]>, <[email protected]>"
import json
import pprint
from typing import Callable
import pytest
from pyteal import Expr, Router
from algosdk.abi import Contract
from algosdk.atomic_transaction_composer import TransactionWithSigner
from algosdk.error import AlgodHTTPError
from algosdk.constants import ZERO_ADDRESS
from algosdk.future.transaction import AssetTransferTxn, PaymentTxn
from sandbox import Sandbox
from account import Account, AppAccount
from smart_asa_asc import (
UNDERLYING_ASA_TOTAL,
LocalState,
compile_stateful,
smart_asa_abi,
)
from smart_asa_client import (
get_smart_asa_params,
get_params,
smart_asa_account_freeze,
smart_asa_app_create,
smart_asa_closeout,
smart_asa_config,
smart_asa_create,
smart_asa_destroy,
smart_asa_freeze,
smart_asa_get,
smart_asa_optin,
smart_asa_transfer,
)
from utils import (
get_global_state,
get_local_state,
get_method,
)
INITIAL_FUNDS = 100_000_000
@pytest.fixture(scope="session")
def smart_asa_abi_router() -> Router:
print("\n --- Creating Smart ASA ABI...")
return smart_asa_abi
@pytest.fixture(scope="session")
def pyteal_approval(smart_asa_abi_router: Router) -> Expr:
approval, _, _ = smart_asa_abi_router.build_program()
print("\n --- Building Smart ASA PyTeal approval program...")
return approval
@pytest.fixture(scope="session")
def pyteal_clear(smart_asa_abi_router: Router) -> Expr:
_, clear, _ = smart_asa_abi_router.build_program()
print("\n --- Building Smart ASA PyTeal clear program...")
return clear
@pytest.fixture(scope="session")
def teal_approval(pyteal_approval: Expr) -> str:
print("\n --- Compiling Smart ASA TEAL approval program...")
return compile_stateful(pyteal_approval)
@pytest.fixture(scope="session")
def teal_clear(pyteal_clear: Expr) -> str:
print("\n --- Compiling Smart ASA TEAL clear program...")
return compile_stateful(pyteal_clear)
@pytest.fixture(scope="session")
def smart_asa_contract(smart_asa_abi_router: Router) -> Contract:
_, _, contract = smart_asa_abi_router.build_program()
print("\n --- Building Smart ASA JSON contract...")
return contract
@pytest.fixture(scope="class")
def creator() -> Account:
print("\n --- Creator Account...")
return Sandbox.create(funds_amount=INITIAL_FUNDS)
@pytest.fixture(scope="class")
def eve() -> Account:
print("\n --- Eve Account...")
return Sandbox.create(funds_amount=INITIAL_FUNDS)
@pytest.fixture(scope="function")
def smart_asa_app(
teal_approval: str,
teal_clear: str,
creator: Account,
) -> AppAccount:
app_account = smart_asa_app_create(
teal_approval=teal_approval,
teal_clear=teal_clear,
creator=creator,
)
creator.pay(receiver=app_account, amount=1_000_000)
print("\n --- Creating Smart ASA App...")
return app_account
@pytest.fixture(
scope="function",
params=[False, True],
ids=["Not Frozen Smart ASA", "Default Frozen Smart ASA"],
)
def smart_asa_id(
smart_asa_contract: Contract,
smart_asa_app: AppAccount,
creator: Account,
request,
) -> int:
print("\n --- Creating Smart ASA...")
return smart_asa_create(
smart_asa_app=smart_asa_app,
creator=creator,
smart_asa_contract=smart_asa_contract,
total=100,
metadata_hash=b"XYZXYZ",
default_frozen=request.param,
)
@pytest.fixture(scope="function")
def opted_in_creator(
smart_asa_contract: Contract,
smart_asa_app: AppAccount,
creator: Account,
smart_asa_id: int,
) -> Account:
print("\n --- Creator opt-in...")
smart_asa_optin(
smart_asa_contract=smart_asa_contract,
smart_asa_app=smart_asa_app,
asset_id=smart_asa_id,
caller=creator,
)
return creator
@pytest.fixture(scope="function")
def creator_with_supply(
smart_asa_contract: Contract,
smart_asa_app: AppAccount,
opted_in_creator: Account,
smart_asa_id: int,
) -> Account:
print("\n --- Minting to Creator...")
smart_asa_transfer(
smart_asa_contract=smart_asa_contract,
smart_asa_app=smart_asa_app,
xfer_asset=smart_asa_id,
asset_amount=50,
caller=opted_in_creator,
asset_receiver=opted_in_creator,
asset_sender=smart_asa_app,
)
return opted_in_creator
@pytest.fixture(scope="function")
def opted_in_account_factory(
smart_asa_contract: Contract, smart_asa_app: AppAccount, smart_asa_id: int
) -> Callable:
def _factory() -> Account:
account = Sandbox.create(funds_amount=INITIAL_FUNDS)
smart_asa_optin(
smart_asa_contract=smart_asa_contract,
smart_asa_app=smart_asa_app,
asset_id=smart_asa_id,
caller=account,
)
print("\n --- Account opt-in...")
return account
return _factory
@pytest.fixture(scope="function")
def account_with_supply_factory(
smart_asa_contract: Contract,
smart_asa_app: AppAccount,
smart_asa_id: int,
creator_with_supply: Account,
opted_in_account_factory: Callable,
) -> Callable:
def _factory() -> Account:
account = opted_in_account_factory()
smart_asa_transfer(
smart_asa_contract=smart_asa_contract,
smart_asa_app=smart_asa_app,
xfer_asset=smart_asa_id,
asset_amount=10,
caller=creator_with_supply,
asset_receiver=account,
)
print("\n --- Minting to Account...")
return account
return _factory
def test_compile(
pyteal_approval: Expr, pyteal_clear: Expr, smart_asa_contract: Contract
) -> None:
# This test simply ensures we can compile the ASC programs
teal_approval_program = compile_stateful(pyteal_approval)
teal_clear_program = compile_stateful(pyteal_clear)
pprint.pprint("\nABI\n" + json.dumps(smart_asa_contract.dictify()))
print("\nAPPROVAL PROGRAM\n" + teal_approval_program)
with open("/tmp/approval.teal", "w") as f:
f.write(teal_approval_program)
print("\nCLEAR PROGRAM\n" + teal_clear_program)
with open("/tmp/clear.teal", "w") as f:
f.write(teal_clear_program)
class TestAppDeployment:
def test_wrong_state_schema(
self,
teal_approval: str,
teal_clear: str,
creator: Account,
) -> None:
with pytest.raises(AlgodHTTPError):
print("\n --- Creating Smart ASA App with wrong State Schema...")
creator.create_asc(
approval_program=teal_approval,
clear_program=teal_clear,
)
print(" --- Rejected as expected!")
def test_app_create_happy_path(
self,
smart_asa_app: AppAccount,
) -> None:
print(f" --- Created Smart ASA App ID: {smart_asa_app.app_id}")
# TODO: test_app_update (fails)
# TODO: test_app_delete (fails)
# TODO: test_app_clear_state (fails)
class TestAssetCreate:
def test_is_not_creator(
self,
smart_asa_contract: Contract,
smart_asa_app: AppAccount,
eve: Account,
) -> None:
print("\n --- Creating Smart ASA not with App Creator...")
with pytest.raises(AlgodHTTPError):
smart_asa_create(
smart_asa_app=smart_asa_app,
creator=eve,
smart_asa_contract=smart_asa_contract,
total=100,
save_abi_call="/tmp/txn.signed",
)
print(" --- Rejected as expected!")
def test_smart_asa_already_created(
self,
smart_asa_contract: Contract,
smart_asa_app: AppAccount,
creator: Account,
smart_asa_id: int,
) -> None:
print("\n --- Creating Smart ASA multiple times...")
with pytest.raises(AlgodHTTPError):
smart_asa_create(
smart_asa_app=smart_asa_app,
creator=creator,
smart_asa_contract=smart_asa_contract,
total=100,
save_abi_call="/tmp/txn.signed",
)
print(" --- Rejected as expected!")
def test_happy_path(
self,
smart_asa_contract: Contract,
smart_asa_app: AppAccount,
creator: Account,
) -> None:
print("\n --- Creating Smart ASA...")
smart_asa_id = smart_asa_create(
smart_asa_app=smart_asa_app,
creator=creator,
smart_asa_contract=smart_asa_contract,
total=100,
)
print(" --- Created Smart ASA ID:", smart_asa_id)
smart_asa = get_smart_asa_params(creator.algod_client, smart_asa_id)
assert smart_asa["smart_asa_id"] == smart_asa_id
assert smart_asa["total"] == 100
assert smart_asa["decimals"] == 0
assert not smart_asa["default_frozen"]
assert not smart_asa["unit_name"]
assert not smart_asa["name"]
assert not smart_asa["url"]
assert smart_asa["metadata_hash"][1] == "\x00"
assert smart_asa["manager_addr"] == creator.address
assert smart_asa["reserve_addr"] == creator.address
assert smart_asa["freeze_addr"] == creator.address
assert smart_asa["clawback_addr"] == creator.address
class TestAssetOptin:
def test_smart_asa_not_created(
self,
smart_asa_contract: Contract,
smart_asa_app: AppAccount,
creator: Account,
) -> None:
print("\n --- Opt-In App with no Smart ASA ID...")
with pytest.raises(AlgodHTTPError):
smart_asa_optin(
smart_asa_contract=smart_asa_contract,
smart_asa_app=smart_asa_app,
asset_id=1,
caller=creator,
)
print(" --- Rejected as expected!")
@pytest.mark.parametrize("smart_asa_id", [False], indirect=True)
def test_wrong_smart_asa_id(
self,
smart_asa_contract: Contract,
smart_asa_app: AppAccount,
creator: Account,
smart_asa_id: int,
) -> None:
creator.optin_to_asset(smart_asa_id)
print("\n --- Opt-In App with wrong Smart ASA ID...")
with pytest.raises(AlgodHTTPError):
smart_asa_optin(
smart_asa_contract=smart_asa_contract,
smart_asa_app=smart_asa_app,
asset_id=smart_asa_id + 1,
caller=creator,
)
print(" --- Rejected as expected!")
def test_optin_group_wrong_asa(
self,
smart_asa_contract: Contract,
smart_asa_app: AppAccount,
creator: Account,
smart_asa_id: int,
) -> None:
wrong_asa = creator.create_asset()
wrong_asa_txn = AssetTransferTxn(
sender=creator.address,
sp=get_params(creator.algod_client),
receiver=creator.address,
amt=0,
index=wrong_asa,
)
wrong_asa_txn = TransactionWithSigner(
txn=wrong_asa_txn,
signer=creator,
)
print("\n --- Opt-In Group with wrong Underlying ASA...")
with pytest.raises(AlgodHTTPError):
smart_asa_optin(
smart_asa_contract=smart_asa_contract,
smart_asa_app=smart_asa_app,
asset_id=smart_asa_id,
caller=creator,
debug_txn=wrong_asa_txn,
)
print(" --- Rejected as expected!")
def test_optin_group_wrong_sender(
self,
smart_asa_contract: Contract,
smart_asa_app: AppAccount,
creator: Account,
eve: Account,
smart_asa_id: int,
) -> None:
eve.optin_to_asset(smart_asa_id)
wrong_sender_txn = AssetTransferTxn(
sender=eve.address,
sp=get_params(eve.algod_client),
receiver=creator.address,
amt=0,
index=smart_asa_id,
)
wrong_sender_txn = TransactionWithSigner(
txn=wrong_sender_txn,
signer=eve,
)
print("\n --- Opt-In Group with wrong Sender...")
with pytest.raises(AlgodHTTPError):
smart_asa_optin(
smart_asa_contract=smart_asa_contract,
smart_asa_app=smart_asa_app,
asset_id=smart_asa_id,
caller=creator,
debug_txn=wrong_sender_txn,
)
print(" --- Rejected as expected!")
def test_optin_group_wrong_receiver(
self,
smart_asa_contract: Contract,
smart_asa_app: AppAccount,
creator: Account,
eve: Account,
smart_asa_id: int,
) -> None:
eve.optin_to_asset(smart_asa_id)
wrong_receiver_txn = AssetTransferTxn(
sender=creator.address,
sp=get_params(creator.algod_client),
receiver=eve.address,
amt=0,
index=smart_asa_id,
)
wrong_receiver_txn = TransactionWithSigner(
txn=wrong_receiver_txn,
signer=creator,
)
print("\n --- Opt-In Group with wrong Receiver...")
with pytest.raises(AlgodHTTPError):
smart_asa_optin(
smart_asa_contract=smart_asa_contract,
smart_asa_app=smart_asa_app,
asset_id=smart_asa_id,
caller=creator,
debug_txn=wrong_receiver_txn,
)
print(" --- Rejected as expected!")
@pytest.mark.parametrize("smart_asa_id", [False], indirect=True)
def test_optin_group_wrong_amount(
self,
smart_asa_contract: Contract,
smart_asa_app: AppAccount,
creator_with_supply: Account,
smart_asa_id: int,
) -> None:
wrong_amount_txn = AssetTransferTxn(
sender=creator_with_supply.address,
sp=get_params(creator_with_supply.algod_client),
receiver=creator_with_supply.address,
amt=1,
index=smart_asa_id,
)
wrong_amount_txn = TransactionWithSigner(
txn=wrong_amount_txn,
signer=creator_with_supply,
)
print("\n --- Opt-In Group with wrong amount...")
with pytest.raises(AlgodHTTPError):
smart_asa_optin(
smart_asa_contract=smart_asa_contract,
smart_asa_app=smart_asa_app,
asset_id=smart_asa_id,
caller=creator_with_supply,
debug_txn=wrong_amount_txn,
)
print(" --- Rejected as expected!")
@pytest.mark.parametrize("smart_asa_id", [False], indirect=True)
def test_optin_group_with_asset_close_to(
self,
smart_asa_contract: Contract,
smart_asa_app: AppAccount,
creator: Account,
smart_asa_id: int,
) -> None:
wrong_close_to_txn = AssetTransferTxn(
sender=creator.address,
sp=get_params(creator.algod_client),
receiver=creator.address,
amt=0,
index=smart_asa_id,
close_assets_to=smart_asa_app.address,
)
wrong_close_to_txn = TransactionWithSigner(
txn=wrong_close_to_txn,
signer=creator,
)
print("\n --- Opt-In Group with Close Asset To...")
with pytest.raises(AlgodHTTPError):
smart_asa_optin(
smart_asa_contract=smart_asa_contract,
smart_asa_app=smart_asa_app,
asset_id=smart_asa_id,
caller=creator,
debug_txn=wrong_close_to_txn,
)
print(" --- Rejected as expected!")
def test_happy_path(
self,
smart_asa_app: AppAccount,
opted_in_creator: Account,
) -> None:
smart_asa = get_global_state(
algod_client=opted_in_creator.algod_client,
asc_idx=smart_asa_app.app_id,
)
local_state = get_local_state(
algod_client=opted_in_creator.algod_client,
account_address=opted_in_creator.address,
asc_idx=smart_asa_app.app_id,
)
if smart_asa["default_frozen"]:
assert local_state["frozen"]
else:
assert not local_state["frozen"]
class TestAssetConfig:
def test_smart_asa_not_created(
self,
smart_asa_contract: Contract,
smart_asa_app: AppAccount,
creator: Account,
) -> None:
wrong_asa = creator.create_asset()
print("\n --- Configuring unexisting Smart ASA...")
with pytest.raises(AlgodHTTPError):
creator.abi_call(
get_method(smart_asa_contract, "asset_config"),
wrong_asa,
100,
2,
True,
"FOO",
"Foo",
"foo.spam",
b"foo",
creator.address,
creator.address,
creator.address,
creator.address,
app=smart_asa_app,
fee=creator.algod_client.suggested_params().fee,
)
print(" --- Rejected as expected!")
def test_is_not_manager(
self,
smart_asa_contract: Contract,
smart_asa_app: AppAccount,
eve: Account,
smart_asa_id: int,
) -> None:
# NOTE: This test ensures also that once `manager_add` is set to
# ZERO_ADDR the Smart ASA can no longer be configured.
print("\n --- Configuring Smart ASA not with Smart ASA Manager...")
with pytest.raises(AlgodHTTPError):
smart_asa_config(
smart_asa_contract=smart_asa_contract,
smart_asa_app=smart_asa_app,
manager=eve,
asset_id=smart_asa_id,
config_manager_addr=ZERO_ADDRESS,
save_abi_call="/tmp/txn.signed",
)
print(" --- Rejected as expected!")
def test_is_not_correct_smart_asa(
self,
smart_asa_contract: Contract,
smart_asa_app: AppAccount,
creator: Account,
smart_asa_id: int,
) -> None:
wrong_asa = creator.create_asset()
print("\n --- Configuring Smart ASA with wrong Asset ID...")
with pytest.raises(AlgodHTTPError):
creator.abi_call(
get_method(smart_asa_contract, "asset_config"),
wrong_asa,
100,
2,
True,
"FOO",
"Foo",
"foo.spam",
b"foo",
creator.address,
creator.address,
creator.address,
creator.address,
app=smart_asa_app,
fee=creator.algod_client.suggested_params().fee,
)
print(" --- Rejected as expected!")
def test_disabled_frozen_and_clawback(
self,
smart_asa_contract: Contract,
smart_asa_app: AppAccount,
creator: Account,
smart_asa_id: int,
) -> None:
print("\n --- Disabling Smart ASA Freeze and Clawback Addresses...")
configured_smart_asa_id = smart_asa_config(
smart_asa_contract=smart_asa_contract,
smart_asa_app=smart_asa_app,
manager=creator,
asset_id=smart_asa_id,
config_freeze_addr=ZERO_ADDRESS,
config_clawback_addr=ZERO_ADDRESS,
)
print(" --- Configured Smart ASA ID:", configured_smart_asa_id)
print("\n --- Changing Smart ASA Freeze Address...")
with pytest.raises(AlgodHTTPError):
smart_asa_config(
smart_asa_contract=smart_asa_contract,
smart_asa_app=smart_asa_app,
manager=creator,
asset_id=smart_asa_id,
config_freeze_addr=creator,
save_abi_call="/tmp/txn.signed",
)
print(" --- Rejected as expected!")
print("\n --- Changing Smart ASA Clawback Address...")
with pytest.raises(AlgodHTTPError):
smart_asa_config(
smart_asa_contract=smart_asa_contract,
smart_asa_app=smart_asa_app,
manager=creator,
asset_id=smart_asa_id,
config_clawback_addr=creator,
save_abi_call="/tmp/txn.signed",
)
print(" --- Rejected as expected!")
def test_happy_path(
self,
smart_asa_contract: Contract,
smart_asa_app: AppAccount,
creator: Account,
eve: Account,
smart_asa_id: int,
) -> None:
config_s_asa = {
"smart_asa_id": smart_asa_id,
"app_id": smart_asa_app.app_id,
"creator_addr": creator.address,
"unit_name": "NEW_TEST_!!!",
"name": "New Test !!!",
"url": "https://new_test.io",
"metadata_hash": b"a" * 32,
"total": 0,
"decimals": 100,
"frozen": False,
"default_frozen": True,
"manager_addr": eve.address,
"reserve_addr": eve.address,
"freeze_addr": eve.address,
"clawback_addr": eve.address,
}
print("\n --- Configuring Smart ASA...")
configured_smart_asa_id = smart_asa_config(
smart_asa_contract=smart_asa_contract,
smart_asa_app=smart_asa_app,
manager=creator,
asset_id=smart_asa_id,
config_total=config_s_asa["total"],
config_decimals=config_s_asa["decimals"],
config_default_frozen=config_s_asa["default_frozen"],
config_unit_name=config_s_asa["unit_name"],
config_name=config_s_asa["name"],
config_url=config_s_asa["url"],
config_metadata_hash=config_s_asa["metadata_hash"],
config_manager_addr=config_s_asa["manager_addr"],
config_reserve_addr=config_s_asa["reserve_addr"],
config_freeze_addr=config_s_asa["freeze_addr"],
config_clawback_addr=config_s_asa["clawback_addr"],
)
print(" --- Configured Smart ASA ID:", configured_smart_asa_id)
smart_asa = get_smart_asa_params(creator.algod_client, smart_asa_id)
assert smart_asa["smart_asa_id"] == smart_asa_id
assert smart_asa["total"] == config_s_asa["total"]
assert smart_asa["decimals"] == config_s_asa["decimals"]
assert smart_asa["default_frozen"] == config_s_asa["default_frozen"]
assert smart_asa["unit_name"] == config_s_asa["unit_name"]
assert smart_asa["name"] == config_s_asa["name"]
assert smart_asa["url"] == config_s_asa["url"]
assert smart_asa["metadata_hash"][2:] == config_s_asa["metadata_hash"].decode()
assert smart_asa["manager_addr"] == config_s_asa["manager_addr"]
assert smart_asa["reserve_addr"] == config_s_asa["reserve_addr"]
assert smart_asa["freeze_addr"] == config_s_asa["freeze_addr"]
assert smart_asa["clawback_addr"] == config_s_asa["clawback_addr"]
@pytest.mark.parametrize("smart_asa_id", [False], indirect=True)
def test_forbidden_total(
self,
smart_asa_contract: Contract,
smart_asa_app: AppAccount,
opted_in_creator: Account,
smart_asa_id: int,
) -> None:
print("\n --- Configuring Smart ASA total...")
configured_smart_asa_id = smart_asa_config(
smart_asa_contract=smart_asa_contract,
smart_asa_app=smart_asa_app,
manager=opted_in_creator,
asset_id=smart_asa_id,
config_total=2,
)
print(" --- Configured Smart ASA ID:", configured_smart_asa_id)
print(
"\n --- Pre Minting Smart ASA circulating supply:",
smart_asa_get(
smart_asa_contract=smart_asa_contract,
smart_asa_app=smart_asa_app,
caller=opted_in_creator,
asset_id=smart_asa_id,
getter="get_circulating_supply",
),
)
print("\n --- Minting Smart ASA...")
smart_asa_transfer(
smart_asa_contract=smart_asa_contract,
smart_asa_app=smart_asa_app,
xfer_asset=smart_asa_id,
asset_amount=2,
caller=opted_in_creator,
asset_receiver=opted_in_creator,
asset_sender=smart_asa_app,
)
print(
"\n --- Post Minting Smart ASA circulating supply:",
smart_asa_get(
smart_asa_contract=smart_asa_contract,
smart_asa_app=smart_asa_app,
caller=opted_in_creator,
asset_id=smart_asa_id,
getter="get_circulating_supply",
),
)
assert opted_in_creator.asa_balance(smart_asa_id) == 2
print("\n --- Configuring forbidden Smart ASA total...")
with pytest.raises(AlgodHTTPError):
smart_asa_config(
smart_asa_contract=smart_asa_contract,
smart_asa_app=smart_asa_app,
manager=opted_in_creator,
asset_id=smart_asa_id,
config_total=1,
)
print(" --- Rejected as expected!")
class TestAssetTransfer:
def test_smart_asa_not_created(
self,
smart_asa_contract: Contract,
smart_asa_app: AppAccount,
creator: Account,
) -> None:
print("\n --- Transferring unexisting Smart ASA...")
with pytest.raises(AlgodHTTPError):
smart_asa_transfer(
smart_asa_contract=smart_asa_contract,
smart_asa_app=smart_asa_app,
xfer_asset=42,
asset_amount=100,
caller=creator,
asset_receiver=creator,
save_abi_call="/tmp/txn.signed",
)
print(" --- Rejected as expected!")
def test_is_not_correct_smart_asa(
self,
smart_asa_contract: Contract,
smart_asa_app: AppAccount,
creator: Account,
smart_asa_id: int,
) -> None:
print("\n --- Transferring Smart ASA with wrong Asset ID...")
with pytest.raises(AlgodHTTPError):
smart_asa_transfer(
smart_asa_contract=smart_asa_contract,
smart_asa_app=smart_asa_app,
xfer_asset=42,
asset_amount=100,
caller=creator,
asset_receiver=creator,
save_abi_call="/tmp/txn.signed",
)
print(" --- Rejected as expected!")
@pytest.mark.parametrize("smart_asa_id", [False], indirect=True)
def test_minting_with_wrong_reserve(
self,
smart_asa_contract: Contract,
smart_asa_app: AppAccount,
opted_in_creator: Account,
opted_in_account_factory: Callable,
smart_asa_id: int,
) -> None:
wrong_reserve_account = opted_in_account_factory()
pre_minting_supply = smart_asa_get(
smart_asa_contract=smart_asa_contract,
smart_asa_app=smart_asa_app,
caller=opted_in_creator,
asset_id=smart_asa_id,
getter="get_circulating_supply",
)
print("\n --- Pre Minting Smart ASA circulating supply:", pre_minting_supply)
print("\n --- Minting Smart ASA with wrong reserve address...")
with pytest.raises(AlgodHTTPError):
smart_asa_transfer(
smart_asa_contract=smart_asa_contract,
smart_asa_app=smart_asa_app,
xfer_asset=smart_asa_id,
asset_amount=100,
caller=wrong_reserve_account,
asset_receiver=wrong_reserve_account,
asset_sender=smart_asa_app,
)
print(" --- Rejected as expected!")
post_minting_supply = smart_asa_get(
smart_asa_contract=smart_asa_contract,
smart_asa_app=smart_asa_app,
caller=opted_in_creator,
asset_id=smart_asa_id,
getter="get_circulating_supply",
)
print("\n --- Post Minting Smart ASA circulating supply:", post_minting_supply)
assert pre_minting_supply == post_minting_supply
@pytest.mark.parametrize("smart_asa_id", [True], indirect=True)
def test_minting_fails_with_frozen_reserve(
self,
smart_asa_contract: Contract,
smart_asa_app: AppAccount,
opted_in_creator: Account,
smart_asa_id: int,
) -> None:
creator_state = get_local_state(
opted_in_creator.algod_client,
opted_in_creator.address,
smart_asa_app.app_id,
)
assert creator_state["frozen"]
pre_minting_supply = smart_asa_get(
smart_asa_contract=smart_asa_contract,
smart_asa_app=smart_asa_app,
caller=opted_in_creator,
asset_id=smart_asa_id,
getter="get_circulating_supply",
)
print("\n --- Pre Minting Smart ASA circulating supply:", pre_minting_supply)
print("\n --- Minting Smart ASA with frozen reserve address...")
with pytest.raises(AlgodHTTPError):
smart_asa_transfer(
smart_asa_contract=smart_asa_contract,
smart_asa_app=smart_asa_app,
xfer_asset=smart_asa_id,
asset_amount=100,
caller=opted_in_creator,
asset_receiver=opted_in_creator,
asset_sender=smart_asa_app,
)
print(" --- Rejected as expected!")
post_minting_supply = smart_asa_get(
smart_asa_contract=smart_asa_contract,
smart_asa_app=smart_asa_app,
caller=opted_in_creator,
asset_id=smart_asa_id,
getter="get_circulating_supply",
)
print("\n --- Post Minting Smart ASA circulating supply:", post_minting_supply)
assert pre_minting_supply == post_minting_supply
@pytest.mark.parametrize("smart_asa_id", [False], indirect=True)
def test_minting_fails_with_frozen_asset(
self,
smart_asa_contract: Contract,
smart_asa_app: AppAccount,
opted_in_creator: Account,
smart_asa_id: int,
) -> None:
smart_asa = get_smart_asa_params(opted_in_creator.algod_client, smart_asa_id)
assert not smart_asa["frozen"]
print("\n --- Freezeing whole Smart ASA...")
smart_asa_freeze(
smart_asa_contract=smart_asa_contract,
smart_asa_app=smart_asa_app,
freezer=opted_in_creator,
freeze_asset=smart_asa_id,
asset_frozen=True,
)
smart_asa = get_smart_asa_params(opted_in_creator.algod_client, smart_asa_id)
assert smart_asa["frozen"]
pre_minting_supply = smart_asa_get(
smart_asa_contract=smart_asa_contract,
smart_asa_app=smart_asa_app,
caller=opted_in_creator,
asset_id=smart_asa_id,
getter="get_circulating_supply",
)
print("\n --- Pre Minting Smart ASA circulating supply:", pre_minting_supply)
print("\n --- Minting frozen Smart ASA...")
with pytest.raises(AlgodHTTPError):
smart_asa_transfer(
smart_asa_contract=smart_asa_contract,
smart_asa_app=smart_asa_app,
xfer_asset=smart_asa_id,
asset_amount=100,
caller=opted_in_creator,
asset_receiver=opted_in_creator,
asset_sender=smart_asa_app,
)
print(" --- Rejected as expected!")
post_minting_supply = smart_asa_get(
smart_asa_contract=smart_asa_contract,
smart_asa_app=smart_asa_app,
caller=opted_in_creator,
asset_id=smart_asa_id,
getter="get_circulating_supply",
)
print("\n --- Post Minting Smart ASA circulating supply:", post_minting_supply)
assert pre_minting_supply == post_minting_supply
@pytest.mark.parametrize("smart_asa_id", [False], indirect=True)
def test_minting_fails_as_clawback(
self,
smart_asa_contract: Contract,
smart_asa_app: AppAccount,
opted_in_creator: Account,
opted_in_account_factory: Callable,
smart_asa_id: int,
) -> None:
clawback = opted_in_account_factory()
old_global_state = get_global_state(
opted_in_creator.algod_client, smart_asa_app.app_id
)
assert old_global_state["clawback_addr"] == opted_in_creator.decoded_address
print("\n --- Configuring clawback in Smart ASA...")
smart_asa_config(