@drift-labs/sdk 0.2.0-master.1 → 0.2.0-master.10

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.
Files changed (112) hide show
  1. package/lib/accounts/types.d.ts +1 -0
  2. package/lib/admin.d.ts +6 -3
  3. package/lib/admin.js +39 -7
  4. package/lib/clearingHouse.d.ts +13 -14
  5. package/lib/clearingHouse.js +166 -106
  6. package/lib/config.js +1 -1
  7. package/lib/constants/banks.js +8 -1
  8. package/lib/constants/numericConstants.d.ts +1 -0
  9. package/lib/constants/numericConstants.js +2 -1
  10. package/lib/factory/bigNum.d.ts +8 -2
  11. package/lib/factory/bigNum.js +14 -6
  12. package/lib/idl/clearing_house.json +277 -55
  13. package/lib/index.d.ts +2 -1
  14. package/lib/index.js +6 -1
  15. package/lib/math/amm.d.ts +6 -1
  16. package/lib/math/amm.js +124 -41
  17. package/lib/math/auction.js +4 -1
  18. package/lib/math/orders.d.ts +2 -2
  19. package/lib/math/orders.js +18 -11
  20. package/lib/math/position.js +3 -1
  21. package/lib/math/repeg.js +1 -1
  22. package/lib/math/trade.d.ts +1 -1
  23. package/lib/math/trade.js +7 -10
  24. package/lib/orderParams.d.ts +14 -5
  25. package/lib/orderParams.js +8 -96
  26. package/lib/orders.d.ts +1 -2
  27. package/lib/orders.js +6 -85
  28. package/lib/slot/SlotSubscriber.d.ts +7 -0
  29. package/lib/slot/SlotSubscriber.js +3 -0
  30. package/lib/tx/utils.js +1 -1
  31. package/lib/types.d.ts +75 -1
  32. package/lib/types.js +42 -1
  33. package/package.json +3 -3
  34. package/src/accounts/bulkAccountLoader.js +197 -0
  35. package/src/accounts/bulkUserSubscription.js +33 -0
  36. package/src/accounts/fetch.js +29 -0
  37. package/src/accounts/pollingClearingHouseAccountSubscriber.js +311 -0
  38. package/src/accounts/pollingOracleSubscriber.js +93 -0
  39. package/src/accounts/pollingTokenAccountSubscriber.js +90 -0
  40. package/src/accounts/pollingUserAccountSubscriber.js +132 -0
  41. package/src/accounts/types.js +10 -0
  42. package/src/accounts/utils.js +7 -0
  43. package/src/accounts/webSocketAccountSubscriber.js +93 -0
  44. package/src/accounts/webSocketClearingHouseAccountSubscriber.js +233 -0
  45. package/src/accounts/webSocketUserAccountSubscriber.js +62 -0
  46. package/src/addresses/marketAddresses.js +26 -0
  47. package/src/addresses/pda.js +104 -0
  48. package/src/admin.ts +60 -8
  49. package/src/assert/assert.js +9 -0
  50. package/src/clearingHouse.ts +223 -183
  51. package/src/config.ts +1 -1
  52. package/src/constants/banks.ts +8 -1
  53. package/src/constants/numericConstants.ts +1 -0
  54. package/src/events/eventList.js +77 -0
  55. package/src/events/eventSubscriber.js +139 -0
  56. package/src/events/fetchLogs.js +50 -0
  57. package/src/events/pollingLogProvider.js +64 -0
  58. package/src/events/sort.js +44 -0
  59. package/src/events/txEventCache.js +71 -0
  60. package/src/events/types.js +20 -0
  61. package/src/events/webSocketLogProvider.js +41 -0
  62. package/src/examples/makeTradeExample.js +80 -0
  63. package/src/factory/bigNum.js +364 -0
  64. package/src/factory/bigNum.ts +26 -9
  65. package/src/factory/oracleClient.js +20 -0
  66. package/src/idl/clearing_house.json +277 -55
  67. package/src/index.js +69 -0
  68. package/src/index.ts +2 -1
  69. package/src/math/amm.js +369 -0
  70. package/src/math/amm.ts +207 -52
  71. package/src/math/auction.js +42 -0
  72. package/src/math/auction.ts +5 -1
  73. package/src/math/bankBalance.js +75 -0
  74. package/src/math/conversion.js +11 -0
  75. package/src/math/funding.js +248 -0
  76. package/src/math/market.js +57 -0
  77. package/src/math/oracles.js +26 -0
  78. package/src/math/orders.js +110 -0
  79. package/src/math/orders.ts +17 -13
  80. package/src/math/position.js +140 -0
  81. package/src/math/position.ts +5 -1
  82. package/src/math/repeg.js +128 -0
  83. package/src/math/repeg.ts +2 -1
  84. package/src/math/state.js +15 -0
  85. package/src/math/trade.js +253 -0
  86. package/src/math/trade.ts +23 -25
  87. package/src/math/utils.js +0 -1
  88. package/src/mockUSDCFaucet.js +171 -0
  89. package/src/oracles/oracleClientCache.js +19 -0
  90. package/src/oracles/pythClient.js +46 -0
  91. package/src/oracles/quoteAssetOracleClient.js +32 -0
  92. package/src/oracles/switchboardClient.js +69 -0
  93. package/src/oracles/types.js +2 -0
  94. package/src/orderParams.js +20 -0
  95. package/src/orderParams.ts +20 -141
  96. package/src/orders.js +134 -0
  97. package/src/orders.ts +7 -131
  98. package/src/slot/SlotSubscriber.js +39 -0
  99. package/src/slot/SlotSubscriber.ts +11 -1
  100. package/src/token/index.js +38 -0
  101. package/src/tx/retryTxSender.js +188 -0
  102. package/src/tx/types.js +2 -0
  103. package/src/tx/utils.js +17 -0
  104. package/src/tx/utils.ts +1 -1
  105. package/src/types.js +114 -0
  106. package/src/types.ts +69 -3
  107. package/src/userName.js +20 -0
  108. package/src/util/promiseTimeout.js +14 -0
  109. package/src/util/tps.js +27 -0
  110. package/src/wallet.js +35 -0
  111. package/src/util/computeUnits.js +0 -17
  112. package/src/util/computeUnits.js.map +0 -1
