@drift-labs/sdk 0.1.36-master.5 → 0.2.0-master.0

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 +7 -6
  2. package/lib/accounts/bulkAccountLoader.js +83 -93
  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 +8 -12
  26. package/lib/admin.js +366 -490
  27. package/lib/clearingHouse.d.ts +84 -103
  28. package/lib/clearingHouse.js +779 -810
  29. package/lib/clearingHouseConfig.d.ts +34 -0
  30. package/lib/clearingHouseConfig.js +2 -0
  31. package/lib/clearingHouseUser.d.ts +19 -18
  32. package/lib/clearingHouseUser.js +157 -115
  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 +16 -0
  42. package/lib/constants/numericConstants.js +22 -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 +112 -0
  61. package/lib/factory/bigNum.js +356 -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 +1739 -2287
  65. package/lib/index.d.ts +13 -4
  66. package/lib/index.js +13 -4
  67. package/lib/math/amm.d.ts +19 -29
  68. package/lib/math/amm.js +129 -179
  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/oracles.d.ts +3 -0
  80. package/lib/math/oracles.js +26 -0
  81. package/lib/math/orders.d.ts +5 -0
  82. package/lib/math/orders.js +31 -1
  83. package/lib/math/position.d.ts +7 -4
  84. package/lib/math/position.js +27 -9
  85. package/lib/math/repeg.d.ts +22 -0
  86. package/lib/math/repeg.js +128 -0
  87. package/lib/math/trade.d.ts +13 -8
  88. package/lib/math/trade.js +85 -22
  89. package/lib/mockUSDCFaucet.js +87 -116
  90. package/lib/oracles/oracleClientCache.d.ts +8 -0
  91. package/lib/oracles/oracleClientCache.js +19 -0
  92. package/lib/oracles/pythClient.d.ts +3 -5
  93. package/lib/oracles/pythClient.js +12 -30
  94. package/lib/oracles/quoteAssetOracleClient.d.ts +9 -0
  95. package/lib/oracles/quoteAssetOracleClient.js +21 -0
  96. package/lib/oracles/switchboardClient.d.ts +3 -5
  97. package/lib/oracles/switchboardClient.js +29 -47
  98. package/lib/oracles/types.d.ts +7 -1
  99. package/lib/orders.d.ts +6 -6
  100. package/lib/orders.js +10 -9
  101. package/lib/slot/SlotSubscriber.d.ts +12 -0
  102. package/lib/slot/SlotSubscriber.js +23 -0
  103. package/lib/tx/retryTxSender.d.ts +2 -2
  104. package/lib/tx/retryTxSender.js +108 -123
  105. package/lib/tx/types.d.ts +5 -1
  106. package/lib/tx/utils.d.ts +1 -1
  107. package/lib/tx/utils.js +11 -2
  108. package/lib/types.d.ts +105 -98
  109. package/lib/types.js +13 -1
  110. package/lib/userName.d.ts +4 -0
  111. package/lib/userName.js +20 -0
  112. package/lib/util/computeUnits.js +10 -21
  113. package/lib/util/tps.js +11 -22
  114. package/lib/wallet.js +7 -20
  115. package/package.json +10 -3
  116. package/src/accounts/bulkAccountLoader.ts +26 -15
  117. package/src/accounts/bulkUserSubscription.ts +1 -45
  118. package/src/accounts/fetch.ts +33 -0
  119. package/src/accounts/pollingClearingHouseAccountSubscriber.ts +249 -214
  120. package/src/accounts/pollingOracleSubscriber.ts +16 -8
  121. package/src/accounts/pollingTokenAccountSubscriber.ts +11 -8
  122. package/src/accounts/pollingUserAccountSubscriber.ts +21 -86
  123. package/src/accounts/types.ts +41 -70
  124. package/src/accounts/webSocketAccountSubscriber.ts +33 -16
  125. package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +190 -240
  126. package/src/accounts/webSocketUserAccountSubscriber.ts +11 -71
  127. package/src/addresses/marketAddresses.ts +18 -0
  128. package/src/addresses/pda.ts +118 -0
  129. package/src/admin.ts +209 -267
  130. package/src/clearingHouse.ts +921 -829
  131. package/src/clearingHouseConfig.ts +37 -0
  132. package/src/clearingHouseUser.ts +280 -127
  133. package/src/clearingHouseUserConfig.ts +18 -0
  134. package/src/config.ts +54 -1
  135. package/src/constants/banks.ts +43 -0
  136. package/src/constants/markets.ts +16 -207
  137. package/src/constants/numericConstants.ts +33 -5
  138. package/src/events/eventList.ts +94 -0
  139. package/src/events/eventSubscriber.ts +194 -0
  140. package/src/events/fetchLogs.ts +80 -0
  141. package/src/events/pollingLogProvider.ts +79 -0
  142. package/src/events/sort.ts +65 -0
  143. package/src/events/txEventCache.ts +74 -0
  144. package/src/events/types.ts +98 -0
  145. package/src/events/webSocketLogProvider.ts +38 -0
  146. package/src/examples/makeTradeExample.ts +20 -11
  147. package/src/factory/bigNum.ts +507 -0
  148. package/src/factory/oracleClient.ts +7 -4
  149. package/src/idl/clearing_house.json +1739 -2287
  150. package/src/index.ts +13 -4
  151. package/src/math/amm.ts +229 -245
  152. package/src/math/auction.ts +39 -0
  153. package/src/math/bankBalance.ts +112 -0
  154. package/src/math/conversion.ts +1 -11
  155. package/src/math/funding.ts +12 -9
  156. package/src/math/market.ts +37 -30
  157. package/src/math/oracles.ts +36 -0
  158. package/src/math/orders.ts +38 -0
  159. package/src/math/position.ts +48 -13
  160. package/src/math/repeg.ts +175 -0
  161. package/src/math/trade.ts +114 -36
  162. package/src/math/utils.js +27 -0
  163. package/src/math/utils.js.map +1 -0
  164. package/src/oracles/oracleClientCache.ts +20 -0
  165. package/src/oracles/pythClient.ts +5 -11
  166. package/src/oracles/quoteAssetOracleClient.ts +25 -0
  167. package/src/oracles/switchboardClient.ts +16 -24
  168. package/src/oracles/types.ts +8 -1
  169. package/src/orders.ts +35 -20
  170. package/src/slot/SlotSubscriber.ts +32 -0
  171. package/src/tx/retryTxSender.ts +6 -4
  172. package/src/tx/types.ts +6 -1
  173. package/src/tx/utils.ts +22 -3
  174. package/src/types.ts +108 -110
  175. package/src/userName.ts +20 -0
  176. package/src/util/computeUnits.js +17 -0
  177. package/src/util/computeUnits.js.map +1 -0
  178. package/tests/bn/test.ts +255 -0
  179. package/tsconfig.json +12 -12
  180. package/lib/addresses.d.ts +0 -9
  181. package/lib/addresses.js +0 -87
  182. package/lib/constants/accounts.d.ts +0 -15
  183. package/lib/constants/accounts.js +0 -18
  184. package/lib/factory/clearingHouse.d.ts +0 -35
  185. package/lib/factory/clearingHouse.js +0 -81
  186. package/lib/factory/clearingHouseUser.d.ts +0 -19
  187. package/lib/factory/clearingHouseUser.js +0 -34
  188. package/lib/math/insuranceFund.d.ts +0 -15
  189. package/lib/math/insuranceFund.js +0 -33
  190. package/lib/tx/defaultTxSender.d.ts +0 -8
  191. package/lib/tx/defaultTxSender.js +0 -12
  192. package/src/addresses.ts +0 -71
  193. package/src/constants/accounts.ts +0 -26
  194. package/src/factory/clearingHouse.ts +0 -173
  195. package/src/factory/clearingHouseUser.ts +0 -73
  196. package/src/math/insuranceFund.ts +0 -29
  197. package/src/tx/defaultTxSender.ts +0 -24
