2024-11-27 20:56:13 +00:00
|
|
|
import datetime
|
|
|
|
from decimal import Decimal
|
|
|
|
|
|
|
|
import fin_defs
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
import fin_depo
|
|
|
|
|
|
|
|
from . import secrets
|
|
|
|
|
|
|
|
TEST_MARKET_ORDERS = True
|
|
|
|
|
|
|
|
needs_secrets = pytest.mark.skipif(
|
|
|
|
not secrets.KUCOIN_KEY,
|
|
|
|
reason='Secret kucoin_USERNAME required',
|
|
|
|
)
|
|
|
|
|
|
|
|
fin_depo.defi_kucoin.logger.setLevel('INFO')
|
|
|
|
|
|
|
|
NOW = datetime.datetime.now(tz=datetime.UTC)
|
|
|
|
|
|
|
|
|
|
|
|
@needs_secrets
|
|
|
|
def test_place_market_order_requires_allow_trades():
|
|
|
|
"""Client fails if allow_trades is not enabled when doing market orders."""
|
|
|
|
fetcher = fin_depo.defi_kucoin.KucoinDepoFetcher(
|
|
|
|
secrets.KUCOIN_KEY,
|
|
|
|
secrets.KUCOIN_SECRET,
|
|
|
|
secrets.KUCOIN_PASS,
|
|
|
|
allow_trades=False,
|
|
|
|
)
|
|
|
|
|
|
|
|
with pytest.raises(PermissionError) as m:
|
|
|
|
fetcher.place_market_order(fin_defs.MPC, Decimal(1), fin_defs.USDT)
|
|
|
|
|
|
|
|
assert 'KucoinDepoFetcher.allow_trades is not enabled: Cannot make trades' in str(m)
|
|
|
|
|
|
|
|
|
|
|
|
@needs_secrets
|
|
|
|
def test_place_buy_side_order():
|
|
|
|
"""Client can place buy side market orders."""
|
|
|
|
fetcher = fin_depo.defi_kucoin.KucoinDepoFetcher(
|
|
|
|
secrets.KUCOIN_KEY,
|
|
|
|
secrets.KUCOIN_SECRET,
|
|
|
|
secrets.KUCOIN_PASS,
|
|
|
|
allow_trades=TEST_MARKET_ORDERS,
|
|
|
|
)
|
|
|
|
|
|
|
|
input_amount = Decimal('0.1')
|
|
|
|
|
|
|
|
order_details = fetcher.place_market_order(
|
2024-11-28 20:51:31 +00:00
|
|
|
fin_defs.AssetAmount(fin_defs.USDT, input_amount),
|
2024-11-27 20:56:13 +00:00
|
|
|
fin_defs.MPC,
|
|
|
|
)
|
|
|
|
|
|
|
|
assert order_details is not None
|
|
|
|
assert order_details.order_id is not None
|
|
|
|
assert order_details.raw_order_details is not None
|
|
|
|
|
|
|
|
assert order_details.input_asset == fin_defs.USDT
|
|
|
|
assert order_details.output_asset == fin_defs.MPC
|
|
|
|
|
|
|
|
assert order_details.input_amount <= input_amount
|
|
|
|
assert order_details.output_amount >= 0
|
|
|
|
|
|
|
|
assert order_details.input_amount != order_details.output_amount
|
|
|
|
|
|
|
|
assert order_details.fee_asset == fin_defs.USDT
|
|
|
|
assert order_details.fee_amount <= Decimal('0.0002')
|
|
|
|
|
|
|
|
assert NOW <= order_details.executed_time <= NOW + datetime.timedelta(minutes=10)
|
|
|
|
|
|
|
|
|
|
|
|
@needs_secrets
|
|
|
|
def test_place_sell_side_order():
|
|
|
|
"""Client can place sell side market orders."""
|
|
|
|
fetcher = fin_depo.defi_kucoin.KucoinDepoFetcher(
|
|
|
|
secrets.KUCOIN_KEY,
|
|
|
|
secrets.KUCOIN_SECRET,
|
|
|
|
secrets.KUCOIN_PASS,
|
|
|
|
allow_trades=TEST_MARKET_ORDERS,
|
|
|
|
)
|
|
|
|
|
|
|
|
input_amount = Decimal('1')
|
|
|
|
|
|
|
|
order_details = fetcher.place_market_order(
|
2024-11-28 20:51:31 +00:00
|
|
|
fin_defs.AssetAmount(fin_defs.MPC, input_amount),
|
2024-11-27 20:56:13 +00:00
|
|
|
fin_defs.USDT,
|
|
|
|
)
|
|
|
|
|
|
|
|
assert order_details is not None
|
|
|
|
assert order_details.order_id is not None
|
|
|
|
assert order_details.raw_order_details is not None
|
|
|
|
|
|
|
|
assert order_details.input_asset == fin_defs.MPC
|
|
|
|
assert order_details.output_asset == fin_defs.USDT
|
|
|
|
|
|
|
|
assert order_details.input_amount <= input_amount
|
|
|
|
assert order_details.output_amount >= 0
|
|
|
|
|
|
|
|
assert order_details.input_amount != order_details.output_amount
|
|
|
|
|
|
|
|
assert order_details.fee_asset == fin_defs.USDT
|
|
|
|
assert order_details.fee_amount is not None
|
|
|
|
|
|
|
|
assert NOW <= order_details.executed_time <= NOW + datetime.timedelta(minutes=10)
|