Skip to content

Commit a097e3e

Browse files
committed
README & configurable spot markets for sendBatchTx examples
1 parent c594422 commit a097e3e

3 files changed

Lines changed: 63 additions & 15 deletions

File tree

examples/README.md

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919
- same flow as `create_modify_cancel_order_http.py`
2020
- sends TXs over WS instead of HTTP
2121

22+
- `create_market_order_eth_buy.py`
23+
- creates a market buy order for 0.1 ETH @ market price
24+
- `create_market_order_eth_sell.py`
25+
- creates a market sell order for 0.1 ETH @ market price
26+
2227
- `create_grouped_ioc_with_attached_sl_tp.py`
2328
- creates an ask (sell) IoC order for 0.1 ETH
2429
- along w/ the order, it sets up a Stop Loss (SL) and a Take Profit (TP) order for the whole size of the order
@@ -31,7 +36,7 @@
3136
- the orders will grow / shrink as you accumulate more position
3237
- the SL/TP orders are canceled when the sign of your position changes
3338

34-
### On SL/TP orders
39+
## On SL/TP orders
3540
SL/TP orders need to be configured beyond just setting the trigger price. When the trigger price is set,
3641
the order will just be executed, like a normal order. This means that a market order, for example, might not have enough slippage! \
3742
Let's say that you have a 1 BTC long position, and the current price is $110'000. \
@@ -43,7 +48,7 @@ What about the order types? Just as normal orders, SL/TP orders trigger an order
4348
- market order
4449
- limit IOC / GTC
4550

46-
### Modify leverage / Margin Mode (Cross, Isolated) / Add Collateral to isolated-only positions
51+
## Modify leverage / Margin Mode (Cross, Isolated) / Add Collateral to isolated-only positions
4752
- `margin_eth_20x_cross_http`
4853
- sets ETH market to 20x leverage and cross-margin mode, using HTTP
4954
- `margin_eth_50x_isolate_ws`
@@ -53,14 +58,51 @@ What about the order types? Just as normal orders, SL/TP orders trigger an order
5358
- `margin_eth_remove_collateral_ws.py`
5459
- removes $5 USDC from the ETH position (must be opened and in isolated mode)
5560

