Skip to content

Commit a26e2e4

Browse files
authored
Merge pull request #8 from elliottech/refactor-signer
Refactor signer
2 parents d687f96 + 0fd595e commit a26e2e4

File tree

177 files changed

+1199
-14343
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

177 files changed

+1199
-14343
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,6 @@ target/
6363
#Ipython Notebook
6464
.ipynb_checkpoints
6565
openapi-generator-cli.jar
66+
67+
68+
.idea

.openapi-generator/FILES

Lines changed: 2 additions & 169 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
.github/workflows/python.yml
2-
.gitignore
3-
.gitlab-ci.yml
4-
.openapi-generator-ignore
5-
.travis.yml
6-
README.md
71
docs/Account.md
82
docs/AccountApi.md
93
docs/AccountApiKeys.md
@@ -24,6 +18,7 @@ docs/Candlesticks.md
2418
docs/ContractAddress.md
2519
docs/CurrentHeight.md
2620
docs/Cursor.md
21+
docs/DailyReturn.md
2722
docs/DepositHistory.md
2823
docs/DepositHistoryItem.md
2924
docs/DetailedAccount.md
@@ -52,7 +47,6 @@ docs/OrderBookStats.md
5247
docs/OrderBooks.md
5348
docs/Orders.md
5449
docs/PnLEntry.md
55-
docs/PositionFunding.md
5650
docs/PriceLevel.md
5751
docs/PublicPool.md
5852
docs/PublicPoolInfo.md
@@ -107,8 +101,6 @@ docs/WithdrawHistory.md
107101
docs/WithdrawHistoryCursor.md
108102
docs/WithdrawHistoryItem.md
109103
docs/ZkLighterInfo.md
110-
git_push.sh
111-
lighter/__init__.py
112104
lighter/api/__init__.py
113105
lighter/api/account_api.py
114106
lighter/api/block_api.py
@@ -124,209 +116,50 @@ lighter/exceptions.py
124116
lighter/models/__init__.py
125117
lighter/models/account.py
126118
lighter/models/account_api_keys.py
127-
lighter/models/account_market_stats.py
128-
lighter/models/account_metadata.py
129119
lighter/models/account_pn_l.py
130120
lighter/models/account_position.py
131-
lighter/models/account_stats.py
132-
lighter/models/accounts.py
133121
lighter/models/api_key.py
134122
lighter/models/block.py
135123
lighter/models/blocks.py
136-
lighter/models/bridge_supported_network.py
137124
lighter/models/candlestick.py
138125
lighter/models/candlesticks.py
139-
lighter/models/contract_address.py
140126
lighter/models/current_height.py
141-
lighter/models/cursor.py
127+
lighter/models/daily_return.py
142128
lighter/models/deposit_history.py
143129
lighter/models/deposit_history_item.py
144130
lighter/models/detailed_account.py
145131
lighter/models/detailed_accounts.py
146-
lighter/models/detailed_candlestick.py
147132
lighter/models/enriched_tx.py
148133
lighter/models/exchange_stats.py
149134
lighter/models/fee_bucket.py
150135
lighter/models/funding.py
151136
lighter/models/fundings.py
152-
lighter/models/is_whitelisted.py
153-
lighter/models/l1_provider_info.py
154137
lighter/models/layer2_basic_info.py
155-
lighter/models/liquidation.py
156-
lighter/models/market_info.py
157138
lighter/models/next_nonce.py
158139
lighter/models/order.py
159140
lighter/models/order_book.py
160-
lighter/models/order_book_depth.py
161141
lighter/models/order_book_detail.py
162142
lighter/models/order_book_details.py
163143
lighter/models/order_book_orders.py
164144
lighter/models/order_book_stats.py
165145
lighter/models/order_books.py
166146
lighter/models/orders.py
167147
lighter/models/pn_l_entry.py
168-
lighter/models/position_funding.py
169-
lighter/models/price_level.py
170148
lighter/models/public_pool.py
171149
lighter/models/public_pool_info.py
172150
lighter/models/public_pool_share.py
173151
lighter/models/public_pools.py
174-
lighter/models/req_get_account.py
175-
lighter/models/req_get_account_active_orders.py
176-
lighter/models/req_get_account_api_keys.py
177-
lighter/models/req_get_account_by_l1_address.py
178-
lighter/models/req_get_account_inactive_orders.py
179-
lighter/models/req_get_account_orders.py
180-
lighter/models/req_get_account_pending_txs.py
181-
lighter/models/req_get_account_pn_l.py
182-
lighter/models/req_get_account_txs.py
183-
lighter/models/req_get_block.py
184-
lighter/models/req_get_block_txs.py
185-
lighter/models/req_get_by_account.py
186-
lighter/models/req_get_candlesticks.py
187-
lighter/models/req_get_deposit_history.py
188-
lighter/models/req_get_fee_bucket.py
189-
lighter/models/req_get_fundings.py
190-
lighter/models/req_get_l1_tx.py
191-
lighter/models/req_get_latest_deposit.py
192-
lighter/models/req_get_next_nonce.py
193-
lighter/models/req_get_order_book_details.py
194-
lighter/models/req_get_order_book_orders.py
195-
lighter/models/req_get_order_books.py
196-
lighter/models/req_get_public_pools.py
197-
lighter/models/req_get_range_with_cursor.py
198-
lighter/models/req_get_range_with_index.py
199-
lighter/models/req_get_range_with_index_sortable.py
200-
lighter/models/req_get_recent_trades.py
201-
lighter/models/req_get_trades.py
202-
lighter/models/req_get_tx.py
203-
lighter/models/req_get_withdraw_history.py
204-
lighter/models/req_is_whitelisted.py
205-
lighter/models/result_code.py
206152
lighter/models/simple_order.py
207153
lighter/models/status.py
208154
lighter/models/sub_accounts.py
209-
lighter/models/ticker.py
210155
lighter/models/trade.py
211156
lighter/models/trades.py
212157
lighter/models/tx.py
213158
lighter/models/tx_hash.py
214159
lighter/models/tx_hashes.py
215160
lighter/models/txs.py
216-
lighter/models/validator_info.py
217161
lighter/models/withdraw_history.py
218-
lighter/models/withdraw_history_cursor.py
219162
lighter/models/withdraw_history_item.py
220163
lighter/models/zk_lighter_info.py
221164
lighter/py.typed
222165
lighter/rest.py
223-
pyproject.toml
224-
requirements.txt
225-
setup.cfg
226-
setup.py
227-
test-requirements.txt
228-
test/__init__.py
229-
test/test_account.py
230-
test/test_account_api.py
231-
test/test_account_api_keys.py
232-
test/test_account_market_stats.py
233-
test/test_account_metadata.py
234-
test/test_account_pn_l.py
235-
test/test_account_position.py
236-
test/test_account_stats.py
237-
test/test_accounts.py
238-
test/test_api_key.py
239-
test/test_block.py
240-
test/test_block_api.py
241-
test/test_blocks.py
242-
test/test_bridge_supported_network.py
243-
test/test_candlestick.py
244-
test/test_candlestick_api.py
245-
test/test_candlesticks.py
246-
test/test_contract_address.py
247-
test/test_current_height.py
248-
test/test_cursor.py
249-
test/test_deposit_history.py
250-
test/test_deposit_history_item.py
251-
test/test_detailed_account.py
252-
test/test_detailed_accounts.py
253-
test/test_detailed_candlestick.py
254-
test/test_enriched_tx.py
255-
test/test_exchange_stats.py
256-
test/test_fee_bucket.py
257-
test/test_funding.py
258-
test/test_fundings.py
259-
test/test_info_api.py
260-
test/test_is_whitelisted.py
261-
test/test_l1_provider_info.py
262-
test/test_layer2_basic_info.py
263-
test/test_liquidation.py
264-
test/test_market_info.py
265-
test/test_next_nonce.py
266-
test/test_order.py
267-
test/test_order_api.py
268-
test/test_order_book.py
269-
test/test_order_book_depth.py
270-
test/test_order_book_detail.py
271-
test/test_order_book_details.py
272-
test/test_order_book_orders.py
273-
test/test_order_book_stats.py
274-
test/test_order_books.py
275-
test/test_orders.py
276-
test/test_pn_l_entry.py
277-
test/test_position_funding.py
278-
test/test_price_level.py
279-
test/test_public_pool.py
280-
test/test_public_pool_info.py
281-
test/test_public_pool_share.py
282-
test/test_public_pools.py
283-
test/test_req_get_account.py
284-
test/test_req_get_account_active_orders.py
285-
test/test_req_get_account_api_keys.py
286-
test/test_req_get_account_by_l1_address.py
287-
test/test_req_get_account_inactive_orders.py
288-
test/test_req_get_account_orders.py
289-
test/test_req_get_account_pending_txs.py
290-
test/test_req_get_account_pn_l.py
291-
test/test_req_get_account_txs.py
292-
test/test_req_get_block.py
293-
test/test_req_get_block_txs.py
294-
test/test_req_get_by_account.py
295-
test/test_req_get_candlesticks.py
296-
test/test_req_get_deposit_history.py
297-
test/test_req_get_fee_bucket.py
298-
test/test_req_get_fundings.py
299-
test/test_req_get_l1_tx.py
300-
test/test_req_get_latest_deposit.py
301-
test/test_req_get_next_nonce.py
302-
test/test_req_get_order_book_details.py
303-
test/test_req_get_order_book_orders.py
304-
test/test_req_get_order_books.py
305-
test/test_req_get_public_pools.py
306-
test/test_req_get_range_with_cursor.py
307-
test/test_req_get_range_with_index.py
308-
test/test_req_get_range_with_index_sortable.py
309-
test/test_req_get_recent_trades.py
310-
test/test_req_get_trades.py
311-
test/test_req_get_tx.py
312-
test/test_req_get_withdraw_history.py
313-
test/test_req_is_whitelisted.py
314-
test/test_result_code.py
315-
test/test_root_api.py
316-
test/test_simple_order.py
317-
test/test_status.py
318-
test/test_sub_accounts.py
319-
test/test_ticker.py
320-
test/test_trade.py
321-
test/test_trades.py
322-
test/test_transaction_api.py
323-
test/test_tx.py
324-
test/test_tx_hash.py
325-
test/test_tx_hashes.py
326-
test/test_txs.py
327-
test/test_validator_info.py
328-
test/test_withdraw_history.py
329-
test/test_withdraw_history_cursor.py
330-
test/test_withdraw_history_item.py
331-
test/test_zk_lighter_info.py
332-
tox.ini

