@drift-labs/sdk 2.100.0-beta.2 → 2.100.0-beta.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/VERSION CHANGED
@@ -1 +1 @@
1
- 2.100.0-beta.2
1
+ 2.100.0-beta.3
@@ -1,17 +1,17 @@
1
1
  import { MemcmpFilter } from '@solana/web3.js';
2
- import { BulkAccountLoader } from '../accounts/bulkAccountLoader';
3
2
  import { DriftClient } from '../driftClient';
4
3
  import { ReferrerInfo } from '../types';
5
4
  export declare class ReferrerMap {
6
5
  /**
7
- * map from authority pubkey to ReferrerInfo.
8
- * - if a user has not been entered into the map, the value is undefined
9
- * - if a user has no referrer, the value is null
10
- * - if a user has a referrer, the value is a ReferrerInfo object
6
+ * map from authority pubkey to referrer pubkey.
11
7
  */
12
- private referrerMap;
8
+ private authorityReferrerMap;
9
+ /**
10
+ * map from referrer pubkey to ReferrerInfo.
11
+ * Will be undefined if the referrer is not in the map yet.
12
+ */
13
+ private referrerReferrerInfoMap;
13
14
  private driftClient;
14
- private bulkAccountLoader;
15
15
  private parallelSync;
16
16
  private fetchPromise?;
17
17
  private fetchPromiseResolver;
@@ -19,16 +19,15 @@ export declare class ReferrerMap {
19
19
  * Creates a new UserStatsMap instance.
20
20
  *
21
21
  * @param {DriftClient} driftClient - The DriftClient instance.
22
- * @param {BulkAccountLoader} [bulkAccountLoader] - If not provided, a new BulkAccountLoader with polling disabled will be created.
23
22
  */
24
- constructor(driftClient: DriftClient, bulkAccountLoader?: BulkAccountLoader, parallelSync?: boolean);
23
+ constructor(driftClient: DriftClient, parallelSync?: boolean);
25
24
  /**
26
25
  * Subscribe to all UserStats accounts.
27
26
  */
28
27
  subscribe(): Promise<void>;
29
28
  has(authorityPublicKey: string): boolean;
30
29
  get(authorityPublicKey: string): ReferrerInfo | undefined;
31
- addReferrerInfo(authority: string, referrerInfo?: ReferrerInfo | null): Promise<void>;
30
+ addReferrer(authority: string, referrer?: string): Promise<void>;
32
31
  /**
33
32
  * Enforce that a UserStats will exist for the given authorityPublicKey,
34
33
  * reading one from the blockchain if necessary.
@@ -36,8 +35,9 @@ export declare class ReferrerMap {
36
35
  * @returns
37
36
  */
38
37
  mustGet(authorityPublicKey: string): Promise<ReferrerInfo | undefined>;
39
- values(): IterableIterator<ReferrerInfo | null>;
38
+ getReferrer(authorityPublicKey: string): ReferrerInfo | undefined;
40
39
  size(): number;
40
+ numberOfReferred(): number;
41
41
  sync(): Promise<void>;
42
42
  syncAll(): Promise<void>;
43
43
  syncReferrer(referrerFilter: MemcmpFilter): Promise<void>;
@@ -2,7 +2,6 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ReferrerMap = void 0;
4
4
  const web3_js_1 = require("@solana/web3.js");
5
- const bulkAccountLoader_1 = require("../accounts/bulkAccountLoader");
6
5
  const pda_1 = require("../addresses/pda");
7
6
  const memcmp_1 = require("../memcmp");
8
7
  const bytes_1 = require("@coral-xyz/anchor/dist/cjs/utils/bytes");
@@ -12,21 +11,18 @@ class ReferrerMap {
12
11
  * Creates a new UserStatsMap instance.
13
12
  *
14
13
  * @param {DriftClient} driftClient - The DriftClient instance.
15
- * @param {BulkAccountLoader} [bulkAccountLoader] - If not provided, a new BulkAccountLoader with polling disabled will be created.
16
14
  */
17
- constructor(driftClient, bulkAccountLoader, parallelSync) {
15
+ constructor(driftClient, parallelSync) {
18
16
  /**
19
- * map from authority pubkey to ReferrerInfo.
20
- * - if a user has not been entered into the map, the value is undefined
21
- * - if a user has no referrer, the value is null
22
- * - if a user has a referrer, the value is a ReferrerInfo object
17
+ * map from authority pubkey to referrer pubkey.
23
18
  */
24
- this.referrerMap = new Map();
19
+ this.authorityReferrerMap = new Map();
20
+ /**
21
+ * map from referrer pubkey to ReferrerInfo.
22
+ * Will be undefined if the referrer is not in the map yet.
23
+ */
24
+ this.referrerReferrerInfoMap = new Map();
25
25
  this.driftClient = driftClient;
26
- if (!bulkAccountLoader) {
27
- bulkAccountLoader = new bulkAccountLoader_1.BulkAccountLoader(driftClient.connection, driftClient.opts.commitment, 0);
28
- }
29
- this.bulkAccountLoader = bulkAccountLoader;
30
26
  this.parallelSync = parallelSync !== undefined ? parallelSync : true;
31
27
  }
32
28
  /**
@@ -40,27 +36,20 @@ class ReferrerMap {
40
36
  await this.sync();
41
37
  }
42
38
  has(authorityPublicKey) {
43
- return this.referrerMap.has(authorityPublicKey);
39
+ return this.authorityReferrerMap.has(authorityPublicKey);
44
40
  }
45
41
  get(authorityPublicKey) {
46
- const info = this.referrerMap.get(authorityPublicKey);
47
- return info === null ? undefined : info;
42
+ return this.getReferrer(authorityPublicKey);
48
43
  }
49
- async addReferrerInfo(authority, referrerInfo) {
50
- if (referrerInfo || referrerInfo === null) {
51
- this.referrerMap.set(authority, referrerInfo);
44
+ async addReferrer(authority, referrer) {
45
+ if (referrer) {
46
+ this.authorityReferrerMap.set(authority, referrer);
52
47
  }
53
- else if (referrerInfo === undefined) {
48
+ else if (referrer === undefined) {
54
49
  const userStatsAccountPublicKey = (0, pda_1.getUserStatsAccountPublicKey)(this.driftClient.program.programId, new web3_js_1.PublicKey(authority));
55
50
  const buffer = (await this.driftClient.connection.getAccountInfo(userStatsAccountPublicKey, 'processed')).data;
56
51
  const referrer = bytes_1.bs58.encode(buffer.subarray(40, 72));
57
- const referrerKey = new web3_js_1.PublicKey(referrer);
58
- this.addReferrerInfo(authority, referrer === DEFAULT_PUBLIC_KEY
59
- ? null
60
- : {
61
- referrer: (0, pda_1.getUserAccountPublicKeySync)(this.driftClient.program.programId, referrerKey, 0),
62
- referrerStats: (0, pda_1.getUserStatsAccountPublicKey)(this.driftClient.program.programId, referrerKey),
63
- });
52
+ this.addReferrer(authority, referrer);
64
53
  }
65
54
  }
66
55
  /**
@@ -71,15 +60,35 @@ class ReferrerMap {
71
60
  */
72
61
  async mustGet(authorityPublicKey) {
73
62
  if (!this.has(authorityPublicKey)) {
74
- await this.addReferrerInfo(authorityPublicKey);
63
+ await this.addReferrer(authorityPublicKey);
75
64
  }
76
- return this.get(authorityPublicKey);
65
+ return this.getReferrer(authorityPublicKey);
77
66
  }
78
- values() {
79
- return this.referrerMap.values();
67
+ getReferrer(authorityPublicKey) {
68
+ const referrer = this.authorityReferrerMap.get(authorityPublicKey);
69
+ if (!referrer) {
70
+ // return undefined if the referrer is not in the map
71
+ return undefined;
72
+ }
73
+ if (referrer === DEFAULT_PUBLIC_KEY) {
74
+ return undefined;
75
+ }
76
+ if (this.referrerReferrerInfoMap.has(referrer)) {
77
+ return this.referrerReferrerInfoMap.get(referrer);
78
+ }
79
+ const referrerKey = new web3_js_1.PublicKey(referrer);
80
+ const referrerInfo = {
81
+ referrer: (0, pda_1.getUserAccountPublicKeySync)(this.driftClient.program.programId, referrerKey, 0),
82
+ referrerStats: (0, pda_1.getUserStatsAccountPublicKey)(this.driftClient.program.programId, referrerKey),
83
+ };
84
+ this.referrerReferrerInfoMap.set(referrer, referrerInfo);
85
+ return referrerInfo;
80
86
  }
81
87
  size() {
82
- return this.referrerMap.size;
88
+ return this.authorityReferrerMap.size;
89
+ }
90
+ numberOfReferred() {
91
+ return Array.from(this.authorityReferrerMap.values()).filter((referrer) => referrer !== DEFAULT_PUBLIC_KEY).length;
83
92
  }
84
93
  async sync() {
85
94
  if (this.fetchPromise) {
@@ -132,7 +141,7 @@ class ReferrerMap {
132
141
  // only add if it isn't already in the map
133
142
  // so that if syncReferrer already set it, we dont overwrite
134
143
  if (!this.has(account.pubkey)) {
135
- this.addReferrerInfo(account.pubkey, null);
144
+ this.addReferrer(account.pubkey, DEFAULT_PUBLIC_KEY);
136
145
  }
137
146
  }
138
147
  }
@@ -162,19 +171,14 @@ class ReferrerMap {
162
171
  const buffer = Buffer.from(programAccount.account.data[0], programAccount.account.data[1]);
163
172
  const authority = bytes_1.bs58.encode(buffer.subarray(8, 40));
164
173
  const referrer = bytes_1.bs58.encode(buffer.subarray(40, 72));
165
- const referrerKey = new web3_js_1.PublicKey(referrer);
166
- this.addReferrerInfo(authority, referrer === DEFAULT_PUBLIC_KEY
167
- ? null
168
- : {
169
- referrer: (0, pda_1.getUserAccountPublicKeySync)(this.driftClient.program.programId, referrerKey, 0),
170
- referrerStats: (0, pda_1.getUserStatsAccountPublicKey)(this.driftClient.program.programId, referrerKey),
171
- });
174
+ this.addReferrer(authority, referrer);
172
175
  }));
173
176
  await new Promise((resolve) => setTimeout(resolve, 0));
174
177
  }
175
178
  }
176
179
  async unsubscribe() {
177
- this.referrerMap.clear();
180
+ this.authorityReferrerMap.clear();
181
+ this.referrerReferrerInfoMap.clear();
178
182
  }
179
183
  }
180
184
  exports.ReferrerMap = ReferrerMap;
@@ -1,17 +1,17 @@
1
1
  import { MemcmpFilter } from '@solana/web3.js';
2
- import { BulkAccountLoader } from '../accounts/bulkAccountLoader';
3
2
  import { DriftClient } from '../driftClient';
4
3
  import { ReferrerInfo } from '../types';
5
4
  export declare class ReferrerMap {
6
5
  /**
7
- * map from authority pubkey to ReferrerInfo.
8
- * - if a user has not been entered into the map, the value is undefined
9
- * - if a user has no referrer, the value is null
10
- * - if a user has a referrer, the value is a ReferrerInfo object
6
+ * map from authority pubkey to referrer pubkey.
11
7
  */
12
- private referrerMap;
8
+ private authorityReferrerMap;
9
+ /**
10
+ * map from referrer pubkey to ReferrerInfo.
11
+ * Will be undefined if the referrer is not in the map yet.
12
+ */
13
+ private referrerReferrerInfoMap;
13
14
  private driftClient;
14
- private bulkAccountLoader;
15
15
  private parallelSync;
16
16
  private fetchPromise?;
17
17
  private fetchPromiseResolver;
@@ -19,16 +19,15 @@ export declare class ReferrerMap {
19
19
  * Creates a new UserStatsMap instance.
20
20
  *
21
21
  * @param {DriftClient} driftClient - The DriftClient instance.
22
- * @param {BulkAccountLoader} [bulkAccountLoader] - If not provided, a new BulkAccountLoader with polling disabled will be created.
23
22
  */
24
- constructor(driftClient: DriftClient, bulkAccountLoader?: BulkAccountLoader, parallelSync?: boolean);
23
+ constructor(driftClient: DriftClient, parallelSync?: boolean);
25
24
  /**
26
25
  * Subscribe to all UserStats accounts.
27
26
  */
28
27
  subscribe(): Promise<void>;
29
28
  has(authorityPublicKey: string): boolean;
30
29
  get(authorityPublicKey: string): ReferrerInfo | undefined;
31
- addReferrerInfo(authority: string, referrerInfo?: ReferrerInfo | null): Promise<void>;
30
+ addReferrer(authority: string, referrer?: string): Promise<void>;
32
31
  /**
33
32
  * Enforce that a UserStats will exist for the given authorityPublicKey,
34
33
  * reading one from the blockchain if necessary.
@@ -36,8 +35,9 @@ export declare class ReferrerMap {
36
35
  * @returns
37
36
  */
38
37
  mustGet(authorityPublicKey: string): Promise<ReferrerInfo | undefined>;
39
- values(): IterableIterator<ReferrerInfo | null>;
38
+ getReferrer(authorityPublicKey: string): ReferrerInfo | undefined;
40
39
  size(): number;
40
+ numberOfReferred(): number;
41
41
  sync(): Promise<void>;
42
42
  syncAll(): Promise<void>;
43
43
  syncReferrer(referrerFilter: MemcmpFilter): Promise<void>;
@@ -2,7 +2,6 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ReferrerMap = void 0;
4
4
  const web3_js_1 = require("@solana/web3.js");
5
- const bulkAccountLoader_1 = require("../accounts/bulkAccountLoader");
6
5
  const pda_1 = require("../addresses/pda");
7
6
  const memcmp_1 = require("../memcmp");
8
7
  const bytes_1 = require("@coral-xyz/anchor/dist/cjs/utils/bytes");
@@ -12,21 +11,18 @@ class ReferrerMap {
12
11
  * Creates a new UserStatsMap instance.
13
12
  *
14
13
  * @param {DriftClient} driftClient - The DriftClient instance.
15
- * @param {BulkAccountLoader} [bulkAccountLoader] - If not provided, a new BulkAccountLoader with polling disabled will be created.
16
14
  */
17
- constructor(driftClient, bulkAccountLoader, parallelSync) {
15
+ constructor(driftClient, parallelSync) {
18
16
  /**
19
- * map from authority pubkey to ReferrerInfo.
20
- * - if a user has not been entered into the map, the value is undefined
21
- * - if a user has no referrer, the value is null
22
- * - if a user has a referrer, the value is a ReferrerInfo object
17
+ * map from authority pubkey to referrer pubkey.
23
18
  */
24
- this.referrerMap = new Map();
19
+ this.authorityReferrerMap = new Map();
20
+ /**
21
+ * map from referrer pubkey to ReferrerInfo.
22
+ * Will be undefined if the referrer is not in the map yet.
23
+ */
24
+ this.referrerReferrerInfoMap = new Map();
25
25
  this.driftClient = driftClient;
26
- if (!bulkAccountLoader) {
27
- bulkAccountLoader = new bulkAccountLoader_1.BulkAccountLoader(driftClient.connection, driftClient.opts.commitment, 0);
28
- }
29
- this.bulkAccountLoader = bulkAccountLoader;
30
26
  this.parallelSync = parallelSync !== undefined ? parallelSync : true;
31
27
  }
32
28
  /**
@@ -40,27 +36,20 @@ class ReferrerMap {
40
36
  await this.sync();
41
37
  }
42
38
  has(authorityPublicKey) {
43
- return this.referrerMap.has(authorityPublicKey);
39
+ return this.authorityReferrerMap.has(authorityPublicKey);
44
40
  }
45
41
  get(authorityPublicKey) {
46
- const info = this.referrerMap.get(authorityPublicKey);
47
- return info === null ? undefined : info;
42
+ return this.getReferrer(authorityPublicKey);
48
43
  }
49
- async addReferrerInfo(authority, referrerInfo) {
50
- if (referrerInfo || referrerInfo === null) {
51
- this.referrerMap.set(authority, referrerInfo);
44
+ async addReferrer(authority, referrer) {
45
+ if (referrer) {
46
+ this.authorityReferrerMap.set(authority, referrer);
52
47
  }
53
- else if (referrerInfo === undefined) {
48
+ else if (referrer === undefined) {
54
49
  const userStatsAccountPublicKey = (0, pda_1.getUserStatsAccountPublicKey)(this.driftClient.program.programId, new web3_js_1.PublicKey(authority));
55
50
  const buffer = (await this.driftClient.connection.getAccountInfo(userStatsAccountPublicKey, 'processed')).data;
56
51
  const referrer = bytes_1.bs58.encode(buffer.subarray(40, 72));
57
- const referrerKey = new web3_js_1.PublicKey(referrer);
58
- this.addReferrerInfo(authority, referrer === DEFAULT_PUBLIC_KEY
59
- ? null
60
- : {
61
- referrer: (0, pda_1.getUserAccountPublicKeySync)(this.driftClient.program.programId, referrerKey, 0),
62
- referrerStats: (0, pda_1.getUserStatsAccountPublicKey)(this.driftClient.program.programId, referrerKey),
63
- });
52
+ this.addReferrer(authority, referrer);
64
53
  }
65
54
  }
66
55
  /**
@@ -71,15 +60,35 @@ class ReferrerMap {
71
60
  */
72
61
  async mustGet(authorityPublicKey) {
73
62
  if (!this.has(authorityPublicKey)) {
74
- await this.addReferrerInfo(authorityPublicKey);
63
+ await this.addReferrer(authorityPublicKey);
75
64
  }
76
- return this.get(authorityPublicKey);
65
+ return this.getReferrer(authorityPublicKey);
77
66
  }
78
- values() {
79
- return this.referrerMap.values();
67
+ getReferrer(authorityPublicKey) {
68
+ const referrer = this.authorityReferrerMap.get(authorityPublicKey);
69
+ if (!referrer) {
70
+ // return undefined if the referrer is not in the map
71
+ return undefined;
72
+ }
73
+ if (referrer === DEFAULT_PUBLIC_KEY) {
74
+ return undefined;
75
+ }
76
+ if (this.referrerReferrerInfoMap.has(referrer)) {
77
+ return this.referrerReferrerInfoMap.get(referrer);
78
+ }
79
+ const referrerKey = new web3_js_1.PublicKey(referrer);
80
+ const referrerInfo = {
81
+ referrer: (0, pda_1.getUserAccountPublicKeySync)(this.driftClient.program.programId, referrerKey, 0),
82
+ referrerStats: (0, pda_1.getUserStatsAccountPublicKey)(this.driftClient.program.programId, referrerKey),
83
+ };
84
+ this.referrerReferrerInfoMap.set(referrer, referrerInfo);
85
+ return referrerInfo;
80
86
  }
81
87
  size() {
82
- return this.referrerMap.size;
88
+ return this.authorityReferrerMap.size;
89
+ }
90
+ numberOfReferred() {
91
+ return Array.from(this.authorityReferrerMap.values()).filter((referrer) => referrer !== DEFAULT_PUBLIC_KEY).length;
83
92
  }
84
93
  async sync() {
85
94
  if (this.fetchPromise) {
@@ -132,7 +141,7 @@ class ReferrerMap {
132
141
  // only add if it isn't already in the map
133
142
  // so that if syncReferrer already set it, we dont overwrite
134
143
  if (!this.has(account.pubkey)) {
135
- this.addReferrerInfo(account.pubkey, null);
144
+ this.addReferrer(account.pubkey, DEFAULT_PUBLIC_KEY);
136
145
  }
137
146
  }
138
147
  }
@@ -162,19 +171,14 @@ class ReferrerMap {
162
171
  const buffer = Buffer.from(programAccount.account.data[0], programAccount.account.data[1]);
163
172
  const authority = bytes_1.bs58.encode(buffer.subarray(8, 40));
164
173
  const referrer = bytes_1.bs58.encode(buffer.subarray(40, 72));
165
- const referrerKey = new web3_js_1.PublicKey(referrer);
166
- this.addReferrerInfo(authority, referrer === DEFAULT_PUBLIC_KEY
167
- ? null
168
- : {
169
- referrer: (0, pda_1.getUserAccountPublicKeySync)(this.driftClient.program.programId, referrerKey, 0),
170
- referrerStats: (0, pda_1.getUserStatsAccountPublicKey)(this.driftClient.program.programId, referrerKey),
171
- });
174
+ this.addReferrer(authority, referrer);
172
175
  }));
173
176
  await new Promise((resolve) => setTimeout(resolve, 0));
174
177
  }
175
178
  }
176
179
  async unsubscribe() {
177
- this.referrerMap.clear();
180
+ this.authorityReferrerMap.clear();
181
+ this.referrerReferrerInfoMap.clear();
178
182
  }
179
183
  }
180
184
  exports.ReferrerMap = ReferrerMap;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "2.100.0-beta.2",
3
+ "version": "2.100.0-beta.3",
4
4
  "main": "lib/node/index.js",
5
5
  "types": "lib/node/index.d.ts",
6
6
  "browser": "./lib/browser/index.js",
@@ -3,7 +3,6 @@ import {
3
3
  PublicKey,
4
4
  RpcResponseAndContext,
5
5
  } from '@solana/web3.js';
6
- import { BulkAccountLoader } from '../accounts/bulkAccountLoader';
7
6
  import { DriftClient } from '../driftClient';
8
7
  import { ReferrerInfo } from '../types';
9
8
  import {
@@ -21,14 +20,15 @@ const DEFAULT_PUBLIC_KEY = PublicKey.default.toBase58();
21
20
 
22
21
  export class ReferrerMap {
23
22
  /**
24
- * map from authority pubkey to ReferrerInfo.
25
- * - if a user has not been entered into the map, the value is undefined
26
- * - if a user has no referrer, the value is null
27
- * - if a user has a referrer, the value is a ReferrerInfo object
23
+ * map from authority pubkey to referrer pubkey.
28
24
  */
29
- private referrerMap = new Map<string, ReferrerInfo | null>();
25
+ private authorityReferrerMap = new Map<string, string>();
26
+ /**
27
+ * map from referrer pubkey to ReferrerInfo.
28
+ * Will be undefined if the referrer is not in the map yet.
29
+ */
30
+ private referrerReferrerInfoMap = new Map<string, ReferrerInfo>();
30
31
  private driftClient: DriftClient;
31
- private bulkAccountLoader: BulkAccountLoader;
32
32
  private parallelSync: boolean;
33
33
 
34
34
  private fetchPromise?: Promise<void>;
@@ -38,22 +38,9 @@ export class ReferrerMap {
38
38
  * Creates a new UserStatsMap instance.
39
39
  *
40
40
  * @param {DriftClient} driftClient - The DriftClient instance.
41
- * @param {BulkAccountLoader} [bulkAccountLoader] - If not provided, a new BulkAccountLoader with polling disabled will be created.
42
41
  */
43
- constructor(
44
- driftClient: DriftClient,
45
- bulkAccountLoader?: BulkAccountLoader,
46
- parallelSync?: boolean
47
- ) {
42
+ constructor(driftClient: DriftClient, parallelSync?: boolean) {
48
43
  this.driftClient = driftClient;
49
- if (!bulkAccountLoader) {
50
- bulkAccountLoader = new BulkAccountLoader(
51
- driftClient.connection,
52
- driftClient.opts.commitment,
53
- 0
54
- );
55
- }
56
- this.bulkAccountLoader = bulkAccountLoader;
57
44
  this.parallelSync = parallelSync !== undefined ? parallelSync : true;
58
45
  }
59
46
 
@@ -70,21 +57,17 @@ export class ReferrerMap {
70
57
  }
71
58
 
72
59
  public has(authorityPublicKey: string): boolean {
73
- return this.referrerMap.has(authorityPublicKey);
60
+ return this.authorityReferrerMap.has(authorityPublicKey);
74
61
  }
75
62
 
76
63
  public get(authorityPublicKey: string): ReferrerInfo | undefined {
77
- const info = this.referrerMap.get(authorityPublicKey);
78
- return info === null ? undefined : info;
64
+ return this.getReferrer(authorityPublicKey);
79
65
  }
80
66
 
81
- public async addReferrerInfo(
82
- authority: string,
83
- referrerInfo?: ReferrerInfo | null
84
- ) {
85
- if (referrerInfo || referrerInfo === null) {
86
- this.referrerMap.set(authority, referrerInfo);
87
- } else if (referrerInfo === undefined) {
67
+ public async addReferrer(authority: string, referrer?: string) {
68
+ if (referrer) {
69
+ this.authorityReferrerMap.set(authority, referrer);
70
+ } else if (referrer === undefined) {
88
71
  const userStatsAccountPublicKey = getUserStatsAccountPublicKey(
89
72
  this.driftClient.program.programId,
90
73
  new PublicKey(authority)
@@ -98,23 +81,7 @@ export class ReferrerMap {
98
81
 
99
82
  const referrer = bs58.encode(buffer.subarray(40, 72));
100
83
 
101
- const referrerKey = new PublicKey(referrer);
102
- this.addReferrerInfo(
103
- authority,
104
- referrer === DEFAULT_PUBLIC_KEY
105
- ? null
106
- : {
107
- referrer: getUserAccountPublicKeySync(
108
- this.driftClient.program.programId,
109
- referrerKey,
110
- 0
111
- ),
112
- referrerStats: getUserStatsAccountPublicKey(
113
- this.driftClient.program.programId,
114
- referrerKey
115
- ),
116
- }
117
- );
84
+ this.addReferrer(authority, referrer);
118
85
  }
119
86
  }
120
87
 
@@ -128,17 +95,51 @@ export class ReferrerMap {
128
95
  authorityPublicKey: string
129
96
  ): Promise<ReferrerInfo | undefined> {
130
97
  if (!this.has(authorityPublicKey)) {
131
- await this.addReferrerInfo(authorityPublicKey);
98
+ await this.addReferrer(authorityPublicKey);
132
99
  }
133
- return this.get(authorityPublicKey);
100
+ return this.getReferrer(authorityPublicKey);
134
101
  }
135
102
 
136
- public values(): IterableIterator<ReferrerInfo | null> {
137
- return this.referrerMap.values();
103
+ public getReferrer(authorityPublicKey: string): ReferrerInfo | undefined {
104
+ const referrer = this.authorityReferrerMap.get(authorityPublicKey);
105
+ if (!referrer) {
106
+ // return undefined if the referrer is not in the map
107
+ return undefined;
108
+ }
109
+
110
+ if (referrer === DEFAULT_PUBLIC_KEY) {
111
+ return undefined;
112
+ }
113
+
114
+ if (this.referrerReferrerInfoMap.has(referrer)) {
115
+ return this.referrerReferrerInfoMap.get(referrer);
116
+ }
117
+
118
+ const referrerKey = new PublicKey(referrer);
119
+ const referrerInfo = {
120
+ referrer: getUserAccountPublicKeySync(
121
+ this.driftClient.program.programId,
122
+ referrerKey,
123
+ 0
124
+ ),
125
+ referrerStats: getUserStatsAccountPublicKey(
126
+ this.driftClient.program.programId,
127
+ referrerKey
128
+ ),
129
+ };
130
+
131
+ this.referrerReferrerInfoMap.set(referrer, referrerInfo);
132
+ return referrerInfo;
138
133
  }
139
134
 
140
135
  public size(): number {
141
- return this.referrerMap.size;
136
+ return this.authorityReferrerMap.size;
137
+ }
138
+
139
+ public numberOfReferred(): number {
140
+ return Array.from(this.authorityReferrerMap.values()).filter(
141
+ (referrer) => referrer !== DEFAULT_PUBLIC_KEY
142
+ ).length;
142
143
  }
143
144
 
144
145
  public async sync(): Promise<void> {
@@ -205,7 +206,7 @@ export class ReferrerMap {
205
206
  // only add if it isn't already in the map
206
207
  // so that if syncReferrer already set it, we dont overwrite
207
208
  if (!this.has(account.pubkey)) {
208
- this.addReferrerInfo(account.pubkey, null);
209
+ this.addReferrer(account.pubkey, DEFAULT_PUBLIC_KEY);
209
210
  }
210
211
  }
211
212
  }
@@ -254,23 +255,7 @@ export class ReferrerMap {
254
255
  const authority = bs58.encode(buffer.subarray(8, 40));
255
256
  const referrer = bs58.encode(buffer.subarray(40, 72));
256
257
 
257
- const referrerKey = new PublicKey(referrer);
258
- this.addReferrerInfo(
259
- authority,
260
- referrer === DEFAULT_PUBLIC_KEY
261
- ? null
262
- : {
263
- referrer: getUserAccountPublicKeySync(
264
- this.driftClient.program.programId,
265
- referrerKey,
266
- 0
267
- ),
268
- referrerStats: getUserStatsAccountPublicKey(
269
- this.driftClient.program.programId,
270
- referrerKey
271
- ),
272
- }
273
- );
258
+ this.addReferrer(authority, referrer);
274
259
  })
275
260
  );
276
261
  await new Promise((resolve) => setTimeout(resolve, 0));
@@ -278,6 +263,7 @@ export class ReferrerMap {
278
263
  }
279
264
 
280
265
  public async unsubscribe() {
281
- this.referrerMap.clear();
266
+ this.authorityReferrerMap.clear();
267
+ this.referrerReferrerInfoMap.clear();
282
268
  }
283
269
  }