@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.
- package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +13 -13
- package/lib/accounts/pollingClearingHouseAccountSubscriber.js +27 -27
- package/lib/accounts/types.d.ts +8 -9
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +14 -14
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +35 -34
- package/lib/addresses/pda.d.ts +7 -6
- package/lib/addresses/pda.js +31 -27
- package/lib/admin.d.ts +9 -6
- package/lib/admin.js +83 -42
- package/lib/clearingHouse.d.ts +69 -42
- package/lib/clearingHouse.js +753 -277
- package/lib/clearingHouseConfig.d.ts +2 -2
- package/lib/clearingHouseUser.d.ts +16 -16
- package/lib/clearingHouseUser.js +139 -119
- package/lib/config.d.ts +7 -7
- package/lib/config.js +20 -20
- package/lib/constants/numericConstants.d.ts +12 -12
- package/lib/constants/numericConstants.js +13 -13
- package/lib/constants/{markets.d.ts → perpMarkets.d.ts} +5 -5
- package/{src/constants/markets.js → lib/constants/perpMarkets.js} +4 -4
- package/lib/constants/{banks.d.ts → spotMarkets.d.ts} +6 -6
- package/lib/constants/{banks.js → spotMarkets.js} +16 -16
- package/lib/events/types.d.ts +2 -1
- package/lib/events/types.js +1 -0
- package/lib/examples/makeTradeExample.js +7 -7
- package/lib/idl/clearing_house.json +1008 -279
- package/lib/index.d.ts +5 -3
- package/lib/index.js +5 -3
- package/lib/math/amm.d.ts +2 -2
- package/lib/math/amm.js +1 -1
- package/lib/math/funding.d.ts +6 -6
- package/lib/math/funding.js +2 -1
- package/lib/math/margin.d.ts +4 -4
- package/lib/math/margin.js +18 -11
- package/lib/math/market.d.ts +10 -9
- package/lib/math/market.js +29 -6
- package/lib/math/oracles.d.ts +2 -1
- package/lib/math/oracles.js +11 -1
- package/lib/math/orders.d.ts +5 -5
- package/lib/math/position.d.ts +13 -13
- package/lib/math/position.js +19 -19
- package/lib/math/spotBalance.d.ts +19 -0
- package/lib/math/spotBalance.js +176 -0
- package/lib/math/spotMarket.d.ts +4 -0
- package/lib/math/spotMarket.js +8 -0
- package/lib/math/spotPosition.d.ts +2 -0
- package/lib/math/spotPosition.js +8 -0
- package/lib/math/state.js +2 -2
- package/lib/math/trade.d.ts +4 -4
- package/lib/orderParams.d.ts +4 -4
- package/lib/orderParams.js +12 -4
- package/lib/serum/serumSubscriber.d.ts +23 -0
- package/lib/serum/serumSubscriber.js +41 -0
- package/lib/serum/types.d.ts +11 -0
- package/lib/serum/types.js +2 -0
- package/lib/tx/retryTxSender.d.ts +1 -1
- package/lib/tx/retryTxSender.js +4 -2
- package/lib/tx/types.d.ts +1 -1
- package/lib/types.d.ts +123 -33
- package/lib/types.js +31 -9
- package/my-script/.env +7 -0
- package/my-script/getUserStats.ts +106 -0
- package/my-script/multiConnections.ts +119 -0
- package/my-script/test-regex.ts +11 -0
- package/my-script/utils.ts +52 -0
- package/package.json +1 -1
- package/src/accounts/bulkAccountLoader.js +249 -0
- package/src/accounts/bulkUserStatsSubscription.js +75 -0
- package/src/accounts/bulkUserSubscription.js +75 -0
- package/src/accounts/fetch.js +92 -0
- package/src/accounts/pollingClearingHouseAccountSubscriber.js +465 -0
- package/src/accounts/pollingClearingHouseAccountSubscriber.ts +38 -38
- package/src/accounts/pollingOracleSubscriber.js +156 -0
- package/src/accounts/pollingTokenAccountSubscriber.js +141 -0
- package/src/accounts/pollingUserAccountSubscriber.js +208 -0
- package/src/accounts/pollingUserStatsAccountSubscriber.js +208 -0
- package/src/accounts/types.js +28 -0
- package/src/accounts/types.ts +11 -9
- package/src/accounts/utils.js +7 -0
- package/src/accounts/webSocketAccountSubscriber.js +138 -0
- package/src/accounts/webSocketClearingHouseAccountSubscriber.js +433 -0
- package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +59 -52
- package/src/accounts/webSocketUserAccountSubscriber.js +113 -0
- package/src/accounts/webSocketUserStatsAccountSubsriber.js +113 -0
- package/src/addresses/pda.js +186 -0
- package/src/addresses/pda.ts +49 -44
- package/src/admin.js +1284 -0
- package/src/admin.ts +140 -47
- package/src/assert/assert.js +1 -1
- package/src/clearingHouse.js +3433 -0
- package/src/clearingHouse.ts +1083 -378
- package/src/clearingHouseConfig.js +2 -0
- package/src/clearingHouseConfig.ts +2 -2
- package/src/clearingHouseUser.js +874 -0
- package/src/clearingHouseUser.ts +232 -168
- package/src/clearingHouseUserConfig.js +2 -0
- package/src/clearingHouseUserStats.js +115 -0
- package/src/clearingHouseUserStatsConfig.js +2 -0
- package/src/config.js +80 -0
- package/src/config.ts +29 -29
- package/src/constants/numericConstants.js +18 -11
- package/src/constants/numericConstants.ts +17 -15
- package/{lib/constants/markets.js → src/constants/perpMarkets.js} +11 -11
- package/src/constants/{markets.ts → perpMarkets.ts} +5 -5
- package/src/constants/spotMarkets.js +51 -0
- package/src/constants/{banks.ts → spotMarkets.ts} +19 -19
- package/src/events/eventList.js +66 -23
- package/src/events/eventSubscriber.js +202 -0
- package/src/events/fetchLogs.js +117 -0
- package/src/events/pollingLogProvider.js +113 -0
- package/src/events/sort.js +41 -0
- package/src/events/txEventCache.js +22 -19
- package/src/events/types.js +25 -0
- package/src/events/types.ts +3 -0
- package/src/events/webSocketLogProvider.js +76 -0
- package/src/examples/makeTradeExample.ts +10 -8
- package/src/factory/bigNum.js +183 -180
- package/src/factory/oracleClient.js +9 -9
- package/src/idl/clearing_house.json +1008 -279
- package/src/index.js +75 -0
- package/src/index.ts +5 -3
- package/src/math/amm.js +422 -0
- package/src/math/amm.ts +6 -3
- package/src/math/auction.js +10 -10
- package/src/math/conversion.js +4 -3
- package/src/math/funding.js +223 -175
- package/src/math/funding.ts +7 -7
- package/src/math/insurance.js +27 -0
- package/src/math/margin.js +77 -0
- package/src/math/margin.ts +34 -23
- package/src/math/market.js +105 -0
- package/src/math/market.ts +71 -19
- package/src/math/oracles.js +40 -10
- package/src/math/oracles.ts +18 -1
- package/src/math/orders.js +153 -0
- package/src/math/orders.ts +5 -5
- package/src/math/position.js +172 -0
- package/src/math/position.ts +31 -31
- package/src/math/repeg.js +40 -40
- package/src/math/spotBalance.js +176 -0
- package/src/math/spotBalance.ts +290 -0
- package/src/math/spotMarket.js +8 -0
- package/src/math/spotMarket.ts +9 -0
- package/src/math/spotPosition.js +8 -0
- package/src/math/spotPosition.ts +6 -0
- package/src/math/state.ts +2 -2
- package/src/math/trade.js +81 -74
- package/src/math/trade.ts +4 -4
- package/src/math/utils.js +8 -7
- package/src/oracles/oracleClientCache.js +10 -9
- package/src/oracles/pythClient.js +52 -17
- package/src/oracles/quoteAssetOracleClient.js +44 -13
- package/src/oracles/switchboardClient.js +69 -37
- package/src/oracles/types.js +1 -1
- package/src/orderParams.js +14 -6
- package/src/orderParams.ts +16 -8
- package/src/serum/serumSubscriber.js +102 -0
- package/src/serum/serumSubscriber.ts +80 -0
- package/src/serum/types.js +2 -0
- package/src/serum/types.ts +13 -0
- package/src/slot/SlotSubscriber.js +67 -20
- package/src/token/index.js +4 -4
- package/src/tokenFaucet.js +288 -154
- package/src/tx/retryTxSender.js +280 -0
- package/src/tx/retryTxSender.ts +5 -2
- package/src/tx/types.js +1 -1
- package/src/tx/types.ts +2 -1
- package/src/tx/utils.js +7 -6
- package/src/types.js +216 -0
- package/src/types.ts +110 -33
- package/src/userName.js +5 -5
- package/src/util/computeUnits.js +46 -11
- package/src/util/promiseTimeout.js +5 -5
- package/src/util/tps.js +46 -12
- package/src/wallet.js +55 -18
- package/lib/math/bankBalance.d.ts +0 -15
- package/lib/math/bankBalance.js +0 -150
- package/src/addresses/marketAddresses.js +0 -26
- package/src/constants/banks.js +0 -42
- package/src/examples/makeTradeExample.js +0 -80
- package/src/math/bankBalance.ts +0 -258
- package/src/math/state.js +0 -15
- package/src/math/utils.js.map +0 -1
- package/src/util/getTokenAddress.js +0 -9
package/src/factory/bigNum.js
CHANGED
|
@@ -1,149 +1,161 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
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
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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
|
-
|
|
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
|
|
50
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
141
|
-
|
|
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
|
-
|
|
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 (
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
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
|
-
|
|
170
|
-
|
|
171
|
-
|
|
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
|
-
|
|
184
|
+
var index = formattedLeftSide.length - 3;
|
|
176
185
|
while (index >= 1) {
|
|
177
|
-
|
|
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
|
|
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
|
-
|
|
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(
|
|
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
|
-
|
|
206
|
-
|
|
207
|
-
|
|
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
|
|
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
|
|
221
|
-
|
|
222
|
-
|
|
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
|
-
|
|
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
|
-
|
|
236
|
-
|
|
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
|
-
|
|
253
|
+
var exponent = delimFullStringLocation - fixedPrecision;
|
|
251
254
|
if (trailingZeroes) {
|
|
252
|
-
precisionPrintString =
|
|
255
|
+
precisionPrintString = "".concat(precisionPrintString).concat(Array(exponent)
|
|
253
256
|
.fill('0')
|
|
254
|
-
.join('')
|
|
257
|
+
.join(''));
|
|
255
258
|
}
|
|
256
259
|
else {
|
|
257
|
-
precisionPrintString =
|
|
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
|
-
|
|
275
|
-
|
|
276
|
-
|
|
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
|
-
|
|
282
|
-
|
|
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
|
-
|
|
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 =
|
|
290
|
+
val = "".concat(val, ".00");
|
|
288
291
|
}
|
|
289
292
|
else {
|
|
290
|
-
val =
|
|
293
|
+
val = "".concat(val).concat(new Array(2 - trailingLength).fill('0').join(''));
|
|
291
294
|
}
|
|
292
295
|
}
|
|
293
296
|
}
|
|
294
|
-
return
|
|
295
|
-
}
|
|
296
|
-
toMillified
|
|
297
|
-
|
|
298
|
-
|
|
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
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
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
|
-
|
|
316
|
-
|
|
316
|
+
var decimalLocation = leftSide.length - 3 * unitNumber;
|
|
317
|
+
var leadString = '';
|
|
317
318
|
if (decimalLocation >= precision) {
|
|
318
|
-
leadString =
|
|
319
|
+
leadString = "".concat(leadDigits);
|
|
319
320
|
}
|
|
320
321
|
else {
|
|
321
|
-
leadString =
|
|
322
|
+
leadString = "".concat(leadDigits.slice(0, decimalLocation)).concat(BigNum.delim).concat(leadDigits.slice(decimalLocation));
|
|
322
323
|
}
|
|
323
|
-
return
|
|
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
|
-
|
|
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
|
-
|
|
354
|
-
|
|
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
|
-
|
|
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
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
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
|
-
|
|
379
|
+
};
|
|
380
|
+
BigNum.max = function (a, b) {
|
|
379
381
|
return a.gt(b) ? a : b;
|
|
380
|
-
}
|
|
381
|
-
|
|
382
|
+
};
|
|
383
|
+
BigNum.min = function (a, b) {
|
|
382
384
|
return a.lt(b) ? a : b;
|
|
383
|
-
}
|
|
384
|
-
|
|
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
|
-
|
|
2
|
+
exports.__esModule = true;
|
|
3
3
|
exports.getOracleClient = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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(
|
|
18
|
+
throw new Error("Unknown oracle source ".concat(oracleSource));
|
|
19
19
|
}
|
|
20
20
|
exports.getOracleClient = getOracleClient;
|