@drift-labs/sdk 0.1.36-master.7 → 0.2.0-master.2

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 (197) hide show
  1. package/lib/accounts/bulkAccountLoader.d.ts +6 -6
  2. package/lib/accounts/bulkAccountLoader.js +81 -95
  3. package/lib/accounts/bulkUserSubscription.js +13 -57
  4. package/lib/accounts/fetch.d.ts +4 -0
  5. package/lib/accounts/fetch.js +18 -0
  6. package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +34 -38
  7. package/lib/accounts/pollingClearingHouseAccountSubscriber.js +224 -224
  8. package/lib/accounts/pollingOracleSubscriber.d.ts +3 -3
  9. package/lib/accounts/pollingOracleSubscriber.js +37 -49
  10. package/lib/accounts/pollingTokenAccountSubscriber.d.ts +3 -3
  11. package/lib/accounts/pollingTokenAccountSubscriber.js +35 -50
  12. package/lib/accounts/pollingUserAccountSubscriber.d.ts +7 -13
  13. package/lib/accounts/pollingUserAccountSubscriber.js +71 -134
  14. package/lib/accounts/types.d.ts +34 -41
  15. package/lib/accounts/webSocketAccountSubscriber.d.ts +6 -4
  16. package/lib/accounts/webSocketAccountSubscriber.js +39 -35
  17. package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +33 -28
  18. package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +153 -185
  19. package/lib/accounts/webSocketUserAccountSubscriber.d.ts +5 -11
  20. package/lib/accounts/webSocketUserAccountSubscriber.js +22 -67
  21. package/lib/addresses/marketAddresses.d.ts +4 -0
  22. package/lib/addresses/marketAddresses.js +15 -0
  23. package/lib/addresses/pda.d.ts +12 -0
  24. package/lib/addresses/pda.js +83 -0
  25. package/lib/admin.d.ts +9 -17
  26. package/lib/admin.js +375 -558
  27. package/lib/clearingHouse.d.ts +86 -109
  28. package/lib/clearingHouse.js +828 -895
  29. package/lib/clearingHouseConfig.d.ts +34 -0
  30. package/lib/clearingHouseConfig.js +2 -0
  31. package/lib/clearingHouseUser.d.ts +19 -22
  32. package/lib/clearingHouseUser.js +155 -141
  33. package/lib/clearingHouseUserConfig.d.ts +14 -0
  34. package/lib/clearingHouseUserConfig.js +2 -0
  35. package/lib/config.d.ts +12 -0
  36. package/lib/config.js +35 -4
  37. package/lib/constants/banks.d.ts +16 -0
  38. package/lib/constants/banks.js +34 -0
  39. package/lib/constants/markets.d.ts +8 -3
  40. package/lib/constants/markets.js +13 -206
  41. package/lib/constants/numericConstants.d.ts +17 -0
  42. package/lib/constants/numericConstants.js +23 -6
  43. package/lib/events/eventList.d.ts +22 -0
  44. package/lib/events/eventList.js +77 -0
  45. package/lib/events/eventSubscriber.d.ts +34 -0
  46. package/lib/events/eventSubscriber.js +126 -0
  47. package/lib/events/fetchLogs.d.ts +13 -0
  48. package/lib/events/fetchLogs.js +39 -0
  49. package/lib/events/pollingLogProvider.d.ts +15 -0
  50. package/lib/events/pollingLogProvider.js +53 -0
  51. package/lib/events/sort.d.ts +2 -0
  52. package/lib/events/sort.js +44 -0
  53. package/lib/events/txEventCache.d.ts +24 -0
  54. package/lib/events/txEventCache.js +71 -0
  55. package/lib/events/types.d.ts +49 -0
  56. package/lib/events/types.js +20 -0
  57. package/lib/events/webSocketLogProvider.d.ts +12 -0
  58. package/lib/events/webSocketLogProvider.js +30 -0
  59. package/lib/examples/makeTradeExample.js +26 -27
  60. package/lib/factory/bigNum.d.ts +118 -0
  61. package/lib/factory/bigNum.js +364 -0
  62. package/lib/factory/oracleClient.d.ts +1 -2
  63. package/lib/factory/oracleClient.js +6 -2
  64. package/lib/idl/clearing_house.json +2287 -2938
  65. package/lib/index.d.ts +12 -4
  66. package/lib/index.js +12 -4
  67. package/lib/math/amm.d.ts +24 -29
  68. package/lib/math/amm.js +239 -178
  69. package/lib/math/auction.d.ts +5 -0
  70. package/lib/math/auction.js +39 -0
  71. package/lib/math/bankBalance.d.ts +9 -0
  72. package/lib/math/bankBalance.js +75 -0
  73. package/lib/math/conversion.d.ts +0 -1
  74. package/lib/math/conversion.js +1 -5
  75. package/lib/math/funding.d.ts +6 -6
  76. package/lib/math/funding.js +158 -175
  77. package/lib/math/market.d.ts +6 -6
  78. package/lib/math/market.js +10 -9
  79. package/lib/math/orders.d.ts +5 -0
  80. package/lib/math/orders.js +31 -1
  81. package/lib/math/position.d.ts +7 -5
  82. package/lib/math/position.js +27 -27
  83. package/lib/math/repeg.d.ts +22 -0
  84. package/lib/math/repeg.js +128 -0
  85. package/lib/math/trade.d.ts +5 -4
  86. package/lib/math/trade.js +29 -21
  87. package/lib/mockUSDCFaucet.js +87 -116
  88. package/lib/oracles/oracleClientCache.d.ts +8 -0
  89. package/lib/oracles/oracleClientCache.js +19 -0
  90. package/lib/oracles/pythClient.d.ts +3 -5
  91. package/lib/oracles/pythClient.js +12 -31
  92. package/lib/oracles/quoteAssetOracleClient.d.ts +9 -0
  93. package/lib/oracles/quoteAssetOracleClient.js +21 -0
  94. package/lib/oracles/switchboardClient.d.ts +3 -5
  95. package/lib/oracles/switchboardClient.js +29 -50
  96. package/lib/oracles/types.d.ts +6 -1
  97. package/lib/orders.d.ts +6 -7
  98. package/lib/orders.js +10 -80
  99. package/lib/slot/SlotSubscriber.d.ts +12 -0
  100. package/lib/slot/SlotSubscriber.js +23 -0
  101. package/lib/tx/retryTxSender.d.ts +2 -2
  102. package/lib/tx/retryTxSender.js +108 -123
  103. package/lib/tx/types.d.ts +5 -1
  104. package/lib/tx/utils.d.ts +1 -1
  105. package/lib/tx/utils.js +11 -2
  106. package/lib/types.d.ts +110 -109
  107. package/lib/types.js +14 -1
  108. package/lib/userName.d.ts +4 -0
  109. package/lib/userName.js +20 -0
  110. package/lib/util/computeUnits.js +10 -21
  111. package/lib/util/tps.js +11 -22
  112. package/lib/wallet.js +7 -20
  113. package/package.json +11 -4
  114. package/src/accounts/bulkAccountLoader.ts +21 -15
  115. package/src/accounts/bulkUserSubscription.ts +1 -45
  116. package/src/accounts/fetch.ts +33 -0
  117. package/src/accounts/pollingClearingHouseAccountSubscriber.ts +249 -214
  118. package/src/accounts/pollingOracleSubscriber.ts +16 -8
  119. package/src/accounts/pollingTokenAccountSubscriber.ts +11 -8
  120. package/src/accounts/pollingUserAccountSubscriber.ts +21 -86
  121. package/src/accounts/types.ts +41 -70
  122. package/src/accounts/webSocketAccountSubscriber.ts +33 -16
  123. package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +190 -240
  124. package/src/accounts/webSocketUserAccountSubscriber.ts +11 -71
  125. package/src/addresses/marketAddresses.ts +18 -0
  126. package/src/addresses/pda.ts +118 -0
  127. package/src/admin.ts +218 -346
  128. package/src/clearingHouse.ts +983 -952
  129. package/src/clearingHouseConfig.ts +37 -0
  130. package/src/clearingHouseUser.ts +275 -185
  131. package/src/clearingHouseUserConfig.ts +18 -0
  132. package/src/config.ts +54 -1
  133. package/src/constants/banks.ts +43 -0
  134. package/src/constants/markets.ts +16 -207
  135. package/src/constants/numericConstants.ts +34 -5
  136. package/src/events/eventList.ts +94 -0
  137. package/src/events/eventSubscriber.ts +194 -0
  138. package/src/events/fetchLogs.ts +80 -0
  139. package/src/events/pollingLogProvider.ts +79 -0
  140. package/src/events/sort.ts +65 -0
  141. package/src/events/txEventCache.ts +74 -0
  142. package/src/events/types.ts +98 -0
  143. package/src/events/webSocketLogProvider.ts +38 -0
  144. package/src/examples/makeTradeExample.ts +20 -11
  145. package/src/factory/bigNum.ts +524 -0
  146. package/src/factory/oracleClient.ts +7 -4
  147. package/src/idl/clearing_house.json +2287 -2938
  148. package/src/index.ts +12 -4
  149. package/src/math/amm.ts +414 -245
  150. package/src/math/auction.ts +39 -0
  151. package/src/math/bankBalance.ts +112 -0
  152. package/src/math/conversion.ts +1 -11
  153. package/src/math/funding.ts +12 -9
  154. package/src/math/market.ts +37 -30
  155. package/src/math/orders.ts +38 -0
  156. package/src/math/position.ts +48 -35
  157. package/src/math/repeg.ts +176 -0
  158. package/src/math/trade.ts +45 -35
  159. package/src/math/utils.js +27 -0
  160. package/src/math/utils.js.map +1 -0
  161. package/src/oracles/oracleClientCache.ts +20 -0
  162. package/src/oracles/pythClient.ts +4 -11
  163. package/src/oracles/quoteAssetOracleClient.ts +25 -0
  164. package/src/oracles/switchboardClient.ts +11 -24
  165. package/src/oracles/types.ts +7 -1
  166. package/src/orders.ts +24 -126
  167. package/src/slot/SlotSubscriber.ts +32 -0
  168. package/src/tx/retryTxSender.ts +6 -4
  169. package/src/tx/types.ts +6 -1
  170. package/src/tx/utils.ts +22 -3
  171. package/src/types.ts +111 -122
  172. package/src/userName.ts +20 -0
  173. package/src/util/computeUnits.js +17 -0
  174. package/src/util/computeUnits.js.map +1 -0
  175. package/tests/bn/test.ts +255 -0
  176. package/tsconfig.json +12 -12
  177. package/lib/addresses.d.ts +0 -10
  178. package/lib/addresses.js +0 -93
  179. package/lib/constants/accounts.d.ts +0 -15
  180. package/lib/constants/accounts.js +0 -18
  181. package/lib/factory/clearingHouse.d.ts +0 -35
  182. package/lib/factory/clearingHouse.js +0 -81
  183. package/lib/factory/clearingHouseUser.d.ts +0 -19
  184. package/lib/factory/clearingHouseUser.js +0 -34
  185. package/lib/math/insuranceFund.d.ts +0 -15
  186. package/lib/math/insuranceFund.js +0 -33
  187. package/lib/settlement.d.ts +0 -4
  188. package/lib/settlement.js +0 -10
  189. package/lib/tx/defaultTxSender.d.ts +0 -8
  190. package/lib/tx/defaultTxSender.js +0 -12
  191. package/src/addresses.ts +0 -82
  192. package/src/constants/accounts.ts +0 -26
  193. package/src/factory/clearingHouse.ts +0 -173
  194. package/src/factory/clearingHouseUser.ts +0 -73
  195. package/src/math/insuranceFund.ts +0 -29
  196. package/src/settlement.ts +0 -9
  197. package/src/tx/defaultTxSender.ts +0 -24
