@drift-labs/sdk 2.28.0-beta.3 → 2.28.0-beta.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dlob_read.ts +155 -0
- package/lib/dlob/DLOBSubscriber.js +1 -1
- package/lib/idl/drift.json +1 -1
- package/package.json +1 -1
- package/src/dlob/DLOBSubscriber.ts +2 -2
- package/src/idl/drift.json +2 -2
- package/src/assert/assert.js +0 -9
- package/src/token/index.js +0 -38
- 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/dlob_read.ts
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { Connection, Keypair, PublicKey } from '@solana/web3.js';
|
|
2
|
+
import {
|
|
3
|
+
BASE_PRECISION,
|
|
4
|
+
BulkAccountLoader,
|
|
5
|
+
configs,
|
|
6
|
+
convertToNumber,
|
|
7
|
+
DLOBSubscriber,
|
|
8
|
+
DriftClient,
|
|
9
|
+
getMarketsAndOraclesForSubscription,
|
|
10
|
+
MarketType,
|
|
11
|
+
PRICE_PRECISION,
|
|
12
|
+
SlotSubscriber,
|
|
13
|
+
UserMap,
|
|
14
|
+
Wallet,
|
|
15
|
+
} from './src/index';
|
|
16
|
+
import {
|
|
17
|
+
DLOBApiClient,
|
|
18
|
+
} from './src/dlob/DLOBApiClient';
|
|
19
|
+
|
|
20
|
+
async function main() {
|
|
21
|
+
|
|
22
|
+
const driftConfig = configs['mainnet-beta'];
|
|
23
|
+
const connection = new Connection('https://api.mainnet-beta.solana.com');
|
|
24
|
+
|
|
25
|
+
const accountLoader = new BulkAccountLoader(
|
|
26
|
+
connection,
|
|
27
|
+
'confirmed',
|
|
28
|
+
10000
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
const { perpMarketIndexes, spotMarketIndexes, oracleInfos } = getMarketsAndOraclesForSubscription('mainnet-beta');
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
const driftClient = new DriftClient({
|
|
35
|
+
connection: connection,
|
|
36
|
+
wallet: new Wallet(new Keypair()),
|
|
37
|
+
programID: new PublicKey(driftConfig.DRIFT_PROGRAM_ID),
|
|
38
|
+
accountSubscription: {
|
|
39
|
+
type: 'polling',
|
|
40
|
+
accountLoader: accountLoader,
|
|
41
|
+
},
|
|
42
|
+
perpMarketIndexes,
|
|
43
|
+
spotMarketIndexes,
|
|
44
|
+
oracleInfos,
|
|
45
|
+
userStats: true,
|
|
46
|
+
env: 'mainnet-beta',
|
|
47
|
+
});
|
|
48
|
+
console.log(`driftClientSubscribed: ${await driftClient.subscribe()}`);
|
|
49
|
+
|
|
50
|
+
const slotSubscriber = new SlotSubscriber(connection);
|
|
51
|
+
await slotSubscriber.subscribe();
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
// Alternatively, you can also use the UserMap class, which loads the DLOB via RPC calls.
|
|
55
|
+
// const userMap = new UserMap(driftClient, driftClient.userAccountSubscriptionConfig, false);
|
|
56
|
+
|
|
57
|
+
// This loads the DLOB from a server hosted by Drift.
|
|
58
|
+
const dlobAPI = new DLOBApiClient({
|
|
59
|
+
url: 'https://dlob.drift.trade/orders/idlWithSlot',
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
const dlobSubscriber = new DLOBSubscriber({
|
|
63
|
+
dlobSource: dlobAPI,
|
|
64
|
+
slotSource: slotSubscriber,
|
|
65
|
+
driftClient: driftClient,
|
|
66
|
+
updateFrequency: 10000,
|
|
67
|
+
});
|
|
68
|
+
await dlobSubscriber.subscribe();
|
|
69
|
+
|
|
70
|
+
const l2 = dlobSubscriber.getL2({
|
|
71
|
+
marketIndex: 0,
|
|
72
|
+
marketType: MarketType.PERP,
|
|
73
|
+
});
|
|
74
|
+
console.log("Level 2 order book:");
|
|
75
|
+
|
|
76
|
+
console.log("Asks:");
|
|
77
|
+
const asks = l2.asks.slice().reverse();
|
|
78
|
+
for (let i = 0; i < asks.length; i++) {
|
|
79
|
+
const ask = asks[i];
|
|
80
|
+
console.log(` [${asks.length - i - 1}] ${convertToNumber(ask.size, BASE_PRECISION)} @ $${convertToNumber(ask.price, PRICE_PRECISION)}`);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
console.log("Bids:");
|
|
84
|
+
const bids = l2.bids;
|
|
85
|
+
for (let i = 0; i < bids.length; i++) {
|
|
86
|
+
const bid = bids[i];
|
|
87
|
+
console.log(` [${i}] ${convertToNumber(bid.size, BASE_PRECISION)} @ $${convertToNumber(bid.price, PRICE_PRECISION)}`);
|
|
88
|
+
}
|
|
89
|
+
console.log("");
|
|
90
|
+
console.log("");
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
Level 2 order book:
|
|
94
|
+
Asks:
|
|
95
|
+
[9] 664.8 @ $21.3248
|
|
96
|
+
[8] 3.5 @ $21.288301
|
|
97
|
+
[7] 3.5 @ $21.287237
|
|
98
|
+
[6] 3.5 @ $21.283129
|
|
99
|
+
[5] 0.1 @ $21.2825
|
|
100
|
+
[4] 1937.6 @ $21.2748
|
|
101
|
+
[3] 1 @ $21.26
|
|
102
|
+
[2] 2015 @ $21.2589
|
|
103
|
+
[1] 503.7 @ $21.2483
|
|
104
|
+
[0] 377.8 @ $21.243
|
|
105
|
+
Bids:
|
|
106
|
+
[0] 1168.2 @ $21.2464
|
|
107
|
+
[1] 136.7 @ $21.2461
|
|
108
|
+
[2] 2570.2 @ $21.2426
|
|
109
|
+
[3] 0.1 @ $21.2419
|
|
110
|
+
[4] 47 @ $21.216188
|
|
111
|
+
[5] 327.8 @ $21.211
|
|
112
|
+
[6] 437 @ $21.2057
|
|
113
|
+
[7] 47 @ $21.205592
|
|
114
|
+
[8] 1748.3 @ $21.1951
|
|
115
|
+
[9] 187 @ $21.1792
|
|
116
|
+
*/
|
|
117
|
+
|
|
118
|
+
const l3 = dlobSubscriber.getL3({
|
|
119
|
+
marketIndex: 0,
|
|
120
|
+
marketType: MarketType.PERP,
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
console.log("Level 3 order book:");
|
|
124
|
+
console.log("Asks:");
|
|
125
|
+
const l3asks = l3.asks.slice().reverse();
|
|
126
|
+
for (let i = 0; i < l3asks.length; i++) {
|
|
127
|
+
const ask = l3asks[i];
|
|
128
|
+
console.log(` [${l3asks.length - i - 1}] ${ask.maker.toBase58()} ${convertToNumber(ask.size, BASE_PRECISION)} @ $${convertToNumber(ask.price, PRICE_PRECISION)}`);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
console.log("Bids:");
|
|
132
|
+
const l3bids = l3.bids;
|
|
133
|
+
for (let i = 0; i < l3bids.length; i++) {
|
|
134
|
+
const bid = l3bids[i];
|
|
135
|
+
console.log(` [${i}] ${bid.maker.toBase58()} ${convertToNumber(bid.size, BASE_PRECISION)} @ $${convertToNumber(bid.price, PRICE_PRECISION)}`);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
Level 3 order book:
|
|
140
|
+
Asks:
|
|
141
|
+
...
|
|
142
|
+
[3] FrEFAwxdrzHxgc7S4cuFfsfLmcg8pfbxnkCQW83euyCS 1 @ $21.26
|
|
143
|
+
[2] C13FZykQfLXKuMAMh2iuG7JxhQqd8otujNRAgVETU6id 2015 @ $21.2589
|
|
144
|
+
[1] C13FZykQfLXKuMAMh2iuG7JxhQqd8otujNRAgVETU6id 503.7 @ $21.2483
|
|
145
|
+
[0] C13FZykQfLXKuMAMh2iuG7JxhQqd8otujNRAgVETU6id 377.8 @ $21.243
|
|
146
|
+
Bids:
|
|
147
|
+
[0] FrEFAwxdrzHxgc7S4cuFfsfLmcg8pfbxnkCQW83euyCS 1168.2 @ $21.2464
|
|
148
|
+
[1] FrEFAwxdrzHxgc7S4cuFfsfLmcg8pfbxnkCQW83euyCS 2570.2 @ $21.2464
|
|
149
|
+
[2] FrEFAwxdrzHxgc7S4cuFfsfLmcg8pfbxnkCQW83euyCS 136.7 @ $21.2461
|
|
150
|
+
[3] FrEFAwxdrzHxgc7S4cuFfsfLmcg8pfbxnkCQW83euyCS 2570.2 @ $21.2426
|
|
151
|
+
...
|
|
152
|
+
*/
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
main().catch(console.error);
|
|
@@ -66,7 +66,7 @@ class DLOBSubscriber {
|
|
|
66
66
|
const isPerp = (0, types_1.isVariant)(marketType, 'perp');
|
|
67
67
|
if (isPerp) {
|
|
68
68
|
const perpMarketAccount = this.driftClient.getPerpMarketAccount(marketIndex);
|
|
69
|
-
oraclePriceData = this.driftClient.
|
|
69
|
+
oraclePriceData = this.driftClient.getOracleDataForPerpMarket(perpMarketAccount.marketIndex);
|
|
70
70
|
fallbackBid = (0, market_1.calculateBidPrice)(perpMarketAccount, oraclePriceData);
|
|
71
71
|
fallbackAsk = (0, market_1.calculateAskPrice)(perpMarketAccount, oraclePriceData);
|
|
72
72
|
}
|
package/lib/idl/drift.json
CHANGED
package/package.json
CHANGED
|
@@ -107,8 +107,8 @@ export class DLOBSubscriber {
|
|
|
107
107
|
if (isPerp) {
|
|
108
108
|
const perpMarketAccount =
|
|
109
109
|
this.driftClient.getPerpMarketAccount(marketIndex);
|
|
110
|
-
oraclePriceData = this.driftClient.
|
|
111
|
-
perpMarketAccount.
|
|
110
|
+
oraclePriceData = this.driftClient.getOracleDataForPerpMarket(
|
|
111
|
+
perpMarketAccount.marketIndex
|
|
112
112
|
);
|
|
113
113
|
fallbackBid = calculateBidPrice(perpMarketAccount, oraclePriceData);
|
|
114
114
|
fallbackAsk = calculateAskPrice(perpMarketAccount, oraclePriceData);
|
package/src/idl/drift.json
CHANGED
package/src/assert/assert.js
DELETED
package/src/token/index.js
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.parseTokenAccount = void 0;
|
|
4
|
-
const spl_token_1 = require("@solana/spl-token");
|
|
5
|
-
const web3_js_1 = require("@solana/web3.js");
|
|
6
|
-
function parseTokenAccount(data) {
|
|
7
|
-
const accountInfo = spl_token_1.AccountLayout.decode(data);
|
|
8
|
-
accountInfo.mint = new web3_js_1.PublicKey(accountInfo.mint);
|
|
9
|
-
accountInfo.owner = new web3_js_1.PublicKey(accountInfo.owner);
|
|
10
|
-
accountInfo.amount = spl_token_1.u64.fromBuffer(accountInfo.amount);
|
|
11
|
-
if (accountInfo.delegateOption === 0) {
|
|
12
|
-
accountInfo.delegate = null;
|
|
13
|
-
// eslint-disable-next-line new-cap
|
|
14
|
-
accountInfo.delegatedAmount = new spl_token_1.u64(0);
|
|
15
|
-
}
|
|
16
|
-
else {
|
|
17
|
-
accountInfo.delegate = new web3_js_1.PublicKey(accountInfo.delegate);
|
|
18
|
-
accountInfo.delegatedAmount = spl_token_1.u64.fromBuffer(accountInfo.delegatedAmount);
|
|
19
|
-
}
|
|
20
|
-
accountInfo.isInitialized = accountInfo.state !== 0;
|
|
21
|
-
accountInfo.isFrozen = accountInfo.state === 2;
|
|
22
|
-
if (accountInfo.isNativeOption === 1) {
|
|
23
|
-
accountInfo.rentExemptReserve = spl_token_1.u64.fromBuffer(accountInfo.isNative);
|
|
24
|
-
accountInfo.isNative = true;
|
|
25
|
-
}
|
|
26
|
-
else {
|
|
27
|
-
accountInfo.rentExemptReserve = null;
|
|
28
|
-
accountInfo.isNative = false;
|
|
29
|
-
}
|
|
30
|
-
if (accountInfo.closeAuthorityOption === 0) {
|
|
31
|
-
accountInfo.closeAuthority = null;
|
|
32
|
-
}
|
|
33
|
-
else {
|
|
34
|
-
accountInfo.closeAuthority = new web3_js_1.PublicKey(accountInfo.closeAuthority);
|
|
35
|
-
}
|
|
36
|
-
return accountInfo;
|
|
37
|
-
}
|
|
38
|
-
exports.parseTokenAccount = parseTokenAccount;
|
package/src/util/computeUnits.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.findComputeUnitConsumption = void 0;
|
|
13
|
-
function findComputeUnitConsumption(programId, connection, txSignature, commitment = 'confirmed') {
|
|
14
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
15
|
-
const tx = yield connection.getTransaction(txSignature, { commitment });
|
|
16
|
-
const computeUnits = [];
|
|
17
|
-
const regex = new RegExp(`Program ${programId.toString()} consumed ([0-9]{0,6}) of ([0-9]{0,7}) compute units`);
|
|
18
|
-
tx.meta.logMessages.forEach((logMessage) => {
|
|
19
|
-
const match = logMessage.match(regex);
|
|
20
|
-
if (match && match[1]) {
|
|
21
|
-
computeUnits.push(match[1]);
|
|
22
|
-
}
|
|
23
|
-
});
|
|
24
|
-
return computeUnits;
|
|
25
|
-
});
|
|
26
|
-
}
|
|
27
|
-
exports.findComputeUnitConsumption = findComputeUnitConsumption;
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getTokenAddress = void 0;
|
|
4
|
-
const spl_token_1 = require("@solana/spl-token");
|
|
5
|
-
const web3_js_1 = require("@solana/web3.js");
|
|
6
|
-
const getTokenAddress = (mintAddress, userPubKey) => {
|
|
7
|
-
return spl_token_1.Token.getAssociatedTokenAddress(spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID, spl_token_1.TOKEN_PROGRAM_ID, new web3_js_1.PublicKey(mintAddress), new web3_js_1.PublicKey(userPubKey));
|
|
8
|
-
};
|
|
9
|
-
exports.getTokenAddress = getTokenAddress;
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.promiseTimeout = void 0;
|
|
4
|
-
function promiseTimeout(promise, timeoutMs) {
|
|
5
|
-
let timeoutId;
|
|
6
|
-
const timeoutPromise = new Promise((resolve) => {
|
|
7
|
-
timeoutId = setTimeout(() => resolve(null), timeoutMs);
|
|
8
|
-
});
|
|
9
|
-
return Promise.race([promise, timeoutPromise]).then((result) => {
|
|
10
|
-
clearTimeout(timeoutId);
|
|
11
|
-
return result;
|
|
12
|
-
});
|
|
13
|
-
}
|
|
14
|
-
exports.promiseTimeout = promiseTimeout;
|
package/src/util/tps.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.estimateTps = void 0;
|
|
13
|
-
function estimateTps(programId, connection, failed) {
|
|
14
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
15
|
-
let signatures = yield connection.getSignaturesForAddress(programId, undefined, 'finalized');
|
|
16
|
-
if (failed) {
|
|
17
|
-
signatures = signatures.filter((signature) => signature.err);
|
|
18
|
-
}
|
|
19
|
-
const numberOfSignatures = signatures.length;
|
|
20
|
-
if (numberOfSignatures === 0) {
|
|
21
|
-
return 0;
|
|
22
|
-
}
|
|
23
|
-
return (numberOfSignatures /
|
|
24
|
-
(signatures[0].blockTime - signatures[numberOfSignatures - 1].blockTime));
|
|
25
|
-
});
|
|
26
|
-
}
|
|
27
|
-
exports.estimateTps = estimateTps;
|