@drift-labs/sdk 0.2.0-master.25 → 0.2.0-master.26

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 (184) hide show
  1. package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +13 -13
  2. package/lib/accounts/pollingClearingHouseAccountSubscriber.js +27 -27
  3. package/lib/accounts/types.d.ts +8 -9
  4. package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +14 -14
  5. package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +35 -34
  6. package/lib/addresses/pda.d.ts +7 -6
  7. package/lib/addresses/pda.js +31 -27
  8. package/lib/admin.d.ts +9 -6
  9. package/lib/admin.js +83 -42
  10. package/lib/clearingHouse.d.ts +69 -42
  11. package/lib/clearingHouse.js +753 -277
  12. package/lib/clearingHouseConfig.d.ts +2 -2
  13. package/lib/clearingHouseUser.d.ts +16 -16
  14. package/lib/clearingHouseUser.js +139 -119
  15. package/lib/config.d.ts +7 -7
  16. package/lib/config.js +20 -20
  17. package/lib/constants/numericConstants.d.ts +12 -12
  18. package/lib/constants/numericConstants.js +13 -13
  19. package/lib/constants/{markets.d.ts → perpMarkets.d.ts} +5 -5
  20. package/{src/constants/markets.js → lib/constants/perpMarkets.js} +4 -4
  21. package/lib/constants/{banks.d.ts → spotMarkets.d.ts} +6 -6
  22. package/lib/constants/{banks.js → spotMarkets.js} +16 -16
  23. package/lib/events/types.d.ts +2 -1
  24. package/lib/events/types.js +1 -0
  25. package/lib/examples/makeTradeExample.js +7 -7
  26. package/lib/idl/clearing_house.json +1008 -279
  27. package/lib/index.d.ts +5 -3
  28. package/lib/index.js +5 -3
  29. package/lib/math/amm.d.ts +2 -2
  30. package/lib/math/amm.js +1 -1
  31. package/lib/math/funding.d.ts +6 -6
  32. package/lib/math/funding.js +2 -1
  33. package/lib/math/margin.d.ts +4 -4
  34. package/lib/math/margin.js +18 -11
  35. package/lib/math/market.d.ts +10 -9
  36. package/lib/math/market.js +29 -6
  37. package/lib/math/oracles.d.ts +2 -1
  38. package/lib/math/oracles.js +11 -1
  39. package/lib/math/orders.d.ts +5 -5
  40. package/lib/math/position.d.ts +13 -13
  41. package/lib/math/position.js +19 -19
  42. package/lib/math/spotBalance.d.ts +19 -0
  43. package/lib/math/spotBalance.js +176 -0
  44. package/lib/math/spotMarket.d.ts +4 -0
  45. package/lib/math/spotMarket.js +8 -0
  46. package/lib/math/spotPosition.d.ts +2 -0
  47. package/lib/math/spotPosition.js +8 -0
  48. package/lib/math/state.js +2 -2
  49. package/lib/math/trade.d.ts +4 -4
  50. package/lib/orderParams.d.ts +4 -4
  51. package/lib/orderParams.js +12 -4
  52. package/lib/serum/serumSubscriber.d.ts +23 -0
  53. package/lib/serum/serumSubscriber.js +41 -0
  54. package/lib/serum/types.d.ts +11 -0
  55. package/lib/serum/types.js +2 -0
  56. package/lib/tx/retryTxSender.d.ts +1 -1
  57. package/lib/tx/retryTxSender.js +4 -2
  58. package/lib/tx/types.d.ts +1 -1
  59. package/lib/types.d.ts +123 -33
  60. package/lib/types.js +31 -9
  61. package/my-script/.env +7 -0
  62. package/my-script/getUserStats.ts +106 -0
  63. package/my-script/multiConnections.ts +119 -0
  64. package/my-script/test-regex.ts +11 -0
  65. package/my-script/utils.ts +52 -0
  66. package/package.json +1 -1
  67. package/src/accounts/bulkAccountLoader.js +249 -0
  68. package/src/accounts/bulkUserStatsSubscription.js +75 -0
  69. package/src/accounts/bulkUserSubscription.js +75 -0
  70. package/src/accounts/fetch.js +92 -0
  71. package/src/accounts/pollingClearingHouseAccountSubscriber.js +465 -0
  72. package/src/accounts/pollingClearingHouseAccountSubscriber.ts +38 -38
  73. package/src/accounts/pollingOracleSubscriber.js +156 -0
  74. package/src/accounts/pollingTokenAccountSubscriber.js +141 -0
  75. package/src/accounts/pollingUserAccountSubscriber.js +208 -0
  76. package/src/accounts/pollingUserStatsAccountSubscriber.js +208 -0
  77. package/src/accounts/types.js +28 -0
  78. package/src/accounts/types.ts +11 -9
  79. package/src/accounts/utils.js +7 -0
  80. package/src/accounts/webSocketAccountSubscriber.js +138 -0
  81. package/src/accounts/webSocketClearingHouseAccountSubscriber.js +433 -0
  82. package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +59 -52
  83. package/src/accounts/webSocketUserAccountSubscriber.js +113 -0
  84. package/src/accounts/webSocketUserStatsAccountSubsriber.js +113 -0
  85. package/src/addresses/pda.js +186 -0
  86. package/src/addresses/pda.ts +49 -44
  87. package/src/admin.js +1284 -0
  88. package/src/admin.ts +140 -47
  89. package/src/assert/assert.js +1 -1
  90. package/src/clearingHouse.js +3433 -0
  91. package/src/clearingHouse.ts +1083 -378
  92. package/src/clearingHouseConfig.js +2 -0
  93. package/src/clearingHouseConfig.ts +2 -2
  94. package/src/clearingHouseUser.js +874 -0
  95. package/src/clearingHouseUser.ts +232 -168
  96. package/src/clearingHouseUserConfig.js +2 -0
  97. package/src/clearingHouseUserStats.js +115 -0
  98. package/src/clearingHouseUserStatsConfig.js +2 -0
  99. package/src/config.js +80 -0
  100. package/src/config.ts +29 -29
  101. package/src/constants/numericConstants.js +18 -11
  102. package/src/constants/numericConstants.ts +17 -15
  103. package/{lib/constants/markets.js → src/constants/perpMarkets.js} +11 -11
  104. package/src/constants/{markets.ts → perpMarkets.ts} +5 -5
  105. package/src/constants/spotMarkets.js +51 -0
  106. package/src/constants/{banks.ts → spotMarkets.ts} +19 -19
  107. package/src/events/eventList.js +66 -23
  108. package/src/events/eventSubscriber.js +202 -0
  109. package/src/events/fetchLogs.js +117 -0
  110. package/src/events/pollingLogProvider.js +113 -0
  111. package/src/events/sort.js +41 -0
  112. package/src/events/txEventCache.js +22 -19
  113. package/src/events/types.js +25 -0
  114. package/src/events/types.ts +3 -0
  115. package/src/events/webSocketLogProvider.js +76 -0
  116. package/src/examples/makeTradeExample.ts +10 -8
  117. package/src/factory/bigNum.js +183 -180
  118. package/src/factory/oracleClient.js +9 -9
  119. package/src/idl/clearing_house.json +1008 -279
  120. package/src/index.js +75 -0
  121. package/src/index.ts +5 -3
  122. package/src/math/amm.js +422 -0
  123. package/src/math/amm.ts +6 -3
  124. package/src/math/auction.js +10 -10
  125. package/src/math/conversion.js +4 -3
  126. package/src/math/funding.js +223 -175
  127. package/src/math/funding.ts +7 -7
  128. package/src/math/insurance.js +27 -0
  129. package/src/math/margin.js +77 -0
  130. package/src/math/margin.ts +34 -23
  131. package/src/math/market.js +105 -0
  132. package/src/math/market.ts +71 -19
  133. package/src/math/oracles.js +40 -10
  134. package/src/math/oracles.ts +18 -1
  135. package/src/math/orders.js +153 -0
  136. package/src/math/orders.ts +5 -5
  137. package/src/math/position.js +172 -0
  138. package/src/math/position.ts +31 -31
  139. package/src/math/repeg.js +40 -40
  140. package/src/math/spotBalance.js +176 -0
  141. package/src/math/spotBalance.ts +290 -0
  142. package/src/math/spotMarket.js +8 -0
  143. package/src/math/spotMarket.ts +9 -0
  144. package/src/math/spotPosition.js +8 -0
  145. package/src/math/spotPosition.ts +6 -0
  146. package/src/math/state.ts +2 -2
  147. package/src/math/trade.js +81 -74
  148. package/src/math/trade.ts +4 -4
  149. package/src/math/utils.js +8 -7
  150. package/src/oracles/oracleClientCache.js +10 -9
  151. package/src/oracles/pythClient.js +52 -17
  152. package/src/oracles/quoteAssetOracleClient.js +44 -13
  153. package/src/oracles/switchboardClient.js +69 -37
  154. package/src/oracles/types.js +1 -1
  155. package/src/orderParams.js +14 -6
  156. package/src/orderParams.ts +16 -8
  157. package/src/serum/serumSubscriber.js +102 -0
  158. package/src/serum/serumSubscriber.ts +80 -0
  159. package/src/serum/types.js +2 -0
  160. package/src/serum/types.ts +13 -0
  161. package/src/slot/SlotSubscriber.js +67 -20
  162. package/src/token/index.js +4 -4
  163. package/src/tokenFaucet.js +288 -154
  164. package/src/tx/retryTxSender.js +280 -0
  165. package/src/tx/retryTxSender.ts +5 -2
  166. package/src/tx/types.js +1 -1
  167. package/src/tx/types.ts +2 -1
  168. package/src/tx/utils.js +7 -6
  169. package/src/types.js +216 -0
  170. package/src/types.ts +110 -33
  171. package/src/userName.js +5 -5
  172. package/src/util/computeUnits.js +46 -11
  173. package/src/util/promiseTimeout.js +5 -5
  174. package/src/util/tps.js +46 -12
  175. package/src/wallet.js +55 -18
  176. package/lib/math/bankBalance.d.ts +0 -15
  177. package/lib/math/bankBalance.js +0 -150
  178. package/src/addresses/marketAddresses.js +0 -26
  179. package/src/constants/banks.js +0 -42
  180. package/src/examples/makeTradeExample.js +0 -80
  181. package/src/math/bankBalance.ts +0 -258
  182. package/src/math/state.js +0 -15
  183. package/src/math/utils.js.map +0 -1
  184. package/src/util/getTokenAddress.js +0 -9
