@drift-labs/sdk 0.2.0-master.26 → 0.2.0-master.27

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 (143) hide show
  1. package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +1 -0
  2. package/lib/accounts/pollingClearingHouseAccountSubscriber.js +3 -0
  3. package/lib/accounts/types.d.ts +1 -0
  4. package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +1 -0
  5. package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +3 -0
  6. package/lib/admin.d.ts +5 -2
  7. package/lib/admin.js +30 -4
  8. package/lib/clearingHouse.d.ts +2 -0
  9. package/lib/clearingHouse.js +14 -1
  10. package/lib/clearingHouseUser.d.ts +11 -9
  11. package/lib/clearingHouseUser.js +182 -106
  12. package/lib/config.js +1 -1
  13. package/lib/dlob/DLOB.d.ts +73 -0
  14. package/lib/dlob/DLOB.js +557 -0
  15. package/lib/dlob/DLOBNode.d.ts +52 -0
  16. package/lib/dlob/DLOBNode.js +82 -0
  17. package/lib/dlob/NodeList.d.ts +26 -0
  18. package/lib/dlob/NodeList.js +138 -0
  19. package/lib/idl/clearing_house.json +122 -202
  20. package/lib/index.d.ts +5 -0
  21. package/lib/index.js +5 -0
  22. package/lib/math/market.d.ts +1 -1
  23. package/lib/math/market.js +1 -1
  24. package/lib/math/orders.d.ts +3 -3
  25. package/lib/math/orders.js +31 -16
  26. package/lib/math/spotBalance.d.ts +3 -0
  27. package/lib/math/spotBalance.js +18 -1
  28. package/lib/math/spotPosition.d.ts +5 -1
  29. package/lib/math/spotPosition.js +16 -1
  30. package/lib/types.d.ts +24 -23
  31. package/lib/types.js +9 -3
  32. package/lib/userMap/userMap.d.ts +25 -0
  33. package/lib/userMap/userMap.js +73 -0
  34. package/lib/userMap/userStatsMap.d.ts +19 -0
  35. package/lib/userMap/userStatsMap.js +68 -0
  36. package/package.json +6 -3
  37. package/src/accounts/pollingClearingHouseAccountSubscriber.ts +4 -0
  38. package/src/accounts/types.ts +1 -0
  39. package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +6 -0
  40. package/src/addresses/marketAddresses.js +26 -0
  41. package/src/admin.ts +50 -8
  42. package/src/assert/assert.js +1 -1
  43. package/src/clearingHouse.ts +23 -1
  44. package/src/clearingHouseUser.ts +364 -165
  45. package/src/config.ts +1 -1
  46. package/src/constants/banks.js +42 -0
  47. package/src/constants/{perpMarkets.js → markets.js} +11 -11
  48. package/src/dlob/DLOB.ts +884 -0
  49. package/src/dlob/DLOBNode.ts +163 -0
  50. package/src/dlob/NodeList.ts +185 -0
  51. package/src/events/eventList.js +23 -66
  52. package/src/events/txEventCache.js +19 -22
  53. package/src/examples/makeTradeExample.js +157 -0
  54. package/src/factory/bigNum.js +180 -183
  55. package/src/factory/oracleClient.js +9 -9
  56. package/src/idl/clearing_house.json +122 -202
  57. package/src/index.ts +5 -0
  58. package/src/math/auction.js +10 -10
  59. package/src/math/conversion.js +3 -4
  60. package/src/math/funding.js +175 -223
  61. package/src/math/market.ts +1 -1
  62. package/src/math/orders.ts +29 -21
  63. package/src/math/repeg.js +40 -40
  64. package/src/math/spotBalance.ts +26 -0
  65. package/src/math/spotPosition.ts +42 -1
  66. package/src/math/trade.js +74 -81
  67. package/src/math/utils.js +7 -8
  68. package/src/math/utils.js.map +1 -0
  69. package/src/oracles/oracleClientCache.js +9 -10
  70. package/src/oracles/pythClient.js +17 -52
  71. package/src/oracles/quoteAssetOracleClient.js +13 -44
  72. package/src/oracles/switchboardClient.js +37 -69
  73. package/src/oracles/types.js +1 -1
  74. package/src/token/index.js +4 -4
  75. package/src/tx/types.js +1 -1
  76. package/src/tx/utils.js +6 -7
  77. package/src/types.ts +25 -23
  78. package/src/userMap/userMap.ts +100 -0
  79. package/src/userMap/userStatsMap.ts +110 -0
  80. package/src/userName.js +5 -5
  81. package/src/util/computeUnits.js +11 -46
  82. package/src/util/getTokenAddress.js +9 -0
  83. package/src/util/promiseTimeout.js +5 -5
  84. package/src/util/tps.js +12 -46
  85. package/src/wallet.js +18 -55
  86. package/tests/bn/test.ts +2 -3
  87. package/tests/dlob/helpers.ts +322 -0
  88. package/tests/dlob/test.ts +2865 -0
  89. package/my-script/.env +0 -7
  90. package/my-script/getUserStats.ts +0 -106
  91. package/my-script/multiConnections.ts +0 -119
  92. package/my-script/test-regex.ts +0 -11
  93. package/my-script/utils.ts +0 -52
  94. package/src/accounts/bulkAccountLoader.js +0 -249
  95. package/src/accounts/bulkUserStatsSubscription.js +0 -75
  96. package/src/accounts/bulkUserSubscription.js +0 -75
  97. package/src/accounts/fetch.js +0 -92
  98. package/src/accounts/pollingClearingHouseAccountSubscriber.js +0 -465
  99. package/src/accounts/pollingOracleSubscriber.js +0 -156
  100. package/src/accounts/pollingTokenAccountSubscriber.js +0 -141
  101. package/src/accounts/pollingUserAccountSubscriber.js +0 -208
  102. package/src/accounts/pollingUserStatsAccountSubscriber.js +0 -208
  103. package/src/accounts/types.js +0 -28
  104. package/src/accounts/utils.js +0 -7
  105. package/src/accounts/webSocketAccountSubscriber.js +0 -138
  106. package/src/accounts/webSocketClearingHouseAccountSubscriber.js +0 -433
  107. package/src/accounts/webSocketUserAccountSubscriber.js +0 -113
  108. package/src/accounts/webSocketUserStatsAccountSubsriber.js +0 -113
  109. package/src/addresses/pda.js +0 -186
  110. package/src/admin.js +0 -1284
  111. package/src/clearingHouse.js +0 -3433
  112. package/src/clearingHouseConfig.js +0 -2
  113. package/src/clearingHouseUser.js +0 -874
  114. package/src/clearingHouseUserConfig.js +0 -2
  115. package/src/clearingHouseUserStats.js +0 -115
  116. package/src/clearingHouseUserStatsConfig.js +0 -2
  117. package/src/config.js +0 -80
  118. package/src/constants/numericConstants.js +0 -48
  119. package/src/constants/spotMarkets.js +0 -51
  120. package/src/events/eventSubscriber.js +0 -202
  121. package/src/events/fetchLogs.js +0 -117
  122. package/src/events/pollingLogProvider.js +0 -113
  123. package/src/events/sort.js +0 -41
  124. package/src/events/types.js +0 -25
  125. package/src/events/webSocketLogProvider.js +0 -76
  126. package/src/index.js +0 -75
  127. package/src/math/amm.js +0 -422
  128. package/src/math/insurance.js +0 -27
  129. package/src/math/margin.js +0 -77
  130. package/src/math/market.js +0 -105
  131. package/src/math/oracles.js +0 -56
  132. package/src/math/orders.js +0 -153
  133. package/src/math/position.js +0 -172
  134. package/src/math/spotBalance.js +0 -176
  135. package/src/math/spotMarket.js +0 -8
  136. package/src/math/spotPosition.js +0 -8
  137. package/src/orderParams.js +0 -28
  138. package/src/serum/serumSubscriber.js +0 -102
  139. package/src/serum/types.js +0 -2
  140. package/src/slot/SlotSubscriber.js +0 -86
  141. package/src/tokenFaucet.js +0 -323
  142. package/src/tx/retryTxSender.js +0 -280
  143. package/src/types.js +0 -216
