@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,118 @@
1
+ /// <reference types="bn.js" />
2
+ import { BN } from '@project-serum/anchor';
3
+ export declare class BigNum {
4
+ val: BN;
5
+ precision: BN;
6
+ static delim: string;
7
+ static spacer: string;
8
+ constructor(val: BN | number | string, precisionVal?: BN | number | string);
9
+ add(bn: BigNum): BigNum;
10
+ sub(bn: BigNum): BigNum;
11
+ mul(bn: BigNum | BN): BigNum;
12
+ /**
13
+ * 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
14
+ * @param bn
15
+ * @returns
16
+ */
17
+ scalarMul(bn: BigNum | BN): BigNum;
18
+ div(bn: BigNum | BN): BigNum;
19
+ /**
20
+ * Shift precision up or down
21
+ * @param exponent
22
+ * @param skipAdjustingPrecision
23
+ * @returns
24
+ */
25
+ shift(exponent: BN | number, skipAdjustingPrecision?: boolean): BigNum;
26
+ /**
27
+ * Shift to a target precision
28
+ * @param targetPrecision
29
+ * @returns
30
+ */
31
+ shiftTo(targetPrecision: BN): BigNum;
32
+ /**
33
+ * Scale the number by a fraction
34
+ * @param numerator
35
+ * @param denominator
36
+ * @returns
37
+ */
38
+ scale(numerator: BN | number, denominator: BN | number): BigNum;
39
+ toPercentage(denominator: BigNum, precision: number): string;
40
+ gt(bn: BigNum | BN, ignorePrecision?: boolean): boolean;
41
+ lt(bn: BigNum | BN, ignorePrecision?: boolean): boolean;
42
+ gte(bn: BigNum | BN, ignorePrecision?: boolean): boolean;
43
+ lte(bn: BigNum | BN, ignorePrecision?: boolean): boolean;
44
+ eq(bn: BigNum | BN, ignorePrecision?: boolean): boolean;
45
+ eqZero(): boolean;
46
+ gtZero(): boolean;
47
+ ltZero(): boolean;
48
+ gteZero(): boolean;
49
+ lteZero(): boolean;
50
+ abs(): BigNum;
51
+ neg(): BigNum;
52
+ toString: (base?: number | 'hex', length?: number) => string;
53
+ /**
54
+ * Pretty print the underlying value in human-readable form. Depends on precision being correct for the output string to be correct
55
+ * @returns
56
+ */
57
+ print(): string;
58
+ prettyPrint(useTradePrecision?: boolean, precisionOverride?: number): string;
59
+ /**
60
+ * Print and remove unnecessary trailing zeroes
61
+ * @returns
62
+ */
63
+ printShort(useTradePrecision?: boolean, precisionOverride?: number): string;
64
+ debug(): void;
65
+ /**
66
+ * Pretty print with the specified number of decimal places
67
+ * @param fixedPrecision
68
+ * @returns
69
+ */
70
+ toFixed(fixedPrecision: number): string;
71
+ /**
72
+ * Pretty print to the specified number of significant figures
73
+ * @param fixedPrecision
74
+ * @returns
75
+ */
76
+ toPrecision(fixedPrecision: number, trailingZeroes?: boolean): string;
77
+ toTradePrecision(): string;
78
+ /**
79
+ * Print dollar formatted value. Defaults to fixed decimals two unless a given precision is given.
80
+ * @param useTradePrecision
81
+ * @param precisionOverride
82
+ * @returns
83
+ */
84
+ toNotional(useTradePrecision?: boolean, precisionOverride?: number): string;
85
+ toMillified(precision?: number): string;
86
+ toJSON(): {
87
+ val: string;
88
+ precision: string;
89
+ };
90
+ isNeg(): boolean;
91
+ isPos(): boolean;
92
+ /**
93
+ * Get the numerical value of the BigNum. This can break if the BigNum is too large.
94
+ * @returns
95
+ */
96
+ toNum(): number;
97
+ static fromJSON(json: {
98
+ val: string;
99
+ precision: string;
100
+ }): BigNum;
101
+ /**
102
+ * Create a BigNum instance
103
+ * @param val
104
+ * @param precision
105
+ * @returns
106
+ */
107
+ static from(val?: BN | number | string, precision?: BN | number | string): BigNum;
108
+ /**
109
+ * Create a BigNum instance from a printed BigNum
110
+ * @param val
111
+ * @param precisionOverride
112
+ * @returns
113
+ */
114
+ static fromPrint(val: string, precisionShift?: BN): BigNum;
115
+ static max(a: BigNum, b: BigNum): BigNum;
116
+ static min(a: BigNum, b: BigNum): BigNum;
117
+ static zero(precision?: BN | number): BigNum;
118
+ }
@@ -0,0 +1,364 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BigNum = void 0;
4
+ const anchor_1 = require("@project-serum/anchor");
5
+ const assert_1 = require("../assert/assert");
6
+ const numericConstants_1 = require("./../constants/numericConstants");
7
+ class BigNum {
8
+ constructor(val, precisionVal = new anchor_1.BN(0)) {
9
+ this.toString = (base, length) => this.val.toString(base, length);
10
+ this.val = new anchor_1.BN(val);
11
+ this.precision = new anchor_1.BN(precisionVal);
12
+ }
13
+ add(bn) {
14
+ (0, assert_1.assert)(bn.precision.eq(this.precision), 'Adding unequal precisions');
15
+ return BigNum.from(this.val.add(bn.val), this.precision);
16
+ }
17
+ sub(bn) {
18
+ (0, assert_1.assert)(bn.precision.eq(this.precision), 'Subtracting unequal precisions');
19
+ return BigNum.from(this.val.sub(bn.val), this.precision);
20
+ }
21
+ mul(bn) {
22
+ const mulVal = bn instanceof BigNum ? bn : BigNum.from(bn);
23
+ return BigNum.from(this.val.mul(mulVal.val), this.precision.add(mulVal.precision));
24
+ }
25
+ /**
26
+ * 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
27
+ * @param bn
28
+ * @returns
29
+ */
30
+ scalarMul(bn) {
31
+ if (bn instanceof anchor_1.BN)
32
+ return BigNum.from(this.val.mul(bn), this.precision);
33
+ return BigNum.from(this.val.mul(bn.val), this.precision.add(bn.precision)).shift(bn.precision.neg());
34
+ }
35
+ div(bn) {
36
+ if (bn instanceof anchor_1.BN)
37
+ return BigNum.from(this.val.div(bn), this.precision);
38
+ return BigNum.from(this.val.div(bn.val), this.precision.sub(bn.precision));
39
+ }
40
+ /**
41
+ * Shift precision up or down
42
+ * @param exponent
43
+ * @param skipAdjustingPrecision
44
+ * @returns
45
+ */
46
+ shift(exponent, skipAdjustingPrecision = false) {
47
+ const shiftVal = typeof exponent === 'number' ? new anchor_1.BN(exponent) : exponent;
48
+ return BigNum.from(shiftVal.isNeg()
49
+ ? this.val.div(new anchor_1.BN(10).pow(shiftVal))
50
+ : this.val.mul(new anchor_1.BN(10).pow(shiftVal)), skipAdjustingPrecision ? this.precision : this.precision.add(shiftVal));
51
+ }
52
+ /**
53
+ * Shift to a target precision
54
+ * @param targetPrecision
55
+ * @returns
56
+ */
57
+ shiftTo(targetPrecision) {
58
+ return this.shift(targetPrecision.sub(this.precision));
59
+ }
60
+ /**
61
+ * Scale the number by a fraction
62
+ * @param numerator
63
+ * @param denominator
64
+ * @returns
65
+ */
66
+ scale(numerator, denominator) {
67
+ return this.mul(BigNum.from(new anchor_1.BN(numerator))).div(new anchor_1.BN(denominator));
68
+ }
69
+ toPercentage(denominator, precision) {
70
+ return this.shift(precision)
71
+ .shift(2, true)
72
+ .div(denominator)
73
+ .toPrecision(precision);
74
+ }
75
+ gt(bn, ignorePrecision) {
76
+ const comparisonVal = bn instanceof BigNum ? bn : BigNum.from(bn);
77
+ if (!ignorePrecision && !comparisonVal.eq(numericConstants_1.ZERO)) {
78
+ (0, assert_1.assert)(comparisonVal.precision.eq(this.precision), 'Trying to compare numbers with different precision. Yo can opt to ignore precision using the ignorePrecision parameter');
79
+ }
80
+ return this.val.gt(comparisonVal.val);
81
+ }
82
+ lt(bn, ignorePrecision) {
83
+ const comparisonVal = bn instanceof BigNum ? bn : BigNum.from(bn);
84
+ if (!ignorePrecision && !comparisonVal.val.eq(numericConstants_1.ZERO)) {
85
+ (0, assert_1.assert)(comparisonVal.precision.eq(this.precision), 'Trying to compare numbers with different precision. Yo can opt to ignore precision using the ignorePrecision parameter');
86
+ }
87
+ return this.val.lt(comparisonVal.val);
88
+ }
89
+ gte(bn, ignorePrecision) {
90
+ const comparisonVal = bn instanceof BigNum ? bn : BigNum.from(bn);
91
+ if (!ignorePrecision && !comparisonVal.val.eq(numericConstants_1.ZERO)) {
92
+ (0, assert_1.assert)(comparisonVal.precision.eq(this.precision), 'Trying to compare numbers with different precision. Yo can opt to ignore precision using the ignorePrecision parameter');
93
+ }
94
+ return this.val.gte(comparisonVal.val);
95
+ }
96
+ lte(bn, ignorePrecision) {
97
+ const comparisonVal = bn instanceof BigNum ? bn : BigNum.from(bn);
98
+ if (!ignorePrecision && !comparisonVal.val.eq(numericConstants_1.ZERO)) {
99
+ (0, assert_1.assert)(comparisonVal.precision.eq(this.precision), 'Trying to compare numbers with different precision. Yo can opt to ignore precision using the ignorePrecision parameter');
100
+ }
101
+ return this.val.lte(comparisonVal.val);
102
+ }
103
+ eq(bn, ignorePrecision) {
104
+ const comparisonVal = bn instanceof BigNum ? bn : BigNum.from(bn);
105
+ if (!ignorePrecision && !comparisonVal.val.eq(numericConstants_1.ZERO)) {
106
+ (0, assert_1.assert)(comparisonVal.precision.eq(this.precision), 'Trying to compare numbers with different precision. Yo can opt to ignore precision using the ignorePrecision parameter');
107
+ }
108
+ return this.val.eq(comparisonVal.val);
109
+ }
110
+ eqZero() {
111
+ return this.val.eq(numericConstants_1.ZERO);
112
+ }
113
+ gtZero() {
114
+ return this.val.gt(numericConstants_1.ZERO);
115
+ }
116
+ ltZero() {
117
+ return this.val.lt(numericConstants_1.ZERO);
118
+ }
119
+ gteZero() {
120
+ return this.val.gte(numericConstants_1.ZERO);
121
+ }
122
+ lteZero() {
123
+ return this.val.lte(numericConstants_1.ZERO);
124
+ }
125
+ abs() {
126
+ return new BigNum(this.val.abs(), this.precision);
127
+ }
128
+ neg() {
129
+ return new BigNum(this.val.neg(), this.precision);
130
+ }
131
+ /**
132
+ * Pretty print the underlying value in human-readable form. Depends on precision being correct for the output string to be correct
133
+ * @returns
134
+ */
135
+ print() {
136
+ (0, assert_1.assert)(this.precision.gte(numericConstants_1.ZERO), 'Tried to print a BN with precision lower than zero');
137
+ const plainString = this.toString();
138
+ const precisionNum = this.precision.toNumber();
139
+ // make a string with at least the precisionNum number of zeroes
140
+ let printString = [
141
+ ...Array(this.precision.toNumber()).fill(0),
142
+ ...plainString.split(''),
143
+ ].join('');
144
+ // inject decimal
145
+ printString =
146
+ printString.substring(0, printString.length - precisionNum) +
147
+ BigNum.delim +
148
+ printString.substring(printString.length - precisionNum);
149
+ // remove leading zeroes
150
+ printString = printString.replace(/^0+/, '');
151
+ // add zero if leading delim
152
+ if (printString[0] === BigNum.delim)
153
+ printString = `0${printString}`;
154
+ // remove trailing delim
155
+ if (printString[printString.length - 1] === BigNum.delim)
156
+ printString = printString.slice(0, printString.length - 1);
157
+ return printString;
158
+ }
159
+ prettyPrint(useTradePrecision, precisionOverride) {
160
+ const [leftSide, rightSide] = this.printShort(useTradePrecision, precisionOverride).split(BigNum.delim);
161
+ let formattedLeftSide = leftSide;
162
+ const isNeg = formattedLeftSide.includes('-');
163
+ if (isNeg) {
164
+ formattedLeftSide = formattedLeftSide.replace('-', '');
165
+ }
166
+ let index = formattedLeftSide.length - 3;
167
+ while (index >= 1) {
168
+ const formattedLeftSideArray = formattedLeftSide.split('');
169
+ formattedLeftSideArray.splice(index, 0, BigNum.spacer);
170
+ formattedLeftSide = formattedLeftSideArray.join('');
171
+ index -= 3;
172
+ }
173
+ return `${isNeg ? '-' : ''}${formattedLeftSide}${rightSide ? `${BigNum.delim}${rightSide}` : ''}`;
174
+ }
175
+ /**
176
+ * Print and remove unnecessary trailing zeroes
177
+ * @returns
178
+ */
179
+ printShort(useTradePrecision, precisionOverride) {
180
+ const printVal = precisionOverride
181
+ ? this.toPrecision(precisionOverride)
182
+ : useTradePrecision
183
+ ? this.toTradePrecision()
184
+ : this.print();
185
+ return printVal.replace(/0+$/g, '').replace(/\.$/, '').replace(/,$/, '');
186
+ }
187
+ debug() {
188
+ console.log(`${this.toString()} | ${this.print()} | ${this.precision.toString()}`);
189
+ }
190
+ /**
191
+ * Pretty print with the specified number of decimal places
192
+ * @param fixedPrecision
193
+ * @returns
194
+ */
195
+ toFixed(fixedPrecision) {
196
+ const printString = this.print();
197
+ const [leftSide, rightSide] = printString.split(BigNum.delim);
198
+ const filledRightSide = [
199
+ ...(rightSide !== null && rightSide !== void 0 ? rightSide : '').slice(0, fixedPrecision),
200
+ ...Array(fixedPrecision).fill('0'),
201
+ ]
202
+ .slice(0, fixedPrecision)
203
+ .join('');
204
+ return `${leftSide}${BigNum.delim}${filledRightSide}`;
205
+ }
206
+ /**
207
+ * Pretty print to the specified number of significant figures
208
+ * @param fixedPrecision
209
+ * @returns
210
+ */
211
+ toPrecision(fixedPrecision, trailingZeroes = false) {
212
+ const printString = this.print();
213
+ let precisionPrintString = printString.slice(0, fixedPrecision + 1);
214
+ if (!precisionPrintString.includes(BigNum.delim) ||
215
+ precisionPrintString[precisionPrintString.length - 1] === BigNum.delim) {
216
+ precisionPrintString = printString.slice(0, fixedPrecision);
217
+ }
218
+ const pointsOfPrecision = precisionPrintString.replace(BigNum.delim, '').length;
219
+ if (pointsOfPrecision < fixedPrecision) {
220
+ precisionPrintString = [
221
+ ...precisionPrintString.split(''),
222
+ ...Array(fixedPrecision - pointsOfPrecision).fill('0'),
223
+ ].join('');
224
+ }
225
+ if (!precisionPrintString.includes(BigNum.delim)) {
226
+ const delimFullStringLocation = printString.indexOf(BigNum.delim);
227
+ let skipExponent = false;
228
+ if (delimFullStringLocation === -1) {
229
+ // no decimal, not missing any precision
230
+ skipExponent = true;
231
+ }
232
+ if (precisionPrintString[precisionPrintString.length - 1] === BigNum.delim) {
233
+ // decimal is at end of string, not missing any precision, do nothing
234
+ skipExponent = true;
235
+ }
236
+ if (printString.indexOf(BigNum.delim) === fixedPrecision) {
237
+ // decimal is at end of string, not missing any precision, do nothing
238
+ skipExponent = true;
239
+ }
240
+ if (!skipExponent) {
241
+ const exponent = delimFullStringLocation - fixedPrecision;
242
+ if (trailingZeroes) {
243
+ precisionPrintString = `${precisionPrintString}${Array(exponent)
244
+ .fill('0')
245
+ .join('')}`;
246
+ }
247
+ else {
248
+ precisionPrintString = `${precisionPrintString}e${exponent}`;
249
+ }
250
+ }
251
+ }
252
+ return precisionPrintString;
253
+ }
254
+ toTradePrecision() {
255
+ return this.toPrecision(6, true);
256
+ }
257
+ /**
258
+ * Print dollar formatted value. Defaults to fixed decimals two unless a given precision is given.
259
+ * @param useTradePrecision
260
+ * @param precisionOverride
261
+ * @returns
262
+ */
263
+ toNotional(useTradePrecision, precisionOverride) {
264
+ const prefix = `${this.lt(BigNum.zero()) ? `-` : ``}$`;
265
+ const val = useTradePrecision || precisionOverride
266
+ ? this.prettyPrint(useTradePrecision, precisionOverride)
267
+ : BigNum.fromPrint(this.toFixed(2), new anchor_1.BN(2)).prettyPrint();
268
+ return `${prefix}${val.replace('-', '')}`;
269
+ }
270
+ toMillified(precision = 3) {
271
+ const stringVal = this.print();
272
+ const [leftSide] = stringVal.split(BigNum.delim);
273
+ if (!leftSide) {
274
+ return this.shift(new anchor_1.BN(precision)).toPrecision(precision, true);
275
+ }
276
+ if (leftSide.length <= 3) {
277
+ return this.shift(new anchor_1.BN(precision)).toPrecision(precision, true);
278
+ }
279
+ const unitTicks = ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'];
280
+ const unitNumber = Math.floor((leftSide.length - 1) / 3);
281
+ const unit = unitTicks[unitNumber];
282
+ let leadDigits = leftSide.slice(0, precision);
283
+ if (leadDigits.length < precision) {
284
+ leadDigits = [
285
+ ...leadDigits.split(''),
286
+ ...Array(precision - leadDigits.length).fill('0'),
287
+ ].join('');
288
+ }
289
+ const decimalLocation = leftSide.length - 3 * unitNumber;
290
+ let leadString = '';
291
+ if (decimalLocation >= precision) {
292
+ leadString = `${leadDigits}`;
293
+ }
294
+ else {
295
+ leadString = `${leadDigits.slice(0, decimalLocation)}${BigNum.delim}${leadDigits.slice(decimalLocation)}`;
296
+ }
297
+ return `${leadString}${unit}`;
298
+ }
299
+ toJSON() {
300
+ return {
301
+ val: this.val.toString(),
302
+ precision: this.precision.toString(),
303
+ };
304
+ }
305
+ isNeg() {
306
+ return this.lt(numericConstants_1.ZERO, true);
307
+ }
308
+ isPos() {
309
+ return !this.isNeg();
310
+ }
311
+ /**
312
+ * Get the numerical value of the BigNum. This can break if the BigNum is too large.
313
+ * @returns
314
+ */
315
+ toNum() {
316
+ return parseFloat(this.print());
317
+ }
318
+ static fromJSON(json) {
319
+ return BigNum.from(new anchor_1.BN(json.val), new anchor_1.BN(json.precision));
320
+ }
321
+ /**
322
+ * Create a BigNum instance
323
+ * @param val
324
+ * @param precision
325
+ * @returns
326
+ */
327
+ static from(val = numericConstants_1.ZERO, precision) {
328
+ (0, assert_1.assert)(new anchor_1.BN(precision).lt(new anchor_1.BN(100)), 'Tried to create a bignum with precision higher than 10^100');
329
+ return new BigNum(val, precision);
330
+ }
331
+ /**
332
+ * Create a BigNum instance from a printed BigNum
333
+ * @param val
334
+ * @param precisionOverride
335
+ * @returns
336
+ */
337
+ static fromPrint(val, precisionShift) {
338
+ var _a;
339
+ // Handle empty number edge cases
340
+ if (!val)
341
+ return BigNum.from(numericConstants_1.ZERO, precisionShift);
342
+ if (!val.replace(BigNum.delim, ''))
343
+ return BigNum.from(numericConstants_1.ZERO, precisionShift);
344
+ const [leftSide, rightSide] = val.split(BigNum.delim);
345
+ const rawBn = new anchor_1.BN(`${leftSide !== null && leftSide !== void 0 ? leftSide : ''}${rightSide !== null && rightSide !== void 0 ? rightSide : ''}`);
346
+ const rightSideLength = (_a = rightSide === null || rightSide === void 0 ? void 0 : rightSide.length) !== null && _a !== void 0 ? _a : 0;
347
+ const totalShift = precisionShift
348
+ ? precisionShift.sub(new anchor_1.BN(rightSideLength))
349
+ : numericConstants_1.ZERO;
350
+ return BigNum.from(rawBn, precisionShift).shift(totalShift, true);
351
+ }
352
+ static max(a, b) {
353
+ return a.gt(b) ? a : b;
354
+ }
355
+ static min(a, b) {
356
+ return a.lt(b) ? a : b;
357
+ }
358
+ static zero(precision) {
359
+ return BigNum.from(0, precision);
360
+ }
361
+ }
362
+ exports.BigNum = BigNum;
363
+ BigNum.delim = '.';
364
+ BigNum.spacer = ',';
@@ -1,5 +1,4 @@
1
1
  import { OracleSource } from '../types';
