66
77import algosdk
88from algokit_utils import (
9- Account ,
10- ApplicationClient ,
11- EnsureBalanceParameters ,
12- ensure_funded ,
13- get_localnet_default_account ,
9+ AlgorandClient ,
10+ AppClient ,
11+ AppClientMethodCallParams ,
12+ AppFactory ,
13+ AppFactoryCreateMethodCallParams ,
14+ AppFactoryCreateParams ,
15+ AppFactoryParams ,
16+ SigningAccount ,
1417)
1518from algosdk .v2client .algod import AlgodClient
1619
@@ -21,65 +24,70 @@ class AVMInvoker:
2124 """Protocol used in global test fixtures to simplify invocation of AVM methods via an Algokit
2225 typed client."""
2326
24- def __init__ (self , client : ApplicationClient ):
27+ def __init__ (self , client : AppClient , factory : AppFactory ):
2528 self .client = client
29+ self .factory = factory
2630
2731 def __call__ (
2832 self ,
2933 method : str ,
3034 on_complete : algosdk .transaction .OnComplete = algosdk .transaction .OnComplete .NoOpOC ,
3135 ** kwargs : typing .Any ,
3236 ) -> object :
33- response = self .client .call (
34- method ,
35- transaction_parameters = {
36- # random note avoids duplicate txn if tests are running concurrently
37- "note" : _random_note (),
38- "accounts" : kwargs .pop ("accounts" , None ),
39- "foreign_apps" : kwargs .pop ("foreign_apps" , None ),
40- "foreign_assets" : kwargs .pop ("foreign_assets" , None ),
41- "suggested_params" : kwargs .pop ("suggested_params" , None ),
42- "on_complete" : on_complete ,
43- },
44- ** kwargs ,
37+ response = self .client .send .call (
38+ AppClientMethodCallParams (
39+ method = method ,
40+ note = _random_note (),
41+ account_references = kwargs .pop ("accounts" , None ),
42+ app_references = kwargs .pop ("foreign_apps" , None ),
43+ asset_references = kwargs .pop ("foreign_assets" , None ),
44+ on_complete = on_complete ,
45+ static_fee = kwargs .pop ("static_fee" , None ),
46+ args = [item [1 ] for item in kwargs .items ()],
47+ ),
4548 )
46- if response .decode_error :
47- raise ValueError (response .decode_error )
48- result = response .return_value
49- if result is None :
50- return response .tx_info .get ("logs" , None )
49+ if response .returns and len (response .returns ) > 0 and response .returns [0 ].decode_error :
50+ raise ValueError (response .returns [0 ].decode_error )
51+ result = response .abi_return
52+ if result is None and response .returns and len (response .returns ) > 0 :
53+ assert response .returns [0 ].tx_info
54+ return response .returns [0 ].tx_info .get ("logs" , None )
5155 if isinstance (result , list ) and all (
5256 isinstance (i , int ) and i >= 0 and i <= 255 for i in result
5357 ):
54- return bytes (result )
58+ return bytes (result ) # type: ignore[arg-type]
5559 return result
5660
5761
5862def _random_note () -> bytes :
5963 return secrets .token_bytes (8 )
6064
6165
62- def create_avm_invoker (app_spec : Path , algod_client : AlgodClient ) -> AVMInvoker :
63- client = ApplicationClient (
64- algod_client ,
65- app_spec ,
66- signer = get_localnet_default_account (algod_client ),
67- )
68-
69- client .create (
70- transaction_parameters = {
71- # random note avoids duplicate txn if tests are running concurrently
72- "note" : _random_note (),
73- }
66+ def create_avm_invoker (app_spec : Path , algorand : AlgorandClient ) -> AVMInvoker :
67+ dispenser = algorand .account .localnet_dispenser ()
68+ factory = AppFactory (
69+ AppFactoryParams (
70+ algorand = algorand ,
71+ app_spec = app_spec .read_text (),
72+ default_sender = dispenser .address ,
73+ ),
7474 )
75+ try :
76+ client , _ = factory .send .bare .create (
77+ AppFactoryCreateParams (note = _random_note ()),
78+ )
79+ except Exception as __ :
80+ client , _ = factory .send .create (
81+ AppFactoryCreateMethodCallParams (method = "create" , note = _random_note ()),
82+ )
7583
76- return AVMInvoker (client )
84+ return AVMInvoker (client , factory )
7785
7886
7987def generate_test_asset ( # noqa: PLR0913
8088 * ,
8189 algod_client : AlgodClient ,
82- sender : Account ,
90+ sender : SigningAccount ,
8391 total : int | None = None ,
8492 decimals : int = 0 ,
8593 default_frozen : bool = False ,
@@ -134,18 +142,3 @@ def generate_test_asset( # noqa: PLR0913
134142 return ptx ["asset-index" ]
135143 else :
136144 raise ValueError ("Unexpected response from pending_transaction_info" )
137-
138-
139- def generate_test_account (algod_client : AlgodClient ) -> Account :
140- raw_account = algosdk .account .generate_account ()
141- account = Account (private_key = raw_account [0 ], address = raw_account [1 ])
142-
143- ensure_funded (
144- algod_client ,
145- EnsureBalanceParameters (
146- account_to_fund = account ,
147- min_spending_balance_micro_algos = INITIAL_BALANCE_MICRO_ALGOS ,
148- ),
149- )
150-
151- return account
0 commit comments