Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion examples/01_create_limit_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from x10.perpetual.trading_client import PerpetualTradingClient

LOGGER = logging.getLogger()
MARKET_NAME = ETH_USD_MARKET
ENDPOINT_CONFIG = MAINNET_CONFIG


Expand All @@ -25,7 +26,7 @@ async def run_example():
trading_client = PerpetualTradingClient(ENDPOINT_CONFIG, stark_account)
markets_dict = await trading_client.markets_info.get_markets_dict()

market = markets_dict[ETH_USD_MARKET]
market = markets_dict[MARKET_NAME]
adjust_price_by_pct = get_adjust_price_by_pct(market.trading_config)

order_size = market.trading_config.min_order_size
Expand Down
3 changes: 2 additions & 1 deletion examples/02_create_limit_order_with_partial_tpsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from x10.perpetual.trading_client import PerpetualTradingClient

LOGGER = logging.getLogger()
MARKET_NAME = ETH_USD_MARKET
ENDPOINT_CONFIG = MAINNET_CONFIG


Expand All @@ -31,7 +32,7 @@ async def run_example():
trading_client = PerpetualTradingClient(ENDPOINT_CONFIG, stark_account)
markets_dict = await trading_client.markets_info.get_markets_dict()

market = markets_dict[ETH_USD_MARKET]
market = markets_dict[MARKET_NAME]
adjust_price_by_pct = get_adjust_price_by_pct(market.trading_config)

order_size = market.trading_config.min_order_size
Expand Down
4 changes: 3 additions & 1 deletion examples/03_subscribe_to_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
from signal import SIGINT, SIGTERM

from examples.init_env import init_env
from x10.config import ETH_USD_MARKET
from x10.perpetual.configuration import MAINNET_CONFIG
from x10.perpetual.stream_client import PerpetualStreamClient

LOGGER = logging.getLogger()
MARKET_NAME = ETH_USD_MARKET
ENDPOINT_CONFIG = MAINNET_CONFIG


Expand All @@ -16,7 +18,7 @@ async def subscribe_to_streams(stop_event: asyncio.Event):
stream_client = PerpetualStreamClient(api_url=ENDPOINT_CONFIG.stream_url)

async def subscribe_to_orderbook():
async with stream_client.subscribe_to_orderbooks("BTC-USD") as orderbook_stream:
async with stream_client.subscribe_to_orderbooks(MARKET_NAME) as orderbook_stream:
while not stop_event.is_set():
try:
msg = await asyncio.wait_for(orderbook_stream.recv(), timeout=1)
Expand Down
67 changes: 67 additions & 0 deletions examples/04_create_limit_order_with_builder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import logging.handlers
from asyncio import run

from examples.init_env import init_env
from examples.utils import find_order_and_cancel, get_adjust_price_by_pct
from x10.config import ETH_USD_MARKET
from x10.perpetual.accounts import StarkPerpetualAccount
from x10.perpetual.configuration import MAINNET_CONFIG
from x10.perpetual.order_object import create_order_object
from x10.perpetual.orders import OrderSide, TimeInForce
from x10.perpetual.trading_client import PerpetualTradingClient

LOGGER = logging.getLogger()
MARKET_NAME = ETH_USD_MARKET
ENDPOINT_CONFIG = MAINNET_CONFIG


async def run_example():
env_config = init_env()

assert env_config.builder_id, "X10_BUILDER_ID is not set"

stark_account = StarkPerpetualAccount(
api_key=env_config.api_key,
public_key=env_config.public_key,
private_key=env_config.private_key,
vault=env_config.vault_id,
)
trading_client = PerpetualTradingClient(ENDPOINT_CONFIG, stark_account)
markets_dict = await trading_client.markets_info.get_markets_dict()
fees = await trading_client.account.get_fees(market_names=[MARKET_NAME], builder_id=env_config.builder_id)
builder_fee = fees.data[0].builder_fee_rate

market = markets_dict[ETH_USD_MARKET]
adjust_price_by_pct = get_adjust_price_by_pct(market.trading_config)

order_size = market.trading_config.min_order_size
order_price = adjust_price_by_pct(market.market_stats.bid_price, -10.0)

LOGGER.info("Builder: id=%s, fee=%s", env_config.builder_id, builder_fee)
LOGGER.info("Creating LIMIT order object for market: %s", market.name)

new_order = create_order_object(
account=stark_account,
starknet_domain=ENDPOINT_CONFIG.starknet_domain,
market=market,
side=OrderSide.BUY,
amount_of_synthetic=order_size,
price=market.trading_config.round_price(order_price),
time_in_force=TimeInForce.GTT,
reduce_only=False,
post_only=True,
builder_id=env_config.builder_id,
builder_fee=builder_fee,
)

LOGGER.info("Placing order...")

placed_order = await trading_client.orders.place_order(order=new_order)

LOGGER.info("Order is placed: %s", placed_order.to_pretty_json())

await find_order_and_cancel(trading_client=trading_client, logger=LOGGER, order_id=placed_order.data.id)


if __name__ == "__main__":
run(main=run_example())
78 changes: 0 additions & 78 deletions examples/builder_order_example_simple.py

This file was deleted.

3 changes: 3 additions & 0 deletions examples/init_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class EnvConfig:
public_key: str | None = None
private_key: str | None = None
vault_id: int | None = None
builder_id: int | None = None


def init_env(require_private_api: bool = True):
Expand All @@ -28,6 +29,7 @@ def init_env(require_private_api: bool = True):
public_key = os.getenv("X10_PUBLIC_KEY")
private_key = os.getenv("X10_PRIVATE_KEY")
vault_id = os.getenv("X10_VAULT_ID")
builder_id = os.getenv("X10_BUILDER_ID")

if require_private_api:
assert api_key, "X10_API_KEY is not set"
Expand All @@ -43,4 +45,5 @@ def init_env(require_private_api: bool = True):
public_key=public_key,
private_key=private_key,
vault_id=int(vault_id) if vault_id else None,
builder_id=int(builder_id) if builder_id else None,
)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ build-backend = "poetry.core.masonry.api"

[tool.poetry]
name = "x10-python-trading-starknet"
version = "0.0.16"
version = "0.0.17"
description = "Python client for X10 API"
authors = ["X10 <tech@ex10.org>"]
repository = "https://github.com/x10xchange/python_sdk"
Expand Down