Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

start adding subaccount ids to drift client #43

Merged
merged 4 commits into from
Nov 26, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
60 changes: 60 additions & 0 deletions src/driftpy/account_subscription_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from typing import Literal, Optional

from solders.pubkey import Pubkey

from driftpy.accounts.bulk_account_loader import BulkAccountLoader
from driftpy.accounts.cache import (
CachedDriftClientAccountSubscriber,
CachedUserAccountSubscriber,
)
from driftpy.accounts.polling import (
PollingDriftClientAccountSubscriber,
PollingUserAccountSubscriber,
)
from anchorpy import Program

from driftpy.accounts.ws import (
WebsocketDriftClientAccountSubscriber,
WebsocketUserAccountSubscriber,
)


class AccountSubscriptionConfig:
@staticmethod
def default():
return AccountSubscriptionConfig("websocket")

def __init__(
self,
type: Literal["polling", "websocket", "cached"],
bulk_account_loader: Optional[BulkAccountLoader] = None,
):
self.type = type

if self.type == "polling":
if bulk_account_loader is None:
raise ValueError("polling subscription requires bulk account loader")

self.bulk_account_loader = bulk_account_loader

def get_drift_client_subscriber(self, program: Program):
match self.type:
case "polling":
return PollingDriftClientAccountSubscriber(
program, self.bulk_account_loader
)
case "websocket":
return WebsocketDriftClientAccountSubscriber(program)
case "cached":
return CachedDriftClientAccountSubscriber(program)

def get_user_client_subscriber(self, program: Program, user_pubkey: Pubkey):
match self.type:
case "polling":
return PollingUserAccountSubscriber(
user_pubkey, program, self.bulk_account_loader
)
case "websocket":
return WebsocketUserAccountSubscriber(user_pubkey, program)
case "cached":
return CachedUserAccountSubscriber(user_pubkey, program)
4 changes: 2 additions & 2 deletions src/driftpy/addresses.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ def get_user_stats_account_public_key(
def get_user_account_public_key(
program_id: Pubkey,
authority: Pubkey,
user_id=0,
sub_account_id=0,
) -> Pubkey:
return Pubkey.find_program_address(
[b"user", bytes(authority), int_to_le_bytes(user_id)], program_id
[b"user", bytes(authority), int_to_le_bytes(sub_account_id)], program_id
)[0]


Expand Down
Loading