Skip to content

Commit 3e2eb0b

Browse files
committed
system setup for sub-accounts & pools
1 parent 6a40345 commit 3e2eb0b

File tree

1 file changed

+24
-17
lines changed

1 file changed

+24
-17
lines changed

examples/system_setup.py

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,39 @@
1414
API_KEY_INDEX = 3
1515
NUM_API_KEYS = 5
1616

17+
# If you set this to something other than None, the script will use that account index instead of using the master account index.
18+
# This is useful if you have multiple accounts on the same L1 address or are the owner of a public pool.
19+
# You need to use the private key associated to the master account or the owner of the public pool to change the API keys.
20+
ACCOUNT_INDEX = None
1721

1822
async def main():
1923
# verify that the account exists & fetch account index
2024
api_client = lighter.ApiClient(configuration=lighter.Configuration(host=BASE_URL))
2125
eth_acc = eth_account.Account.from_key(ETH_PRIVATE_KEY)
2226
eth_address = eth_acc.address
2327

24-
try:
25-
response = await lighter.AccountApi(api_client).accounts_by_l1_address(
26-
l1_address=eth_address
27-
)
28-
except lighter.ApiException as e:
29-
if e.data.message == "account not found":
30-
print(f"error: account not found for {eth_address}")
31-
return
28+
if ACCOUNT_INDEX is not None:
29+
account_index = ACCOUNT_INDEX
30+
else:
31+
try:
32+
response = await lighter.AccountApi(api_client).accounts_by_l1_address(l1_address=eth_address)
33+
except lighter.ApiException as e:
34+
if e.data.message == "account not found":
35+
print(f"error: account not found for {eth_address}")
36+
return
37+
else:
38+
raise e
39+
40+
if len(response.sub_accounts) > 1:
41+
for sub_account in response.sub_accounts:
42+
print(f"found accountIndex: {sub_account.index}")
43+
44+
account = min(response.sub_accounts, key=lambda x: int(x.index))
45+
account_index = account.index
46+
print(f"multiple accounts found, using the master account {account_index}")
3247
else:
33-
raise e
48+
account_index = response.sub_accounts[0].index
3449

35-
if len(response.sub_accounts) > 1:
36-
for sub_account in response.sub_accounts:
37-
print(f"found accountIndex: {sub_account.index}")
38-
39-
print("multiple accounts found, using the first one")
40-
account_index = response.sub_accounts[0].index
41-
else:
42-
account_index = response.sub_accounts[0].index
4350

4451
# create a private/public key pair for the new API key
4552
# pass any string to be used as seed for create_api_key like

0 commit comments

Comments
 (0)