@drift-labs/sdk 2.31.0 → 2.31.1-beta.1

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/README.md CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  <p>
7
7
  <a href="https://www.npmjs.com/package/@drift-labs/sdk"><img alt="SDK npm package" src="https://img.shields.io/npm/v/@drift-labs/sdk" /></a>
8
- <a href="https://drift-labs.github.io/protocol-v1/sdk/"><img alt="Docs" src="https://img.shields.io/badge/docs-tutorials-blueviolet" /></a>
8
+ <a href="https://drift-labs.github.io/protocol-v2/sdk/"><img alt="Docs" src="https://img.shields.io/badge/docs-tutorials-blueviolet" /></a>
9
9
  <a href="https://discord.com/channels/849494028176588802/878700556904980500"><img alt="Discord Chat" src="https://img.shields.io/discord/889577356681945098?color=blueviolet" /></a>
10
10
  <a href="https://opensource.org/licenses/Apache-2.0"><img alt="License" src="https://img.shields.io/github/license/project-serum/anchor?color=blueviolet" /></a>
11
11
  </p>
@@ -53,6 +53,7 @@ The Drift SDK uses some common precisions, which are available as constants to i
53
53
  | PEG_PRECISION | 10^6 |
54
54
  | PRICE_PRECISION | 10^6 |
55
55
  | AMM_RESERVE_PRECISION | 10^9 |
56
+ | BASE_PRECISION | 10^9 |
56
57
 
57
58
  **Important Note for BigNum division**
58
59
 
@@ -238,7 +239,7 @@ main();
238
239
 
239
240
  ## License
240
241
 
241
- Drift Protocol v1 is licensed under [Apache 2.0](./LICENSE).
242
+ Drift Protocol v2 is licensed under [Apache 2.0](./LICENSE).
242
243
 
243
244
  Unless you explicitly state otherwise, any contribution intentionally submitted
244
245
  for inclusion in Drift SDK by you, as defined in the Apache-2.0 license, shall be
package/VERSION ADDED
@@ -0,0 +1 @@
1
+ 2.31.1-beta.1
@@ -2,10 +2,12 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.AuctionSubscriber = void 0;
4
4
  const memcmp_1 = require("../memcmp");
