mirror of
https://github.com/discountry/ritmex-bot.git
synced 2026-09-09 16:28:06 +00:00
refactor: restructure Lighter SDK by removing deprecated Go files and enhancing Python documentation with new examples and models
This commit is contained in:
@@ -3,15 +3,21 @@ import logging
|
||||
import time
|
||||
import eth_account
|
||||
import lighter
|
||||
from utils import save_api_key_config
|
||||
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
|
||||
# this is a dummy private key which is registered on Testnet.
|
||||
# this is a dummy private key registered on Testnet.
|
||||
# It serves as a good example
|
||||
BASE_URL = "https://testnet.zklighter.elliot.ai"
|
||||
ETH_PRIVATE_KEY = "1234567812345678123456781234567812345678123456781234567812345678"
|
||||
API_KEY_INDEX = 3
|
||||
NUM_API_KEYS = 5
|
||||
|
||||
# If you set this to something other than None, the script will use that account index instead of using the master account index.
|
||||
# This is useful if you have multiple accounts on the same L1 address or are the owner of a public pool.
|
||||
# You need to use the private key associated to the master account or the owner of the public pool to change the API keys.
|
||||
ACCOUNT_INDEX = None
|
||||
|
||||
async def main():
|
||||
# verify that the account exists & fetch account index
|
||||
@@ -19,47 +25,58 @@ async def main():
|
||||
eth_acc = eth_account.Account.from_key(ETH_PRIVATE_KEY)
|
||||
eth_address = eth_acc.address
|
||||
|
||||
try:
|
||||
response = await lighter.AccountApi(api_client).accounts_by_l1_address(
|
||||
l1_address=eth_address
|
||||
)
|
||||
except lighter.ApiException as e:
|
||||
if e.data.message == "account not found":
|
||||
print(f"error: account not found for {eth_address}")
|
||||
return
|
||||
else:
|
||||
raise e
|
||||
|
||||
if len(response.sub_accounts) > 1:
|
||||
for sub_account in response.sub_accounts:
|
||||
print(f"found accountIndex: {sub_account.index}")
|
||||
|
||||
print("multiple accounts found, using the first one")
|
||||
account_index = response.sub_accounts[0].index
|
||||
if ACCOUNT_INDEX is not None:
|
||||
account_index = ACCOUNT_INDEX
|
||||
else:
|
||||
account_index = response.sub_accounts[0].index
|
||||
try:
|
||||
response = await lighter.AccountApi(api_client).accounts_by_l1_address(l1_address=eth_address)
|
||||
except lighter.ApiException as e:
|
||||
if e.data.message == "account not found":
|
||||
print(f"error: account not found for {eth_address}")
|
||||
return
|
||||
else:
|
||||
raise e
|
||||
|
||||
if len(response.sub_accounts) > 1:
|
||||
for sub_account in response.sub_accounts:
|
||||
print(f"found accountIndex: {sub_account.index}")
|
||||
|
||||
account = min(response.sub_accounts, key=lambda x: int(x.index))
|
||||
account_index = account.index
|
||||
print(f"multiple accounts found, using the master account {account_index}")
|
||||
else:
|
||||
account_index = response.sub_accounts[0].index
|
||||
|
||||
|
||||
# create a private/public key pair for the new API key
|
||||
# pass any string to be used as seed for create_api_key like
|
||||
# create_api_key("Hello world random seed to make things more secure")
|
||||
private_key, public_key, err = lighter.create_api_key()
|
||||
if err is not None:
|
||||
raise Exception(err)
|
||||
|
||||
private_keys = {}
|
||||
public_keys = []
|
||||
|
||||
for i in range(NUM_API_KEYS):
|
||||
private_key, public_key, err = lighter.create_api_key()
|
||||
if err is not None:
|
||||
raise Exception(err)
|
||||
public_keys.append(public_key)
|
||||
private_keys[API_KEY_INDEX + i] = private_key
|
||||
|
||||
tx_client = lighter.SignerClient(
|
||||
url=BASE_URL,
|
||||
private_key=private_key,
|
||||
account_index=account_index,
|
||||
api_key_index=API_KEY_INDEX,
|
||||
api_private_keys=private_keys,
|
||||
)
|
||||
|
||||
# change the API key
|
||||
response, err = await tx_client.change_api_key(
|
||||
eth_private_key=ETH_PRIVATE_KEY,
|
||||
new_pubkey=public_key,
|
||||
)
|
||||
if err is not None:
|
||||
raise Exception(err)
|
||||
# change all API keys
|
||||
for i in range(NUM_API_KEYS):
|
||||
response, err = await tx_client.change_api_key(
|
||||
eth_private_key=ETH_PRIVATE_KEY,
|
||||
new_pubkey=public_keys[i],
|
||||
api_key_index=API_KEY_INDEX + i
|
||||
)
|
||||
if err is not None:
|
||||
raise Exception(err)
|
||||
|
||||
# wait some time so that we receive the new API key in the response
|
||||
time.sleep(10)
|
||||
@@ -69,14 +86,7 @@ async def main():
|
||||
if err is not None:
|
||||
raise Exception(err)
|
||||
|
||||
print(
|
||||
f"""
|
||||
BASE_URL = '{BASE_URL}'
|
||||
API_KEY_PRIVATE_KEY = '{private_key}'
|
||||
ACCOUNT_INDEX = {account_index}
|
||||
API_KEY_INDEX = {API_KEY_INDEX}
|
||||
"""
|
||||
)
|
||||
save_api_key_config(BASE_URL, account_index, private_keys)
|
||||
|
||||
await tx_client.close()
|
||||
await api_client.close()
|
||||
|
||||
Reference in New Issue
Block a user