@drift-labs/sdk 0.2.0-temp.2 → 2.0.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 (278) hide show
  1. package/README.md +27 -29
  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/pollingUserAccountSubscriber.d.ts +1 -1
  15. package/lib/accounts/pollingUserAccountSubscriber.js +5 -11
  16. package/lib/accounts/pollingUserStatsAccountSubscriber.d.ts +27 -0
  17. package/lib/accounts/pollingUserStatsAccountSubscriber.js +107 -0
  18. package/lib/accounts/types.d.ts +26 -15
  19. package/lib/accounts/webSocketDriftClientAccountSubscriber.d.ts +49 -0
  20. package/lib/accounts/{webSocketClearingHouseAccountSubscriber.js → webSocketDriftClientAccountSubscriber.js} +47 -45
  21. package/lib/accounts/webSocketUserStatsAccountSubsriber.d.ts +20 -0
  22. package/lib/accounts/webSocketUserStatsAccountSubsriber.js +47 -0
  23. package/lib/addresses/marketAddresses.d.ts +1 -3
  24. package/lib/addresses/marketAddresses.js +1 -1
  25. package/lib/addresses/pda.d.ts +15 -9
  26. package/lib/addresses/pda.js +73 -35
  27. package/lib/adminClient.d.ts +65 -0
  28. package/lib/adminClient.js +637 -0
  29. package/lib/config.d.ts +10 -10
  30. package/lib/config.js +26 -22
  31. package/lib/constants/numericConstants.d.ts +29 -12
  32. package/lib/constants/numericConstants.js +40 -21
  33. package/lib/constants/perpMarkets.d.ts +18 -0
  34. package/lib/constants/{markets.js → perpMarkets.js} +20 -9
  35. package/lib/constants/spotMarkets.d.ts +19 -0
  36. package/lib/constants/spotMarkets.js +63 -0
  37. package/lib/dlob/DLOB.d.ts +82 -0
  38. package/lib/dlob/DLOB.js +694 -0
  39. package/lib/dlob/DLOBNode.d.ts +54 -0
  40. package/lib/dlob/DLOBNode.js +77 -0
  41. package/lib/dlob/NodeList.d.ts +27 -0
  42. package/lib/dlob/NodeList.js +144 -0
  43. package/lib/driftClient.d.ts +234 -0
  44. package/lib/driftClient.js +2108 -0
  45. package/lib/{clearingHouseConfig.d.ts → driftClientConfig.d.ts} +9 -9
  46. package/lib/{clearingHouseConfig.js → driftClientConfig.js} +0 -0
  47. package/lib/events/eventList.d.ts +0 -1
  48. package/lib/events/eventList.js +0 -7
  49. package/lib/events/eventSubscriber.d.ts +5 -2
  50. package/lib/events/eventSubscriber.js +25 -11
  51. package/lib/events/fetchLogs.d.ts +13 -2
  52. package/lib/events/fetchLogs.js +45 -14
  53. package/lib/events/pollingLogProvider.d.ts +2 -1
  54. package/lib/events/pollingLogProvider.js +7 -3
  55. package/lib/events/sort.js +8 -11
  56. package/lib/events/txEventCache.d.ts +0 -2
  57. package/lib/events/txEventCache.js +0 -14
  58. package/lib/events/types.d.ts +11 -3
  59. package/lib/events/types.js +8 -0
  60. package/lib/events/webSocketLogProvider.js +1 -1
  61. package/lib/examples/makeTradeExample.js +30 -18
  62. package/lib/factory/bigNum.d.ts +8 -4
  63. package/lib/factory/bigNum.js +109 -19
  64. package/lib/idl/drift.json +8392 -0
  65. package/lib/index.d.ts +29 -12
  66. package/lib/index.js +29 -12
  67. package/lib/math/amm.d.ts +9 -6
  68. package/lib/math/amm.js +91 -38
  69. package/lib/math/conversion.js +1 -1
  70. package/lib/math/exchangeStatus.d.ts +4 -0
  71. package/lib/math/exchangeStatus.js +18 -0
  72. package/lib/math/funding.d.ts +6 -6
  73. package/lib/math/funding.js +23 -21
  74. package/lib/math/insurance.d.ts +4 -0
  75. package/lib/math/insurance.js +27 -0
  76. package/lib/math/margin.d.ts +11 -0
  77. package/lib/math/margin.js +82 -0
  78. package/lib/math/market.d.ts +14 -9
  79. package/lib/math/market.js +70 -10
  80. package/lib/math/oracles.d.ts +4 -0
  81. package/lib/math/oracles.js +36 -8
  82. package/lib/math/orders.d.ts +16 -6
  83. package/lib/math/orders.js +97 -17
  84. package/lib/math/position.d.ts +27 -13
  85. package/lib/math/position.js +91 -37
  86. package/lib/math/repeg.js +17 -8
  87. package/lib/math/spotBalance.d.ts +22 -0
  88. package/lib/math/spotBalance.js +192 -0
  89. package/lib/math/spotMarket.d.ts +4 -0
  90. package/lib/math/spotMarket.js +8 -0
  91. package/lib/math/spotPosition.d.ts +6 -0
  92. package/lib/math/spotPosition.js +23 -0
  93. package/lib/math/trade.d.ts +10 -10
  94. package/lib/math/trade.js +27 -31
  95. package/lib/oracles/pythClient.js +1 -1
  96. package/lib/oracles/quoteAssetOracleClient.js +1 -1
  97. package/lib/oracles/switchboardClient.js +1 -1
  98. package/lib/orderParams.d.ts +4 -4
  99. package/lib/orderParams.js +12 -4
  100. package/lib/serum/serumFulfillmentConfigMap.d.ts +10 -0
  101. package/lib/serum/serumFulfillmentConfigMap.js +17 -0
  102. package/lib/serum/serumSubscriber.d.ts +27 -0
  103. package/lib/serum/serumSubscriber.js +56 -0
  104. package/lib/serum/types.d.ts +11 -0
  105. package/{src/oracles → lib/serum}/types.js +0 -0
  106. package/lib/tokenFaucet.d.ts +1 -0
  107. package/lib/tokenFaucet.js +23 -12
  108. package/lib/tx/retryTxSender.d.ts +1 -1
  109. package/lib/tx/retryTxSender.js +13 -4
  110. package/lib/tx/types.d.ts +1 -1
  111. package/lib/types.d.ts +631 -222
  112. package/lib/types.js +137 -24
  113. package/lib/user.d.ts +228 -0
  114. package/lib/user.js +959 -0
  115. package/lib/userConfig.d.ts +14 -0
  116. package/lib/{clearingHouseUserConfig.js → userConfig.js} +0 -0
  117. package/lib/userMap/userMap.d.ts +41 -0
  118. package/lib/userMap/userMap.js +85 -0
  119. package/lib/userMap/userStatsMap.d.ts +19 -0
  120. package/lib/userMap/userStatsMap.js +68 -0
  121. package/lib/userName.d.ts +1 -0
  122. package/lib/userName.js +3 -2
  123. package/lib/userStats.d.ts +18 -0
  124. package/lib/userStats.js +49 -0
  125. package/lib/userStatsConfig.d.ts +14 -0
  126. package/{src/clearingHouseConfig.js → lib/userStatsConfig.js} +0 -0
  127. package/lib/util/getTokenAddress.d.ts +2 -0
  128. package/lib/util/getTokenAddress.js +9 -0
  129. package/package.json +12 -5
  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/pollingUserAccountSubscriber.ts +5 -12
  137. package/src/accounts/pollingUserStatsAccountSubscriber.ts +166 -0
  138. package/src/accounts/types.ts +35 -15
  139. package/src/accounts/{webSocketClearingHouseAccountSubscriber.ts → webSocketDriftClientAccountSubscriber.ts} +78 -73
  140. package/src/accounts/webSocketUserStatsAccountSubsriber.ts +80 -0
  141. package/src/addresses/marketAddresses.ts +3 -4
  142. package/src/addresses/pda.ts +105 -33
  143. package/src/adminClient.ts +1207 -0
  144. package/src/config.ts +41 -34
  145. package/src/constants/numericConstants.ts +59 -26
  146. package/src/constants/{markets.ts → perpMarkets.ts} +22 -11
  147. package/src/constants/spotMarkets.ts +83 -0
  148. package/src/dlob/DLOB.ts +1120 -0
  149. package/src/dlob/DLOBNode.ts +155 -0
  150. package/src/dlob/NodeList.ts +195 -0
  151. package/src/driftClient.ts +3594 -0
  152. package/src/{clearingHouseConfig.ts → driftClientConfig.ts} +9 -8
  153. package/src/events/eventList.ts +1 -8
  154. package/src/events/eventSubscriber.ts +36 -14
  155. package/src/events/fetchLogs.ts +60 -15
  156. package/src/events/pollingLogProvider.ts +11 -3
  157. package/src/events/sort.ts +11 -15
  158. package/src/events/txEventCache.ts +0 -16
  159. package/src/events/types.ts +27 -2
  160. package/src/events/webSocketLogProvider.ts +1 -1
  161. package/src/examples/makeTradeExample.js +152 -75
  162. package/src/examples/makeTradeExample.ts +44 -28
  163. package/src/factory/bigNum.ts +150 -22
  164. package/src/idl/drift.json +8392 -0
  165. package/src/idl/pyth.json +98 -2
  166. package/src/index.ts +29 -12
  167. package/src/math/amm.ts +161 -48
  168. package/src/math/conversion.ts +2 -2
  169. package/src/math/exchangeStatus.ts +31 -0
  170. package/src/math/funding.ts +41 -31
  171. package/src/math/insurance.ts +35 -0
  172. package/src/math/margin.ts +133 -0
  173. package/src/math/market.ts +143 -14
  174. package/src/math/oracles.ts +63 -9
  175. package/src/math/orders.ts +168 -26
  176. package/src/math/position.ts +133 -59
  177. package/src/math/repeg.ts +19 -9
  178. package/src/math/spotBalance.ts +319 -0
  179. package/src/math/spotMarket.ts +9 -0
  180. package/src/math/spotPosition.ts +47 -0
  181. package/src/math/trade.ts +33 -37
  182. package/src/oracles/pythClient.ts +2 -2
  183. package/src/oracles/quoteAssetOracleClient.ts +2 -2
  184. package/src/oracles/switchboardClient.ts +2 -2
  185. package/src/orderParams.ts +16 -8
  186. package/src/serum/serumFulfillmentConfigMap.ts +26 -0
  187. package/src/serum/serumSubscriber.ts +99 -0
  188. package/src/serum/types.ts +13 -0
  189. package/src/tokenFaucet.ts +38 -15
  190. package/src/tx/retryTxSender.ts +16 -5
  191. package/src/tx/types.ts +2 -1
  192. package/src/types.ts +594 -195
  193. package/src/user.ts +1599 -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/getTokenAddress.ts +18 -0
  201. package/tests/bn/test.ts +46 -11
  202. package/tests/dlob/helpers.ts +619 -0
  203. package/tests/dlob/test.ts +4586 -0
  204. package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +0 -49
  205. package/lib/admin.d.ts +0 -44
  206. package/lib/admin.js +0 -438
  207. package/lib/clearingHouse.d.ts +0 -146
  208. package/lib/clearingHouse.js +0 -1154
  209. package/lib/clearingHouseUser.d.ts +0 -187
  210. package/lib/clearingHouseUser.js +0 -634
  211. package/lib/clearingHouseUserConfig.d.ts +0 -14
  212. package/lib/constants/banks.d.ts +0 -16
  213. package/lib/constants/banks.js +0 -41
  214. package/lib/constants/markets.d.ts +0 -19
  215. package/lib/idl/clearing_house.json +0 -4464
  216. package/lib/math/bankBalance.d.ts +0 -9
  217. package/lib/math/bankBalance.js +0 -75
  218. package/lib/math/state.d.ts +0 -8
  219. package/lib/math/state.js +0 -15
  220. package/lib/orders.d.ts +0 -8
  221. package/lib/orders.js +0 -134
  222. package/src/accounts/bulkAccountLoader.js +0 -197
  223. package/src/accounts/bulkUserSubscription.js +0 -33
  224. package/src/accounts/pollingClearingHouseAccountSubscriber.js +0 -311
  225. package/src/accounts/pollingOracleSubscriber.js +0 -93
  226. package/src/accounts/pollingTokenAccountSubscriber.js +0 -90
  227. package/src/accounts/pollingUserAccountSubscriber.js +0 -132
  228. package/src/accounts/types.js +0 -10
  229. package/src/accounts/utils.js +0 -7
  230. package/src/accounts/webSocketAccountSubscriber.js +0 -93
  231. package/src/accounts/webSocketClearingHouseAccountSubscriber.js +0 -233
  232. package/src/accounts/webSocketUserAccountSubscriber.js +0 -62
  233. package/src/addresses/marketAddresses.js +0 -26
  234. package/src/admin.js +0 -517
  235. package/src/admin.ts +0 -730
  236. package/src/clearingHouse.ts +0 -1828
  237. package/src/clearingHouseUser.ts +0 -978
  238. package/src/clearingHouseUserConfig.js +0 -2
  239. package/src/config.js +0 -67
  240. package/src/constants/banks.js +0 -42
  241. package/src/constants/banks.ts +0 -50
  242. package/src/constants/markets.js +0 -42
  243. package/src/constants/numericConstants.js +0 -41
  244. package/src/events/eventSubscriber.js +0 -139
  245. package/src/events/fetchLogs.js +0 -50
  246. package/src/events/pollingLogProvider.js +0 -64
  247. package/src/events/sort.js +0 -44
  248. package/src/events/txEventCache.js +0 -71
  249. package/src/events/types.js +0 -20
  250. package/src/events/webSocketLogProvider.js +0 -41
  251. package/src/factory/bigNum.js +0 -390
  252. package/src/factory/oracleClient.js +0 -20
  253. package/src/idl/clearing_house.json +0 -4464
  254. package/src/index.js +0 -69
  255. package/src/math/amm.js +0 -369
  256. package/src/math/auction.js +0 -42
  257. package/src/math/bankBalance.ts +0 -112
  258. package/src/math/conversion.js +0 -11
  259. package/src/math/funding.js +0 -248
  260. package/src/math/oracles.js +0 -26
  261. package/src/math/repeg.js +0 -128
  262. package/src/math/state.js +0 -15
  263. package/src/math/state.ts +0 -14
  264. package/src/math/trade.js +0 -253
  265. package/src/math/utils.js +0 -26
  266. package/src/math/utils.js.map +0 -1
  267. package/src/mockUSDCFaucet.js +0 -280
  268. package/src/oracles/oracleClientCache.js +0 -19
  269. package/src/oracles/pythClient.js +0 -46
  270. package/src/oracles/quoteAssetOracleClient.js +0 -32
  271. package/src/oracles/switchboardClient.js +0 -69
  272. package/src/orderParams.js +0 -20
  273. package/src/orders.ts +0 -245
  274. package/src/slot/SlotSubscriber.js +0 -39
  275. package/src/tokenFaucet.js +0 -189
  276. package/src/types.js +0 -125
  277. package/src/userName.js +0 -20
  278. package/src/wallet.js +0 -35
