@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,112 @@
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(): 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
+ toNotional(): string;
79
+ toMillified(precision?: number): string;
80
+ toJSON(): {
81
+ val: string;
82
+ precision: string;
83
+ };
84
+ isNeg(): boolean;
85
+ isPos(): boolean;
86
+ /**
87
+ * Get the numerical value of the BigNum. This can break if the BigNum is too large.
88
+ * @returns
89
+ */
90
+ toNum(): number;
91
+ static fromJSON(json: {
92
+ val: string;
93
+ precision: string;
94
+ }): BigNum;
95
+ /**
96
+ * Create a BigNum instance
97
+ * @param val
98
+ * @param precision
99
+ * @returns
100
+ */
101
+ static from(val?: BN | number | string, precision?: BN | number | string): BigNum;
102
+ /**
103
+ * Create a BigNum instance from a printed BigNum
104
+ * @param val
105
+ * @param precisionOverride
106
+ * @returns
107
+ */
108
+ static fromPrint(val: string, precisionShift?: BN): BigNum;
109
+ static max(a: BigNum, b: BigNum): BigNum;
110
+ static min(a: BigNum, b: BigNum): BigNum;
111
+ static zero(precision?: BN | number): BigNum;
112
+ }
@@ -0,0 +1,356 @@
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() {
160
+ const [leftSide, rightSide] = this.printShort().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
+ toNotional() {
258
+ return `${this.lt(BigNum.zero()) ? `-` : ``}$${BigNum.fromPrint(this.toFixed(2), new anchor_1.BN(2))
259
+ .prettyPrint()
260
+ .replace('-', '')}`;
261
+ }
262
+ toMillified(precision = 3) {
263
+ const stringVal = this.print();
264
+ const [leftSide] = stringVal.split(BigNum.delim);
265
+ if (!leftSide) {
266
+ return this.shift(new anchor_1.BN(precision)).toPrecision(precision, true);
267
+ }
268
+ if (leftSide.length <= 3) {
269
+ return this.shift(new anchor_1.BN(precision)).toPrecision(precision, true);
270
+ }
271
+ const unitTicks = ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'];
272
+ const unitNumber = Math.floor((leftSide.length - 1) / 3);
273
+ const unit = unitTicks[unitNumber];
274
+ let leadDigits = leftSide.slice(0, precision);
275
+ if (leadDigits.length < precision) {
276
+ leadDigits = [
277
+ ...leadDigits.split(''),
278
+ ...Array(precision - leadDigits.length).fill('0'),
279
+ ].join('');
280
+ }
281
+ const decimalLocation = leftSide.length - 3 * unitNumber;
282
+ let leadString = '';
283
+ if (decimalLocation >= precision) {
284
+ leadString = `${leadDigits}`;
285
+ }
286
+ else {
287
+ leadString = `${leadDigits.slice(0, decimalLocation)}${BigNum.delim}${leadDigits.slice(decimalLocation)}`;
288
+ }
289
+ return `${leadString}${unit}`;
290
+ }
291
+ toJSON() {
292
+ return {
293
+ val: this.val.toString(),
294
+ precision: this.precision.toString(),
295
+ };
296
+ }
297
+ isNeg() {
298
+ return this.lt(numericConstants_1.ZERO, true);
299
+ }
300
+ isPos() {
301
+ return !this.isNeg();
302
+ }
303
+ /**
304
+ * Get the numerical value of the BigNum. This can break if the BigNum is too large.
305
+ * @returns
306
+ */
307
+ toNum() {
308
+ return parseFloat(this.print());
309
+ }
310
+ static fromJSON(json) {
311
+ return BigNum.from(new anchor_1.BN(json.val), new anchor_1.BN(json.precision));
312
+ }
313
+ /**
314
+ * Create a BigNum instance
315
+ * @param val
316
+ * @param precision
317
+ * @returns
318
+ */
319
+ static from(val = numericConstants_1.ZERO, precision) {
320
+ (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');
321
+ return new BigNum(val, precision);
322
+ }
323
+ /**
324
+ * Create a BigNum instance from a printed BigNum
325
+ * @param val
326
+ * @param precisionOverride
327
+ * @returns
328
+ */
329
+ static fromPrint(val, precisionShift) {
330
+ var _a;
331
+ // Handle empty number edge cases
332
+ if (!val)
333
+ return BigNum.from(numericConstants_1.ZERO, precisionShift);
334
+ if (!val.replace(BigNum.delim, ''))
335
+ return BigNum.from(numericConstants_1.ZERO, precisionShift);
336
+ const [leftSide, rightSide] = val.split(BigNum.delim);
337
+ const rawBn = new anchor_1.BN(`${leftSide !== null && leftSide !== void 0 ? leftSide : ''}${rightSide !== null && rightSide !== void 0 ? rightSide : ''}`);
338
+ const rightSideLength = (_a = rightSide === null || rightSide === void 0 ? void 0 : rightSide.length) !== null && _a !== void 0 ? _a : 0;
339
+ const totalShift = precisionShift
340
+ ? precisionShift.sub(new anchor_1.BN(rightSideLength))
341
+ : numericConstants_1.ZERO;
342
+ return BigNum.from(rawBn, precisionShift).shift(totalShift, true);
343
+ }
344
+ static max(a, b) {
345
+ return a.gt(b) ? a : b;
346
+ }
347
+ static min(a, b) {
348
+ return a.lt(b) ? a : b;
349
+ }
350
+ static zero(precision) {
351
+ return BigNum.from(0, precision);
352
+ }
353
+ }
354
+ exports.BigNum = BigNum;
355
+ BigNum.delim = '.';
356
+ 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
  }