@@ -1,149 +1,161 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
2
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
3
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
4
+ if (ar || !(i in from)) {
5
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
6
+ ar[i] = from[i];
7
+ }
8
+ }
9
+ return to.concat(ar || Array.prototype.slice.call(from));
10
+ };
11
+ exports.__esModule = true;
3
12
  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);
13
+ var anchor_1 = require("@project-serum/anchor");
14
+ var assert_1 = require("../assert/assert");
15
+ var numericConstants_1 = require("./../constants/numericConstants");
16
+ var BigNum = /** @class */ (function () {
17
+ function BigNum(val, precisionVal) {
18
+ var _this = this;
19
+ if (precisionVal === void 0) { precisionVal = new anchor_1.BN(0); }
20
+ this.toString = function (base, length) {
21
+ return _this.val.toString(base, length);
22
+ };
10
23
  this.val = new anchor_1.BN(val);
11
24
  this.precision = new anchor_1.BN(precisionVal);
12
25
  }
13
- bigNumFromParam(bn) {
26
+ BigNum.prototype.bigNumFromParam = function (bn) {
14
27
  return anchor_1.BN.isBN(bn) ? BigNum.from(bn) : bn;
15
- }
16
- add(bn) {
17
- assert_1.assert(bn.precision.eq(this.precision), 'Adding unequal precisions');
28
+ };
29
+ BigNum.prototype.add = function (bn) {
30
+ (0, assert_1.assert)(bn.precision.eq(this.precision), 'Adding unequal precisions');
18
31
  return BigNum.from(this.val.add(bn.val), this.precision);
19
- }
20
- sub(bn) {
21
- assert_1.assert(bn.precision.eq(this.precision), 'Subtracting unequal precisions');
32
+ };
33
+ BigNum.prototype.sub = function (bn) {
34
+ (0, assert_1.assert)(bn.precision.eq(this.precision), 'Subtracting unequal precisions');
22
35
  return BigNum.from(this.val.sub(bn.val), this.precision);
23
- }
24
- mul(bn) {
25
- const mulVal = this.bigNumFromParam(bn);
36
+ };
37
+ BigNum.prototype.mul = function (bn) {
38
+ var mulVal = this.bigNumFromParam(bn);
26
39
  return BigNum.from(this.val.mul(mulVal.val), this.precision.add(mulVal.precision));
27
- }
40
+ };
28
41
  /**
29
42
  * Multiplies by another big number then scales the result down by the big number's precision so that we're in the same precision space
30
43
  * @param bn
31
44
  * @returns
32
45
  */
33
- scalarMul(bn) {
46
+ BigNum.prototype.scalarMul = function (bn) {
34
47
  if (anchor_1.BN.isBN(bn))
35
48
  return BigNum.from(this.val.mul(bn), this.precision);
36
49
  return BigNum.from(this.val.mul(bn.val), this.precision.add(bn.precision)).shift(bn.precision.neg());
37
- }
38
- div(bn) {
50
+ };
51
+ BigNum.prototype.div = function (bn) {
39
52
  if (anchor_1.BN.isBN(bn))
40
53
  return BigNum.from(this.val.div(bn), this.precision);
41
54
  return BigNum.from(this.val.div(bn.val), this.precision.sub(bn.precision));
42
- }
55
+ };
43
56
  /**
44
57
  * Shift precision up or down
45
58
  * @param exponent
46
59
  * @param skipAdjustingPrecision
47
60
  * @returns
48
61
  */
49
- shift(exponent, skipAdjustingPrecision = false) {
50
- const shiftVal = typeof exponent === 'number' ? new anchor_1.BN(exponent) : exponent;
62
+ BigNum.prototype.shift = function (exponent, skipAdjustingPrecision) {
63
+ if (skipAdjustingPrecision === void 0) { skipAdjustingPrecision = false; }
64
+ var shiftVal = typeof exponent === 'number' ? new anchor_1.BN(exponent) : exponent;
51
65
  return BigNum.from(shiftVal.isNeg()
52
66
  ? this.val.div(new anchor_1.BN(10).pow(shiftVal))
53
67
  : this.val.mul(new anchor_1.BN(10).pow(shiftVal)), skipAdjustingPrecision ? this.precision : this.precision.add(shiftVal));
54
- }
68
+ };
55
69
  /**
56
70
  * Shift to a target precision
57
71
  * @param targetPrecision
58
72
  * @returns
59
73
  */
60
- shiftTo(targetPrecision) {
74
+ BigNum.prototype.shiftTo = function (targetPrecision) {
61
75
  return this.shift(targetPrecision.sub(this.precision));
62
- }
76
+ };
63
77
  /**
64
78
  * Scale the number by a fraction
65
79
  * @param numerator
66
80
  * @param denominator
67
81
  * @returns
68
82
  */
69
- scale(numerator, denominator) {
83
+ BigNum.prototype.scale = function (numerator, denominator) {
70
84
  return this.mul(BigNum.from(new anchor_1.BN(numerator))).div(new anchor_1.BN(denominator));
71
- }
72
- toPercentage(denominator, precision) {
85
+ };
86
+ BigNum.prototype.toPercentage = function (denominator, precision) {
73
87
  return this.shift(precision)
74
88
  .shift(2, true)
75
89
  .div(denominator)
76
90
  .toPrecision(precision);
77
- }
78
- gt(bn, ignorePrecision) {
79
- const comparisonVal = this.bigNumFromParam(bn);
91
+ };
92
+ BigNum.prototype.gt = function (bn, ignorePrecision) {
93
+ var comparisonVal = this.bigNumFromParam(bn);
80
94
  if (!ignorePrecision && !comparisonVal.eq(numericConstants_1.ZERO)) {
81
- 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');
95
+ (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');
82
96
  }
83
97
  return this.val.gt(comparisonVal.val);
84
- }
85
- lt(bn, ignorePrecision) {
86
- const comparisonVal = this.bigNumFromParam(bn);
98
+ };
99
+ BigNum.prototype.lt = function (bn, ignorePrecision) {
100
+ var comparisonVal = this.bigNumFromParam(bn);
87
101
  if (!ignorePrecision && !comparisonVal.val.eq(numericConstants_1.ZERO)) {
88
- 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');
102
+ (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');
89
103
  }
90
104
  return this.val.lt(comparisonVal.val);
91
- }
92
- gte(bn, ignorePrecision) {
93
- const comparisonVal = this.bigNumFromParam(bn);
105
+ };
106
+ BigNum.prototype.gte = function (bn, ignorePrecision) {
107
+ var comparisonVal = this.bigNumFromParam(bn);
94
108
  if (!ignorePrecision && !comparisonVal.val.eq(numericConstants_1.ZERO)) {
95
- 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');
109
+ (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');
96
110
  }
97
111
  return this.val.gte(comparisonVal.val);
98
- }
99
- lte(bn, ignorePrecision) {
100
- const comparisonVal = this.bigNumFromParam(bn);
112
+ };
113
+ BigNum.prototype.lte = function (bn, ignorePrecision) {
114
+ var comparisonVal = this.bigNumFromParam(bn);
101
115
  if (!ignorePrecision && !comparisonVal.val.eq(numericConstants_1.ZERO)) {
102
- 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');
116
+ (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');
103
117
  }
104
118
  return this.val.lte(comparisonVal.val);
105
- }
106
- eq(bn, ignorePrecision) {
107
- const comparisonVal = this.bigNumFromParam(bn);
119
+ };
120
+ BigNum.prototype.eq = function (bn, ignorePrecision) {
121
+ var comparisonVal = this.bigNumFromParam(bn);
108
122
  if (!ignorePrecision && !comparisonVal.val.eq(numericConstants_1.ZERO)) {
109
- 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');
123
+ (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');
110
124
  }
111
125
  return this.val.eq(comparisonVal.val);
112
- }
113
- eqZero() {
126
+ };
127
+ BigNum.prototype.eqZero = function () {
114
128
  return this.val.eq(numericConstants_1.ZERO);
115
- }
116
- gtZero() {
129
+ };
130
+ BigNum.prototype.gtZero = function () {
117
131
  return this.val.gt(numericConstants_1.ZERO);
118
- }
119
- ltZero() {
132
+ };
133
+ BigNum.prototype.ltZero = function () {
120
134
  return this.val.lt(numericConstants_1.ZERO);
121
- }
122
- gteZero() {
135
+ };
136
+ BigNum.prototype.gteZero = function () {
123
137
  return this.val.gte(numericConstants_1.ZERO);
124
- }
125
- lteZero() {
138
+ };
139
+ BigNum.prototype.lteZero = function () {
126
140
  return this.val.lte(numericConstants_1.ZERO);
127
- }
128
- abs() {
141
+ };
142
+ BigNum.prototype.abs = function () {
129
143
  return new BigNum(this.val.abs(), this.precision);
130
- }
131
- neg() {
144
+ };
145
+ BigNum.prototype.neg = function () {
132
146
  return new BigNum(this.val.neg(), this.precision);
133
- }
147
+ };
134
148
  /**
135
149
  * Pretty print the underlying value in human-readable form. Depends on precision being correct for the output string to be correct
136
150
  * @returns
137
151
  */
138
- print() {
139
- assert_1.assert(this.precision.gte(numericConstants_1.ZERO), 'Tried to print a BN with precision lower than zero');
140
- const plainString = this.toString();
141
- const precisionNum = this.precision.toNumber();
152
+ BigNum.prototype.print = function () {
153
+ (0, assert_1.assert)(this.precision.gte(numericConstants_1.ZERO), 'Tried to print a BN with precision lower than zero');
154
+ var isNeg = this.isNeg();
155
+ var plainString = this.abs().toString();
156
+ var precisionNum = this.precision.toNumber();
142
157
  // make a string with at least the precisionNum number of zeroes
143
- let printString = [
144
- ...Array(this.precision.toNumber()).fill(0),
145
- ...plainString.split(''),
146
- ].join('');
158
+ var printString = __spreadArray(__spreadArray([], Array(this.precision.toNumber()).fill(0), true), plainString.split(''), true).join('');
147
159
  // inject decimal
148
160
  printString =
149
161
  printString.substring(0, printString.length - precisionNum) +
@@ -152,88 +164,79 @@ class BigNum {
152
164
  // remove leading zeroes
153
165
  printString = printString.replace(/^0+/, '');
154
166
  // add zero if leading delim
155
- if (this.isNeg()) {
156
- if (printString[1] === BigNum.delim)
157
- printString = printString.replace('-.', '-0.');
158
- }
159
- else {
160
- if (printString[0] === BigNum.delim)
161
- printString = `0${printString}`;
162
- }
167
+ if (printString[0] === BigNum.delim)
168
+ printString = "0".concat(printString);
169
+ // Add minus if negative
170
+ if (isNeg)
171
+ printString = "-".concat(printString);
163
172
  // remove trailing delim
164
173
  if (printString[printString.length - 1] === BigNum.delim)
165
174
  printString = printString.slice(0, printString.length - 1);
166
175
  return printString;
167
- }
168
- prettyPrint(useTradePrecision, precisionOverride) {
169
- const [leftSide, rightSide] = this.printShort(useTradePrecision, precisionOverride).split(BigNum.delim);
170
- let formattedLeftSide = leftSide;
171
- const isNeg = formattedLeftSide.includes('-');
176
+ };
177
+ BigNum.prototype.prettyPrint = function (useTradePrecision, precisionOverride) {
178
+ var _a = this.printShort(useTradePrecision, precisionOverride).split(BigNum.delim), leftSide = _a[0], rightSide = _a[1];
179
+ var formattedLeftSide = leftSide;
180
+ var isNeg = formattedLeftSide.includes('-');
172
181
  if (isNeg) {
173
182
  formattedLeftSide = formattedLeftSide.replace('-', '');
174
183
  }
175
- let index = formattedLeftSide.length - 3;
184
+ var index = formattedLeftSide.length - 3;
176
185
  while (index >= 1) {
177
- const formattedLeftSideArray = formattedLeftSide.split('');
186
+ var formattedLeftSideArray = formattedLeftSide.split('');
178
187
  formattedLeftSideArray.splice(index, 0, BigNum.spacer);
179
188
  formattedLeftSide = formattedLeftSideArray.join('');
180
189
  index -= 3;
181
190
  }
182
- return `${isNeg ? '-' : ''}${formattedLeftSide}${rightSide ? `${BigNum.delim}${rightSide}` : ''}`;
183
- }
191
+ return "".concat(isNeg ? '-' : '').concat(formattedLeftSide).concat(rightSide ? "".concat(BigNum.delim).concat(rightSide) : '');
192
+ };
184
193
  /**
185
194
  * Print and remove unnecessary trailing zeroes
186
195
  * @returns
187
196
  */
188
- printShort(useTradePrecision, precisionOverride) {
189
- const printVal = precisionOverride
197
+ BigNum.prototype.printShort = function (useTradePrecision, precisionOverride) {
198
+ var printVal = precisionOverride
190
199
  ? this.toPrecision(precisionOverride)
191
200
  : useTradePrecision
192
201
  ? this.toTradePrecision()
193
202
  : this.print();
194
203
  return printVal.replace(/0+$/g, '').replace(/\.$/, '').replace(/,$/, '');
195
- }
196
- debug() {
197
- console.log(`${this.toString()} | ${this.print()} | ${this.precision.toString()}`);
198
- }
204
+ };
205
+ BigNum.prototype.debug = function () {
206
+ console.log("".concat(this.toString(), " | ").concat(this.print(), " | ").concat(this.precision.toString()));
207
+ };
199
208
  /**
200
209
  * Pretty print with the specified number of decimal places
201
210
  * @param fixedPrecision
202
211
  * @returns
203
212
  */
204
- toFixed(fixedPrecision) {
205
- const printString = this.print();
206
- const [leftSide, rightSide] = printString.split(BigNum.delim);
207
- const filledRightSide = [
208
- ...(rightSide !== null && rightSide !== void 0 ? rightSide : '').slice(0, fixedPrecision),
209
- ...Array(fixedPrecision).fill('0'),
210
- ]
211
- .slice(0, fixedPrecision)
213
+ BigNum.prototype.toFixed = function (fixedPrecision) {
214
+ var printString = this.print();
215
+ var _a = printString.split(BigNum.delim), leftSide = _a[0], rightSide = _a[1];
216
+ var filledRightSide = __spreadArray(__spreadArray([], (rightSide !== null && rightSide !== void 0 ? rightSide : '').slice(0, fixedPrecision), true), Array(fixedPrecision).fill('0'), true).slice(0, fixedPrecision)
212
217
  .join('');
213
- return `${leftSide}${BigNum.delim}${filledRightSide}`;
214
- }
218
+ return "".concat(leftSide).concat(BigNum.delim).concat(filledRightSide);
219
+ };
215
220
  /**
216
221
  * Pretty print to the specified number of significant figures
217
222
  * @param fixedPrecision
218
223
  * @returns
219
224
  */
220
- toPrecision(fixedPrecision, trailingZeroes = false) {
221
- const printString = this.print();
222
- let precisionPrintString = printString.slice(0, fixedPrecision + 1);
225
+ BigNum.prototype.toPrecision = function (fixedPrecision, trailingZeroes) {
226
+ if (trailingZeroes === void 0) { trailingZeroes = false; }
227
+ var printString = this.print();
228
+ var precisionPrintString = printString.slice(0, fixedPrecision + 1);
223
229
  if (!precisionPrintString.includes(BigNum.delim) ||
224
230
  precisionPrintString[precisionPrintString.length - 1] === BigNum.delim) {
225
231
  precisionPrintString = printString.slice(0, fixedPrecision);
226
232
  }
227
- const pointsOfPrecision = precisionPrintString.replace(BigNum.delim, '').length;
233
+ var pointsOfPrecision = precisionPrintString.replace(BigNum.delim, '').length;
228
234
  if (pointsOfPrecision < fixedPrecision) {
229
- precisionPrintString = [
230
- ...precisionPrintString.split(''),
231
- ...Array(fixedPrecision - pointsOfPrecision).fill('0'),
232
- ].join('');
235
+ precisionPrintString = __spreadArray(__spreadArray([], precisionPrintString.split(''), true), Array(fixedPrecision - pointsOfPrecision).fill('0'), true).join('');
233
236
  }
234
237
  if (!precisionPrintString.includes(BigNum.delim)) {
235
- const delimFullStringLocation = printString.indexOf(BigNum.delim);
236
- let skipExponent = false;
238
+ var delimFullStringLocation = printString.indexOf(BigNum.delim);
239
+ var skipExponent = false;
237
240
  if (delimFullStringLocation === -1) {
238
241
  // no decimal, not missing any precision
239
242
  skipExponent = true;
@@ -247,144 +250,144 @@ class BigNum {
247
250
  skipExponent = true;
248
251
  }
249
252
  if (!skipExponent) {
250
- const exponent = delimFullStringLocation - fixedPrecision;
253
+ var exponent = delimFullStringLocation - fixedPrecision;
251
254
  if (trailingZeroes) {
252
- precisionPrintString = `${precisionPrintString}${Array(exponent)
255
+ precisionPrintString = "".concat(precisionPrintString).concat(Array(exponent)
253
256
  .fill('0')
254
- .join('')}`;
257
+ .join(''));
255
258
  }
256
259
  else {
257
- precisionPrintString = `${precisionPrintString}e${exponent}`;
260
+ precisionPrintString = "".concat(precisionPrintString, "e").concat(exponent);
258
261
  }
259
262
  }
260
263
  }
261
264
  return precisionPrintString;
262
- }
263
- toTradePrecision() {
265
+ };
266
+ BigNum.prototype.toTradePrecision = function () {
264
267
  return this.toPrecision(6, true);
265
- }
268
+ };
266
269
  /**
267
270
  * Print dollar formatted value. Defaults to fixed decimals two unless a given precision is given.
268
271
  * @param useTradePrecision
269
272
  * @param precisionOverride
270
273
  * @returns
271
274
  */
272
- toNotional(useTradePrecision, precisionOverride) {
275
+ BigNum.prototype.toNotional = function (useTradePrecision, precisionOverride) {
273
276
  var _a;
274
- const prefix = `${this.lt(BigNum.zero()) ? `-` : ``}$`;
275
- const usingCustomPrecision = true && (useTradePrecision || precisionOverride);
276
- let val = usingCustomPrecision
277
+ var prefix = "".concat(this.lt(BigNum.zero()) ? "-" : "", "$");
278
+ var usingCustomPrecision = true && (useTradePrecision || precisionOverride);
279
+ var val = usingCustomPrecision
277
280
  ? this.prettyPrint(useTradePrecision, precisionOverride)
278
281
  : BigNum.fromPrint(this.toFixed(2), new anchor_1.BN(2)).prettyPrint();
279
282
  // Append two trailing zeroes if not using custom precision
280
283
  if (!usingCustomPrecision) {
281
- const [_, rightSide] = val.split(BigNum.delim);
282
- const trailingLength = (_a = rightSide === null || rightSide === void 0 ? void 0 : rightSide.length) !== null && _a !== void 0 ? _a : 0;
284
+ var _b = val.split(BigNum.delim), _1 = _b[0], rightSide = _b[1];
285
+ var trailingLength = (_a = rightSide === null || rightSide === void 0 ? void 0 : rightSide.length) !== null && _a !== void 0 ? _a : 0;
283
286
  if (trailingLength < 2) {
284
- const numHasDecimals = this.print().includes(BigNum.delim);
287
+ var numHasDecimals = this.print().includes(BigNum.delim);
285
288
  // Handle case where pretty print won't include the decimal point
286
289
  if (trailingLength === 0 && numHasDecimals) {
287
- val = `${val}.00`;
290
+ val = "".concat(val, ".00");
288
291
  }
289
292
  else {
290
- val = `${val}${new Array(2 - trailingLength).fill('0').join('')}`;
293
+ val = "".concat(val).concat(new Array(2 - trailingLength).fill('0').join(''));
291
294
  }
292
295
  }
293
296
  }
294
- return `${prefix}${val.replace('-', '')}`;
295
- }
296
- toMillified(precision = 3) {
297
- const stringVal = this.print();
298
- const [leftSide] = stringVal.split(BigNum.delim);
297
+ return "".concat(prefix).concat(val.replace('-', ''));
298
+ };
299
+ BigNum.prototype.toMillified = function (precision) {
300
+ if (precision === void 0) { precision = 3; }
301
+ var stringVal = this.print();
302
+ var leftSide = stringVal.split(BigNum.delim)[0];
299
303
  if (!leftSide) {
300
304
  return this.shift(new anchor_1.BN(precision)).toPrecision(precision, true);
301
305
  }
302
306
  if (leftSide.length <= 3) {
303
307
  return this.shift(new anchor_1.BN(precision)).toPrecision(precision, true);
304
308
  }
305
- const unitTicks = ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'];
306
- const unitNumber = Math.floor((leftSide.length - 1) / 3);
307
- const unit = unitTicks[unitNumber];
308
- let leadDigits = leftSide.slice(0, precision);
309
+ var unitTicks = ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'];
310
+ var unitNumber = Math.floor((leftSide.length - 1) / 3);
311
+ var unit = unitTicks[unitNumber];
312
+ var leadDigits = leftSide.slice(0, precision);
309
313
  if (leadDigits.length < precision) {
310
- leadDigits = [
311
- ...leadDigits.split(''),
312
- ...Array(precision - leadDigits.length).fill('0'),
313
- ].join('');
314
+ leadDigits = __spreadArray(__spreadArray([], leadDigits.split(''), true), Array(precision - leadDigits.length).fill('0'), true).join('');
314
315
  }
315
- const decimalLocation = leftSide.length - 3 * unitNumber;
316
- let leadString = '';
316
+ var decimalLocation = leftSide.length - 3 * unitNumber;
317
+ var leadString = '';
317
318
  if (decimalLocation >= precision) {
318
- leadString = `${leadDigits}`;
319
+ leadString = "".concat(leadDigits);
319
320
  }
320
321
  else {
321
- leadString = `${leadDigits.slice(0, decimalLocation)}${BigNum.delim}${leadDigits.slice(decimalLocation)}`;
322
+ leadString = "".concat(leadDigits.slice(0, decimalLocation)).concat(BigNum.delim).concat(leadDigits.slice(decimalLocation));
322
323
  }
323
- return `${leadString}${unit}`;
324
- }
325
- toJSON() {
324
+ return "".concat(leadString).concat(unit);
325
+ };
326
+ BigNum.prototype.toJSON = function () {
326
327
  return {
327
328
  val: this.val.toString(),
328
- precision: this.precision.toString(),
329
+ precision: this.precision.toString()
329
330
  };
330
- }
331
- isNeg() {
331
+ };
332
+ BigNum.prototype.isNeg = function () {
332
333
  return this.lt(numericConstants_1.ZERO, true);
333
- }
334
- isPos() {
334
+ };
335
+ BigNum.prototype.isPos = function () {
335
336
  return !this.isNeg();
336
- }
337
+ };
337
338
  /**
338
339
  * Get the numerical value of the BigNum. This can break if the BigNum is too large.
339
340
  * @returns
340
341
  */
341
- toNum() {
342
+ BigNum.prototype.toNum = function () {
342
343
  return parseFloat(this.print());
343
- }
344
- static fromJSON(json) {
344
+ };
345
+ BigNum.fromJSON = function (json) {
345
346
  return BigNum.from(new anchor_1.BN(json.val), new anchor_1.BN(json.precision));
346
- }
347
+ };
347
348
  /**
348
349
  * Create a BigNum instance
349
350
  * @param val
350
351
  * @param precision
351
352
  * @returns
352
353
  */
353
- static from(val = numericConstants_1.ZERO, precision) {
354
- 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');
354
+ BigNum.from = function (val, precision) {
355
+ if (val === void 0) { val = numericConstants_1.ZERO; }
356
+ (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');
355
357
  return new BigNum(val, precision);
356
- }
358
+ };
357
359
  /**
358
360
  * Create a BigNum instance from a printed BigNum
359
361
  * @param val
360
362
  * @param precisionOverride
361
363
  * @returns
362
364
  */
363
- static fromPrint(val, precisionShift) {
365
+ BigNum.fromPrint = function (val, precisionShift) {
364
366
  var _a;
365
367
  // Handle empty number edge cases
366
368
  if (!val)
367
369
  return BigNum.from(numericConstants_1.ZERO, precisionShift);
368
370
  if (!val.replace(BigNum.delim, ''))
369
371
  return BigNum.from(numericConstants_1.ZERO, precisionShift);
370
- const [leftSide, rightSide] = val.split(BigNum.delim);
371
- const rawBn = new anchor_1.BN(`${leftSide !== null && leftSide !== void 0 ? leftSide : ''}${rightSide !== null && rightSide !== void 0 ? rightSide : ''}`);
372
- const rightSideLength = (_a = rightSide === null || rightSide === void 0 ? void 0 : rightSide.length) !== null && _a !== void 0 ? _a : 0;
373
- const totalShift = precisionShift
372
+ var _b = val.split(BigNum.delim), leftSide = _b[0], rightSide = _b[1];
373
+ var rawBn = new anchor_1.BN("".concat(leftSide !== null && leftSide !== void 0 ? leftSide : '').concat(rightSide !== null && rightSide !== void 0 ? rightSide : ''));
374
+ var rightSideLength = (_a = rightSide === null || rightSide === void 0 ? void 0 : rightSide.length) !== null && _a !== void 0 ? _a : 0;
375
+ var totalShift = precisionShift
374
376
  ? precisionShift.sub(new anchor_1.BN(rightSideLength))
375
377
  : numericConstants_1.ZERO;
376
378
  return BigNum.from(rawBn, precisionShift).shift(totalShift, true);
377
- }
378
- static max(a, b) {
379
+ };
380
+ BigNum.max = function (a, b) {
379
381
  return a.gt(b) ? a : b;
380
- }
381
- static min(a, b) {
382
+ };
383
+ BigNum.min = function (a, b) {
382
384
  return a.lt(b) ? a : b;
383
- }
384
- static zero(precision) {
385
+ };
386
+ BigNum.zero = function (precision) {
385
387
  return BigNum.from(0, precision);
386
- }
387
- }
388
+ };
389
+ BigNum.delim = '.';
390
+ BigNum.spacer = ',';
391
+ return BigNum;
392
+ }());
388
393
  exports.BigNum = BigNum;
389
- BigNum.delim = '.';
390
- BigNum.spacer = ',';
@@ -1,20 +1,20 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
2
+ exports.__esModule = true;
3
3
  exports.getOracleClient = void 0;
4
- const types_1 = require("../types");
5
- const pythClient_1 = require("../oracles/pythClient");
6
- const switchboardClient_1 = require("../oracles/switchboardClient");
7
- const quoteAssetOracleClient_1 = require("../oracles/quoteAssetOracleClient");
4
+ var types_1 = require("../types");
5
+ var pythClient_1 = require("../oracles/pythClient");
6
+ var switchboardClient_1 = require("../oracles/switchboardClient");
7
+ var quoteAssetOracleClient_1 = require("../oracles/quoteAssetOracleClient");
8
8
  function getOracleClient(oracleSource, connection) {
9
- if (types_1.isVariant(oracleSource, 'pyth')) {
9
+ if ((0, types_1.isVariant)(oracleSource, 'pyth')) {
10
10
  return new pythClient_1.PythClient(connection);
11
11
  }
12
- if (types_1.isVariant(oracleSource, 'switchboard')) {
12
+ if ((0, types_1.isVariant)(oracleSource, 'switchboard')) {
13
13
  return new switchboardClient_1.SwitchboardClient(connection);
14
14
  }
15
- if (types_1.isVariant(oracleSource, 'quoteAsset')) {
15
+ if ((0, types_1.isVariant)(oracleSource, 'quoteAsset')) {
16
16
  return new quoteAssetOracleClient_1.QuoteAssetOracleClient();
17
17
  }
18
- throw new Error(`Unknown oracle source ${oracleSource}`);
18
+ throw new Error("Unknown oracle source ".concat(oracleSource));
19
19
  }
20
20
  exports.getOracleClient = getOracleClient;