@drift-labs/sdk 0.2.0-master.12 → 0.2.0-master.15
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/lib/accounts/bulkUserStatsSubscription.d.ts +7 -0
- package/lib/accounts/bulkUserStatsSubscription.js +21 -0
- 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/pollingUserStatsAccountSubscriber.d.ts +27 -0
- package/lib/accounts/pollingUserStatsAccountSubscriber.js +113 -0
- package/lib/accounts/types.d.ts +14 -2
- package/lib/accounts/webSocketUserStatsAccountSubsriber.d.ts +20 -0
- package/lib/accounts/webSocketUserStatsAccountSubsriber.js +47 -0
- package/lib/addresses/pda.d.ts +1 -0
- package/lib/addresses/pda.js +8 -1
- package/lib/admin.d.ts +2 -0
- package/lib/admin.js +18 -0
- package/lib/clearingHouse.d.ts +17 -1
- package/lib/clearingHouse.js +195 -14
- package/lib/clearingHouseConfig.d.ts +1 -0
- package/lib/clearingHouseUser.d.ts +5 -0
- package/lib/clearingHouseUser.js +97 -3
- package/lib/clearingHouseUserStats.d.ts +17 -0
- package/lib/clearingHouseUserStats.js +36 -0
- package/lib/clearingHouseUserStatsConfig.d.ts +14 -0
- package/{src/clearingHouseConfig.js → lib/clearingHouseUserStatsConfig.js} +0 -0
- package/lib/config.js +1 -1
- package/lib/factory/bigNum.js +7 -9
- package/lib/idl/clearing_house.json +620 -64
- package/lib/math/amm.js +2 -2
- package/lib/math/bankBalance.d.ts +4 -0
- package/lib/math/bankBalance.js +24 -2
- package/lib/math/oracles.d.ts +3 -0
- package/lib/math/oracles.js +25 -5
- package/lib/math/position.js +2 -1
- package/lib/math/trade.js +2 -2
- package/lib/types.d.ts +57 -9
- package/lib/types.js +6 -0
- package/package.json +1 -1
- package/src/accounts/bulkUserStatsSubscription.ts +33 -0
- package/src/accounts/bulkUserSubscription.ts +0 -1
- package/src/accounts/fetch.ts +27 -2
- package/src/accounts/pollingUserStatsAccountSubscriber.ts +172 -0
- package/src/accounts/types.ts +18 -0
- package/src/accounts/webSocketUserStatsAccountSubsriber.ts +80 -0
- package/src/addresses/pda.ts +13 -0
- package/src/admin.ts +29 -1
- package/src/clearingHouse.ts +318 -15
- package/src/clearingHouseConfig.ts +1 -0
- package/src/clearingHouseUser.ts +113 -10
- package/src/clearingHouseUserStats.ts +53 -0
- package/src/clearingHouseUserStatsConfig.ts +18 -0
- package/src/config.ts +1 -1
- package/src/factory/bigNum.ts +6 -7
- package/src/idl/clearing_house.json +620 -64
- package/src/math/amm.ts +8 -5
- package/src/math/bankBalance.ts +50 -1
- package/src/math/oracles.ts +42 -5
- package/src/math/position.ts +2 -1
- package/src/math/trade.ts +2 -2
- package/src/types.ts +61 -9
- package/tests/bn/test.ts +8 -0
- package/src/accounts/bulkAccountLoader.js +0 -197
- package/src/accounts/bulkUserSubscription.js +0 -33
- package/src/accounts/pollingClearingHouseAccountSubscriber.js +0 -311
- package/src/accounts/pollingOracleSubscriber.js +0 -93
- package/src/accounts/pollingTokenAccountSubscriber.js +0 -90
- package/src/accounts/pollingUserAccountSubscriber.js +0 -132
- package/src/accounts/types.js +0 -10
- package/src/accounts/utils.js +0 -7
- package/src/accounts/webSocketAccountSubscriber.js +0 -93
- package/src/accounts/webSocketClearingHouseAccountSubscriber.js +0 -233
- package/src/accounts/webSocketUserAccountSubscriber.js +0 -62
- package/src/addresses/marketAddresses.js +0 -26
- package/src/assert/assert.js +0 -9
- package/src/clearingHouseUserConfig.js +0 -2
- package/src/constants/banks.js +0 -42
- package/src/constants/markets.js +0 -42
- package/src/constants/numericConstants.js +0 -41
- package/src/events/eventList.js +0 -77
- package/src/events/eventSubscriber.js +0 -139
- package/src/events/fetchLogs.js +0 -50
- package/src/events/pollingLogProvider.js +0 -64
- package/src/events/sort.js +0 -44
- package/src/events/txEventCache.js +0 -71
- package/src/events/types.js +0 -20
- package/src/events/webSocketLogProvider.js +0 -41
- package/src/examples/makeTradeExample.js +0 -80
- package/src/factory/bigNum.js +0 -390
- package/src/factory/oracleClient.js +0 -20
- package/src/index.js +0 -69
- package/src/math/amm.js +0 -369
- package/src/math/auction.js +0 -42
- package/src/math/conversion.js +0 -11
- package/src/math/funding.js +0 -248
- package/src/math/oracles.js +0 -26
- package/src/math/repeg.js +0 -128
- package/src/math/state.js +0 -15
- package/src/math/trade.js +0 -253
- package/src/math/utils.js +0 -26
- package/src/math/utils.js.map +0 -1
- package/src/mockUSDCFaucet.js +0 -280
- package/src/oracles/oracleClientCache.js +0 -19
- package/src/oracles/pythClient.js +0 -46
- package/src/oracles/quoteAssetOracleClient.js +0 -32
- package/src/oracles/switchboardClient.js +0 -69
- package/src/oracles/types.js +0 -2
- package/src/orderParams.js +0 -20
- package/src/slot/SlotSubscriber.js +0 -39
- package/src/token/index.js +0 -38
- package/src/tokenFaucet.js +0 -189
- package/src/tx/types.js +0 -2
- package/src/tx/utils.js +0 -17
- package/src/types.js +0 -125
- package/src/userName.js +0 -20
- package/src/util/computeUnits.js +0 -27
- package/src/util/getTokenAddress.js +0 -9
- package/src/util/promiseTimeout.js +0 -14
- package/src/util/tps.js +0 -27
- package/src/wallet.js +0 -35
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ClearingHouseUserStats } from '../clearingHouseUserStats';
|
|
2
|
+
import { BulkAccountLoader } from './bulkAccountLoader';
|
|
3
|
+
/**
|
|
4
|
+
* @param userStats
|
|
5
|
+
* @param accountLoader
|
|
6
|
+
*/
|
|
7
|
+
export declare function bulkPollingUserStatsSubscribe(userStats: ClearingHouseUserStats[], accountLoader: BulkAccountLoader): Promise<void>;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.bulkPollingUserStatsSubscribe = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* @param userStats
|
|
6
|
+
* @param accountLoader
|
|
7
|
+
*/
|
|
8
|
+
async function bulkPollingUserStatsSubscribe(userStats, accountLoader) {
|
|
9
|
+
if (userStats.length === 0) {
|
|
10
|
+
await accountLoader.load();
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
await Promise.all(userStats.map((userStat) => {
|
|
14
|
+
return userStat.accountSubscriber.addToAccountLoader();
|
|
15
|
+
}));
|
|
16
|
+
await accountLoader.load();
|
|
17
|
+
await Promise.all(userStats.map(async (userStat) => {
|
|
18
|
+
return userStat.subscribe();
|
|
19
|
+
}));
|
|
20
|
+
}
|
|
21
|
+
exports.bulkPollingUserStatsSubscribe = bulkPollingUserStatsSubscribe;
|
|
@@ -11,7 +11,6 @@ async function bulkPollingUserSubscribe(users, accountLoader) {
|
|
|
11
11
|
return;
|
|
12
12
|
}
|
|
13
13
|
await Promise.all(users.map((user) => {
|
|
14
|
-
// Pull the keys from the authority map so we can skip fetching them in addToAccountLoader
|
|
15
14
|
return user.accountSubscriber.addToAccountLoader();
|
|
16
15
|
}));
|
|
17
16
|
await accountLoader.load();
|
package/lib/accounts/fetch.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Connection, PublicKey } from '@solana/web3.js';
|
|
2
|
-
import { UserAccount } from '../types';
|
|
2
|
+
import { UserAccount, UserStatsAccount } from '../types';
|
|
3
3
|
import { Program } from '@project-serum/anchor';
|
|
4
4
|
export declare function fetchUserAccounts(connection: Connection, program: Program, authority: PublicKey, limit?: number): Promise<(UserAccount | undefined)[]>;
|
|
5
|
+
export declare function fetchUserStatsAccount(connection: Connection, program: Program, authority: PublicKey): Promise<UserStatsAccount | undefined>;
|
package/lib/accounts/fetch.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.fetchUserAccounts = void 0;
|
|
3
|
+
exports.fetchUserStatsAccount = exports.fetchUserAccounts = void 0;
|
|
4
4
|
const pda_1 = require("../addresses/pda");
|
|
5
5
|
async function fetchUserAccounts(connection, program, authority, limit = 8) {
|
|
6
6
|
const userAccountPublicKeys = new Array();
|
|
@@ -16,3 +16,11 @@ async function fetchUserAccounts(connection, program, authority, limit = 8) {
|
|
|
16
16
|
});
|
|
17
17
|
}
|
|
18
18
|
exports.fetchUserAccounts = fetchUserAccounts;
|
|
19
|
+
async function fetchUserStatsAccount(connection, program, authority) {
|
|
20
|
+
const userStatsPublicKey = (0, pda_1.getUserStatsAccountPublicKey)(program.programId, authority);
|
|
21
|
+
const accountInfo = await connection.getAccountInfo(userStatsPublicKey, 'confirmed');
|
|
22
|
+
return accountInfo
|
|
23
|
+
? program.account.user.coder.accounts.decode('UserStats', accountInfo.data)
|
|
24
|
+
: undefined;
|
|
25
|
+
}
|
|
26
|
+
exports.fetchUserStatsAccount = fetchUserStatsAccount;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { DataAndSlot, AccountToPoll, UserStatsAccountSubscriber, UserStatsAccountEvents } from './types';
|
|
3
|
+
import { Program } from '@project-serum/anchor';
|
|
4
|
+
import StrictEventEmitter from 'strict-event-emitter-types';
|
|
5
|
+
import { EventEmitter } from 'events';
|
|
6
|
+
import { PublicKey } from '@solana/web3.js';
|
|
7
|
+
import { UserStatsAccount } from '../types';
|
|
8
|
+
import { BulkAccountLoader } from './bulkAccountLoader';
|
|
9
|
+
export declare class PollingUserStatsAccountSubscriber implements UserStatsAccountSubscriber {
|
|
10
|
+
isSubscribed: boolean;
|
|
11
|
+
program: Program;
|
|
12
|
+
eventEmitter: StrictEventEmitter<EventEmitter, UserStatsAccountEvents>;
|
|
13
|
+
userStatsAccountPublicKey: PublicKey;
|
|
14
|
+
accountLoader: BulkAccountLoader;
|
|
15
|
+
accountsToPoll: Map<string, AccountToPoll>;
|
|
16
|
+
errorCallbackId?: string;
|
|
17
|
+
userStats?: DataAndSlot<UserStatsAccount>;
|
|
18
|
+
constructor(program: Program, userStatsAccountPublicKey: PublicKey, accountLoader: BulkAccountLoader);
|
|
19
|
+
subscribe(): Promise<boolean>;
|
|
20
|
+
addToAccountLoader(): Promise<void>;
|
|
21
|
+
fetchIfUnloaded(): Promise<void>;
|
|
22
|
+
fetch(): Promise<void>;
|
|
23
|
+
didSubscriptionSucceed(): boolean;
|
|
24
|
+
unsubscribe(): Promise<void>;
|
|
25
|
+
assertIsSubscribed(): void;
|
|
26
|
+
getUserStatsAccountAndSlot(): DataAndSlot<UserStatsAccount>;
|
|
27
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PollingUserStatsAccountSubscriber = void 0;
|
|
4
|
+
const types_1 = require("./types");
|
|
5
|
+
const events_1 = require("events");
|
|
6
|
+
const utils_1 = require("./utils");
|
|
7
|
+
class PollingUserStatsAccountSubscriber {
|
|
8
|
+
constructor(program, userStatsAccountPublicKey, accountLoader) {
|
|
9
|
+
this.accountsToPoll = new Map();
|
|
10
|
+
this.isSubscribed = false;
|
|
11
|
+
this.program = program;
|
|
12
|
+
this.accountLoader = accountLoader;
|
|
13
|
+
this.eventEmitter = new events_1.EventEmitter();
|
|
14
|
+
this.userStatsAccountPublicKey = userStatsAccountPublicKey;
|
|
15
|
+
}
|
|
16
|
+
async subscribe() {
|
|
17
|
+
if (this.isSubscribed) {
|
|
18
|
+
return true;
|
|
19
|
+
}
|
|
20
|
+
await this.addToAccountLoader();
|
|
21
|
+
let subscriptionSucceeded = false;
|
|
22
|
+
let retries = 0;
|
|
23
|
+
while (!subscriptionSucceeded && retries < 5) {
|
|
24
|
+
await this.fetchIfUnloaded();
|
|
25
|
+
subscriptionSucceeded = this.didSubscriptionSucceed();
|
|
26
|
+
retries++;
|
|
27
|
+
}
|
|
28
|
+
if (subscriptionSucceeded) {
|
|
29
|
+
this.eventEmitter.emit('update');
|
|
30
|
+
}
|
|
31
|
+
this.isSubscribed = subscriptionSucceeded;
|
|
32
|
+
return subscriptionSucceeded;
|
|
33
|
+
}
|
|
34
|
+
async addToAccountLoader() {
|
|
35
|
+
if (this.accountsToPoll.size > 0) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
this.accountsToPoll.set(this.userStatsAccountPublicKey.toString(), {
|
|
39
|
+
key: 'userStats',
|
|
40
|
+
publicKey: this.userStatsAccountPublicKey,
|
|
41
|
+
eventType: 'userStatsAccountUpdate',
|
|
42
|
+
});
|
|
43
|
+
for (const [_, accountToPoll] of this.accountsToPoll) {
|
|
44
|
+
accountToPoll.callbackId = this.accountLoader.addAccount(accountToPoll.publicKey, (buffer, slot) => {
|
|
45
|
+
if (!buffer) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
const account = this.program.account[accountToPoll.key].coder.accounts.decode((0, utils_1.capitalize)(accountToPoll.key), buffer);
|
|
49
|
+
this[accountToPoll.key] = { data: account, slot };
|
|
50
|
+
// @ts-ignore
|
|
51
|
+
this.eventEmitter.emit(accountToPoll.eventType, account);
|
|
52
|
+
this.eventEmitter.emit('update');
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
this.errorCallbackId = this.accountLoader.addErrorCallbacks((error) => {
|
|
56
|
+
this.eventEmitter.emit('error', error);
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
async fetchIfUnloaded() {
|
|
60
|
+
let shouldFetch = false;
|
|
61
|
+
for (const [_, accountToPoll] of this.accountsToPoll) {
|
|
62
|
+
if (this[accountToPoll.key] === undefined) {
|
|
63
|
+
shouldFetch = true;
|
|
64
|
+
break;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
if (shouldFetch) {
|
|
68
|
+
await this.fetch();
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
async fetch() {
|
|
72
|
+
await this.accountLoader.load();
|
|
73
|
+
for (const [_, accountToPoll] of this.accountsToPoll) {
|
|
74
|
+
const { buffer, slot } = this.accountLoader.getBufferAndSlot(accountToPoll.publicKey);
|
|
75
|
+
if (buffer) {
|
|
76
|
+
const account = this.program.account[accountToPoll.key].coder.accounts.decode((0, utils_1.capitalize)(accountToPoll.key), buffer);
|
|
77
|
+
this[accountToPoll.key] = { data: account, slot };
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
didSubscriptionSucceed() {
|
|
82
|
+
let success = true;
|
|
83
|
+
for (const [_, accountToPoll] of this.accountsToPoll) {
|
|
84
|
+
if (!this[accountToPoll.key]) {
|
|
85
|
+
success = false;
|
|
86
|
+
break;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return success;
|
|
90
|
+
}
|
|
91
|
+
async unsubscribe() {
|
|
92
|
+
if (!this.isSubscribed) {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
for (const [_, accountToPoll] of this.accountsToPoll) {
|
|
96
|
+
this.accountLoader.removeAccount(accountToPoll.publicKey, accountToPoll.callbackId);
|
|
97
|
+
}
|
|
98
|
+
this.accountLoader.removeErrorCallbacks(this.errorCallbackId);
|
|
99
|
+
this.errorCallbackId = undefined;
|
|
100
|
+
this.accountsToPoll.clear();
|
|
101
|
+
this.isSubscribed = false;
|
|
102
|
+
}
|
|
103
|
+
assertIsSubscribed() {
|
|
104
|
+
if (!this.isSubscribed) {
|
|
105
|
+
throw new types_1.NotSubscribedError('You must call `subscribe` before using this function');
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
getUserStatsAccountAndSlot() {
|
|
109
|
+
this.assertIsSubscribed();
|
|
110
|
+
return this.userStats;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
exports.PollingUserStatsAccountSubscriber = PollingUserStatsAccountSubscriber;
|
package/lib/accounts/types.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
/// <reference types="bn.js" />
|
|
3
|
-
|
|
4
|
-
import { BankAccount, MarketAccount, OracleSource, StateAccount, UserAccount } from '../types';
|
|
3
|
+
import { BankAccount, MarketAccount, OracleSource, StateAccount, UserAccount, UserStatsAccount } from '../types';
|
|
5
4
|
import StrictEventEmitter from 'strict-event-emitter-types';
|
|
6
5
|
import { EventEmitter } from 'events';
|
|
7
6
|
import { PublicKey } from '@solana/web3.js';
|
|
@@ -100,3 +99,16 @@ export declare type DataAndSlot<T> = {
|
|
|
100
99
|
data: T;
|
|
101
100
|
slot: number;
|
|
102
101
|
};
|
|
102
|
+
export interface UserStatsAccountEvents {
|
|
103
|
+
userStatsAccountUpdate: (payload: UserStatsAccount) => void;
|
|
104
|
+
update: void;
|
|
105
|
+
error: (e: Error) => void;
|
|
106
|
+
}
|
|
107
|
+
export interface UserStatsAccountSubscriber {
|
|
108
|
+
eventEmitter: StrictEventEmitter<EventEmitter, UserStatsAccountEvents>;
|
|
109
|
+
isSubscribed: boolean;
|
|
110
|
+
subscribe(): Promise<boolean>;
|
|
111
|
+
fetch(): Promise<void>;
|
|
112
|
+
unsubscribe(): Promise<void>;
|
|
113
|
+
getUserStatsAccountAndSlot(): DataAndSlot<UserStatsAccount>;
|
|
114
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { DataAndSlot, AccountSubscriber, UserStatsAccountSubscriber, UserStatsAccountEvents } from './types';
|
|
3
|
+
import { Program } from '@project-serum/anchor';
|
|
4
|
+
import StrictEventEmitter from 'strict-event-emitter-types';
|
|
5
|
+
import { EventEmitter } from 'events';
|
|
6
|
+
import { PublicKey } from '@solana/web3.js';
|
|
7
|
+
import { UserStatsAccount } from '../types';
|
|
8
|
+
export declare class WebSocketUserStatsAccountSubscriber implements UserStatsAccountSubscriber {
|
|
9
|
+
isSubscribed: boolean;
|
|
10
|
+
program: Program;
|
|
11
|
+
eventEmitter: StrictEventEmitter<EventEmitter, UserStatsAccountEvents>;
|
|
12
|
+
userStatsAccountPublicKey: PublicKey;
|
|
13
|
+
userStatsAccountSubscriber: AccountSubscriber<UserStatsAccount>;
|
|
14
|
+
constructor(program: Program, userStatsAccountPublicKey: PublicKey);
|
|
15
|
+
subscribe(): Promise<boolean>;
|
|
16
|
+
fetch(): Promise<void>;
|
|
17
|
+
unsubscribe(): Promise<void>;
|
|
18
|
+
assertIsSubscribed(): void;
|
|
19
|
+
getUserStatsAccountAndSlot(): DataAndSlot<UserStatsAccount>;
|
|
20
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.WebSocketUserStatsAccountSubscriber = void 0;
|
|
4
|
+
const types_1 = require("./types");
|
|
5
|
+
const events_1 = require("events");
|
|
6
|
+
const webSocketAccountSubscriber_1 = require("./webSocketAccountSubscriber");
|
|
7
|
+
class WebSocketUserStatsAccountSubscriber {
|
|
8
|
+
constructor(program, userStatsAccountPublicKey) {
|
|
9
|
+
this.isSubscribed = false;
|
|
10
|
+
this.program = program;
|
|
11
|
+
this.userStatsAccountPublicKey = userStatsAccountPublicKey;
|
|
12
|
+
this.eventEmitter = new events_1.EventEmitter();
|
|
13
|
+
}
|
|
14
|
+
async subscribe() {
|
|
15
|
+
if (this.isSubscribed) {
|
|
16
|
+
return true;
|
|
17
|
+
}
|
|
18
|
+
this.userStatsAccountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('userStats', this.program, this.userStatsAccountPublicKey);
|
|
19
|
+
await this.userStatsAccountSubscriber.subscribe((data) => {
|
|
20
|
+
this.eventEmitter.emit('userStatsAccountUpdate', data);
|
|
21
|
+
this.eventEmitter.emit('update');
|
|
22
|
+
});
|
|
23
|
+
this.eventEmitter.emit('update');
|
|
24
|
+
this.isSubscribed = true;
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
async fetch() {
|
|
28
|
+
await Promise.all([this.userStatsAccountSubscriber.fetch()]);
|
|
29
|
+
}
|
|
30
|
+
async unsubscribe() {
|
|
31
|
+
if (!this.isSubscribed) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
await Promise.all([this.userStatsAccountSubscriber.unsubscribe()]);
|
|
35
|
+
this.isSubscribed = false;
|
|
36
|
+
}
|
|
37
|
+
assertIsSubscribed() {
|
|
38
|
+
if (!this.isSubscribed) {
|
|
39
|
+
throw new types_1.NotSubscribedError('You must call `subscribe` before using this function');
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
getUserStatsAccountAndSlot() {
|
|
43
|
+
this.assertIsSubscribed();
|
|
44
|
+
return this.userStatsAccountSubscriber.dataAndSlot;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
exports.WebSocketUserStatsAccountSubscriber = WebSocketUserStatsAccountSubscriber;
|
package/lib/addresses/pda.d.ts
CHANGED
|
@@ -6,6 +6,7 @@ export declare function getClearingHouseStateAccountPublicKey(programId: PublicK
|
|
|
6
6
|
export declare function getUserAccountPublicKeyAndNonce(programId: PublicKey, authority: PublicKey, userId?: number): Promise<[PublicKey, number]>;
|
|
7
7
|
export declare function getUserAccountPublicKey(programId: PublicKey, authority: PublicKey, userId?: number): Promise<PublicKey>;
|
|
8
8
|
export declare function getUserAccountPublicKeySync(programId: PublicKey, authority: PublicKey, userId?: number): PublicKey;
|
|
9
|
+
export declare function getUserStatsAccountPublicKey(programId: PublicKey, authority: PublicKey): PublicKey;
|
|
9
10
|
export declare function getMarketPublicKey(programId: PublicKey, marketIndex: BN): Promise<PublicKey>;
|
|
10
11
|
export declare function getBankPublicKey(programId: PublicKey, bankIndex: BN): Promise<PublicKey>;
|
|
11
12
|
export declare function getBankVaultPublicKey(programId: PublicKey, bankIndex: BN): Promise<PublicKey>;
|
package/lib/addresses/pda.js
CHANGED
|
@@ -23,7 +23,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.getBankVaultAuthorityPublicKey = exports.getBankVaultPublicKey = exports.getBankPublicKey = exports.getMarketPublicKey = exports.getUserAccountPublicKeySync = exports.getUserAccountPublicKey = exports.getUserAccountPublicKeyAndNonce = exports.getClearingHouseStateAccountPublicKey = exports.getClearingHouseStateAccountPublicKeyAndNonce = void 0;
|
|
26
|
+
exports.getBankVaultAuthorityPublicKey = exports.getBankVaultPublicKey = exports.getBankPublicKey = exports.getMarketPublicKey = exports.getUserStatsAccountPublicKey = exports.getUserAccountPublicKeySync = exports.getUserAccountPublicKey = exports.getUserAccountPublicKeyAndNonce = exports.getClearingHouseStateAccountPublicKey = exports.getClearingHouseStateAccountPublicKeyAndNonce = void 0;
|
|
27
27
|
const anchor = __importStar(require("@project-serum/anchor"));
|
|
28
28
|
async function getClearingHouseStateAccountPublicKeyAndNonce(programId) {
|
|
29
29
|
return anchor.web3.PublicKey.findProgramAddress([Buffer.from(anchor.utils.bytes.utf8.encode('clearing_house'))], programId);
|
|
@@ -53,6 +53,13 @@ function getUserAccountPublicKeySync(programId, authority, userId = 0) {
|
|
|
53
53
|
], programId)[0];
|
|
54
54
|
}
|
|
55
55
|
exports.getUserAccountPublicKeySync = getUserAccountPublicKeySync;
|
|
56
|
+
function getUserStatsAccountPublicKey(programId, authority) {
|
|
57
|
+
return anchor.web3.PublicKey.findProgramAddressSync([
|
|
58
|
+
Buffer.from(anchor.utils.bytes.utf8.encode('user_stats')),
|
|
59
|
+
authority.toBuffer(),
|
|
60
|
+
], programId)[0];
|
|
61
|
+
}
|
|
62
|
+
exports.getUserStatsAccountPublicKey = getUserStatsAccountPublicKey;
|
|
56
63
|
async function getMarketPublicKey(programId, marketIndex) {
|
|
57
64
|
return (await anchor.web3.PublicKey.findProgramAddress([
|
|
58
65
|
Buffer.from(anchor.utils.bytes.utf8.encode('market')),
|
package/lib/admin.d.ts
CHANGED
|
@@ -29,6 +29,8 @@ export declare class Admin extends ClearingHouse {
|
|
|
29
29
|
updateOrderFillerRewardStructure(orderFillerRewardStructure: OrderFillerRewardStructure): Promise<TransactionSignature>;
|
|
30
30
|
updateFee(fees: FeeStructure): Promise<TransactionSignature>;
|
|
31
31
|
updateOracleGuardRails(oracleGuardRails: OracleGuardRails): Promise<TransactionSignature>;
|
|
32
|
+
updateBankWithdrawGuardThreshold(bankIndex: BN, withdrawGuardThreshold: BN): Promise<TransactionSignature>;
|
|
33
|
+
updateLpCooldownTime(marketIndex: BN, cooldownTime: BN): Promise<TransactionSignature>;
|
|
32
34
|
updateMarketOracle(marketIndex: BN, oracle: PublicKey, oracleSource: OracleSource): Promise<TransactionSignature>;
|
|
33
35
|
updateMarketMinimumQuoteAssetTradeSize(marketIndex: BN, minimumTradeSize: BN): Promise<TransactionSignature>;
|
|
34
36
|
updateMarketBaseAssetAmountStepSize(marketIndex: BN, stepSize: BN): Promise<TransactionSignature>;
|
package/lib/admin.js
CHANGED
|
@@ -333,6 +333,24 @@ class Admin extends clearingHouse_1.ClearingHouse {
|
|
|
333
333
|
},
|
|
334
334
|
});
|
|
335
335
|
}
|
|
336
|
+
async updateBankWithdrawGuardThreshold(bankIndex, withdrawGuardThreshold) {
|
|
337
|
+
return await this.program.rpc.updateBankWithdrawGuardThreshold(withdrawGuardThreshold, {
|
|
338
|
+
accounts: {
|
|
339
|
+
admin: this.wallet.publicKey,
|
|
340
|
+
state: await this.getStatePublicKey(),
|
|
341
|
+
bank: await (0, pda_1.getBankPublicKey)(this.program.programId, bankIndex),
|
|
342
|
+
},
|
|
343
|
+
});
|
|
344
|
+
}
|
|
345
|
+
async updateLpCooldownTime(marketIndex, cooldownTime) {
|
|
346
|
+
return await this.program.rpc.updateLpCooldownTime(cooldownTime, {
|
|
347
|
+
accounts: {
|
|
348
|
+
admin: this.wallet.publicKey,
|
|
349
|
+
state: await this.getStatePublicKey(),
|
|
350
|
+
market: await (0, pda_1.getMarketPublicKey)(this.program.programId, marketIndex),
|
|
351
|
+
},
|
|
352
|
+
});
|
|
353
|
+
}
|
|
336
354
|
async updateMarketOracle(marketIndex, oracle, oracleSource) {
|
|
337
355
|
return await this.program.rpc.updateMarketOracle(oracle, oracleSource, {
|
|
338
356
|
accounts: {
|
package/lib/clearingHouse.d.ts
CHANGED
|
@@ -12,6 +12,7 @@ import { OraclePriceData } from './oracles/types';
|
|
|
12
12
|
import { ClearingHouseConfig } from './clearingHouseConfig';
|
|
13
13
|
import { ClearingHouseUser } from './clearingHouseUser';
|
|
14
14
|
import { ClearingHouseUserAccountSubscriptionConfig } from './clearingHouseUserConfig';
|
|
15
|
+
import { ClearingHouseUserStats } from './clearingHouseUserStats';
|
|
15
16
|
/**
|
|
16
17
|
* # ClearingHouse
|
|
17
18
|
* This class is the main way to interact with Drift Protocol. It allows you to subscribe to the various accounts where the Market's state is stored, as well as: opening positions, liquidating, settling funding, depositing & withdrawing, and more.
|
|
@@ -23,6 +24,7 @@ export declare class ClearingHouse {
|
|
|
23
24
|
provider: AnchorProvider;
|
|
24
25
|
opts?: ConfirmOptions;
|
|
25
26
|
users: Map<number, ClearingHouseUser>;
|
|
27
|
+
userStats?: ClearingHouseUserStats;
|
|
26
28
|
activeUserId: number;
|
|
27
29
|
userAccountSubscriptionConfig: ClearingHouseUserAccountSubscriptionConfig;
|
|
28
30
|
accountSubscriber: ClearingHouseAccountSubscriber;
|
|
@@ -62,8 +64,12 @@ export declare class ClearingHouse {
|
|
|
62
64
|
addUser(userId: number): Promise<void>;
|
|
63
65
|
initializeUserAccount(userId?: number, name?: string): Promise<[TransactionSignature, PublicKey]>;
|
|
64
66
|
getInitializeUserInstructions(userId?: number, name?: string): Promise<[PublicKey, TransactionInstruction]>;
|
|
67
|
+
getInitializeUserStatsIx(): Promise<TransactionInstruction>;
|
|
65
68
|
getUser(userId?: number): ClearingHouseUser;
|
|
66
69
|
getUsers(): ClearingHouseUser[];
|
|
70
|
+
getUserStats(): ClearingHouseUserStats;
|
|
71
|
+
userStatsAccountPublicKey: PublicKey;
|
|
72
|
+
getUserStatsAccountPublicKey(): PublicKey;
|
|
67
73
|
getUserAccountPublicKey(): Promise<PublicKey>;
|
|
68
74
|
getUserAccount(userId?: number): UserAccount | undefined;
|
|
69
75
|
getUserAccountAndSlot(userId?: number): DataAndSlot<UserAccount> | undefined;
|
|
@@ -99,6 +105,12 @@ export declare class ClearingHouse {
|
|
|
99
105
|
getTransferDepositIx(amount: BN, bankIndex: BN, fromUserId: number, toUserId: number): Promise<TransactionInstruction>;
|
|
100
106
|
updateBankCumulativeInterest(bankIndex: BN): Promise<TransactionSignature>;
|
|
101
107
|
updateBankCumulativeInterestIx(bankIndex: BN): Promise<TransactionInstruction>;
|
|
108
|
+
settleLP(settleeUserAccountPublicKey: PublicKey, marketIndex: BN): Promise<TransactionSignature>;
|
|
109
|
+
settleLPIx(settleeUserAccountPublicKey: PublicKey, marketIndex: BN): Promise<TransactionInstruction>;
|
|
110
|
+
removeLiquidity(marketIndex: BN, sharesToBurn?: BN): Promise<TransactionSignature>;
|
|
111
|
+
getRemoveLiquidityIx(marketIndex: BN, sharesToBurn?: BN): Promise<TransactionInstruction>;
|
|
112
|
+
addLiquidity(amount: BN, marketIndex: BN): Promise<TransactionSignature>;
|
|
113
|
+
getAddLiquidityIx(amount: BN, marketIndex: BN): Promise<TransactionInstruction>;
|
|
102
114
|
openPosition(direction: PositionDirection, amount: BN, marketIndex: BN, limitPrice?: BN): Promise<TransactionSignature>;
|
|
103
115
|
placeOrder(orderParams: OptionalOrderParams): Promise<TransactionSignature>;
|
|
104
116
|
getOrderParams(optionalOrderParams: OptionalOrderParams): OrderParams;
|
|
@@ -122,7 +134,7 @@ export declare class ClearingHouse {
|
|
|
122
134
|
* @param marketIndex
|
|
123
135
|
* @returns
|
|
124
136
|
*/
|
|
125
|
-
closePosition(marketIndex: BN): Promise<TransactionSignature>;
|
|
137
|
+
closePosition(marketIndex: BN, limitPrice?: BN): Promise<TransactionSignature>;
|
|
126
138
|
settlePNLs(users: {
|
|
127
139
|
settleeUserAccountPublicKey: PublicKey;
|
|
128
140
|
settleeUserAccount: UserAccount;
|
|
@@ -137,6 +149,10 @@ export declare class ClearingHouse {
|
|
|
137
149
|
getLiquidateBorrowForPerpPnlIx(userAccountPublicKey: PublicKey, userAccount: UserAccount, perpMarketIndex: BN, liabilityBankIndex: BN, maxLiabilityTransfer: BN): Promise<TransactionInstruction>;
|
|
138
150
|
liquidatePerpPnlForDeposit(userAccountPublicKey: PublicKey, userAccount: UserAccount, perpMarketIndex: BN, assetBankIndex: BN, maxPnlTransfer: BN): Promise<TransactionSignature>;
|
|
139
151
|
getLiquidatePerpPnlForDepositIx(userAccountPublicKey: PublicKey, userAccount: UserAccount, perpMarketIndex: BN, assetBankIndex: BN, maxPnlTransfer: BN): Promise<TransactionInstruction>;
|
|
152
|
+
resolvePerpBankruptcy(userAccountPublicKey: PublicKey, userAccount: UserAccount, marketIndex: BN): Promise<TransactionSignature>;
|
|
153
|
+
getResolvePerpBankruptcyIx(userAccountPublicKey: PublicKey, userAccount: UserAccount, marketIndex: BN): Promise<TransactionInstruction>;
|
|
154
|
+
resolveBorrowBankruptcy(userAccountPublicKey: PublicKey, userAccount: UserAccount, bankIndex: BN): Promise<TransactionSignature>;
|
|
155
|
+
getResolveBorrowBankruptcyIx(userAccountPublicKey: PublicKey, userAccount: UserAccount, bankIndex: BN): Promise<TransactionInstruction>;
|
|
140
156
|
getRemainingAccountsForLiquidation(params: {
|
|
141
157
|
userAccount: UserAccount;
|
|
142
158
|
writableMarketIndex?: BN;
|