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: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,6 @@ target/
#Ipython Notebook
.ipynb_checkpoints
openapi-generator-cli.jar


.idea
171 changes: 2 additions & 169 deletions .openapi-generator/FILES
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
.github/workflows/python.yml
.gitignore
.gitlab-ci.yml
.openapi-generator-ignore
.travis.yml
README.md
docs/Account.md
docs/AccountApi.md
docs/AccountApiKeys.md
Expand All @@ -24,6 +18,7 @@ docs/Candlesticks.md
docs/ContractAddress.md
docs/CurrentHeight.md
docs/Cursor.md
docs/DailyReturn.md
docs/DepositHistory.md
docs/DepositHistoryItem.md
docs/DetailedAccount.md
Expand Down Expand Up @@ -52,7 +47,6 @@ docs/OrderBookStats.md
docs/OrderBooks.md
docs/Orders.md
docs/PnLEntry.md
docs/PositionFunding.md
docs/PriceLevel.md
docs/PublicPool.md
docs/PublicPoolInfo.md
Expand Down Expand Up @@ -107,8 +101,6 @@ docs/WithdrawHistory.md
docs/WithdrawHistoryCursor.md
docs/WithdrawHistoryItem.md
docs/ZkLighterInfo.md
git_push.sh
lighter/__init__.py
lighter/api/__init__.py
lighter/api/account_api.py
lighter/api/block_api.py
Expand All @@ -124,209 +116,50 @@ lighter/exceptions.py
lighter/models/__init__.py
lighter/models/account.py
lighter/models/account_api_keys.py
lighter/models/account_market_stats.py
lighter/models/account_metadata.py
lighter/models/account_pn_l.py
lighter/models/account_position.py
lighter/models/account_stats.py
lighter/models/accounts.py
lighter/models/api_key.py
lighter/models/block.py
lighter/models/blocks.py
lighter/models/bridge_supported_network.py
lighter/models/candlestick.py
lighter/models/candlesticks.py
lighter/models/contract_address.py
lighter/models/current_height.py
lighter/models/cursor.py
lighter/models/daily_return.py
lighter/models/deposit_history.py
lighter/models/deposit_history_item.py
lighter/models/detailed_account.py
lighter/models/detailed_accounts.py
lighter/models/detailed_candlestick.py
lighter/models/enriched_tx.py
lighter/models/exchange_stats.py
lighter/models/fee_bucket.py
lighter/models/funding.py
lighter/models/fundings.py
lighter/models/is_whitelisted.py
lighter/models/l1_provider_info.py
lighter/models/layer2_basic_info.py
lighter/models/liquidation.py
lighter/models/market_info.py
lighter/models/next_nonce.py
lighter/models/order.py
lighter/models/order_book.py
lighter/models/order_book_depth.py
lighter/models/order_book_detail.py
lighter/models/order_book_details.py
lighter/models/order_book_orders.py
lighter/models/order_book_stats.py
lighter/models/order_books.py
lighter/models/orders.py
lighter/models/pn_l_entry.py
lighter/models/position_funding.py
lighter/models/price_level.py
lighter/models/public_pool.py
lighter/models/public_pool_info.py
lighter/models/public_pool_share.py
lighter/models/public_pools.py
lighter/models/req_get_account.py
lighter/models/req_get_account_active_orders.py
lighter/models/req_get_account_api_keys.py
lighter/models/req_get_account_by_l1_address.py
lighter/models/req_get_account_inactive_orders.py
lighter/models/req_get_account_orders.py
lighter/models/req_get_account_pending_txs.py
lighter/models/req_get_account_pn_l.py
lighter/models/req_get_account_txs.py
lighter/models/req_get_block.py
lighter/models/req_get_block_txs.py
lighter/models/req_get_by_account.py
lighter/models/req_get_candlesticks.py
lighter/models/req_get_deposit_history.py
lighter/models/req_get_fee_bucket.py
lighter/models/req_get_fundings.py
lighter/models/req_get_l1_tx.py
lighter/models/req_get_latest_deposit.py
lighter/models/req_get_next_nonce.py
lighter/models/req_get_order_book_details.py
lighter/models/req_get_order_book_orders.py
lighter/models/req_get_order_books.py
lighter/models/req_get_public_pools.py
lighter/models/req_get_range_with_cursor.py
lighter/models/req_get_range_with_index.py
lighter/models/req_get_range_with_index_sortable.py
lighter/models/req_get_recent_trades.py
lighter/models/req_get_trades.py
lighter/models/req_get_tx.py
lighter/models/req_get_withdraw_history.py
lighter/models/req_is_whitelisted.py
lighter/models/result_code.py
lighter/models/simple_order.py
lighter/models/status.py
lighter/models/sub_accounts.py
lighter/models/ticker.py
lighter/models/trade.py
lighter/models/trades.py
lighter/models/tx.py
lighter/models/tx_hash.py
lighter/models/tx_hashes.py
lighter/models/txs.py
lighter/models/validator_info.py
lighter/models/withdraw_history.py
lighter/models/withdraw_history_cursor.py
lighter/models/withdraw_history_item.py
lighter/models/zk_lighter_info.py
lighter/py.typed
lighter/rest.py
pyproject.toml
requirements.txt
setup.cfg
setup.py
test-requirements.txt
test/__init__.py
test/test_account.py
test/test_account_api.py
test/test_account_api_keys.py
test/test_account_market_stats.py
test/test_account_metadata.py
test/test_account_pn_l.py
test/test_account_position.py
test/test_account_stats.py
test/test_accounts.py
test/test_api_key.py
test/test_block.py
test/test_block_api.py
test/test_blocks.py
test/test_bridge_supported_network.py
test/test_candlestick.py
test/test_candlestick_api.py
test/test_candlesticks.py
test/test_contract_address.py
test/test_current_height.py
test/test_cursor.py
test/test_deposit_history.py
test/test_deposit_history_item.py
test/test_detailed_account.py
test/test_detailed_accounts.py
test/test_detailed_candlestick.py
test/test_enriched_tx.py
test/test_exchange_stats.py
test/test_fee_bucket.py
test/test_funding.py
test/test_fundings.py
test/test_info_api.py
test/test_is_whitelisted.py
test/test_l1_provider_info.py
test/test_layer2_basic_info.py
test/test_liquidation.py
test/test_market_info.py
test/test_next_nonce.py
test/test_order.py
test/test_order_api.py
test/test_order_book.py
test/test_order_book_depth.py
test/test_order_book_detail.py
test/test_order_book_details.py
test/test_order_book_orders.py
test/test_order_book_stats.py
test/test_order_books.py
test/test_orders.py
test/test_pn_l_entry.py
test/test_position_funding.py
test/test_price_level.py
test/test_public_pool.py
test/test_public_pool_info.py
test/test_public_pool_share.py
test/test_public_pools.py
test/test_req_get_account.py
test/test_req_get_account_active_orders.py
test/test_req_get_account_api_keys.py
test/test_req_get_account_by_l1_address.py
test/test_req_get_account_inactive_orders.py
test/test_req_get_account_orders.py
test/test_req_get_account_pending_txs.py
test/test_req_get_account_pn_l.py
test/test_req_get_account_txs.py
test/test_req_get_block.py
test/test_req_get_block_txs.py
test/test_req_get_by_account.py
test/test_req_get_candlesticks.py
test/test_req_get_deposit_history.py
test/test_req_get_fee_bucket.py
test/test_req_get_fundings.py
test/test_req_get_l1_tx.py
test/test_req_get_latest_deposit.py
test/test_req_get_next_nonce.py
test/test_req_get_order_book_details.py
test/test_req_get_order_book_orders.py
test/test_req_get_order_books.py
test/test_req_get_public_pools.py
test/test_req_get_range_with_cursor.py
test/test_req_get_range_with_index.py
test/test_req_get_range_with_index_sortable.py
test/test_req_get_recent_trades.py
test/test_req_get_trades.py
test/test_req_get_tx.py
test/test_req_get_withdraw_history.py
test/test_req_is_whitelisted.py
test/test_result_code.py
test/test_root_api.py
test/test_simple_order.py
test/test_status.py
test/test_sub_accounts.py
test/test_ticker.py
test/test_trade.py
test/test_trades.py
test/test_transaction_api.py
test/test_tx.py
test/test_tx_hash.py
test/test_tx_hashes.py
test/test_txs.py
test/test_validator_info.py
test/test_withdraw_history.py
test/test_withdraw_history_cursor.py
test/test_withdraw_history_item.py
test/test_zk_lighter_info.py
tox.ini
24 changes: 24 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
## Setup steps for testnet
- Go to https://testnet.app.lighter.xyz/ and connect a wallet to receive $500
- run `system_setup.py` with the correct ETH Private key configured
- 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/)
- this will require you to enter your Ethereum private key
- the eth private key will only be used in the Py SDK to sign a message
- the eth private key is not required in order to trade on the platform
- the eth private key is not passed to the binary
- copy the output of the script and post it into `create_cancel_order.py`
- the output should look like
```
BASE_URL = 'https://testnet.zklighter.elliot.ai'
API_KEY_PRIVATE_KEY = '0xea5d2eca5be67eca056752eaf27b173518b8a5550117c09d2b58c7ea7d306cc4426f913ccf27ab19'
ACCOUNT_INDEX = 595
API_KEY_INDEX = 1
```
- start trading using
- `create_cancel_order.py` has an example which created an order on testnet & cancels it
- you'll need to set up both your account index, api key index & API Key private key

