def test_add_faulty_offchain_transaction() -> None: with pytest.raises(ValueError): add_transaction( amount=100, currency=diem_utils.types.currencies.DiemCurrency.Coin1, payment_type=types.TransactionType.OFFCHAIN, status=types.TransactionStatus.PENDING, source_id=1, source_address="sender_address", source_subaddress="sender_subaddress", destination_id=123, destination_address="receiver_address", destination_subaddress="receiver_subaddress", )
def add_transaction_to_db(transaction): metadata = transaction.transaction.script.metadata receiver_sub_address = None sender_sub_address = None if metadata: receiver_sub_address, sender_sub_address = deserialize_metadata( metadata) source_id = None destination_id = None sender_address = transaction.transaction.sender receiver_address = transaction.transaction.script.receiver # outgoing transaction if sender_address.lower() == VASP_ADDRESS.lower(): sender_sub_address, source_id = handle_outgoing_transaction( sender_sub_address) # incoming transaction elif receiver_address.lower() == VASP_ADDRESS.lower(): destination_id, receiver_sub_address = handle_incoming_transaction( receiver_sub_address) else: logger.warning( f"LRW VASP address is not the source nor the destination of transaction with version {transaction.version}" ) return None add_transaction( amount=transaction.transaction.script.amount, currency=transaction.transaction.script.currency, payment_type=TransactionType.EXTERNAL, status=TransactionStatus.COMPLETED, source_id=source_id, source_address=sender_address, source_subaddress=sender_sub_address, destination_id=destination_id, destination_address=receiver_address, destination_subaddress=receiver_sub_address, sequence=transaction.transaction.sequence_number, blockchain_version=transaction.version, )
def test_transaction_seq_exist() -> None: source_addr = "fake_source_addr" source_subaddr = "fake_source_subaddr" assert not storage.get_transaction_by_details( source_address=source_addr, source_subaddress=source_subaddr, sequence=0 ) storage.add_transaction( amount=100, currency=DiemCurrency.Coin1, payment_type=TransactionType.INTERNAL, status=TransactionStatus.COMPLETED, source_address=source_addr, source_subaddress=source_subaddr, sequence=0, ) assert storage.get_transaction_by_details( source_address=source_addr, source_subaddress=source_subaddr, sequence=0 )
def test_process_incoming_travel_rule_txn() -> None: account = create_account("fake_account") sender_addr = "46db232847705e05525db0336fd9f337" receiver_addr = "lrw_vasp" sender_subaddr = generate_new_subaddress(account.id) amount = 1000 * 1_000_000 sender = account_address(sender_addr) sequence = 1 currency = DiemCurrency.XUS blockchain_version = 1 off_chain_reference_id = "off_chain_reference_id" metadata, _ = travel_rule(off_chain_reference_id, sender, amount) storage.add_transaction( amount=amount, currency=currency, payment_type=TransactionType.OFFCHAIN, status=TransactionStatus.OFF_CHAIN_READY, source_id=account.id, source_address=sender_addr, source_subaddress=sender_subaddr, destination_address=receiver_addr, reference_id=off_chain_reference_id, ) process_incoming_transaction( sender_address=sender_addr, receiver_address=receiver_addr, sequence=sequence, amount=amount, currency=currency, metadata=diem_types.Metadata__TravelRuleMetadata.bcs_deserialize( metadata), blockchain_version=blockchain_version, ) # successfully parse meta and sequence tx = storage.get_transaction_by_details(source_address=sender_addr, source_subaddress=sender_subaddr, sequence=sequence) assert tx is not None assert tx.sequence == sequence assert tx.blockchain_version == blockchain_version
def test_add_transaction() -> None: tx = add_transaction( amount=100, currency=diem_utils.types.currencies.DiemCurrency.Coin1, payment_type=types.TransactionType.EXTERNAL, status=types.TransactionStatus.PENDING, source_id=1, source_address="sender_address", source_subaddress="sender_subaddress", destination_id=123, destination_address="receiver_address", destination_subaddress="receiver_subaddress", ) assert tx.id in get_account_transaction_ids(1)
def test_process_incoming_refund_txn() -> None: initial_sender_account = create_account("fake_account") initial_sender_subaddr = generate_new_subaddress(initial_sender_account.id) initial_sender_addr = "lrw_vasp" initial_receiver_addr = "46db232847705e05525db0336fd9f337" meta = refund_metadata( original_transaction_version=1, reason=diem_types.RefundReason__InvalidSubaddress(), ) initial_tx = storage.add_transaction( amount=500, currency=DiemCurrency.XUS, payment_type=TransactionType.EXTERNAL, status=TransactionStatus.COMPLETED, source_id=initial_sender_account.id, source_address=initial_sender_addr, source_subaddress=initial_sender_subaddr, destination_address=initial_receiver_addr, blockchain_version=1, ) assert initial_tx is not None assert initial_tx.blockchain_version == 1 assert storage.get_transaction_by_blockchain_version(1) is not None process_incoming_transaction( sender_address=initial_receiver_addr, receiver_address=initial_sender_addr, sequence=1, amount=500, currency=DiemCurrency.XUS, metadata=diem_types.Metadata__RefundMetadata.bcs_deserialize(meta), blockchain_version=2, ) tx = storage.get_transaction_by_blockchain_version(2) assert tx is not None assert tx.type == TransactionType.REFUND assert tx.original_txn_id == initial_tx.id