def test_balance_calculation_out_canceled() -> None: account_id = 1 counter_id = 0 income = Transaction( source_id=counter_id, destination_id=account_id, amount=100, currency=DiemCurrency.XUS, status=TransactionStatus.COMPLETED, ) outgoing = Transaction( source_id=account_id, destination_id=counter_id, amount=50, currency=DiemCurrency.XUS, status=TransactionStatus.CANCELED, ) balance = calc_account_balance(account_id=account_id, transactions=[income, outgoing]) assert balance.total == { DiemCurrency.XUS: 100, } assert balance.frozen == { DiemCurrency.XUS: 0, }
def add_outgoing_user_transaction_to_db(amount, account_name, receiver_address, sender_sub_address, sequence, user, version): user_account_id = Account.query.filter_by(name=account_name).first().id tx = Transaction( amount=amount, source_address=VASP_ADDRESS, destination_address=receiver_address, ) if version is not None: tx.blockchain_version = version if sequence is not None: tx.sequence = sequence tx.created_timestamp = datetime.now() tx.type = TransactionType.EXTERNAL tx.currency = CURRENCY tx.status = TransactionStatus.COMPLETED tx.source_id = user.id tx.source_subaddress = sender_sub_address user.account.sent_transactions.append(tx) db_session.add( SubAddress(address=sender_sub_address, account_id=user_account_id)) db_session.commit()
def run( db_session, account_amount: int, account_currency: DiemCurrency, inventory_amount: int, inventory_currency: DiemCurrency, convert_from_amount: int, convert_to_amount: int, ) -> Tuple[int, int, Order]: user = deepcopy(prototypes.user) user.account = Account(name="fake_account_seed") inventory_account = Account(name=INVENTORY_ACCOUNT_NAME) db_session.add(user) db_session.add(inventory_account) db_session.commit() inventory_income = Transaction( created_timestamp=datetime.now(), amount=inventory_amount, currency=inventory_currency, type=TransactionType.EXTERNAL, status=TransactionStatus.COMPLETED, source_address="lp", destination_id=inventory_account.id, ) inventory_account.received_transactions.append(inventory_income) user_income = Transaction( created_timestamp=datetime.now(), amount=account_amount, currency=account_currency, type=TransactionType.EXTERNAL, status=TransactionStatus.COMPLETED, source_address="na", destination_id=user.account.id, ) user.account.received_transactions.append(user_income) order = Order( amount=convert_from_amount, direction=Direction.Buy, base_currency=account_currency, quote_currency=inventory_currency, order_status=OrderStatus.PendingExecution, cover_status=CoverStatus.PendingCover, order_expiration=datetime.utcnow() + timedelta(minutes=10), exchange_amount=convert_to_amount, order_type=OrderType.DirectConvert, ) user.orders.append(order) db_session.add(order) db_session.commit() return inventory_account.id, user.account.id, order
def send_mock( sender_id: int, amount: int, currency: DiemCurrency, destination_address: str, destination_subaddress: Optional[str] = None, payment_type: Optional[TransactionType] = None, ) -> Optional[Transaction]: saved.update({ "sender_id": sender_id, "amount": amount, "currency": currency, "destination_address": destination_address, "destination_subaddress": destination_subaddress, "payment_type": payment_type, }) tx = Transaction( id=5, type=TransactionType.EXTERNAL.value, amount=amount, currency=currency.value, status=TransactionStatus.PENDING.value, source_id=sender_id, source_subaddress="122ce7420f15bec5", source_address="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", destination_address=destination_address, destination_subaddress=destination_subaddress, created_timestamp=datetime.fromisoformat( "2020-06-23T19:50:26.989849"), blockchain_version=55, sequence=10, ) return tx
def add_inventory_account_with_initial_funds_to_db(amount): inventory_user = User( username=INVENTORY_ACCOUNT_NAME, registration_status=RegistrationStatus.Approved, selected_fiat_currency=FiatCurrency.USD, selected_language="en", password_salt="123", password_hash="deadbeef", ) inventory_user.account = Account(name=INVENTORY_ACCOUNT_NAME) tx = Transaction( created_timestamp=datetime.now(), amount=amount, currency=CURRENCY, type=TransactionType.EXTERNAL, status=TransactionStatus.COMPLETED, sequence=0, blockchain_version=0, source_address=DD_ADDRESS, destination_address=VASP_ADDRESS, destination_id=inventory_user.id, ) inventory_user.account.received_transactions.append(tx) db_session.add(inventory_user) db_session.commit()
def run( db_session, account_amount: Optional[int] = None, account_currency: Optional[DiemCurrency] = None, registration_status: Optional[RegistrationStatus] = RegistrationStatus. Approved, account_name: str = "fake_account", username: Optional[str] = None, ) -> User: user = deepcopy(prototypes.user) if username: user.username = username user.first_name = f"{username} first name" user.last_name = f"{username} last name" user.registration_status = registration_status user.account = Account(name=account_name) db_session.add(user) db_session.commit() if account_amount and account_currency: user_income = Transaction( created_timestamp=datetime.now(), amount=account_amount, currency=account_currency, type=TransactionType.EXTERNAL, status=TransactionStatus.COMPLETED, source_address="na", destination_id=user.account.id, ) user.account.received_transactions.append(user_income) db_session.commit() return user
def test_balance_calculation_simple_income() -> None: account_id = 1 counter_id = 0 tx = Transaction( source_id=counter_id, destination_id=account_id, amount=100, currency=DiemCurrency.XUS, status=TransactionStatus.COMPLETED, ) balance = calc_account_balance(account_id=account_id, transactions=[tx]) assert balance.total == { DiemCurrency.XUS: 100, }
def test_balance_calculation_in_pending() -> None: account_id = 1 counter_id = 0 income = Transaction( source_id=counter_id, destination_id=account_id, amount=100, currency=DiemCurrency.Coin1, status=TransactionStatus.PENDING, ) balance = calc_account_balance(account_id=account_id, transactions=[income]) assert balance.total == { DiemCurrency.Coin1: 0, }
def test_balance_calculation_in_and_out() -> None: account_id = 1 counter_id = 0 income = Transaction( source_id=counter_id, destination_id=account_id, amount=100, currency=DiemCurrency.Coin1, status=TransactionStatus.COMPLETED, ) outgoing = Transaction( source_id=account_id, destination_id=counter_id, amount=50, currency=DiemCurrency.Coin1, status=TransactionStatus.COMPLETED, ) balance = calc_account_balance( account_id=account_id, transactions=[income, outgoing] ) assert balance.total == { DiemCurrency.Coin1: 50, }
def add_inventory_account_with_initial_funds_to_db(amount): inventory_user = add_inventory_account_to_db() tx = Transaction( created_timestamp=datetime.now(), amount=amount, currency=CURRENCY, type=TransactionType.EXTERNAL, status=TransactionStatus.COMPLETED, sequence=0, blockchain_version=0, source_address=DD_ADDRESS, destination_address=VASP_ADDRESS, destination_id=inventory_user.id, ) inventory_user.account.received_transactions.append(tx) db_session.add(inventory_user) db_session.commit()
from wallet.storage import Transaction from wallet.types import ( Balance, TransactionDirection, TransactionStatus, TransactionType, TransactionSortOption, ) INTERNAL_TX = Transaction( id=1, type=TransactionType.INTERNAL.value, amount=100, currency=DiemCurrency.Coin1.value, status=TransactionStatus.COMPLETED.value, source_id=1, source_address="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", source_subaddress="863bc063df8b2bf3", destination_id=2, destination_address="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", destination_subaddress="75b12ccd3ab503a6", created_timestamp=datetime.fromisoformat("2020-06-23T19:49:26.989849"), ) FULL_ADDRESS = "tlb1pztdjx2z8wp0q25jakqeklk0nxj2wmk2kg9whu8cxdewwy" @pytest.fixture def allow_user_to_account(monkeypatch): monkeypatch.setattr(account_service, "is_user_allowed_for_account", lambda user, account_name: True)
password_hash="deadbeef", ) order = Order( amount=10, direction=Direction.Buy.value, base_currency=Currency.Coin1, quote_currency=Currency.USD, order_status=OrderStatus.PendingExecution, cover_status=CoverStatus.PendingCover, payment_method="payment_method", exchange_amount=10, order_expiration=datetime.utcnow() + timedelta(minutes=10), order_type=OrderType.Trade, ) tx = Transaction( amount=1000000, currency="Coin1", type="transfer_internal", status="completed", created_timestamp=datetime.utcnow(), source_id=0, source_address="source_address", source_subaddress="source_subaddress", destination_id=1, destination_address="destination_address", destination_subaddress="destination_subaddress", sequence=1, )