@drift-labs/sdk 0.2.0-master.24 → 0.2.0-master.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 (193) hide show
  1. package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +13 -13
  2. package/lib/accounts/pollingClearingHouseAccountSubscriber.js +27 -27
  3. package/lib/accounts/types.d.ts +8 -9
  4. package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +14 -14
  5. package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +35 -34
  6. package/lib/addresses/pda.d.ts +8 -6
  7. package/lib/addresses/pda.js +35 -27
  8. package/lib/admin.d.ts +9 -6
  9. package/lib/admin.js +90 -54
  10. package/lib/clearingHouse.d.ts +71 -42
  11. package/lib/clearingHouse.js +765 -282
  12. package/lib/clearingHouseConfig.d.ts +2 -2
  13. package/lib/clearingHouseUser.d.ts +19 -17
  14. package/lib/clearingHouseUser.js +145 -123
  15. package/lib/config.d.ts +7 -7
  16. package/lib/config.js +21 -21
  17. package/lib/constants/numericConstants.d.ts +12 -12
  18. package/lib/constants/numericConstants.js +13 -13
  19. package/lib/constants/{markets.d.ts → perpMarkets.d.ts} +5 -5
  20. package/{src/constants/markets.js → lib/constants/perpMarkets.js} +4 -4
  21. package/lib/constants/{banks.d.ts → spotMarkets.d.ts} +6 -6
  22. package/lib/constants/{banks.js → spotMarkets.js} +16 -16
  23. package/lib/events/eventSubscriber.d.ts +4 -2
  24. package/lib/events/eventSubscriber.js +16 -9
  25. package/lib/events/fetchLogs.d.ts +10 -1
  26. package/lib/events/fetchLogs.js +27 -7
  27. package/lib/events/pollingLogProvider.d.ts +2 -1
  28. package/lib/events/pollingLogProvider.js +6 -2
  29. package/lib/events/types.d.ts +4 -2
  30. package/lib/events/types.js +2 -0
  31. package/lib/examples/makeTradeExample.js +18 -6
  32. package/lib/idl/clearing_house.json +1128 -347
  33. package/lib/index.d.ts +6 -3
  34. package/lib/index.js +6 -3
  35. package/lib/math/amm.d.ts +5 -3
  36. package/lib/math/amm.js +42 -4
  37. package/lib/math/funding.d.ts +6 -6
  38. package/lib/math/funding.js +2 -1
  39. package/lib/math/margin.d.ts +4 -4
  40. package/lib/math/margin.js +18 -11
  41. package/lib/math/market.d.ts +10 -9
  42. package/lib/math/market.js +29 -6
  43. package/lib/math/oracles.d.ts +2 -1
  44. package/lib/math/oracles.js +11 -1
  45. package/lib/math/orders.d.ts +5 -5
  46. package/lib/math/position.d.ts +13 -13
  47. package/lib/math/position.js +19 -19
  48. package/lib/math/spotBalance.d.ts +19 -0
  49. package/lib/math/spotBalance.js +176 -0
  50. package/lib/math/spotMarket.d.ts +4 -0
  51. package/lib/math/spotMarket.js +8 -0
  52. package/lib/math/spotPosition.d.ts +2 -0
  53. package/lib/math/spotPosition.js +8 -0
  54. package/lib/math/state.js +2 -2
  55. package/lib/math/trade.d.ts +4 -4
  56. package/lib/orderParams.d.ts +4 -4
  57. package/lib/orderParams.js +12 -4
  58. package/lib/serum/serumSubscriber.d.ts +23 -0
  59. package/lib/serum/serumSubscriber.js +41 -0
  60. package/lib/serum/types.d.ts +11 -0
  61. package/lib/serum/types.js +2 -0
  62. package/lib/tx/retryTxSender.d.ts +1 -1
  63. package/lib/tx/retryTxSender.js +4 -2
  64. package/lib/tx/types.d.ts +1 -1
  65. package/lib/types.d.ts +148 -39
  66. package/lib/types.js +37 -9
  67. package/my-script/.env +7 -0
  68. package/my-script/getUserStats.ts +106 -0
  69. package/my-script/multiConnections.ts +119 -0
  70. package/my-script/test-regex.ts +11 -0
  71. package/my-script/utils.ts +52 -0
  72. package/package.json +1 -1
  73. package/src/accounts/bulkAccountLoader.js +249 -0
  74. package/src/accounts/bulkUserStatsSubscription.js +75 -0
  75. package/src/accounts/bulkUserSubscription.js +75 -0
  76. package/src/accounts/fetch.js +92 -0
  77. package/src/accounts/pollingClearingHouseAccountSubscriber.js +465 -0
  78. package/src/accounts/pollingClearingHouseAccountSubscriber.ts +38 -38
  79. package/src/accounts/pollingOracleSubscriber.js +156 -0
  80. package/src/accounts/pollingTokenAccountSubscriber.js +141 -0
  81. package/src/accounts/pollingUserAccountSubscriber.js +208 -0
  82. package/src/accounts/pollingUserStatsAccountSubscriber.js +208 -0
  83. package/src/accounts/types.js +28 -0
  84. package/src/accounts/types.ts +11 -9
  85. package/src/accounts/utils.js +7 -0
  86. package/src/accounts/webSocketAccountSubscriber.js +138 -0
  87. package/src/accounts/webSocketClearingHouseAccountSubscriber.js +433 -0
  88. package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +59 -52
  89. package/src/accounts/webSocketUserAccountSubscriber.js +113 -0
  90. package/src/accounts/webSocketUserStatsAccountSubsriber.js +113 -0
  91. package/src/addresses/pda.js +186 -0
  92. package/src/addresses/pda.ts +56 -42
  93. package/src/admin.js +1284 -0
  94. package/src/admin.ts +149 -75
  95. package/src/assert/assert.js +1 -1
  96. package/src/clearingHouse.js +3433 -0
  97. package/src/clearingHouse.ts +1097 -380
  98. package/src/clearingHouseConfig.js +2 -0
  99. package/src/clearingHouseConfig.ts +2 -2
  100. package/src/clearingHouseUser.js +874 -0
  101. package/src/clearingHouseUser.ts +237 -172
  102. package/src/clearingHouseUserConfig.js +2 -0
  103. package/src/clearingHouseUserStats.js +115 -0
  104. package/src/clearingHouseUserStatsConfig.js +2 -0
  105. package/src/config.js +80 -0
  106. package/src/config.ts +30 -30
  107. package/src/constants/numericConstants.js +18 -11
  108. package/src/constants/numericConstants.ts +17 -15
  109. package/{lib/constants/markets.js → src/constants/perpMarkets.js} +11 -11
  110. package/src/constants/{markets.ts → perpMarkets.ts} +5 -5
  111. package/src/constants/spotMarkets.js +51 -0
  112. package/src/constants/{banks.ts → spotMarkets.ts} +19 -19
  113. package/src/events/eventList.js +66 -23
  114. package/src/events/eventSubscriber.js +202 -0
  115. package/src/events/eventSubscriber.ts +20 -12
  116. package/src/events/fetchLogs.js +117 -0
  117. package/src/events/fetchLogs.ts +35 -8
  118. package/src/events/pollingLogProvider.js +113 -0
  119. package/src/events/pollingLogProvider.ts +10 -2
  120. package/src/events/sort.js +41 -0
  121. package/src/events/txEventCache.js +22 -19
  122. package/src/events/types.js +25 -0
  123. package/src/events/types.ts +7 -1
  124. package/src/events/webSocketLogProvider.js +76 -0
  125. package/src/examples/makeTradeExample.ts +27 -6
  126. package/src/factory/bigNum.js +183 -180
  127. package/src/factory/oracleClient.js +9 -9
  128. package/src/idl/clearing_house.json +1128 -347
  129. package/src/index.js +75 -0
  130. package/src/index.ts +6 -3
  131. package/src/math/amm.js +422 -0
  132. package/src/math/amm.ts +73 -5
  133. package/src/math/auction.js +10 -10
  134. package/src/math/conversion.js +4 -3
  135. package/src/math/funding.js +223 -175
  136. package/src/math/funding.ts +7 -7
  137. package/src/math/insurance.js +27 -0
  138. package/src/math/margin.js +77 -0
  139. package/src/math/margin.ts +34 -23
  140. package/src/math/market.js +105 -0
  141. package/src/math/market.ts +71 -19
  142. package/src/math/oracles.js +40 -10
  143. package/src/math/oracles.ts +18 -1
  144. package/src/math/orders.js +153 -0
  145. package/src/math/orders.ts +5 -5
  146. package/src/math/position.js +172 -0
  147. package/src/math/position.ts +31 -31
  148. package/src/math/repeg.js +40 -40
  149. package/src/math/spotBalance.js +176 -0
  150. package/src/math/spotBalance.ts +290 -0
  151. package/src/math/spotMarket.js +8 -0
  152. package/src/math/spotMarket.ts +9 -0
  153. package/src/math/spotPosition.js +8 -0
  154. package/src/math/spotPosition.ts +6 -0
  155. package/src/math/state.ts +2 -2
  156. package/src/math/trade.js +81 -74
  157. package/src/math/trade.ts +4 -4
  158. package/src/math/utils.js +8 -7
  159. package/src/oracles/oracleClientCache.js +10 -9
  160. package/src/oracles/pythClient.js +52 -17
  161. package/src/oracles/quoteAssetOracleClient.js +44 -13
  162. package/src/oracles/switchboardClient.js +69 -37
  163. package/src/oracles/types.js +1 -1
  164. package/src/orderParams.js +14 -6
  165. package/src/orderParams.ts +16 -8
  166. package/src/serum/serumSubscriber.js +102 -0
  167. package/src/serum/serumSubscriber.ts +80 -0
  168. package/src/serum/types.js +2 -0
  169. package/src/serum/types.ts +13 -0
  170. package/src/slot/SlotSubscriber.js +67 -20
  171. package/src/token/index.js +4 -4
  172. package/src/tokenFaucet.js +288 -154
  173. package/src/tx/retryTxSender.js +280 -0
  174. package/src/tx/retryTxSender.ts +5 -2
  175. package/src/tx/types.js +1 -1
  176. package/src/tx/types.ts +2 -1
  177. package/src/tx/utils.js +7 -6
  178. package/src/types.js +216 -0
  179. package/src/types.ts +131 -39
  180. package/src/userName.js +5 -5
  181. package/src/util/computeUnits.js +46 -11
  182. package/src/util/promiseTimeout.js +5 -5
  183. package/src/util/tps.js +46 -12
  184. package/src/wallet.js +55 -18
  185. package/lib/math/bankBalance.d.ts +0 -15
  186. package/lib/math/bankBalance.js +0 -150
  187. package/src/addresses/marketAddresses.js +0 -26
  188. package/src/constants/banks.js +0 -42
  189. package/src/examples/makeTradeExample.js +0 -80
  190. package/src/math/bankBalance.ts +0 -258
  191. package/src/math/state.js +0 -15
  192. package/src/math/utils.js.map +0 -1
  193. package/src/util/getTokenAddress.js +0 -9
