@drift-labs/sdk 0.2.0-master.4 → 0.2.0-master.40

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 (234) hide show
  1. package/README.md +27 -27
  2. package/lib/accounts/bulkAccountLoader.d.ts +2 -0
  3. package/lib/accounts/bulkAccountLoader.js +36 -29
  4. package/lib/accounts/bulkUserStatsSubscription.d.ts +7 -0
  5. package/lib/accounts/bulkUserStatsSubscription.js +21 -0
  6. package/lib/accounts/bulkUserSubscription.d.ts +2 -2
  7. package/lib/accounts/bulkUserSubscription.js +0 -1
  8. package/lib/accounts/fetch.d.ts +2 -1
  9. package/lib/accounts/fetch.js +9 -1
  10. package/lib/accounts/{pollingClearingHouseAccountSubscriber.d.ts → pollingDriftClientAccountSubscriber.d.ts} +20 -25
  11. package/lib/accounts/{pollingClearingHouseAccountSubscriber.js → pollingDriftClientAccountSubscriber.js} +45 -49
  12. package/lib/accounts/{pollingOracleSubscriber.d.ts → pollingOracleAccountSubscriber.d.ts} +2 -2
  13. package/lib/accounts/{pollingOracleSubscriber.js → pollingOracleAccountSubscriber.js} +3 -3
  14. package/lib/accounts/pollingUserStatsAccountSubscriber.d.ts +27 -0
  15. package/lib/accounts/pollingUserStatsAccountSubscriber.js +113 -0
  16. package/lib/accounts/types.d.ts +26 -15
  17. package/lib/accounts/webSocketDriftClientAccountSubscriber.d.ts +49 -0
  18. package/lib/accounts/{webSocketClearingHouseAccountSubscriber.js → webSocketDriftClientAccountSubscriber.js} +47 -45
  19. package/lib/accounts/webSocketUserStatsAccountSubsriber.d.ts +20 -0
  20. package/lib/accounts/webSocketUserStatsAccountSubsriber.js +47 -0
  21. package/lib/addresses/marketAddresses.d.ts +1 -3
  22. package/lib/addresses/marketAddresses.js +1 -1
  23. package/lib/addresses/pda.d.ts +15 -9
  24. package/lib/addresses/pda.js +73 -35
  25. package/lib/adminClient.d.ts +65 -0
  26. package/lib/adminClient.js +637 -0
  27. package/lib/config.d.ts +9 -9
  28. package/lib/config.js +25 -21
  29. package/lib/constants/numericConstants.d.ts +30 -12
  30. package/lib/constants/numericConstants.js +41 -21
  31. package/lib/constants/perpMarkets.d.ts +18 -0
  32. package/lib/constants/{markets.js → perpMarkets.js} +7 -7
  33. package/lib/constants/spotMarkets.d.ts +19 -0
  34. package/lib/constants/spotMarkets.js +53 -0
  35. package/lib/dlob/DLOB.d.ts +82 -0
  36. package/lib/dlob/DLOB.js +696 -0
  37. package/lib/dlob/DLOBNode.d.ts +54 -0
  38. package/lib/dlob/DLOBNode.js +77 -0
  39. package/lib/dlob/NodeList.d.ts +27 -0
  40. package/lib/dlob/NodeList.js +144 -0
  41. package/lib/driftClient.d.ts +233 -0
  42. package/lib/driftClient.js +2096 -0
  43. package/lib/{clearingHouseConfig.d.ts → driftClientConfig.d.ts} +9 -9
  44. package/lib/{clearingHouseConfig.js → driftClientConfig.js} +0 -0
  45. package/lib/events/eventList.js +3 -0
  46. package/lib/events/eventSubscriber.d.ts +5 -2
  47. package/lib/events/eventSubscriber.js +25 -11
  48. package/lib/events/fetchLogs.d.ts +13 -2
  49. package/lib/events/fetchLogs.js +40 -12
  50. package/lib/events/pollingLogProvider.d.ts +2 -1
  51. package/lib/events/pollingLogProvider.js +7 -3
  52. package/lib/events/sort.js +8 -11
  53. package/lib/events/types.d.ts +9 -3
  54. package/lib/events/types.js +6 -0
  55. package/lib/events/webSocketLogProvider.js +1 -1
  56. package/lib/examples/makeTradeExample.js +30 -18
  57. package/lib/factory/bigNum.d.ts +8 -4
  58. package/lib/factory/bigNum.js +109 -19
  59. package/lib/idl/drift.json +8250 -0
  60. package/lib/idl/{mock_usdc_faucet.json → token_faucet.json} +46 -23
  61. package/lib/index.d.ts +30 -13
  62. package/lib/index.js +30 -13
  63. package/lib/math/amm.d.ts +9 -6
  64. package/lib/math/amm.js +91 -38
  65. package/lib/math/conversion.js +1 -1
  66. package/lib/math/exchangeStatus.d.ts +4 -0
  67. package/lib/math/exchangeStatus.js +18 -0
  68. package/lib/math/funding.d.ts +6 -6
  69. package/lib/math/funding.js +23 -21
  70. package/lib/math/insurance.d.ts +4 -0
  71. package/lib/math/insurance.js +27 -0
  72. package/lib/math/margin.d.ts +11 -0
  73. package/lib/math/margin.js +82 -0
  74. package/lib/math/market.d.ts +14 -9
  75. package/lib/math/market.js +70 -10
  76. package/lib/math/oracles.d.ts +4 -0
  77. package/lib/math/oracles.js +36 -8
  78. package/lib/math/orders.d.ts +14 -6
  79. package/lib/math/orders.js +90 -17
  80. package/lib/math/position.d.ts +27 -13
  81. package/lib/math/position.js +92 -36
  82. package/lib/math/repeg.js +17 -8
  83. package/lib/math/spotBalance.d.ts +22 -0
  84. package/lib/math/spotBalance.js +192 -0
  85. package/lib/math/spotMarket.d.ts +4 -0
  86. package/lib/math/spotMarket.js +8 -0
  87. package/lib/math/spotPosition.d.ts +6 -0
  88. package/lib/math/spotPosition.js +23 -0
  89. package/lib/math/trade.d.ts +10 -10
  90. package/lib/math/trade.js +27 -31
  91. package/lib/oracles/pythClient.js +1 -1
  92. package/lib/oracles/quoteAssetOracleClient.js +1 -1
  93. package/lib/oracles/switchboardClient.js +1 -1
  94. package/lib/orderParams.d.ts +4 -4
  95. package/lib/orderParams.js +12 -4
  96. package/lib/serum/serumFulfillmentConfigMap.d.ts +10 -0
  97. package/lib/serum/serumFulfillmentConfigMap.js +17 -0
  98. package/lib/serum/serumSubscriber.d.ts +27 -0
  99. package/lib/serum/serumSubscriber.js +56 -0
  100. package/lib/serum/types.d.ts +11 -0
  101. package/lib/{clearingHouseUserConfig.js → serum/types.js} +0 -0
  102. package/lib/slot/SlotSubscriber.d.ts +7 -0
  103. package/lib/slot/SlotSubscriber.js +3 -0
  104. package/lib/{mockUSDCFaucet.d.ts → tokenFaucet.d.ts} +8 -5
  105. package/lib/{mockUSDCFaucet.js → tokenFaucet.js} +63 -51
  106. package/lib/tx/retryTxSender.d.ts +1 -1
  107. package/lib/tx/retryTxSender.js +13 -4
  108. package/lib/tx/types.d.ts +1 -1
  109. package/lib/tx/utils.js +1 -1
  110. package/lib/types.d.ts +589 -196
  111. package/lib/types.js +108 -17
  112. package/lib/user.d.ts +226 -0
  113. package/lib/user.js +949 -0
  114. package/lib/userConfig.d.ts +14 -0
  115. package/lib/userConfig.js +2 -0
  116. package/lib/userMap/userMap.d.ts +41 -0
  117. package/lib/userMap/userMap.js +85 -0
  118. package/lib/userMap/userStatsMap.d.ts +19 -0
  119. package/lib/userMap/userStatsMap.js +68 -0
  120. package/lib/userName.d.ts +1 -0
  121. package/lib/userName.js +3 -2
  122. package/lib/userStats.d.ts +18 -0
  123. package/lib/userStats.js +49 -0
  124. package/lib/userStatsConfig.d.ts +14 -0
  125. package/lib/userStatsConfig.js +2 -0
  126. package/lib/util/computeUnits.js +1 -1
  127. package/lib/util/getTokenAddress.d.ts +2 -0
  128. package/lib/util/getTokenAddress.js +9 -0
  129. package/package.json +10 -3
  130. package/src/accounts/bulkAccountLoader.ts +44 -34
  131. package/src/accounts/bulkUserStatsSubscription.ts +33 -0
  132. package/src/accounts/bulkUserSubscription.ts +2 -3
  133. package/src/accounts/fetch.ts +27 -2
  134. package/src/accounts/{pollingClearingHouseAccountSubscriber.ts → pollingDriftClientAccountSubscriber.ts} +65 -75
  135. package/src/accounts/{pollingOracleSubscriber.ts → pollingOracleAccountSubscriber.ts} +2 -2
  136. package/src/accounts/pollingUserStatsAccountSubscriber.ts +172 -0
  137. package/src/accounts/types.ts +35 -15
  138. package/src/accounts/{webSocketClearingHouseAccountSubscriber.ts → webSocketDriftClientAccountSubscriber.ts} +78 -73
  139. package/src/accounts/webSocketUserStatsAccountSubsriber.ts +80 -0
  140. package/src/addresses/marketAddresses.ts +3 -4
  141. package/src/addresses/pda.ts +105 -33
  142. package/src/adminClient.ts +1207 -0
  143. package/src/config.ts +37 -31
  144. package/src/constants/numericConstants.ts +58 -24
  145. package/src/constants/{markets.ts → perpMarkets.ts} +10 -10
  146. package/src/constants/spotMarkets.ts +73 -0
  147. package/src/dlob/DLOB.ts +1123 -0
  148. package/src/dlob/DLOBNode.ts +155 -0
  149. package/src/dlob/NodeList.ts +195 -0
  150. package/src/driftClient.ts +3564 -0
  151. package/src/{clearingHouseConfig.ts → driftClientConfig.ts} +9 -8
  152. package/src/events/eventList.ts +3 -0
  153. package/src/events/eventSubscriber.ts +36 -14
  154. package/src/events/fetchLogs.ts +55 -13
  155. package/src/events/pollingLogProvider.ts +11 -3
  156. package/src/events/sort.ts +11 -15
  157. package/src/events/types.ts +21 -2
  158. package/src/events/webSocketLogProvider.ts +1 -1
  159. package/src/examples/makeTradeExample.ts +44 -28
  160. package/src/factory/bigNum.ts +150 -22
  161. package/src/idl/drift.json +8250 -0
  162. package/src/idl/pyth.json +98 -2
  163. package/src/idl/{mock_usdc_faucet.json → token_faucet.json} +46 -23
  164. package/src/index.ts +30 -13
  165. package/src/math/amm.ts +161 -48
  166. package/src/math/conversion.ts +2 -2
  167. package/src/math/exchangeStatus.ts +31 -0
  168. package/src/math/funding.ts +41 -31
  169. package/src/math/insurance.ts +35 -0
  170. package/src/math/margin.ts +133 -0
  171. package/src/math/market.ts +143 -14
  172. package/src/math/oracles.ts +63 -9
  173. package/src/math/orders.ts +163 -26
  174. package/src/math/position.ts +136 -58
  175. package/src/math/repeg.ts +19 -9
  176. package/src/math/spotBalance.ts +319 -0
  177. package/src/math/spotMarket.ts +9 -0
  178. package/src/math/spotPosition.ts +47 -0
  179. package/src/math/trade.ts +33 -37
  180. package/src/oracles/pythClient.ts +2 -2
  181. package/src/oracles/quoteAssetOracleClient.ts +2 -2
  182. package/src/oracles/switchboardClient.ts +2 -2
  183. package/src/orderParams.ts +16 -8
  184. package/src/serum/serumFulfillmentConfigMap.ts +26 -0
  185. package/src/serum/serumSubscriber.ts +99 -0
  186. package/src/serum/types.ts +13 -0
  187. package/src/slot/SlotSubscriber.ts +11 -1
  188. package/src/{mockUSDCFaucet.ts → tokenFaucet.ts} +82 -70
  189. package/src/tx/retryTxSender.ts +16 -5
  190. package/src/tx/types.ts +2 -1
  191. package/src/tx/utils.ts +1 -1
  192. package/src/types.ts +572 -178
  193. package/src/user.ts +1582 -0
  194. package/src/{clearingHouseUserConfig.ts → userConfig.ts} +5 -5
  195. package/src/userMap/userMap.ts +124 -0
  196. package/src/userMap/userStatsMap.ts +108 -0
  197. package/src/userName.ts +2 -1
  198. package/src/userStats.ts +75 -0
  199. package/src/userStatsConfig.ts +18 -0
  200. package/src/util/computeUnits.ts +1 -1
  201. package/src/util/getTokenAddress.ts +18 -0
  202. package/tests/bn/test.ts +46 -11
  203. package/tests/dlob/helpers.ts +611 -0
  204. package/tests/dlob/test.ts +4588 -0
  205. package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +0 -49
  206. package/lib/admin.d.ts +0 -44
  207. package/lib/admin.js +0 -433
  208. package/lib/clearingHouse.d.ts +0 -133
  209. package/lib/clearingHouse.js +0 -931
  210. package/lib/clearingHouseUser.d.ts +0 -187
  211. package/lib/clearingHouseUser.js +0 -643
  212. package/lib/clearingHouseUserConfig.d.ts +0 -14
  213. package/lib/constants/banks.d.ts +0 -16
  214. package/lib/constants/banks.js +0 -34
  215. package/lib/constants/markets.d.ts +0 -19
  216. package/lib/idl/clearing_house.json +0 -3998
  217. package/lib/math/bankBalance.d.ts +0 -9
  218. package/lib/math/bankBalance.js +0 -75
  219. package/lib/math/state.d.ts +0 -8
  220. package/lib/math/state.js +0 -15
  221. package/lib/orders.d.ts +0 -8
  222. package/lib/orders.js +0 -134
  223. package/src/admin.ts +0 -722
  224. package/src/clearingHouse.ts +0 -1451
  225. package/src/clearingHouseUser.ts +0 -989
  226. package/src/constants/banks.ts +0 -43
  227. package/src/idl/clearing_house.json +0 -3998
  228. package/src/math/bankBalance.ts +0 -112
  229. package/src/math/state.ts +0 -14
  230. package/src/math/utils.js +0 -27
  231. package/src/math/utils.js.map +0 -1
  232. package/src/orders.ts +0 -244
  233. package/src/util/computeUnits.js +0 -17
  234. package/src/util/computeUnits.js.map +0 -1
