|
| 1 | + |
| 2 | +import pytest |
| 3 | + |
| 4 | +from starknet_py.net.account.account import Account |
| 5 | +from starknet_py.net.client_models import ( |
| 6 | + TransactionFinalityStatus |
| 7 | +) |
| 8 | + |
| 9 | +@pytest.mark.asyncio |
| 10 | +async def test_account_outside_execution_any_caller( |
| 11 | + client, |
| 12 | + argent_account_class_hash, |
| 13 | + deployed_balance_contract, |
| 14 | + deploy_account_details_factory, |
| 15 | +): |
| 16 | + # pylint: disable=import-outside-toplevel,too-many-locals |
| 17 | + address, key_pair, salt, class_hash = await deploy_account_details_factory.get( |
| 18 | + class_hash=argent_account_class_hash, argent_calldata=True |
| 19 | + ) |
| 20 | + |
| 21 | + deploy_result = await Account.deploy_account_v1( |
| 22 | + address=address, |
| 23 | + class_hash=class_hash, |
| 24 | + salt=salt, |
| 25 | + key_pair=key_pair, |
| 26 | + client=client, |
| 27 | + constructor_calldata=[key_pair.public_key, 0], |
| 28 | + max_fee=int(1e18), |
| 29 | + ) |
| 30 | + await deploy_result.wait_for_acceptance() |
| 31 | + account = deploy_result.account |
| 32 | + |
| 33 | + # docs: start |
| 34 | + import datetime |
| 35 | + |
| 36 | + from starknet_py.constants import ANY_CALLER |
| 37 | + from starknet_py.hash.selector import get_selector_from_name |
| 38 | + from starknet_py.net.client_models import ( |
| 39 | + Call, |
| 40 | + ExecutionTimeBounds, |
| 41 | + ) |
| 42 | + |
| 43 | + # Create a call to increase the balance by 100. That will be executed |
| 44 | + # as part of external execution |
| 45 | + |
| 46 | + increase_balance_call = Call( |
| 47 | + to_addr=deployed_balance_contract.address, |
| 48 | + selector=get_selector_from_name("increase_balance"), |
| 49 | + calldata=[100], |
| 50 | + ) |
| 51 | + |
| 52 | + # Create a special signed execution call. This call now be executed by |
| 53 | + # caller specified. In this case, it is ANY_CALLER, a special constant |
| 54 | + # that allows any caller to execute the call. |
| 55 | + call = await account.sign_outside_execution_call( |
| 56 | + calls=[ |
| 57 | + increase_balance_call, |
| 58 | + ], |
| 59 | + execution_time_bounds=ExecutionTimeBounds( |
| 60 | + execute_after=datetime.datetime.now() - datetime.timedelta(hours=1), |
| 61 | + execute_before=datetime.datetime.now() + datetime.timedelta(hours=1), |
| 62 | + ), |
| 63 | + caller=ANY_CALLER, |
| 64 | + ) |
| 65 | + |
| 66 | + # Execute the call as a normal invoke transaction |
| 67 | + tx = await account.execute_v1(calls=[call], max_fee=int(1e18)) |
| 68 | + await account.client.wait_for_tx(tx.transaction_hash) |
| 69 | + |
| 70 | + # docs: end |
| 71 | + |
| 72 | + receipt = await account.client.get_transaction_receipt( |
| 73 | + tx_hash=tx.transaction_hash |
| 74 | + ) |
| 75 | + |
| 76 | + assert receipt.finality_status == TransactionFinalityStatus.ACCEPTED_ON_L2 |
0 commit comments