def fx_currencies(fx_session: Session) -> Mapping[str, Currency]: currencies = { 'USDT': Currency( id='USDT', name='Tether', decimals=8, confirmations=12, minimum_deposit_amount=decimal.Decimal('2'), minimum_withdrawal_amount=decimal.Decimal('2'), withdrawal_fee=decimal.Decimal('1'), latest_synced_block_number=9345018, ), 'BTC': Currency( id='BTC', name='Bitcoin', decimals=8, confirmations=1, minimum_deposit_amount=decimal.Decimal('0.001'), minimum_withdrawal_amount=decimal.Decimal('0.001'), withdrawal_fee=decimal.Decimal('0.0005'), latest_synced_block_number=614336, ), } fx_session.add_all(currencies.values()) fx_session.flush() return currencies
def test_currency_check_constraints( fx_currency_usdt: Currency, fx_session: Session, ): fx_currency_usdt.decimals = -1 with raises(IntegrityError) as e: fx_session.flush() assert 'violates check constraint "ck_currency_decimals' in str(e.value)
def fx_user(fx_session: Session, fx_utcnow: datetime.datetime) -> User: user = User( id=uuid.UUID(int=1), created_at=fx_utcnow, email='*****@*****.**', password='******', ) fx_session.add(user) fx_session.flush() return user
def fx_trades( fx_market: Market, fx_orders: Mapping[OrderSide, Sequence[Order]], fx_session: Session, fx_user: User, fx_utcnow: datetime.datetime, ) -> Sequence[Trade]: second = datetime.timedelta(seconds=1) trades = [ Trade( id=uuid.UUID(int=1), created_at=fx_utcnow - 1 * second, pair='BTC/USDT', buy_order=Order( side=OrderSide.buy, user=fx_user, market=fx_market, volume=decimal.Decimal('2'), remaining_volume=decimal.Decimal('2'), price=decimal.Decimal('10000'), ), sell_order=fx_orders[OrderSide.sell][0], side=OrderSide.buy, volume=decimal.Decimal('2'), price=decimal.Decimal('10000'), index=0, ), Trade( id=uuid.UUID(int=2), created_at=fx_utcnow - 1 * second, pair='BTC/USDT', buy_order=fx_orders[OrderSide.buy][0], sell_order=Order( side=OrderSide.sell, user=fx_user, market=fx_market, volume=decimal.Decimal('3'), remaining_volume=decimal.Decimal('3'), price=decimal.Decimal('8000'), ), side=OrderSide.sell, volume=decimal.Decimal('3'), price=decimal.Decimal('9000'), index=0, ), ] for trade in trades: trade.buy_order.remaining_volume -= trade.volume trade.sell_order.remaining_volume -= trade.volume fx_session.add_all(trades) fx_session.flush() return trades
def fx_balance( fx_currency_usdt: Currency, fx_session: Session, fx_user: User, ) -> Balance: balance = Balance( user=fx_user, currency=fx_currency_usdt.id, amount=decimal.Decimal('100000'), locked_amount=decimal.Decimal('0'), ) fx_session.add(balance) fx_session.flush() return balance
def fx_market( fx_currencies: Mapping[str, Currency], fx_session: Session, ) -> Market: market = Market( pair='BTC/USDT', current_price=decimal.Decimal('8496.27'), maker_fee=decimal.Decimal('0.001'), taker_fee=decimal.Decimal('0.002'), minimum_order_amount=decimal.Decimal('0.0001'), ) fx_session.add(market) fx_session.flush() return market
def fx_session(fx_wsgi_app: Flask) -> SessionType: session = create_session(fx_wsgi_app) engine = session.bind with test_connection(fx_wsgi_app, Base.metadata, engine) as connection: yield Session(bind=connection)
def fx_orders( fx_market: Market, fx_session: Session, fx_user: User, fx_utcnow: datetime.datetime, ) -> Mapping[OrderSide, Sequence[Order]]: second = datetime.timedelta(seconds=1) order_map = { OrderSide.sell: [ Order( id=uuid.UUID(int=1), created_at=fx_utcnow - 60 * second, volume=decimal.Decimal('20'), remaining_volume=decimal.Decimal('20'), price=decimal.Decimal('10000'), ), Order( id=uuid.UUID(int=2), created_at=fx_utcnow - 50 * second, volume=decimal.Decimal('25'), remaining_volume=decimal.Decimal('25'), price=decimal.Decimal('10000'), ), Order( id=uuid.UUID(int=3), created_at=fx_utcnow - 40 * second, volume=decimal.Decimal('30'), remaining_volume=decimal.Decimal('30'), price=decimal.Decimal('11000'), ), ], OrderSide.buy: [ Order( id=uuid.UUID(int=4), created_at=fx_utcnow - 30 * second, volume=decimal.Decimal('15'), remaining_volume=decimal.Decimal('15'), price=decimal.Decimal('9000'), ), Order( id=uuid.UUID(int=5), created_at=fx_utcnow - 20 * second, volume=decimal.Decimal('10'), remaining_volume=decimal.Decimal('10'), price=decimal.Decimal('9000'), ), Order( id=uuid.UUID(int=6), created_at=fx_utcnow - 10 * second, volume=decimal.Decimal('5'), remaining_volume=decimal.Decimal('5'), price=decimal.Decimal('8000'), ), ], } for side, orders in order_map.items(): for order in orders: order.side = side order.user = fx_user order.market = fx_market fx_session.add_all(itertools.chain.from_iterable(order_map.values())) fx_session.flush() return order_map