56-
### Batch orders
61+
## Batch orders
5762
- `send_batch_tx_http.py`
5863
- sends multiple orders in a single HTTP request
5964
- `send_batch_tx_ws.py`
6065
- sends multiple orders in a single WS request`
6166

6267
Batch TXs will be executed back to back, without the possibility of other TXs interfering.
6368

69+
## Spot Trading
70+
To trade spot markets, you need to have spot USDC. USDC used in your perpetual account will be used as collateral for your cross-positions.
71+
USDC deposited in the spot account can only be used to buy spot assets.
72+
To transfer USDC between spot <> perp balance, or vice verse, check out
73+
- `spot_self_transfer_perp_spot.py`
74+
- `spot_self_transfer_spot_perp.py`
75+
76+
Order placement / trades work in the same way as for perpetual markets.
77+
The fee will be paid in the received asset for premium spot trades.
78+
This means that if you sell ETH, you'll receive less USDC, and if you BUY 1 ETH, you'll receive slightly less than 1 ETH.
79+
You can check out the following examples, which should work on spot ETH by changing the market index to 2048 instead of 0.
80+
- `create_modify_cancel_order_http.py`
81+
- `create_modify_cancel_order_ws.py`
82+
- `create_market_order_eth_buy.py`
83+
- `create_market_order_eth_sell.py`
84+
- `send_batch_tx_http.py`
85+
- `send_batch_tx_ws.py`
86+
87+
Trading setup is very similar to perpetual markets.
88+
The only difference is that you'll need to hold USDC / ETH before placing an order.
89+
For example, on perp markets you can place an order to short (sell) ETH without having to worry that much.
90+
The limitation there would be to have enough available collateral to cover the order.
91+
On spot orders, you need to have enough assets in your spot account to cover all open orders.
92+
If you want to place two orders, to buy 1000 USDC worth of ETH and 1000 USDC worth of ZK, you'll need to have at least 2000 available USDC.
93+
94+
You can get the order book details (including symbol and market index) as well as quote asset id (ETH) and base asset id (USDC)
95+
by following the example below:
96+
- `spot_get_order_books.py`
97+
98+
Note: you'll need the quote asset id and base asset id to check available balance.
99+
Available balance is not locked in open orders.
100+
101+
To keep track of your spot balance, you can use HTTP calls or a websocket subscription.
102+
Examples on how to do this can be found here:
103+
- `spot_get_account_assets_http.py`
104+
- `spot_get_account_assets_ws.py`
105+
64106
## Setup steps for mainnet
65107
- deposit money on Lighter to create an account first
66108
- change the URL to `mainnet.zklighter.elliot.ai`

examples/send_batch_tx_http.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77
async def main():
88
client, api_client, _ = default_example_setup()
99

10+
# Note: change this to 2048 to trade spot ETH. Make sure you have at least 0.1 ETH to trade spot.
11+
market_index = 0
12+
1013
api_key_index, nonce = client.nonce_manager.next_nonce()
1114
ask_tx_type, ask_tx_info, ask_tx_hash, error = client.sign_create_order(
12-
market_index=0,
15+
market_index=market_index,
1316
client_order_index=1001, # Unique identifier for this order
1417
base_amount=1000, # 0.1 ETH
1518
price=5000_00, # $5000
@@ -30,7 +33,7 @@ async def main():
3033
# in batch TXs, all TXs must come from the same API key.
3134
api_key_index, nonce = client.nonce_manager.next_nonce(api_key_index)
3235
bid_tx_type, bid_tx_info, bid_tx_hash, error = client.sign_create_order(
33-
market_index=0,
36+
market_index=market_index,
3437
client_order_index=1002, # Different unique identifier
3538
base_amount=1000, # 0.1 ETH
3639
price=1500_00, # $1500
@@ -63,7 +66,7 @@ async def main():
6366
# since this is a new batch, we can request a fresh API key
6467
api_key_index, nonce = client.nonce_manager.next_nonce()
6568
cancel_tx_type, cancel_tx_info, cancel_tx_hash, error = client.sign_cancel_order(
66-
market_index=0,
69+
market_index=market_index,
6770
order_index=1001, # the index of the order we want cancelled
6871
nonce=nonce,
6972
api_key_index=api_key_index,
@@ -77,7 +80,7 @@ async def main():
7780
# in batch TXs, all TXs must come from the same API key.
7881
api_key_index, nonce = client.nonce_manager.next_nonce(api_key_index)
7982
new_ask_tx_type, new_ask_tx_info, new_ask_tx_hash, error = client.sign_create_order(
80-
market_index=0,
83+
market_index=market_index,
8184
client_order_index=1003, # Different unique identifier
8285
base_amount=2000, # 0.2 ETH
8386
price=5500_00, # $5500
@@ -110,7 +113,7 @@ async def main():
110113
# since this is a new batch, we can request a fresh API key
111114
api_key_index, nonce = client.nonce_manager.next_nonce()
112115
cancel_1_tx_type, cancel_1_tx_info, cancel_1_tx_hash, error = client.sign_cancel_order(
113-
market_index=0,
116+
market_index=market_index,
114117
order_index=1002, # the index of the order we want cancelled
115118
nonce=nonce,
116119
api_key_index=api_key_index,
@@ -122,7 +125,7 @@ async def main():
122125

123126
api_key_index, nonce = client.nonce_manager.next_nonce(api_key_index)
124127
cancel_2_tx_type, cancel_2_tx_info, cancel_2_tx_hash, error = client.sign_cancel_order(
125-
market_index=0,
128+
market_index=market_index,
126129
order_index=1003, # the index of the order we want cancelled
127130
nonce=nonce,
128131
api_key_index=api_key_index,

examples/send_batch_tx_ws.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@ async def main():
1313
ws_client: websockets.ClientConnection = await ws_client_promise
1414
print("Received:", await ws_client.recv())
1515

16+
# Note: change this to 2048 to trade spot ETH. Make sure you have at least 0.1 ETH to trade spot.
17+
market_index = 2048
18+
1619
api_key_index, nonce = client.nonce_manager.next_nonce()
1720
ask_tx_type, ask_tx_info, ask_tx_hash, error = client.sign_create_order(
18-
market_index=0,
21+
market_index=market_index,
1922
client_order_index=1001, # Unique identifier for this order
2023
base_amount=1000, # 0.1 ETH
2124
price=5000_00, # $5000
@@ -36,7 +39,7 @@ async def main():
3639
# in batch TXs, all TXs must come from the same API key.
3740
api_key_index, nonce = client.nonce_manager.next_nonce(api_key_index)
3841
bid_tx_type, bid_tx_info, bid_tx_hash, error = client.sign_create_order(
39-
market_index=0,
42+
market_index=market_index,
4043
client_order_index=1002, # Different unique identifier
4144
base_amount=1000, # 0.1 ETH
4245
price=1500_00, # $1500
@@ -65,7 +68,7 @@ async def main():
6568
# since this is a new batch, we can request a fresh API key
6669
api_key_index, nonce = client.nonce_manager.next_nonce()
6770
cancel_tx_type, cancel_tx_info, cancel_tx_hash, error = client.sign_cancel_order(
68-
market_index=0,
71+
market_index=market_index,
6972
order_index=1001, # the index of the order we want cancelled
7073
nonce=nonce,
7174
api_key_index=api_key_index,
@@ -79,7 +82,7 @@ async def main():
7982
# in batch TXs, all TXs must come from the same API key.
8083
api_key_index, nonce = client.nonce_manager.next_nonce(api_key_index)
8184
new_ask_tx_type, new_ask_tx_info, new_ask_tx_hash, error = client.sign_create_order(
82-
market_index=0,
85+
market_index=market_index,
8386
client_order_index=1003, # Different unique identifier
8487
base_amount=2000, # 0.2 ETH
8588
price=5500_00, # $5500
@@ -108,7 +111,7 @@ async def main():
108111
# since this is a new batch, we can request a fresh API key
109112
api_key_index, nonce = client.nonce_manager.next_nonce()
110113
cancel_1_tx_type, cancel_1_tx_info, cancel_1_tx_hash, error = client.sign_cancel_order(
111-
market_index=0,
114+
market_index=market_index,
112115
order_index=1002, # the index of the order we want cancelled
113116
nonce=nonce,
114117
api_key_index=api_key_index,
@@ -120,7 +123,7 @@ async def main():
120123

121124
api_key_index, nonce = client.nonce_manager.next_nonce(api_key_index)
122125
cancel_2_tx_type, cancel_2_tx_info, cancel_2_tx_hash, error = client.sign_cancel_order(
123-
market_index=0,
126+
market_index=market_index,
124127
order_index=1003, # the index of the order we want cancelled
125128
nonce=nonce,
126129
api_key_index=api_key_index,

0 commit comments

Comments
 (0)