@@ -0,0 +1,113 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (_) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ exports.__esModule = true;
39
+ exports.PollingLogProvider = void 0;
40
+ var fetchLogs_1 = require("./fetchLogs");
41
+ var PollingLogProvider = /** @class */ (function () {
42
+ function PollingLogProvider(connection, programId, commitment, frequency) {
43
+ if (frequency === void 0) { frequency = 15 * 1000; }
44
+ this.connection = connection;
45
+ this.programId = programId;
46
+ this.frequency = frequency;
47
+ this.firstFetch = true;
48
+ this.finality = commitment === 'finalized' ? 'finalized' : 'confirmed';
49
+ }
50
+ PollingLogProvider.prototype.subscribe = function (callback, skipHistory) {
51
+ var _this = this;
52
+ if (this.intervalId) {
53
+ return true;
54
+ }
55
+ this.intervalId = setInterval(function () { return __awaiter(_this, void 0, void 0, function () {
56
+ var response, mostRecentTx, transactionLogs, _i, transactionLogs_1, _a, txSig, slot, logs, e_1;
57
+ return __generator(this, function (_b) {
58
+ switch (_b.label) {
59
+ case 0:
60
+ if (this.mutex === 1) {
61
+ return [2 /*return*/];
62
+ }
63
+ this.mutex = 1;
64
+ _b.label = 1;
65
+ case 1:
66
+ _b.trys.push([1, 3, 4, 5]);
67
+ return [4 /*yield*/, (0, fetchLogs_1.fetchLogs)(this.connection, this.programId, this.finality, undefined, this.mostRecentSeenTx,
68
+ // If skipping history, only fetch one log back, not the maximum amount available
69
+ skipHistory && this.firstFetch ? 1 : undefined)];
70
+ case 2:
71
+ response = _b.sent();
72
+ this.firstFetch = false;
73
+ if (response === undefined) {
74
+ return [2 /*return*/];
75
+ }
76
+ mostRecentTx = response.mostRecentTx, transactionLogs = response.transactionLogs;
77
+ for (_i = 0, transactionLogs_1 = transactionLogs; _i < transactionLogs_1.length; _i++) {
78
+ _a = transactionLogs_1[_i], txSig = _a.txSig, slot = _a.slot, logs = _a.logs;
79
+ callback(txSig, slot, logs);
80
+ }
81
+ this.mostRecentSeenTx = mostRecentTx;
82
+ return [3 /*break*/, 5];
83
+ case 3:
84
+ e_1 = _b.sent();
85
+ console.error('PollingLogProvider threw an Error');
86
+ console.error(e_1);
87
+ return [3 /*break*/, 5];
88
+ case 4:
89
+ this.mutex = 0;
90
+ return [7 /*endfinally*/];
91
+ case 5: return [2 /*return*/];
92
+ }
93
+ });
94
+ }); }, this.frequency);
95
+ return true;
96
+ };
97
+ PollingLogProvider.prototype.isSubscribed = function () {
98
+ return this.intervalId !== undefined;
99
+ };
100
+ PollingLogProvider.prototype.unsubscribe = function () {
101
+ return __awaiter(this, void 0, void 0, function () {
102
+ return __generator(this, function (_a) {
103
+ if (this.intervalId !== undefined) {
104
+ clearInterval(this.intervalId);
105
+ this.intervalId = undefined;
106
+ }
107
+ return [2 /*return*/, true];
108
+ });
109
+ });
110
+ };
111
+ return PollingLogProvider;
112
+ }());
113
+ exports.PollingLogProvider = PollingLogProvider;
@@ -13,6 +13,7 @@ export class PollingLogProvider implements LogProvider {
13
13
  private intervalId: NodeJS.Timer;
14
14
  private mostRecentSeenTx?: TransactionSignature;
15
15
  private mutex: number;
16
+ private firstFetch = true;
16
17
 
17
18
  public constructor(
18
19
  private connection: Connection,
@@ -23,7 +24,10 @@ export class PollingLogProvider implements LogProvider {
23
24
  this.finality = commitment === 'finalized' ? 'finalized' : 'confirmed';
24
25
  }
25
26
 
26
- public subscribe(callback: logProviderCallback): boolean {
27
+ public subscribe(
28
+ callback: logProviderCallback,
29
+ skipHistory?: boolean
30
+ ): boolean {
27
31
  if (this.intervalId) {
28
32
  return true;
29
33
  }
@@ -40,9 +44,13 @@ export class PollingLogProvider implements LogProvider {
40
44
  this.programId,
41
45
  this.finality,
42
46
  undefined,
43
- this.mostRecentSeenTx
47
+ this.mostRecentSeenTx,
48
+ // If skipping history, only fetch one log back, not the maximum amount available
49
+ skipHistory && this.firstFetch ? 1 : undefined
44
50
  );
45
51
 
52
+ this.firstFetch = false;
53
+
46
54
  if (response === undefined) {
47
55
  return;
48
56
  }
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ exports.__esModule = true;
3
+ exports.getSortFn = void 0;
4
+ var index_1 = require("../index");
5
+ function clientSortAscFn() {
6
+ return 'less than';
7
+ }
8
+ function clientSortDescFn() {
9
+ return 'greater than';
10
+ }
11
+ function defaultBlockchainSortFn(currentEvent, newEvent) {
12
+ return currentEvent.slot <= newEvent.slot ? 'less than' : 'greater than';
13
+ }
14
+ function orderActionRecordSortFn(currentEvent, newEvent) {
15
+ var _a, _b;
16
+ var currentEventMarketIndex = currentEvent.marketIndex;
17
+ var newEventMarketIndex = newEvent.marketIndex;
18
+ if (!currentEventMarketIndex.eq(newEventMarketIndex)) {
19
+ return currentEvent.ts.lte(newEvent.ts) ? 'less than' : 'greater than';
20
+ }
21
+ if (((_a = currentEvent.fillRecordId) === null || _a === void 0 ? void 0 : _a.gt(index_1.ZERO)) && ((_b = newEvent.fillRecordId) === null || _b === void 0 ? void 0 : _b.gt(index_1.ZERO))) {
22
+ return currentEvent.fillRecordId.lte(newEvent.fillRecordId)
23
+ ? 'less than'
24
+ : 'greater than';
25
+ }
26
+ else {
27
+ return currentEvent.ts.lte(newEvent.ts) ? 'less than' : 'greater than';
28
+ }
29
+ }
30
+ function getSortFn(orderBy, orderDir, eventType) {
31
+ if (orderBy === 'client') {
32
+ return orderDir === 'asc' ? clientSortAscFn : clientSortDescFn;
33
+ }
34
+ switch (eventType) {
35
+ case 'OrderActionRecord':
36
+ return orderActionRecordSortFn;
37
+ default:
38
+ return defaultBlockchainSortFn;
39
+ }
40
+ }
41
+ exports.getSortFn = getSortFn;
@@ -1,23 +1,25 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
2
+ exports.__esModule = true;
3
3
  exports.TxEventCache = void 0;
4
- class Node {
5
- constructor(key, value, next, prev) {
4
+ var Node = /** @class */ (function () {
5
+ function Node(key, value, next, prev) {
6
6
  this.key = key;
7
7
  this.value = value;
8
8
  this.next = next;
9
9
  this.prev = prev;
10
10
  }
11
- }
11
+ return Node;
12
+ }());
12
13
  // lru cache
13
- class TxEventCache {
14
- constructor(maxTx = 1024) {
14
+ var TxEventCache = /** @class */ (function () {
15
+ function TxEventCache(maxTx) {
16
+ if (maxTx === void 0) { maxTx = 1024; }
15
17
  this.maxTx = maxTx;
16
18
  this.size = 0;
17
19
  this.cacheMap = {};
18
20
  }
19
- add(key, events) {
20
- const existingNode = this.cacheMap[key];
21
+ TxEventCache.prototype.add = function (key, events) {
22
+ var existingNode = this.cacheMap[key];
21
23
  if (existingNode) {
22
24
  this.detach(existingNode);
23
25
  this.size--;
@@ -32,22 +34,22 @@ class TxEventCache {
32
34
  this.head = this.tail = new Node(key, events);
33
35
  }
34
36
  else {
35
- const node = new Node(key, events, this.head);
37
+ var node = new Node(key, events, this.head);
36
38
  this.head.prev = node;
37
39
  this.head = node;
38
40
  }
39
41
  // update cacheMap with LinkedList key and Node reference
40
42
  this.cacheMap[key] = this.head;
41
43
  this.size++;
42
- }
43
- has(key) {
44
+ };
45
+ TxEventCache.prototype.has = function (key) {
44
46
  return this.cacheMap.hasOwnProperty(key);
45
- }
46
- get(key) {
47
+ };
48
+ TxEventCache.prototype.get = function (key) {
47
49
  var _a;
48
50
  return (_a = this.cacheMap[key]) === null || _a === void 0 ? void 0 : _a.value;
49
- }
50
- detach(node) {
51
+ };
52
+ TxEventCache.prototype.detach = function (node) {
51
53
  if (node.prev !== undefined) {
52
54
  node.prev.next = node.next;
53
55
  }
@@ -60,12 +62,13 @@ class TxEventCache {
60
62
  else {
61
63
  this.tail = node.prev;
62
64
  }
63
- }
64
- clear() {
65
+ };
66
+ TxEventCache.prototype.clear = function () {
65
67
  this.head = undefined;
66
68
  this.tail = undefined;
67
69
  this.size = 0;
68
70
  this.cacheMap = {};
69
- }
70
- }
71
+ };
72
+ return TxEventCache;
73
+ }());
71
74
  exports.TxEventCache = TxEventCache;
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ exports.__esModule = true;
3
+ exports.DefaultEventSubscriptionOptions = void 0;
4
+ exports.DefaultEventSubscriptionOptions = {
5
+ eventTypes: [
6
+ 'DepositRecord',
7
+ 'FundingPaymentRecord',
8
+ 'LiquidationRecord',
9
+ 'OrderRecord',
10
+ 'OrderActionRecord',
11
+ 'FundingRateRecord',
12
+ 'NewUserRecord',
13
+ 'SettlePnlRecord',
14
+ 'LPRecord',
15
+ 'InsuranceFundRecord',
16
+ ],
17
+ maxEventsPerType: 4096,
18
+ orderBy: 'blockchain',
19
+ orderDir: 'asc',
20
+ commitment: 'confirmed',
21
+ maxTx: 4096,
22
+ logProviderConfig: {
23
+ type: 'websocket'
24
+ }
25
+ };
@@ -8,6 +8,8 @@ import {
8
8
  OrderActionRecord,
9
9
  OrderRecord,
10
10
  SettlePnlRecord,
11
+ LPRecord,
12
+ InsuranceFundRecord,
11
13
  } from '../index';
12
14
 
13
15
  export type EventSubscriptionOptions = {
@@ -33,6 +35,8 @@ export const DefaultEventSubscriptionOptions: EventSubscriptionOptions = {
33
35
  'FundingRateRecord',
34
36
  'NewUserRecord',
35
37
  'SettlePnlRecord',
38
+ 'LPRecord',
39
+ 'InsuranceFundRecord',
36
40
  ],
37
41
  maxEventsPerType: 4096,
38
42
  orderBy: 'blockchain',
@@ -68,6 +72,8 @@ export type EventMap = {
68
72
  OrderActionRecord: Event<OrderActionRecord>;
69
73
  SettlePnlRecord: Event<SettlePnlRecord>;
70
74
  NewUserRecord: Event<NewUserRecord>;
75
+ LPRecord: Event<LPRecord>;
76
+ InsuranceFundRecord: Event<InsuranceFundRecord>;
71
77
  };
72
78
 
73
79
  export type EventType = keyof EventMap;
@@ -89,7 +95,7 @@ export type logProviderCallback = (
89
95
 
90
96
  export interface LogProvider {
91
97
  isSubscribed(): boolean;
92
- subscribe(callback: logProviderCallback): boolean;
98
+ subscribe(callback: logProviderCallback, skipHistory?: boolean): boolean;
93
99
  unsubscribe(): Promise<boolean>;
94
100
  }
95
101
 
@@ -0,0 +1,76 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (_) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ exports.__esModule = true;
39
+ exports.WebSocketLogProvider = void 0;
40
+ var WebSocketLogProvider = /** @class */ (function () {
41
+ function WebSocketLogProvider(connection, programId, commitment) {
42
+ this.connection = connection;
43
+ this.programId = programId;
44
+ this.commitment = commitment;
45
+ }
46
+ WebSocketLogProvider.prototype.subscribe = function (callback) {
47
+ if (this.subscriptionId) {
48
+ return true;
49
+ }
50
+ this.subscriptionId = this.connection.onLogs(this.programId, function (logs, ctx) {
51
+ callback(logs.signature, ctx.slot, logs.logs);
52
+ }, this.commitment);
53
+ return true;
54
+ };
55
+ WebSocketLogProvider.prototype.isSubscribed = function () {
56
+ return this.subscriptionId !== undefined;
57
+ };
58
+ WebSocketLogProvider.prototype.unsubscribe = function () {
59
+ return __awaiter(this, void 0, void 0, function () {
60
+ return __generator(this, function (_a) {
61
+ switch (_a.label) {
62
+ case 0:
63
+ if (!(this.subscriptionId !== undefined)) return [3 /*break*/, 2];
64
+ return [4 /*yield*/, this.connection.removeOnLogsListener(this.subscriptionId)];
65
+ case 1:
66
+ _a.sent();
67
+ this.subscriptionId = undefined;
68
+ _a.label = 2;
69
+ case 2: return [2 /*return*/, true];
70
+ }
71
+ });
72
+ });
73
+ };
74
+ return WebSocketLogProvider;
75
+ }());
76
+ exports.WebSocketLogProvider = WebSocketLogProvider;
@@ -10,10 +10,12 @@ import {
10
10
  PositionDirection,
11
11
  convertToNumber,
12
12
  calculateTradeSlippage,
13
+ BulkAccountLoader,
14
+ PerpMarkets,
13
15
  MARK_PRICE_PRECISION,
14
16
  QUOTE_PRECISION,
15
17
  } from '..';
16
- import { Banks } from '../constants/banks';
18
+ import { SpotMarkets } from '../constants/spotMarkets';
17
19
 
18
20
  export const getTokenAddress = (
19
21
  mintAddress: string,
@@ -27,9 +29,11 @@ export const getTokenAddress = (
27
29
  );
28
30
  };
29
31
 
32
+ const cluster = 'devnet';
33
+
30
34
  const main = async () => {
31
35
  // Initialize Drift SDK
32
- const sdkConfig = initialize({ env: 'devnet' });
36
+ const sdkConfig = initialize({ env: cluster });
33
37
 
34
38
  // Set up the Wallet and Provider
35
39
  const privateKey = process.env.BOT_PRIVATE_KEY; // stored as an array string
@@ -63,10 +67,23 @@ const main = async () => {
63
67
  const clearingHousePublicKey = new PublicKey(
64
68
  sdkConfig.CLEARING_HOUSE_PROGRAM_ID
65
69
  );
70
+ const bulkAccountLoader = new BulkAccountLoader(
71
+ connection,
72
+ 'confirmed',
73
+ 1000
74
+ );
66
75
  const clearingHouse = new ClearingHouse({
67
76
  connection,
68
77
  wallet: provider.wallet,
69
78
  programID: clearingHousePublicKey,
79
+ perpMarketIndexes: PerpMarkets[cluster].map((market) => market.marketIndex),
80
+ spotMarketIndexes: SpotMarkets[cluster].map(
81
+ (spotMarket) => spotMarket.marketIndex
82
+ ),
83
+ accountSubscription: {
84
+ type: 'polling',
85
+ accountLoader: bulkAccountLoader,
86
+ },
70
87
  });
71
88
  await clearingHouse.subscribe();
72
89
 
@@ -74,6 +91,10 @@ const main = async () => {
74
91
  const user = new ClearingHouseUser({
75
92
  clearingHouse,
76
93
  userAccountPublicKey: await clearingHouse.getUserAccountPublicKey(),
94
+ accountSubscription: {
95
+ type: 'polling',
96
+ accountLoader: bulkAccountLoader,
97
+ },
77
98
  });
78
99
 
79
100
  //// Check if clearing house account exists for the current wallet
@@ -88,19 +109,19 @@ const main = async () => {
88
109
  usdcTokenAddress.toString(),
89
110
  wallet.publicKey.toString()
90
111
  ),
91
- Banks['devnet'][0].bankIndex
112
+ SpotMarkets['devnet'][0].marketIndex
92
113
  );
93
114
  }
94
115
 
95
116
  await user.subscribe();
96
117
 
97
118
  // Get current price
98
- const solMarketInfo = sdkConfig.MARKETS.find(
119
+ const solMarketInfo = sdkConfig.PERP_MARKETS.find(
99
120
  (market) => market.baseAssetSymbol === 'SOL'
100
121
  );
101
122
 
102
123
  const currentMarketPrice = calculateMarkPrice(
103
- clearingHouse.getMarketAccount(solMarketInfo.marketIndex),
124
+ clearingHouse.getPerpMarketAccount(solMarketInfo.marketIndex),
104
125
  undefined
105
126
  );
106
127
 
@@ -112,7 +133,7 @@ const main = async () => {
112
133
  console.log(`Current Market Price is $${formattedPrice}`);
113
134
 
114
135
  // Estimate the slippage for a $5000 LONG trade
115
- const solMarketAccount = clearingHouse.getMarketAccount(
136
+ const solMarketAccount = clearingHouse.getPerpMarketAccount(
116
137
  solMarketInfo.marketIndex
117
138
  );
118
139