async def test_create_child_vasp(): client = create_client() faucet = Faucet(client) parent_vasp = await faucet.gen_account() seq_num = await client.get_account_sequence(parent_vasp.account_address) child_vasp = LocalAccount.generate() payload = stdlib.encode_create_child_vasp_account_script_function( coin_type=utils.currency_code(XUS), child_address=child_vasp.account_address, auth_key_prefix=child_vasp.auth_key.prefix(), add_all_currencies=False, child_initial_balance=100_000_000, ) raw_txn = diem_types.RawTransaction( sender=parent_vasp.account_address, sequence_number=seq_num, payload=payload, max_gas_amount=1_000_000, gas_unit_price=0, gas_currency_code=XUS, expiration_timestamp_secs=int(time.time()) + 30, chain_id=chain_ids.TESTNET, ) txn = parent_vasp.sign(raw_txn) await client.submit(txn) executed_txn = await client.wait_for_transaction(txn) assert executed_txn is not None
async def test_submit_p2p_with_unknown_address(): client = create_client() faucet = Faucet(client) account = await faucet.gen_account() da = DiemAccount(account, [], client) with pytest.raises(ValueError): payee = LocalAccount().account_identifier() await da.submit_p2p(gen_txn(payee=payee), (b"", b""), by_address=LocalAccount().account_address)
async def test_ensure_account_balance_is_always_enough(): client = create_client() faucet = Faucet(client) account = LocalAccount.generate() await faucet.mint(account.auth_key.hex(), 1, XUS) da = DiemAccount(account, [], client) account_data = await client.must_get_account(account.account_address) amount = utils.balance(account_data, XUS) + 1 payee = await faucet.gen_account() txn = await da.submit_p2p(gen_txn(payee=payee.account_identifier(), amount=amount), (b"", b"")) await client.wait_for_transaction(txn)
async def test_no_child_accounts(): client = create_client() faucet = Faucet(client) account = await faucet.gen_account() da = DiemAccount(account, [], client) assert da.hrp == account.hrp assert da.account_identifier() == account.account_identifier() payee = await faucet.gen_account() signed_txn = await da.submit_p2p(gen_txn(payee=payee.account_identifier()), (b"", b"")) assert signed_txn.raw_txn.sender == account.account_address
async def test_p2p_above_threshold_by_reference_id(): client = create_client() faucet = Faucet(client) sender = await faucet.gen_account() receiver = await faucet.gen_account() await receiver.rotate_dual_attestation_info(client, base_url="http://localhost") amount = 20_000_000_000 reference_id = str(uuid.uuid4()) metadata, metadata_signing_msg = txnmetadata.travel_rule( reference_id, sender.account_address, amount) metadata_signature = receiver.compliance_key.sign(metadata_signing_msg) payload = stdlib.encode_peer_to_peer_with_metadata_script_function( currency=utils.currency_code(XUS), payee=receiver.account_address, amount=amount, metadata=metadata, metadata_signature=metadata_signature, ) seq_num = await client.get_account_sequence(sender.account_address) txn = diem_types.RawTransaction( sender=sender.account_address, sequence_number=seq_num, payload=payload, max_gas_amount=1_000_000, gas_unit_price=0, gas_currency_code=XUS, expiration_timestamp_secs=int(time.time()) + 30, chain_id=chain_ids.TESTNET, ) signed_txn = sender.sign(txn) await client.submit(signed_txn) executed_txn = await client.wait_for_transaction(signed_txn) assert executed_txn is not None
async def test_gen_child_vasp(): client = create_client() faucet = Faucet(client) account = LocalAccount( hrp=identifier.DM, txn_gas_currency_code="XUS", txn_max_gas_amount=1_000_001, txn_gas_unit_price=1, txn_expire_duration_secs=60, ) await faucet.mint(account.auth_key.hex(), 10_000_000, "XUS") child_account = await account.gen_child_vasp(client, 1, "XUS") assert child_account.hrp == account.hrp assert child_account.txn_gas_currency_code == account.txn_gas_currency_code assert child_account.txn_max_gas_amount == account.txn_max_gas_amount assert child_account.txn_gas_unit_price == account.txn_gas_unit_price assert child_account.txn_expire_duration_secs == account.txn_expire_duration_secs assert child_account.compliance_key == account.compliance_key assert child_account.private_key != account.private_key child_diem_account = await client.must_get_account( child_account.account_address) assert child_diem_account.role.type == "child_vasp"
async def test_p2p_under_threshold_by_subaddress(): client = create_client() faucet = Faucet(client) sender = await faucet.gen_account() sender_subaddress = identifier.gen_subaddress() receiver = await faucet.gen_account() receiver_subaddress = identifier.gen_subaddress() amount = 2_000_000 payload = stdlib.encode_peer_to_peer_with_metadata_script_function( currency=utils.currency_code(XUS), payee=receiver.account_address, amount=amount, metadata=txnmetadata.general_metadata(sender_subaddress, receiver_subaddress), metadata_signature=b"", # only travel rule metadata requires signature ) seq_num = await client.get_account_sequence(sender.account_address) txn = diem_types.RawTransaction( sender=sender.account_address, sequence_number=seq_num, payload=payload, max_gas_amount=1_000_000, gas_unit_price=0, gas_currency_code=XUS, expiration_timestamp_secs=int(time.time()) + 30, chain_id=chain_ids.TESTNET, ) signed_txn = sender.sign(txn) await client.submit(signed_txn) executed_txn = await client.wait_for_transaction(signed_txn) assert executed_txn is not None
async def start_server( name: str, host: str, port: int, diem_account_base_url: Optional[str], jsonrpc: str, faucet: str, disable_events_api: bool, import_diem_account_config_file: Optional[TextIO], logfile: Optional[str], hrp: str, ) -> None: logging.basicConfig(level=logging.INFO, format=log_format, filename=logfile) conf = AppConfig( name=name, server_conf=ServerConfig(host=host, port=port, base_url=diem_account_base_url or ""), disable_events_api=disable_events_api, ) if import_diem_account_config_file: conf.account_config = json.load(import_diem_account_config_file) if hrp: conf.account_config["hrp"] = hrp print("Server Config: %s" % conf) client = create_client() metadata = await client.get_metadata() print("Diem chain id: %s" % metadata.chain_id) _, runner = await conf.start(client) try: while True: await asyncio.sleep(3600) finally: await runner.cleanup()
async def diem_client() -> AsyncGenerator[AsyncClient, None]: async with create_client() as client: yield client
async def diem_client() -> AsyncGenerator[AsyncClient, None]: async with create_client() as client: print("Diem JSON-RPC URL: %s" % client._url) yield client
def client() -> AsyncClient: return create_client()