Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 4 additions & 1 deletion examples/config.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
Fill in your secret key e.g. 0x0000000000000000000000000000000000000000000000000000000000000000
If you are using an Agent/API Wallet you MUST also specify the public address of your account, not the
address of the Agent/API Wallet.
Otherwise, feel free to leave it blank and it will be automatically derived from the secret key.
Otherwise, feel free to leave it blank and it will be automatically derived from the secret key. Alternatively,
you can specify a local `keystore_path` which will prompt you for the keystore password interactively.
Note: If both `secret_key` and `keystore_path` are provided, the keystore will take precedence.

You can also populate the "multi_sig" section with the secret keys of the authorized user wallets that you
wish to sign multi-sig actions for.
",
"keystore_path": "",
"secret_key": "",
"account_address": "",
"multi_sig": {
Expand Down
36 changes: 29 additions & 7 deletions examples/example_utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import getpass
import json
import os

Expand All @@ -12,13 +13,7 @@ def setup(base_url=None, skip_ws=False, perp_dexs=None):
config_path = os.path.join(os.path.dirname(__file__), "config.json")
with open(config_path) as f:
config = json.load(f)
account: LocalAccount = eth_account.Account.from_key(config["secret_key"])
address = config["account_address"]
if address == "":
address = account.address
print("Running with account address:", address)
if address != account.address:
print("Running with agent address:", account.address)
account, address = create_account(config)
info = Info(base_url, skip_ws, perp_dexs=perp_dexs)
user_state = info.user_state(address)
spot_user_state = info.spot_user_state(address)
Expand All @@ -32,6 +27,33 @@ def setup(base_url=None, skip_ws=False, perp_dexs=None):
return address, info, exchange


def create_account(config):
account: LocalAccount
if config["keystore_path"]:
keystore_path = config["keystore_path"]
keystore_path = os.path.expanduser(keystore_path)
if not os.path.isabs(keystore_path):
keystore_path = os.path.join(os.path.dirname(__file__), keystore_path)
if not os.path.exists(keystore_path):
raise FileNotFoundError(f"Keystore file not found: {keystore_path}")
if not os.path.isfile(keystore_path):
raise ValueError(f"Keystore path is not a file: {keystore_path}")
with open(keystore_path) as f:
keystore = json.load(f)
password = getpass.getpass("Enter keystore password: ")
private_key = eth_account.Account.decrypt(keystore, password)
account = eth_account.Account.from_key(private_key)
else:
account = eth_account.Account.from_key(config["secret_key"])
address = config["account_address"]
if address == "":
address = account.address
print("Running with account address:", address)
if address != account.address:
print("Running with agent address:", account.address)
return account, address


def setup_multi_sig_wallets():
config_path = os.path.join(os.path.dirname(__file__), "config.json")
with open(config_path) as f:
Expand Down