@@ -10,6 +10,13 @@ class BigNum {
10
10
  this.val = new anchor_1.BN(val);
11
11
  this.precision = new anchor_1.BN(precisionVal);
12
12
  }
13
+ static setLocale(locale) {
14
+ BigNum.delim = (1.1).toLocaleString(locale).slice(1, 2) || '.';
15
+ BigNum.spacer = (1000).toLocaleString(locale).slice(1, 2) || ',';
16
+ }
17
+ bigNumFromParam(bn) {
18
+ return anchor_1.BN.isBN(bn) ? BigNum.from(bn) : bn;
19
+ }
13
20
  add(bn) {
14
21
  (0, assert_1.assert)(bn.precision.eq(this.precision), 'Adding unequal precisions');
15
22
  return BigNum.from(this.val.add(bn.val), this.precision);
@@ -19,7 +26,7 @@ class BigNum {
19
26
  return BigNum.from(this.val.sub(bn.val), this.precision);
20
27
  }
21
28
  mul(bn) {
22
- const mulVal = bn instanceof BigNum ? bn : BigNum.from(bn);
29
+ const mulVal = this.bigNumFromParam(bn);
23
30
  return BigNum.from(this.val.mul(mulVal.val), this.precision.add(mulVal.precision));
24
31
  }
25
32
  /**
@@ -28,12 +35,12 @@ class BigNum {
28
35
  * @returns
29
36
  */
30
37
  scalarMul(bn) {
31
- if (bn instanceof anchor_1.BN)
38
+ if (anchor_1.BN.isBN(bn))
32
39
  return BigNum.from(this.val.mul(bn), this.precision);
33
40
  return BigNum.from(this.val.mul(bn.val), this.precision.add(bn.precision)).shift(bn.precision.neg());
34
41
  }
35
42
  div(bn) {
36
- if (bn instanceof anchor_1.BN)
43
+ if (anchor_1.BN.isBN(bn))
37
44
  return BigNum.from(this.val.div(bn), this.precision);
38
45
  return BigNum.from(this.val.div(bn.val), this.precision.sub(bn.precision));
39
46
  }
@@ -73,35 +80,35 @@ class BigNum {
73
80
  .toPrecision(precision);
74
81
  }
75
82
  gt(bn, ignorePrecision) {
76
- const comparisonVal = bn instanceof BigNum ? bn : BigNum.from(bn);
83
+ const comparisonVal = this.bigNumFromParam(bn);
77
84
  if (!ignorePrecision && !comparisonVal.eq(numericConstants_1.ZERO)) {
78
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');
79
86
  }
80
87
  return this.val.gt(comparisonVal.val);
81
88
  }
82
89
  lt(bn, ignorePrecision) {
83
- const comparisonVal = bn instanceof BigNum ? bn : BigNum.from(bn);
90
+ const comparisonVal = this.bigNumFromParam(bn);
84
91
  if (!ignorePrecision && !comparisonVal.val.eq(numericConstants_1.ZERO)) {
85
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');
86
93
  }
87
94
  return this.val.lt(comparisonVal.val);
88
95
  }
89
96
  gte(bn, ignorePrecision) {
90
- const comparisonVal = bn instanceof BigNum ? bn : BigNum.from(bn);
97
+ const comparisonVal = this.bigNumFromParam(bn);
91
98
  if (!ignorePrecision && !comparisonVal.val.eq(numericConstants_1.ZERO)) {
92
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');
93
100
  }
94
101
  return this.val.gte(comparisonVal.val);
95
102
  }
96
103
  lte(bn, ignorePrecision) {
97
- const comparisonVal = bn instanceof BigNum ? bn : BigNum.from(bn);
104
+ const comparisonVal = this.bigNumFromParam(bn);
98
105
  if (!ignorePrecision && !comparisonVal.val.eq(numericConstants_1.ZERO)) {
99
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');
100
107
  }
101
108
  return this.val.lte(comparisonVal.val);
102
109
  }
103
110
  eq(bn, ignorePrecision) {
104
- const comparisonVal = bn instanceof BigNum ? bn : BigNum.from(bn);
111
+ const comparisonVal = this.bigNumFromParam(bn);
105
112
  if (!ignorePrecision && !comparisonVal.val.eq(numericConstants_1.ZERO)) {
106
113
  (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
114
  }
@@ -134,7 +141,8 @@ class BigNum {
134
141
  */
135
142
  print() {
136
143
  (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();
144
+ const isNeg = this.isNeg();
145
+ const plainString = this.abs().toString();
138
146
  const precisionNum = this.precision.toNumber();
139
147
  // make a string with at least the precisionNum number of zeroes
140
148
  let printString = [
@@ -151,6 +159,9 @@ class BigNum {
151
159
  // add zero if leading delim
152
160
  if (printString[0] === BigNum.delim)
153
161
  printString = `0${printString}`;
162
+ // Add minus if negative
163
+ if (isNeg)
164
+ printString = `-${printString}`;
154
165
  // remove trailing delim
155
166
  if (printString[printString.length - 1] === BigNum.delim)
156
167
  printString = printString.slice(0, printString.length - 1);
@@ -182,6 +193,8 @@ class BigNum {
182
193
  : useTradePrecision
183
194
  ? this.toTradePrecision()
184
195
  : this.print();
196
+ if (!printVal.includes(BigNum.delim))
197
+ return printVal;
185
198
  return printVal.replace(/0+$/g, '').replace(/\.$/, '').replace(/,$/, '');
186
199
  }
187
200
  debug() {
@@ -192,7 +205,10 @@ class BigNum {
192
205
  * @param fixedPrecision
193
206
  * @returns
194
207
  */
195
- toFixed(fixedPrecision) {
208
+ toFixed(fixedPrecision, rounded = false) {
209
+ if (rounded) {
210
+ return this.toRounded(fixedPrecision).toFixed(fixedPrecision);
211
+ }
196
212
  const printString = this.print();
197
213
  const [leftSide, rightSide] = printString.split(BigNum.delim);
198
214
  const filledRightSide = [
@@ -203,14 +219,47 @@ class BigNum {
203
219
  .join('');
204
220
  return `${leftSide}${BigNum.delim}${filledRightSide}`;
205
221
  }
222
+ getZeroes(count) {
223
+ return new Array(Math.max(count, 0)).fill('0').join('');
224
+ }
225
+ toRounded(roundingPrecision) {
226
+ const printString = this.toString();
227
+ let shouldRoundUp = false;
228
+ const roundingDigitChar = printString[roundingPrecision];
229
+ if (roundingDigitChar) {
230
+ const roundingDigitVal = Number(roundingDigitChar);
231
+ if (roundingDigitVal >= 5)
232
+ shouldRoundUp = true;
233
+ }
234
+ if (shouldRoundUp) {
235
+ const valueWithRoundedPrecisionAdded = this.add(BigNum.from(new anchor_1.BN(10).pow(new anchor_1.BN(printString.length - roundingPrecision)), this.precision));
236
+ const roundedUpPrintString = valueWithRoundedPrecisionAdded.toString().slice(0, roundingPrecision) +
237
+ this.getZeroes(printString.length - roundingPrecision);
238
+ return BigNum.from(roundedUpPrintString, this.precision);
239
+ }
240
+ else {
241
+ const roundedDownPrintString = printString.slice(0, roundingPrecision) +
242
+ this.getZeroes(printString.length - roundingPrecision);
243
+ return BigNum.from(roundedDownPrintString, this.precision);
244
+ }
245
+ }
206
246
  /**
207
247
  * Pretty print to the specified number of significant figures
208
248
  * @param fixedPrecision
209
249
  * @returns
210
250
  */
211
- toPrecision(fixedPrecision, trailingZeroes = false) {
251
+ toPrecision(fixedPrecision, trailingZeroes = false, rounded = false) {
252
+ if (rounded) {
253
+ return this.toRounded(fixedPrecision).toPrecision(fixedPrecision, trailingZeroes);
254
+ }
212
255
  const printString = this.print();
213
256
  let precisionPrintString = printString.slice(0, fixedPrecision + 1);
257
+ const thisString = this.toString();
258
+ if (!printString.includes(BigNum.delim) &&
259
+ thisString.length < fixedPrecision) {
260
+ const precisionMismatch = fixedPrecision - thisString.length;
261
+ return BigNum.from(thisString + this.getZeroes(precisionMismatch), precisionMismatch).toPrecision(fixedPrecision, trailingZeroes);
262
+ }
214
263
  if (!precisionPrintString.includes(BigNum.delim) ||
215
264
  precisionPrintString[precisionPrintString.length - 1] === BigNum.delim) {
216
265
  precisionPrintString = printString.slice(0, fixedPrecision);
@@ -251,8 +300,8 @@ class BigNum {
251
300
  }
252
301
  return precisionPrintString;
253
302
  }
254
- toTradePrecision() {
255
- return this.toPrecision(6, true);
303
+ toTradePrecision(rounded = false) {
304
+ return this.toPrecision(6, true, rounded);
256
305
  }
257
306
  /**
258
307
  * Print dollar formatted value. Defaults to fixed decimals two unless a given precision is given.
@@ -261,18 +310,41 @@ class BigNum {
261
310
  * @returns
262
311
  */
263
312
  toNotional(useTradePrecision, precisionOverride) {
313
+ var _a;
264
314
  const prefix = `${this.lt(BigNum.zero()) ? `-` : ``}$`;
265
- const val = useTradePrecision || precisionOverride
315
+ const usingCustomPrecision = true && (useTradePrecision || precisionOverride);
316
+ let val = usingCustomPrecision
266
317
  ? this.prettyPrint(useTradePrecision, precisionOverride)
267
318
  : BigNum.fromPrint(this.toFixed(2), new anchor_1.BN(2)).prettyPrint();
319
+ // Append two trailing zeroes if not using custom precision
320
+ if (!usingCustomPrecision) {
321
+ const [_, rightSide] = val.split(BigNum.delim);
322
+ const trailingLength = (_a = rightSide === null || rightSide === void 0 ? void 0 : rightSide.length) !== null && _a !== void 0 ? _a : 0;
323
+ if (trailingLength < 2) {
324
+ const numHasDecimals = this.print().includes(BigNum.delim);
325
+ // Handle case where pretty print won't include the decimal point
326
+ if (trailingLength === 0 && numHasDecimals) {
327
+ val = `${val}.00`;
328
+ }
329
+ else {
330
+ val = `${val}${new Array(2 - trailingLength).fill('0').join('')}`;
331
+ }
332
+ }
333
+ }
268
334
  return `${prefix}${val.replace('-', '')}`;
269
335
  }
270
- toMillified(precision = 3) {
336
+ toMillified(precision = 3, rounded = false) {
337
+ if (rounded) {
338
+ return this.toRounded(precision).toMillified(precision);
339
+ }
271
340
  const stringVal = this.print();
272
341
  const [leftSide] = stringVal.split(BigNum.delim);
273
342
  if (!leftSide) {
274
343
  return this.shift(new anchor_1.BN(precision)).toPrecision(precision, true);
275
344
  }
345
+ if (leftSide.length <= precision) {
346
+ return this.toPrecision(precision);
347
+ }
276
348
  if (leftSide.length <= 3) {
277
349
  return this.shift(new anchor_1.BN(precision)).toPrecision(precision, true);
278
350
  }
@@ -313,7 +385,21 @@ class BigNum {
313
385
  * @returns
314
386
  */
315
387
  toNum() {
316
- return parseFloat(this.print());
388
+ let printedValue = this.print();
389
+ // Must convert any non-US delimiters and spacers to US format before using parseFloat
390
+ if (BigNum.delim !== '.' || BigNum.spacer !== ',') {
391
+ printedValue = printedValue
392
+ .split('')
393
+ .map((char) => {
394
+ if (char === BigNum.delim)
395
+ return '.';
396
+ if (char === BigNum.spacer)
397
+ return ',';
398
+ return char;
399
+ })
400
+ .join('');
401
+ }
402
+ return parseFloat(printedValue);
317
403
  }
318
404
  static fromJSON(json) {
319
405
  return BigNum.from(new anchor_1.BN(json.val), new anchor_1.BN(json.precision));
@@ -339,10 +425,14 @@ class BigNum {
339
425
  // Handle empty number edge cases
340
426
  if (!val)
341
427
  return BigNum.from(numericConstants_1.ZERO, precisionShift);
342
- if (!val.replace(BigNum.delim, ''))
428
+ if (!val.replace(BigNum.delim, '')) {
343
429
  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 : ''}`);
430
+ }
431
+ const sides = val.split(BigNum.delim);
432
+ const rightSide = sides[1];
433
+ const leftSide = sides[0].replace(/\D/g, '');
434
+ const bnInput = `${leftSide !== null && leftSide !== void 0 ? leftSide : ''}${rightSide !== null && rightSide !== void 0 ? rightSide : ''}`;
435
+ const rawBn = new anchor_1.BN(bnInput);
346
436
  const rightSideLength = (_a = rightSide === null || rightSide === void 0 ? void 0 : rightSide.length) !== null && _a !== void 0 ? _a : 0;
347
437
  const totalShift = precisionShift
348
438
  ? precisionShift.sub(new anchor_1.BN(rightSideLength))