@drift-labs/sdk 0.1.23-master.4 → 0.1.26

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 (61) hide show
  1. package/lib/accounts/bulkAccountLoader.d.ts +2 -0
  2. package/lib/accounts/bulkAccountLoader.js +24 -0
  3. package/lib/addresses.js +5 -1
  4. package/lib/admin.d.ts +2 -2
  5. package/lib/admin.js +11 -5
  6. package/lib/clearingHouse.js +7 -1
  7. package/lib/clearingHouseUser.d.ts +12 -17
  8. package/lib/clearingHouseUser.js +114 -217
  9. package/lib/constants/markets.d.ts +6 -4
  10. package/lib/constants/markets.js +48 -0
  11. package/lib/constants/numericConstants.d.ts +2 -2
  12. package/lib/constants/numericConstants.js +3 -3
  13. package/lib/factory/oracleClient.d.ts +5 -0
  14. package/lib/factory/oracleClient.js +16 -0
  15. package/lib/idl/clearing_house.json +58 -5
  16. package/lib/idl/switchboard_v2.json +4663 -0
  17. package/lib/index.d.ts +4 -1
  18. package/lib/index.js +9 -2
  19. package/lib/math/funding.d.ts +6 -6
  20. package/lib/math/funding.js +4 -16
  21. package/lib/math/orders.d.ts +1 -0
  22. package/lib/math/orders.js +19 -1
  23. package/lib/mockUSDCFaucet.js +5 -1
  24. package/lib/oracles/pythClient.d.ts +14 -0
  25. package/lib/oracles/pythClient.js +53 -0
  26. package/lib/oracles/switchboardClient.d.ts +13 -0
  27. package/lib/oracles/switchboardClient.js +76 -0
  28. package/lib/oracles/types.d.ts +15 -0
  29. package/lib/oracles/types.js +2 -0
  30. package/lib/orderParams.d.ts +1 -1
  31. package/lib/orderParams.js +2 -2
  32. package/lib/orders.js +1 -1
  33. package/lib/types.d.ts +5 -1
  34. package/package.json +2 -1
  35. package/src/accounts/bulkAccountLoader.ts +32 -0
  36. package/src/accounts/types.js +10 -0
  37. package/src/accounts/utils.js +7 -0
  38. package/src/accounts/webSocketAccountSubscriber.js +76 -0
  39. package/src/addresses.js +83 -0
  40. package/src/admin.ts +15 -4
  41. package/src/clearingHouse.ts +2 -0
  42. package/src/clearingHouseUser.ts +161 -330
  43. package/src/constants/markets.ts +54 -3
  44. package/src/constants/numericConstants.ts +2 -2
  45. package/src/factory/oracleClient.ts +22 -0
  46. package/src/idl/clearing_house.json +58 -5
  47. package/src/idl/switchboard_v2.json +4663 -0
  48. package/src/index.ts +4 -1
  49. package/src/math/funding.ts +9 -25
  50. package/src/math/orders.ts +28 -0
  51. package/src/mockUSDCFaucet.js +171 -0
  52. package/src/oracles/pythClient.ts +49 -0
  53. package/src/oracles/switchboardClient.ts +87 -0
  54. package/src/oracles/types.ts +15 -0
  55. package/src/orderParams.ts +3 -2
  56. package/src/orders.ts +1 -1
  57. package/src/types.js +60 -0
  58. package/src/types.ts +6 -1
  59. package/lib/pythClient.d.ts +0 -7
  60. package/lib/pythClient.js +0 -25
  61. package/src/pythClient.ts +0 -15
