2121"""
2222
2323import requests
24- import typing
24+ import typing , time
2525
26- from . import diem_types , jsonrpc , utils , local_account , serde_types , auth_key , chain_ids , lcs
26+ from . import diem_types , jsonrpc , utils , local_account , serde_types , auth_key , chain_ids , lcs , stdlib , LocalAccount
2727
2828
2929JSON_RPC_URL : str = "https://testnet.diem.com/v1"
@@ -40,6 +40,62 @@ def create_client() -> jsonrpc.Client:
4040 return jsonrpc .Client (JSON_RPC_URL )
4141
4242
43+ def gen_vasp_account (base_url : str ) -> LocalAccount :
44+ account = gen_account ()
45+ exec_txn (
46+ account ,
47+ stdlib .encode_rotate_dual_attestation_info_script (
48+ new_url = base_url .encode ("utf-8" ), new_key = account .compliance_public_key_bytes
49+ ),
50+ )
51+ return account
52+
53+
54+ def gen_account () -> LocalAccount :
55+ """generates a Testnet onchain account"""
56+
57+ return Faucet (create_client ()).gen_account ()
58+
59+
60+ def gen_child_vasp (
61+ parent_vasp : LocalAccount , initial_balance : int = 10_000_000_000 , currency : str = TEST_CURRENCY_CODE
62+ ) -> LocalAccount :
63+ child_vasp = LocalAccount .generate ()
64+ exec_txn (
65+ parent_vasp ,
66+ stdlib .encode_create_child_vasp_account_script (
67+ coin_type = utils .currency_code (currency ),
68+ child_address = child_vasp .account_address ,
69+ auth_key_prefix = child_vasp .auth_key .prefix (),
70+ add_all_currencies = False ,
71+ child_initial_balance = initial_balance ,
72+ ),
73+ )
74+ return child_vasp
75+
76+
77+ def exec_txn (sender : LocalAccount , script : diem_types .Script ) -> jsonrpc .Transaction :
78+ """create, submit and wait for the transaction"""
79+
80+ client = create_client ()
81+ sender_account_sequence = client .get_account_sequence (sender .account_address )
82+ expire = 30
83+ txn = sender .sign (
84+ diem_types .RawTransaction ( # pyre-ignore
85+ sender = sender .account_address ,
86+ sequence_number = diem_types .st .uint64 (sender_account_sequence ),
87+ payload = diem_types .TransactionPayload__Script (value = script ),
88+ max_gas_amount = 1_000_000 ,
89+ gas_unit_price = 0 ,
90+ gas_currency_code = TEST_CURRENCY_CODE ,
91+ expiration_timestamp_secs = int (time .time ()) + expire ,
92+ chain_id = CHAIN_ID ,
93+ )
94+ )
95+ client .submit (txn )
96+ return client .wait_for_transaction (txn , timeout_secs = expire )
97+
98+
4399class Faucet :
44100 """Faucet service is a proxy server to mint coins for your test account on Testnet
45101
@@ -57,9 +113,9 @@ def __init__(
57113 self ._retry : jsonrpc .Retry = retry or jsonrpc .Retry (5 , 0.2 , Exception )
58114 self ._session : requests .Session = requests .Session ()
59115
60- def gen_account (self , currency_code : str = TEST_CURRENCY_CODE ) -> local_account . LocalAccount :
61- account = local_account . LocalAccount .generate ()
62- self .mint (account .auth_key .hex (), 5_000_000_000 , currency_code )
116+ def gen_account (self , currency_code : str = TEST_CURRENCY_CODE ) -> LocalAccount :
117+ account = LocalAccount .generate ()
118+ self .mint (account .auth_key .hex (), 100_000_000_000 , currency_code )
63119 return account
64120
65121 def mint (self , authkey : str , amount : int , currency_code : str ) -> None :
0 commit comments