@drift-labs/sdk 2.21.0-beta.2 → 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.
- package/lib/driftClient.d.ts +1 -0
- package/lib/driftClient.js +13 -1
- package/lib/idl/drift.json +1 -1
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/math/bankruptcy.d.ts +2 -0
- package/lib/math/bankruptcy.js +31 -0
- package/lib/math/position.d.ts +1 -0
- package/lib/math/position.js +7 -1
- package/package.json +2 -2
- package/src/driftClient.ts +19 -1
- package/src/idl/drift.json +1 -1
- package/src/index.ts +1 -0
- package/src/math/bankruptcy.ts +34 -0
- package/src/math/position.ts +8 -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/getTokenAddress.js +0 -9
- package/src/util/promiseTimeout.js +0 -14
- package/src/util/tps.js +0 -27
package/lib/driftClient.d.ts
CHANGED
|
@@ -104,6 +104,7 @@ export declare class DriftClient {
|
|
|
104
104
|
fetchAllUserAccounts(includeIdle?: boolean): Promise<ProgramAccount<UserAccount>[]>;
|
|
105
105
|
getUserAccountsForDelegate(delegate: PublicKey): Promise<UserAccount[]>;
|
|
106
106
|
getUserAccountsForAuthority(authority: PublicKey): Promise<UserAccount[]>;
|
|
107
|
+
getReferrerNameAccountsForAuthority(authority: PublicKey): Promise<ReferrerNameAccount[]>;
|
|
107
108
|
deleteUser(subAccountId?: number, txParams?: TxParams): Promise<TransactionSignature>;
|
|
108
109
|
getUser(subAccountId?: number): User;
|
|
109
110
|
getUsers(): User[];
|
package/lib/driftClient.js
CHANGED
|
@@ -361,7 +361,7 @@ class DriftClient {
|
|
|
361
361
|
async initializeReferrerName(name) {
|
|
362
362
|
const userAccountPublicKey = (0, pda_1.getUserAccountPublicKeySync)(this.program.programId, this.wallet.publicKey, 0);
|
|
363
363
|
const nameBuffer = (0, userName_1.encodeName)(name);
|
|
364
|
-
const referrerNameAccountPublicKey =
|
|
364
|
+
const referrerNameAccountPublicKey = (0, pda_1.getReferrerNamePublicKeySync)(this.program.programId, nameBuffer);
|
|
365
365
|
const tx = await this.program.transaction.initializeReferrerName(nameBuffer, {
|
|
366
366
|
accounts: {
|
|
367
367
|
referrerName: referrerNameAccountPublicKey,
|
|
@@ -462,6 +462,18 @@ class DriftClient {
|
|
|
462
462
|
]);
|
|
463
463
|
return programAccounts.map((programAccount) => programAccount.account);
|
|
464
464
|
}
|
|
465
|
+
async getReferrerNameAccountsForAuthority(authority) {
|
|
466
|
+
const programAccounts = await this.program.account.referrerName.all([
|
|
467
|
+
{
|
|
468
|
+
memcmp: {
|
|
469
|
+
offset: 8,
|
|
470
|
+
/** data to match, as base-58 encoded string and limited to less than 129 bytes */
|
|
471
|
+
bytes: bs58_1.default.encode(authority.toBuffer()),
|
|
472
|
+
},
|
|
473
|
+
},
|
|
474
|
+
]);
|
|
475
|
+
return programAccounts.map((programAccount) => programAccount.account);
|
|
476
|
+
}
|
|
465
477
|
async deleteUser(subAccountId = 0, txParams) {
|
|
466
478
|
var _a;
|
|
467
479
|
const userAccountPublicKey = (0, pda_1.getUserAccountPublicKeySync)(this.program.programId, this.wallet.publicKey, subAccountId);
|
package/lib/idl/drift.json
CHANGED
package/lib/index.d.ts
CHANGED
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,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;
|
package/lib/math/position.d.ts
CHANGED
|
@@ -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;
|
package/lib/math/position.js
CHANGED
|
@@ -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
package/src/driftClient.ts
CHANGED
|
@@ -603,7 +603,7 @@ export class DriftClient {
|
|
|
603
603
|
|
|
604
604
|
const nameBuffer = encodeName(name);
|
|
605
605
|
|
|
606
|
-
const referrerNameAccountPublicKey =
|
|
606
|
+
const referrerNameAccountPublicKey = getReferrerNamePublicKeySync(
|
|
607
607
|
this.program.programId,
|
|
608
608
|
nameBuffer
|
|
609
609
|
);
|
|
@@ -774,6 +774,24 @@ export class DriftClient {
|
|
|
774
774
|
);
|
|
775
775
|
}
|
|
776
776
|
|
|
777
|
+
public async getReferrerNameAccountsForAuthority(
|
|
778
|
+
authority: PublicKey
|
|
779
|
+
): Promise<ReferrerNameAccount[]> {
|
|
780
|
+
const programAccounts = await this.program.account.referrerName.all([
|
|
781
|
+
{
|
|
782
|
+
memcmp: {
|
|
783
|
+
offset: 8,
|
|
784
|
+
/** data to match, as base-58 encoded string and limited to less than 129 bytes */
|
|
785
|
+
bytes: bs58.encode(authority.toBuffer()),
|
|
786
|
+
},
|
|
787
|
+
},
|
|
788
|
+
]);
|
|
789
|
+
|
|
790
|
+
return programAccounts.map(
|
|
791
|
+
(programAccount) => programAccount.account as ReferrerNameAccount
|
|
792
|
+
);
|
|
793
|
+
}
|
|
794
|
+
|
|
777
795
|
public async deleteUser(
|
|
778
796
|
subAccountId = 0,
|
|
779
797
|
txParams?: TxParams
|
package/src/idl/drift.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -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
|
+
}
|
package/src/math/position.ts
CHANGED
|
@@ -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
|
+
}
|
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;
|