@drift-labs/sdk 0.2.0-master.3 → 0.2.0-master.6

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.
@@ -2,107 +2,19 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getMarketOrderParams = exports.getTriggerLimitOrderParams = exports.getTriggerMarketOrderParams = exports.getLimitOrderParams = void 0;
4
4
  const types_1 = require("./types");
5
- const numericConstants_1 = require("./constants/numericConstants");
6
- function getLimitOrderParams(marketIndex, direction, baseAssetAmount, price, reduceOnly, discountToken = false, referrer = false, userOrderId = 0, postOnly = false, oraclePriceOffset = numericConstants_1.ZERO, immediateOrCancel = false) {
7
- return {
8
- orderType: types_1.OrderType.LIMIT,
9
- userOrderId,
10
- marketIndex,
11
- direction,
12
- quoteAssetAmount: numericConstants_1.ZERO,
13
- baseAssetAmount,
14
- price,
15
- reduceOnly,
16
- postOnly,
17
- immediateOrCancel,
18
- positionLimit: numericConstants_1.ZERO,
19
- padding0: true,
20
- padding1: numericConstants_1.ZERO,
21
- optionalAccounts: {
22
- discountToken,
23
- referrer,
24
- },
25
- triggerCondition: types_1.OrderTriggerCondition.ABOVE,
26
- triggerPrice: numericConstants_1.ZERO,
27
- oraclePriceOffset,
28
- };
5
+ function getLimitOrderParams(params) {
6
+ return Object.assign({}, params, { orderType: types_1.OrderType.LIMIT });
29
7
  }
30
8
  exports.getLimitOrderParams = getLimitOrderParams;
31
- function getTriggerMarketOrderParams(marketIndex, direction, baseAssetAmount, triggerPrice, triggerCondition, reduceOnly, discountToken = false, referrer = false, userOrderId = 0) {
32
- return {
33
- orderType: types_1.OrderType.TRIGGER_MARKET,
34
- userOrderId,
35
- marketIndex,
36
- direction,
37
- quoteAssetAmount: numericConstants_1.ZERO,
38
- baseAssetAmount,
39
- price: numericConstants_1.ZERO,
40
- reduceOnly,
41
- postOnly: false,
42
- immediateOrCancel: false,
43
- positionLimit: numericConstants_1.ZERO,
44
- padding0: true,
45
- padding1: numericConstants_1.ZERO,
46
- optionalAccounts: {
47
- discountToken,
48
- referrer,
49
- },
50
- triggerCondition,
51
- triggerPrice,
52
- oraclePriceOffset: numericConstants_1.ZERO,
53
- };
9
+ function getTriggerMarketOrderParams(params) {
10
+ return Object.assign({}, params, { orderType: types_1.OrderType.TRIGGER_MARKET });
54
11
  }
55
12
  exports.getTriggerMarketOrderParams = getTriggerMarketOrderParams;
56
- function getTriggerLimitOrderParams(marketIndex, direction, baseAssetAmount, price, triggerPrice, triggerCondition, reduceOnly, discountToken = false, referrer = false, userOrderId = 0) {
57
- return {
58
- orderType: types_1.OrderType.TRIGGER_LIMIT,
59
- userOrderId,
60
- marketIndex,
61
- direction,
62
- quoteAssetAmount: numericConstants_1.ZERO,
63
- baseAssetAmount,
64
- price,
65
- reduceOnly,
66
- postOnly: false,
67
- immediateOrCancel: false,
68
- positionLimit: numericConstants_1.ZERO,
69
- padding0: true,
70
- padding1: numericConstants_1.ZERO,
71
- optionalAccounts: {
72
- discountToken,
73
- referrer,
74
- },
75
- triggerCondition,
76
- triggerPrice,
77
- oraclePriceOffset: numericConstants_1.ZERO,
78
- };
13
+ function getTriggerLimitOrderParams(params) {
14
+ return Object.assign({}, params, { orderType: types_1.OrderType.TRIGGER_LIMIT });
79
15
  }
80
16
  exports.getTriggerLimitOrderParams = getTriggerLimitOrderParams;
81
- function getMarketOrderParams(marketIndex, direction, quoteAssetAmount, baseAssetAmount, reduceOnly, price = numericConstants_1.ZERO, discountToken = false, referrer = false) {
82
- if (baseAssetAmount.eq(numericConstants_1.ZERO) && quoteAssetAmount.eq(numericConstants_1.ZERO)) {
83
- throw Error('baseAssetAmount or quoteAssetAmount must be zero');
84
- }
85
- return {
86
- orderType: types_1.OrderType.MARKET,
87
- userOrderId: 0,
88
- marketIndex,
89
- direction,
90
- quoteAssetAmount,
91
- baseAssetAmount,
92
- price,
93
- reduceOnly,
94
- postOnly: false,
95
- immediateOrCancel: false,
96
- positionLimit: numericConstants_1.ZERO,
97
- padding0: true,
98
- padding1: numericConstants_1.ZERO,
99
- optionalAccounts: {
100
- discountToken,
101
- referrer,
102
- },
103
- triggerCondition: types_1.OrderTriggerCondition.ABOVE,
104
- triggerPrice: numericConstants_1.ZERO,
105
- oraclePriceOffset: numericConstants_1.ZERO,
106
- };
17
+ function getMarketOrderParams(params) {
18
+ return Object.assign({}, params, { orderType: types_1.OrderType.MARKET });
107
19
  }
108
20
  exports.getMarketOrderParams = getMarketOrderParams;
@@ -1,9 +1,16 @@
1
+ /// <reference types="node" />
1
2
  import { Connection } from '@solana/web3.js';
3
+ import { EventEmitter } from 'events';
4
+ import StrictEventEmitter from 'strict-event-emitter-types/types/src';
2
5
  declare type SlotSubscriberConfig = {};
6
+ export interface SlotSubscriberEvents {
7
+ newSlot: (newSlot: number) => void;
8
+ }
3
9
  export declare class SlotSubscriber {
4
10
  private connection;
5
11
  currentSlot: number;
6
12
  subscriptionId: number;
13
+ eventEmitter: StrictEventEmitter<EventEmitter, SlotSubscriberEvents>;
7
14
  constructor(connection: Connection, _config?: SlotSubscriberConfig);
8
15
  subscribe(): Promise<void>;
9
16
  getSlot(): number;
@@ -1,14 +1,17 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SlotSubscriber = void 0;
4
+ const events_1 = require("events");
4
5
  class SlotSubscriber {
5
6
  constructor(connection, _config) {
6
7
  this.connection = connection;
8
+ this.eventEmitter = new events_1.EventEmitter();
7
9
  }
8
10
  async subscribe() {
9
11
  this.currentSlot = await this.connection.getSlot('confirmed');
10
12
  this.subscriptionId = this.connection.onSlotChange((slotInfo) => {
11
13
  this.currentSlot = slotInfo.slot;
14
+ this.eventEmitter.emit('newSlot', slotInfo.slot);
12
15
  });
13
16
  }
14
17
  getSlot() {
package/lib/tx/utils.js CHANGED
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.wrapInTx = void 0;
4
4
  const web3_js_1 = require("@solana/web3.js");
5
5
  const COMPUTE_UNITS_DEFAULT = 200000;
6
- function wrapInTx(instruction, computeUnits = 500000 // TODO, requires less code change
6
+ function wrapInTx(instruction, computeUnits = 600000 // TODO, requires less code change
7
7
  ) {
8
8
  const tx = new web3_js_1.Transaction();
9
9
  if (computeUnits != COMPUTE_UNITS_DEFAULT) {
package/lib/types.d.ts CHANGED
@@ -25,6 +25,14 @@ export declare class PositionDirection {
25
25
  short: {};
26
26
  };
27
27
  }
28
+ export declare class DepositDirection {
29
+ static readonly DEPOSIT: {
30
+ deposit: {};
31
+ };
32
+ static readonly WITHDRAW: {
33
+ withdraw: {};
34
+ };
35
+ }
28
36
  export declare class OracleSource {
29
37
  static readonly PYTH: {
30
38
  pyth: {};
@@ -92,6 +100,20 @@ export declare class OrderAction {
92
100
  trigger: {};
93
101
  };
94
102
  }
103
+ export declare class OrderActionExplanation {
104
+ static readonly NONE: {
105
+ none: {};
106
+ };
107
+ static readonly BREACHED_MARGIN_REQUIREMENT: {
108
+ breachedMarginRequirement: {};
109
+ };
110
+ static readonly ORACLE_PRICE_BREACHED_LIMIT_PRICE: {
111
+ oraclePriceBreachedLimitPrice: {};
112
+ };
113
+ static readonly MARKET_ORDER_FILLED_TO_LIMIT_PRICE: {
114
+ marketOrderFilledToLimitPrice: {};
115
+ };
116
+ }
95
117
  export declare class OrderTriggerCondition {
96
118
  static readonly ABOVE: {
97
119
  above: {};
@@ -118,6 +140,7 @@ export declare type DepositRecord = {
118
140
  };
119
141
  bankIndex: BN;
120
142
  amount: BN;
143
+ oraclePrice: BN;
121
144
  from?: PublicKey;
122
145
  to?: PublicKey;
123
146
  };
@@ -185,7 +208,10 @@ export declare type OrderRecord = {
185
208
  maker: PublicKey;
186
209
  takerOrder: Order;
187
210
  makerOrder: Order;
211
+ takerUnsettledPnl: BN;
212
+ makerUnsettledPnl: BN;
188
213
  action: OrderAction;
214
+ actionExplanation: OrderActionExplanation;
189
215
  filler: PublicKey;
190
216
  fillRecordId: BN;
191
217
  marketIndex: BN;
@@ -277,6 +303,8 @@ export declare type AMM = {
277
303
  lastMarkPriceTwapTs: BN;
278
304
  lastOraclePriceTwap: BN;
279
305
  lastOraclePriceTwapTs: BN;
306
+ lastOracleMarkSpreadPct: BN;
307
+ lastOracleConfPct: BN;
280
308
  oracle: PublicKey;
281
309
  oracleSource: OracleSource;
282
310
  fundingPeriod: BN;
@@ -291,6 +319,8 @@ export declare type AMM = {
291
319
  totalFee: BN;
292
320
  minimumQuoteAssetTradeSize: BN;
293
321
  baseAssetAmountStepSize: BN;
322
+ maxBaseAssetAmountRatio: number;
323
+ maxSlippageRatio: number;
294
324
  lastOraclePrice: BN;
295
325
  baseSpread: number;
296
326
  curveUpdateIntensity: number;
@@ -375,7 +405,6 @@ export declare type OrderParams = {
375
405
  orderType: OrderType;
376
406
  userOrderId: number;
377
407
  direction: PositionDirection;
378
- quoteAssetAmount: BN;
379
408
  baseAssetAmount: BN;
380
409
  price: BN;
381
410
  marketIndex: BN;
@@ -393,10 +422,50 @@ export declare type OrderParams = {
393
422
  referrer: boolean;
394
423
  };
395
424
  };
425
+ export declare type NecessaryOrderParams = {
426
+ orderType: OrderType;
427
+ marketIndex: BN;
428
+ baseAssetAmount: BN;
429
+ direction: PositionDirection;
430
+ };
431
+ export declare type OptionalOrderParams = {
432
+ [Property in keyof OrderParams]?: OrderParams[Property];
433
+ } & NecessaryOrderParams;
434
+ export declare const DefaultOrderParams: {
435
+ orderType: {
436
+ market: {};
437
+ };
438
+ userOrderId: number;
439
+ direction: {
440
+ long: {};
441
+ };
442
+ baseAssetAmount: BN;
443
+ price: BN;
444
+ marketIndex: BN;
445
+ reduceOnly: boolean;
446
+ postOnly: boolean;
447
+ immediateOrCancel: boolean;
448
+ triggerPrice: BN;
449
+ triggerCondition: {
450
+ above: {};
451
+ };
452
+ positionLimit: BN;
453
+ oraclePriceOffset: BN;
454
+ padding0: BN;
455
+ padding1: BN;
456
+ optionalAccounts: {
457
+ discountToken: boolean;
458
+ referrer: boolean;
459
+ };
460
+ };
396
461
  export declare type MakerInfo = {
397
462
  maker: PublicKey;
398
463
  order: Order;
399
464
  };
465
+ export declare type TakerInfo = {
466
+ taker: PublicKey;
467
+ order: Order;
468
+ };
400
469
  export interface IWallet {
401
470
  signTransaction(tx: Transaction): Promise<Transaction>;
402
471
  signAllTransactions(txs: Transaction[]): Promise<Transaction[]>;
package/lib/types.js CHANGED
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TradeSide = exports.isOneOfVariant = exports.isVariant = exports.OrderTriggerCondition = exports.OrderAction = exports.OrderDiscountTier = exports.OrderStatus = exports.OrderType = exports.OracleSource = exports.PositionDirection = exports.BankBalanceType = exports.SwapDirection = void 0;
3
+ exports.DefaultOrderParams = exports.TradeSide = exports.isOneOfVariant = exports.isVariant = exports.OrderTriggerCondition = exports.OrderActionExplanation = exports.OrderAction = exports.OrderDiscountTier = exports.OrderStatus = exports.OrderType = exports.OracleSource = exports.DepositDirection = exports.PositionDirection = exports.BankBalanceType = exports.SwapDirection = void 0;
4
+ const _1 = require(".");
4
5
  // # Utility Types / Enums / Constants
5
6
  class SwapDirection {
6
7
  }
@@ -17,6 +18,11 @@ class PositionDirection {
17
18
  exports.PositionDirection = PositionDirection;
18
19
  PositionDirection.LONG = { long: {} };
19
20
  PositionDirection.SHORT = { short: {} };
21
+ class DepositDirection {
22
+ }
23
+ exports.DepositDirection = DepositDirection;
24
+ DepositDirection.DEPOSIT = { deposit: {} };
25
+ DepositDirection.WITHDRAW = { withdraw: {} };
20
26
  class OracleSource {
21
27
  }
22
28
  exports.OracleSource = OracleSource;
@@ -51,6 +57,19 @@ OrderAction.CANCEL = { cancel: {} };
51
57
  OrderAction.EXPIRE = { expire: {} };
52
58
  OrderAction.FILL = { fill: {} };
53
59
  OrderAction.TRIGGER = { trigger: {} };
60
+ class OrderActionExplanation {
61
+ }
62
+ exports.OrderActionExplanation = OrderActionExplanation;
63
+ OrderActionExplanation.NONE = { none: {} };
64
+ OrderActionExplanation.BREACHED_MARGIN_REQUIREMENT = {
65
+ breachedMarginRequirement: {},
66
+ };
67
+ OrderActionExplanation.ORACLE_PRICE_BREACHED_LIMIT_PRICE = {
68
+ oraclePriceBreachedLimitPrice: {},
69
+ };
70
+ OrderActionExplanation.MARKET_ORDER_FILLED_TO_LIMIT_PRICE = {
71
+ marketOrderFilledToLimitPrice: {},
72
+ };
54
73
  class OrderTriggerCondition {
55
74
  }
56
75
  exports.OrderTriggerCondition = OrderTriggerCondition;
@@ -72,3 +91,24 @@ var TradeSide;
72
91
  TradeSide[TradeSide["Buy"] = 1] = "Buy";
73
92
  TradeSide[TradeSide["Sell"] = 2] = "Sell";
74
93
  })(TradeSide = exports.TradeSide || (exports.TradeSide = {}));
94
+ exports.DefaultOrderParams = {
95
+ orderType: OrderType.MARKET,
96
+ userOrderId: 0,
97
+ direction: PositionDirection.LONG,
98
+ baseAssetAmount: _1.ZERO,
99
+ price: _1.ZERO,
100
+ marketIndex: _1.ZERO,
101
+ reduceOnly: false,
102
+ postOnly: false,
103
+ immediateOrCancel: false,
104
+ triggerPrice: _1.ZERO,
105
+ triggerCondition: OrderTriggerCondition.ABOVE,
106
+ positionLimit: _1.ZERO,
107
+ oraclePriceOffset: _1.ZERO,
108
+ padding0: _1.ZERO,
109
+ padding1: _1.ZERO,
110
+ optionalAccounts: {
111
+ discountToken: false,
112
+ referrer: false,
113
+ },
114
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "0.2.0-master.3",
3
+ "version": "0.2.0-master.6",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -30,7 +30,7 @@
30
30
  },
31
31
  "dependencies": {
32
32
  "@project-serum/anchor": "0.24.2",
33
- "@pythnetwork/client": "2.5.1",
33
+ "@pythnetwork/client": "2.5.3",
34
34
  "@solana/spl-token": "^0.1.6",
35
35
  "@solana/web3.js": "1.41.0",
36
36
  "@switchboard-xyz/switchboard-v2": "^0.0.67",
@@ -39,10 +39,10 @@
39
39
  },
40
40
  "devDependencies": {
41
41
  "@types/chai": "^4.3.1",
42
+ "@types/jest": "^28.1.3",
42
43
  "@types/mocha": "^9.1.1",
43
44
  "@typescript-eslint/eslint-plugin": "^4.28.0",
44
45
  "@typescript-eslint/parser": "^4.28.0",
45
- "@types/jest": "^28.1.3",
46
46
  "chai": "^4.3.6",
47
47
  "eslint": "^7.29.0",
48
48
  "eslint-config-prettier": "^8.3.0",
package/src/admin.ts CHANGED
@@ -85,7 +85,8 @@ export class Admin extends ClearingHouse {
85
85
  initialAssetWeight: BN,
86
86
  maintenanceAssetWeight: BN,
87
87
  initialLiabilityWeight: BN,
88
- maintenanceLiabilityWeight: BN
88
+ maintenanceLiabilityWeight: BN,
89
+ imfFactor = new BN(0)
89
90
  ): Promise<TransactionSignature> {
90
91
  const bankIndex = this.getStateAccount().numberOfBanks;
91
92
  const bank = await getBankPublicKey(this.program.programId, bankIndex);
@@ -109,6 +110,7 @@ export class Admin extends ClearingHouse {
109
110
  maintenanceAssetWeight,
110
111
  initialLiabilityWeight,
111
112
  maintenanceLiabilityWeight,
113
+ imfFactor,
112
114
  {
113
115
  accounts: {
114
116
  admin: this.wallet.publicKey,
@@ -329,10 +331,12 @@ export class Admin extends ClearingHouse {
329
331
  recipient: PublicKey
330
332
  ): Promise<TransactionSignature> {
331
333
  const state = await this.getStateAccount();
334
+ const bank = this.getQuoteAssetBankAccount();
332
335
  return await this.program.rpc.withdrawFromInsuranceVault(amount, {
333
336
  accounts: {
334
337
  admin: this.wallet.publicKey,
335
338
  state: await this.getStatePublicKey(),
339
+ bank: bank.pubkey,
336
340
  insuranceVault: state.insuranceVault,
337
341
  insuranceVaultAuthority: state.insuranceVaultAuthority,
338
342
  recipient: recipient,
@@ -341,7 +345,7 @@ export class Admin extends ClearingHouse {
341
345
  });
342
346
  }
343
347
 
344
- public async withdrawFees(
348
+ public async withdrawFromMarketToInsuranceVault(
345
349
  marketIndex: BN,
346
350
  amount: BN,
347
351
  recipient: PublicKey
@@ -351,7 +355,7 @@ export class Admin extends ClearingHouse {
351
355
  marketIndex
352
356
  );
353
357
  const bank = this.getQuoteAssetBankAccount();
354
- return await this.program.rpc.withdrawFees(amount, {
358
+ return await this.program.rpc.withdrawFromMarketToInsuranceVault(amount, {
355
359
  accounts: {
356
360
  admin: this.wallet.publicKey,
357
361
  state: await this.getStatePublicKey(),
@@ -370,6 +374,8 @@ export class Admin extends ClearingHouse {
370
374
  amount: BN
371
375
  ): Promise<TransactionSignature> {
372
376
  const state = await this.getStateAccount();
377
+ const bank = this.getQuoteAssetBankAccount();
378
+
373
379
  return await this.program.rpc.withdrawFromInsuranceVaultToMarket(amount, {
374
380
  accounts: {
375
381
  admin: this.wallet.publicKey,
@@ -377,7 +383,9 @@ export class Admin extends ClearingHouse {
377
383
  market: await getMarketPublicKey(this.program.programId, marketIndex),
378
384
  insuranceVault: state.insuranceVault,
379
385
  insuranceVaultAuthority: state.insuranceVaultAuthority,
380
- bankVault: this.getQuoteAssetBankAccount().vault,
386
+ bank: bank.pubkey,
387
+ bankVault: bank.vault,
388
+ bankVaultAuthority: bank.vaultAuthority,
381
389
  tokenProgram: TOKEN_PROGRAM_ID,
382
390
  },
383
391
  });
@@ -675,11 +683,13 @@ export class Admin extends ClearingHouse {
675
683
  });
676
684
  }
677
685
 
678
- public async updateOrderAuctionTime(
679
- time: BN | number
686
+ public async updateAuctionDuration(
687
+ minDuration: BN | number,
688
+ maxDuration: BN | number
680
689
  ): Promise<TransactionSignature> {
681
- return await this.program.rpc.updateOrderAuctionTime(
682
- typeof time === 'number' ? time : time.toNumber,
690
+ return await this.program.rpc.updateAuctionDuration(
691
+ typeof minDuration === 'number' ? minDuration : minDuration.toNumber(),
692
+ typeof maxDuration === 'number' ? maxDuration : maxDuration.toNumber(),
683
693
  {
684
694
  accounts: {
685
695
  admin: this.wallet.publicKey,
@@ -688,4 +698,33 @@ export class Admin extends ClearingHouse {
688
698
  }
689
699
  );
690
700
  }
701
+
702
+ public async updateMaxBaseAssetAmountRatio(
703
+ marketIndex: BN,
704
+ maxBaseAssetAmountRatio: number
705
+ ): Promise<TransactionSignature> {
706
+ return await this.program.rpc.updateMaxBaseAssetAmountRatio(
707
+ maxBaseAssetAmountRatio,
708
+ {
709
+ accounts: {
710
+ admin: this.wallet.publicKey,
711
+ state: await this.getStatePublicKey(),
712
+ market: this.getMarketAccount(marketIndex).pubkey,
713
+ },
714
+ }
715
+ );
716
+ }
717
+
718
+ public async updateMaxSlippageRatio(
719
+ marketIndex: BN,
720
+ maxSlippageRatio: number
721
+ ): Promise<TransactionSignature> {
722
+ return await this.program.rpc.updateMaxSlippageRatio(maxSlippageRatio, {
723
+ accounts: {
724
+ admin: this.wallet.publicKey,
725
+ state: await this.getStatePublicKey(),
726
+ market: this.getMarketAccount(marketIndex).pubkey,
727
+ },
728
+ });
729
+ }
691
730
  }