5
+ const events_1 = require("events");
5
6
  class AuctionSubscriber {
6
7
  constructor({ driftClient, opts }) {
7
8
  this.driftClient = driftClient;
8
9
  this.opts = opts || this.driftClient.opts;
10
+ this.eventEmitter = new events_1.EventEmitter();
9
11
  }
10
12
  async subscribe() {
11
13
  this.websocketId = this.driftClient.connection.onProgramAccountChange(this.driftClient.program.programId, (keyAccountInfo, context) => {
@@ -472,6 +472,10 @@ export declare class DriftClient {
472
472
  settleeUserAccountPublicKey: PublicKey;
473
473
  settleeUserAccount: UserAccount;
474
474
  }[], marketIndexes: number[]): Promise<TransactionSignature>;
475
+ getSettlePNLsIxs(users: {
476
+ settleeUserAccountPublicKey: PublicKey;
477
+ settleeUserAccount: UserAccount;
478
+ }[], marketIndexes: number[]): Promise<Array<TransactionInstruction>>;
475
479
  settlePNL(settleeUserAccountPublicKey: PublicKey, settleeUserAccount: UserAccount, marketIndex: number, txParams?: TxParams): Promise<TransactionSignature>;
476
480
  settlePNLIx(settleeUserAccountPublicKey: PublicKey, settleeUserAccount: UserAccount, marketIndex: number): Promise<TransactionInstruction>;
477
481
  liquidatePerp(userAccountPublicKey: PublicKey, userAccount: UserAccount, marketIndex: number, maxBaseAssetAmount: BN, limitPrice?: BN, txParams?: TxParams): Promise<TransactionSignature>;
@@ -2527,12 +2527,7 @@ class DriftClient {
2527
2527
  });
2528
2528
  }
2529
2529
  async settlePNLs(users, marketIndexes) {
2530
- const ixs = [];
2531
- for (const { settleeUserAccountPublicKey, settleeUserAccount } of users) {
2532
- for (const marketIndex of marketIndexes) {
2533
- ixs.push(await this.settlePNLIx(settleeUserAccountPublicKey, settleeUserAccount, marketIndex));
2534
- }
2535
- }
2530
+ const ixs = await this.getSettlePNLsIxs(users, marketIndexes);
2536
2531
  const tx = new web3_js_1.Transaction()
2537
2532
  .add(web3_js_1.ComputeBudgetProgram.setComputeUnitLimit({
2538
2533
  units: 1000000,
@@ -2541,6 +2536,15 @@ class DriftClient {
2541
2536
  const { txSig } = await this.sendTransaction(tx, [], this.opts);
2542
2537
  return txSig;
2543
2538
  }
2539
+ async getSettlePNLsIxs(users, marketIndexes) {
2540
+ const ixs = [];
2541
+ for (const { settleeUserAccountPublicKey, settleeUserAccount } of users) {
2542
+ for (const marketIndex of marketIndexes) {
2543
+ ixs.push(await this.settlePNLIx(settleeUserAccountPublicKey, settleeUserAccount, marketIndex));
2544
+ }
2545
+ }
2546
+ return ixs;
2547
+ }
2544
2548
  async settlePNL(settleeUserAccountPublicKey, settleeUserAccount, marketIndex, txParams) {
2545
2549
  const { txSig } = await this.sendTransaction(await this.buildTransaction(await this.settlePNLIx(settleeUserAccountPublicKey, settleeUserAccount, marketIndex), txParams), [], this.opts);
2546
2550
  return txSig;
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "2.31.0",
2
+ "version": "2.31.1-beta.0",
3
3
  "name": "drift",
4
4
  "instructions": [
5
5
  {
package/lib/index.d.ts CHANGED
@@ -57,6 +57,7 @@ export * from './serum/serumFulfillmentConfigMap';
57
57
  export * from './phoenix/phoenixSubscriber';
58
58
  export * from './phoenix/phoenixFulfillmentConfigMap';
59
59
  export * from './tx/retryTxSender';
60
+ export * from './tx/types';
60
61
  export * from './util/computeUnits';
61
62
  export * from './util/tps';
62
63
  export * from './util/promiseTimeout';
@@ -75,4 +76,7 @@ export * from './userMap/userMap';
75
76
  export * from './userMap/userStatsMap';
76
77
  export * from './math/bankruptcy';
77
78
  export * from './orderSubscriber';
79
+ export * from './orderSubscriber/types';
80
+ export * from './auctionSubscriber';
81
+ export * from './auctionSubscriber/types';
78
82
  export { BN, PublicKey, pyth };
package/lib/index.js CHANGED
@@ -27,7 +27,6 @@ exports.pyth = client_1.default;
27
27
  __exportStar(require("./tokenFaucet"), exports);
28
28
  __exportStar(require("./oracles/types"), exports);
29
29
  __exportStar(require("./oracles/pythClient"), exports);
30
- // export * from './oracles/switchboardClient';
31
30
  __exportStar(require("./types"), exports);
32
31
  __exportStar(require("./constants/perpMarkets"), exports);
33
32
  __exportStar(require("./accounts/fetch"), exports);
@@ -81,6 +80,7 @@ __exportStar(require("./serum/serumFulfillmentConfigMap"), exports);
81
80
  __exportStar(require("./phoenix/phoenixSubscriber"), exports);
82
81
  __exportStar(require("./phoenix/phoenixFulfillmentConfigMap"), exports);
83
82
  __exportStar(require("./tx/retryTxSender"), exports);
83
+ __exportStar(require("./tx/types"), exports);
84
84
  __exportStar(require("./util/computeUnits"), exports);
85
85
  __exportStar(require("./util/tps"), exports);
86
86
  __exportStar(require("./util/promiseTimeout"), exports);
@@ -99,3 +99,6 @@ __exportStar(require("./userMap/userMap"), exports);
99
99
  __exportStar(require("./userMap/userStatsMap"), exports);
100
100
  __exportStar(require("./math/bankruptcy"), exports);
101
101
  __exportStar(require("./orderSubscriber"), exports);
102
+ __exportStar(require("./orderSubscriber/types"), exports);
103
+ __exportStar(require("./auctionSubscriber"), exports);
104
+ __exportStar(require("./auctionSubscriber/types"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "2.31.0",
3
+ "version": "2.31.1-beta.1",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -18,7 +18,8 @@
18
18
  "test:bignum": "mocha -r ts-node/register tests/bn/**/*.ts",
19
19
  "patch-and-pub": "npm version patch --force && npm publish",
20
20
  "prettify": "prettier --check './src/***/*.ts'",
21
- "prettify:fix": "prettier --write './{src,tests}/***/*.ts'"
21
+ "prettify:fix": "prettier --write './{src,tests}/***/*.ts'",
22
+ "version": "node ./scripts/updateVersion.js"
22
23
  },
23
24
  "keywords": [
24
25
  "drift-labs",
@@ -0,0 +1,28 @@
1
+ const fs = require('fs');
2
+ const path = require('path');
3
+ const packageJson = require('../package.json');
4
+
5
+ const versionFilePath = path.join(__dirname, '..', 'VERSION');
6
+
7
+ let version = fs.readFileSync(versionFilePath, 'utf8');
8
+ version = version.replace(/\n/g, '');
9
+
10
+ const filesToReplace = [
11
+ // sdk/src/idl/drift.json
12
+ path.join(__dirname, '..', 'src', 'idl', 'drift.json'),
13
+ // programs/drift/Cargo.toml
14
+ path.join(__dirname, '..', '..', 'programs', 'drift', 'Cargo.toml'),
15
+ // Cargo.lock
16
+ path.join(__dirname, '..', '..', 'Cargo.lock'),
17
+ ]
18
+
19
+ console.log(`Updating versions from ${version} to ${packageJson.version} in:`);
20
+ for (const file of filesToReplace) {
21
+ console.log(`* ${file}`);
22
+ const fileContents = fs.readFileSync(file, 'utf8');
23
+ const newFileContents = fileContents.replace(version, packageJson.version);
24
+ fs.writeFileSync(file, newFileContents);
25
+ }
26
+
27
+ fs.writeFileSync(versionFilePath, packageJson.version);
28
+ console.log("");
@@ -17,6 +17,7 @@ export class AuctionSubscriber {
17
17
  constructor({ driftClient, opts }: AuctionSubscriberConfig) {
18
18
  this.driftClient = driftClient;
19
19
  this.opts = opts || this.driftClient.opts;
20
+ this.eventEmitter = new EventEmitter();
20
21
  }
21
22
 
22
23
  public async subscribe() {
@@ -4309,6 +4309,26 @@ export class DriftClient {
4309
4309
  }[],
4310
4310
  marketIndexes: number[]
4311
4311
  ): Promise<TransactionSignature> {
4312
+ const ixs = await this.getSettlePNLsIxs(users, marketIndexes);
4313
+ const tx = new Transaction()
4314
+ .add(
4315
+ ComputeBudgetProgram.setComputeUnitLimit({
4316
+ units: 1_000_000,
4317
+ })
4318
+ )
4319
+ .add(...ixs);
4320
+
4321
+ const { txSig } = await this.sendTransaction(tx, [], this.opts);
4322
+ return txSig;
4323
+ }
4324
+
4325
+ public async getSettlePNLsIxs(
4326
+ users: {
4327
+ settleeUserAccountPublicKey: PublicKey;
4328
+ settleeUserAccount: UserAccount;
4329
+ }[],
4330
+ marketIndexes: number[]
4331
+ ): Promise<Array<TransactionInstruction>> {
4312
4332
  const ixs = [];
4313
4333
  for (const { settleeUserAccountPublicKey, settleeUserAccount } of users) {
4314
4334
  for (const marketIndex of marketIndexes) {
@@ -4322,16 +4342,7 @@ export class DriftClient {
4322
4342
  }
4323
4343
  }
4324
4344
 
4325
- const tx = new Transaction()
4326
- .add(
4327
- ComputeBudgetProgram.setComputeUnitLimit({
4328
- units: 1_000_000,
4329
- })
4330
- )
4331
- .add(...ixs);
4332
-
4333
- const { txSig } = await this.sendTransaction(tx, [], this.opts);
4334
- return txSig;
4345
+ return ixs;
4335
4346
  }
4336
4347
 
4337
4348
  public async settlePNL(
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "2.31.0",
2
+ "version": "2.31.1-beta.1",
3
3
  "name": "drift",
4
4
  "instructions": [
5
5
  {
package/src/index.ts CHANGED
@@ -5,7 +5,6 @@ import pyth from '@pythnetwork/client';
5
5
  export * from './tokenFaucet';
6
6
  export * from './oracles/types';
7
7
  export * from './oracles/pythClient';
8
- // export * from './oracles/switchboardClient';
9
8
  export * from './types';
10
9
  export * from './constants/perpMarkets';
11
10
  export * from './accounts/fetch';
@@ -59,6 +58,7 @@ export * from './serum/serumFulfillmentConfigMap';
59
58
  export * from './phoenix/phoenixSubscriber';
60
59
  export * from './phoenix/phoenixFulfillmentConfigMap';
61
60
  export * from './tx/retryTxSender';
61
+ export * from './tx/types';
62
62
  export * from './util/computeUnits';
63
63
  export * from './util/tps';
64
64
  export * from './util/promiseTimeout';
@@ -77,5 +77,8 @@ export * from './userMap/userMap';
77
77
  export * from './userMap/userStatsMap';
78
78
  export * from './math/bankruptcy';
79
79
  export * from './orderSubscriber';
80
+ export * from './orderSubscriber/types';
81
+ export * from './auctionSubscriber';
82
+ export * from './auctionSubscriber/types';
80
83
 
81
84
  export { BN, PublicKey, pyth };
@@ -1,9 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.assert = void 0;
4
- function assert(condition, error) {
5
- if (!condition) {
6
- throw new Error(error || 'Unspecified AssertionError');
7
- }
8
- }
9
- exports.assert = assert;
@@ -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;
@@ -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;