-
Notifications
You must be signed in to change notification settings - Fork 81
/
userStats.ts
83 lines (75 loc) · 2.49 KB
/
userStats.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { DriftClient } from './driftClient';
import { PublicKey } from '@solana/web3.js';
import { DataAndSlot, UserStatsAccountSubscriber } from './accounts/types';
import { UserStatsConfig } from './userStatsConfig';
import { PollingUserStatsAccountSubscriber } from './accounts/pollingUserStatsAccountSubscriber';
import { WebSocketUserStatsAccountSubscriber } from './accounts/webSocketUserStatsAccountSubsriber';
import { ReferrerInfo, UserStatsAccount } from './types';
import {
getUserAccountPublicKeySync,
getUserStatsAccountPublicKey,
} from './addresses/pda';
export class UserStats {
driftClient: DriftClient;
userStatsAccountPublicKey: PublicKey;
accountSubscriber: UserStatsAccountSubscriber;
isSubscribed: boolean;
public constructor(config: UserStatsConfig) {
this.driftClient = config.driftClient;
this.userStatsAccountPublicKey = config.userStatsAccountPublicKey;
if (config.accountSubscription?.type === 'polling') {
this.accountSubscriber = new PollingUserStatsAccountSubscriber(
config.driftClient.program,
config.userStatsAccountPublicKey,
config.accountSubscription.accountLoader
);
} else if (config.accountSubscription?.type === 'custom') {
throw new Error(
'Custom account subscription not yet implemented for user stats'
);
} else {
this.accountSubscriber = new WebSocketUserStatsAccountSubscriber(
config.driftClient.program,
config.userStatsAccountPublicKey
);
}
}
public async subscribe(
userStatsAccount?: UserStatsAccount
): Promise<boolean> {
this.isSubscribed = await this.accountSubscriber.subscribe(
userStatsAccount
);
return this.isSubscribed;
}
public async fetchAccounts(): Promise<void> {
await this.accountSubscriber.fetch();
}
public async unsubscribe(): Promise<void> {
await this.accountSubscriber.unsubscribe();
this.isSubscribed = false;
}
public getAccountAndSlot(): DataAndSlot<UserStatsAccount> {
return this.accountSubscriber.getUserStatsAccountAndSlot();
}
public getAccount(): UserStatsAccount {
return this.accountSubscriber.getUserStatsAccountAndSlot().data;
}
public getReferrerInfo(): ReferrerInfo | undefined {
if (this.getAccount().referrer.equals(PublicKey.default)) {
return undefined;
} else {
return {
referrer: getUserAccountPublicKeySync(
this.driftClient.program.programId,
this.getAccount().referrer,
0
),
referrerStats: getUserStatsAccountPublicKey(
this.driftClient.program.programId,
this.getAccount().referrer
),
};
}
}
}