@@ -0,0 +1,83 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __importStar = (this && this.__importStar) || function (mod) {
15
+ if (mod && mod.__esModule) return mod;
16
+ var result = {};
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
19
+ return result;
20
+ };
21
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
22
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
23
+ return new (P || (P = Promise))(function (resolve, reject) {
24
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
25
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
26
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
27
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
28
+ });
29
+ };
30
+ Object.defineProperty(exports, "__esModule", { value: true });
31
+ exports.getUserOrdersAccountPublicKey = exports.getUserOrdersAccountPublicKeyAndNonce = exports.getUserAccountPublicKey = exports.getUserAccountPublicKeyAndNonce = exports.getClearingHouseStateAccountPublicKey = exports.getOrderStateAccountPublicKeyAndNonce = exports.getOrderStateAccountPublicKey = exports.getClearingHouseStateAccountPublicKeyAndNonce = void 0;
32
+ const anchor = __importStar(require("@project-serum/anchor"));
33
+ function getClearingHouseStateAccountPublicKeyAndNonce(programId) {
34
+ return __awaiter(this, void 0, void 0, function* () {
35
+ return anchor.web3.PublicKey.findProgramAddress([Buffer.from(anchor.utils.bytes.utf8.encode('clearing_house'))], programId);
36
+ });
37
+ }
38
+ exports.getClearingHouseStateAccountPublicKeyAndNonce = getClearingHouseStateAccountPublicKeyAndNonce;
39
+ function getOrderStateAccountPublicKey(programId) {
40
+ return __awaiter(this, void 0, void 0, function* () {
41
+ return (yield getOrderStateAccountPublicKeyAndNonce(programId))[0];
42
+ });
43
+ }
44
+ exports.getOrderStateAccountPublicKey = getOrderStateAccountPublicKey;
45
+ function getOrderStateAccountPublicKeyAndNonce(programId) {
46
+ return __awaiter(this, void 0, void 0, function* () {
47
+ return anchor.web3.PublicKey.findProgramAddress([Buffer.from(anchor.utils.bytes.utf8.encode('order_state'))], programId);
48
+ });
49
+ }
50
+ exports.getOrderStateAccountPublicKeyAndNonce = getOrderStateAccountPublicKeyAndNonce;
51
+ function getClearingHouseStateAccountPublicKey(programId) {
52
+ return __awaiter(this, void 0, void 0, function* () {
53
+ return (yield getClearingHouseStateAccountPublicKeyAndNonce(programId))[0];
54
+ });
55
+ }
56
+ exports.getClearingHouseStateAccountPublicKey = getClearingHouseStateAccountPublicKey;
57
+ function getUserAccountPublicKeyAndNonce(programId, authority) {
58
+ return __awaiter(this, void 0, void 0, function* () {
59
+ return anchor.web3.PublicKey.findProgramAddress([Buffer.from(anchor.utils.bytes.utf8.encode('user')), authority.toBuffer()], programId);
60
+ });
61
+ }
62
+ exports.getUserAccountPublicKeyAndNonce = getUserAccountPublicKeyAndNonce;
63
+ function getUserAccountPublicKey(programId, authority) {
64
+ return __awaiter(this, void 0, void 0, function* () {
65
+ return (yield getUserAccountPublicKeyAndNonce(programId, authority))[0];
66
+ });
67
+ }
68
+ exports.getUserAccountPublicKey = getUserAccountPublicKey;
69
+ function getUserOrdersAccountPublicKeyAndNonce(programId, userAccount) {
70
+ return __awaiter(this, void 0, void 0, function* () {
71
+ return anchor.web3.PublicKey.findProgramAddress([
72
+ Buffer.from(anchor.utils.bytes.utf8.encode('user_orders')),
73
+ userAccount.toBuffer(),
74
+ ], programId);
75
+ });
76
+ }
77
+ exports.getUserOrdersAccountPublicKeyAndNonce = getUserOrdersAccountPublicKeyAndNonce;
78
+ function getUserOrdersAccountPublicKey(programId, userAccount) {
79
+ return __awaiter(this, void 0, void 0, function* () {
80
+ return (yield getUserOrdersAccountPublicKeyAndNonce(programId, userAccount))[0];
81
+ });
82
+ }
83
+ exports.getUserOrdersAccountPublicKey = getUserOrdersAccountPublicKey;
package/src/admin.ts CHANGED
@@ -216,7 +216,11 @@ export class Admin extends ClearingHouse {
216
216
  baseAssetReserve: BN,
217
217
  quoteAssetReserve: BN,
218
218
  periodicity: BN,
219
- pegMultiplier: BN = PEG_PRECISION
219
+ pegMultiplier: BN = PEG_PRECISION,
220
+ oracleSource: OracleSource = OracleSource.PYTH,
221
+ marginRatioInitial = 2000,
222
+ marginRatioPartial = 625,
223
+ marginRatioMaintenance = 500
220
224
  ): Promise<TransactionSignature> {
221
225
  if (this.getMarketsAccount().markets[marketIndex.toNumber()].initialized) {
222
226
  throw Error(`MarketIndex ${marketIndex.toNumber()} already initialized`);
@@ -228,6 +232,10 @@ export class Admin extends ClearingHouse {
228
232
  quoteAssetReserve,
229
233
  periodicity,
230
234
  pegMultiplier,
235
+ oracleSource,
236
+ marginRatioInitial,
237
+ marginRatioPartial,
238
+ marginRatioMaintenance,
231
239
  {
232
240
  accounts: {
233
241
  state: await this.getStatePublicKey(),
@@ -460,11 +468,13 @@ export class Admin extends ClearingHouse {
460
468
  }
461
469
 
462
470
  public async updateMarginRatio(
463
- marginRatioInitial: BN,
464
- marginRatioPartial: BN,
465
- marginRatioMaintenance: BN
471
+ marketIndex: BN,
472
+ marginRatioInitial: number,
473
+ marginRatioPartial: number,
474
+ marginRatioMaintenance: number
466
475
  ): Promise<TransactionSignature> {
467
476
  return await this.program.rpc.updateMarginRatio(
477
+ marketIndex,
468
478
  marginRatioInitial,
469
479
  marginRatioPartial,
470
480
  marginRatioMaintenance,
@@ -472,6 +482,7 @@ export class Admin extends ClearingHouse {
472
482
  accounts: {
473
483
  admin: this.wallet.publicKey,
474
484
  state: await this.getStatePublicKey(),
485
+ markets: this.getStateAccount().markets,
475
486
  },
476
487
  }
477
488
  );
@@ -258,6 +258,8 @@ export class ClearingHouse {
258
258
  this.program = newProgram;
259
259
  this.userAccountPublicKey = undefined;
260
260
  this.userAccount = undefined;
261
+ this.userOrdersAccountPublicKey = undefined;
262
+ this.userOrdersExist = undefined;
261
263
  }
262
264
 
263
265
  public async initializeUserAccount(): Promise<