@@ -0,0 +1,163 @@
1
+ import {
2
+ isOneOfVariant,
3
+ AMM_RESERVE_PRECISION,
4
+ BN,
5
+ convertToNumber,
6
+ getLimitPrice,
7
+ isVariant,
8
+ SpotMarketAccount,
9
+ PerpMarketAccount,
10
+ MARK_PRICE_PRECISION,
11
+ OraclePriceData,
12
+ Order,
13
+ ZERO,
14
+ } from '..';
15
+ import { PublicKey } from '@solana/web3.js';
16
+ import { getOrderSignature } from './NodeList';
17
+
18
+ export interface DLOBNode {
19
+ getPrice(oraclePriceData: OraclePriceData, slot: number): BN;
20
+ isVammNode(): boolean;
21
+ order: Order | undefined;
22
+ haveFilled: boolean;
23
+ userAccount: PublicKey | undefined;
24
+ market: SpotMarketAccount | PerpMarketAccount;
25
+ }
26
+
27
+ export abstract class OrderNode implements DLOBNode {
28
+ order: Order;
29
+ market: SpotMarketAccount | PerpMarketAccount;
30
+ userAccount: PublicKey;
31
+ sortValue: BN;
32
+ haveFilled = false;
33
+ haveTrigger = false;
34
+
35
+ constructor(
36
+ order: Order,
37
+ market: SpotMarketAccount | PerpMarketAccount,
38
+ userAccount: PublicKey
39
+ ) {
40
+ this.order = order;
41
+ this.market = market;
42
+ this.userAccount = userAccount;
43
+ this.sortValue = this.getSortValue(order);
44
+ }
45
+
46
+ abstract getSortValue(order: Order): BN;
47
+
48
+ public getLabel(): string {
49
+ let msg = `Order ${getOrderSignature(
50
+ this.order.orderId,
51
+ this.userAccount
52
+ )}`;
53
+ msg += ` ${isVariant(this.order.direction, 'long') ? 'LONG' : 'SHORT'} `;
54
+ msg += `${convertToNumber(
55
+ this.order.baseAssetAmount,
56
+ AMM_RESERVE_PRECISION
57
+ ).toFixed(3)}`;
58
+ if (this.order.price.gt(ZERO)) {
59
+ msg += ` @ ${convertToNumber(
60
+ this.order.price,
61
+ MARK_PRICE_PRECISION
62
+ ).toFixed(3)}`;
63
+ }
64
+ if (this.order.triggerPrice.gt(ZERO)) {
65
+ msg += ` ${
66
+ isVariant(this.order.triggerCondition, 'below') ? 'BELOW' : 'ABOVE'
67
+ }`;
68
+ msg += ` ${convertToNumber(
69
+ this.order.triggerPrice,
70
+ MARK_PRICE_PRECISION
71
+ ).toFixed(3)}`;
72
+ }
73
+ return msg;
74
+ }
75
+
76
+ getPrice(oraclePriceData: OraclePriceData, slot: number): BN {
77
+ if (isOneOfVariant(this.order.marketType, ['spot'])) {
78
+ return getLimitPrice(this.order, oraclePriceData, slot);
79
+ } else if (isOneOfVariant(this.order.marketType, ['perp'])) {
80
+ return getLimitPrice(
81
+ this.order,
82
+ oraclePriceData,
83
+ slot,
84
+ this.market as PerpMarketAccount
85
+ );
86
+ } else {
87
+ console.error(`Unknown market type: ${this.order.marketType}`);
88
+ }
89
+ }
90
+
91
+ isVammNode(): boolean {
92
+ return false;
93
+ }
94
+ }
95
+
96
+ export class LimitOrderNode extends OrderNode {
97
+ next?: LimitOrderNode;
98
+ previous?: LimitOrderNode;
99
+
100
+ getSortValue(order: Order): BN {
101
+ return order.price;
102
+ }
103
+ }
104
+
105
+ export class FloatingLimitOrderNode extends OrderNode {
106
+ next?: FloatingLimitOrderNode;
107
+ previous?: FloatingLimitOrderNode;
108
+
109
+ getSortValue(order: Order): BN {
110
+ return order.oraclePriceOffset;
111
+ }
112
+ }
113
+
114
+ export class MarketOrderNode extends OrderNode {
115
+ next?: MarketOrderNode;
116
+ previous?: MarketOrderNode;
117
+
118
+ getSortValue(order: Order): BN {
119
+ return order.slot;
120
+ }
121
+ }
122
+
123
+ export class TriggerOrderNode extends OrderNode {
124
+ next?: TriggerOrderNode;
125
+ previous?: TriggerOrderNode;
126
+
127
+ getSortValue(order: Order): BN {
128
+ return order.triggerPrice;
129
+ }
130
+ }
131
+
132
+ export type DLOBNodeMap = {
133
+ limit: LimitOrderNode;
134
+ floatingLimit: FloatingLimitOrderNode;
135
+ market: MarketOrderNode;
136
+ trigger: TriggerOrderNode;
137
+ };
138
+
139
+ export type DLOBNodeType =
140
+ | 'limit'
141
+ | 'floatingLimit'
142
+ | 'market'
143
+ | ('trigger' & keyof DLOBNodeMap);
144
+
145
+ export function createNode<T extends DLOBNodeType>(
146
+ nodeType: T,
147
+ order: Order,
148
+ market: SpotMarketAccount | PerpMarketAccount,
149
+ userAccount: PublicKey
150
+ ): DLOBNodeMap[T] {
151
+ switch (nodeType) {
152
+ case 'floatingLimit':
153
+ return new FloatingLimitOrderNode(order, market, userAccount);
154
+ case 'limit':
155
+ return new LimitOrderNode(order, market, userAccount);
156
+ case 'market':
157
+ return new MarketOrderNode(order, market, userAccount);
158
+ case 'trigger':
159
+ return new TriggerOrderNode(order, market, userAccount);
160
+ default:
161
+ throw Error(`Unknown DLOBNode type ${nodeType}`);
162
+ }
163
+ }
@@ -0,0 +1,185 @@
1
+ import {
2
+ BN,
3
+ isVariant,
4
+ MarketTypeStr,
5
+ Order,
6
+ PerpMarketAccount,
7
+ SpotMarketAccount,
8
+ } from '..';
9
+ import { PublicKey } from '@solana/web3.js';
10
+ import { createNode, DLOBNode, DLOBNodeMap } from './DLOBNode';
11
+
12
+ export type SortDirection = 'asc' | 'desc';
13
+
14
+ export function getOrderSignature(orderId: BN, userAccount: PublicKey): string {
15
+ return `${userAccount.toString()}-${orderId.toString()}`;
16
+ }
17
+
18
+ export interface DLOBNodeGenerator {
19
+ getGenerator(): Generator<DLOBNode>;
20
+ }
21
+
22
+ export class NodeList<NodeType extends keyof DLOBNodeMap>
23
+ implements DLOBNodeGenerator
24
+ {
25
+ head?: DLOBNodeMap[NodeType];
26
+ length = 0;
27
+ nodeMap = new Map<string, DLOBNodeMap[NodeType]>();
28
+
29
+ constructor(
30
+ private nodeType: NodeType,
31
+ private sortDirection: SortDirection
32
+ ) {}
33
+
34
+ public insert(
35
+ order: Order,
36
+ marketType: MarketTypeStr,
37
+ market: PerpMarketAccount | SpotMarketAccount,
38
+ userAccount: PublicKey
39
+ ): void {
40
+ if (isVariant(order.status, 'init')) {
41
+ return;
42
+ }
43
+
44
+ if (marketType === 'spot') {
45
+ market = market as SpotMarketAccount;
46
+ } else if (marketType === 'perp') {
47
+ market = market as PerpMarketAccount;
48
+ }
49
+
50
+ const newNode = createNode(this.nodeType, order, market, userAccount);
51
+
52
+ const orderId = getOrderSignature(order.orderId, userAccount);
53
+ if (this.nodeMap.has(orderId)) {
54
+ return;
55
+ }
56
+ this.nodeMap.set(orderId, newNode);
57
+
58
+ this.length += 1;
59
+
60
+ if (this.head === undefined) {
61
+ this.head = newNode;
62
+ return;
63
+ }
64
+
65
+ if (this.prependNode(this.head, newNode)) {
66
+ this.head.previous = newNode;
67
+ newNode.next = this.head;
68
+ this.head = newNode;
69
+ return;
70
+ }
71
+
72
+ let currentNode = this.head;
73
+ while (
74
+ currentNode.next !== undefined &&
75
+ !this.prependNode(currentNode.next, newNode)
76
+ ) {
77
+ currentNode = currentNode.next;
78
+ }
79
+
80
+ newNode.next = currentNode.next;
81
+ if (currentNode.next !== undefined) {
82
+ newNode.next.previous = newNode;
83
+ }
84
+ currentNode.next = newNode;
85
+ newNode.previous = currentNode;
86
+ }
87
+
88
+ prependNode(
89
+ currentNode: DLOBNodeMap[NodeType],
90
+ newNode: DLOBNodeMap[NodeType]
91
+ ): boolean {
92
+ const currentOrder = currentNode.order;
93
+ const newOrder = newNode.order;
94
+
95
+ const currentOrderSortPrice = currentNode.sortValue;
96
+ const newOrderSortPrice = newNode.sortValue;
97
+
98
+ if (newOrderSortPrice.eq(currentOrderSortPrice)) {
99
+ return newOrder.ts.lt(currentOrder.ts);
100
+ }
101
+
102
+ if (this.sortDirection === 'asc') {
103
+ return newOrderSortPrice.lt(currentOrderSortPrice);
104
+ } else {
105
+ return newOrderSortPrice.gt(currentOrderSortPrice);
106
+ }
107
+ }
108
+
109
+ public update(order: Order, userAccount: PublicKey): void {
110
+ const orderId = getOrderSignature(order.orderId, userAccount);
111
+ if (this.nodeMap.has(orderId)) {
112
+ const node = this.nodeMap.get(orderId);
113
+ Object.assign(node.order, order);
114
+ node.haveFilled = false;
115
+ }
116
+ }
117
+
118
+ public remove(order: Order, userAccount: PublicKey): void {
119
+ const orderId = getOrderSignature(order.orderId, userAccount);
120
+ if (this.nodeMap.has(orderId)) {
121
+ const node = this.nodeMap.get(orderId);
122
+ if (node.next) {
123
+ node.next.previous = node.previous;
124
+ }
125
+ if (node.previous) {
126
+ node.previous.next = node.next;
127
+ }
128
+
129
+ if (this.head && node.order.orderId.eq(this.head.order.orderId)) {
130
+ this.head = node.next;
131
+ }
132
+
133
+ node.previous = undefined;
134
+ node.next = undefined;
135
+
136
+ this.nodeMap.delete(orderId);
137
+
138
+ this.length--;
139
+ }
140
+ }
141
+
142
+ *getGenerator(): Generator<DLOBNode> {
143
+ let node = this.head;
144
+ while (node !== undefined) {
145
+ yield node;
146
+ node = node.next;
147
+ }
148
+ }
149
+
150
+ public has(order: Order, userAccount: PublicKey): boolean {
151
+ return this.nodeMap.has(getOrderSignature(order.orderId, userAccount));
152
+ }
153
+
154
+ public print(): void {
155
+ let currentNode = this.head;
156
+ while (currentNode !== undefined) {
157
+ console.log(currentNode.getLabel());
158
+ currentNode = currentNode.next;
159
+ }
160
+ }
161
+
162
+ public printTop(): void {
163
+ if (this.head) {
164
+ console.log(this.sortDirection.toUpperCase(), this.head.getLabel());
165
+ } else {
166
+ console.log('---');
167
+ }
168
+ }
169
+ }
170
+
171
+ export function* getVammNodeGenerator(
172
+ price: BN | undefined
173
+ ): Generator<DLOBNode> {
174
+ if (!price) {
175
+ return;
176
+ }
177
+ yield {
178
+ getPrice: () => price,
179
+ isVammNode: () => true,
180
+ order: undefined,
181
+ market: undefined,
182
+ userAccount: undefined,
183
+ haveFilled: false,
184
+ };
185
+ }
@@ -1,52 +1,24 @@
1
1
  "use strict";