examples/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
## Setup steps for testnet
2+
- Go to https://testnet.app.lighter.xyz/ and connect a wallet to receive $500
3+
- run `system_setup.py` with the correct ETH Private key configured
4+
- set an API key index which is not 0, so you won't override the one used by [app.lighter.xyz](https://app.lighter.xyz/)
5+
- this will require you to enter your Ethereum private key
6+
- the eth private key will only be used in the Py SDK to sign a message
7+
- the eth private key is not required in order to trade on the platform
8+
- the eth private key is not passed to the binary
9+
- copy the output of the script and post it into `create_cancel_order.py`
10+
- the output should look like
11+
```
12+
BASE_URL = 'https://testnet.zklighter.elliot.ai'
13+
API_KEY_PRIVATE_KEY = '0xea5d2eca5be67eca056752eaf27b173518b8a5550117c09d2b58c7ea7d306cc4426f913ccf27ab19'
14+
ACCOUNT_INDEX = 595
15+
API_KEY_INDEX = 1
16+
```
17+
- start trading using
18+
- `create_cancel_order.py` has an example which created an order on testnet & cancels it
19+
- you'll need to set up both your account index, api key index & API Key private key
20+
21+
## Setup steps for mainnet
22+
- deposit money on Lighter to create an account first
23+
- change the URL to `mainnet.zklighter.elliot.ai`
24+
- repeat setup step

