Setup
This page discusses how to set up DriftPy to work with your Drift account. The steps in this page are necessary before taking any other actions to interact with the Drift Protocol.
Creating a Drift Client
The first step to using DriftPy is setting up the Drift Client. The Drift Client serves as an entrypoint to the Drift Protocol, and can be used for many different functions, such as placing orders, settling PnL, and creating user accounts.
The Drift Client has multiple different types of Subscriptions. Subscriptions are the method by which you intend to receive data from the blockchain for use in your program. For example, if you want to receive data via WebSocket, you would specify AccountSubscriptionConfig("websocket")
in your Drift Client constructor. The default configuration is websocket.
Here's an full example of how to create a Drift Client with a .env
file holding your PRIVATE_KEY
and your RPC_URL
. Note: this example requires the python-dotenv
library, which can be installed with pip install
and poetry add
.
Note the await drift_client.subscribe()
call on the last line of this excerpt. A common pitfall is for users to fail to subscribe to the Drift Client prior to requesting data, which will always cause an error. If you are seeing errors that look something like this:
return self.cache["spot_markets"][market_index] IndexError: list index out of range
You probably have not subscribed to the Drift Client yet.
Also note the await drift_client.add_user(0)
call preceding the subscription. This says, "Register sub-account 0 (the default sub-account) as a usuable Drift User account for this instance of the Drift Client". This function call is necessary for loading the corresponding DriftUser
object and its data. We'll go over the DriftUser
more later, but for now, assume that that line should always be there.
Creating a Drift Account with DriftPy
That add_user(0)
function call in the prior code block assumes that a Drift account has already been created for the Keypair
with which you created your DriftClient
, either through the UI, the TypeScript SDK, or previously through DriftPy. However, if that is not the case, you'll have to go through the steps of creating a Drift account for that Keypair
.
Here's how you can do that in DriftPy. Note: This code does not require subscription to the Drift Client.
Once you've set up your Drift account, registered it with the Drift Client, and subscribed to the Drift Client, you're ready to start interacting with the Drift Protocol.
Drift Client Optional Parameters
There are several optional parameters for the Drift Client. Here's the whole constructor:
For most use cases, you should be able to use the example code provided above for setting up your Drift Client. However, there may be times when you need some sort of customization. Here's what those optional parameters mean (starting at opts
):
opts
- ATxOpts
object used to specify some parameters for sending transactions from the Drift Client. Defaults toDEFAULT_TX_OPTIONS
. This does not include priority fee parameters.authority
- If you are trading under a delegate, thisPubkey
corresponds to the actual authority of the Drift account. (i.e. the vault pubkey if you're trading as a vault delegate)account_subscription
- TheAccountSubscriptionConfig
you intend to use to receive data from Drift. Defaults toAccountSubscriptionConfig.default()
which is"websocket"
.perp_market_indexes
- A list ofint
s representing the perp markets you intend to subscribe to. Only provide this if you intend to listen to a subset of perp markets and not all perp markets.spot_market_indexes
-A list ofint
s representing the spot markets you intend to subscribe to. Only provide this if you intend to listen to a subset of spot markets and not all spot markets. If you provide anyperp_market_indexes
, you must provide[0]
here, as that is the quote (USDC) spot market index for all pairs on Drift.oracle_infos
- A list ofOracleInfos
corresponding to theperp_market_indexes
andspot_market_indexes
provided. Do not provide this if you did not specifyperp_market_indexes
orspot_market_indexes
.tx_params
- ATxParams
object used to specify priority fee values for sending transactions. Includescompute_units
andcompute_units_price
.tx_version
- EitherLegacy
or anint
specifying the type of transaction you intend to send from the Drift Client.tx_sender
- AStandardTxSender
orFastTxSender
object representing how you'd like to send transactions from the Drift Client. Defaults toStandardTxSender
.active_sub_account_id
- Anint
representing thesub_account_id
that you intend to begin using the Drift Client undersub_account_ids
- A list ofint
s representing allsub_account_id
s that you intend to use under theDriftClient
market_lookup_table
- APubkey
corresponding to a custom address lookup table, if you intend to use one. If you do not know what that means, you should not provide any value to this argument.
Note: the sub_account_id
arguments do not need to be provided to use the Drift Client. You can follow the steps to adding a user described in the code examples above, but adding sub_account_id
s will force the Drift Client to add those users on DriftClient.subscribe()
.
Last updated