@drift-labs/sdk 2.31.0-beta.4 → 2.31.0-beta.5
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/get_events.ts +47 -0
- package/lib/config.js +2 -2
- package/lib/dlob/orderBookLevels.d.ts +1 -1
- package/lib/dlob/orderBookLevels.js +7 -4
- package/lib/idl/drift.json +1 -1
- package/lib/memcmp.d.ts +1 -0
- package/lib/memcmp.js +10 -1
- package/package.json +1 -1
- package/src/config.ts +6 -3
- package/src/dlob/orderBookLevels.ts +13 -4
- package/src/idl/drift.json +1 -1
- package/src/memcmp.ts +9 -0
- package/src/assert/assert.js +0 -9
- package/src/token/index.js +0 -38
- package/src/util/computeUnits.js +0 -27
- package/src/util/promiseTimeout.js +0 -14
- package/src/util/tps.js +0 -27
package/get_events.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { Connection, Keypair, PublicKey } from '@solana/web3.js';
|
|
2
|
+
import {
|
|
3
|
+
configs,
|
|
4
|
+
DriftClient,
|
|
5
|
+
Wallet,
|
|
6
|
+
} from "@drift-labs/sdk";
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
async function main() {
|
|
10
|
+
|
|
11
|
+
const driftConfig = configs['mainnet-beta'];
|
|
12
|
+
const connection = new Connection('https://api.mainnet-beta.solana.com');
|
|
13
|
+
|
|
14
|
+
const driftClient = new DriftClient({
|
|
15
|
+
connection: connection,
|
|
16
|
+
wallet: new Wallet(new Keypair()),
|
|
17
|
+
programID: new PublicKey(driftConfig.DRIFT_PROGRAM_ID),
|
|
18
|
+
userStats: true,
|
|
19
|
+
env: 'mainnet-beta',
|
|
20
|
+
});
|
|
21
|
+
console.log(`driftClientSubscribed: ${await driftClient.subscribe()}`);
|
|
22
|
+
|
|
23
|
+
const txHash = "3gvGQufckXGHrFDv4dNWEXuXKRMy3NZkKHMyFrAhLoYScaXXTGCp9vq58kWkfyJ8oDYZrz4bTyGayjUy9PKigeLS";
|
|
24
|
+
|
|
25
|
+
const tx = await driftClient.connection.getParsedTransaction(txHash, {
|
|
26
|
+
commitment: "confirmed",
|
|
27
|
+
maxSupportedTransactionVersion: 0,
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
let logIdx = 0;
|
|
31
|
+
// @ts-ignore
|
|
32
|
+
for (const event of driftClient.program._events._eventParser.parseLogs(tx!.meta!.logMessages)) {
|
|
33
|
+
console.log("----------------------------------------");
|
|
34
|
+
console.log(`Log ${logIdx++}`);
|
|
35
|
+
console.log("----------------------------------------");
|
|
36
|
+
console.log(`${JSON.stringify(event, null, 2)}`);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
console.log("========================================");
|
|
40
|
+
console.log("Raw transaction logs");
|
|
41
|
+
console.log("========================================");
|
|
42
|
+
console.log(JSON.stringify(tx!.meta!.logMessages, null, 2));
|
|
43
|
+
|
|
44
|
+
process.exit(0);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
main().catch(console.error);
|
package/lib/config.js
CHANGED
|
@@ -78,8 +78,8 @@ async function findAllMarketAndOracles(program) {
|
|
|
78
78
|
const perpMarketIndexes = [];
|
|
79
79
|
const spotMarketIndexes = [];
|
|
80
80
|
const oracleInfos = new Map();
|
|
81
|
-
const perpMarketProgramAccounts = await program.account.perpMarket.all();
|
|
82
|
-
const spotMarketProgramAccounts = await program.account.spotMarket.all();
|
|
81
|
+
const perpMarketProgramAccounts = (await program.account.perpMarket.all());
|
|
82
|
+
const spotMarketProgramAccounts = (await program.account.spotMarket.all());
|
|
83
83
|
for (const perpMarketProgramAccount of perpMarketProgramAccounts) {
|
|
84
84
|
const perpMarket = perpMarketProgramAccount.account;
|
|
85
85
|
perpMarketIndexes.push(perpMarket.marketIndex);
|
|
@@ -41,5 +41,5 @@ export declare function getVammL2Generator({ marketAccount, oraclePriceData, num
|
|
|
41
41
|
numOrders: number;
|
|
42
42
|
now?: BN;
|
|
43
43
|
}): L2OrderBookGenerator;
|
|
44
|
-
export declare function groupL2(l2: L2OrderBook, grouping: BN): L2OrderBook;
|
|
44
|
+
export declare function groupL2(l2: L2OrderBook, grouping: BN, depth: number): L2OrderBook;
|
|
45
45
|
export {};
|
|
@@ -135,14 +135,14 @@ function getVammL2Generator({ marketAccount, oraclePriceData, numOrders, now, })
|
|
|
135
135
|
};
|
|
136
136
|
}
|
|
137
137
|
exports.getVammL2Generator = getVammL2Generator;
|
|
138
|
-
function groupL2(l2, grouping) {
|
|
138
|
+
function groupL2(l2, grouping, depth) {
|
|
139
139
|
return {
|
|
140
|
-
bids: groupL2Levels(l2.bids, grouping, __1.PositionDirection.LONG),
|
|
141
|
-
asks: groupL2Levels(l2.asks, grouping, __1.PositionDirection.SHORT),
|
|
140
|
+
bids: groupL2Levels(l2.bids, grouping, __1.PositionDirection.LONG, depth),
|
|
141
|
+
asks: groupL2Levels(l2.asks, grouping, __1.PositionDirection.SHORT, depth),
|
|
142
142
|
};
|
|
143
143
|
}
|
|
144
144
|
exports.groupL2 = groupL2;
|
|
145
|
-
function groupL2Levels(levels, grouping, direction) {
|
|
145
|
+
function groupL2Levels(levels, grouping, direction, depth) {
|
|
146
146
|
const groupedLevels = [];
|
|
147
147
|
for (const level of levels) {
|
|
148
148
|
const price = (0, __1.standardizePrice)(level.price, grouping, direction);
|
|
@@ -168,6 +168,9 @@ function groupL2Levels(levels, grouping, direction) {
|
|
|
168
168
|
};
|
|
169
169
|
groupedLevels.push(groupedLevel);
|
|
170
170
|
}
|
|
171
|
+
if (groupedLevels.length === depth) {
|
|
172
|
+
break;
|
|
173
|
+
}
|
|
171
174
|
}
|
|
172
175
|
return groupedLevels;
|
|
173
176
|
}
|
package/lib/idl/drift.json
CHANGED
package/lib/memcmp.d.ts
CHANGED
|
@@ -3,3 +3,4 @@ export declare function getUserFilter(): MemcmpFilter;
|
|
|
3
3
|
export declare function getNonIdleUserFilter(): MemcmpFilter;
|
|
4
4
|
export declare function getUserWithOrderFilter(): MemcmpFilter;
|
|
5
5
|
export declare function getUserWithAuctionFilter(): MemcmpFilter;
|
|
6
|
+
export declare function getUserThatHasBeenLP(): MemcmpFilter;
|
package/lib/memcmp.js
CHANGED
|
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.getUserWithAuctionFilter = exports.getUserWithOrderFilter = exports.getNonIdleUserFilter = exports.getUserFilter = void 0;
|
|
6
|
+
exports.getUserThatHasBeenLP = exports.getUserWithAuctionFilter = exports.getUserWithOrderFilter = exports.getNonIdleUserFilter = exports.getUserFilter = void 0;
|
|
7
7
|
const bs58_1 = __importDefault(require("bs58"));
|
|
8
8
|
const anchor_1 = require("@coral-xyz/anchor");
|
|
9
9
|
function getUserFilter() {
|
|
@@ -42,3 +42,12 @@ function getUserWithAuctionFilter() {
|
|
|
42
42
|
};
|
|
43
43
|
}
|
|
44
44
|
exports.getUserWithAuctionFilter = getUserWithAuctionFilter;
|
|
45
|
+
function getUserThatHasBeenLP() {
|
|
46
|
+
return {
|
|
47
|
+
memcmp: {
|
|
48
|
+
offset: 4267,
|
|
49
|
+
bytes: bs58_1.default.encode(Uint8Array.from([99])),
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
exports.getUserThatHasBeenLP = getUserThatHasBeenLP;
|
package/package.json
CHANGED
package/src/config.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { PerpMarketAccount, SpotMarketAccount } from '.';
|
|
1
2
|
import {
|
|
2
3
|
DevnetPerpMarkets,
|
|
3
4
|
MainnetPerpMarkets,
|
|
@@ -11,7 +12,7 @@ import {
|
|
|
11
12
|
MainnetSpotMarkets,
|
|
12
13
|
} from './constants/spotMarkets';
|
|
13
14
|
import { OracleInfo } from './oracles/types';
|
|
14
|
-
import { Program } from '@coral-xyz/anchor';
|
|
15
|
+
import { Program, ProgramAccount } from '@coral-xyz/anchor';
|
|
15
16
|
|
|
16
17
|
type DriftConfig = {
|
|
17
18
|
ENV: DriftEnv;
|
|
@@ -124,8 +125,10 @@ export async function findAllMarketAndOracles(program: Program): Promise<{
|
|
|
124
125
|
const spotMarketIndexes = [];
|
|
125
126
|
const oracleInfos = new Map<string, OracleInfo>();
|
|
126
127
|
|
|
127
|
-
const perpMarketProgramAccounts =
|
|
128
|
-
|
|
128
|
+
const perpMarketProgramAccounts =
|
|
129
|
+
(await program.account.perpMarket.all()) as ProgramAccount<PerpMarketAccount>[];
|
|
130
|
+
const spotMarketProgramAccounts =
|
|
131
|
+
(await program.account.spotMarket.all()) as ProgramAccount<SpotMarketAccount>[];
|
|
129
132
|
|
|
130
133
|
for (const perpMarketProgramAccount of perpMarketProgramAccounts) {
|
|
131
134
|
const perpMarket = perpMarketProgramAccount.account;
|
|
@@ -244,17 +244,22 @@ export function getVammL2Generator({
|
|
|
244
244
|
};
|
|
245
245
|
}
|
|
246
246
|
|
|
247
|
-
export function groupL2(
|
|
247
|
+
export function groupL2(
|
|
248
|
+
l2: L2OrderBook,
|
|
249
|
+
grouping: BN,
|
|
250
|
+
depth: number
|
|
251
|
+
): L2OrderBook {
|
|
248
252
|
return {
|
|
249
|
-
bids: groupL2Levels(l2.bids, grouping, PositionDirection.LONG),
|
|
250
|
-
asks: groupL2Levels(l2.asks, grouping, PositionDirection.SHORT),
|
|
253
|
+
bids: groupL2Levels(l2.bids, grouping, PositionDirection.LONG, depth),
|
|
254
|
+
asks: groupL2Levels(l2.asks, grouping, PositionDirection.SHORT, depth),
|
|
251
255
|
};
|
|
252
256
|
}
|
|
253
257
|
|
|
254
258
|
function groupL2Levels(
|
|
255
259
|
levels: L2Level[],
|
|
256
260
|
grouping: BN,
|
|
257
|
-
direction: PositionDirection
|
|
261
|
+
direction: PositionDirection,
|
|
262
|
+
depth: number
|
|
258
263
|
): L2Level[] {
|
|
259
264
|
const groupedLevels = [];
|
|
260
265
|
for (const level of levels) {
|
|
@@ -281,6 +286,10 @@ function groupL2Levels(
|
|
|
281
286
|
};
|
|
282
287
|
groupedLevels.push(groupedLevel);
|
|
283
288
|
}
|
|
289
|
+
|
|
290
|
+
if (groupedLevels.length === depth) {
|
|
291
|
+
break;
|
|
292
|
+
}
|
|
284
293
|
}
|
|
285
294
|
return groupedLevels;
|
|
286
295
|
}
|
package/src/idl/drift.json
CHANGED
package/src/memcmp.ts
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,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;
|