2
- var __generator = (this && this.__generator) || function (thisArg, body) {
3
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
4
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
5
- function verb(n) { return function (v) { return step([n, v]); }; }
6
- function step(op) {
7
- if (f) throw new TypeError("Generator is already executing.");
8
- while (_) try {
9
- 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;
10
- if (y = 0, t) op = [op[0] & 2, t.value];
11
- switch (op[0]) {
12
- case 0: case 1: t = op; break;
13
- case 4: _.label++; return { value: op[1], done: false };
14
- case 5: _.label++; y = op[1]; op = [0]; continue;
15
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
16
- default:
17
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
18
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
19
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
20
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
21
- if (t[2]) _.ops.pop();
22
- _.trys.pop(); continue;
23
- }
24
- op = body.call(thisArg, _);
25
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
26
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
27
- }
28
- };
29
- exports.__esModule = true;
2
+ Object.defineProperty(exports, "__esModule", { value: true });
30
3
  exports.EventList = void 0;
31
- var Node = /** @class */ (function () {
32
- function Node(event, next, prev) {
4
+ class Node {
5
+ constructor(event, next, prev) {
33
6
  this.event = event;
34
7
  this.next = next;
35
8
  this.prev = prev;
36
9
  }
37
- return Node;
38
- }());
39
- var EventList = /** @class */ (function () {
40
- function EventList(eventType, maxSize, sortFn, orderDirection) {
10
+ }
11
+ class EventList {
12
+ constructor(eventType, maxSize, sortFn, orderDirection) {
41
13
  this.eventType = eventType;
42
14
  this.maxSize = maxSize;
43
15
  this.sortFn = sortFn;
44
16
  this.orderDirection = orderDirection;
45
17
  this.size = 0;
46
18
  }
47
- EventList.prototype.insert = function (event) {
19
+ insert(event) {
48
20
  this.size++;
49
- var newNode = new Node(event);
21
+ const newNode = new Node(event);
50
22
  if (this.head === undefined) {
51
23
  this.head = this.tail = newNode;
52
24
  return;
@@ -58,7 +30,7 @@ var EventList = /** @class */ (function () {
58
30
  this.head = newNode;
59
31
  }
60
32
  else {
61
- var currentNode = this.head;
33
+ let currentNode = this.head;
62
34
  while (currentNode.next !== undefined &&
63
35
  this.sortFn(currentNode.next.event, newNode.event) !==
64
36
  (this.orderDirection === 'asc' ? 'less than' : 'greater than')) {
@@ -68,18 +40,15 @@ var EventList = /** @class */ (function () {
68
40
  if (currentNode.next !== undefined) {
69
41
  newNode.next.prev = newNode;
70
42
  }
71
- else {
72
- this.tail = newNode;
73
- }
74
43
  currentNode.next = newNode;
75
44
  newNode.prev = currentNode;
76
45
  }
77
46
  if (this.size > this.maxSize) {
78
47
  this.detach();
79
48
  }
80
- };
81
- EventList.prototype.detach = function () {
82
- var node = this.tail;
49
+ }
50
+ detach() {
51
+ const node = this.tail;
83
52
  if (node.prev !== undefined) {
84
53
  node.prev.next = node.next;
85
54
  }
@@ -93,28 +62,16 @@ var EventList = /** @class */ (function () {
93
62
  this.tail = node.prev;
94
63
  }
95
64
  this.size--;
96
- };
97
- EventList.prototype.toArray = function () {
65
+ }
66
+ toArray() {
98
67
  return Array.from(this);
99
- };
100
- EventList.prototype[Symbol.iterator] = function () {
101
- var node;
102
- return __generator(this, function (_a) {
103
- switch (_a.label) {
104
- case 0:
105
- node = this.head;
106
- _a.label = 1;
107
- case 1:
108
- if (!node) return [3 /*break*/, 3];
109
- return [4 /*yield*/, node.event];
110
- case 2:
111
- _a.sent();
112
- node = node.next;
113
- return [3 /*break*/, 1];
114
- case 3: return [2 /*return*/];
115
- }
116
- });
117
- };
118
- return EventList;
119
- }());
68
+ }
69
+ *[Symbol.iterator]() {
70
+ let node = this.head;
71
+ while (node) {
72
+ yield node.event;
73
+ node = node.next;
74
+ }
75
+ }
76
+ }
120
77
  exports.EventList = EventList;
@@ -1,25 +1,23 @@
1
1
  "use strict";
2
- exports.__esModule = true;
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.TxEventCache = void 0;
4
- var Node = /** @class */ (function () {
5
- function Node(key, value, next, prev) {
4
+ class Node {
5
+ constructor(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
- return Node;
12
- }());
11
+ }
13
12
  // lru cache
14
- var TxEventCache = /** @class */ (function () {
15
- function TxEventCache(maxTx) {
16
- if (maxTx === void 0) { maxTx = 1024; }
13
+ class TxEventCache {
14
+ constructor(maxTx = 1024) {
17
15
  this.maxTx = maxTx;
18
16
  this.size = 0;
19
17
  this.cacheMap = {};
20
18
  }
21
- TxEventCache.prototype.add = function (key, events) {
22
- var existingNode = this.cacheMap[key];
19
+ add(key, events) {
20
+ const existingNode = this.cacheMap[key];
23
21
  if (existingNode) {
24
22
  this.detach(existingNode);
25
23
  this.size--;
@@ -34,22 +32,22 @@ var TxEventCache = /** @class */ (function () {
34
32
  this.head = this.tail = new Node(key, events);
35
33
  }
36
34
  else {
37
- var node = new Node(key, events, this.head);
35
+ const node = new Node(key, events, this.head);
38
36
  this.head.prev = node;
39
37
  this.head = node;
40
38
  }
41
39
  // update cacheMap with LinkedList key and Node reference
42
40
  this.cacheMap[key] = this.head;
43
41
  this.size++;
44
- };
45
- TxEventCache.prototype.has = function (key) {
42
+ }
43
+ has(key) {
46
44
  return this.cacheMap.hasOwnProperty(key);
47
- };
48
- TxEventCache.prototype.get = function (key) {
45
+ }
46
+ get(key) {
49
47
  var _a;
50
48
  return (_a = this.cacheMap[key]) === null || _a === void 0 ? void 0 : _a.value;
51
- };
52
- TxEventCache.prototype.detach = function (node) {
49
+ }
50
+ detach(node) {
53
51
  if (node.prev !== undefined) {
54
52
  node.prev.next = node.next;
55
53
  }
@@ -62,13 +60,12 @@ var TxEventCache = /** @class */ (function () {
62
60
  else {
63
61
  this.tail = node.prev;
64
62
  }
65
- };
66
- TxEventCache.prototype.clear = function () {
63
+ }
64
+ clear() {
67
65
  this.head = undefined;
68
66
  this.tail = undefined;
69
67
  this.size = 0;
70
68
  this.cacheMap = {};
71
- };
72
- return TxEventCache;
73
- }());
69
+ }
70
+ }
74
71
  exports.TxEventCache = TxEventCache;