Skip to content

Instantly share code, notes, and snippets.

@jordy25519
Last active March 27, 2024 08:19
Show Gist options
  • Save jordy25519/e3c3b934d27944d3afd18be1cd27ad89 to your computer and use it in GitHub Desktop.
Save jordy25519/e3c3b934d27944d3afd18be1cd27ad89 to your computer and use it in GitHub Desktop.
driftpy query User accounts
"""
Shows how to query user account data using driftpy
python 3.10.13
"""
import asyncio
from anchorpy import Wallet
from driftpy.addresses import get_user_account_public_key
from driftpy.drift_client import DriftClient;
from driftpy.user_map.user_map import UserMap, UserMapConfig, PollingConfig;
from solders.keypair import Keypair
from solders.pubkey import Pubkey
from solana.rpc.async_api import AsyncClient
async def run():
connection = AsyncClient("https://api.mainnet-beta.solana.com")
client = DriftClient(
connection,
Wallet(Keypair()), # an empty wallet, not signing any txs
"mainnet",
)
user_map = UserMap(
UserMapConfig(
client,
PollingConfig(10),
skip_initial_load = False,
)
);
# these are the wallet public keys
user_pub_keys = [
"DxoRJ4f5XRMvXU9SGuM4ZziBFUxbhB3ubur5sVZEvue2",
"EBUVGBRzBq4aSWvRFS114oWngTVkjXmTMEznTr1KJ39C",
]
for authority_key in user_pub_keys:
# get position for user subaccount 0
sub_account_0: Pubkey = get_user_account_public_key(client.program_id, Pubkey.from_string(authority_key), 0)
await user_map.add_pubkey(sub_account_0)
user_data = await user_map.must_get(str(sub_account_0))
# USDC
print(str(sub_account_0), user_data.get_spot_position(0))
# SOL spot
print(str(sub_account_0), user_data.get_spot_position(1))
if __name__ == '__main__':
asyncio.run(run())
anchorpy==0.*
driftpy==0.7.43
solana==0.*
solders==0.*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment