@drift-labs/sdk 0.2.0-temp.2 → 2.0.1
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.
- package/README.md +27 -29
- package/lib/accounts/bulkAccountLoader.d.ts +2 -0
- package/lib/accounts/bulkAccountLoader.js +36 -29
- package/lib/accounts/bulkUserStatsSubscription.d.ts +7 -0
- package/lib/accounts/bulkUserStatsSubscription.js +21 -0
- package/lib/accounts/bulkUserSubscription.d.ts +2 -2
- package/lib/accounts/bulkUserSubscription.js +0 -1
- package/lib/accounts/fetch.d.ts +2 -1
- package/lib/accounts/fetch.js +9 -1
- package/lib/accounts/{pollingClearingHouseAccountSubscriber.d.ts → pollingDriftClientAccountSubscriber.d.ts} +20 -25
- package/lib/accounts/{pollingClearingHouseAccountSubscriber.js → pollingDriftClientAccountSubscriber.js} +45 -49
- package/lib/accounts/{pollingOracleSubscriber.d.ts → pollingOracleAccountSubscriber.d.ts} +2 -2
- package/lib/accounts/{pollingOracleSubscriber.js → pollingOracleAccountSubscriber.js} +3 -3
- package/lib/accounts/pollingUserAccountSubscriber.d.ts +1 -1
- package/lib/accounts/pollingUserAccountSubscriber.js +5 -11
- package/lib/accounts/pollingUserStatsAccountSubscriber.d.ts +27 -0
- package/lib/accounts/pollingUserStatsAccountSubscriber.js +107 -0
- package/lib/accounts/types.d.ts +26 -15
- package/lib/accounts/webSocketDriftClientAccountSubscriber.d.ts +49 -0
- package/lib/accounts/{webSocketClearingHouseAccountSubscriber.js → webSocketDriftClientAccountSubscriber.js} +47 -45
- package/lib/accounts/webSocketUserStatsAccountSubsriber.d.ts +20 -0
- package/lib/accounts/webSocketUserStatsAccountSubsriber.js +47 -0
- package/lib/addresses/marketAddresses.d.ts +1 -3
- package/lib/addresses/marketAddresses.js +1 -1
- package/lib/addresses/pda.d.ts +15 -9
- package/lib/addresses/pda.js +73 -35
- package/lib/adminClient.d.ts +65 -0
- package/lib/adminClient.js +637 -0
- package/lib/config.d.ts +10 -10
- package/lib/config.js +26 -22
- package/lib/constants/numericConstants.d.ts +29 -12
- package/lib/constants/numericConstants.js +40 -21
- package/lib/constants/perpMarkets.d.ts +18 -0
- package/lib/constants/{markets.js → perpMarkets.js} +20 -9
- package/lib/constants/spotMarkets.d.ts +19 -0
- package/lib/constants/spotMarkets.js +63 -0
- package/lib/dlob/DLOB.d.ts +82 -0
- package/lib/dlob/DLOB.js +694 -0
- package/lib/dlob/DLOBNode.d.ts +54 -0
- package/lib/dlob/DLOBNode.js +77 -0
- package/lib/dlob/NodeList.d.ts +27 -0
- package/lib/dlob/NodeList.js +144 -0
- package/lib/driftClient.d.ts +234 -0
- package/lib/driftClient.js +2108 -0
- package/lib/{clearingHouseConfig.d.ts → driftClientConfig.d.ts} +9 -9
- package/lib/{clearingHouseConfig.js → driftClientConfig.js} +0 -0
- package/lib/events/eventList.d.ts +0 -1
- package/lib/events/eventList.js +0 -7
- package/lib/events/eventSubscriber.d.ts +5 -2
- package/lib/events/eventSubscriber.js +25 -11
- package/lib/events/fetchLogs.d.ts +13 -2
- package/lib/events/fetchLogs.js +45 -14
- package/lib/events/pollingLogProvider.d.ts +2 -1
- package/lib/events/pollingLogProvider.js +7 -3
- package/lib/events/sort.js +8 -11
- package/lib/events/txEventCache.d.ts +0 -2
- package/lib/events/txEventCache.js +0 -14
- package/lib/events/types.d.ts +11 -3
- package/lib/events/types.js +8 -0
- package/lib/events/webSocketLogProvider.js +1 -1
- package/lib/examples/makeTradeExample.js +30 -18
- package/lib/factory/bigNum.d.ts +8 -4
- package/lib/factory/bigNum.js +109 -19
- package/lib/idl/drift.json +8392 -0
- package/lib/index.d.ts +29 -12
- package/lib/index.js +29 -12
- package/lib/math/amm.d.ts +9 -6
- package/lib/math/amm.js +91 -38
- package/lib/math/conversion.js +1 -1
- package/lib/math/exchangeStatus.d.ts +4 -0
- package/lib/math/exchangeStatus.js +18 -0
- package/lib/math/funding.d.ts +6 -6
- package/lib/math/funding.js +23 -21
- package/lib/math/insurance.d.ts +4 -0
- package/lib/math/insurance.js +27 -0
- package/lib/math/margin.d.ts +11 -0
- package/lib/math/margin.js +82 -0
- package/lib/math/market.d.ts +14 -9
- package/lib/math/market.js +70 -10
- package/lib/math/oracles.d.ts +4 -0
- package/lib/math/oracles.js +36 -8
- package/lib/math/orders.d.ts +16 -6
- package/lib/math/orders.js +97 -17
- package/lib/math/position.d.ts +27 -13
- package/lib/math/position.js +91 -37
- package/lib/math/repeg.js +17 -8
- package/lib/math/spotBalance.d.ts +22 -0
- package/lib/math/spotBalance.js +192 -0
- package/lib/math/spotMarket.d.ts +4 -0
- package/lib/math/spotMarket.js +8 -0
- package/lib/math/spotPosition.d.ts +6 -0
- package/lib/math/spotPosition.js +23 -0
- package/lib/math/trade.d.ts +10 -10
- package/lib/math/trade.js +27 -31
- package/lib/oracles/pythClient.js +1 -1
- package/lib/oracles/quoteAssetOracleClient.js +1 -1
- package/lib/oracles/switchboardClient.js +1 -1
- package/lib/orderParams.d.ts +4 -4
- package/lib/orderParams.js +12 -4
- package/lib/serum/serumFulfillmentConfigMap.d.ts +10 -0
- package/lib/serum/serumFulfillmentConfigMap.js +17 -0
- package/lib/serum/serumSubscriber.d.ts +27 -0
- package/lib/serum/serumSubscriber.js +56 -0
- package/lib/serum/types.d.ts +11 -0
- package/{src/oracles → lib/serum}/types.js +0 -0
- package/lib/tokenFaucet.d.ts +1 -0
- package/lib/tokenFaucet.js +23 -12
- package/lib/tx/retryTxSender.d.ts +1 -1
- package/lib/tx/retryTxSender.js +13 -4
- package/lib/tx/types.d.ts +1 -1
- package/lib/types.d.ts +631 -222
- package/lib/types.js +137 -24
- package/lib/user.d.ts +228 -0
- package/lib/user.js +959 -0
- package/lib/userConfig.d.ts +14 -0
- package/lib/{clearingHouseUserConfig.js → userConfig.js} +0 -0
- package/lib/userMap/userMap.d.ts +41 -0
- package/lib/userMap/userMap.js +85 -0
- package/lib/userMap/userStatsMap.d.ts +19 -0
- package/lib/userMap/userStatsMap.js +68 -0
- package/lib/userName.d.ts +1 -0
- package/lib/userName.js +3 -2
- package/lib/userStats.d.ts +18 -0
- package/lib/userStats.js +49 -0
- package/lib/userStatsConfig.d.ts +14 -0
- package/{src/clearingHouseConfig.js → lib/userStatsConfig.js} +0 -0
- package/lib/util/getTokenAddress.d.ts +2 -0
- package/lib/util/getTokenAddress.js +9 -0
- package/package.json +12 -5
- package/src/accounts/bulkAccountLoader.ts +44 -34
- package/src/accounts/bulkUserStatsSubscription.ts +33 -0
- package/src/accounts/bulkUserSubscription.ts +2 -3
- package/src/accounts/fetch.ts +27 -2
- package/src/accounts/{pollingClearingHouseAccountSubscriber.ts → pollingDriftClientAccountSubscriber.ts} +65 -75
- package/src/accounts/{pollingOracleSubscriber.ts → pollingOracleAccountSubscriber.ts} +2 -2
- package/src/accounts/pollingUserAccountSubscriber.ts +5 -12
- package/src/accounts/pollingUserStatsAccountSubscriber.ts +166 -0
- package/src/accounts/types.ts +35 -15
- package/src/accounts/{webSocketClearingHouseAccountSubscriber.ts → webSocketDriftClientAccountSubscriber.ts} +78 -73
- package/src/accounts/webSocketUserStatsAccountSubsriber.ts +80 -0
- package/src/addresses/marketAddresses.ts +3 -4
- package/src/addresses/pda.ts +105 -33
- package/src/adminClient.ts +1207 -0
- package/src/config.ts +41 -34
- package/src/constants/numericConstants.ts +59 -26
- package/src/constants/{markets.ts → perpMarkets.ts} +22 -11
- package/src/constants/spotMarkets.ts +83 -0
- package/src/dlob/DLOB.ts +1120 -0
- package/src/dlob/DLOBNode.ts +155 -0
- package/src/dlob/NodeList.ts +195 -0
- package/src/driftClient.ts +3594 -0
- package/src/{clearingHouseConfig.ts → driftClientConfig.ts} +9 -8
- package/src/events/eventList.ts +1 -8
- package/src/events/eventSubscriber.ts +36 -14
- package/src/events/fetchLogs.ts +60 -15
- package/src/events/pollingLogProvider.ts +11 -3
- package/src/events/sort.ts +11 -15
- package/src/events/txEventCache.ts +0 -16
- package/src/events/types.ts +27 -2
- package/src/events/webSocketLogProvider.ts +1 -1
- package/src/examples/makeTradeExample.js +152 -75
- package/src/examples/makeTradeExample.ts +44 -28
- package/src/factory/bigNum.ts +150 -22
- package/src/idl/drift.json +8392 -0
- package/src/idl/pyth.json +98 -2
- package/src/index.ts +29 -12
- package/src/math/amm.ts +161 -48
- package/src/math/conversion.ts +2 -2
- package/src/math/exchangeStatus.ts +31 -0
- package/src/math/funding.ts +41 -31
- package/src/math/insurance.ts +35 -0
- package/src/math/margin.ts +133 -0
- package/src/math/market.ts +143 -14
- package/src/math/oracles.ts +63 -9
- package/src/math/orders.ts +168 -26
- package/src/math/position.ts +133 -59
- package/src/math/repeg.ts +19 -9
- package/src/math/spotBalance.ts +319 -0
- package/src/math/spotMarket.ts +9 -0
- package/src/math/spotPosition.ts +47 -0
- package/src/math/trade.ts +33 -37
- package/src/oracles/pythClient.ts +2 -2
- package/src/oracles/quoteAssetOracleClient.ts +2 -2
- package/src/oracles/switchboardClient.ts +2 -2
- package/src/orderParams.ts +16 -8
- package/src/serum/serumFulfillmentConfigMap.ts +26 -0
- package/src/serum/serumSubscriber.ts +99 -0
- package/src/serum/types.ts +13 -0
- package/src/tokenFaucet.ts +38 -15
- package/src/tx/retryTxSender.ts +16 -5
- package/src/tx/types.ts +2 -1
- package/src/types.ts +594 -195
- package/src/user.ts +1599 -0
- package/src/{clearingHouseUserConfig.ts → userConfig.ts} +5 -5
- package/src/userMap/userMap.ts +124 -0
- package/src/userMap/userStatsMap.ts +108 -0
- package/src/userName.ts +2 -1
- package/src/userStats.ts +75 -0
- package/src/userStatsConfig.ts +18 -0
- package/src/util/getTokenAddress.ts +18 -0
- package/tests/bn/test.ts +46 -11
- package/tests/dlob/helpers.ts +619 -0
- package/tests/dlob/test.ts +4586 -0
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +0 -49
- package/lib/admin.d.ts +0 -44
- package/lib/admin.js +0 -438
- package/lib/clearingHouse.d.ts +0 -146
- package/lib/clearingHouse.js +0 -1154
- package/lib/clearingHouseUser.d.ts +0 -187
- package/lib/clearingHouseUser.js +0 -634
- package/lib/clearingHouseUserConfig.d.ts +0 -14
- package/lib/constants/banks.d.ts +0 -16
- package/lib/constants/banks.js +0 -41
- package/lib/constants/markets.d.ts +0 -19
- package/lib/idl/clearing_house.json +0 -4464
- package/lib/math/bankBalance.d.ts +0 -9
- package/lib/math/bankBalance.js +0 -75
- package/lib/math/state.d.ts +0 -8
- package/lib/math/state.js +0 -15
- package/lib/orders.d.ts +0 -8
- package/lib/orders.js +0 -134
- package/src/accounts/bulkAccountLoader.js +0 -197
- package/src/accounts/bulkUserSubscription.js +0 -33
- package/src/accounts/pollingClearingHouseAccountSubscriber.js +0 -311
- package/src/accounts/pollingOracleSubscriber.js +0 -93
- package/src/accounts/pollingTokenAccountSubscriber.js +0 -90
- package/src/accounts/pollingUserAccountSubscriber.js +0 -132
- package/src/accounts/types.js +0 -10
- package/src/accounts/utils.js +0 -7
- package/src/accounts/webSocketAccountSubscriber.js +0 -93
- package/src/accounts/webSocketClearingHouseAccountSubscriber.js +0 -233
- package/src/accounts/webSocketUserAccountSubscriber.js +0 -62
- package/src/addresses/marketAddresses.js +0 -26
- package/src/admin.js +0 -517
- package/src/admin.ts +0 -730
- package/src/clearingHouse.ts +0 -1828
- package/src/clearingHouseUser.ts +0 -978
- package/src/clearingHouseUserConfig.js +0 -2
- package/src/config.js +0 -67
- package/src/constants/banks.js +0 -42
- package/src/constants/banks.ts +0 -50
- package/src/constants/markets.js +0 -42
- package/src/constants/numericConstants.js +0 -41
- package/src/events/eventSubscriber.js +0 -139
- package/src/events/fetchLogs.js +0 -50
- package/src/events/pollingLogProvider.js +0 -64
- package/src/events/sort.js +0 -44
- package/src/events/txEventCache.js +0 -71
- package/src/events/types.js +0 -20
- package/src/events/webSocketLogProvider.js +0 -41
- package/src/factory/bigNum.js +0 -390
- package/src/factory/oracleClient.js +0 -20
- package/src/idl/clearing_house.json +0 -4464
- package/src/index.js +0 -69
- package/src/math/amm.js +0 -369
- package/src/math/auction.js +0 -42
- package/src/math/bankBalance.ts +0 -112
- package/src/math/conversion.js +0 -11
- package/src/math/funding.js +0 -248
- package/src/math/oracles.js +0 -26
- package/src/math/repeg.js +0 -128
- package/src/math/state.js +0 -15
- package/src/math/state.ts +0 -14
- package/src/math/trade.js +0 -253
- package/src/math/utils.js +0 -26
- package/src/math/utils.js.map +0 -1
- package/src/mockUSDCFaucet.js +0 -280
- package/src/oracles/oracleClientCache.js +0 -19
- package/src/oracles/pythClient.js +0 -46
- package/src/oracles/quoteAssetOracleClient.js +0 -32
- package/src/oracles/switchboardClient.js +0 -69
- package/src/orderParams.js +0 -20
- package/src/orders.ts +0 -245
- package/src/slot/SlotSubscriber.js +0 -39
- package/src/tokenFaucet.js +0 -189
- package/src/types.js +0 -125
- package/src/userName.js +0 -20
- package/src/wallet.js +0 -35
package/lib/factory/bigNum.js
CHANGED
|
@@ -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 =
|
|
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 (
|
|
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 (
|
|
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 =
|
|
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 =
|
|
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 =
|
|
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 =
|
|
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 =
|
|
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
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
345
|
-
const
|
|
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))
|