@drift-labs/sdk 0.2.0-master.4 → 0.2.0-master.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.
@@ -1,6 +1,5 @@
1
1
  /// <reference types="node" />
2
2
  /// <reference types="bn.js" />
3
- /// <reference types="node" />
4
3
  import { BankAccount, MarketAccount, OracleSource, StateAccount, UserAccount } from '../types';
5
4
  import StrictEventEmitter from 'strict-event-emitter-types';
6
5
  import { EventEmitter } from 'events';
package/lib/admin.d.ts CHANGED
@@ -5,7 +5,7 @@ import { BN } from '@project-serum/anchor';
5
5
  import { ClearingHouse } from './clearingHouse';
6
6
  export declare class Admin extends ClearingHouse {
7
7
  initialize(usdcMint: PublicKey, adminControlsPrices: boolean): Promise<[TransactionSignature]>;
8
- initializeBank(mint: PublicKey, optimalUtilization: BN, optimalRate: BN, maxRate: BN, oracle: PublicKey, oracleSource: OracleSource, initialAssetWeight: BN, maintenanceAssetWeight: BN, initialLiabilityWeight: BN, maintenanceLiabilityWeight: BN): Promise<TransactionSignature>;
8
+ initializeBank(mint: PublicKey, optimalUtilization: BN, optimalRate: BN, maxRate: BN, oracle: PublicKey, oracleSource: OracleSource, initialAssetWeight: BN, maintenanceAssetWeight: BN, initialLiabilityWeight: BN, maintenanceLiabilityWeight: BN, imfFactor?: BN): Promise<TransactionSignature>;
9
9
  initializeMarket(priceOracle: PublicKey, baseAssetReserve: BN, quoteAssetReserve: BN, periodicity: BN, pegMultiplier?: BN, oracleSource?: OracleSource, marginRatioInitial?: number, marginRatioPartial?: number, marginRatioMaintenance?: number): Promise<TransactionSignature>;
10
10
  moveAmmPrice(baseAssetReserve: BN, quoteAssetReserve: BN, marketIndex: BN): Promise<TransactionSignature>;
11
11
  updateK(sqrtK: BN, marketIndex: BN): Promise<TransactionSignature>;
@@ -14,7 +14,7 @@ export declare class Admin extends ClearingHouse {
14
14
  updateAmmOracleTwap(marketIndex: BN): Promise<TransactionSignature>;
15
15
  resetAmmOracleTwap(marketIndex: BN): Promise<TransactionSignature>;
16
16
  withdrawFromInsuranceVault(amount: BN, recipient: PublicKey): Promise<TransactionSignature>;
17
- withdrawFees(marketIndex: BN, amount: BN, recipient: PublicKey): Promise<TransactionSignature>;
17
+ withdrawFromMarketToInsuranceVault(marketIndex: BN, amount: BN, recipient: PublicKey): Promise<TransactionSignature>;
18
18
  withdrawFromInsuranceVaultToMarket(marketIndex: BN, amount: BN): Promise<TransactionSignature>;
19
19
  updateAdmin(admin: PublicKey): Promise<TransactionSignature>;
20
20
  updateCurveUpdateIntensity(marketIndex: BN, curveUpdateIntensity: number): Promise<TransactionSignature>;
package/lib/admin.js CHANGED
@@ -58,12 +58,12 @@ class Admin extends clearingHouse_1.ClearingHouse {
58
58
  const { txSig: initializeTxSig } = await this.txSender.send(initializeTx, [], this.opts);
59
59
  return [initializeTxSig];
60
60
  }
61
- async initializeBank(mint, optimalUtilization, optimalRate, maxRate, oracle, oracleSource, initialAssetWeight, maintenanceAssetWeight, initialLiabilityWeight, maintenanceLiabilityWeight) {
61
+ async initializeBank(mint, optimalUtilization, optimalRate, maxRate, oracle, oracleSource, initialAssetWeight, maintenanceAssetWeight, initialLiabilityWeight, maintenanceLiabilityWeight, imfFactor = new anchor_1.BN(0)) {
62
62
  const bankIndex = this.getStateAccount().numberOfBanks;
63
63
  const bank = await (0, pda_1.getBankPublicKey)(this.program.programId, bankIndex);
64
64
  const bankVault = await (0, pda_1.getBankVaultPublicKey)(this.program.programId, bankIndex);
65
65
  const bankVaultAuthority = await (0, pda_1.getBankVaultAuthorityPublicKey)(this.program.programId, bankIndex);
66
- const initializeTx = await this.program.transaction.initializeBank(optimalUtilization, optimalRate, maxRate, oracleSource, initialAssetWeight, maintenanceAssetWeight, initialLiabilityWeight, maintenanceLiabilityWeight, {
66
+ const initializeTx = await this.program.transaction.initializeBank(optimalUtilization, optimalRate, maxRate, oracleSource, initialAssetWeight, maintenanceAssetWeight, initialLiabilityWeight, maintenanceLiabilityWeight, imfFactor, {
67
67
  accounts: {
68
68
  admin: this.wallet.publicKey,
69
69
  state: await this.getStatePublicKey(),
@@ -177,10 +177,12 @@ class Admin extends clearingHouse_1.ClearingHouse {
177
177
  }
178
178
  async withdrawFromInsuranceVault(amount, recipient) {
179
179
  const state = await this.getStateAccount();
180
+ const bank = this.getQuoteAssetBankAccount();
180
181
  return await this.program.rpc.withdrawFromInsuranceVault(amount, {
181
182
  accounts: {
182
183
  admin: this.wallet.publicKey,
183
184
  state: await this.getStatePublicKey(),
185
+ bank: bank.pubkey,
184
186
  insuranceVault: state.insuranceVault,
185
187
  insuranceVaultAuthority: state.insuranceVaultAuthority,
186
188
  recipient: recipient,
@@ -188,10 +190,10 @@ class Admin extends clearingHouse_1.ClearingHouse {
188
190
  },
189
191
  });
190
192
  }
191
- async withdrawFees(marketIndex, amount, recipient) {
193
+ async withdrawFromMarketToInsuranceVault(marketIndex, amount, recipient) {
192
194
  const marketPublicKey = await (0, pda_1.getMarketPublicKey)(this.program.programId, marketIndex);
193
195
  const bank = this.getQuoteAssetBankAccount();
194
- return await this.program.rpc.withdrawFees(amount, {
196
+ return await this.program.rpc.withdrawFromMarketToInsuranceVault(amount, {
195
197
  accounts: {
196
198
  admin: this.wallet.publicKey,
197
199
  state: await this.getStatePublicKey(),
@@ -206,6 +208,7 @@ class Admin extends clearingHouse_1.ClearingHouse {
206
208
  }
207
209
  async withdrawFromInsuranceVaultToMarket(marketIndex, amount) {
208
210
  const state = await this.getStateAccount();
211
+ const bank = this.getQuoteAssetBankAccount();
209
212
  return await this.program.rpc.withdrawFromInsuranceVaultToMarket(amount, {
210
213
  accounts: {
211
214
  admin: this.wallet.publicKey,
@@ -213,7 +216,9 @@ class Admin extends clearingHouse_1.ClearingHouse {
213
216
  market: await (0, pda_1.getMarketPublicKey)(this.program.programId, marketIndex),
214
217
  insuranceVault: state.insuranceVault,
215
218
  insuranceVaultAuthority: state.insuranceVaultAuthority,
216
- bankVault: this.getQuoteAssetBankAccount().vault,
219
+ bank: bank.pubkey,
220
+ bankVault: bank.vault,
221
+ bankVaultAuthority: bank.vaultAuthority,
217
222
  tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
218
223
  },
219
224
  });
@@ -1,7 +1,7 @@
1
1
  /// <reference types="node" />
2
2
  /// <reference types="bn.js" />
3
3
  import { AnchorProvider, BN, Program } from '@project-serum/anchor';
4
- import { StateAccount, IWallet, PositionDirection, UserAccount, MarketAccount, OrderParams, Order, BankAccount, UserBankBalance, MakerInfo, OptionalOrderParams } from './types';
4
+ import { StateAccount, IWallet, PositionDirection, UserAccount, MarketAccount, OrderParams, Order, BankAccount, UserBankBalance, MakerInfo, TakerInfo, OptionalOrderParams } from './types';
5
5
  import { Connection, PublicKey, TransactionSignature, ConfirmOptions, TransactionInstruction, AccountMeta } from '@solana/web3.js';
6
6
  import { MockUSDCFaucet } from './mockUSDCFaucet';
7
7
  import { EventEmitter } from 'events';
@@ -110,6 +110,8 @@ export declare class ClearingHouse {
110
110
  getTriggerOrderIx(userAccountPublicKey: PublicKey, userAccount: UserAccount, order: Order): Promise<TransactionInstruction>;
111
111
  placeAndTake(orderParams: OptionalOrderParams, makerInfo?: MakerInfo): Promise<TransactionSignature>;
112
112
  getPlaceAndTakeIx(orderParams: OptionalOrderParams, makerInfo?: MakerInfo): Promise<TransactionInstruction>;
113
+ placeAndMake(orderParams: OptionalOrderParams, takerInfo: TakerInfo): Promise<TransactionSignature>;
114
+ getPlaceAndMakeIx(orderParams: OptionalOrderParams, takerInfo: TakerInfo): Promise<TransactionInstruction>;
113
115
  /**
114
116
  * Close an entire position. If you want to reduce a position, use the {@link openPosition} method in the opposite direction of the current position.
115
117
  * @param marketIndex
@@ -734,6 +734,34 @@ class ClearingHouse {
734
734
  remainingAccounts,
735
735
  });
736
736
  }
737
+ async placeAndMake(orderParams, takerInfo) {
738
+ const { txSig, slot } = await this.txSender.send((0, utils_1.wrapInTx)(await this.getPlaceAndMakeIx(orderParams, takerInfo)), [], this.opts);
739
+ this.marketLastSlotCache.set(orderParams.marketIndex.toNumber(), slot);
740
+ return txSig;
741
+ }
742
+ async getPlaceAndMakeIx(orderParams, takerInfo) {
743
+ orderParams = this.getOrderParams(orderParams);
744
+ const userAccountPublicKey = await this.getUserAccountPublicKey();
745
+ const remainingAccounts = this.getRemainingAccounts({
746
+ writableMarketIndex: orderParams.marketIndex,
747
+ writableBankIndex: numericConstants_1.QUOTE_ASSET_BANK_INDEX,
748
+ });
749
+ const takerOrderId = takerInfo.order.orderId;
750
+ remainingAccounts.push({
751
+ pubkey: takerInfo.taker,
752
+ isSigner: false,
753
+ isWritable: true,
754
+ });
755
+ return await this.program.instruction.placeAndMake(orderParams, takerOrderId, {
756
+ accounts: {
757
+ state: await this.getStatePublicKey(),
758
+ user: userAccountPublicKey,
759
+ taker: takerInfo.taker,
760
+ authority: this.wallet.publicKey,
761
+ },
762
+ remainingAccounts,
763
+ });
764
+ }
737
765
  /**
738
766
  * Close an entire position. If you want to reduce a position, use the {@link openPosition} method in the opposite direction of the current position.
739
767
  * @param marketIndex
package/lib/config.js CHANGED
@@ -7,7 +7,7 @@ exports.configs = {
7
7
  devnet: {
8
8
  ENV: 'devnet',
9
9
  PYTH_ORACLE_MAPPING_ADDRESS: 'BmA9Z6FjioHJPpjT39QazZyhDRUdZy2ezwx4GiDdE2u2',
10
- CLEARING_HOUSE_PROGRAM_ID: '9jwr5nC2f9yAraXrg4UzHXmCX3vi9FQkjD6p9e8bRqNa',
10
+ CLEARING_HOUSE_PROGRAM_ID: 'BMow898PH56jD8z4EaqxicoGXkR1HhN17qrER6Uc4AYq',
11
11
  USDC_MINT_ADDRESS: '8zGuJQqwhZafTah7Uc7Z4tXRnguqkn5KLFAP8oV6PHe2',
12
12
  MARKETS: markets_1.DevnetMarkets,
13
13
  BANKS: banks_1.DevnetBanks,
@@ -141,6 +141,10 @@
141
141
  {
142
142
  "name": "maintenanceLiabilityWeight",
143
143
  "type": "u128"
144
+ },
145
+ {
146
+ "name": "imfFactor",
147
+ "type": "u128"
144
148
  }
145
149
  ]
146
150
  },
@@ -534,7 +538,7 @@
534
538
  },
535
539
  {
536
540
  "name": "taker",
537
- "isMut": false,
541
+ "isMut": true,
538
542
  "isSigner": false
539
543
  },
540
544
  {
@@ -716,7 +720,7 @@
716
720
  ]
717
721
  },
718
722
  {
719
- "name": "withdrawFees",
723
+ "name": "withdrawFromMarketToInsuranceVault",
720
724
  "accounts": [
721
725
  {
722
726
  "name": "state",
@@ -730,7 +734,7 @@
730
734
  },
731
735
  {
732
736
  "name": "bank",
733
- "isMut": false,
737
+ "isMut": true,
734
738
  "isSigner": false
735
739
  },
736
740
  {
@@ -835,6 +839,11 @@
835
839
  "isMut": false,
836
840
  "isSigner": false
837
841
  },
842
+ {
843
+ "name": "bank",
844
+ "isMut": true,
845
+ "isSigner": false
846
+ },
838
847
  {
839
848
  "name": "bankVault",
840
849
  "isMut": true,
@@ -1093,6 +1102,62 @@
1093
1102
  }
1094
1103
  ]
1095
1104
  },
1105
+ {
1106
+ "name": "updateMarketImfFactor",
1107
+ "accounts": [
1108
+ {
1109
+ "name": "admin",
1110
+ "isMut": false,
1111
+ "isSigner": true
1112
+ },
1113
+ {
1114
+ "name": "state",
1115
+ "isMut": false,
1116
+ "isSigner": false
1117
+ },
1118
+ {
1119
+ "name": "market",
1120
+ "isMut": true,
1121
+ "isSigner": false
1122
+ }
1123
+ ],
1124
+ "args": [
1125
+ {
1126
+ "name": "imfFactor",
1127
+ "type": "u128"
1128
+ }
1129
+ ]
1130
+ },
1131
+ {
1132
+ "name": "updateMarketUnsettledAssetWeight",
1133
+ "accounts": [
1134
+ {
1135
+ "name": "admin",
1136
+ "isMut": false,
1137
+ "isSigner": true
1138
+ },
1139
+ {
1140
+ "name": "state",
1141
+ "isMut": false,
1142
+ "isSigner": false
1143
+ },
1144
+ {
1145
+ "name": "market",
1146
+ "isMut": true,
1147
+ "isSigner": false
1148
+ }
1149
+ ],
1150
+ "args": [
1151
+ {
1152
+ "name": "unsettledInitialAssetWeight",
1153
+ "type": "u8"
1154
+ },
1155
+ {
1156
+ "name": "unsettledMaintenanceAssetWeight",
1157
+ "type": "u8"
1158
+ }
1159
+ ]
1160
+ },
1096
1161
  {
1097
1162
  "name": "updateCurveUpdateIntensity",
1098
1163
  "accounts": [
@@ -1731,6 +1796,10 @@
1731
1796
  {
1732
1797
  "name": "maintenanceLiabilityWeight",
1733
1798
  "type": "u128"
1799
+ },
1800
+ {
1801
+ "name": "imfFactor",
1802
+ "type": "u128"
1734
1803
  }
1735
1804
  ]
1736
1805
  }
@@ -1808,6 +1877,22 @@
1808
1877
  "name": "unsettledLoss",
1809
1878
  "type": "u128"
1810
1879
  },
1880
+ {
1881
+ "name": "imfFactor",
1882
+ "type": "u128"
1883
+ },
1884
+ {
1885
+ "name": "unsettledInitialAssetWeight",
1886
+ "type": "u8"
1887
+ },
1888
+ {
1889
+ "name": "unsettledMaintenanceAssetWeight",
1890
+ "type": "u8"
1891
+ },
1892
+ {
1893
+ "name": "unsettledImfFactor",
1894
+ "type": "u128"
1895
+ },
1811
1896
  {
1812
1897
  "name": "padding0",
1813
1898
  "type": "u32"
@@ -101,7 +101,9 @@ function calculatePositionFundingPNL(market, marketPosition) {
101
101
  }
102
102
  exports.calculatePositionFundingPNL = calculatePositionFundingPNL;
103
103
  function positionIsAvailable(position) {
104
- return position.baseAssetAmount.eq(numericConstants_1.ZERO) && position.openOrders.eq(numericConstants_1.ZERO);
104
+ return (position.baseAssetAmount.eq(numericConstants_1.ZERO) &&
105
+ position.openOrders.eq(numericConstants_1.ZERO) &&
106
+ position.unsettledPnl.eq(numericConstants_1.ZERO));
105
107
  }
106
108
  exports.positionIsAvailable = positionIsAvailable;
107
109
  /**
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: {};
@@ -132,6 +140,7 @@ export declare type DepositRecord = {
132
140
  };
133
141
  bankIndex: BN;
134
142
  amount: BN;
143
+ oraclePrice: BN;
135
144
  from?: PublicKey;
136
145
  to?: PublicKey;
137
146
  };
@@ -453,6 +462,10 @@ export declare type MakerInfo = {
453
462
  maker: PublicKey;
454
463
  order: Order;
455
464
  };
465
+ export declare type TakerInfo = {
466
+ taker: PublicKey;
467
+ order: Order;
468
+ };
456
469
  export interface IWallet {
457
470
  signTransaction(tx: Transaction): Promise<Transaction>;
458
471
  signAllTransactions(txs: Transaction[]): Promise<Transaction[]>;
package/lib/types.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DefaultOrderParams = exports.TradeSide = exports.isOneOfVariant = exports.isVariant = exports.OrderTriggerCondition = exports.OrderActionExplanation = 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
4
  const _1 = require(".");
5
5
  // # Utility Types / Enums / Constants
6
6
  class SwapDirection {
@@ -18,6 +18,11 @@ class PositionDirection {
18
18
  exports.PositionDirection = PositionDirection;
19
19
  PositionDirection.LONG = { long: {} };
20
20
  PositionDirection.SHORT = { short: {} };
21
+ class DepositDirection {
22
+ }
23
+ exports.DepositDirection = DepositDirection;
24
+ DepositDirection.DEPOSIT = { deposit: {} };
25
+ DepositDirection.WITHDRAW = { withdraw: {} };
21
26
  class OracleSource {
22
27
  }
23
28
  exports.OracleSource = OracleSource;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "0.2.0-master.4",
3
+ "version": "0.2.0-master.5",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
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
  });
@@ -11,6 +11,7 @@ import {
11
11
  BankAccount,
12
12
  UserBankBalance,
13
13
  MakerInfo,
14
+ TakerInfo,
14
15
  OptionalOrderParams,
15
16
  DefaultOrderParams,
16
17
  OrderType,
@@ -1024,6 +1025,7 @@ export class ClearingHouse {
1024
1025
 
1025
1026
  const orderId = order.orderId;
1026
1027
  const makerOrderId = makerInfo ? makerInfo.order.orderId : null;
1028
+
1027
1029
  return await this.program.instruction.fillOrder(orderId, makerOrderId, {
1028
1030
  accounts: {
1029
1031
  state: await this.getStatePublicKey(),
@@ -1162,6 +1164,55 @@ export class ClearingHouse {
1162
1164
  );
1163
1165
  }
1164
1166
 
1167
+ public async placeAndMake(
1168
+ orderParams: OptionalOrderParams,
1169
+ takerInfo: TakerInfo,
1170
+ ): Promise<TransactionSignature> {
1171
+ const { txSig, slot } = await this.txSender.send(
1172
+ wrapInTx(await this.getPlaceAndMakeIx(orderParams, takerInfo)),
1173
+ [],
1174
+ this.opts
1175
+ );
1176
+
1177
+ this.marketLastSlotCache.set(orderParams.marketIndex.toNumber(), slot);
1178
+
1179
+ return txSig;
1180
+ }
1181
+
1182
+ public async getPlaceAndMakeIx(
1183
+ orderParams: OptionalOrderParams,
1184
+ takerInfo: TakerInfo,
1185
+ ): Promise<TransactionInstruction> {
1186
+ orderParams = this.getOrderParams(orderParams);
1187
+ const userAccountPublicKey = await this.getUserAccountPublicKey();
1188
+
1189
+ const remainingAccounts = this.getRemainingAccounts({
1190
+ writableMarketIndex: orderParams.marketIndex,
1191
+ writableBankIndex: QUOTE_ASSET_BANK_INDEX,
1192
+ });
1193
+
1194
+ const takerOrderId = takerInfo!.order!.orderId;
1195
+ remainingAccounts.push({
1196
+ pubkey: takerInfo.taker,
1197
+ isSigner: false,
1198
+ isWritable: true,
1199
+ });
1200
+
1201
+ return await this.program.instruction.placeAndMake(
1202
+ orderParams,
1203
+ takerOrderId,
1204
+ {
1205
+ accounts: {
1206
+ state: await this.getStatePublicKey(),
1207
+ user: userAccountPublicKey,
1208
+ taker: takerInfo.taker,
1209
+ authority: this.wallet.publicKey,
1210
+ },
1211
+ remainingAccounts,
1212
+ }
1213
+ );
1214
+ }
1215
+
1165
1216
  /**
1166
1217
  * Close an entire position. If you want to reduce a position, use the {@link openPosition} method in the opposite direction of the current position.
1167
1218
  * @param marketIndex
package/src/config.ts CHANGED
@@ -28,7 +28,7 @@ export const configs: { [key in DriftEnv]: DriftConfig } = {
28
28
  devnet: {
29
29
  ENV: 'devnet',
30
30
  PYTH_ORACLE_MAPPING_ADDRESS: 'BmA9Z6FjioHJPpjT39QazZyhDRUdZy2ezwx4GiDdE2u2',
31
- CLEARING_HOUSE_PROGRAM_ID: '9jwr5nC2f9yAraXrg4UzHXmCX3vi9FQkjD6p9e8bRqNa',
31
+ CLEARING_HOUSE_PROGRAM_ID: 'BMow898PH56jD8z4EaqxicoGXkR1HhN17qrER6Uc4AYq',
32
32
  USDC_MINT_ADDRESS: '8zGuJQqwhZafTah7Uc7Z4tXRnguqkn5KLFAP8oV6PHe2',
33
33
  MARKETS: DevnetMarkets,
34
34
  BANKS: DevnetBanks,
@@ -141,6 +141,10 @@
141
141
  {
142
142
  "name": "maintenanceLiabilityWeight",
143
143
  "type": "u128"
144
+ },
145
+ {
146
+ "name": "imfFactor",
147
+ "type": "u128"
144
148
  }
145
149
  ]
146
150
  },
@@ -534,7 +538,7 @@
534
538
  },
535
539
  {
536
540
  "name": "taker",
537
- "isMut": false,
541
+ "isMut": true,
538
542
  "isSigner": false
539
543
  },
540
544
  {
@@ -716,7 +720,7 @@
716
720
  ]
717
721
  },
718
722
  {
719
- "name": "withdrawFees",
723
+ "name": "withdrawFromMarketToInsuranceVault",
720
724
  "accounts": [
721
725
  {
722
726
  "name": "state",
@@ -730,7 +734,7 @@
730
734
  },
731
735
  {
732
736
  "name": "bank",
733
- "isMut": false,
737
+ "isMut": true,
734
738
  "isSigner": false
735
739
  },
736
740
  {
@@ -835,6 +839,11 @@
835
839
  "isMut": false,
836
840
  "isSigner": false
837
841
  },
842
+ {
843
+ "name": "bank",
844
+ "isMut": true,
845
+ "isSigner": false
846
+ },
838
847
  {
839
848
  "name": "bankVault",
840
849
  "isMut": true,
@@ -1093,6 +1102,62 @@
1093
1102
  }
1094
1103
  ]
1095
1104
  },
1105
+ {
1106
+ "name": "updateMarketImfFactor",
1107
+ "accounts": [
1108
+ {
1109
+ "name": "admin",
1110
+ "isMut": false,
1111
+ "isSigner": true
1112
+ },
1113
+ {
1114
+ "name": "state",
1115
+ "isMut": false,
1116
+ "isSigner": false
1117
+ },
1118
+ {
1119
+ "name": "market",
1120
+ "isMut": true,
1121
+ "isSigner": false
1122
+ }
1123
+ ],
1124
+ "args": [
1125
+ {
1126
+ "name": "imfFactor",
1127
+ "type": "u128"
1128
+ }
1129
+ ]
1130
+ },
1131
+ {
1132
+ "name": "updateMarketUnsettledAssetWeight",
1133
+ "accounts": [
1134
+ {
1135
+ "name": "admin",
1136
+ "isMut": false,
1137
+ "isSigner": true
1138
+ },
1139
+ {
1140
+ "name": "state",
1141
+ "isMut": false,
1142
+ "isSigner": false
1143
+ },
1144
+ {
1145
+ "name": "market",
1146
+ "isMut": true,
1147
+ "isSigner": false
1148
+ }
1149
+ ],
1150
+ "args": [
1151
+ {
1152
+ "name": "unsettledInitialAssetWeight",
1153
+ "type": "u8"
1154
+ },
1155
+ {
1156
+ "name": "unsettledMaintenanceAssetWeight",
1157
+ "type": "u8"
1158
+ }
1159
+ ]
1160
+ },
1096
1161
  {
1097
1162
  "name": "updateCurveUpdateIntensity",
1098
1163
  "accounts": [
@@ -1731,6 +1796,10 @@
1731
1796
  {
1732
1797
  "name": "maintenanceLiabilityWeight",
1733
1798
  "type": "u128"
1799
+ },
1800
+ {
1801
+ "name": "imfFactor",
1802
+ "type": "u128"
1734
1803
  }
1735
1804
  ]
1736
1805
  }
@@ -1808,6 +1877,22 @@
1808
1877
  "name": "unsettledLoss",
1809
1878
  "type": "u128"
1810
1879
  },
1880
+ {
1881
+ "name": "imfFactor",
1882
+ "type": "u128"
1883
+ },
1884
+ {
1885
+ "name": "unsettledInitialAssetWeight",
1886
+ "type": "u8"
1887
+ },
1888
+ {
1889
+ "name": "unsettledMaintenanceAssetWeight",
1890
+ "type": "u8"
1891
+ },
1892
+ {
1893
+ "name": "unsettledImfFactor",
1894
+ "type": "u128"
1895
+ },
1811
1896
  {
1812
1897
  "name": "padding0",
1813
1898
  "type": "u32"
@@ -153,7 +153,11 @@ export function calculatePositionFundingPNL(
153
153
  }
154
154
 
155
155
  export function positionIsAvailable(position: UserPosition): boolean {
156
- return position.baseAssetAmount.eq(ZERO) && position.openOrders.eq(ZERO);
156
+ return (
157
+ position.baseAssetAmount.eq(ZERO) &&
158
+ position.openOrders.eq(ZERO) &&
159
+ position.unsettledPnl.eq(ZERO)
160
+ );
157
161
  }
158
162
 
159
163
  /**
package/src/tx/utils.ts CHANGED
@@ -8,7 +8,7 @@ const COMPUTE_UNITS_DEFAULT = 200_000;
8
8
 
9
9
  export function wrapInTx(
10
10
  instruction: TransactionInstruction,
11
- computeUnits = 500_000 // TODO, requires less code change
11
+ computeUnits = 600_000 // TODO, requires less code change
12
12
  ): Transaction {
13
13
  const tx = new Transaction();
14
14
  if (computeUnits != COMPUTE_UNITS_DEFAULT) {
package/src/types.ts CHANGED
@@ -17,6 +17,11 @@ export class PositionDirection {
17
17
  static readonly SHORT = { short: {} };
18
18
  }
19
19
 
20
+ export class DepositDirection {
21
+ static readonly DEPOSIT = { deposit: {} };
22
+ static readonly WITHDRAW = { withdraw: {} };
23
+ }
24
+
20
25
  export class OracleSource {
21
26
  static readonly PYTH = { pyth: {} };
22
27
  static readonly SWITCHBOARD = { switchboard: {} };
@@ -105,6 +110,7 @@ export type DepositRecord = {
105
110
  };
106
111
  bankIndex: BN;
107
112
  amount: BN;
113
+ oraclePrice: BN;
108
114
  from?: PublicKey;
109
115
  to?: PublicKey;
110
116
  };
@@ -441,6 +447,11 @@ export type MakerInfo = {
441
447
  order: Order;
442
448
  };
443
449
 
450
+ export type TakerInfo = {
451
+ taker: PublicKey;
452
+ order: Order;
453
+ };
454
+
444
455
  // # Misc Types
445
456
  export interface IWallet {
446
457
  signTransaction(tx: Transaction): Promise<Transaction>;