@drift-labs/sdk 2.14.0-beta.0 → 2.15.0-beta.0
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/bulkAccountLoader.d.ts +0 -1
- package/lib/accounts/bulkAccountLoader.js +3 -3
- package/lib/accounts/fetch.js +2 -2
- package/lib/accounts/pollingDriftClientAccountSubscriber.js +7 -7
- package/lib/accounts/pollingTokenAccountSubscriber.js +2 -2
- package/lib/accounts/pollingUserAccountSubscriber.js +2 -2
- package/lib/accounts/pollingUserStatsAccountSubscriber.js +2 -2
- package/lib/accounts/types.d.ts +0 -1
- package/lib/accounts/webSocketAccountSubscriber.js +1 -1
- package/lib/accounts/webSocketDriftClientAccountSubscriber.js +3 -3
- package/lib/addresses/marketAddresses.js +1 -1
- package/lib/addresses/pda.js +1 -5
- package/lib/adminClient.js +57 -61
- package/lib/constants/numericConstants.d.ts +1 -0
- package/lib/constants/numericConstants.js +3 -2
- package/lib/constants/perpMarkets.js +10 -0
- package/lib/dlob/DLOB.js +68 -68
- package/lib/dlob/DLOBNode.js +7 -7
- package/lib/dlob/NodeList.js +2 -2
- package/lib/driftClient.js +81 -85
- package/lib/events/eventSubscriber.js +2 -2
- package/lib/events/pollingLogProvider.js +1 -1
- package/lib/examples/loadDlob.js +2 -2
- package/lib/examples/makeTradeExample.js +9 -9
- package/lib/factory/bigNum.js +9 -9
- package/lib/factory/oracleClient.js +2 -2
- package/lib/idl/drift.json +1 -1
- package/lib/index.js +1 -5
- package/lib/math/amm.js +23 -23
- package/lib/math/auction.js +6 -6
- package/lib/math/exchangeStatus.js +2 -2
- package/lib/math/funding.js +2 -2
- package/lib/math/margin.js +5 -5
- package/lib/math/market.js +13 -13
- package/lib/math/oracles.js +1 -1
- package/lib/math/orders.js +23 -23
- package/lib/math/position.js +5 -5
- package/lib/math/repeg.js +1 -1
- package/lib/math/spotBalance.js +8 -8
- package/lib/math/spotPosition.js +3 -3
- package/lib/math/trade.js +42 -42
- package/lib/oracles/oracleClientCache.js +1 -1
- package/lib/oracles/pythClient.js +1 -1
- package/lib/tokenFaucet.js +1 -5
- package/lib/tx/retryTxSender.js +1 -1
- package/lib/user.js +52 -52
- package/lib/userMap/userMap.js +1 -1
- package/lib/userMap/userStatsMap.js +3 -3
- package/lib/userStats.js +2 -2
- package/package.json +1 -1
- package/src/assert/assert.js +9 -0
- package/src/constants/numericConstants.ts +1 -0
- package/src/constants/perpMarkets.ts +10 -0
- package/src/dlob/DLOB.ts +1 -1
- package/src/idl/drift.json +1 -1
- package/src/token/index.js +38 -0
- package/src/tx/types.js +2 -0
- package/src/tx/utils.js +17 -0
- package/src/user.ts +2 -1
- package/src/util/computeUnits.js +27 -0
- package/src/util/getTokenAddress.js +9 -0
- package/src/util/promiseTimeout.js +14 -0
- package/src/util/tps.js +27 -0
|
@@ -18,7 +18,7 @@ class BulkAccountLoader {
|
|
|
18
18
|
}
|
|
19
19
|
async addAccount(publicKey, callback) {
|
|
20
20
|
const existingSize = this.accountsToLoad.size;
|
|
21
|
-
const callbackId =
|
|
21
|
+
const callbackId = uuid_1.v4();
|
|
22
22
|
const existingAccountToLoad = this.accountsToLoad.get(publicKey.toString());
|
|
23
23
|
if (existingAccountToLoad) {
|
|
24
24
|
existingAccountToLoad.callbacks.set(callbackId, callback);
|
|
@@ -52,7 +52,7 @@ class BulkAccountLoader {
|
|
|
52
52
|
}
|
|
53
53
|
}
|
|
54
54
|
addErrorCallbacks(callback) {
|
|
55
|
-
const callbackId =
|
|
55
|
+
const callbackId = uuid_1.v4();
|
|
56
56
|
this.errorCallbacks.set(callbackId, callback);
|
|
57
57
|
return callbackId;
|
|
58
58
|
}
|
|
@@ -114,7 +114,7 @@ class BulkAccountLoader {
|
|
|
114
114
|
args,
|
|
115
115
|
});
|
|
116
116
|
}
|
|
117
|
-
const rpcResponses = await
|
|
117
|
+
const rpcResponses = await promiseTimeout_1.promiseTimeout(
|
|
118
118
|
// @ts-ignore
|
|
119
119
|
this.connection._rpcBatchRequest(requests), 10 * 1000 // 30 second timeout
|
|
120
120
|
);
|
package/lib/accounts/fetch.js
CHANGED
|
@@ -5,7 +5,7 @@ const pda_1 = require("../addresses/pda");
|
|
|
5
5
|
async function fetchUserAccounts(connection, program, authority, limit = 8) {
|
|
6
6
|
const userAccountPublicKeys = new Array();
|
|
7
7
|
for (let i = 0; i < limit; i++) {
|
|
8
|
-
userAccountPublicKeys.push(await
|
|
8
|
+
userAccountPublicKeys.push(await pda_1.getUserAccountPublicKey(program.programId, authority, i));
|
|
9
9
|
}
|
|
10
10
|
const accountInfos = await connection.getMultipleAccountsInfo(userAccountPublicKeys, 'confirmed');
|
|
11
11
|
return accountInfos.map((accountInfo) => {
|
|
@@ -17,7 +17,7 @@ async function fetchUserAccounts(connection, program, authority, limit = 8) {
|
|
|
17
17
|
}
|
|
18
18
|
exports.fetchUserAccounts = fetchUserAccounts;
|
|
19
19
|
async function fetchUserStatsAccount(connection, program, authority) {
|
|
20
|
-
const userStatsPublicKey =
|
|
20
|
+
const userStatsPublicKey = pda_1.getUserStatsAccountPublicKey(program.programId, authority);
|
|
21
21
|
const accountInfo = await connection.getAccountInfo(userStatsPublicKey, 'confirmed');
|
|
22
22
|
return accountInfo
|
|
23
23
|
? program.account.user.coder.accounts.decode('UserStats', accountInfo.data)
|
|
@@ -58,7 +58,7 @@ class PollingDriftClientAccountSubscriber {
|
|
|
58
58
|
if (this.accountsToPoll.size > 0) {
|
|
59
59
|
return;
|
|
60
60
|
}
|
|
61
|
-
const statePublicKey = await
|
|
61
|
+
const statePublicKey = await pda_1.getDriftStateAccountPublicKey(this.program.programId);
|
|
62
62
|
this.accountsToPoll.set(statePublicKey.toString(), {
|
|
63
63
|
key: 'state',
|
|
64
64
|
publicKey: statePublicKey,
|
|
@@ -74,7 +74,7 @@ class PollingDriftClientAccountSubscriber {
|
|
|
74
74
|
return true;
|
|
75
75
|
}
|
|
76
76
|
async addPerpMarketAccountToPoll(marketIndex) {
|
|
77
|
-
const perpMarketPublicKey = await
|
|
77
|
+
const perpMarketPublicKey = await pda_1.getPerpMarketPublicKey(this.program.programId, marketIndex);
|
|
78
78
|
this.accountsToPoll.set(perpMarketPublicKey.toString(), {
|
|
79
79
|
key: 'perpMarket',
|
|
80
80
|
publicKey: perpMarketPublicKey,
|
|
@@ -90,7 +90,7 @@ class PollingDriftClientAccountSubscriber {
|
|
|
90
90
|
return true;
|
|
91
91
|
}
|
|
92
92
|
async addSpotMarketAccountToPoll(marketIndex) {
|
|
93
|
-
const marketPublicKey = await
|
|
93
|
+
const marketPublicKey = await pda_1.getSpotMarketPublicKey(this.program.programId, marketIndex);
|
|
94
94
|
this.accountsToPoll.set(marketPublicKey.toString(), {
|
|
95
95
|
key: 'spotMarket',
|
|
96
96
|
publicKey: marketPublicKey,
|
|
@@ -129,7 +129,7 @@ class PollingDriftClientAccountSubscriber {
|
|
|
129
129
|
accountToPoll.callbackId = await this.accountLoader.addAccount(accountToPoll.publicKey, (buffer, slot) => {
|
|
130
130
|
if (!buffer)
|
|
131
131
|
return;
|
|
132
|
-
const account = this.program.account[accountToPoll.key].coder.accounts.decode(
|
|
132
|
+
const account = this.program.account[accountToPoll.key].coder.accounts.decode(utils_1.capitalize(accountToPoll.key), buffer);
|
|
133
133
|
const dataAndSlot = {
|
|
134
134
|
data: account,
|
|
135
135
|
slot,
|
|
@@ -168,7 +168,7 @@ class PollingDriftClientAccountSubscriber {
|
|
|
168
168
|
for (const [_, accountToPoll] of this.accountsToPoll) {
|
|
169
169
|
const { buffer, slot } = this.accountLoader.getBufferAndSlot(accountToPoll.publicKey);
|
|
170
170
|
if (buffer) {
|
|
171
|
-
const account = this.program.account[accountToPoll.key].coder.accounts.decode(
|
|
171
|
+
const account = this.program.account[accountToPoll.key].coder.accounts.decode(utils_1.capitalize(accountToPoll.key), buffer);
|
|
172
172
|
if (accountToPoll.mapKey != undefined) {
|
|
173
173
|
this[accountToPoll.key].set(accountToPoll.mapKey, {
|
|
174
174
|
data: account,
|
|
@@ -214,7 +214,7 @@ class PollingDriftClientAccountSubscriber {
|
|
|
214
214
|
this.isSubscribed = false;
|
|
215
215
|
}
|
|
216
216
|
async addSpotMarket(marketIndex) {
|
|
217
|
-
const marketPublicKey = await
|
|
217
|
+
const marketPublicKey = await pda_1.getSpotMarketPublicKey(this.program.programId, marketIndex);
|
|
218
218
|
if (this.accountsToPoll.has(marketPublicKey.toString())) {
|
|
219
219
|
return true;
|
|
220
220
|
}
|
|
@@ -224,7 +224,7 @@ class PollingDriftClientAccountSubscriber {
|
|
|
224
224
|
return true;
|
|
225
225
|
}
|
|
226
226
|
async addPerpMarket(marketIndex) {
|
|
227
|
-
const marketPublicKey = await
|
|
227
|
+
const marketPublicKey = await pda_1.getPerpMarketPublicKey(this.program.programId, marketIndex);
|
|
228
228
|
if (this.accountsToPoll.has(marketPublicKey.toString())) {
|
|
229
229
|
return true;
|
|
230
230
|
}
|
|
@@ -34,7 +34,7 @@ class PollingTokenAccountSubscriber {
|
|
|
34
34
|
return;
|
|
35
35
|
}
|
|
36
36
|
this.callbackId = await this.accountLoader.addAccount(this.publicKey, (buffer, slot) => {
|
|
37
|
-
const tokenAccount =
|
|
37
|
+
const tokenAccount = token_1.parseTokenAccount(buffer);
|
|
38
38
|
this.tokenAccountAndSlot = { data: tokenAccount, slot };
|
|
39
39
|
// @ts-ignore
|
|
40
40
|
this.eventEmitter.emit('tokenAccountUpdate', tokenAccount);
|
|
@@ -47,7 +47,7 @@ class PollingTokenAccountSubscriber {
|
|
|
47
47
|
async fetch() {
|
|
48
48
|
await this.accountLoader.load();
|
|
49
49
|
const { buffer, slot } = this.accountLoader.getBufferAndSlot(this.publicKey);
|
|
50
|
-
this.tokenAccountAndSlot = { data:
|
|
50
|
+
this.tokenAccountAndSlot = { data: token_1.parseTokenAccount(buffer), slot };
|
|
51
51
|
}
|
|
52
52
|
async unsubscribe() {
|
|
53
53
|
if (!this.isSubscribed) {
|
|
@@ -39,7 +39,7 @@ class PollingUserAccountSubscriber {
|
|
|
39
39
|
if (!buffer) {
|
|
40
40
|
return;
|
|
41
41
|
}
|
|
42
|
-
const account = this.program.account[accountToPoll.key].coder.accounts.decode(
|
|
42
|
+
const account = this.program.account[accountToPoll.key].coder.accounts.decode(utils_1.capitalize(accountToPoll.key), buffer);
|
|
43
43
|
this[accountToPoll.key] = { data: account, slot };
|
|
44
44
|
// @ts-ignore
|
|
45
45
|
this.eventEmitter.emit(accountToPoll.eventType, account);
|
|
@@ -67,7 +67,7 @@ class PollingUserAccountSubscriber {
|
|
|
67
67
|
for (const [_, accountToPoll] of this.accountsToPoll) {
|
|
68
68
|
const { buffer, slot } = this.accountLoader.getBufferAndSlot(accountToPoll.publicKey);
|
|
69
69
|
if (buffer) {
|
|
70
|
-
const account = this.program.account[accountToPoll.key].coder.accounts.decode(
|
|
70
|
+
const account = this.program.account[accountToPoll.key].coder.accounts.decode(utils_1.capitalize(accountToPoll.key), buffer);
|
|
71
71
|
this[accountToPoll.key] = { data: account, slot };
|
|
72
72
|
}
|
|
73
73
|
}
|
|
@@ -39,7 +39,7 @@ class PollingUserStatsAccountSubscriber {
|
|
|
39
39
|
if (!buffer) {
|
|
40
40
|
return;
|
|
41
41
|
}
|
|
42
|
-
const account = this.program.account[accountToPoll.key].coder.accounts.decode(
|
|
42
|
+
const account = this.program.account[accountToPoll.key].coder.accounts.decode(utils_1.capitalize(accountToPoll.key), buffer);
|
|
43
43
|
this[accountToPoll.key] = { data: account, slot };
|
|
44
44
|
// @ts-ignore
|
|
45
45
|
this.eventEmitter.emit(accountToPoll.eventType, account);
|
|
@@ -67,7 +67,7 @@ class PollingUserStatsAccountSubscriber {
|
|
|
67
67
|
for (const [_, accountToPoll] of this.accountsToPoll) {
|
|
68
68
|
const { buffer, slot } = this.accountLoader.getBufferAndSlot(accountToPoll.publicKey);
|
|
69
69
|
if (buffer) {
|
|
70
|
-
const account = this.program.account[accountToPoll.key].coder.accounts.decode(
|
|
70
|
+
const account = this.program.account[accountToPoll.key].coder.accounts.decode(utils_1.capitalize(accountToPoll.key), buffer);
|
|
71
71
|
this[accountToPoll.key] = { data: account, slot };
|
|
72
72
|
}
|
|
73
73
|
}
|
package/lib/accounts/types.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
/// <reference types="node" />
|
|
3
2
|
import { SpotMarketAccount, PerpMarketAccount, OracleSource, StateAccount, UserAccount, UserStatsAccount } from '../types';
|
|
4
3
|
import StrictEventEmitter from 'strict-event-emitter-types';
|
|
5
4
|
import { EventEmitter } from 'events';
|
|
@@ -77,7 +77,7 @@ class WebSocketAccountSubscriber {
|
|
|
77
77
|
return this.decodeBufferFn(buffer);
|
|
78
78
|
}
|
|
79
79
|
else {
|
|
80
|
-
return this.program.account[this.accountName].coder.accounts.decode(
|
|
80
|
+
return this.program.account[this.accountName].coder.accounts.decode(utils_1.capitalize(this.accountName), buffer);
|
|
81
81
|
}
|
|
82
82
|
}
|
|
83
83
|
unsubscribe() {
|
|
@@ -33,7 +33,7 @@ class WebSocketDriftClientAccountSubscriber {
|
|
|
33
33
|
this.subscriptionPromise = new Promise((res) => {
|
|
34
34
|
this.subscriptionPromiseResolver = res;
|
|
35
35
|
});
|
|
36
|
-
const statePublicKey = await
|
|
36
|
+
const statePublicKey = await pda_1.getDriftStateAccountPublicKey(this.program.programId);
|
|
37
37
|
// create and activate main state account subscription
|
|
38
38
|
this.stateAccountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('state', this.program, statePublicKey);
|
|
39
39
|
await this.stateAccountSubscriber.subscribe((data) => {
|
|
@@ -59,7 +59,7 @@ class WebSocketDriftClientAccountSubscriber {
|
|
|
59
59
|
return true;
|
|
60
60
|
}
|
|
61
61
|
async subscribeToPerpMarketAccount(marketIndex) {
|
|
62
|
-
const perpMarketPublicKey = await
|
|
62
|
+
const perpMarketPublicKey = await pda_1.getPerpMarketPublicKey(this.program.programId, marketIndex);
|
|
63
63
|
const accountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('perpMarket', this.program, perpMarketPublicKey);
|
|
64
64
|
await accountSubscriber.subscribe((data) => {
|
|
65
65
|
this.eventEmitter.emit('perpMarketAccountUpdate', data);
|
|
@@ -75,7 +75,7 @@ class WebSocketDriftClientAccountSubscriber {
|
|
|
75
75
|
return true;
|
|
76
76
|
}
|
|
77
77
|
async subscribeToSpotMarketAccount(marketIndex) {
|
|
78
|
-
const marketPublicKey = await
|
|
78
|
+
const marketPublicKey = await pda_1.getSpotMarketPublicKey(this.program.programId, marketIndex);
|
|
79
79
|
const accountSubscriber = new webSocketAccountSubscriber_1.WebSocketAccountSubscriber('spotMarket', this.program, marketPublicKey);
|
|
80
80
|
await accountSubscriber.subscribe((data) => {
|
|
81
81
|
this.eventEmitter.emit('spotMarketAccountUpdate', data);
|
|
@@ -8,7 +8,7 @@ async function getMarketAddress(programId, marketIndex) {
|
|
|
8
8
|
if (CACHE.has(cacheKey)) {
|
|
9
9
|
return CACHE.get(cacheKey);
|
|
10
10
|
}
|
|
11
|
-
const publicKey = await
|
|
11
|
+
const publicKey = await pda_1.getPerpMarketPublicKey(programId, marketIndex);
|
|
12
12
|
CACHE.set(cacheKey, publicKey);
|
|
13
13
|
return publicKey;
|
|
14
14
|
}
|
package/lib/addresses/pda.js
CHANGED
|
@@ -1,11 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
3
|
if (k2 === undefined) k2 = k;
|
|
4
|
-
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
9
5
|
}) : (function(o, m, k, k2) {
|
|
10
6
|
if (k2 === undefined) k2 = k;
|
|
11
7
|
o[k2] = m[k];
|