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

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 +2 -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 +3 -0
  9. package/lib/clearingHouse.js +19 -1
  10. package/lib/clearingHouseUser.d.ts +11 -9
  11. package/lib/clearingHouseUser.js +184 -108
  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 +123 -203
  20. package/lib/index.d.ts +5 -0
  21. package/lib/index.js +5 -0
  22. package/lib/math/market.d.ts +3 -2
  23. package/lib/math/market.js +17 -12
  24. package/lib/math/orders.d.ts +3 -3
  25. package/lib/math/orders.js +31 -16
  26. package/lib/math/position.d.ts +3 -3
  27. package/lib/math/position.js +23 -16
  28. package/lib/math/repeg.js +8 -0
  29. package/lib/math/spotBalance.d.ts +3 -0
  30. package/lib/math/spotBalance.js +18 -1
  31. package/lib/math/spotPosition.d.ts +5 -1
  32. package/lib/math/spotPosition.js +16 -1
  33. package/lib/types.d.ts +25 -24
  34. package/lib/types.js +9 -3
  35. package/lib/userMap/userMap.d.ts +25 -0
  36. package/lib/userMap/userMap.js +73 -0
  37. package/lib/userMap/userStatsMap.d.ts +19 -0
  38. package/lib/userMap/userStatsMap.js +68 -0
  39. package/package.json +6 -3
  40. package/src/accounts/pollingClearingHouseAccountSubscriber.ts +4 -0
  41. package/src/accounts/types.ts +1 -0
  42. package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +6 -0
  43. package/src/admin.ts +50 -8
  44. package/src/clearingHouse.ts +30 -1
  45. package/src/clearingHouseUser.ts +366 -167
  46. package/src/config.ts +1 -1
  47. package/src/dlob/DLOB.ts +884 -0
  48. package/src/dlob/DLOBNode.ts +163 -0
  49. package/src/dlob/NodeList.ts +185 -0
  50. package/src/idl/clearing_house.json +123 -203
  51. package/src/index.ts +5 -0
  52. package/src/math/market.ts +22 -13
  53. package/src/math/orders.ts +29 -21
  54. package/src/math/position.ts +36 -22
  55. package/src/math/repeg.ts +9 -0
  56. package/src/math/spotBalance.ts +26 -0
  57. package/src/math/spotPosition.ts +42 -1
  58. package/src/types.ts +26 -24
  59. package/src/userMap/userMap.ts +100 -0
  60. package/src/userMap/userStatsMap.ts +110 -0
  61. package/tests/bn/test.ts +2 -3
  62. package/tests/dlob/helpers.ts +322 -0
  63. package/tests/dlob/test.ts +2865 -0
  64. package/my-script/.env +0 -7
  65. package/my-script/getUserStats.ts +0 -106
  66. package/my-script/multiConnections.ts +0 -119
  67. package/my-script/test-regex.ts +0 -11
  68. package/my-script/utils.ts +0 -52
  69. package/src/accounts/bulkAccountLoader.js +0 -249
  70. package/src/accounts/bulkUserStatsSubscription.js +0 -75
  71. package/src/accounts/bulkUserSubscription.js +0 -75
  72. package/src/accounts/fetch.js +0 -92
  73. package/src/accounts/pollingClearingHouseAccountSubscriber.js +0 -465
  74. package/src/accounts/pollingOracleSubscriber.js +0 -156
  75. package/src/accounts/pollingTokenAccountSubscriber.js +0 -141
  76. package/src/accounts/pollingUserAccountSubscriber.js +0 -208
  77. package/src/accounts/pollingUserStatsAccountSubscriber.js +0 -208
  78. package/src/accounts/types.js +0 -28
  79. package/src/accounts/utils.js +0 -7
  80. package/src/accounts/webSocketAccountSubscriber.js +0 -138
  81. package/src/accounts/webSocketClearingHouseAccountSubscriber.js +0 -433
  82. package/src/accounts/webSocketUserAccountSubscriber.js +0 -113
  83. package/src/accounts/webSocketUserStatsAccountSubsriber.js +0 -113
  84. package/src/addresses/pda.js +0 -186
  85. package/src/admin.js +0 -1284
  86. package/src/assert/assert.js +0 -9
  87. package/src/clearingHouse.js +0 -3433
  88. package/src/clearingHouseConfig.js +0 -2
  89. package/src/clearingHouseUser.js +0 -874
  90. package/src/clearingHouseUserConfig.js +0 -2
  91. package/src/clearingHouseUserStats.js +0 -115
  92. package/src/clearingHouseUserStatsConfig.js +0 -2
  93. package/src/config.js +0 -80
  94. package/src/constants/numericConstants.js +0 -48
  95. package/src/constants/perpMarkets.js +0 -42
  96. package/src/constants/spotMarkets.js +0 -51
  97. package/src/events/eventList.js +0 -120
  98. package/src/events/eventSubscriber.js +0 -202
  99. package/src/events/fetchLogs.js +0 -117
  100. package/src/events/pollingLogProvider.js +0 -113
  101. package/src/events/sort.js +0 -41
  102. package/src/events/txEventCache.js +0 -74
  103. package/src/events/types.js +0 -25
  104. package/src/events/webSocketLogProvider.js +0 -76
  105. package/src/factory/bigNum.js +0 -393
  106. package/src/factory/oracleClient.js +0 -20
  107. package/src/index.js +0 -75
  108. package/src/math/amm.js +0 -422
  109. package/src/math/auction.js +0 -42
  110. package/src/math/conversion.js +0 -12
  111. package/src/math/funding.js +0 -296
  112. package/src/math/insurance.js +0 -27
  113. package/src/math/margin.js +0 -77
  114. package/src/math/market.js +0 -105
  115. package/src/math/oracles.js +0 -56
  116. package/src/math/orders.js +0 -153
  117. package/src/math/position.js +0 -172
  118. package/src/math/repeg.js +0 -128
  119. package/src/math/spotBalance.js +0 -176
  120. package/src/math/spotMarket.js +0 -8
  121. package/src/math/spotPosition.js +0 -8
  122. package/src/math/trade.js +0 -260
  123. package/src/math/utils.js +0 -27
  124. package/src/oracles/oracleClientCache.js +0 -20
  125. package/src/oracles/pythClient.js +0 -81
  126. package/src/oracles/quoteAssetOracleClient.js +0 -63
  127. package/src/oracles/switchboardClient.js +0 -101
  128. package/src/oracles/types.js +0 -2
  129. package/src/orderParams.js +0 -28
  130. package/src/serum/serumSubscriber.js +0 -102
  131. package/src/serum/types.js +0 -2
  132. package/src/slot/SlotSubscriber.js +0 -86
  133. package/src/token/index.js +0 -38
  134. package/src/tokenFaucet.js +0 -323
  135. package/src/tx/retryTxSender.js +0 -280
  136. package/src/tx/types.js +0 -2
  137. package/src/tx/utils.js +0 -18
  138. package/src/types.js +0 -216
  139. package/src/userName.js +0 -20
  140. package/src/util/computeUnits.js +0 -62
  141. package/src/util/promiseTimeout.js +0 -14
  142. package/src/util/tps.js +0 -61
  143. package/src/wallet.js +0 -72
