-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: user stats map * chore: remove unnecessary async * fix: default factory for order params defaults
- Loading branch information
1 parent
7f24dea
commit 45cd32e
Showing
10 changed files
with
372 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
from .drift_client import * | ||
from .user import * | ||
from .user_stats import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from typing import Optional | ||
|
||
from driftpy.accounts import DataAndSlot | ||
from driftpy.types import UserStatsAccount | ||
|
||
from driftpy.accounts.ws.account_subscriber import WebsocketAccountSubscriber | ||
from driftpy.accounts.types import UserStatsAccountSubscriber | ||
|
||
|
||
class WebsocketUserStatsAccountSubscriber( | ||
WebsocketAccountSubscriber[UserStatsAccount], UserStatsAccountSubscriber | ||
): | ||
def get_user_stats_account_and_slot( | ||
self, | ||
) -> Optional[DataAndSlot[UserStatsAccount]]: | ||
return self.data_and_slot |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
from dataclasses import dataclass | ||
from typing import Optional | ||
|
||
from solders.pubkey import Pubkey | ||
from solana.rpc.commitment import Commitment, Confirmed | ||
|
||
from driftpy.accounts.types import DataAndSlot | ||
from driftpy.types import ReferrerInfo, UserStatsAccount | ||
from driftpy.accounts.ws.user_stats import WebsocketUserStatsAccountSubscriber | ||
from driftpy.addresses import ( | ||
get_user_account_public_key, | ||
get_user_stats_account_public_key, | ||
) | ||
from driftpy.drift_client import DriftClient | ||
|
||
|
||
@dataclass | ||
class UserStatsSubscriptionConfig: | ||
commitment: Commitment = Confirmed | ||
resub_timeout_ms: Optional[int] = None | ||
initial_data: Optional[DataAndSlot[UserStatsAccount]] = None | ||
|
||
|
||
class DriftUserStats: | ||
def __init__( | ||
self, | ||
drift_client: DriftClient, | ||
user_stats_account_pubkey: Pubkey, | ||
config: UserStatsSubscriptionConfig, | ||
): | ||
self.drift_client = drift_client | ||
self.user_stats_account_pubkey = user_stats_account_pubkey | ||
self.account_subscriber = WebsocketUserStatsAccountSubscriber( | ||
user_stats_account_pubkey, | ||
drift_client.program, | ||
config.commitment, | ||
initial_data=config.initial_data, | ||
) | ||
self.subscribed = False | ||
|
||
async def subscribe(self) -> bool: | ||
if self.subscribed: | ||
return | ||
|
||
await self.account_subscriber.subscribe() | ||
self.subscribed = True | ||
|
||
return self.subscribed | ||
|
||
async def fetch_accounts(self): | ||
await self.account_subscriber.fetch() | ||
|
||
def unsubscribe(self): | ||
self.account_subscriber.unsubscribe() | ||
|
||
def get_account_and_slot(self) -> DataAndSlot[UserStatsAccount]: | ||
return self.account_subscriber.get_user_stats_account_and_slot() | ||
|
||
def get_account(self) -> UserStatsAccount: | ||
return self.account_subscriber.get_user_stats_account_and_slot().data | ||
|
||
def get_referrer_info(self) -> Optional[ReferrerInfo]: | ||
if self.get_account().referrer == Pubkey.default(): | ||
return None | ||
else: | ||
return ReferrerInfo( | ||
get_user_account_public_key( | ||
self.drift_client.program_id, self.get_account().referrer, 0 | ||
), | ||
get_user_stats_account_public_key( | ||
self.drift_client.program_id, self.get_account().referrer | ||
), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.