Skip to content

Commit

Permalink
add opts to drift client and commitment to account subscription config
Browse files Browse the repository at this point in the history
  • Loading branch information
crispheaney committed Nov 26, 2023
1 parent 4ac43e4 commit 1c1daa5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
21 changes: 17 additions & 4 deletions src/driftpy/account_subscription_config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Literal, Optional

from solders.pubkey import Pubkey
from solana.rpc.commitment import Commitment

from driftpy.accounts.bulk_account_loader import BulkAccountLoader
from driftpy.accounts.cache import (
Expand Down Expand Up @@ -28,25 +29,33 @@ def __init__(
self,
type: Literal["polling", "websocket", "cached"],
bulk_account_loader: Optional[BulkAccountLoader] = None,
commitment: Commitment = None,
):
self.type = type

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

if commitment is not None and commitment != bulk_account_loader.commitment:
raise ValueError(
f"bulk account loader commitment {bulk_account_loader.commitment} != commitment passed {commitment}"
)

self.bulk_account_loader = bulk_account_loader

self.commitment = commitment

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)
return WebsocketDriftClientAccountSubscriber(program, self.commitment)
case "cached":
return CachedDriftClientAccountSubscriber(program)
return CachedDriftClientAccountSubscriber(program, self.commitment)

def get_user_client_subscriber(self, program: Program, user_pubkey: Pubkey):
match self.type:
Expand All @@ -55,6 +64,10 @@ def get_user_client_subscriber(self, program: Program, user_pubkey: Pubkey):
user_pubkey, program, self.bulk_account_loader
)
case "websocket":
return WebsocketUserAccountSubscriber(user_pubkey, program)
return WebsocketUserAccountSubscriber(
user_pubkey, program, self.commitment
)
case "cached":
return CachedUserAccountSubscriber(user_pubkey, program)
return CachedUserAccountSubscriber(
user_pubkey, program, self.commitment
)
11 changes: 8 additions & 3 deletions src/driftpy/drift_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from solders.sysvar import RENT
from solders.address_lookup_table_account import AddressLookupTableAccount
from solana.rpc.async_api import AsyncClient
from solana.rpc.types import TxOpts
from solana.rpc.commitment import Processed
from solana.transaction import AccountMeta
from solders.compute_budget import set_compute_unit_limit, set_compute_unit_price
from spl.token.constants import TOKEN_PROGRAM_ID
Expand All @@ -26,13 +28,15 @@
from driftpy.sdk_types import *
from driftpy.accounts import *

from driftpy.constants.config import Config, DriftEnv, DRIFT_PROGRAM_ID, configs
from driftpy.constants.config import DriftEnv, DRIFT_PROGRAM_ID, configs

from typing import Union, Optional, List, Sequence
from typing import Union, Optional, List
from driftpy.math.positions import is_available, is_spot_position_available

DEFAULT_USER_NAME = "Main Account"

DEFAULT_TX_OPTIONS = TxOpts(skip_confirmation=False, preflight_commitment=Processed)


class DriftClient:
"""This class is the main way to interact with Drift Protocol including
Expand All @@ -45,6 +49,7 @@ def __init__(
wallet: Union[Keypair, Wallet],
env: DriftEnv = "mainnet",
program_id: Optional[Pubkey] = DRIFT_PROGRAM_ID,
opts: TxOpts = DEFAULT_TX_OPTIONS,
authority: Pubkey = None,
account_subscription: Optional[
AccountSubscriptionConfig
Expand All @@ -68,7 +73,7 @@ def __init__(
raw = file.read_text()
idl = Idl.from_json(raw)

provider = Provider(connection, wallet)
provider = Provider(connection, wallet, opts)
self.program_id = program_id
self.program = Program(
idl,
Expand Down

0 comments on commit 1c1daa5

Please sign in to comment.