examples/create_cancel_order.py

Lines changed: 38 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,56 @@
11
import asyncio
22
import logging
3-
import time
4-
from lighter import SignerClient
53
import lighter
6-
import lighter.configuration
74

85
logging.basicConfig(level=logging.DEBUG)
96

10-
7+
# The API_KEY_PRIVATE_KEY provided belongs to a dummy account registered on Testnet.
8+
# It was generated using the setup_system.py script, and servers as an example.
119
BASE_URL = "https://testnet.zklighter.elliot.ai"
12-
PRIVATE_KEY = "WALLET_PRIVATE_KEY"
10+
API_KEY_PRIVATE_KEY = "0xc0a06468a5bbc9a7b785065a8b227a37fdfa18e2b81d51b018cb03ddd99bfbef4b7551f0f0765639"
11+
ACCOUNT_INDEX = 595
12+
API_KEY_INDEX = 1
1313

1414

1515
def trim_exception(e: Exception) -> str:
1616
return str(e).strip().split("\n")[-1]
1717

1818

1919
async def main():
20-
api_client = lighter.ApiClient(
21-
configuration=lighter.Configuration(host=BASE_URL)
20+
api_client = lighter.ApiClient(configuration=lighter.Configuration(host=BASE_URL))
21+
22+
client = lighter.SignerClient(
23+
url=BASE_URL,
24+
private_key=API_KEY_PRIVATE_KEY,
25+
account_index=ACCOUNT_INDEX,
26+
api_key_index=API_KEY_INDEX,
2227
)
2328

24-
# assuring that client is created
25-
account_created = False
26-
new_account = False
27-
while not account_created:
28-
try:
29-
client = SignerClient(url=BASE_URL, private_key=PRIVATE_KEY)
30-
await client.set_account_index()
31-
account_created = True
32-
if new_account:
33-
time.sleep(10)
34-
except Exception as e:
35-
new_account = True
36-
print("Account not created yet", trim_exception(e))
37-
time.sleep(2)
38-
39-
print("Account Index: ", client.account_index)
40-
41-
tx = await client.create_order(
29+
err = client.check_client()
30+
if err is not None:
31+
print(f"CheckClient error: {trim_exception(err)}")
32+
return
33+
34+
# create order
35+
tx, tx_hash, err = await client.create_order(
4236
market_index=0,
43-
client_order_index=0,
44-
base_amount=10000,
45-
price=250000,
46-
is_ask=False,
37+
client_order_index=123,
38+
base_amount=100000,
39+
price=200000,
40+
is_ask=True,
4741
order_type=lighter.SignerClient.ORDER_TYPE_LIMIT,
4842
time_in_force=lighter.SignerClient.ORDER_TIME_IN_FORCE_GOOD_TILL_TIME,
4943
reduce_only=0,
5044
trigger_price=0,
5145
)
52-
print("Create Order Tx:", tx)
46+
print(f"Create Order {tx=} {tx_hash=} {err=}")
47+
if err is not None:
48+
raise Exception(err)
5349

54-
auth = client.create_auth_token_with_expiry(SignerClient.DEFAULT_10_MIN_AUTH_EXPIRY)
55-
print("Auth:", auth)
50+
auth, err = client.create_auth_token_with_expiry(lighter.SignerClient.DEFAULT_10_MIN_AUTH_EXPIRY)
51+
print(f"{auth=}")
52+
if err is not None:
53+
raise Exception(err)
5654

5755
active_orders = await lighter.OrderApi(api_client).account_active_orders(
5856
account_index=client.account_index, market_id=0, auth=auth
@@ -61,16 +59,16 @@ async def main():
6159
print("No active orders")
6260
return
6361

64-
print("Active Orders:", active_orders.orders)
62+
print(f"{active_orders.orders=}")
6563

66-
last_order_index = active_orders.orders[0].order_index
67-
68-
tx, response = await client.cancel_order(
69-
market_index=0, order_index=last_order_index
70-
)
71-
print(
72-
f"code: {response.code} msg: {response.message} l2Hash: {response.tx_hash} tx: {tx.to_json()}"
64+
# cancel order
65+
tx, tx_hash, err = await client.cancel_order(
66+
market_index=0,
67+
order_index=123,
7368
)
69+
print(f"Cancel Order {tx=} {tx_hash=} {err=}")
70+
if err is not None:
71+
raise Exception(err)
7472

7573
await client.close()
7674
await api_client.close()

0 commit comments

Comments
 (0)