@drift-labs/sdk 2.21.0 → 2.22.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.
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "2.21.0",
2
+ "version": "2.22.0-beta.0",
3
3
  "name": "drift",
4
4
  "instructions": [
5
5
  {
package/lib/index.d.ts CHANGED
@@ -66,4 +66,5 @@ export * from './dlob/DLOBOrders';
66
66
  export * from './dlob/NodeList';
67
67
  export * from './userMap/userMap';
68
68
  export * from './userMap/userStatsMap';
69
+ export * from './math/bankruptcy';
69
70
  export { BN, PublicKey, pyth };
package/lib/index.js CHANGED
@@ -90,3 +90,4 @@ __exportStar(require("./dlob/DLOBOrders"), exports);
90
90
  __exportStar(require("./dlob/NodeList"), exports);
91
91
  __exportStar(require("./userMap/userMap"), exports);
92
92
  __exportStar(require("./userMap/userStatsMap"), exports);
93
+ __exportStar(require("./math/bankruptcy"), exports);
@@ -0,0 +1,2 @@
1
+ import { User } from '../user';
2
+ export declare function isUserBankrupt(user: User): boolean;
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isUserBankrupt = void 0;
4
+ const __1 = require("..");
5
+ function isUserBankrupt(user) {
6
+ const userAccount = user.getUserAccount();
7
+ let hasLiability = false;
8
+ for (const position of userAccount.spotPositions) {
9
+ if (position.scaledBalance.gt(__1.ZERO)) {
10
+ if ((0, __1.isVariant)(position.balanceType, 'deposit')) {
11
+ return false;
12
+ }
13
+ if ((0, __1.isVariant)(position.balanceType, 'borrow')) {
14
+ hasLiability = true;
15
+ }
16
+ }
17
+ }
18
+ for (const position of userAccount.perpPositions) {
19
+ if (!position.baseAssetAmount.eq(__1.ZERO) ||
20
+ position.quoteAssetAmount.gt(__1.ZERO) ||
21
+ (0, __1.hasOpenOrders)(position) ||
22
+ position.lpShares.gt(__1.ZERO)) {
23
+ return false;
24
+ }
25
+ if (position.quoteAssetAmount.lt(__1.ZERO)) {
26
+ hasLiability = true;
27
+ }
28
+ }
29
+ return hasLiability;
30
+ }
31
+ exports.isUserBankrupt = isUserBankrupt;
@@ -51,3 +51,4 @@ export declare function calculateCostBasis(userPosition: PerpPosition): BN;
51
51
  export declare function findDirectionToClose(userPosition: PerpPosition): PositionDirection;
52
52
  export declare function positionCurrentDirection(userPosition: PerpPosition): PositionDirection;
53
53
  export declare function isEmptyPosition(userPosition: PerpPosition): boolean;
54
+ export declare function hasOpenOrders(position: PerpPosition): boolean;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isEmptyPosition = exports.positionCurrentDirection = exports.findDirectionToClose = exports.calculateCostBasis = exports.calculateEntryPrice = exports.calculateBreakEvenPrice = exports.positionIsAvailable = exports.calculatePositionFundingPNL = exports.calculateClaimablePnl = exports.calculatePositionPNL = exports.calculateBaseAssetValue = void 0;
3
+ exports.hasOpenOrders = exports.isEmptyPosition = exports.positionCurrentDirection = exports.findDirectionToClose = exports.calculateCostBasis = exports.calculateEntryPrice = exports.calculateBreakEvenPrice = exports.positionIsAvailable = exports.calculatePositionFundingPNL = exports.calculateClaimablePnl = exports.calculatePositionPNL = exports.calculateBaseAssetValue = void 0;
4
4
  const __1 = require("../");
5
5
  const numericConstants_1 = require("../constants/numericConstants");
6
6
  const types_1 = require("../types");
@@ -190,3 +190,9 @@ function isEmptyPosition(userPosition) {
190
190
  return userPosition.baseAssetAmount.eq(numericConstants_1.ZERO) && userPosition.openOrders === 0;
191
191
  }
192
192
  exports.isEmptyPosition = isEmptyPosition;
193
+ function hasOpenOrders(position) {
194
+ return (position.openOrders != 0 ||
195
+ !position.openBids.eq(numericConstants_1.ZERO) ||
196
+ !position.openAsks.eq(numericConstants_1.ZERO));
197
+ }
198
+ exports.hasOpenOrders = hasOpenOrders;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "2.21.0",
3
+ "version": "2.22.0-beta.0",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -60,4 +60,4 @@
60
60
  "engines": {
61
61
  "node": ">=12"
62
62
  }
63
- }
63
+ }
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "2.21.0",
2
+ "version": "2.22.0-beta.0",
3
3
  "name": "drift",
4
4
  "instructions": [
5
5
  {
package/src/index.ts CHANGED
@@ -68,5 +68,6 @@ export * from './dlob/DLOBOrders';
68
68
  export * from './dlob/NodeList';
69
69
  export * from './userMap/userMap';
70
70
  export * from './userMap/userStatsMap';
71
+ export * from './math/bankruptcy';
71
72
 
72
73
  export { BN, PublicKey, pyth };
@@ -0,0 +1,34 @@
1
+ import { ZERO, hasOpenOrders, isVariant } from '..';
2
+ import { User } from '../user';
3
+
4
+ export function isUserBankrupt(user: User): boolean {
5
+ const userAccount = user.getUserAccount();
6
+ let hasLiability = false;
7
+ for (const position of userAccount.spotPositions) {
8
+ if (position.scaledBalance.gt(ZERO)) {
9
+ if (isVariant(position.balanceType, 'deposit')) {
10
+ return false;
11
+ }
12
+ if (isVariant(position.balanceType, 'borrow')) {
13
+ hasLiability = true;
14
+ }
15
+ }
16
+ }
17
+
18
+ for (const position of userAccount.perpPositions) {
19
+ if (
20
+ !position.baseAssetAmount.eq(ZERO) ||
21
+ position.quoteAssetAmount.gt(ZERO) ||
22
+ hasOpenOrders(position) ||
23
+ position.lpShares.gt(ZERO)
24
+ ) {
25
+ return false;
26
+ }
27
+
28
+ if (position.quoteAssetAmount.lt(ZERO)) {
29
+ hasLiability = true;
30
+ }
31
+ }
32
+
33
+ return hasLiability;
34
+ }
@@ -270,3 +270,11 @@ export function positionCurrentDirection(
270
270
  export function isEmptyPosition(userPosition: PerpPosition): boolean {
271
271
  return userPosition.baseAssetAmount.eq(ZERO) && userPosition.openOrders === 0;
272
272
  }
273
+
274
+ export function hasOpenOrders(position: PerpPosition): boolean {
275
+ return (
276
+ position.openOrders != 0 ||
277
+ !position.openBids.eq(ZERO) ||
278
+ !position.openAsks.eq(ZERO)
279
+ );
280
+ }
@@ -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,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;