@@ -0,0 +1,524 @@
1
+ import { BN } from '@project-serum/anchor';
2
+ import { assert } from '../assert/assert';
3
+ import { ZERO } from './../constants/numericConstants';
4
+
5
+ export class BigNum {
6
+ val: BN;
7
+ precision: BN;
8
+
9
+ static delim = '.';
10
+ static spacer = ',';
11
+
12
+ constructor(
13
+ val: BN | number | string,
14
+ precisionVal: BN | number | string = new BN(0)
15
+ ) {
16
+ this.val = new BN(val);
17
+ this.precision = new BN(precisionVal);
18
+ }
19
+
20
+ public add(bn: BigNum): BigNum {
21
+ assert(bn.precision.eq(this.precision), 'Adding unequal precisions');
22
+
23
+ return BigNum.from(this.val.add(bn.val), this.precision);
24
+ }
25
+
26
+ public sub(bn: BigNum): BigNum {
27
+ assert(bn.precision.eq(this.precision), 'Subtracting unequal precisions');
28
+
29
+ return BigNum.from(this.val.sub(bn.val), this.precision);
30
+ }
31
+
32
+ public mul(bn: BigNum | BN): BigNum {
33
+ const mulVal = bn instanceof BigNum ? bn : BigNum.from(bn);
34
+
35
+ return BigNum.from(
36
+ this.val.mul(mulVal.val),
37
+ this.precision.add(mulVal.precision)
38
+ );
39
+ }
40
+
41
+ /**
42
+ * Multiplies by another big number then scales the result down by the big number's precision so that we're in the same precision space
43
+ * @param bn
44
+ * @returns
45
+ */
46
+ public scalarMul(bn: BigNum | BN): BigNum {
47
+ if (bn instanceof BN) return BigNum.from(this.val.mul(bn), this.precision);
48
+
49
+ return BigNum.from(
50
+ this.val.mul(bn.val),
51
+ this.precision.add(bn.precision)
52
+ ).shift(bn.precision.neg());
53
+ }
54
+
55
+ public div(bn: BigNum | BN): BigNum {
56
+ if (bn instanceof BN) return BigNum.from(this.val.div(bn), this.precision);
57
+ return BigNum.from(this.val.div(bn.val), this.precision.sub(bn.precision));
58
+ }
59
+
60
+ /**
61
+ * Shift precision up or down
62
+ * @param exponent
63
+ * @param skipAdjustingPrecision
64
+ * @returns
65
+ */
66
+ public shift(exponent: BN | number, skipAdjustingPrecision = false): BigNum {
67
+ const shiftVal = typeof exponent === 'number' ? new BN(exponent) : exponent;
68
+
69
+ return BigNum.from(
70
+ shiftVal.isNeg()
71
+ ? this.val.div(new BN(10).pow(shiftVal))
72
+ : this.val.mul(new BN(10).pow(shiftVal)),
73
+ skipAdjustingPrecision ? this.precision : this.precision.add(shiftVal)
74
+ );
75
+ }
76
+
77
+ /**
78
+ * Shift to a target precision
79
+ * @param targetPrecision
80
+ * @returns
81
+ */
82
+ public shiftTo(targetPrecision: BN): BigNum {
83
+ return this.shift(targetPrecision.sub(this.precision));
84
+ }
85
+
86
+ /**
87
+ * Scale the number by a fraction
88
+ * @param numerator
89
+ * @param denominator
90
+ * @returns
91
+ */
92
+ public scale(numerator: BN | number, denominator: BN | number): BigNum {
93
+ return this.mul(BigNum.from(new BN(numerator))).div(new BN(denominator));
94
+ }
95
+
96
+ public toPercentage(denominator: BigNum, precision: number): string {
97
+ return this.shift(precision)
98
+ .shift(2, true)
99
+ .div(denominator)
100
+ .toPrecision(precision);
101
+ }
102
+
103
+ public gt(bn: BigNum | BN, ignorePrecision?: boolean): boolean {
104
+ const comparisonVal = bn instanceof BigNum ? bn : BigNum.from(bn);
105
+
106
+ if (!ignorePrecision && !comparisonVal.eq(ZERO)) {
107
+ assert(
108
+ comparisonVal.precision.eq(this.precision),
109
+ 'Trying to compare numbers with different precision. Yo can opt to ignore precision using the ignorePrecision parameter'
110
+ );
111
+ }
112
+
113
+ return this.val.gt(comparisonVal.val);
114
+ }
115
+
116
+ public lt(bn: BigNum | BN, ignorePrecision?: boolean): boolean {
117
+ const comparisonVal = bn instanceof BigNum ? bn : BigNum.from(bn);
118
+
119
+ if (!ignorePrecision && !comparisonVal.val.eq(ZERO)) {
120
+ assert(
121
+ comparisonVal.precision.eq(this.precision),
122
+ 'Trying to compare numbers with different precision. Yo can opt to ignore precision using the ignorePrecision parameter'
123
+ );
124
+ }
125
+
126
+ return this.val.lt(comparisonVal.val);
127
+ }
128
+
129
+ public gte(bn: BigNum | BN, ignorePrecision?: boolean): boolean {
130
+ const comparisonVal = bn instanceof BigNum ? bn : BigNum.from(bn);
131
+
132
+ if (!ignorePrecision && !comparisonVal.val.eq(ZERO)) {
133
+ assert(
134
+ comparisonVal.precision.eq(this.precision),
135
+ 'Trying to compare numbers with different precision. Yo can opt to ignore precision using the ignorePrecision parameter'
136
+ );
137
+ }
138
+
139
+ return this.val.gte(comparisonVal.val);
140
+ }
141
+
142
+ public lte(bn: BigNum | BN, ignorePrecision?: boolean): boolean {
143
+ const comparisonVal = bn instanceof BigNum ? bn : BigNum.from(bn);
144
+
145
+ if (!ignorePrecision && !comparisonVal.val.eq(ZERO)) {
146
+ assert(
147
+ comparisonVal.precision.eq(this.precision),
148
+ 'Trying to compare numbers with different precision. Yo can opt to ignore precision using the ignorePrecision parameter'
149
+ );
150
+ }
151
+
152
+ return this.val.lte(comparisonVal.val);
153
+ }
154
+
155
+ public eq(bn: BigNum | BN, ignorePrecision?: boolean): boolean {
156
+ const comparisonVal = bn instanceof BigNum ? bn : BigNum.from(bn);
157
+
158
+ if (!ignorePrecision && !comparisonVal.val.eq(ZERO)) {
159
+ assert(
160
+ comparisonVal.precision.eq(this.precision),
161
+ 'Trying to compare numbers with different precision. Yo can opt to ignore precision using the ignorePrecision parameter'
162
+ );
163
+ }
164
+
165
+ return this.val.eq(comparisonVal.val);
166
+ }
167
+
168
+ public eqZero() {
169
+ return this.val.eq(ZERO);
170
+ }
171
+
172
+ public gtZero() {
173
+ return this.val.gt(ZERO);
174
+ }
175
+
176
+ public ltZero() {
177
+ return this.val.lt(ZERO);
178
+ }
179
+
180
+ public gteZero() {
181
+ return this.val.gte(ZERO);
182
+ }
183
+
184
+ public lteZero() {
185
+ return this.val.lte(ZERO);
186
+ }
187
+
188
+ public abs(): BigNum {
189
+ return new BigNum(this.val.abs(), this.precision);
190
+ }
191
+
192
+ public neg(): BigNum {
193
+ return new BigNum(this.val.neg(), this.precision);
194
+ }
195
+
196
+ public toString = (base?: number | 'hex', length?: number): string =>
197
+ this.val.toString(base, length);
198
+
199
+ /**
200
+ * Pretty print the underlying value in human-readable form. Depends on precision being correct for the output string to be correct
201
+ * @returns
202
+ */
203
+ public print(): string {
204
+ assert(
205
+ this.precision.gte(ZERO),
206
+ 'Tried to print a BN with precision lower than zero'
207
+ );
208
+
209
+ const plainString = this.toString();
210
+ const precisionNum = this.precision.toNumber();
211
+
212
+ // make a string with at least the precisionNum number of zeroes
213
+ let printString = [
214
+ ...Array(this.precision.toNumber()).fill(0),
215
+ ...plainString.split(''),
216
+ ].join('');
217
+
218
+ // inject decimal
219
+ printString =
220
+ printString.substring(0, printString.length - precisionNum) +
221
+ BigNum.delim +
222
+ printString.substring(printString.length - precisionNum);
223
+
224
+ // remove leading zeroes
225
+ printString = printString.replace(/^0+/, '');
226
+
227
+ // add zero if leading delim
228
+ if (printString[0] === BigNum.delim) printString = `0${printString}`;
229
+
230
+ // remove trailing delim
231
+ if (printString[printString.length - 1] === BigNum.delim)
232
+ printString = printString.slice(0, printString.length - 1);
233
+
234
+ return printString;
235
+ }
236
+
237
+ public prettyPrint(
238
+ useTradePrecision?: boolean,
239
+ precisionOverride?: number
240
+ ): string {
241
+ const [leftSide, rightSide] = this.printShort(
242
+ useTradePrecision,
243
+ precisionOverride
244
+ ).split(BigNum.delim);
245
+
246
+ let formattedLeftSide = leftSide;
247
+
248
+ const isNeg = formattedLeftSide.includes('-');
249
+ if (isNeg) {
250
+ formattedLeftSide = formattedLeftSide.replace('-', '');
251
+ }
252
+
253
+ let index = formattedLeftSide.length - 3;
254
+
255
+ while (index >= 1) {
256
+ const formattedLeftSideArray = formattedLeftSide.split('');
257
+
258
+ formattedLeftSideArray.splice(index, 0, BigNum.spacer);
259
+
260
+ formattedLeftSide = formattedLeftSideArray.join('');
261
+
262
+ index -= 3;
263
+ }
264
+
265
+ return `${isNeg ? '-' : ''}${formattedLeftSide}${
266
+ rightSide ? `${BigNum.delim}${rightSide}` : ''
267
+ }`;
268
+ }
269
+
270
+ /**
271
+ * Print and remove unnecessary trailing zeroes
272
+ * @returns
273
+ */
274
+ public printShort(
275
+ useTradePrecision?: boolean,
276
+ precisionOverride?: number
277
+ ): string {
278
+ const printVal = precisionOverride
279
+ ? this.toPrecision(precisionOverride)
280
+ : useTradePrecision
281
+ ? this.toTradePrecision()
282
+ : this.print();
283
+
284
+ return printVal.replace(/0+$/g, '').replace(/\.$/, '').replace(/,$/, '');
285
+ }
286
+
287
+ public debug() {
288
+ console.log(
289
+ `${this.toString()} | ${this.print()} | ${this.precision.toString()}`
290
+ );
291
+ }
292
+
293
+ /**
294
+ * Pretty print with the specified number of decimal places
295
+ * @param fixedPrecision
296
+ * @returns
297
+ */
298
+ public toFixed(fixedPrecision: number): string {
299
+ const printString = this.print();
300
+
301
+ const [leftSide, rightSide] = printString.split(BigNum.delim);
302
+
303
+ const filledRightSide = [
304
+ ...(rightSide ?? '').slice(0, fixedPrecision),
305
+ ...Array(fixedPrecision).fill('0'),
306
+ ]
307
+ .slice(0, fixedPrecision)
308
+ .join('');
309
+
310
+ return `${leftSide}${BigNum.delim}${filledRightSide}`;
311
+ }
312
+
313
+ /**
314
+ * Pretty print to the specified number of significant figures
315
+ * @param fixedPrecision
316
+ * @returns
317
+ */
318
+ public toPrecision(fixedPrecision: number, trailingZeroes = false): string {
319
+ const printString = this.print();
320
+
321
+ let precisionPrintString = printString.slice(0, fixedPrecision + 1);
322
+
323
+ if (
324
+ !precisionPrintString.includes(BigNum.delim) ||
325
+ precisionPrintString[precisionPrintString.length - 1] === BigNum.delim
326
+ ) {
327
+ precisionPrintString = printString.slice(0, fixedPrecision);
328
+ }
329
+
330
+ const pointsOfPrecision = precisionPrintString.replace(
331
+ BigNum.delim,
332
+ ''
333
+ ).length;
334
+
335
+ if (pointsOfPrecision < fixedPrecision) {
336
+ precisionPrintString = [
337
+ ...precisionPrintString.split(''),
338
+ ...Array(fixedPrecision - pointsOfPrecision).fill('0'),
339
+ ].join('');
340
+ }
341
+
342
+ if (!precisionPrintString.includes(BigNum.delim)) {
343
+ const delimFullStringLocation = printString.indexOf(BigNum.delim);
344
+
345
+ let skipExponent = false;
346
+
347
+ if (delimFullStringLocation === -1) {
348
+ // no decimal, not missing any precision
349
+ skipExponent = true;
350
+ }
351
+
352
+ if (
353
+ precisionPrintString[precisionPrintString.length - 1] === BigNum.delim
354
+ ) {
355
+ // decimal is at end of string, not missing any precision, do nothing
356
+ skipExponent = true;
357
+ }
358
+
359
+ if (printString.indexOf(BigNum.delim) === fixedPrecision) {
360
+ // decimal is at end of string, not missing any precision, do nothing
361
+ skipExponent = true;
362
+ }
363
+
364
+ if (!skipExponent) {
365
+ const exponent = delimFullStringLocation - fixedPrecision;
366
+ if (trailingZeroes) {
367
+ precisionPrintString = `${precisionPrintString}${Array(exponent)
368
+ .fill('0')
369
+ .join('')}`;
370
+ } else {
371
+ precisionPrintString = `${precisionPrintString}e${exponent}`;
372
+ }
373
+ }
374
+ }
375
+
376
+ return precisionPrintString;
377
+ }
378
+
379
+ public toTradePrecision(): string {
380
+ return this.toPrecision(6, true);
381
+ }
382
+
383
+ /**
384
+ * Print dollar formatted value. Defaults to fixed decimals two unless a given precision is given.
385
+ * @param useTradePrecision
386
+ * @param precisionOverride
387
+ * @returns
388
+ */
389
+ public toNotional(
390
+ useTradePrecision?: boolean,
391
+ precisionOverride?: number
392
+ ): string {
393
+ const prefix = `${this.lt(BigNum.zero()) ? `-` : ``}$`;
394
+
395
+ const val =
396
+ useTradePrecision || precisionOverride
397
+ ? this.prettyPrint(useTradePrecision, precisionOverride)
398
+ : BigNum.fromPrint(this.toFixed(2), new BN(2)).prettyPrint();
399
+
400
+ return `${prefix}${val.replace('-', '')}`;
401
+ }
402
+
403
+ public toMillified(precision = 3): string {
404
+ const stringVal = this.print();
405
+
406
+ const [leftSide] = stringVal.split(BigNum.delim);
407
+
408
+ if (!leftSide) {
409
+ return this.shift(new BN(precision)).toPrecision(precision, true);
410
+ }
411
+
412
+ if (leftSide.length <= 3) {
413
+ return this.shift(new BN(precision)).toPrecision(precision, true);
414
+ }
415
+
416
+ const unitTicks = ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'];
417
+ const unitNumber = Math.floor((leftSide.length - 1) / 3);
418
+ const unit = unitTicks[unitNumber];
419
+
420
+ let leadDigits = leftSide.slice(0, precision);
421
+
422
+ if (leadDigits.length < precision) {
423
+ leadDigits = [
424
+ ...leadDigits.split(''),
425
+ ...Array(precision - leadDigits.length).fill('0'),
426
+ ].join('');
427
+ }
428
+
429
+ const decimalLocation = leftSide.length - 3 * unitNumber;
430
+
431
+ let leadString = '';
432
+
433
+ if (decimalLocation >= precision) {
434
+ leadString = `${leadDigits}`;
435
+ } else {
436
+ leadString = `${leadDigits.slice(0, decimalLocation)}${
437
+ BigNum.delim
438
+ }${leadDigits.slice(decimalLocation)}`;
439
+ }
440
+
441
+ return `${leadString}${unit}`;
442
+ }
443
+
444
+ public toJSON() {
445
+ return {
446
+ val: this.val.toString(),
447
+ precision: this.precision.toString(),
448
+ };
449
+ }
450
+
451
+ public isNeg() {
452
+ return this.lt(ZERO, true);
453
+ }
454
+
455
+ public isPos() {
456
+ return !this.isNeg();
457
+ }
458
+
459
+ /**
460
+ * Get the numerical value of the BigNum. This can break if the BigNum is too large.
461
+ * @returns
462
+ */
463
+ public toNum() {
464
+ return parseFloat(this.print());
465
+ }
466
+
467
+ static fromJSON(json: { val: string; precision: string }) {
468
+ return BigNum.from(new BN(json.val), new BN(json.precision));
469
+ }
470
+
471
+ /**
472
+ * Create a BigNum instance
473
+ * @param val
474
+ * @param precision
475
+ * @returns
476
+ */
477
+ static from(
478
+ val: BN | number | string = ZERO,
479
+ precision?: BN | number | string
480
+ ): BigNum {
481
+ assert(
482
+ new BN(precision).lt(new BN(100)),
483
+ 'Tried to create a bignum with precision higher than 10^100'
484
+ );
485
+ return new BigNum(val, precision);
486
+ }
487
+
488
+ /**
489
+ * Create a BigNum instance from a printed BigNum
490
+ * @param val
491
+ * @param precisionOverride
492
+ * @returns
493
+ */
494
+ static fromPrint(val: string, precisionShift?: BN): BigNum {
495
+ // Handle empty number edge cases
496
+ if (!val) return BigNum.from(ZERO, precisionShift);
497
+ if (!val.replace(BigNum.delim, ''))
498
+ return BigNum.from(ZERO, precisionShift);
499
+
500
+ const [leftSide, rightSide] = val.split(BigNum.delim);
501
+
502
+ const rawBn = new BN(`${leftSide ?? ''}${rightSide ?? ''}`);
503
+
504
+ const rightSideLength = rightSide?.length ?? 0;
505
+
506
+ const totalShift = precisionShift
507
+ ? precisionShift.sub(new BN(rightSideLength))
508
+ : ZERO;
509
+
510
+ return BigNum.from(rawBn, precisionShift).shift(totalShift, true);
511
+ }
512
+
513
+ static max(a: BigNum, b: BigNum): BigNum {
514
+ return a.gt(b) ? a : b;
515
+ }
516
+
517
+ static min(a: BigNum, b: BigNum): BigNum {
518
+ return a.lt(b) ? a : b;
519
+ }
520
+
521
+ static zero(precision?: BN | number): BigNum {
522
+ return BigNum.from(0, precision);
523
+ }
524
+ }
@@ -1,21 +1,24 @@
1
1
  import { isVariant, OracleSource } from '../types';
2
2
  import { Connection } from '@solana/web3.js';
3
- import { DriftEnv } from '../config';
4
3
  import { OracleClient } from '../oracles/types';
5
4
  import { PythClient } from '../oracles/pythClient';
6
5
  import { SwitchboardClient } from '../oracles/switchboardClient';
6
+ import { QuoteAssetOracleClient } from '../oracles/quoteAssetOracleClient';
7
7
 
8
8
  export function getOracleClient(
9
9
  oracleSource: OracleSource,
10
- connection: Connection,
11
- env: DriftEnv
10
+ connection: Connection
12
11
  ): OracleClient {
13
12
  if (isVariant(oracleSource, 'pyth')) {
14
13
  return new PythClient(connection);
15
14
  }
16
15
 
17
16
  if (isVariant(oracleSource, 'switchboard')) {
18
- return new SwitchboardClient(connection, env);
17
+ return new SwitchboardClient(connection);
18
+ }
19
+
20
+ if (isVariant(oracleSource, 'quoteAsset')) {
21
+ return new QuoteAssetOracleClient();
19
22
  }
20
23
 
21
24
  throw new Error(`Unknown oracle source ${oracleSource}`);