@drift-labs/sdk 0.2.0-master.11 → 0.2.0-master.14
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/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 -1
- 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 +24 -3
- package/lib/clearingHouse.js +352 -51
- package/lib/clearingHouseConfig.d.ts +1 -0
- package/lib/clearingHouseUser.d.ts +15 -15
- package/lib/clearingHouseUser.js +185 -73
- package/lib/clearingHouseUserStats.d.ts +17 -0
- package/lib/clearingHouseUserStats.js +36 -0
- package/lib/clearingHouseUserStatsConfig.d.ts +14 -0
- package/lib/clearingHouseUserStatsConfig.js +2 -0
- package/lib/config.js +1 -1
- package/lib/constants/banks.d.ts +2 -2
- package/lib/constants/banks.js +4 -3
- package/lib/constants/numericConstants.d.ts +2 -0
- package/lib/constants/numericConstants.js +3 -1
- package/lib/events/eventList.js +3 -0
- package/lib/events/types.d.ts +2 -1
- package/lib/factory/bigNum.d.ts +1 -0
- package/lib/factory/bigNum.js +37 -11
- package/lib/idl/clearing_house.json +692 -58
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/math/amm.js +2 -2
- package/lib/math/bankBalance.d.ts +7 -1
- package/lib/math/bankBalance.js +76 -1
- package/lib/math/margin.d.ts +11 -0
- package/lib/math/margin.js +72 -0
- package/lib/math/market.d.ts +4 -1
- package/lib/math/market.js +35 -1
- package/lib/math/oracles.d.ts +3 -0
- package/lib/math/oracles.js +25 -5
- package/lib/math/position.d.ts +8 -0
- package/lib/math/position.js +43 -12
- package/lib/math/trade.js +2 -2
- package/lib/orders.d.ts +1 -2
- package/lib/orders.js +2 -77
- package/lib/tokenFaucet.d.ts +1 -0
- package/lib/tokenFaucet.js +23 -12
- package/lib/tx/retryTxSender.js +9 -2
- package/lib/types.d.ts +78 -10
- package/lib/types.js +12 -0
- package/lib/util/getTokenAddress.d.ts +2 -0
- package/lib/util/getTokenAddress.js +9 -0
- package/package.json +1 -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 +619 -62
- package/src/clearingHouseConfig.ts +1 -0
- package/src/clearingHouseUser.ts +317 -105
- package/src/clearingHouseUserStats.ts +53 -0
- package/src/clearingHouseUserStatsConfig.ts +18 -0
- package/src/config.ts +1 -1
- package/src/constants/banks.js +42 -0
- package/src/constants/banks.ts +6 -3
- package/src/constants/markets.js +42 -0
- package/src/constants/numericConstants.js +41 -0
- package/src/constants/numericConstants.ts +3 -0
- package/src/events/eventList.ts +3 -0
- package/src/events/types.ts +2 -0
- package/src/factory/bigNum.js +37 -11
- package/src/factory/bigNum.ts +43 -13
- package/src/idl/clearing_house.json +692 -58
- package/src/index.ts +1 -0
- package/src/math/amm.ts +8 -5
- package/src/math/bankBalance.ts +147 -1
- package/src/math/margin.ts +124 -0
- package/src/math/market.ts +66 -1
- package/src/math/oracles.ts +42 -5
- package/src/math/position.ts +60 -9
- package/src/math/trade.ts +2 -2
- package/src/orders.ts +4 -157
- package/src/tokenFaucet.js +189 -0
- package/src/tokenFaucet.ts +38 -15
- package/src/tx/retryTxSender.ts +11 -3
- package/src/types.js +12 -1
- package/src/types.ts +83 -10
- package/src/{accounts/fetch.js → util/computeUnits.js} +11 -13
- package/src/util/getTokenAddress.js +9 -0
- package/src/util/getTokenAddress.ts +18 -0
- package/tests/bn/test.ts +2 -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/pda.js +0 -104
- package/src/index.js +0 -100
- package/src/math/bankBalance.js +0 -75
- package/src/math/market.js +0 -57
- package/src/math/orders.js +0 -110
- package/src/math/position.js +0 -140
- package/src/mockUSDCFaucet.js +0 -280
- package/src/orders.js +0 -134
- package/src/tx/retryTxSender.js +0 -188
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,7 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
/// <reference types="bn.js" />
|
|
3
3
|
/// <reference types="node" />
|
|
4
|
-
import { BankAccount, MarketAccount, OracleSource, StateAccount, UserAccount } from '../types';
|
|
4
|
+
import { BankAccount, MarketAccount, OracleSource, StateAccount, UserAccount, UserStatsAccount } from '../types';
|
|
5
5
|
import StrictEventEmitter from 'strict-event-emitter-types';
|
|
6
6
|
import { EventEmitter } from 'events';
|
|
7
7
|
import { PublicKey } from '@solana/web3.js';
|
|
@@ -100,3 +100,16 @@ export declare type DataAndSlot<T> = {
|
|
|
100
100
|
data: T;
|
|
101
101
|
slot: number;
|
|
102
102
|
};
|
|
103
|
+
export interface UserStatsAccountEvents {
|
|
104
|
+
userStatsAccountUpdate: (payload: UserStatsAccount) => void;
|
|
105
|
+
update: void;
|
|
106
|
+
error: (e: Error) => void;
|
|
107
|
+
}
|
|
108
|
+
export interface UserStatsAccountSubscriber {
|
|
109
|
+
eventEmitter: StrictEventEmitter<EventEmitter, UserStatsAccountEvents>;
|
|
110
|
+
isSubscribed: boolean;
|
|
111
|
+
subscribe(): Promise<boolean>;
|
|
112
|
+
fetch(): Promise<void>;
|
|
113
|
+
unsubscribe(): Promise<void>;
|
|
114
|
+
getUserStatsAccountAndSlot(): DataAndSlot<UserStatsAccount>;
|
|
115
|
+
}
|
|
@@ -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;
|
|
@@ -72,17 +78,22 @@ export declare class ClearingHouse {
|
|
|
72
78
|
getRemainingAccounts(params: {
|
|
73
79
|
writableMarketIndex?: BN;
|
|
74
80
|
writableBankIndex?: BN;
|
|
81
|
+
readableMarketIndex?: BN;
|
|
75
82
|
}): AccountMeta[];
|
|
76
83
|
getOrder(orderId: BN | number): Order | undefined;
|
|
77
84
|
getOrderByUserId(userOrderId: number): Order | undefined;
|
|
78
85
|
deposit(amount: BN, bankIndex: BN, collateralAccountPublicKey: PublicKey, userId?: number, reduceOnly?: boolean): Promise<TransactionSignature>;
|
|
79
86
|
getDepositInstruction(amount: BN, bankIndex: BN, userTokenAccount: PublicKey, userId?: number, reduceOnly?: boolean, userInitialized?: boolean): Promise<TransactionInstruction>;
|
|
87
|
+
private checkIfAccountExists;
|
|
88
|
+
private getSolWithdrawalIxs;
|
|
89
|
+
private getWrappedSolAccountCreationIxs;
|
|
80
90
|
/**
|
|
81
91
|
* Creates the Clearing House User account for a user, and deposits some initial collateral
|
|
82
|
-
* @param userId
|
|
83
|
-
* @param name
|
|
84
92
|
* @param amount
|
|
85
93
|
* @param userTokenAccount
|
|
94
|
+
* @param bankIndex
|
|
95
|
+
* @param userId
|
|
96
|
+
* @param name
|
|
86
97
|
* @param fromUserId
|
|
87
98
|
* @returns
|
|
88
99
|
*/
|
|
@@ -94,6 +105,12 @@ export declare class ClearingHouse {
|
|
|
94
105
|
getTransferDepositIx(amount: BN, bankIndex: BN, fromUserId: number, toUserId: number): Promise<TransactionInstruction>;
|
|
95
106
|
updateBankCumulativeInterest(bankIndex: BN): Promise<TransactionSignature>;
|
|
96
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>;
|
|
97
114
|
openPosition(direction: PositionDirection, amount: BN, marketIndex: BN, limitPrice?: BN): Promise<TransactionSignature>;
|
|
98
115
|
placeOrder(orderParams: OptionalOrderParams): Promise<TransactionSignature>;
|
|
99
116
|
getOrderParams(optionalOrderParams: OptionalOrderParams): OrderParams;
|
|
@@ -117,7 +134,7 @@ export declare class ClearingHouse {
|
|
|
117
134
|
* @param marketIndex
|
|
118
135
|
* @returns
|
|
119
136
|
*/
|
|
120
|
-
closePosition(marketIndex: BN): Promise<TransactionSignature>;
|
|
137
|
+
closePosition(marketIndex: BN, limitPrice?: BN): Promise<TransactionSignature>;
|
|
121
138
|
settlePNLs(users: {
|
|
122
139
|
settleeUserAccountPublicKey: PublicKey;
|
|
123
140
|
settleeUserAccount: UserAccount;
|
|
@@ -132,6 +149,10 @@ export declare class ClearingHouse {
|
|
|
132
149
|
getLiquidateBorrowForPerpPnlIx(userAccountPublicKey: PublicKey, userAccount: UserAccount, perpMarketIndex: BN, liabilityBankIndex: BN, maxLiabilityTransfer: BN): Promise<TransactionInstruction>;
|
|
133
150
|
liquidatePerpPnlForDeposit(userAccountPublicKey: PublicKey, userAccount: UserAccount, perpMarketIndex: BN, assetBankIndex: BN, maxPnlTransfer: BN): Promise<TransactionSignature>;
|
|
134
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>;
|
|
135
156
|
getRemainingAccountsForLiquidation(params: {
|
|
136
157
|
userAccount: UserAccount;
|
|
137
158
|
writableMarketIndex?: BN;
|