@drift-labs/sdk 0.1.26 → 0.1.29-master.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.
Files changed (121) hide show
  1. package/lib/accounts/bulkAccountLoader.js +8 -3
  2. package/lib/accounts/pollingOracleSubscriber.d.ts +26 -0
  3. package/lib/accounts/pollingOracleSubscriber.js +79 -0
  4. package/lib/accounts/types.d.ts +14 -1
  5. package/lib/clearingHouse.d.ts +6 -4
  6. package/lib/clearingHouse.js +56 -7
  7. package/lib/constants/markets.d.ts +2 -2
  8. package/lib/constants/markets.js +50 -28
  9. package/lib/idl/clearing_house.json +60 -1
  10. package/lib/index.d.ts +1 -0
  11. package/lib/index.js +1 -0
  12. package/lib/math/market.d.ts +2 -0
  13. package/lib/math/market.js +6 -1
  14. package/lib/orderParams.d.ts +1 -1
  15. package/lib/orderParams.js +2 -2
  16. package/lib/orders.d.ts +3 -2
  17. package/lib/orders.js +11 -4
  18. package/lib/types.d.ts +3 -0
  19. package/lib/types.js +1 -0
  20. package/package.json +1 -1
  21. package/src/accounts/bulkAccountLoader.ts +10 -4
  22. package/src/accounts/bulkUserSubscription.js +56 -0
  23. package/src/accounts/bulkUserSubscription.js.map +1 -0
  24. package/src/accounts/pollingClearingHouseAccountSubscriber.js +210 -0
  25. package/src/accounts/pollingClearingHouseAccountSubscriber.js.map +1 -0
  26. package/src/accounts/pollingOracleSubscriber.js +65 -0
  27. package/src/accounts/pollingOracleSubscriber.js.map +1 -0
  28. package/src/accounts/pollingOracleSubscriber.ts +103 -0
  29. package/src/accounts/pollingTokenAccountSubscriber.js +65 -0
  30. package/src/accounts/pollingTokenAccountSubscriber.js.map +1 -0
  31. package/src/accounts/pollingUserAccountSubscriber.js +139 -0
  32. package/src/accounts/pollingUserAccountSubscriber.js.map +1 -0
  33. package/src/accounts/types.js +1 -0
  34. package/src/accounts/types.js.map +1 -0
  35. package/src/accounts/types.ts +22 -1
  36. package/src/accounts/utils.js +1 -0
  37. package/src/accounts/utils.js.map +1 -0
  38. package/src/accounts/webSocketAccountSubscriber.js +15 -27
  39. package/src/accounts/webSocketAccountSubscriber.js.map +1 -0
  40. package/src/accounts/webSocketClearingHouseAccountSubscriber.js +212 -0
  41. package/src/accounts/webSocketClearingHouseAccountSubscriber.js.map +1 -0
  42. package/src/accounts/webSocketUserAccountSubscriber.js +78 -0
  43. package/src/accounts/webSocketUserAccountSubscriber.js.map +1 -0
  44. package/src/addresses.js +20 -44
  45. package/src/addresses.js.map +1 -0
  46. package/src/admin.js +443 -0
  47. package/src/admin.js.map +1 -0
  48. package/src/assert/assert.js +10 -0
  49. package/src/assert/assert.js.map +1 -0
  50. package/src/clearingHouse.ts +85 -7
  51. package/src/clearingHouseUser.js +581 -0
  52. package/src/clearingHouseUser.js.map +1 -0
  53. package/src/config.js +37 -0
  54. package/src/config.js.map +1 -0
  55. package/src/constants/markets.js +167 -0
  56. package/src/constants/markets.js.map +1 -0
  57. package/src/constants/markets.ts +52 -30
  58. package/src/constants/numericConstants.js +22 -0
  59. package/src/constants/numericConstants.js.map +1 -0
  60. package/src/factory/clearingHouse.js +65 -0
  61. package/src/factory/clearingHouse.js.map +1 -0
  62. package/src/factory/clearingHouseUser.js +35 -0
  63. package/src/factory/clearingHouseUser.js.map +1 -0
  64. package/src/factory/oracleClient.js +17 -0
  65. package/src/factory/oracleClient.js.map +1 -0
  66. package/src/idl/clearing_house.json +60 -1
  67. package/src/index.js +56 -0
  68. package/src/index.js.map +1 -0
  69. package/src/index.ts +1 -0
  70. package/src/math/amm.js +285 -0
  71. package/src/math/amm.js.map +1 -0
  72. package/src/math/conversion.js +16 -0
  73. package/src/math/conversion.js.map +1 -0
  74. package/src/math/funding.js +223 -0
  75. package/src/math/funding.js.map +1 -0
  76. package/src/math/insuranceFund.js +23 -0
  77. package/src/math/insuranceFund.js.map +1 -0
  78. package/src/math/market.js +30 -0
  79. package/src/math/market.js.map +1 -0
  80. package/src/math/market.ts +9 -0
  81. package/src/math/orders.js +73 -0
  82. package/src/math/orders.js.map +1 -0
  83. package/src/math/position.js +121 -0
  84. package/src/math/position.js.map +1 -0
  85. package/src/math/trade.js +182 -0
  86. package/src/math/trade.js.map +1 -0
  87. package/src/math/utils.js +27 -0
  88. package/src/math/utils.js.map +1 -0
  89. package/src/mockUSDCFaucet.js +88 -116
  90. package/src/mockUSDCFaucet.js.map +1 -0
  91. package/src/oracles/pythClient.js +39 -0
  92. package/src/oracles/pythClient.js.map +1 -0
  93. package/src/oracles/switchboardClient.js +60 -0
  94. package/src/oracles/switchboardClient.js.map +1 -0
  95. package/src/oracles/types.js +3 -0
  96. package/src/oracles/types.js.map +1 -0
  97. package/src/orderParams.js +109 -0
  98. package/src/orderParams.js.map +1 -0
  99. package/src/orderParams.ts +3 -2
  100. package/src/orders.js +172 -0
  101. package/src/orders.js.map +1 -0
  102. package/src/orders.ts +17 -4
  103. package/src/token/index.js +39 -0
  104. package/src/token/index.js.map +1 -0
  105. package/src/tx/defaultTxSender.js +13 -0
  106. package/src/tx/defaultTxSender.js.map +1 -0
  107. package/src/tx/retryTxSender.js +137 -0
  108. package/src/tx/retryTxSender.js.map +1 -0
  109. package/src/tx/types.js +3 -0
  110. package/src/tx/types.js.map +1 -0
  111. package/src/tx/utils.js +9 -0
  112. package/src/tx/utils.js.map +1 -0
  113. package/src/types.js +1 -0
  114. package/src/types.js.map +1 -0
  115. package/src/types.ts +1 -0
  116. package/src/util/computeUnits.js +17 -0
  117. package/src/util/computeUnits.js.map +1 -0
  118. package/src/util/tps.js +17 -0
  119. package/src/util/tps.js.map +1 -0
  120. package/src/wallet.js +23 -0
  121. package/src/wallet.js.map +1 -0