## Setup steps for mainnet
- deposit money on Lighter to create an account first
- change the URL to `mainnet.zklighter.elliot.ai`
- repeat setup step
78 changes: 38 additions & 40 deletions examples/create_cancel_order.py
Original file line number Diff line number Diff line change
@@ -1,58 +1,56 @@
import asyncio
import logging
import time
from lighter import SignerClient
import lighter
import lighter.configuration

logging.basicConfig(level=logging.DEBUG)


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


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


async def main():
api_client = lighter.ApiClient(
configuration=lighter.Configuration(host=BASE_URL)
api_client = lighter.ApiClient(configuration=lighter.Configuration(host=BASE_URL))

client = lighter.SignerClient(
url=BASE_URL,
private_key=API_KEY_PRIVATE_KEY,
account_index=ACCOUNT_INDEX,
api_key_index=API_KEY_INDEX,
)

# assuring that client is created
account_created = False
new_account = False
while not account_created:
try:
client = SignerClient(url=BASE_URL, private_key=PRIVATE_KEY)
await client.set_account_index()
account_created = True
if new_account:
time.sleep(10)
except Exception as e:
new_account = True
print("Account not created yet", trim_exception(e))
time.sleep(2)

print("Account Index: ", client.account_index)

tx = await client.create_order(
err = client.check_client()
if err is not None:
print(f"CheckClient error: {trim_exception(err)}")
return

# create order
tx, tx_hash, err = await client.create_order(
market_index=0,
client_order_index=0,
base_amount=10000,
price=250000,
is_ask=False,
client_order_index=123,
base_amount=100000,
price=200000,
is_ask=True,
order_type=lighter.SignerClient.ORDER_TYPE_LIMIT,
time_in_force=lighter.SignerClient.ORDER_TIME_IN_FORCE_GOOD_TILL_TIME,
reduce_only=0,
trigger_price=0,
)
print("Create Order Tx:", tx)
print(f"Create Order {tx=} {tx_hash=} {err=}")
if err is not None:
raise Exception(err)

auth = client.create_auth_token_with_expiry(SignerClient.DEFAULT_10_MIN_AUTH_EXPIRY)
print("Auth:", auth)
auth, err = client.create_auth_token_with_expiry(lighter.SignerClient.DEFAULT_10_MIN_AUTH_EXPIRY)
print(f"{auth=}")
if err is not None:
raise Exception(err)

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

print("Active Orders:", active_orders.orders)
print(f"{active_orders.orders=}")

last_order_index = active_orders.orders[0].order_index

tx, response = await client.cancel_order(
market_index=0, order_index=last_order_index
)
print(
f"code: {response.code} msg: {response.message} l2Hash: {response.tx_hash} tx: {tx.to_json()}"
# cancel order
tx, tx_hash, err = await client.cancel_order(
market_index=0,
order_index=123,
)
print(f"Cancel Order {tx=} {tx_hash=} {err=}")
if err is not None:
raise Exception(err)

await client.close()
await api_client.close()
Expand Down
Loading