2
2
  import { Connection } from '@solana/web3.js';
3
- import { DriftEnv } from '../config';
4
3
  import { OracleClient } from '../oracles/types';
5
- export declare function getOracleClient(oracleSource: OracleSource, connection: Connection, env: DriftEnv): OracleClient;
4
+ export declare function getOracleClient(oracleSource: OracleSource, connection: Connection): OracleClient;
@@ -4,12 +4,16 @@ exports.getOracleClient = void 0;
4
4
  const types_1 = require("../types");
5
5
  const pythClient_1 = require("../oracles/pythClient");
6
6
  const switchboardClient_1 = require("../oracles/switchboardClient");
7
- function getOracleClient(oracleSource, connection, env) {
7
+ const quoteAssetOracleClient_1 = require("../oracles/quoteAssetOracleClient");
8
+ function getOracleClient(oracleSource, connection) {
8
9
  if ((0, types_1.isVariant)(oracleSource, 'pyth')) {
9
10
  return new pythClient_1.PythClient(connection);
10
11
  }
11
12
  if ((0, types_1.isVariant)(oracleSource, 'switchboard')) {
12
- return new switchboardClient_1.SwitchboardClient(connection, env);
13
+ return new switchboardClient_1.SwitchboardClient(connection);
14
+ }
15
+ if ((0, types_1.isVariant)(oracleSource, 'quoteAsset')) {
16
+ return new quoteAssetOracleClient_1.QuoteAssetOracleClient();
13
17
  }
14
18
  throw new Error(`Unknown oracle source ${oracleSource}`);
15
19
  }