Wallet Setup
To manage your accounts, issue orders, and perform other operations that are required to be signed, a Wallet is required. To instantiate a Wallet, you must first have your associated mnemonic.
Unification Plan
- The Python client requires the use of an address to setup the Wallet. However, the address can only be fetched using a Wallet. The address is derived from the mnemonic (address < public key < private key < mnemonic).
- Wallet, accounts, subaccounts are all handled differently among the clients. Probably the Rust client handles this best, giving the user more control:
- There is a
Wallet
; - The
Wallet
is used to derive anAccount
by index (eachAccount
is associated with a keypair); - An
Account
is used to derive aSubaccount
by index. ASubaccount
is employed to create orders.
- There is a
Getting the mnemonic
A Wallet is setup using your secret mnemonic phrase. A mnemonic is a set of 24 words to back up and access your account.
You can fetch your mnemonic from the dYdX Frontend. After logging in, follow the instructions in "Export secret phrase", accessed by clicking your address in the upper right corner.
For the purpose of this guide, lets copy and store the mnemonic in a mnemonic.txt
file.
Read the mnemonic
Lets start coding. Load the mnemonic into a string variable. This assumes the mnemonic is stored in a text file.
mnemonic = open('mnemonic.txt').read().strip()
Create the Wallet
Use the mnemonic to create a Wallet instance capable of signing transactions.
from dydx_v4_client.key_pair import KeyPair
from dydx_v4_client.wallet import Wallet
# Define your address.
address = Wallet(KeyPair.from_mnemonic(mnemonic), 0, 0).address()
# Create a Wallet with updated parameters required for trading
wallet = await Wallet.from_mnemonic(node, mnemonic, address)
Instantiate a Subaccount
When issuing orders, the relevant Subaccount must be chosen to place the order under. A Subaccount is associated with an Account, and is meant to provide trade isolation against your other Subaccounts and enhance funds management. See more about Accounts and Subaccounts.
# Not required. The `wallet` instance created above already contains the necessary information.
# The Subaccount to be used is defined using an integer when creating an order.