@drift-labs/sdk 2.100.0-beta.2 → 2.100.0-beta.4
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.
|
|
1
|
+
2.100.0-beta.4
|
|
@@ -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
|
|
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
|
|
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,
|
|
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
|
-
|
|
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
|
-
|
|
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,
|
|
15
|
+
constructor(driftClient, parallelSync) {
|
|
18
16
|
/**
|
|
19
|
-
* map from authority pubkey to
|
|
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.
|
|
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.
|
|
39
|
+
return this.authorityReferrerMap.has(authorityPublicKey);
|
|
44
40
|
}
|
|
45
41
|
get(authorityPublicKey) {
|
|
46
|
-
|
|
47
|
-
return info === null ? undefined : info;
|
|
42
|
+
return this.getReferrer(authorityPublicKey);
|
|
48
43
|
}
|
|
49
|
-
async
|
|
50
|
-
if (
|
|
51
|
-
this.
|
|
44
|
+
async addReferrer(authority, referrer) {
|
|
45
|
+
if (referrer) {
|
|
46
|
+
this.authorityReferrerMap.set(authority, referrer);
|
|
52
47
|
}
|
|
53
|
-
else if (
|
|
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
|
-
|
|
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.
|
|
63
|
+
await this.addReferrer(authorityPublicKey);
|
|
75
64
|
}
|
|
76
|
-
return this.
|
|
65
|
+
return this.getReferrer(authorityPublicKey);
|
|
77
66
|
}
|
|
78
|
-
|
|
79
|
-
|
|
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.
|
|
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) {
|
|
@@ -102,9 +111,6 @@ class ReferrerMap {
|
|
|
102
111
|
await this.syncReferrer((0, memcmp_1.getUserStatsIsReferredOrReferrerFilter)());
|
|
103
112
|
}
|
|
104
113
|
}
|
|
105
|
-
catch (e) {
|
|
106
|
-
console.error('error in referrerMap.sync', e);
|
|
107
|
-
}
|
|
108
114
|
finally {
|
|
109
115
|
this.fetchPromiseResolver();
|
|
110
116
|
this.fetchPromise = undefined;
|
|
@@ -132,7 +138,7 @@ class ReferrerMap {
|
|
|
132
138
|
// only add if it isn't already in the map
|
|
133
139
|
// so that if syncReferrer already set it, we dont overwrite
|
|
134
140
|
if (!this.has(account.pubkey)) {
|
|
135
|
-
this.
|
|
141
|
+
this.addReferrer(account.pubkey, DEFAULT_PUBLIC_KEY);
|
|
136
142
|
}
|
|
137
143
|
}
|
|
138
144
|
}
|
|
@@ -162,19 +168,14 @@ class ReferrerMap {
|
|
|
162
168
|
const buffer = Buffer.from(programAccount.account.data[0], programAccount.account.data[1]);
|
|
163
169
|
const authority = bytes_1.bs58.encode(buffer.subarray(8, 40));
|
|
164
170
|
const referrer = bytes_1.bs58.encode(buffer.subarray(40, 72));
|
|
165
|
-
|
|
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
|
-
});
|
|
171
|
+
this.addReferrer(authority, referrer);
|
|
172
172
|
}));
|
|
173
173
|
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
174
174
|
}
|
|
175
175
|
}
|
|
176
176
|
async unsubscribe() {
|
|
177
|
-
this.
|
|
177
|
+
this.authorityReferrerMap.clear();
|
|
178
|
+
this.referrerReferrerInfoMap.clear();
|
|
178
179
|
}
|
|
179
180
|
}
|
|
180
181
|
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
|
|
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
|
|
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,
|
|
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
|
-
|
|
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
|
-
|
|
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,
|
|
15
|
+
constructor(driftClient, parallelSync) {
|
|
18
16
|
/**
|
|
19
|
-
* map from authority pubkey to
|
|
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.
|
|
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.
|
|
39
|
+
return this.authorityReferrerMap.has(authorityPublicKey);
|
|
44
40
|
}
|
|
45
41
|
get(authorityPublicKey) {
|
|
46
|
-
|
|
47
|
-
return info === null ? undefined : info;
|
|
42
|
+
return this.getReferrer(authorityPublicKey);
|
|
48
43
|
}
|
|
49
|
-
async
|
|
50
|
-
if (
|
|
51
|
-
this.
|
|
44
|
+
async addReferrer(authority, referrer) {
|
|
45
|
+
if (referrer) {
|
|
46
|
+
this.authorityReferrerMap.set(authority, referrer);
|
|
52
47
|
}
|
|
53
|
-
else if (
|
|
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
|
-
|
|
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.
|
|
63
|
+
await this.addReferrer(authorityPublicKey);
|
|
75
64
|
}
|
|
76
|
-
return this.
|
|
65
|
+
return this.getReferrer(authorityPublicKey);
|
|
77
66
|
}
|
|
78
|
-
|
|
79
|
-
|
|
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.
|
|
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) {
|
|
@@ -102,9 +111,6 @@ class ReferrerMap {
|
|
|
102
111
|
await this.syncReferrer((0, memcmp_1.getUserStatsIsReferredOrReferrerFilter)());
|
|
103
112
|
}
|
|
104
113
|
}
|
|
105
|
-
catch (e) {
|
|
106
|
-
console.error('error in referrerMap.sync', e);
|
|
107
|
-
}
|
|
108
114
|
finally {
|
|
109
115
|
this.fetchPromiseResolver();
|
|
110
116
|
this.fetchPromise = undefined;
|
|
@@ -132,7 +138,7 @@ class ReferrerMap {
|
|
|
132
138
|
// only add if it isn't already in the map
|
|
133
139
|
// so that if syncReferrer already set it, we dont overwrite
|
|
134
140
|
if (!this.has(account.pubkey)) {
|
|
135
|
-
this.
|
|
141
|
+
this.addReferrer(account.pubkey, DEFAULT_PUBLIC_KEY);
|
|
136
142
|
}
|
|
137
143
|
}
|
|
138
144
|
}
|
|
@@ -162,19 +168,14 @@ class ReferrerMap {
|
|
|
162
168
|
const buffer = Buffer.from(programAccount.account.data[0], programAccount.account.data[1]);
|
|
163
169
|
const authority = bytes_1.bs58.encode(buffer.subarray(8, 40));
|
|
164
170
|
const referrer = bytes_1.bs58.encode(buffer.subarray(40, 72));
|
|
165
|
-
|
|
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
|
-
});
|
|
171
|
+
this.addReferrer(authority, referrer);
|
|
172
172
|
}));
|
|
173
173
|
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
174
174
|
}
|
|
175
175
|
}
|
|
176
176
|
async unsubscribe() {
|
|
177
|
-
this.
|
|
177
|
+
this.authorityReferrerMap.clear();
|
|
178
|
+
this.referrerReferrerInfoMap.clear();
|
|
178
179
|
}
|
|
179
180
|
}
|
|
180
181
|
exports.ReferrerMap = ReferrerMap;
|
package/package.json
CHANGED
|
@@ -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
|
|
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
|
|
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.
|
|
60
|
+
return this.authorityReferrerMap.has(authorityPublicKey);
|
|
74
61
|
}
|
|
75
62
|
|
|
76
63
|
public get(authorityPublicKey: string): ReferrerInfo | undefined {
|
|
77
|
-
|
|
78
|
-
return info === null ? undefined : info;
|
|
64
|
+
return this.getReferrer(authorityPublicKey);
|
|
79
65
|
}
|
|
80
66
|
|
|
81
|
-
public async
|
|
82
|
-
|
|
83
|
-
|
|
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
|
-
|
|
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.
|
|
98
|
+
await this.addReferrer(authorityPublicKey);
|
|
132
99
|
}
|
|
133
|
-
return this.
|
|
100
|
+
return this.getReferrer(authorityPublicKey);
|
|
134
101
|
}
|
|
135
102
|
|
|
136
|
-
public
|
|
137
|
-
|
|
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.
|
|
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> {
|
|
@@ -162,8 +163,6 @@ export class ReferrerMap {
|
|
|
162
163
|
await this.syncReferrer(getUserStatsIsReferredFilter());
|
|
163
164
|
await this.syncReferrer(getUserStatsIsReferredOrReferrerFilter());
|
|
164
165
|
}
|
|
165
|
-
} catch (e) {
|
|
166
|
-
console.error('error in referrerMap.sync', e);
|
|
167
166
|
} finally {
|
|
168
167
|
this.fetchPromiseResolver();
|
|
169
168
|
this.fetchPromise = undefined;
|
|
@@ -205,7 +204,7 @@ export class ReferrerMap {
|
|
|
205
204
|
// only add if it isn't already in the map
|
|
206
205
|
// so that if syncReferrer already set it, we dont overwrite
|
|
207
206
|
if (!this.has(account.pubkey)) {
|
|
208
|
-
this.
|
|
207
|
+
this.addReferrer(account.pubkey, DEFAULT_PUBLIC_KEY);
|
|
209
208
|
}
|
|
210
209
|
}
|
|
211
210
|
}
|
|
@@ -254,23 +253,7 @@ export class ReferrerMap {
|
|
|
254
253
|
const authority = bs58.encode(buffer.subarray(8, 40));
|
|
255
254
|
const referrer = bs58.encode(buffer.subarray(40, 72));
|
|
256
255
|
|
|
257
|
-
|
|
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
|
-
);
|
|
256
|
+
this.addReferrer(authority, referrer);
|
|
274
257
|
})
|
|
275
258
|
);
|
|
276
259
|
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
@@ -278,6 +261,7 @@ export class ReferrerMap {
|
|
|
278
261
|
}
|
|
279
262
|
|
|
280
263
|
public async unsubscribe() {
|
|
281
|
-
this.
|
|
264
|
+
this.authorityReferrerMap.clear();
|
|
265
|
+
this.referrerReferrerInfoMap.clear();
|
|
282
266
|
}
|
|
283
267
|
}
|