@@ -9,6 +9,11 @@ export class BigNum {
9
9
  static delim = '.';
10
10
  static spacer = ',';
11
11
 
12
+ public static setLocale(locale: string): void {
13
+ BigNum.delim = (1.1).toLocaleString(locale).slice(1, 2) || '.';
14
+ BigNum.spacer = (1000).toLocaleString(locale).slice(1, 2) || ',';
15
+ }
16
+
12
17
  constructor(
13
18
  val: BN | number | string,
14
19
  precisionVal: BN | number | string = new BN(0)
@@ -17,6 +22,10 @@ export class BigNum {
17
22
  this.precision = new BN(precisionVal);
18
23
  }
19
24
 
25
+ private bigNumFromParam(bn: BigNum | BN) {
26
+ return BN.isBN(bn) ? BigNum.from(bn) : bn;
27
+ }
28
+
20
29
  public add(bn: BigNum): BigNum {
21
30
  assert(bn.precision.eq(this.precision), 'Adding unequal precisions');
22
31
 
@@ -30,7 +39,7 @@ export class BigNum {
30
39
  }
31
40
 
32
41
  public mul(bn: BigNum | BN): BigNum {
33
- const mulVal = bn instanceof BigNum ? bn : BigNum.from(bn);
42
+ const mulVal = this.bigNumFromParam(bn);
34
43
 
35
44
  return BigNum.from(
36
45
  this.val.mul(mulVal.val),
@@ -44,7 +53,7 @@ export class BigNum {
44
53
  * @returns
45
54
  */
46
55
  public scalarMul(bn: BigNum | BN): BigNum {
47
- if (bn instanceof BN) return BigNum.from(this.val.mul(bn), this.precision);
56
+ if (BN.isBN(bn)) return BigNum.from(this.val.mul(bn), this.precision);
48
57
 
49
58
  return BigNum.from(
50
59
  this.val.mul(bn.val),
@@ -53,7 +62,8 @@ export class BigNum {
53
62
  }
54
63
 
55
64
  public div(bn: BigNum | BN): BigNum {
56
- if (bn instanceof BN) return BigNum.from(this.val.div(bn), this.precision);
65
+ if (BN.isBN(bn)) return BigNum.from(this.val.div(bn), this.precision);
66
+
57
67
  return BigNum.from(this.val.div(bn.val), this.precision.sub(bn.precision));
58
68
  }
59
69
 
@@ -101,7 +111,7 @@ export class BigNum {
101
111
  }
102
112
 
103
113
  public gt(bn: BigNum | BN, ignorePrecision?: boolean): boolean {
104
- const comparisonVal = bn instanceof BigNum ? bn : BigNum.from(bn);
114
+ const comparisonVal = this.bigNumFromParam(bn);
105
115
 
106
116
  if (!ignorePrecision && !comparisonVal.eq(ZERO)) {
107
117
  assert(
@@ -114,7 +124,7 @@ export class BigNum {
114
124
  }
115
125
 
116
126
  public lt(bn: BigNum | BN, ignorePrecision?: boolean): boolean {
117
- const comparisonVal = bn instanceof BigNum ? bn : BigNum.from(bn);
127
+ const comparisonVal = this.bigNumFromParam(bn);
118
128
 
119
129
  if (!ignorePrecision && !comparisonVal.val.eq(ZERO)) {
120
130
  assert(
@@ -127,7 +137,7 @@ export class BigNum {
127
137
  }
128
138
 
129
139
  public gte(bn: BigNum | BN, ignorePrecision?: boolean): boolean {
130
- const comparisonVal = bn instanceof BigNum ? bn : BigNum.from(bn);
140
+ const comparisonVal = this.bigNumFromParam(bn);
131
141
 
132
142
  if (!ignorePrecision && !comparisonVal.val.eq(ZERO)) {
133
143
  assert(
@@ -140,7 +150,7 @@ export class BigNum {
140
150
  }
141
151
 
142
152
  public lte(bn: BigNum | BN, ignorePrecision?: boolean): boolean {
143
- const comparisonVal = bn instanceof BigNum ? bn : BigNum.from(bn);
153
+ const comparisonVal = this.bigNumFromParam(bn);
144
154
 
145
155
  if (!ignorePrecision && !comparisonVal.val.eq(ZERO)) {
146
156
  assert(
@@ -153,7 +163,7 @@ export class BigNum {
153
163
  }
154
164
 
155
165
  public eq(bn: BigNum | BN, ignorePrecision?: boolean): boolean {
156
- const comparisonVal = bn instanceof BigNum ? bn : BigNum.from(bn);
166
+ const comparisonVal = this.bigNumFromParam(bn);
157
167
 
158
168
  if (!ignorePrecision && !comparisonVal.val.eq(ZERO)) {
159
169
  assert(
@@ -206,7 +216,8 @@ export class BigNum {
206
216
  'Tried to print a BN with precision lower than zero'
207
217
  );
208
218
 
209
- const plainString = this.toString();
219
+ const isNeg = this.isNeg();
220
+ const plainString = this.abs().toString();
210
221
  const precisionNum = this.precision.toNumber();
211
222
 
212
223
  // make a string with at least the precisionNum number of zeroes
@@ -227,6 +238,9 @@ export class BigNum {
227
238
  // add zero if leading delim
228
239
  if (printString[0] === BigNum.delim) printString = `0${printString}`;
229
240
 
241
+ // Add minus if negative
242
+ if (isNeg) printString = `-${printString}`;
243
+
230
244
  // remove trailing delim
231
245
  if (printString[printString.length - 1] === BigNum.delim)
232
246
  printString = printString.slice(0, printString.length - 1);
@@ -281,6 +295,8 @@ export class BigNum {
281
295
  ? this.toTradePrecision()
282
296
  : this.print();
283
297
 
298
+ if (!printVal.includes(BigNum.delim)) return printVal;
299
+
284
300
  return printVal.replace(/0+$/g, '').replace(/\.$/, '').replace(/,$/, '');
285
301
  }
286
302
 
@@ -295,7 +311,11 @@ export class BigNum {
295
311
  * @param fixedPrecision
296
312
  * @returns
297
313
  */
298
- public toFixed(fixedPrecision: number): string {
314
+ public toFixed(fixedPrecision: number, rounded = false): string {
315
+ if (rounded) {
316
+ return this.toRounded(fixedPrecision).toFixed(fixedPrecision);
317
+ }
318
+
299
319
  const printString = this.print();
300
320
 
301
321
  const [leftSide, rightSide] = printString.split(BigNum.delim);
@@ -310,16 +330,78 @@ export class BigNum {
310
330
  return `${leftSide}${BigNum.delim}${filledRightSide}`;
311
331
  }
312
332
 
333
+ private getZeroes(count: number) {
334
+ return new Array(Math.max(count, 0)).fill('0').join('');
335
+ }
336
+
337
+ public toRounded(roundingPrecision: number) {
338
+ const printString = this.toString();
339
+
340
+ let shouldRoundUp = false;
341
+
342
+ const roundingDigitChar = printString[roundingPrecision];
343
+
344
+ if (roundingDigitChar) {
345
+ const roundingDigitVal = Number(roundingDigitChar);
346
+ if (roundingDigitVal >= 5) shouldRoundUp = true;
347
+ }
348
+
349
+ if (shouldRoundUp) {
350
+ const valueWithRoundedPrecisionAdded = this.add(
351
+ BigNum.from(
352
+ new BN(10).pow(new BN(printString.length - roundingPrecision)),
353
+ this.precision
354
+ )
355
+ );
356
+
357
+ const roundedUpPrintString =
358
+ valueWithRoundedPrecisionAdded.toString().slice(0, roundingPrecision) +
359
+ this.getZeroes(printString.length - roundingPrecision);
360
+
361
+ return BigNum.from(roundedUpPrintString, this.precision);
362
+ } else {
363
+ const roundedDownPrintString =
364
+ printString.slice(0, roundingPrecision) +
365
+ this.getZeroes(printString.length - roundingPrecision);
366
+
367
+ return BigNum.from(roundedDownPrintString, this.precision);
368
+ }
369
+ }
370
+
313
371
  /**
314
372
  * Pretty print to the specified number of significant figures
315
373
  * @param fixedPrecision
316
374
  * @returns
317
375
  */
318
- public toPrecision(fixedPrecision: number, trailingZeroes = false): string {
376
+ public toPrecision(
377
+ fixedPrecision: number,
378
+ trailingZeroes = false,
379
+ rounded = false
380
+ ): string {
381
+ if (rounded) {
382
+ return this.toRounded(fixedPrecision).toPrecision(
383
+ fixedPrecision,
384
+ trailingZeroes
385
+ );
386
+ }
387
+
319
388
  const printString = this.print();
320
389
 
321
390
  let precisionPrintString = printString.slice(0, fixedPrecision + 1);
322
391
 
392
+ const thisString = this.toString();
393
+
394
+ if (
395
+ !printString.includes(BigNum.delim) &&
396
+ thisString.length < fixedPrecision
397
+ ) {
398
+ const precisionMismatch = fixedPrecision - thisString.length;
399
+ return BigNum.from(
400
+ thisString + this.getZeroes(precisionMismatch),
401
+ precisionMismatch
402
+ ).toPrecision(fixedPrecision, trailingZeroes);
403
+ }
404
+
323
405
  if (
324
406
  !precisionPrintString.includes(BigNum.delim) ||
325
407
  precisionPrintString[precisionPrintString.length - 1] === BigNum.delim
@@ -376,8 +458,8 @@ export class BigNum {
376
458
  return precisionPrintString;
377
459
  }
378
460
 
379
- public toTradePrecision(): string {
380
- return this.toPrecision(6, true);
461
+ public toTradePrecision(rounded = false): string {
462
+ return this.toPrecision(6, true, rounded);
381
463
  }
382
464
 
383
465
  /**
@@ -392,15 +474,39 @@ export class BigNum {
392
474
  ): string {
393
475
  const prefix = `${this.lt(BigNum.zero()) ? `-` : ``}$`;
394
476
 
395
- const val =
396
- useTradePrecision || precisionOverride
397
- ? this.prettyPrint(useTradePrecision, precisionOverride)
398
- : BigNum.fromPrint(this.toFixed(2), new BN(2)).prettyPrint();
477
+ const usingCustomPrecision =
478
+ true && (useTradePrecision || precisionOverride);
479
+
480
+ let val = usingCustomPrecision
481
+ ? this.prettyPrint(useTradePrecision, precisionOverride)
482
+ : BigNum.fromPrint(this.toFixed(2), new BN(2)).prettyPrint();
483
+
484
+ // Append two trailing zeroes if not using custom precision
485
+ if (!usingCustomPrecision) {
486
+ const [_, rightSide] = val.split(BigNum.delim);
487
+
488
+ const trailingLength = rightSide?.length ?? 0;
489
+
490
+ if (trailingLength < 2) {
491
+ const numHasDecimals = this.print().includes(BigNum.delim);
492
+
493
+ // Handle case where pretty print won't include the decimal point
494
+ if (trailingLength === 0 && numHasDecimals) {
495
+ val = `${val}.00`;
496
+ } else {
497
+ val = `${val}${new Array(2 - trailingLength).fill('0').join('')}`;
498
+ }
499
+ }
500
+ }
399
501
 
400
502
  return `${prefix}${val.replace('-', '')}`;
401
503
  }
402
504
 
403
- public toMillified(precision = 3): string {
505
+ public toMillified(precision = 3, rounded = false): string {
506
+ if (rounded) {
507
+ return this.toRounded(precision).toMillified(precision);
508
+ }
509
+
404
510
  const stringVal = this.print();
405
511
 
406
512
  const [leftSide] = stringVal.split(BigNum.delim);
@@ -409,6 +515,10 @@ export class BigNum {
409
515
  return this.shift(new BN(precision)).toPrecision(precision, true);
410
516
  }
411
517
 
518
+ if (leftSide.length <= precision) {
519
+ return this.toPrecision(precision);
520
+ }
521
+
412
522
  if (leftSide.length <= 3) {
413
523
  return this.shift(new BN(precision)).toPrecision(precision, true);
414
524
  }
@@ -461,7 +571,21 @@ export class BigNum {
461
571
  * @returns
462
572
  */
463
573
  public toNum() {
464
- return parseFloat(this.print());
574
+ let printedValue = this.print();
575
+
576
+ // Must convert any non-US delimiters and spacers to US format before using parseFloat
577
+ if (BigNum.delim !== '.' || BigNum.spacer !== ',') {
578
+ printedValue = printedValue
579
+ .split('')
580
+ .map((char) => {
581
+ if (char === BigNum.delim) return '.';
582
+ if (char === BigNum.spacer) return ',';
583
+ return char;
584
+ })
585
+ .join('');
586
+ }
587
+
588
+ return parseFloat(printedValue);
465
589
  }
466
590
 
467
591
  static fromJSON(json: { val: string; precision: string }) {
@@ -494,12 +618,16 @@ export class BigNum {
494
618
  static fromPrint(val: string, precisionShift?: BN): BigNum {
495
619
  // Handle empty number edge cases
496
620
  if (!val) return BigNum.from(ZERO, precisionShift);
497
- if (!val.replace(BigNum.delim, ''))
621
+ if (!val.replace(BigNum.delim, '')) {
498
622
  return BigNum.from(ZERO, precisionShift);
623
+ }
499
624
 
500
- const [leftSide, rightSide] = val.split(BigNum.delim);
625
+ const sides = val.split(BigNum.delim);
626
+ const rightSide = sides[1];
627
+ const leftSide = sides[0].replace(/\D/g, '');
628
+ const bnInput = `${leftSide ?? ''}${rightSide ?? ''}`;
501
629
 
502
- const rawBn = new BN(`${leftSide ?? ''}${rightSide ?? ''}`);
630
+ const rawBn = new BN(bnInput);
503
631
 
504
632
  const rightSideLength = rightSide?.length ?? 0;
505
633