@@ -11,6 +11,10 @@ import {
11
11
  BankAccount,
12
12
  UserBankBalance,
13
13
  MakerInfo,
14
+ TakerInfo,
15
+ OptionalOrderParams,
16
+ DefaultOrderParams,
17
+ OrderType,
14
18
  } from './types';
15
19
  import * as anchor from '@project-serum/anchor';
16
20
  import clearingHouseIDL from './idl/clearing_house.json';
@@ -52,7 +56,6 @@ import { WebSocketClearingHouseAccountSubscriber } from './accounts/webSocketCle
52
56
  import { RetryTxSender } from './tx/retryTxSender';
53
57
  import { ClearingHouseUser } from './clearingHouseUser';
54
58
  import { ClearingHouseUserAccountSubscriptionConfig } from './clearingHouseUserConfig';
55
- import { getMarketOrderParams } from './orderParams';
56
59
  import { getMarketsBanksAndOraclesForSubscription } from './config';
57
60
 
58
61
  /**
@@ -561,13 +564,19 @@ export class ClearingHouse {
561
564
  writableBankIndex: bankIndex,
562
565
  });
563
566
  } else {
564
- remainingAccounts = [
565
- {
566
- pubkey: this.getBankAccount(bankIndex).pubkey,
567
+ const bankAccount = this.getBankAccount(bankIndex);
568
+ if (!bankAccount.oracle.equals(PublicKey.default)) {
569
+ remainingAccounts.push({
570
+ pubkey: bankAccount.oracle,
567
571
  isSigner: false,
568
- isWritable: true,
569
- },
570
- ];
572
+ isWritable: false,
573
+ });
574
+ }
575
+ remainingAccounts.push({
576
+ pubkey: bankAccount.pubkey,
577
+ isSigner: false,
578
+ isWritable: true,
579
+ });
571
580
  }
572
581
 
573
582
  const bank = this.getBankAccount(bankIndex);
@@ -798,20 +807,17 @@ export class ClearingHouse {
798
807
  marketIndex: BN,
799
808
  limitPrice?: BN
800
809
  ): Promise<TransactionSignature> {
801
- return await this.placeAndTake(
802
- getMarketOrderParams(
803
- marketIndex,
804
- direction,
805
- ZERO,
806
- amount,
807
- false,
808
- limitPrice
809
- )
810
- );
810
+ return await this.placeAndTake({
811
+ orderType: OrderType.MARKET,
812
+ marketIndex,
813
+ direction,
814
+ baseAssetAmount: amount,
815
+ price: limitPrice,
816
+ });
811
817
  }
812
818
 
813
819
  public async placeOrder(
814
- orderParams: OrderParams
820
+ orderParams: OptionalOrderParams
815
821
  ): Promise<TransactionSignature> {
816
822
  const { txSig, slot } = await this.txSender.send(
817
823
  wrapInTx(await this.getPlaceOrderIx(orderParams)),
@@ -822,14 +828,16 @@ export class ClearingHouse {
822
828
  return txSig;
823
829
  }
824
830
 
831
+ getOrderParams(optionalOrderParams: OptionalOrderParams): OrderParams {
832
+ return Object.assign({}, DefaultOrderParams, optionalOrderParams);
833
+ }
834
+
825
835
  public async getPlaceOrderIx(
826
- orderParams: OrderParams
836
+ orderParams: OptionalOrderParams
827
837
  ): Promise<TransactionInstruction> {
838
+ orderParams = this.getOrderParams(orderParams);
828
839
  const userAccountPublicKey = await this.getUserAccountPublicKey();
829
840
 
830
- const priceOracle = this.getMarketAccount(orderParams.marketIndex).amm
831
- .oracle;
832
-
833
841
  const remainingAccounts = this.getRemainingAccounts({
834
842
  writableMarketIndex: orderParams.marketIndex,
835
843
  });
@@ -839,38 +847,11 @@ export class ClearingHouse {
839
847
  state: await this.getStatePublicKey(),
840
848
  user: userAccountPublicKey,
841
849
  authority: this.wallet.publicKey,
842
- oracle: priceOracle,
843
850
  },
844
851
  remainingAccounts,
845
852
  });
846
853
  }
847
854
 
848
- public async expireOrders(
849
- userAccountPublicKey: PublicKey
850
- ): Promise<TransactionSignature> {
851
- const { txSig } = await this.txSender.send(
852
- wrapInTx(await this.getExpireOrdersIx(userAccountPublicKey)),
853
- [],
854
- this.opts
855
- );
856
- return txSig;
857
- }
858
-
859
- public async getExpireOrdersIx(
860
- userAccountPublicKey: PublicKey
861
- ): Promise<TransactionInstruction> {
862
- const fillerPublicKey = await this.getUserAccountPublicKey();
863
-
864
- return await this.program.instruction.expireOrders({
865
- accounts: {
866
- state: await this.getStatePublicKey(),
867
- filler: fillerPublicKey,
868
- user: userAccountPublicKey,
869
- authority: this.wallet.publicKey,
870
- },
871
- });
872
- }
873
-
874
855
  public async updateAMMs(marketIndexes: BN[]): Promise<TransactionSignature> {
875
856
  const { txSig } = await this.txSender.send(
876
857
  wrapInTx(await this.getUpdateAMMsIx(marketIndexes)),
@@ -914,7 +895,7 @@ export class ClearingHouse {
914
895
  });
915
896
  }
916
897
 
917
- public async cancelOrder(orderId: BN): Promise<TransactionSignature> {
898
+ public async cancelOrder(orderId?: BN): Promise<TransactionSignature> {
918
899
  const { txSig } = await this.txSender.send(
919
900
  wrapInTx(await this.getCancelOrderIx(orderId)),
920
901
  [],
@@ -923,20 +904,16 @@ export class ClearingHouse {
923
904
  return txSig;
924
905
  }
925
906
 
926
- public async getCancelOrderIx(orderId: BN): Promise<TransactionInstruction> {
907
+ public async getCancelOrderIx(orderId?: BN): Promise<TransactionInstruction> {
927
908
  const userAccountPublicKey = await this.getUserAccountPublicKey();
928
909
 
929
- const order = this.getOrder(orderId);
930
- const oracle = this.getMarketAccount(order.marketIndex).amm.oracle;
931
-
932
910
  const remainingAccounts = this.getRemainingAccounts({});
933
911
 
934
- return await this.program.instruction.cancelOrder(orderId, {
912
+ return await this.program.instruction.cancelOrder(orderId ?? null, {
935
913
  accounts: {
936
914
  state: await this.getStatePublicKey(),
937
915
  user: userAccountPublicKey,
938
916
  authority: this.wallet.publicKey,
939
- oracle,
940
917
  },
941
918
  remainingAccounts,
942
919
  });
@@ -974,36 +951,107 @@ export class ClearingHouse {
974
951
  });
975
952
  }
976
953
 
977
- public async cancelAllOrders(
978
- bestEffort?: boolean
954
+ public async fillOrder(
955
+ userAccountPublicKey: PublicKey,
956
+ user: UserAccount,
957
+ order?: Order,
958
+ makerInfo?: MakerInfo
979
959
  ): Promise<TransactionSignature> {
980
960
  const { txSig } = await this.txSender.send(
981
- wrapInTx(await this.getCancelAllOrdersIx(bestEffort)),
961
+ wrapInTx(
962
+ await this.getFillOrderIx(userAccountPublicKey, user, order, makerInfo)
963
+ ),
982
964
  [],
983
965
  this.opts
984
966
  );
985
967
  return txSig;
986
968
  }
987
969
 
988
- public async getCancelAllOrdersIx(
989
- bestEffort?: boolean
970
+ public async getFillOrderIx(
971
+ userAccountPublicKey: PublicKey,
972
+ userAccount: UserAccount,
973
+ order: Order,
974
+ makerInfo?: MakerInfo
990
975
  ): Promise<TransactionInstruction> {
991
- const userAccountPublicKey = await this.getUserAccountPublicKey();
976
+ const fillerPublicKey = await this.getUserAccountPublicKey();
992
977
 
993
- const remainingAccounts = this.getRemainingAccounts({});
978
+ const marketIndex = order.marketIndex;
979
+ const marketAccount = this.getMarketAccount(marketIndex);
980
+
981
+ const oracleAccountMap = new Map<string, AccountMeta>();
982
+ const bankAccountMap = new Map<number, AccountMeta>();
983
+ const marketAccountMap = new Map<number, AccountMeta>();
984
+
985
+ marketAccountMap.set(marketIndex.toNumber(), {
986
+ pubkey: marketAccount.pubkey,
987
+ isWritable: true,
988
+ isSigner: false,
989
+ });
990
+ oracleAccountMap.set(marketAccount.amm.oracle.toString(), {
991
+ pubkey: marketAccount.amm.oracle,
992
+ isWritable: false,
993
+ isSigner: false,
994
+ });
995
+
996
+ for (const bankBalance of userAccount.bankBalances) {
997
+ if (!bankBalance.balance.eq(ZERO)) {
998
+ const bankAccount = this.getBankAccount(bankBalance.bankIndex);
999
+ bankAccountMap.set(bankBalance.bankIndex.toNumber(), {
1000
+ pubkey: bankAccount.pubkey,
1001
+ isSigner: false,
1002
+ isWritable: true,
1003
+ });
1004
+
1005
+ if (!bankAccount.oracle.equals(PublicKey.default)) {
1006
+ oracleAccountMap.set(bankAccount.oracle.toString(), {
1007
+ pubkey: bankAccount.oracle,
1008
+ isSigner: false,
1009
+ isWritable: false,
1010
+ });
1011
+ }
1012
+ }
1013
+ }
1014
+
1015
+ for (const position of userAccount.positions) {
1016
+ if (
1017
+ !positionIsAvailable(position) &&
1018
+ !position.marketIndex.eq(order.marketIndex)
1019
+ ) {
1020
+ const market = this.getMarketAccount(position.marketIndex);
1021
+ marketAccountMap.set(position.marketIndex.toNumber(), {
1022
+ pubkey: market.pubkey,
1023
+ isWritable: true,
1024
+ isSigner: false,
1025
+ });
1026
+ oracleAccountMap.set(market.amm.oracle.toString(), {
1027
+ pubkey: market.amm.oracle,
1028
+ isWritable: false,
1029
+ isSigner: false,
1030
+ });
1031
+ }
1032
+ }
1033
+
1034
+ const remainingAccounts = [
1035
+ ...oracleAccountMap.values(),
1036
+ ...bankAccountMap.values(),
1037
+ ...marketAccountMap.values(),
1038
+ ];
994
1039
 
995
- for (const order of this.getUserAccount().orders) {
996
- const oracle = this.getMarketAccount(order.marketIndex).amm.oracle;
1040
+ if (makerInfo) {
997
1041
  remainingAccounts.push({
998
- pubkey: oracle,
999
- isWritable: false,
1042
+ pubkey: makerInfo.maker,
1043
+ isWritable: true,
1000
1044
  isSigner: false,
1001
1045
  });
1002
1046
  }
1003
1047
 
1004
- return await this.program.instruction.cancelAllOrders(bestEffort, {
1048
+ const orderId = order.orderId;
1049
+ const makerOrderId = makerInfo ? makerInfo.order.orderId : null;
1050
+
1051
+ return await this.program.instruction.fillOrder(orderId, makerOrderId, {
1005
1052
  accounts: {
1006
1053
  state: await this.getStatePublicKey(),
1054
+ filler: fillerPublicKey,
1007
1055
  user: userAccountPublicKey,
1008
1056
  authority: this.wallet.publicKey,
1009
1057
  },
@@ -1011,153 +1059,102 @@ export class ClearingHouse {
1011
1059
  });
1012
1060
  }
1013
1061
 
1014
- public async cancelOrdersByMarketAndSide(
1015
- bestEffort?: boolean,
1016
- marketIndexOnly?: BN,
1017
- directionOnly?: PositionDirection
1018
- ): Promise<TransactionSignature> {
1019
- const { txSig } = await this.txSender.send(
1020
- wrapInTx(
1021
- await this.getCancelOrdersByMarketAndSideIx(
1022
- bestEffort,
1023
- marketIndexOnly,
1024
- directionOnly
1025
- )
1026
- ),
1027
- [],
1028
- this.opts
1029
- );
1030
- return txSig;
1031
- }
1032
-
1033
- public async getCancelOrdersByMarketAndSideIx(
1034
- bestEffort?: boolean,
1035
- marketIndexOnly?: BN,
1036
- directionOnly?: PositionDirection
1037
- ): Promise<TransactionInstruction> {
1038
- const userAccountPublicKey = await this.getUserAccountPublicKey();
1039
-
1040
- const remainingAccounts = this.getRemainingAccounts({});
1041
-
1042
- for (const order of this.getUserAccount().orders) {
1043
- const oracle = this.getMarketAccount(order.marketIndex).amm.oracle;
1044
- remainingAccounts.push({
1045
- pubkey: oracle,
1046
- isWritable: false,
1047
- isSigner: false,
1048
- });
1049
- }
1050
-
1051
- return await this.program.instruction.cancelOrdersByMarketAndSide(
1052
- bestEffort,
1053
- marketIndexOnly,
1054
- directionOnly,
1055
- {
1056
- accounts: {
1057
- state: await this.getStatePublicKey(),
1058
- user: userAccountPublicKey,
1059
- authority: this.wallet.publicKey,
1060
- },
1061
- remainingAccounts,
1062
- }
1063
- );
1064
- }
1065
-
1066
- public async fillOrder(
1062
+ public async triggerOrder(
1067
1063
  userAccountPublicKey: PublicKey,
1068
1064
  user: UserAccount,
1069
- order: Order,
1070
- makerInfo?: MakerInfo
1065
+ order: Order
1071
1066
  ): Promise<TransactionSignature> {
1072
1067
  const { txSig } = await this.txSender.send(
1073
- wrapInTx(
1074
- await this.getFillOrderIx(userAccountPublicKey, user, order, makerInfo)
1075
- ),
1068
+ wrapInTx(await this.getTriggerOrderIx(userAccountPublicKey, user, order)),
1076
1069
  [],
1077
1070
  this.opts
1078
1071
  );
1079
1072
  return txSig;
1080
1073
  }
1081
1074
 
1082
- public async getFillOrderIx(
1075
+ public async getTriggerOrderIx(
1083
1076
  userAccountPublicKey: PublicKey,
1084
1077
  userAccount: UserAccount,
1085
- order: Order,
1086
- makerInfo?: MakerInfo
1078
+ order: Order
1087
1079
  ): Promise<TransactionInstruction> {
1088
1080
  const fillerPublicKey = await this.getUserAccountPublicKey();
1089
1081
 
1090
1082
  const marketIndex = order.marketIndex;
1091
1083
  const marketAccount = this.getMarketAccount(marketIndex);
1092
- const oracle = marketAccount.amm.oracle;
1093
1084
 
1094
- const bankAccountInfos = [
1095
- {
1096
- pubkey: this.getQuoteAssetBankAccount().pubkey,
1097
- isSigner: false,
1098
- isWritable: true,
1099
- },
1100
- ];
1101
- const marketAccountInfos = [
1102
- {
1103
- pubkey: marketAccount.pubkey,
1104
- isWritable: true,
1105
- isSigner: false,
1106
- },
1107
- ];
1108
- const oracleAccountInfos = [
1109
- {
1110
- pubkey: marketAccount.amm.oracle,
1111
- isWritable: false,
1112
- isSigner: false,
1113
- },
1114
- ];
1085
+ const oracleAccountMap = new Map<string, AccountMeta>();
1086
+ const bankAccountMap = new Map<number, AccountMeta>();
1087
+ const marketAccountMap = new Map<number, AccountMeta>();
1088
+
1089
+ marketAccountMap.set(marketIndex.toNumber(), {
1090
+ pubkey: marketAccount.pubkey,
1091
+ isWritable: true,
1092
+ isSigner: false,
1093
+ });
1094
+ oracleAccountMap.set(marketAccount.amm.oracle.toString(), {
1095
+ pubkey: marketAccount.amm.oracle,
1096
+ isWritable: false,
1097
+ isSigner: false,
1098
+ });
1099
+
1100
+ for (const bankBalance of userAccount.bankBalances) {
1101
+ if (!bankBalance.balance.eq(ZERO)) {
1102
+ const bankAccount = this.getBankAccount(bankBalance.bankIndex);
1103
+ bankAccountMap.set(bankBalance.bankIndex.toNumber(), {
1104
+ pubkey: bankAccount.pubkey,
1105
+ isSigner: false,
1106
+ isWritable: true,
1107
+ });
1108
+
1109
+ if (!bankAccount.oracle.equals(PublicKey.default)) {
1110
+ oracleAccountMap.set(bankAccount.oracle.toString(), {
1111
+ pubkey: bankAccount.oracle,
1112
+ isSigner: false,
1113
+ isWritable: false,
1114
+ });
1115
+ }
1116
+ }
1117
+ }
1118
+
1115
1119
  for (const position of userAccount.positions) {
1116
1120
  if (
1117
1121
  !positionIsAvailable(position) &&
1118
1122
  !position.marketIndex.eq(order.marketIndex)
1119
1123
  ) {
1120
1124
  const market = this.getMarketAccount(position.marketIndex);
1121
- marketAccountInfos.push({
1125
+ marketAccountMap.set(position.marketIndex.toNumber(), {
1122
1126
  pubkey: market.pubkey,
1123
- isWritable: false,
1127
+ isWritable: true,
1124
1128
  isSigner: false,
1125
1129
  });
1126
- oracleAccountInfos.push({
1130
+ oracleAccountMap.set(market.amm.oracle.toString(), {
1127
1131
  pubkey: market.amm.oracle,
1128
1132
  isWritable: false,
1129
1133
  isSigner: false,
1130
1134
  });
1131
1135
  }
1132
1136
  }
1133
- const remainingAccounts = oracleAccountInfos.concat(
1134
- bankAccountInfos.concat(marketAccountInfos)
1135
- );
1136
1137
 
1137
- if (makerInfo) {
1138
- remainingAccounts.push({
1139
- pubkey: makerInfo.maker,
1140
- isWritable: true,
1141
- isSigner: false,
1142
- });
1143
- }
1138
+ const remainingAccounts = [
1139
+ ...oracleAccountMap.values(),
1140
+ ...bankAccountMap.values(),
1141
+ ...marketAccountMap.values(),
1142
+ ];
1144
1143
 
1145
1144
  const orderId = order.orderId;
1146
- const makerOrderId = makerInfo ? makerInfo.order.orderId : null;
1147
- return await this.program.instruction.fillOrder(orderId, makerOrderId, {
1145
+ return await this.program.instruction.triggerOrder(orderId, {
1148
1146
  accounts: {
1149
1147
  state: await this.getStatePublicKey(),
1150
1148
  filler: fillerPublicKey,
1151
1149
  user: userAccountPublicKey,
1152
1150
  authority: this.wallet.publicKey,
1153
- oracle: oracle,
1154
1151
  },
1155
1152
  remainingAccounts,
1156
1153
  });
1157
1154
  }
1158
1155
 
1159
1156
  public async placeAndTake(
1160
- orderParams: OrderParams,
1157
+ orderParams: OptionalOrderParams,
1161
1158
  makerInfo?: MakerInfo
1162
1159
  ): Promise<TransactionSignature> {
1163
1160
  const { txSig, slot } = await this.txSender.send(
@@ -1170,14 +1167,12 @@ export class ClearingHouse {
1170
1167
  }
1171
1168
 
1172
1169
  public async getPlaceAndTakeIx(
1173
- orderParams: OrderParams,
1170
+ orderParams: OptionalOrderParams,
1174
1171
  makerInfo?: MakerInfo
1175
1172
  ): Promise<TransactionInstruction> {
1173
+ orderParams = this.getOrderParams(orderParams);
1176
1174
  const userAccountPublicKey = await this.getUserAccountPublicKey();
1177
1175
 
1178
- const priceOracle = this.getMarketAccount(orderParams.marketIndex).amm
1179
- .oracle;
1180
-
1181
1176
  const remainingAccounts = this.getRemainingAccounts({
1182
1177
  writableMarketIndex: orderParams.marketIndex,
1183
1178
  writableBankIndex: QUOTE_ASSET_BANK_INDEX,
@@ -1201,7 +1196,55 @@ export class ClearingHouse {
1201
1196
  state: await this.getStatePublicKey(),
1202
1197
  user: userAccountPublicKey,
1203
1198
  authority: this.wallet.publicKey,
1204
- oracle: priceOracle,
1199
+ },
1200
+ remainingAccounts,
1201
+ }
1202
+ );
1203
+ }
1204
+
1205
+ public async placeAndMake(
1206
+ orderParams: OptionalOrderParams,
1207
+ takerInfo: TakerInfo
1208
+ ): Promise<TransactionSignature> {
1209
+ const { txSig, slot } = await this.txSender.send(
1210
+ wrapInTx(await this.getPlaceAndMakeIx(orderParams, takerInfo)),
1211
+ [],
1212
+ this.opts
1213
+ );
1214
+
1215
+ this.marketLastSlotCache.set(orderParams.marketIndex.toNumber(), slot);
1216
+
1217
+ return txSig;
1218
+ }
1219
+
1220
+ public async getPlaceAndMakeIx(
1221
+ orderParams: OptionalOrderParams,
1222
+ takerInfo: TakerInfo
1223
+ ): Promise<TransactionInstruction> {
1224
+ orderParams = this.getOrderParams(orderParams);
1225
+ const userAccountPublicKey = await this.getUserAccountPublicKey();
1226
+
1227
+ const remainingAccounts = this.getRemainingAccounts({
1228
+ writableMarketIndex: orderParams.marketIndex,
1229
+ writableBankIndex: QUOTE_ASSET_BANK_INDEX,
1230
+ });
1231
+
1232
+ const takerOrderId = takerInfo!.order!.orderId;
1233
+ remainingAccounts.push({
1234
+ pubkey: takerInfo.taker,
1235
+ isSigner: false,
1236
+ isWritable: true,
1237
+ });
1238
+
1239
+ return await this.program.instruction.placeAndMake(
1240
+ orderParams,
1241
+ takerOrderId,
1242
+ {
1243
+ accounts: {
1244
+ state: await this.getStatePublicKey(),
1245
+ user: userAccountPublicKey,
1246
+ taker: takerInfo.taker,
1247
+ authority: this.wallet.publicKey,
1205
1248
  },
1206
1249
  remainingAccounts,
1207
1250
  }
@@ -1219,16 +1262,13 @@ export class ClearingHouse {
1219
1262
  throw Error(`No position in market ${marketIndex.toString()}`);
1220
1263
  }
1221
1264
 
1222
- return await this.placeAndTake(
1223
- getMarketOrderParams(
1224
- marketIndex,
1225
- findDirectionToClose(userPosition),
1226
- ZERO,
1227
- userPosition.baseAssetAmount,
1228
- true,
1229
- undefined
1230
- )
1231
- );
1265
+ return await this.placeAndTake({
1266
+ orderType: OrderType.MARKET,
1267
+ marketIndex,
1268
+ direction: findDirectionToClose(userPosition),
1269
+ baseAssetAmount: userPosition.baseAssetAmount,
1270
+ reduceOnly: true,
1271
+ });
1232
1272
  }
1233
1273
 
1234
1274
  public async settlePNLs(
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: 'GCuH76fb1rXc7bjFXNcnNvWSekLgzpsnMbc1Ng4FsGSs',
31
+ CLEARING_HOUSE_PROGRAM_ID: 'BMow898PH56jD8z4EaqxicoGXkR1HhN17qrER6Uc4AYq',
32
32
  USDC_MINT_ADDRESS: '8zGuJQqwhZafTah7Uc7Z4tXRnguqkn5KLFAP8oV6PHe2',
33
33
  MARKETS: DevnetMarkets,
34
34
  BANKS: DevnetBanks,
@@ -19,8 +19,15 @@ export const DevnetBanks: BankConfig[] = [
19
19
  mint: new PublicKey('8zGuJQqwhZafTah7Uc7Z4tXRnguqkn5KLFAP8oV6PHe2'),
20
20
  },
21
21
  {
22
- symbol: 'BTC',
22
+ symbol: 'SOL',
23
23
  bankIndex: new BN(1),
24
+ oracle: new PublicKey('J83w4HKfqxwcq3BEMMkPFSppX3gqekLyLJBexebFVkix'),
25
+ oracleSource: OracleSource.PYTH,
26
+ mint: new PublicKey('So11111111111111111111111111111111111111112'),
27
+ },
28
+ {
29
+ symbol: 'BTC',
30
+ bankIndex: new BN(2),
24
31
  oracle: new PublicKey('HovQMDrbAgAYPCmHVSrezcSmkMtXSSUsLDFANExrZh2J'),
25
32
  oracleSource: OracleSource.PYTH,
26
33
  mint: new PublicKey('Gh9ZwEmdLJ8DscKNTkTqPbNwLNNBjuSzaG9Vp2KGtKJr'),
@@ -43,6 +43,7 @@ export const BASE_PRECISION_EXP = AMM_RESERVE_PRECISION_EXP;
43
43
 
44
44
  export const AMM_TO_QUOTE_PRECISION_RATIO =
45
45
  AMM_RESERVE_PRECISION.div(QUOTE_PRECISION); // 10^7
46
+ export const PRICE_DIV_PEG = MARK_PRICE_PRECISION.div(PEG_PRECISION); //10^7
46
47
  export const PRICE_TO_QUOTE_PRECISION =
47
48
  MARK_PRICE_PRECISION.div(QUOTE_PRECISION);
48
49
  export const AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO =
@@ -0,0 +1,77 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EventList = void 0;
4
+ class Node {
5
+ constructor(event, next, prev) {
6
+ this.event = event;
7
+ this.next = next;
8
+ this.prev = prev;
9
+ }
10
+ }
11
+ class EventList {
12
+ constructor(eventType, maxSize, sortFn, orderDirection) {
13
+ this.eventType = eventType;
14
+ this.maxSize = maxSize;
15
+ this.sortFn = sortFn;
16
+ this.orderDirection = orderDirection;
17
+ this.size = 0;
18
+ }
19
+ insert(event) {
20
+ this.size++;
21
+ const newNode = new Node(event);
22
+ if (this.head === undefined) {
23
+ this.head = this.tail = newNode;
24
+ return;
25
+ }
26
+ if (this.sortFn(this.head.event, newNode.event) ===
27
+ (this.orderDirection === 'asc' ? 'less than' : 'greater than')) {
28
+ this.head.prev = newNode;
29
+ newNode.next = this.head;
30
+ this.head = newNode;
31
+ }
32
+ else {
33
+ let currentNode = this.head;
34
+ while (currentNode.next !== undefined &&
35
+ this.sortFn(currentNode.next.event, newNode.event) !==
36
+ (this.orderDirection === 'asc' ? 'less than' : 'greater than')) {
37
+ currentNode = currentNode.next;
38
+ }
39
+ newNode.next = currentNode.next;
40
+ if (currentNode.next !== undefined) {
41
+ newNode.next.prev = newNode;
42
+ }
43
+ currentNode.next = newNode;
44
+ newNode.prev = currentNode;
45
+ }
46
+ if (this.size > this.maxSize) {
47
+ this.detach();
48
+ }
49
+ }
50
+ detach() {
51
+ const node = this.tail;
52
+ if (node.prev !== undefined) {
53
+ node.prev.next = node.next;
54
+ }
55
+ else {
56
+ this.head = node.next;
57
+ }
58
+ if (node.next !== undefined) {
59
+ node.next.prev = node.prev;
60
+ }
61
+ else {
62
+ this.tail = node.prev;
63
+ }
64
+ this.size--;
65
+ }
66
+ toArray() {
67
+ return Array.from(this);
68
+ }
69
+ *[Symbol.iterator]() {
70
+ let node = this.head;
71
+ while (node) {
72
+ yield node.event;
73
+ node = node.next;
74
+ }
75
+ }
76
+ }
77
+ exports.EventList = EventList;