@@ -0,0 +1,507 @@
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(): string {
238
+ const [leftSide, rightSide] = this.printShort().split(BigNum.delim);
239
+
240
+ let formattedLeftSide = leftSide;
241
+
242
+ const isNeg = formattedLeftSide.includes('-');
243
+ if (isNeg) {
244
+ formattedLeftSide = formattedLeftSide.replace('-', '');
245
+ }
246
+
247
+ let index = formattedLeftSide.length - 3;
248
+
249
+ while (index >= 1) {
250
+ const formattedLeftSideArray = formattedLeftSide.split('');
251
+
252
+ formattedLeftSideArray.splice(index, 0, BigNum.spacer);
253
+
254
+ formattedLeftSide = formattedLeftSideArray.join('');
255
+
256
+ index -= 3;
257
+ }
258
+
259
+ return `${isNeg ? '-' : ''}${formattedLeftSide}${
260
+ rightSide ? `${BigNum.delim}${rightSide}` : ''
261
+ }`;
262
+ }
263
+
264
+ /**
265
+ * Print and remove unnecessary trailing zeroes
266
+ * @returns
267
+ */
268
+ public printShort(
269
+ useTradePrecision?: boolean,
270
+ precisionOverride?: number
271
+ ): string {
272
+ const printVal = precisionOverride
273
+ ? this.toPrecision(precisionOverride)
274
+ : useTradePrecision
275
+ ? this.toTradePrecision()
276
+ : this.print();
277
+
278
+ return printVal.replace(/0+$/g, '').replace(/\.$/, '').replace(/,$/, '');
279
+ }
280
+
281
+ public debug() {
282
+ console.log(
283
+ `${this.toString()} | ${this.print()} | ${this.precision.toString()}`
284
+ );
285
+ }
286
+
287
+ /**
288
+ * Pretty print with the specified number of decimal places
289
+ * @param fixedPrecision
290
+ * @returns
291
+ */
292
+ public toFixed(fixedPrecision: number): string {
293
+ const printString = this.print();
294
+
295
+ const [leftSide, rightSide] = printString.split(BigNum.delim);
296
+
297
+ const filledRightSide = [
298
+ ...(rightSide ?? '').slice(0, fixedPrecision),
299
+ ...Array(fixedPrecision).fill('0'),
300
+ ]
301
+ .slice(0, fixedPrecision)
302
+ .join('');
303
+
304
+ return `${leftSide}${BigNum.delim}${filledRightSide}`;
305
+ }
306
+
307
+ /**
308
+ * Pretty print to the specified number of significant figures
309
+ * @param fixedPrecision
310
+ * @returns
311
+ */
312
+ public toPrecision(fixedPrecision: number, trailingZeroes = false): string {
313
+ const printString = this.print();
314
+
315
+ let precisionPrintString = printString.slice(0, fixedPrecision + 1);
316
+
317
+ if (
318
+ !precisionPrintString.includes(BigNum.delim) ||
319
+ precisionPrintString[precisionPrintString.length - 1] === BigNum.delim
320
+ ) {
321
+ precisionPrintString = printString.slice(0, fixedPrecision);
322
+ }
323
+
324
+ const pointsOfPrecision = precisionPrintString.replace(
325
+ BigNum.delim,
326
+ ''
327
+ ).length;
328
+
329
+ if (pointsOfPrecision < fixedPrecision) {
330
+ precisionPrintString = [
331
+ ...precisionPrintString.split(''),
332
+ ...Array(fixedPrecision - pointsOfPrecision).fill('0'),
333
+ ].join('');
334
+ }
335
+
336
+ if (!precisionPrintString.includes(BigNum.delim)) {
337
+ const delimFullStringLocation = printString.indexOf(BigNum.delim);
338
+
339
+ let skipExponent = false;
340
+
341
+ if (delimFullStringLocation === -1) {
342
+ // no decimal, not missing any precision
343
+ skipExponent = true;
344
+ }
345
+
346
+ if (
347
+ precisionPrintString[precisionPrintString.length - 1] === BigNum.delim
348
+ ) {
349
+ // decimal is at end of string, not missing any precision, do nothing
350
+ skipExponent = true;
351
+ }
352
+
353
+ if (printString.indexOf(BigNum.delim) === fixedPrecision) {
354
+ // decimal is at end of string, not missing any precision, do nothing
355
+ skipExponent = true;
356
+ }
357
+
358
+ if (!skipExponent) {
359
+ const exponent = delimFullStringLocation - fixedPrecision;
360
+ if (trailingZeroes) {
361
+ precisionPrintString = `${precisionPrintString}${Array(exponent)
362
+ .fill('0')
363
+ .join('')}`;
364
+ } else {
365
+ precisionPrintString = `${precisionPrintString}e${exponent}`;
366
+ }
367
+ }
368
+ }
369
+
370
+ return precisionPrintString;
371
+ }
372
+
373
+ public toTradePrecision(): string {
374
+ return this.toPrecision(6, true);
375
+ }
376
+
377
+ public toNotional(): string {
378
+ return `${this.lt(BigNum.zero()) ? `-` : ``}$${BigNum.fromPrint(
379
+ this.toFixed(2),
380
+ new BN(2)
381
+ )
382
+ .prettyPrint()
383
+ .replace('-', '')}`;
384
+ }
385
+
386
+ public toMillified(precision = 3): string {
387
+ const stringVal = this.print();
388
+
389
+ const [leftSide] = stringVal.split(BigNum.delim);
390
+
391
+ if (!leftSide) {
392
+ return this.shift(new BN(precision)).toPrecision(precision, true);
393
+ }
394
+
395
+ if (leftSide.length <= 3) {
396
+ return this.shift(new BN(precision)).toPrecision(precision, true);
397
+ }
398
+
399
+ const unitTicks = ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'];
400
+ const unitNumber = Math.floor((leftSide.length - 1) / 3);
401
+ const unit = unitTicks[unitNumber];
402
+
403
+ let leadDigits = leftSide.slice(0, precision);
404
+
405
+ if (leadDigits.length < precision) {
406
+ leadDigits = [
407
+ ...leadDigits.split(''),
408
+ ...Array(precision - leadDigits.length).fill('0'),
409
+ ].join('');
410
+ }
411
+
412
+ const decimalLocation = leftSide.length - 3 * unitNumber;
413
+
414
+ let leadString = '';
415
+
416
+ if (decimalLocation >= precision) {
417
+ leadString = `${leadDigits}`;
418
+ } else {
419
+ leadString = `${leadDigits.slice(0, decimalLocation)}${
420
+ BigNum.delim
421
+ }${leadDigits.slice(decimalLocation)}`;
422
+ }
423
+
424
+ return `${leadString}${unit}`;
425
+ }
426
+
427
+ public toJSON() {
428
+ return {
429
+ val: this.val.toString(),
430
+ precision: this.precision.toString(),
431
+ };
432
+ }
433
+
434
+ public isNeg() {
435
+ return this.lt(ZERO, true);
436
+ }
437
+
438
+ public isPos() {
439
+ return !this.isNeg();
440
+ }
441
+
442
+ /**
443
+ * Get the numerical value of the BigNum. This can break if the BigNum is too large.
444
+ * @returns
445
+ */
446
+ public toNum() {
447
+ return parseFloat(this.print());
448
+ }
449
+
450
+ static fromJSON(json: { val: string; precision: string }) {
451
+ return BigNum.from(new BN(json.val), new BN(json.precision));
452
+ }
453
+
454
+ /**
455
+ * Create a BigNum instance
456
+ * @param val
457
+ * @param precision
458
+ * @returns
459
+ */
460
+ static from(
461
+ val: BN | number | string = ZERO,
462
+ precision?: BN | number | string
463
+ ): BigNum {
464
+ assert(
465
+ new BN(precision).lt(new BN(100)),
466
+ 'Tried to create a bignum with precision higher than 10^100'
467
+ );
468
+ return new BigNum(val, precision);
469
+ }
470
+
471
+ /**
472
+ * Create a BigNum instance from a printed BigNum
473
+ * @param val
474
+ * @param precisionOverride
475
+ * @returns
476
+ */
477
+ static fromPrint(val: string, precisionShift?: BN): BigNum {
478
+ // Handle empty number edge cases
479
+ if (!val) return BigNum.from(ZERO, precisionShift);
480
+ if (!val.replace(BigNum.delim, ''))
481
+ return BigNum.from(ZERO, precisionShift);
482
+
483
+ const [leftSide, rightSide] = val.split(BigNum.delim);
484
+
485
+ const rawBn = new BN(`${leftSide ?? ''}${rightSide ?? ''}`);
486
+
487
+ const rightSideLength = rightSide?.length ?? 0;
488
+
489
+ const totalShift = precisionShift
490
+ ? precisionShift.sub(new BN(rightSideLength))
491
+ : ZERO;
492
+
493
+ return BigNum.from(rawBn, precisionShift).shift(totalShift, true);
494
+ }
495
+
496
+ static max(a: BigNum, b: BigNum): BigNum {
497
+ return a.gt(b) ? a : b;
498
+ }
499
+
500
+ static min(a: BigNum, b: BigNum): BigNum {
501
+ return a.lt(b) ? a : b;
502
+ }
503
+
504
+ static zero(precision?: BN | number): BigNum {
505
+ return BigNum.from(0, precision);
506
+ }
507
+ }
@@ -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}`);