@drift-labs/sdk 0.2.0-master.4 → 0.2.0-master.40
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/README.md +27 -27
- package/lib/accounts/bulkAccountLoader.d.ts +2 -0
- package/lib/accounts/bulkAccountLoader.js +36 -29
- package/lib/accounts/bulkUserStatsSubscription.d.ts +7 -0
- package/lib/accounts/bulkUserStatsSubscription.js +21 -0
- package/lib/accounts/bulkUserSubscription.d.ts +2 -2
- package/lib/accounts/bulkUserSubscription.js +0 -1
- package/lib/accounts/fetch.d.ts +2 -1
- package/lib/accounts/fetch.js +9 -1
- package/lib/accounts/{pollingClearingHouseAccountSubscriber.d.ts → pollingDriftClientAccountSubscriber.d.ts} +20 -25
- package/lib/accounts/{pollingClearingHouseAccountSubscriber.js → pollingDriftClientAccountSubscriber.js} +45 -49
- package/lib/accounts/{pollingOracleSubscriber.d.ts → pollingOracleAccountSubscriber.d.ts} +2 -2
- package/lib/accounts/{pollingOracleSubscriber.js → pollingOracleAccountSubscriber.js} +3 -3
- package/lib/accounts/pollingUserStatsAccountSubscriber.d.ts +27 -0
- package/lib/accounts/pollingUserStatsAccountSubscriber.js +113 -0
- package/lib/accounts/types.d.ts +26 -15
- package/lib/accounts/webSocketDriftClientAccountSubscriber.d.ts +49 -0
- package/lib/accounts/{webSocketClearingHouseAccountSubscriber.js → webSocketDriftClientAccountSubscriber.js} +47 -45
- package/lib/accounts/webSocketUserStatsAccountSubsriber.d.ts +20 -0
- package/lib/accounts/webSocketUserStatsAccountSubsriber.js +47 -0
- package/lib/addresses/marketAddresses.d.ts +1 -3
- package/lib/addresses/marketAddresses.js +1 -1
- package/lib/addresses/pda.d.ts +15 -9
- package/lib/addresses/pda.js +73 -35
- package/lib/adminClient.d.ts +65 -0
- package/lib/adminClient.js +637 -0
- package/lib/config.d.ts +9 -9
- package/lib/config.js +25 -21
- package/lib/constants/numericConstants.d.ts +30 -12
- package/lib/constants/numericConstants.js +41 -21
- package/lib/constants/perpMarkets.d.ts +18 -0
- package/lib/constants/{markets.js → perpMarkets.js} +7 -7
- package/lib/constants/spotMarkets.d.ts +19 -0
- package/lib/constants/spotMarkets.js +53 -0
- package/lib/dlob/DLOB.d.ts +82 -0
- package/lib/dlob/DLOB.js +696 -0
- package/lib/dlob/DLOBNode.d.ts +54 -0
- package/lib/dlob/DLOBNode.js +77 -0
- package/lib/dlob/NodeList.d.ts +27 -0
- package/lib/dlob/NodeList.js +144 -0
- package/lib/driftClient.d.ts +233 -0
- package/lib/driftClient.js +2096 -0
- package/lib/{clearingHouseConfig.d.ts → driftClientConfig.d.ts} +9 -9
- package/lib/{clearingHouseConfig.js → driftClientConfig.js} +0 -0
- package/lib/events/eventList.js +3 -0
- package/lib/events/eventSubscriber.d.ts +5 -2
- package/lib/events/eventSubscriber.js +25 -11
- package/lib/events/fetchLogs.d.ts +13 -2
- package/lib/events/fetchLogs.js +40 -12
- package/lib/events/pollingLogProvider.d.ts +2 -1
- package/lib/events/pollingLogProvider.js +7 -3
- package/lib/events/sort.js +8 -11
- package/lib/events/types.d.ts +9 -3
- package/lib/events/types.js +6 -0
- package/lib/events/webSocketLogProvider.js +1 -1
- package/lib/examples/makeTradeExample.js +30 -18
- package/lib/factory/bigNum.d.ts +8 -4
- package/lib/factory/bigNum.js +109 -19
- package/lib/idl/drift.json +8250 -0
- package/lib/idl/{mock_usdc_faucet.json → token_faucet.json} +46 -23
- package/lib/index.d.ts +30 -13
- package/lib/index.js +30 -13
- package/lib/math/amm.d.ts +9 -6
- package/lib/math/amm.js +91 -38
- package/lib/math/conversion.js +1 -1
- package/lib/math/exchangeStatus.d.ts +4 -0
- package/lib/math/exchangeStatus.js +18 -0
- package/lib/math/funding.d.ts +6 -6
- package/lib/math/funding.js +23 -21
- package/lib/math/insurance.d.ts +4 -0
- package/lib/math/insurance.js +27 -0
- package/lib/math/margin.d.ts +11 -0
- package/lib/math/margin.js +82 -0
- package/lib/math/market.d.ts +14 -9
- package/lib/math/market.js +70 -10
- package/lib/math/oracles.d.ts +4 -0
- package/lib/math/oracles.js +36 -8
- package/lib/math/orders.d.ts +14 -6
- package/lib/math/orders.js +90 -17
- package/lib/math/position.d.ts +27 -13
- package/lib/math/position.js +92 -36
- package/lib/math/repeg.js +17 -8
- package/lib/math/spotBalance.d.ts +22 -0
- package/lib/math/spotBalance.js +192 -0
- package/lib/math/spotMarket.d.ts +4 -0
- package/lib/math/spotMarket.js +8 -0
- package/lib/math/spotPosition.d.ts +6 -0
- package/lib/math/spotPosition.js +23 -0
- package/lib/math/trade.d.ts +10 -10
- package/lib/math/trade.js +27 -31
- package/lib/oracles/pythClient.js +1 -1
- package/lib/oracles/quoteAssetOracleClient.js +1 -1
- package/lib/oracles/switchboardClient.js +1 -1
- package/lib/orderParams.d.ts +4 -4
- package/lib/orderParams.js +12 -4
- package/lib/serum/serumFulfillmentConfigMap.d.ts +10 -0
- package/lib/serum/serumFulfillmentConfigMap.js +17 -0
- package/lib/serum/serumSubscriber.d.ts +27 -0
- package/lib/serum/serumSubscriber.js +56 -0
- package/lib/serum/types.d.ts +11 -0
- package/lib/{clearingHouseUserConfig.js → serum/types.js} +0 -0
- package/lib/slot/SlotSubscriber.d.ts +7 -0
- package/lib/slot/SlotSubscriber.js +3 -0
- package/lib/{mockUSDCFaucet.d.ts → tokenFaucet.d.ts} +8 -5
- package/lib/{mockUSDCFaucet.js → tokenFaucet.js} +63 -51
- package/lib/tx/retryTxSender.d.ts +1 -1
- package/lib/tx/retryTxSender.js +13 -4
- package/lib/tx/types.d.ts +1 -1
- package/lib/tx/utils.js +1 -1
- package/lib/types.d.ts +589 -196
- package/lib/types.js +108 -17
- package/lib/user.d.ts +226 -0
- package/lib/user.js +949 -0
- package/lib/userConfig.d.ts +14 -0
- package/lib/userConfig.js +2 -0
- package/lib/userMap/userMap.d.ts +41 -0
- package/lib/userMap/userMap.js +85 -0
- package/lib/userMap/userStatsMap.d.ts +19 -0
- package/lib/userMap/userStatsMap.js +68 -0
- package/lib/userName.d.ts +1 -0
- package/lib/userName.js +3 -2
- package/lib/userStats.d.ts +18 -0
- package/lib/userStats.js +49 -0
- package/lib/userStatsConfig.d.ts +14 -0
- package/lib/userStatsConfig.js +2 -0
- package/lib/util/computeUnits.js +1 -1
- package/lib/util/getTokenAddress.d.ts +2 -0
- package/lib/util/getTokenAddress.js +9 -0
- package/package.json +10 -3
- package/src/accounts/bulkAccountLoader.ts +44 -34
- package/src/accounts/bulkUserStatsSubscription.ts +33 -0
- package/src/accounts/bulkUserSubscription.ts +2 -3
- package/src/accounts/fetch.ts +27 -2
- package/src/accounts/{pollingClearingHouseAccountSubscriber.ts → pollingDriftClientAccountSubscriber.ts} +65 -75
- package/src/accounts/{pollingOracleSubscriber.ts → pollingOracleAccountSubscriber.ts} +2 -2
- package/src/accounts/pollingUserStatsAccountSubscriber.ts +172 -0
- package/src/accounts/types.ts +35 -15
- package/src/accounts/{webSocketClearingHouseAccountSubscriber.ts → webSocketDriftClientAccountSubscriber.ts} +78 -73
- package/src/accounts/webSocketUserStatsAccountSubsriber.ts +80 -0
- package/src/addresses/marketAddresses.ts +3 -4
- package/src/addresses/pda.ts +105 -33
- package/src/adminClient.ts +1207 -0
- package/src/config.ts +37 -31
- package/src/constants/numericConstants.ts +58 -24
- package/src/constants/{markets.ts → perpMarkets.ts} +10 -10
- package/src/constants/spotMarkets.ts +73 -0
- package/src/dlob/DLOB.ts +1123 -0
- package/src/dlob/DLOBNode.ts +155 -0
- package/src/dlob/NodeList.ts +195 -0
- package/src/driftClient.ts +3564 -0
- package/src/{clearingHouseConfig.ts → driftClientConfig.ts} +9 -8
- package/src/events/eventList.ts +3 -0
- package/src/events/eventSubscriber.ts +36 -14
- package/src/events/fetchLogs.ts +55 -13
- package/src/events/pollingLogProvider.ts +11 -3
- package/src/events/sort.ts +11 -15
- package/src/events/types.ts +21 -2
- package/src/events/webSocketLogProvider.ts +1 -1
- package/src/examples/makeTradeExample.ts +44 -28
- package/src/factory/bigNum.ts +150 -22
- package/src/idl/drift.json +8250 -0
- package/src/idl/pyth.json +98 -2
- package/src/idl/{mock_usdc_faucet.json → token_faucet.json} +46 -23
- package/src/index.ts +30 -13
- package/src/math/amm.ts +161 -48
- package/src/math/conversion.ts +2 -2
- package/src/math/exchangeStatus.ts +31 -0
- package/src/math/funding.ts +41 -31
- package/src/math/insurance.ts +35 -0
- package/src/math/margin.ts +133 -0
- package/src/math/market.ts +143 -14
- package/src/math/oracles.ts +63 -9
- package/src/math/orders.ts +163 -26
- package/src/math/position.ts +136 -58
- package/src/math/repeg.ts +19 -9
- package/src/math/spotBalance.ts +319 -0
- package/src/math/spotMarket.ts +9 -0
- package/src/math/spotPosition.ts +47 -0
- package/src/math/trade.ts +33 -37
- package/src/oracles/pythClient.ts +2 -2
- package/src/oracles/quoteAssetOracleClient.ts +2 -2
- package/src/oracles/switchboardClient.ts +2 -2
- package/src/orderParams.ts +16 -8
- package/src/serum/serumFulfillmentConfigMap.ts +26 -0
- package/src/serum/serumSubscriber.ts +99 -0
- package/src/serum/types.ts +13 -0
- package/src/slot/SlotSubscriber.ts +11 -1
- package/src/{mockUSDCFaucet.ts → tokenFaucet.ts} +82 -70
- package/src/tx/retryTxSender.ts +16 -5
- package/src/tx/types.ts +2 -1
- package/src/tx/utils.ts +1 -1
- package/src/types.ts +572 -178
- package/src/user.ts +1582 -0
- package/src/{clearingHouseUserConfig.ts → userConfig.ts} +5 -5
- package/src/userMap/userMap.ts +124 -0
- package/src/userMap/userStatsMap.ts +108 -0
- package/src/userName.ts +2 -1
- package/src/userStats.ts +75 -0
- package/src/userStatsConfig.ts +18 -0
- package/src/util/computeUnits.ts +1 -1
- package/src/util/getTokenAddress.ts +18 -0
- package/tests/bn/test.ts +46 -11
- package/tests/dlob/helpers.ts +611 -0
- package/tests/dlob/test.ts +4588 -0
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +0 -49
- package/lib/admin.d.ts +0 -44
- package/lib/admin.js +0 -433
- package/lib/clearingHouse.d.ts +0 -133
- package/lib/clearingHouse.js +0 -931
- package/lib/clearingHouseUser.d.ts +0 -187
- package/lib/clearingHouseUser.js +0 -643
- package/lib/clearingHouseUserConfig.d.ts +0 -14
- package/lib/constants/banks.d.ts +0 -16
- package/lib/constants/banks.js +0 -34
- package/lib/constants/markets.d.ts +0 -19
- package/lib/idl/clearing_house.json +0 -3998
- package/lib/math/bankBalance.d.ts +0 -9
- package/lib/math/bankBalance.js +0 -75
- package/lib/math/state.d.ts +0 -8
- package/lib/math/state.js +0 -15
- package/lib/orders.d.ts +0 -8
- package/lib/orders.js +0 -134
- package/src/admin.ts +0 -722
- package/src/clearingHouse.ts +0 -1451
- package/src/clearingHouseUser.ts +0 -989
- package/src/constants/banks.ts +0 -43
- package/src/idl/clearing_house.json +0 -3998
- package/src/math/bankBalance.ts +0 -112
- package/src/math/state.ts +0 -14
- package/src/math/utils.js +0 -27
- package/src/math/utils.js.map +0 -1
- package/src/orders.ts +0 -244
- package/src/util/computeUnits.js +0 -17
- package/src/util/computeUnits.js.map +0 -1
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { DriftClient } from './driftClient';
|
|
2
2
|
import { PublicKey } from '@solana/web3.js';
|
|
3
3
|
import { BulkAccountLoader } from './accounts/bulkAccountLoader';
|
|
4
4
|
|
|
5
|
-
export type
|
|
6
|
-
accountSubscription?:
|
|
7
|
-
|
|
5
|
+
export type UserConfig = {
|
|
6
|
+
accountSubscription?: UserSubscriptionConfig;
|
|
7
|
+
driftClient: DriftClient;
|
|
8
8
|
userAccountPublicKey: PublicKey;
|
|
9
9
|
};
|
|
10
10
|
|
|
11
|
-
export type
|
|
11
|
+
export type UserSubscriptionConfig =
|
|
12
12
|
| {
|
|
13
13
|
type: 'websocket';
|
|
14
14
|
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import {
|
|
2
|
+
User,
|
|
3
|
+
DriftClient,
|
|
4
|
+
UserAccount,
|
|
5
|
+
bulkPollingUserSubscribe,
|
|
6
|
+
OrderRecord,
|
|
7
|
+
UserSubscriptionConfig,
|
|
8
|
+
} from '..';
|
|
9
|
+
import { ProgramAccount } from '@project-serum/anchor';
|
|
10
|
+
|
|
11
|
+
import { PublicKey } from '@solana/web3.js';
|
|
12
|
+
|
|
13
|
+
export interface UserMapInterface {
|
|
14
|
+
fetchAllUsers(): Promise<void>;
|
|
15
|
+
addPubkey(userAccountPublicKey: PublicKey): Promise<void>;
|
|
16
|
+
has(key: string): boolean;
|
|
17
|
+
get(key: string): User | undefined;
|
|
18
|
+
mustGet(key: string): Promise<User>;
|
|
19
|
+
getUserAuthority(key: string): PublicKey | undefined;
|
|
20
|
+
updateWithOrderRecord(record: OrderRecord): Promise<void>;
|
|
21
|
+
values(): IterableIterator<User>;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export class UserMap implements UserMapInterface {
|
|
25
|
+
private userMap = new Map<string, User>();
|
|
26
|
+
private driftClient: DriftClient;
|
|
27
|
+
private accountSubscription: UserSubscriptionConfig;
|
|
28
|
+
|
|
29
|
+
constructor(
|
|
30
|
+
driftClient: DriftClient,
|
|
31
|
+
accountSubscription: UserSubscriptionConfig
|
|
32
|
+
) {
|
|
33
|
+
this.driftClient = driftClient;
|
|
34
|
+
this.accountSubscription = accountSubscription;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
public async fetchAllUsers() {
|
|
38
|
+
const userArray: User[] = [];
|
|
39
|
+
|
|
40
|
+
const programUserAccounts =
|
|
41
|
+
(await this.driftClient.program.account.user.all()) as ProgramAccount<UserAccount>[];
|
|
42
|
+
for (const programUserAccount of programUserAccounts) {
|
|
43
|
+
if (this.userMap.has(programUserAccount.publicKey.toString())) {
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const user = new User({
|
|
48
|
+
driftClient: this.driftClient,
|
|
49
|
+
userAccountPublicKey: programUserAccount.publicKey,
|
|
50
|
+
accountSubscription: this.accountSubscription,
|
|
51
|
+
});
|
|
52
|
+
userArray.push(user);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (this.accountSubscription.type === 'polling') {
|
|
56
|
+
await bulkPollingUserSubscribe(
|
|
57
|
+
userArray,
|
|
58
|
+
this.accountSubscription.accountLoader
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
for (const user of userArray) {
|
|
63
|
+
this.userMap.set(user.getUserAccountPublicKey().toString(), user);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
public async addPubkey(userAccountPublicKey: PublicKey) {
|
|
68
|
+
const user = new User({
|
|
69
|
+
driftClient: this.driftClient,
|
|
70
|
+
userAccountPublicKey,
|
|
71
|
+
accountSubscription: this.accountSubscription,
|
|
72
|
+
});
|
|
73
|
+
await user.subscribe();
|
|
74
|
+
this.userMap.set(userAccountPublicKey.toString(), user);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
public has(key: string): boolean {
|
|
78
|
+
return this.userMap.has(key);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* gets the User for a particular userAccountPublicKey, if no User exists, undefined is returned
|
|
83
|
+
* @param key userAccountPublicKey to get User for
|
|
84
|
+
* @returns user User | undefined
|
|
85
|
+
*/
|
|
86
|
+
public get(key: string): User | undefined {
|
|
87
|
+
return this.userMap.get(key);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* gets the User for a particular userAccountPublicKey, if no User exists, new one is created
|
|
92
|
+
* @param key userAccountPublicKey to get User for
|
|
93
|
+
* @returns User
|
|
94
|
+
*/
|
|
95
|
+
public async mustGet(key: string): Promise<User> {
|
|
96
|
+
if (!this.has(key)) {
|
|
97
|
+
await this.addPubkey(new PublicKey(key));
|
|
98
|
+
}
|
|
99
|
+
const user = this.userMap.get(key);
|
|
100
|
+
await user.fetchAccounts();
|
|
101
|
+
return user;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* gets the Authority for a particular userAccountPublicKey, if no User exists, undefined is returned
|
|
106
|
+
* @param key userAccountPublicKey to get User for
|
|
107
|
+
* @returns authority PublicKey | undefined
|
|
108
|
+
*/
|
|
109
|
+
public getUserAuthority(key: string): PublicKey | undefined {
|
|
110
|
+
const chUser = this.userMap.get(key);
|
|
111
|
+
if (!chUser) {
|
|
112
|
+
return undefined;
|
|
113
|
+
}
|
|
114
|
+
return chUser.getUserAccount().authority;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
public async updateWithOrderRecord(record: OrderRecord) {
|
|
118
|
+
await this.addPubkey(record.user);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
public values(): IterableIterator<User> {
|
|
122
|
+
return this.userMap.values();
|
|
123
|
+
}
|
|
124
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DriftClient,
|
|
3
|
+
getUserStatsAccountPublicKey,
|
|
4
|
+
OrderRecord,
|
|
5
|
+
UserStatsAccount,
|
|
6
|
+
UserStats,
|
|
7
|
+
UserStatsSubscriptionConfig,
|
|
8
|
+
bulkPollingUserStatsSubscribe,
|
|
9
|
+
} from '..';
|
|
10
|
+
import { ProgramAccount } from '@project-serum/anchor';
|
|
11
|
+
import { PublicKey } from '@solana/web3.js';
|
|
12
|
+
|
|
13
|
+
import { UserMap } from './userMap';
|
|
14
|
+
|
|
15
|
+
export class UserStatsMap {
|
|
16
|
+
/**
|
|
17
|
+
* map from authority pubkey to UserStats
|
|
18
|
+
*/
|
|
19
|
+
private userStatsMap = new Map<string, UserStats>();
|
|
20
|
+
private driftClient: DriftClient;
|
|
21
|
+
private accountSubscription: UserStatsSubscriptionConfig;
|
|
22
|
+
|
|
23
|
+
constructor(
|
|
24
|
+
driftClient: DriftClient,
|
|
25
|
+
accountSubscription: UserStatsSubscriptionConfig
|
|
26
|
+
) {
|
|
27
|
+
this.driftClient = driftClient;
|
|
28
|
+
this.accountSubscription = accountSubscription;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
public async fetchAllUserStats() {
|
|
32
|
+
const userStatArray: UserStats[] = [];
|
|
33
|
+
|
|
34
|
+
const programUserAccounts =
|
|
35
|
+
(await this.driftClient.program.account.userStats.all()) as ProgramAccount<UserStatsAccount>[];
|
|
36
|
+
|
|
37
|
+
for (const programUserAccount of programUserAccounts) {
|
|
38
|
+
const userStat: UserStatsAccount = programUserAccount.account;
|
|
39
|
+
if (this.userStatsMap.has(userStat.authority.toString())) {
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const chUserStat = new UserStats({
|
|
44
|
+
driftClient: this.driftClient,
|
|
45
|
+
userStatsAccountPublicKey: getUserStatsAccountPublicKey(
|
|
46
|
+
this.driftClient.program.programId,
|
|
47
|
+
userStat.authority
|
|
48
|
+
),
|
|
49
|
+
accountSubscription: this.accountSubscription,
|
|
50
|
+
});
|
|
51
|
+
userStatArray.push(chUserStat);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (this.accountSubscription.type === 'polling') {
|
|
55
|
+
await bulkPollingUserStatsSubscribe(
|
|
56
|
+
userStatArray,
|
|
57
|
+
this.accountSubscription.accountLoader
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
for (const userStat of userStatArray) {
|
|
62
|
+
this.userStatsMap.set(
|
|
63
|
+
userStat.getAccount().authority.toString(),
|
|
64
|
+
userStat
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
public async addUserStat(authority: PublicKey) {
|
|
70
|
+
const userStat = new UserStats({
|
|
71
|
+
driftClient: this.driftClient,
|
|
72
|
+
userStatsAccountPublicKey: getUserStatsAccountPublicKey(
|
|
73
|
+
this.driftClient.program.programId,
|
|
74
|
+
authority
|
|
75
|
+
),
|
|
76
|
+
accountSubscription: this.accountSubscription,
|
|
77
|
+
});
|
|
78
|
+
await userStat.subscribe();
|
|
79
|
+
|
|
80
|
+
this.userStatsMap.set(authority.toString(), userStat);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
public async updateWithOrderRecord(record: OrderRecord, userMap: UserMap) {
|
|
84
|
+
if (!this.has(record.user.toString())) {
|
|
85
|
+
const takerUserAccount = await userMap.mustGet(record.user.toString());
|
|
86
|
+
this.addUserStat(takerUserAccount.getUserAccount().authority);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
public has(authorityPublicKey: string): boolean {
|
|
91
|
+
return this.userStatsMap.has(authorityPublicKey);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
public get(authorityPublicKey: string): UserStats {
|
|
95
|
+
return this.userStatsMap.get(authorityPublicKey);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
public async mustGet(authorityPublicKey: string): Promise<UserStats> {
|
|
99
|
+
if (!this.has(authorityPublicKey)) {
|
|
100
|
+
await this.addUserStat(new PublicKey(authorityPublicKey));
|
|
101
|
+
}
|
|
102
|
+
return this.get(authorityPublicKey);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
public values(): IterableIterator<UserStats> {
|
|
106
|
+
return this.userStatsMap.values();
|
|
107
|
+
}
|
|
108
|
+
}
|
package/src/userName.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
export const MAX_NAME_LENGTH = 32;
|
|
2
2
|
|
|
3
3
|
export const DEFAULT_USER_NAME = 'Main Account';
|
|
4
|
+
export const DEFAULT_MARKET_NAME = 'Default Market Name';
|
|
4
5
|
|
|
5
6
|
export function encodeName(name: string): number[] {
|
|
6
7
|
if (name.length > MAX_NAME_LENGTH) {
|
|
7
|
-
throw Error(`
|
|
8
|
+
throw Error(`Name (${name}) longer than 32 characters`);
|
|
8
9
|
}
|
|
9
10
|
|
|
10
11
|
const buffer = Buffer.alloc(32);
|
package/src/userStats.ts
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { DriftClient } from './driftClient';
|
|
2
|
+
import { PublicKey } from '@solana/web3.js';
|
|
3
|
+
import { DataAndSlot, UserStatsAccountSubscriber } from './accounts/types';
|
|
4
|
+
import { UserStatsConfig } from './userStatsConfig';
|
|
5
|
+
import { PollingUserStatsAccountSubscriber } from './accounts/pollingUserStatsAccountSubscriber';
|
|
6
|
+
import { WebSocketUserStatsAccountSubscriber } from './accounts/webSocketUserStatsAccountSubsriber';
|
|
7
|
+
import { ReferrerInfo, UserStatsAccount } from './types';
|
|
8
|
+
import {
|
|
9
|
+
getUserAccountPublicKeySync,
|
|
10
|
+
getUserStatsAccountPublicKey,
|
|
11
|
+
} from './addresses/pda';
|
|
12
|
+
|
|
13
|
+
export class UserStats {
|
|
14
|
+
driftClient: DriftClient;
|
|
15
|
+
userStatsAccountPublicKey: PublicKey;
|
|
16
|
+
accountSubscriber: UserStatsAccountSubscriber;
|
|
17
|
+
isSubscribed: boolean;
|
|
18
|
+
|
|
19
|
+
public constructor(config: UserStatsConfig) {
|
|
20
|
+
this.driftClient = config.driftClient;
|
|
21
|
+
this.userStatsAccountPublicKey = config.userStatsAccountPublicKey;
|
|
22
|
+
if (config.accountSubscription?.type === 'polling') {
|
|
23
|
+
this.accountSubscriber = new PollingUserStatsAccountSubscriber(
|
|
24
|
+
config.driftClient.program,
|
|
25
|
+
config.userStatsAccountPublicKey,
|
|
26
|
+
config.accountSubscription.accountLoader
|
|
27
|
+
);
|
|
28
|
+
} else {
|
|
29
|
+
this.accountSubscriber = new WebSocketUserStatsAccountSubscriber(
|
|
30
|
+
config.driftClient.program,
|
|
31
|
+
config.userStatsAccountPublicKey
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
public async subscribe(): Promise<boolean> {
|
|
37
|
+
this.isSubscribed = await this.accountSubscriber.subscribe();
|
|
38
|
+
return this.isSubscribed;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
public async fetchAccounts(): Promise<void> {
|
|
42
|
+
await this.accountSubscriber.fetch();
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
public async unsubscribe(): Promise<void> {
|
|
46
|
+
await this.accountSubscriber.unsubscribe();
|
|
47
|
+
this.isSubscribed = false;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
public getAccountAndSlot(): DataAndSlot<UserStatsAccount> {
|
|
51
|
+
return this.accountSubscriber.getUserStatsAccountAndSlot();
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
public getAccount(): UserStatsAccount {
|
|
55
|
+
return this.accountSubscriber.getUserStatsAccountAndSlot().data;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
public getReferrerInfo(): ReferrerInfo | undefined {
|
|
59
|
+
if (this.getAccount().referrer.equals(PublicKey.default)) {
|
|
60
|
+
return undefined;
|
|
61
|
+
} else {
|
|
62
|
+
return {
|
|
63
|
+
referrer: getUserAccountPublicKeySync(
|
|
64
|
+
this.driftClient.program.programId,
|
|
65
|
+
this.getAccount().referrer,
|
|
66
|
+
0
|
|
67
|
+
),
|
|
68
|
+
referrerStats: getUserStatsAccountPublicKey(
|
|
69
|
+
this.driftClient.program.programId,
|
|
70
|
+
this.getAccount().referrer
|
|
71
|
+
),
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { DriftClient } from './driftClient';
|
|
2
|
+
import { PublicKey } from '@solana/web3.js';
|
|
3
|
+
import { BulkAccountLoader } from './accounts/bulkAccountLoader';
|
|
4
|
+
|
|
5
|
+
export type UserStatsConfig = {
|
|
6
|
+
accountSubscription?: UserStatsSubscriptionConfig;
|
|
7
|
+
driftClient: DriftClient;
|
|
8
|
+
userStatsAccountPublicKey: PublicKey;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export type UserStatsSubscriptionConfig =
|
|
12
|
+
| {
|
|
13
|
+
type: 'websocket';
|
|
14
|
+
}
|
|
15
|
+
| {
|
|
16
|
+
type: 'polling';
|
|
17
|
+
accountLoader: BulkAccountLoader;
|
|
18
|
+
};
|
package/src/util/computeUnits.ts
CHANGED
|
@@ -9,7 +9,7 @@ export async function findComputeUnitConsumption(
|
|
|
9
9
|
const tx = await connection.getTransaction(txSignature, { commitment });
|
|
10
10
|
const computeUnits = [];
|
|
11
11
|
const regex = new RegExp(
|
|
12
|
-
`Program ${programId.toString()} consumed ([0-9]{0,6}) of
|
|
12
|
+
`Program ${programId.toString()} consumed ([0-9]{0,6}) of ([0-9]{0,7}) compute units`
|
|
13
13
|
);
|
|
14
14
|
tx.meta.logMessages.forEach((logMessage) => {
|
|
15
15
|
const match = logMessage.match(regex);
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Token,
|
|
3
|
+
ASSOCIATED_TOKEN_PROGRAM_ID,
|
|
4
|
+
TOKEN_PROGRAM_ID,
|
|
5
|
+
} from '@solana/spl-token';
|
|
6
|
+
import { PublicKey } from '@solana/web3.js';
|
|
7
|
+
|
|
8
|
+
export const getTokenAddress = (
|
|
9
|
+
mintAddress: string,
|
|
10
|
+
userPubKey: string
|
|
11
|
+
): Promise<PublicKey> => {
|
|
12
|
+
return Token.getAssociatedTokenAddress(
|
|
13
|
+
ASSOCIATED_TOKEN_PROGRAM_ID,
|
|
14
|
+
TOKEN_PROGRAM_ID,
|
|
15
|
+
new PublicKey(mintAddress),
|
|
16
|
+
new PublicKey(userPubKey)
|
|
17
|
+
);
|
|
18
|
+
};
|
package/tests/bn/test.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { BN } from '
|
|
1
|
+
import { BN } from '../../src/index';
|
|
2
2
|
import { expect } from 'chai';
|
|
3
3
|
import { BigNum } from '../../src/factory/bigNum';
|
|
4
4
|
import {
|
|
@@ -6,6 +6,7 @@ import {
|
|
|
6
6
|
BASE_PRECISION_EXP,
|
|
7
7
|
TEN_THOUSAND,
|
|
8
8
|
} from '../../src/constants/numericConstants';
|
|
9
|
+
|
|
9
10
|
// if you used the '@types/mocha' method to install mocha type definitions, uncomment the following line
|
|
10
11
|
// import 'mocha';
|
|
11
12
|
|
|
@@ -70,14 +71,15 @@ describe('BigNum Tests', () => {
|
|
|
70
71
|
|
|
71
72
|
expect(val.print()).to.equal('1234.56789');
|
|
72
73
|
|
|
73
|
-
expect(val.toFixed(3)).to.equal('1234.
|
|
74
|
-
|
|
74
|
+
expect(val.toNum().toFixed(3)).to.equal('1234.568');
|
|
75
75
|
expect(val.toPrecision(1)).to.equal('1e3');
|
|
76
76
|
expect(val.toPrecision(3)).to.equal('123e1');
|
|
77
77
|
expect(val.toPrecision(4)).to.equal('1234');
|
|
78
78
|
expect(val.toPrecision(5)).to.equal('1234.5');
|
|
79
79
|
expect(val.toPrecision(11)).to.equal('1234.5678900');
|
|
80
80
|
|
|
81
|
+
expect(BigNum.from('1234').toPrecision(5)).to.equal('1234.0');
|
|
82
|
+
|
|
81
83
|
// Case 2
|
|
82
84
|
const val2 = BigNum.from(1, 5);
|
|
83
85
|
|
|
@@ -100,9 +102,9 @@ describe('BigNum Tests', () => {
|
|
|
100
102
|
.shift(AMM_RESERVE_PRECISION_EXP)
|
|
101
103
|
.div(BigNum.from(entryPriceNum * 10 ** 8));
|
|
102
104
|
|
|
103
|
-
expect(val4.toString()).to.equal('
|
|
104
|
-
expect(val4.print()).to.equal('0.
|
|
105
|
-
expect(val4.toFixed(3)).to.equal('0.025');
|
|
105
|
+
expect(val4.toString()).to.equal('25000000');
|
|
106
|
+
expect(val4.print()).to.equal('0.025000000');
|
|
107
|
+
expect(val4.toNum().toFixed(3)).to.equal('0.025');
|
|
106
108
|
expect(val4.toPrecision(4)).to.equal('0.025');
|
|
107
109
|
|
|
108
110
|
// Case 5
|
|
@@ -119,12 +121,22 @@ describe('BigNum Tests', () => {
|
|
|
119
121
|
expect(BigNum.fromPrint('1').toMillified(5)).to.equal('1.0000');
|
|
120
122
|
expect(BigNum.fromPrint('12').toMillified(5)).to.equal('12.000');
|
|
121
123
|
expect(BigNum.fromPrint('123').toMillified(5)).to.equal('123.00');
|
|
122
|
-
expect(BigNum.fromPrint('1234').toMillified(5)).to.equal('
|
|
123
|
-
expect(BigNum.fromPrint('12345').toMillified(5)).to.equal('
|
|
124
|
+
expect(BigNum.fromPrint('1234').toMillified(5)).to.equal('1234.0');
|
|
125
|
+
expect(BigNum.fromPrint('12345').toMillified(5)).to.equal('12345');
|
|
124
126
|
expect(BigNum.fromPrint('123456').toMillified(5)).to.equal('123.45K');
|
|
125
127
|
expect(BigNum.fromPrint('1234567').toMillified(5)).to.equal('1.2345M');
|
|
126
128
|
expect(BigNum.fromPrint('12345678').toMillified(5)).to.equal('12.345M');
|
|
127
129
|
expect(BigNum.fromPrint('123456789').toMillified(5)).to.equal('123.45M');
|
|
130
|
+
|
|
131
|
+
expect(BigNum.from(-95, 2).print()).to.equal('-0.95');
|
|
132
|
+
|
|
133
|
+
// Case 6 strange numbers
|
|
134
|
+
expect(BigNum.from('-100', 2).print()).to.equal('-1.00');
|
|
135
|
+
expect(BigNum.from('-8402189', 13).print()).to.equal('-0.0000008402189');
|
|
136
|
+
expect(BigNum.from('-10000000000000', 13).print()).to.equal(
|
|
137
|
+
'-1.0000000000000'
|
|
138
|
+
);
|
|
139
|
+
expect(BigNum.from('-100', 6).print()).to.equal('-0.000100');
|
|
128
140
|
});
|
|
129
141
|
|
|
130
142
|
it('can initialise from string values correctly', () => {
|
|
@@ -133,8 +145,8 @@ describe('BigNum Tests', () => {
|
|
|
133
145
|
const baseAmountVal1 = '14.33';
|
|
134
146
|
const val1 = BigNum.fromPrint(baseAmountVal1, BASE_PRECISION_EXP);
|
|
135
147
|
|
|
136
|
-
expect(val1.toString()).to.equal('
|
|
137
|
-
expect(val1.print()).to.equal('14.
|
|
148
|
+
expect(val1.toString()).to.equal('14330000000');
|
|
149
|
+
expect(val1.print()).to.equal('14.330000000');
|
|
138
150
|
|
|
139
151
|
const baseAmountVal2 = '34.1';
|
|
140
152
|
const val2 = BigNum.fromPrint(baseAmountVal2, BASE_PRECISION_EXP);
|
|
@@ -216,7 +228,7 @@ describe('BigNum Tests', () => {
|
|
|
216
228
|
.shift(AMM_RESERVE_PRECISION_EXP)
|
|
217
229
|
.div(BigNum.from(entryPriceNum * 10 ** 8));
|
|
218
230
|
|
|
219
|
-
expect(val.toString()).to.equal('
|
|
231
|
+
expect(val.toString()).to.equal('25000000');
|
|
220
232
|
expect(val.printShort()).to.equal('0.025');
|
|
221
233
|
|
|
222
234
|
const val2 = BigNum.from(10000, 4);
|
|
@@ -251,5 +263,28 @@ describe('BigNum Tests', () => {
|
|
|
251
263
|
|
|
252
264
|
const val9 = BigNum.from('1000000000123', 6);
|
|
253
265
|
expect(val9.prettyPrint()).to.equal('1,000,000.000123');
|
|
266
|
+
|
|
267
|
+
const val10 = BigNum.from('100000000000', 6);
|
|
268
|
+
expect(val10.prettyPrint(true)).to.equal('100,000');
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
it('can round up and down', () => {
|
|
272
|
+
const val1 = BigNum.from('1234', 1);
|
|
273
|
+
expect(val1.toRounded(3).toString()).to.equal('1230');
|
|
274
|
+
|
|
275
|
+
const val2 = BigNum.from('1236', 1);
|
|
276
|
+
expect(val2.toRounded(3).toString()).to.equal('1240');
|
|
277
|
+
|
|
278
|
+
const val3 = BigNum.from('123456789', 5);
|
|
279
|
+
expect(val3.toRounded(4).print()).to.equal('1235.00000');
|
|
280
|
+
|
|
281
|
+
const val4 = BigNum.from('123456789', 5);
|
|
282
|
+
expect(val4.toRounded(3).print()).to.equal('1230.00000');
|
|
283
|
+
|
|
284
|
+
const val5 = BigNum.from('123000000', 5);
|
|
285
|
+
expect(val5.toRounded(3).print()).to.equal('1230.00000');
|
|
286
|
+
|
|
287
|
+
const val6 = BigNum.from('0', 5);
|
|
288
|
+
expect(val6.toRounded(3).print()).to.equal('0.00000');
|
|
254
289
|
});
|
|
255
290
|
});
|