package/src/math/trade.js DELETED
@@ -1,260 +0,0 @@
1
- "use strict";
2
- exports.__esModule = true;
3
- exports.calculateTargetPriceTrade = exports.calculateTradeAcquiredAmounts = exports.calculateTradeSlippage = void 0;
4
- var types_1 = require("../types");
5
- var anchor_1 = require("@project-serum/anchor");
6
- var assert_1 = require("../assert/assert");
7
- var numericConstants_1 = require("../constants/numericConstants");
8
- var market_1 = require("./market");
9
- var amm_1 = require("./amm");
10
- var utils_1 = require("./utils");
11
- var types_2 = require("../types");
12
- var MAXPCT = new anchor_1.BN(1000); //percentage units are [0,1000] => [0,1]
13
- /**
14
- * Calculates avg/max slippage (price impact) for candidate trade
15
- * @param direction
16
- * @param amount
17
- * @param market
18
- * @param inputAssetType which asset is being traded
19
- * @param useSpread whether to consider spread with calculating slippage
20
- * @return [pctAvgSlippage, pctMaxSlippage, entryPrice, newPrice]
21
- *
22
- * 'pctAvgSlippage' => the percentage change to entryPrice (average est slippage in execution) : Precision MARK_PRICE_PRECISION
23
- *
24
- * 'pctMaxSlippage' => the percentage change to maxPrice (highest est slippage in execution) : Precision MARK_PRICE_PRECISION
25
- *
26
- * 'entryPrice' => the average price of the trade : Precision MARK_PRICE_PRECISION
27
- *
28
- * 'newPrice' => the price of the asset after the trade : Precision MARK_PRICE_PRECISION
29
- */
30
- function calculateTradeSlippage(direction, amount, market, inputAssetType, oraclePriceData, useSpread) {
31
- if (inputAssetType === void 0) { inputAssetType = 'quote'; }
32
- if (useSpread === void 0) { useSpread = true; }
33
- var oldPrice;
34
- if (useSpread && market.amm.baseSpread > 0) {
35
- if ((0, types_2.isVariant)(direction, 'long')) {
36
- oldPrice = (0, market_1.calculateAskPrice)(market, oraclePriceData);
37
- }
38
- else {
39
- oldPrice = (0, market_1.calculateBidPrice)(market, oraclePriceData);
40
- }
41
- }
42
- else {
43
- oldPrice = (0, market_1.calculateMarkPrice)(market, oraclePriceData);
44
- }
45
- if (amount.eq(numericConstants_1.ZERO)) {
46
- return [numericConstants_1.ZERO, numericConstants_1.ZERO, oldPrice, oldPrice];
47
- }
48
- var _a = calculateTradeAcquiredAmounts(direction, amount, market, inputAssetType, oraclePriceData, useSpread), acquiredBaseReserve = _a[0], acquiredQuoteReserve = _a[1], acquiredQuoteAssetAmount = _a[2];
49
- var entryPrice = acquiredQuoteAssetAmount
50
- .mul(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO)
51
- .mul(numericConstants_1.MARK_PRICE_PRECISION)
52
- .div(acquiredBaseReserve.abs());
53
- var amm;
54
- if (useSpread && market.amm.baseSpread > 0) {
55
- var _b = (0, amm_1.calculateUpdatedAMMSpreadReserves)(market.amm, direction, oraclePriceData), baseAssetReserve = _b.baseAssetReserve, quoteAssetReserve = _b.quoteAssetReserve, sqrtK = _b.sqrtK, newPeg = _b.newPeg;
56
- amm = {
57
- baseAssetReserve: baseAssetReserve,
58
- quoteAssetReserve: quoteAssetReserve,
59
- sqrtK: sqrtK,
60
- pegMultiplier: newPeg
61
- };
62
- }
63
- else {
64
- amm = market.amm;
65
- }
66
- var newPrice = (0, amm_1.calculatePrice)(amm.baseAssetReserve.sub(acquiredBaseReserve), amm.quoteAssetReserve.sub(acquiredQuoteReserve), amm.pegMultiplier);
67
- if (direction == types_1.PositionDirection.SHORT) {
68
- (0, assert_1.assert)(newPrice.lte(oldPrice));
69
- }
70
- else {
71
- (0, assert_1.assert)(oldPrice.lte(newPrice));
72
- }
73
- var pctMaxSlippage = newPrice
74
- .sub(oldPrice)
75
- .mul(numericConstants_1.MARK_PRICE_PRECISION)
76
- .div(oldPrice)
77
- .abs();
78
- var pctAvgSlippage = entryPrice
79
- .sub(oldPrice)
80
- .mul(numericConstants_1.MARK_PRICE_PRECISION)
81
- .div(oldPrice)
82
- .abs();
83
- return [pctAvgSlippage, pctMaxSlippage, entryPrice, newPrice];
84
- }
85
- exports.calculateTradeSlippage = calculateTradeSlippage;
86
- /**
87
- * Calculates acquired amounts for trade executed
88
- * @param direction
89
- * @param amount
90
- * @param market
91
- * @param inputAssetType
92
- * @param useSpread
93
- * @return
94
- * | 'acquiredBase' => positive/negative change in user's base : BN AMM_RESERVE_PRECISION
95
- * | 'acquiredQuote' => positive/negative change in user's quote : BN TODO-PRECISION
96
- */
97
- function calculateTradeAcquiredAmounts(direction, amount, market, inputAssetType, oraclePriceData, useSpread) {
98
- if (inputAssetType === void 0) { inputAssetType = 'quote'; }
99
- if (useSpread === void 0) { useSpread = true; }
100
- if (amount.eq(numericConstants_1.ZERO)) {
101
- return [numericConstants_1.ZERO, numericConstants_1.ZERO, numericConstants_1.ZERO];
102
- }
103
- var swapDirection = (0, amm_1.getSwapDirection)(inputAssetType, direction);
104
- var amm;
105
- if (useSpread && market.amm.baseSpread > 0) {
106
- var _a = (0, amm_1.calculateUpdatedAMMSpreadReserves)(market.amm, direction, oraclePriceData), baseAssetReserve = _a.baseAssetReserve, quoteAssetReserve = _a.quoteAssetReserve, sqrtK = _a.sqrtK, newPeg = _a.newPeg;
107
- amm = {
108
- baseAssetReserve: baseAssetReserve,
109
- quoteAssetReserve: quoteAssetReserve,
110
- sqrtK: sqrtK,
111
- pegMultiplier: newPeg
112
- };
113
- }
114
- else {
115
- amm = market.amm;
116
- }
117
- var _b = (0, amm_1.calculateAmmReservesAfterSwap)(amm, inputAssetType, amount, swapDirection), newQuoteAssetReserve = _b[0], newBaseAssetReserve = _b[1];
118
- var acquiredBase = amm.baseAssetReserve.sub(newBaseAssetReserve);
119
- var acquiredQuote = amm.quoteAssetReserve.sub(newQuoteAssetReserve);
120
- var acquiredQuoteAssetAmount = (0, amm_1.calculateQuoteAssetAmountSwapped)(acquiredQuote.abs(), amm.pegMultiplier, swapDirection);
121
- return [acquiredBase, acquiredQuote, acquiredQuoteAssetAmount];
122
- }
123
- exports.calculateTradeAcquiredAmounts = calculateTradeAcquiredAmounts;
124
- /**
125
- * calculateTargetPriceTrade
126
- * simple function for finding arbitraging trades
127
- * @param market
128
- * @param targetPrice
129
- * @param pct optional default is 100% gap filling, can set smaller.
130
- * @param outputAssetType which asset to trade.
131
- * @param useSpread whether or not to consider the spread when calculating the trade size
132
- * @returns trade direction/size in order to push price to a targetPrice,
133
- *
134
- * [
135
- * direction => direction of trade required, PositionDirection
136
- * tradeSize => size of trade required, TODO-PRECISION
137
- * entryPrice => the entry price for the trade, MARK_PRICE_PRECISION
138
- * targetPrice => the target price MARK_PRICE_PRECISION
139
- * ]
140
- */
141
- function calculateTargetPriceTrade(market, targetPrice, pct, outputAssetType, oraclePriceData, useSpread) {
142
- if (pct === void 0) { pct = MAXPCT; }
143
- if (outputAssetType === void 0) { outputAssetType = 'quote'; }
144
- if (useSpread === void 0) { useSpread = true; }
145
- (0, assert_1.assert)(market.amm.baseAssetReserve.gt(numericConstants_1.ZERO));
146
- (0, assert_1.assert)(targetPrice.gt(numericConstants_1.ZERO));
147
- (0, assert_1.assert)(pct.lte(MAXPCT) && pct.gt(numericConstants_1.ZERO));
148
- var markPriceBefore = (0, market_1.calculateMarkPrice)(market, oraclePriceData);
149
- var bidPriceBefore = (0, market_1.calculateBidPrice)(market, oraclePriceData);
150
- var askPriceBefore = (0, market_1.calculateAskPrice)(market, oraclePriceData);
151
- var direction;
152
- if (targetPrice.gt(markPriceBefore)) {
153
- var priceGap = targetPrice.sub(markPriceBefore);
154
- var priceGapScaled = priceGap.mul(pct).div(MAXPCT);
155
- targetPrice = markPriceBefore.add(priceGapScaled);
156
- direction = types_1.PositionDirection.LONG;
157
- }
158
- else {
159
- var priceGap = markPriceBefore.sub(targetPrice);
160
- var priceGapScaled = priceGap.mul(pct).div(MAXPCT);
161
- targetPrice = markPriceBefore.sub(priceGapScaled);
162
- direction = types_1.PositionDirection.SHORT;
163
- }
164
- var tradeSize;
165
- var baseSize;
166
- var baseAssetReserveBefore;
167
- var quoteAssetReserveBefore;
168
- var peg = market.amm.pegMultiplier;
169
- if (useSpread && market.amm.baseSpread > 0) {
170
- var _a = (0, amm_1.calculateUpdatedAMMSpreadReserves)(market.amm, direction, oraclePriceData), baseAssetReserve = _a.baseAssetReserve, quoteAssetReserve = _a.quoteAssetReserve, newPeg = _a.newPeg;
171
- baseAssetReserveBefore = baseAssetReserve;
172
- quoteAssetReserveBefore = quoteAssetReserve;
173
- peg = newPeg;
174
- }
175
- else {
176
- baseAssetReserveBefore = market.amm.baseAssetReserve;
177
- quoteAssetReserveBefore = market.amm.quoteAssetReserve;
178
- }
179
- var invariant = market.amm.sqrtK.mul(market.amm.sqrtK);
180
- var k = invariant.mul(numericConstants_1.MARK_PRICE_PRECISION);
181
- var baseAssetReserveAfter;
182
- var quoteAssetReserveAfter;
183
- var biasModifier = new anchor_1.BN(1);
184
- var markPriceAfter;
185
- if (useSpread &&
186
- targetPrice.lt(askPriceBefore) &&
187
- targetPrice.gt(bidPriceBefore)) {
188
- // no trade, market is at target
189
- if (markPriceBefore.gt(targetPrice)) {
190
- direction = types_1.PositionDirection.SHORT;
191
- }
192
- else {
193
- direction = types_1.PositionDirection.LONG;
194
- }
195
- tradeSize = numericConstants_1.ZERO;
196
- return [direction, tradeSize, targetPrice, targetPrice];
197
- }
198
- else if (markPriceBefore.gt(targetPrice)) {
199
- // overestimate y2
200
- baseAssetReserveAfter = (0, utils_1.squareRootBN)(k.div(targetPrice).mul(peg).div(numericConstants_1.PEG_PRECISION).sub(biasModifier)).sub(new anchor_1.BN(1));
201
- quoteAssetReserveAfter = k
202
- .div(numericConstants_1.MARK_PRICE_PRECISION)
203
- .div(baseAssetReserveAfter);
204
- markPriceAfter = (0, amm_1.calculatePrice)(baseAssetReserveAfter, quoteAssetReserveAfter, peg);
205
- direction = types_1.PositionDirection.SHORT;
206
- tradeSize = quoteAssetReserveBefore
207
- .sub(quoteAssetReserveAfter)
208
- .mul(peg)
209
- .div(numericConstants_1.PEG_PRECISION)
210
- .div(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO);
211
- baseSize = baseAssetReserveAfter.sub(baseAssetReserveBefore);
212
- }
213
- else if (markPriceBefore.lt(targetPrice)) {
214
- // underestimate y2
215
- baseAssetReserveAfter = (0, utils_1.squareRootBN)(k.div(targetPrice).mul(peg).div(numericConstants_1.PEG_PRECISION).add(biasModifier)).add(new anchor_1.BN(1));
216
- quoteAssetReserveAfter = k
217
- .div(numericConstants_1.MARK_PRICE_PRECISION)
218
- .div(baseAssetReserveAfter);
219
- markPriceAfter = (0, amm_1.calculatePrice)(baseAssetReserveAfter, quoteAssetReserveAfter, peg);
220
- direction = types_1.PositionDirection.LONG;
221
- tradeSize = quoteAssetReserveAfter
222
- .sub(quoteAssetReserveBefore)
223
- .mul(peg)
224
- .div(numericConstants_1.PEG_PRECISION)
225
- .div(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO);
226
- baseSize = baseAssetReserveBefore.sub(baseAssetReserveAfter);
227
- }
228
- else {
229
- // no trade, market is at target
230
- direction = types_1.PositionDirection.LONG;
231
- tradeSize = numericConstants_1.ZERO;
232
- return [direction, tradeSize, targetPrice, targetPrice];
233
- }
234
- var tp1 = targetPrice;
235
- var tp2 = markPriceAfter;
236
- var originalDiff = targetPrice.sub(markPriceBefore);
237
- if (direction == types_1.PositionDirection.SHORT) {
238
- tp1 = markPriceAfter;
239
- tp2 = targetPrice;
240
- originalDiff = markPriceBefore.sub(targetPrice);
241
- }
242
- var entryPrice = tradeSize
243
- .mul(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO)
244
- .mul(numericConstants_1.MARK_PRICE_PRECISION)
245
- .div(baseSize.abs());
246
- (0, assert_1.assert)(tp1.sub(tp2).lte(originalDiff), 'Target Price Calculation incorrect');
247
- (0, assert_1.assert)(tp2.lte(tp1) || tp2.sub(tp1).abs() < 100000, 'Target Price Calculation incorrect' +
248
- tp2.toString() +
249
- '>=' +
250
- tp1.toString() +
251
- 'err: ' +
252
- tp2.sub(tp1).abs().toString());
253
- if (outputAssetType == 'quote') {
254
- return [direction, tradeSize, entryPrice, targetPrice];
255
- }
256
- else {
257
- return [direction, baseSize, entryPrice, targetPrice];
258
- }
259
- }
260
- exports.calculateTargetPriceTrade = calculateTargetPriceTrade;
package/src/math/utils.js DELETED
@@ -1,27 +0,0 @@
1
- "use strict";
2
- exports.__esModule = true;
3
- exports.squareRootBN = void 0;
4
- var __1 = require("../");
5
- var squareRootBN = function (n, closeness) {
6
- if (closeness === void 0) { closeness = new __1.BN(1); }
7
- // Assuming the sqrt of n as n only
8
- var x = n;
9
- // The closed guess will be stored in the root
10
- var root;
11
- // To count the number of iterations
12
- var count = 0;
13
- var TWO = new __1.BN(2);
14
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
15
- while (count < Number.MAX_SAFE_INTEGER) {
16
- count++;
17
- // Calculate more closed x
18
- root = x.add(n.div(x)).div(TWO);
19
- // Check for closeness
20
- if (x.sub(root).abs().lte(closeness))
21
- break;
22
- // Update root
23
- x = root;
24
- }
25
- return root;
26
- };
27
- exports.squareRootBN = squareRootBN;
@@ -1,20 +0,0 @@
1
- "use strict";
2
- exports.__esModule = true;
3
- exports.OracleClientCache = void 0;
4
- var oracleClient_1 = require("../factory/oracleClient");
5
- var OracleClientCache = /** @class */ (function () {
6
- function OracleClientCache() {
7
- this.cache = new Map();
8
- }
9
- OracleClientCache.prototype.get = function (oracleSource, connection) {
10
- var key = Object.keys(oracleSource)[0];
11
- if (this.cache.has(key)) {
12
- return this.cache.get(key);
13
- }
14
- var client = (0, oracleClient_1.getOracleClient)(oracleSource, connection);
15
- this.cache.set(key, client);
16
- return client;
17
- };
18
- return OracleClientCache;
19
- }());
20
- exports.OracleClientCache = OracleClientCache;
@@ -1,81 +0,0 @@
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.convertPythPrice = exports.PythClient = void 0;
40
- var client_1 = require("@pythnetwork/client");
41
- var anchor_1 = require("@project-serum/anchor");
42
- var numericConstants_1 = require("../constants/numericConstants");
43
- var PythClient = /** @class */ (function () {
44
- function PythClient(connection) {
45
- this.connection = connection;
46
- }
47
- PythClient.prototype.getOraclePriceData = function (pricePublicKey) {
48
- return __awaiter(this, void 0, void 0, function () {
49
- var accountInfo;
50
- return __generator(this, function (_a) {
51
- switch (_a.label) {
52
- case 0: return [4 /*yield*/, this.connection.getAccountInfo(pricePublicKey)];
53
- case 1:
54
- accountInfo = _a.sent();
55
- return [2 /*return*/, this.getOraclePriceDataFromBuffer(accountInfo.data)];
56
- }
57
- });
58
- });
59
- };
60
- PythClient.prototype.getOraclePriceDataFromBuffer = function (buffer) {
61
- var priceData = (0, client_1.parsePriceData)(buffer);
62
- return {
63
- price: convertPythPrice(priceData.aggregate.price, priceData.exponent),
64
- slot: new anchor_1.BN(priceData.lastSlot.toString()),
65
- confidence: convertPythPrice(priceData.confidence, priceData.exponent),
66
- twap: convertPythPrice(priceData.twap.value, priceData.exponent),
67
- twapConfidence: convertPythPrice(priceData.twac.value, priceData.exponent),
68
- hasSufficientNumberOfDataPoints: true
69
- };
70
- };
71
- return PythClient;
72
- }());
73
- exports.PythClient = PythClient;
74
- function convertPythPrice(price, exponent) {
75
- exponent = Math.abs(exponent);
76
- var pythPrecision = numericConstants_1.TEN.pow(new anchor_1.BN(exponent).abs());
77
- return new anchor_1.BN(price * Math.pow(10, exponent))
78
- .mul(numericConstants_1.MARK_PRICE_PRECISION)
79
- .div(pythPrecision);
80
- }
81
- exports.convertPythPrice = convertPythPrice;
@@ -1,63 +0,0 @@
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.QuoteAssetOracleClient = exports.QUOTE_ORACLE_PRICE_DATA = void 0;
40
- var anchor_1 = require("@project-serum/anchor");
41
- var numericConstants_1 = require("../constants/numericConstants");
42
- exports.QUOTE_ORACLE_PRICE_DATA = {
43
- price: numericConstants_1.MARK_PRICE_PRECISION,
44
- slot: new anchor_1.BN(0),
45
- confidence: new anchor_1.BN(1),
46
- hasSufficientNumberOfDataPoints: true
47
- };
48
- var QuoteAssetOracleClient = /** @class */ (function () {
49
- function QuoteAssetOracleClient() {
50
- }
51
- QuoteAssetOracleClient.prototype.getOraclePriceData = function (_pricePublicKey) {
52
- return __awaiter(this, void 0, void 0, function () {
53
- return __generator(this, function (_a) {
54
- return [2 /*return*/, Promise.resolve(exports.QUOTE_ORACLE_PRICE_DATA)];
55
- });
56
- });
57
- };
58
- QuoteAssetOracleClient.prototype.getOraclePriceDataFromBuffer = function (_buffer) {
59
- return exports.QUOTE_ORACLE_PRICE_DATA;
60
- };
61
- return QuoteAssetOracleClient;
62
- }());
63
- exports.QuoteAssetOracleClient = QuoteAssetOracleClient;
@@ -1,101 +0,0 @@
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.SwitchboardClient = void 0;
40
- var web3_js_1 = require("@solana/web3.js");
41
- var anchor_1 = require("@project-serum/anchor");
42
- var numericConstants_1 = require("../constants/numericConstants");
43
- var wallet_1 = require("../wallet");
44
- var switchboard_v2_json_1 = require("../idl/switchboard_v2.json");
45
- var program;
46
- var SwitchboardClient = /** @class */ (function () {
47
- function SwitchboardClient(connection) {
48
- this.connection = connection;
49
- }
50
- SwitchboardClient.prototype.getOraclePriceData = function (pricePublicKey) {
51
- return __awaiter(this, void 0, void 0, function () {
52
- var accountInfo;
53
- return __generator(this, function (_a) {
54
- switch (_a.label) {
55
- case 0: return [4 /*yield*/, this.connection.getAccountInfo(pricePublicKey)];
56
- case 1:
57
- accountInfo = _a.sent();
58
- return [2 /*return*/, this.getOraclePriceDataFromBuffer(accountInfo.data)];
59
- }
60
- });
61
- });
62
- };
63
- SwitchboardClient.prototype.getOraclePriceDataFromBuffer = function (buffer) {
64
- var program = this.getProgram();
65
- var aggregatorAccountData = program.account.aggregatorAccountData.coder.accounts.decode('AggregatorAccountData', buffer);
66
- var price = convertSwitchboardDecimal(aggregatorAccountData.latestConfirmedRound.result);
67
- var confidence = convertSwitchboardDecimal(aggregatorAccountData.latestConfirmedRound
68
- .stdDeviation);
69
- var hasSufficientNumberOfDataPoints = aggregatorAccountData.latestConfirmedRound.numSuccess >=
70
- aggregatorAccountData.minOracleResults;
71
- var slot = aggregatorAccountData.latestConfirmedRound.roundOpenSlot;
72
- return {
73
- price: price,
74
- slot: slot,
75
- confidence: confidence,
76
- hasSufficientNumberOfDataPoints: hasSufficientNumberOfDataPoints
77
- };
78
- };
79
- SwitchboardClient.prototype.getProgram = function () {
80
- if (program) {
81
- return program;
82
- }
83
- program = getSwitchboardProgram(this.connection);
84
- return program;
85
- };
86
- return SwitchboardClient;
87
- }());
88
- exports.SwitchboardClient = SwitchboardClient;
89
- function getSwitchboardProgram(connection) {
90
- var DEFAULT_KEYPAIR = web3_js_1.Keypair.fromSeed(new Uint8Array(32).fill(1));
91
- var programId = web3_js_1.PublicKey["default"];
92
- var wallet = new wallet_1.Wallet(DEFAULT_KEYPAIR);
93
- var provider = new anchor_1.AnchorProvider(connection, wallet, {});
94
- return new anchor_1.Program(switchboard_v2_json_1["default"], programId, provider);
95
- }
96
- function convertSwitchboardDecimal(switchboardDecimal) {
97
- var switchboardPrecision = numericConstants_1.TEN.pow(new anchor_1.BN(switchboardDecimal.scale));
98
- return switchboardDecimal.mantissa
99
- .mul(numericConstants_1.MARK_PRICE_PRECISION)
100
- .div(switchboardPrecision);
101
- }
@@ -1,2 +0,0 @@
1
- "use strict";
2
- exports.__esModule = true;
@@ -1,28 +0,0 @@
1
- "use strict";
2
- exports.__esModule = true;
3
- exports.getMarketOrderParams = exports.getTriggerLimitOrderParams = exports.getTriggerMarketOrderParams = exports.getLimitOrderParams = void 0;
4
- var types_1 = require("./types");
5
- function getLimitOrderParams(params) {
6
- return Object.assign({}, params, {
7
- orderType: types_1.OrderType.LIMIT
8
- });
9
- }
10
- exports.getLimitOrderParams = getLimitOrderParams;
11
- function getTriggerMarketOrderParams(params) {
12
- return Object.assign({}, params, {
13
- orderType: types_1.OrderType.TRIGGER_MARKET
14
- });
15
- }
16
- exports.getTriggerMarketOrderParams = getTriggerMarketOrderParams;
17
- function getTriggerLimitOrderParams(params) {
18
- return Object.assign({}, params, {
19
- orderType: types_1.OrderType.TRIGGER_LIMIT
20
- });
21
- }
22
- exports.getTriggerLimitOrderParams = getTriggerLimitOrderParams;
23
- function getMarketOrderParams(params) {
24
- return Object.assign({}, params, {
25
- orderType: types_1.OrderType.MARKET
26
- });
27
- }
28
- exports.getMarketOrderParams = getMarketOrderParams;