@@ -57,6 +57,7 @@ import {
57
57
  getClearingHouse,
58
58
  getWebSocketClearingHouseConfig,
59
59
  } from './factory/clearingHouse';
60
+ import { ZERO } from './constants/numericConstants';
60
61
 
61
62
  /**
62
63
  * # ClearingHouse
@@ -760,6 +761,14 @@ export class ClearingHouse {
760
761
  });
761
762
  }
762
763
 
764
+ if (!orderParams.oraclePriceOffset.eq(ZERO)) {
765
+ remainingAccounts.push({
766
+ pubkey: priceOracle,
767
+ isWritable: false,
768
+ isSigner: false,
769
+ });
770
+ }
771
+
763
772
  const state = this.getStateAccount();
764
773
  const orderState = this.getOrderStateAccount();
765
774
  return await this.program.instruction.placeOrder(orderParams, {
@@ -774,26 +783,81 @@ export class ClearingHouse {
774
783
  fundingRateHistory: state.fundingRateHistory,
775
784
  orderState: await this.getOrderStatePublicKey(),
776
785
  orderHistory: orderState.orderHistory,
777
- oracle: priceOracle,
778
786
  },
779
787
  remainingAccounts,
780
788
  });
781
789
  }
782
790
 
783
- public async cancelOrder(orderId: BN): Promise<TransactionSignature> {
791
+ public async expireOrders(
792
+ userAccountPublicKey: PublicKey,
793
+ userOrdersAccountPublicKey: PublicKey
794
+ ): Promise<TransactionSignature> {
795
+ return await this.txSender.send(
796
+ wrapInTx(
797
+ await this.getExpireOrdersIx(
798
+ userAccountPublicKey,
799
+ userOrdersAccountPublicKey
800
+ )
801
+ ),
802
+ [],
803
+ this.opts
804
+ );
805
+ }
806
+
807
+ public async getExpireOrdersIx(
808
+ userAccountPublicKey: PublicKey,
809
+ userOrdersAccountPublicKey: PublicKey
810
+ ): Promise<TransactionInstruction> {
811
+ const fillerPublicKey = await this.getUserAccountPublicKey();
812
+ const userAccount: any = await this.program.account.user.fetch(
813
+ userAccountPublicKey
814
+ );
815
+
816
+ const orderState = this.getOrderStateAccount();
817
+ return await this.program.instruction.expireOrders({
818
+ accounts: {
819
+ state: await this.getStatePublicKey(),
820
+ filler: fillerPublicKey,
821
+ user: userAccountPublicKey,
822
+ authority: this.wallet.publicKey,
823
+ userPositions: userAccount.positions,
824
+ userOrders: userOrdersAccountPublicKey,
825
+ orderState: await this.getOrderStatePublicKey(),
826
+ orderHistory: orderState.orderHistory,
827
+ },
828
+ });
829
+ }
830
+
831
+ public async cancelOrder(
832
+ orderId: BN,
833
+ oracle?: PublicKey
834
+ ): Promise<TransactionSignature> {
784
835
  return await this.txSender.send(
785
- wrapInTx(await this.getCancelOrderIx(orderId)),
836
+ wrapInTx(await this.getCancelOrderIx(orderId, oracle)),
786
837
  [],
787
838
  this.opts
788
839
  );
789
840
  }
790
841
 
791
- public async getCancelOrderIx(orderId: BN): Promise<TransactionInstruction> {
842
+ public async getCancelOrderIx(
843
+ orderId: BN,
844
+ oracle?: PublicKey
845
+ ): Promise<TransactionInstruction> {
792
846
  const userAccountPublicKey = await this.getUserAccountPublicKey();
793
847
  const userAccount = await this.getUserAccount();
794
848
 
795
849
  const state = this.getStateAccount();
796
850
  const orderState = this.getOrderStateAccount();
851
+
852
+ const remainingAccounts = [];
853
+ if (oracle) {
854
+ remainingAccounts.push({
855
+ pubkey: oracle,
856
+ isWritable: false,
857
+ isSigner: false,
858
+ });
859
+ }
860
+
797
861
  return await this.program.instruction.cancelOrder(orderId, {
798
862
  accounts: {
799
863
  state: await this.getStatePublicKey(),
@@ -807,27 +871,40 @@ export class ClearingHouse {
807
871
  orderState: await this.getOrderStatePublicKey(),
808
872
  orderHistory: orderState.orderHistory,
809
873
  },
874
+ remainingAccounts,
810
875
  });
811
876
  }
812
877
 
813
878
  public async cancelOrderByUserId(
814
- userOrderId: number
879
+ userOrderId: number,
880
+ oracle?: PublicKey
815
881
  ): Promise<TransactionSignature> {
816
882
  return await this.txSender.send(
817
- wrapInTx(await this.getCancelOrderByUserIdIx(userOrderId)),
883
+ wrapInTx(await this.getCancelOrderByUserIdIx(userOrderId, oracle)),
818
884
  [],
819
885
  this.opts
820
886
  );
821
887
  }
822
888
 
823
889
  public async getCancelOrderByUserIdIx(
824
- userOrderId: number
890
+ userOrderId: number,
891
+ oracle?: PublicKey
825
892
  ): Promise<TransactionInstruction> {
826
893
  const userAccountPublicKey = await this.getUserAccountPublicKey();
827
894
  const userAccount = await this.getUserAccount();
828
895
 
829
896
  const state = this.getStateAccount();
830
897
  const orderState = this.getOrderStateAccount();
898
+
899
+ const remainingAccounts = [];
900
+ if (oracle) {
901
+ remainingAccounts.push({
902
+ pubkey: oracle,
903
+ isWritable: false,
904
+ isSigner: false,
905
+ });
906
+ }
907
+
831
908
  return await this.program.instruction.cancelOrderByUserId(userOrderId, {
832
909
  accounts: {
833
910
  state: await this.getStatePublicKey(),
@@ -841,6 +918,7 @@ export class ClearingHouse {
841
918
  orderState: await this.getOrderStatePublicKey(),
842
919
  orderHistory: orderState.orderHistory,
843
920
  },
921
+ remainingAccounts,
844
922
  });
845
923
  }
846
924