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