@drift-labs/sdk 0.1.36-master.7 → 0.2.0-master.10
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/bulkAccountLoader.d.ts +6 -6
- package/lib/accounts/bulkAccountLoader.js +81 -95
- package/lib/accounts/bulkUserSubscription.js +13 -57
- package/lib/accounts/fetch.d.ts +4 -0
- package/lib/accounts/fetch.js +18 -0
- package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +34 -38
- package/lib/accounts/pollingClearingHouseAccountSubscriber.js +224 -224
- package/lib/accounts/pollingOracleSubscriber.d.ts +3 -3
- package/lib/accounts/pollingOracleSubscriber.js +37 -49
- package/lib/accounts/pollingTokenAccountSubscriber.d.ts +3 -3
- package/lib/accounts/pollingTokenAccountSubscriber.js +35 -50
- package/lib/accounts/pollingUserAccountSubscriber.d.ts +7 -13
- package/lib/accounts/pollingUserAccountSubscriber.js +71 -134
- package/lib/accounts/types.d.ts +34 -41
- package/lib/accounts/webSocketAccountSubscriber.d.ts +6 -4
- package/lib/accounts/webSocketAccountSubscriber.js +39 -35
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +33 -28
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +153 -185
- package/lib/accounts/webSocketUserAccountSubscriber.d.ts +5 -11
- package/lib/accounts/webSocketUserAccountSubscriber.js +22 -67
- package/lib/addresses/marketAddresses.d.ts +4 -0
- package/lib/addresses/marketAddresses.js +15 -0
- package/lib/addresses/pda.d.ts +12 -0
- package/lib/addresses/pda.js +83 -0
- package/lib/admin.d.ts +12 -18
- package/lib/admin.js +398 -558
- package/lib/clearingHouse.d.ts +83 -109
- package/lib/clearingHouse.js +832 -893
- package/lib/clearingHouseConfig.d.ts +34 -0
- package/lib/clearingHouseConfig.js +2 -0
- package/lib/clearingHouseUser.d.ts +19 -22
- package/lib/clearingHouseUser.js +155 -141
- package/lib/clearingHouseUserConfig.d.ts +14 -0
- package/lib/clearingHouseUserConfig.js +2 -0
- package/lib/config.d.ts +12 -0
- package/lib/config.js +35 -4
- package/lib/constants/banks.d.ts +16 -0
- package/lib/constants/banks.js +41 -0
- package/lib/constants/markets.d.ts +8 -3
- package/lib/constants/markets.js +13 -206
- package/lib/constants/numericConstants.d.ts +17 -0
- package/lib/constants/numericConstants.js +23 -6
- package/lib/events/eventList.d.ts +22 -0
- package/lib/events/eventList.js +77 -0
- package/lib/events/eventSubscriber.d.ts +34 -0
- package/lib/events/eventSubscriber.js +126 -0
- package/lib/events/fetchLogs.d.ts +13 -0
- package/lib/events/fetchLogs.js +39 -0
- package/lib/events/pollingLogProvider.d.ts +15 -0
- package/lib/events/pollingLogProvider.js +53 -0
- package/lib/events/sort.d.ts +2 -0
- package/lib/events/sort.js +44 -0
- package/lib/events/txEventCache.d.ts +24 -0
- package/lib/events/txEventCache.js +71 -0
- package/lib/events/types.d.ts +49 -0
- package/lib/events/types.js +20 -0
- package/lib/events/webSocketLogProvider.d.ts +12 -0
- package/lib/events/webSocketLogProvider.js +30 -0
- package/lib/examples/makeTradeExample.js +26 -27
- package/lib/factory/bigNum.d.ts +118 -0
- package/lib/factory/bigNum.js +364 -0
- package/lib/factory/oracleClient.d.ts +1 -2
- package/lib/factory/oracleClient.js +6 -2
- package/lib/idl/clearing_house.json +2258 -2774
- package/lib/index.d.ts +14 -5
- package/lib/index.js +18 -5
- package/lib/math/amm.d.ts +24 -29
- package/lib/math/amm.js +209 -176
- package/lib/math/auction.d.ts +5 -0
- package/lib/math/auction.js +42 -0
- package/lib/math/bankBalance.d.ts +9 -0
- package/lib/math/bankBalance.js +75 -0
- package/lib/math/conversion.d.ts +0 -1
- package/lib/math/conversion.js +1 -5
- package/lib/math/funding.d.ts +6 -6
- package/lib/math/funding.js +158 -175
- package/lib/math/market.d.ts +6 -6
- package/lib/math/market.js +10 -9
- package/lib/math/orders.d.ts +6 -1
- package/lib/math/orders.js +38 -1
- package/lib/math/position.d.ts +7 -5
- package/lib/math/position.js +29 -27
- package/lib/math/repeg.d.ts +22 -0
- package/lib/math/repeg.js +128 -0
- package/lib/math/trade.d.ts +5 -4
- package/lib/math/trade.js +29 -24
- package/lib/mockUSDCFaucet.js +87 -116
- package/lib/oracles/oracleClientCache.d.ts +8 -0
- package/lib/oracles/oracleClientCache.js +19 -0
- package/lib/oracles/pythClient.d.ts +3 -5
- package/lib/oracles/pythClient.js +12 -31
- package/lib/oracles/quoteAssetOracleClient.d.ts +9 -0
- package/lib/oracles/quoteAssetOracleClient.js +21 -0
- package/lib/oracles/switchboardClient.d.ts +3 -5
- package/lib/oracles/switchboardClient.js +29 -50
- package/lib/oracles/types.d.ts +6 -1
- package/lib/orderParams.d.ts +14 -5
- package/lib/orderParams.js +8 -96
- package/lib/orders.d.ts +6 -7
- package/lib/orders.js +11 -89
- package/lib/slot/SlotSubscriber.d.ts +19 -0
- package/lib/slot/SlotSubscriber.js +26 -0
- package/lib/tx/retryTxSender.d.ts +2 -2
- package/lib/tx/retryTxSender.js +108 -123
- package/lib/tx/types.d.ts +5 -1
- package/lib/tx/utils.d.ts +1 -1
- package/lib/tx/utils.js +11 -2
- package/lib/types.d.ts +180 -110
- package/lib/types.js +54 -1
- package/lib/userName.d.ts +4 -0
- package/lib/userName.js +20 -0
- package/lib/util/computeUnits.js +10 -21
- package/lib/util/tps.js +11 -22
- package/lib/wallet.js +7 -20
- package/package.json +11 -4
- package/src/accounts/bulkAccountLoader.js +197 -0
- package/src/accounts/bulkAccountLoader.ts +21 -15
- package/src/accounts/bulkUserSubscription.js +33 -0
- package/src/accounts/bulkUserSubscription.ts +1 -45
- package/src/accounts/fetch.js +29 -0
- package/src/accounts/fetch.ts +33 -0
- package/src/accounts/pollingClearingHouseAccountSubscriber.js +311 -0
- package/src/accounts/pollingClearingHouseAccountSubscriber.ts +249 -214
- package/src/accounts/pollingOracleSubscriber.js +93 -0
- package/src/accounts/pollingOracleSubscriber.ts +16 -8
- package/src/accounts/pollingTokenAccountSubscriber.js +90 -0
- package/src/accounts/pollingTokenAccountSubscriber.ts +11 -8
- package/src/accounts/pollingUserAccountSubscriber.js +132 -0
- package/src/accounts/pollingUserAccountSubscriber.ts +21 -86
- package/src/accounts/types.js +10 -0
- package/src/accounts/types.ts +41 -70
- package/src/accounts/utils.js +7 -0
- package/src/accounts/webSocketAccountSubscriber.js +93 -0
- package/src/accounts/webSocketAccountSubscriber.ts +33 -16
- package/src/accounts/webSocketClearingHouseAccountSubscriber.js +233 -0
- package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +190 -240
- package/src/accounts/webSocketUserAccountSubscriber.js +62 -0
- package/src/accounts/webSocketUserAccountSubscriber.ts +11 -71
- package/src/addresses/marketAddresses.js +26 -0
- package/src/addresses/marketAddresses.ts +18 -0
- package/{lib/addresses.js → src/addresses/pda.js} +45 -34
- package/src/addresses/pda.ts +118 -0
- package/src/admin.ts +247 -336
- package/src/assert/assert.js +9 -0
- package/src/clearingHouse.ts +1023 -1026
- package/src/clearingHouseConfig.ts +37 -0
- package/src/clearingHouseUser.ts +275 -185
- package/src/clearingHouseUserConfig.ts +18 -0
- package/src/config.ts +54 -1
- package/src/constants/banks.ts +50 -0
- package/src/constants/markets.ts +16 -207
- package/src/constants/numericConstants.ts +34 -5
- package/src/events/eventList.js +77 -0
- package/src/events/eventList.ts +94 -0
- package/src/events/eventSubscriber.js +139 -0
- package/src/events/eventSubscriber.ts +194 -0
- package/src/events/fetchLogs.js +50 -0
- package/src/events/fetchLogs.ts +80 -0
- package/src/events/pollingLogProvider.js +64 -0
- package/src/events/pollingLogProvider.ts +79 -0
- package/src/events/sort.js +44 -0
- package/src/events/sort.ts +65 -0
- package/src/events/txEventCache.js +71 -0
- package/src/events/txEventCache.ts +74 -0
- package/src/events/types.js +20 -0
- package/src/events/types.ts +98 -0
- package/src/events/webSocketLogProvider.js +41 -0
- package/src/events/webSocketLogProvider.ts +38 -0
- package/src/examples/makeTradeExample.js +80 -0
- package/src/examples/makeTradeExample.ts +20 -11
- package/src/factory/bigNum.js +364 -0
- package/src/factory/bigNum.ts +524 -0
- package/src/factory/oracleClient.js +20 -0
- package/src/factory/oracleClient.ts +7 -4
- package/src/idl/clearing_house.json +2258 -2774
- package/src/index.js +69 -0
- package/src/index.ts +14 -5
- package/src/math/amm.js +369 -0
- package/src/math/amm.ts +382 -243
- package/src/math/auction.js +42 -0
- package/src/math/auction.ts +43 -0
- package/src/math/bankBalance.js +75 -0
- package/src/math/bankBalance.ts +112 -0
- package/src/math/conversion.js +11 -0
- package/src/math/conversion.ts +1 -11
- package/src/math/funding.js +248 -0
- package/src/math/funding.ts +12 -9
- package/src/math/market.js +57 -0
- package/src/math/market.ts +37 -30
- package/src/math/oracles.js +26 -0
- package/src/math/orders.js +110 -0
- package/src/math/orders.ts +43 -1
- package/src/math/position.js +140 -0
- package/src/math/position.ts +52 -35
- package/src/math/repeg.js +128 -0
- package/src/math/repeg.ts +176 -0
- package/src/math/state.js +15 -0
- package/src/math/trade.js +253 -0
- package/src/math/trade.ts +55 -47
- package/src/math/utils.js +26 -0
- package/src/math/utils.js.map +1 -0
- package/src/mockUSDCFaucet.js +171 -0
- package/src/oracles/oracleClientCache.js +19 -0
- package/src/oracles/oracleClientCache.ts +20 -0
- package/src/oracles/pythClient.js +46 -0
- package/src/oracles/pythClient.ts +4 -11
- package/src/oracles/quoteAssetOracleClient.js +32 -0
- package/src/oracles/quoteAssetOracleClient.ts +25 -0
- package/src/oracles/switchboardClient.js +69 -0
- package/src/oracles/switchboardClient.ts +11 -24
- package/src/oracles/types.js +2 -0
- package/src/oracles/types.ts +7 -1
- package/src/orderParams.js +20 -0
- package/src/orderParams.ts +20 -141
- package/src/orders.js +134 -0
- package/src/orders.ts +25 -134
- package/src/slot/SlotSubscriber.js +39 -0
- package/src/slot/SlotSubscriber.ts +42 -0
- package/src/token/index.js +38 -0
- package/src/tx/retryTxSender.js +188 -0
- package/src/tx/retryTxSender.ts +6 -4
- package/src/tx/types.js +2 -0
- package/src/tx/types.ts +6 -1
- package/src/tx/utils.js +17 -0
- package/src/tx/utils.ts +22 -3
- package/src/types.js +114 -0
- package/src/types.ts +176 -124
- package/src/userName.js +20 -0
- package/src/userName.ts +20 -0
- package/src/util/promiseTimeout.js +14 -0
- package/src/util/tps.js +27 -0
- package/src/wallet.js +35 -0
- package/tests/bn/test.ts +255 -0
- package/tsconfig.json +12 -12
- package/lib/addresses.d.ts +0 -10
- package/lib/constants/accounts.d.ts +0 -15
- package/lib/constants/accounts.js +0 -18
- package/lib/factory/clearingHouse.d.ts +0 -35
- package/lib/factory/clearingHouse.js +0 -81
- package/lib/factory/clearingHouseUser.d.ts +0 -19
- package/lib/factory/clearingHouseUser.js +0 -34
- package/lib/math/insuranceFund.d.ts +0 -15
- package/lib/math/insuranceFund.js +0 -33
- package/lib/settlement.d.ts +0 -4
- package/lib/settlement.js +0 -10
- package/lib/tx/defaultTxSender.d.ts +0 -8
- package/lib/tx/defaultTxSender.js +0 -12
- package/src/addresses.ts +0 -82
- package/src/constants/accounts.ts +0 -26
- package/src/factory/clearingHouse.ts +0 -173
- package/src/factory/clearingHouseUser.ts +0 -73
- package/src/math/insuranceFund.ts +0 -29
- package/src/settlement.ts +0 -9
- package/src/tx/defaultTxSender.ts +0 -24
|
@@ -0,0 +1,524 @@
|
|
|
1
|
+
import { BN } from '@project-serum/anchor';
|
|
2
|
+
import { assert } from '../assert/assert';
|
|
3
|
+
import { ZERO } from './../constants/numericConstants';
|
|
4
|
+
|
|
5
|
+
export class BigNum {
|
|
6
|
+
val: BN;
|
|
7
|
+
precision: BN;
|
|
8
|
+
|
|
9
|
+
static delim = '.';
|
|
10
|
+
static spacer = ',';
|
|
11
|
+
|
|
12
|
+
constructor(
|
|
13
|
+
val: BN | number | string,
|
|
14
|
+
precisionVal: BN | number | string = new BN(0)
|
|
15
|
+
) {
|
|
16
|
+
this.val = new BN(val);
|
|
17
|
+
this.precision = new BN(precisionVal);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
public add(bn: BigNum): BigNum {
|
|
21
|
+
assert(bn.precision.eq(this.precision), 'Adding unequal precisions');
|
|
22
|
+
|
|
23
|
+
return BigNum.from(this.val.add(bn.val), this.precision);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
public sub(bn: BigNum): BigNum {
|
|
27
|
+
assert(bn.precision.eq(this.precision), 'Subtracting unequal precisions');
|
|
28
|
+
|
|
29
|
+
return BigNum.from(this.val.sub(bn.val), this.precision);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
public mul(bn: BigNum | BN): BigNum {
|
|
33
|
+
const mulVal = bn instanceof BigNum ? bn : BigNum.from(bn);
|
|
34
|
+
|
|
35
|
+
return BigNum.from(
|
|
36
|
+
this.val.mul(mulVal.val),
|
|
37
|
+
this.precision.add(mulVal.precision)
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
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
|
|
43
|
+
* @param bn
|
|
44
|
+
* @returns
|
|
45
|
+
*/
|
|
46
|
+
public scalarMul(bn: BigNum | BN): BigNum {
|
|
47
|
+
if (bn instanceof BN) return BigNum.from(this.val.mul(bn), this.precision);
|
|
48
|
+
|
|
49
|
+
return BigNum.from(
|
|
50
|
+
this.val.mul(bn.val),
|
|
51
|
+
this.precision.add(bn.precision)
|
|
52
|
+
).shift(bn.precision.neg());
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
public div(bn: BigNum | BN): BigNum {
|
|
56
|
+
if (bn instanceof BN) return BigNum.from(this.val.div(bn), this.precision);
|
|
57
|
+
return BigNum.from(this.val.div(bn.val), this.precision.sub(bn.precision));
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Shift precision up or down
|
|
62
|
+
* @param exponent
|
|
63
|
+
* @param skipAdjustingPrecision
|
|
64
|
+
* @returns
|
|
65
|
+
*/
|
|
66
|
+
public shift(exponent: BN | number, skipAdjustingPrecision = false): BigNum {
|
|
67
|
+
const shiftVal = typeof exponent === 'number' ? new BN(exponent) : exponent;
|
|
68
|
+
|
|
69
|
+
return BigNum.from(
|
|
70
|
+
shiftVal.isNeg()
|
|
71
|
+
? this.val.div(new BN(10).pow(shiftVal))
|
|
72
|
+
: this.val.mul(new BN(10).pow(shiftVal)),
|
|
73
|
+
skipAdjustingPrecision ? this.precision : this.precision.add(shiftVal)
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Shift to a target precision
|
|
79
|
+
* @param targetPrecision
|
|
80
|
+
* @returns
|
|
81
|
+
*/
|
|
82
|
+
public shiftTo(targetPrecision: BN): BigNum {
|
|
83
|
+
return this.shift(targetPrecision.sub(this.precision));
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Scale the number by a fraction
|
|
88
|
+
* @param numerator
|
|
89
|
+
* @param denominator
|
|
90
|
+
* @returns
|
|
91
|
+
*/
|
|
92
|
+
public scale(numerator: BN | number, denominator: BN | number): BigNum {
|
|
93
|
+
return this.mul(BigNum.from(new BN(numerator))).div(new BN(denominator));
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
public toPercentage(denominator: BigNum, precision: number): string {
|
|
97
|
+
return this.shift(precision)
|
|
98
|
+
.shift(2, true)
|
|
99
|
+
.div(denominator)
|
|
100
|
+
.toPrecision(precision);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
public gt(bn: BigNum | BN, ignorePrecision?: boolean): boolean {
|
|
104
|
+
const comparisonVal = bn instanceof BigNum ? bn : BigNum.from(bn);
|
|
105
|
+
|
|
106
|
+
if (!ignorePrecision && !comparisonVal.eq(ZERO)) {
|
|
107
|
+
assert(
|
|
108
|
+
comparisonVal.precision.eq(this.precision),
|
|
109
|
+
'Trying to compare numbers with different precision. Yo can opt to ignore precision using the ignorePrecision parameter'
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return this.val.gt(comparisonVal.val);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
public lt(bn: BigNum | BN, ignorePrecision?: boolean): boolean {
|
|
117
|
+
const comparisonVal = bn instanceof BigNum ? bn : BigNum.from(bn);
|
|
118
|
+
|
|
119
|
+
if (!ignorePrecision && !comparisonVal.val.eq(ZERO)) {
|
|
120
|
+
assert(
|
|
121
|
+
comparisonVal.precision.eq(this.precision),
|
|
122
|
+
'Trying to compare numbers with different precision. Yo can opt to ignore precision using the ignorePrecision parameter'
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return this.val.lt(comparisonVal.val);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
public gte(bn: BigNum | BN, ignorePrecision?: boolean): boolean {
|
|
130
|
+
const comparisonVal = bn instanceof BigNum ? bn : BigNum.from(bn);
|
|
131
|
+
|
|
132
|
+
if (!ignorePrecision && !comparisonVal.val.eq(ZERO)) {
|
|
133
|
+
assert(
|
|
134
|
+
comparisonVal.precision.eq(this.precision),
|
|
135
|
+
'Trying to compare numbers with different precision. Yo can opt to ignore precision using the ignorePrecision parameter'
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return this.val.gte(comparisonVal.val);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
public lte(bn: BigNum | BN, ignorePrecision?: boolean): boolean {
|
|
143
|
+
const comparisonVal = bn instanceof BigNum ? bn : BigNum.from(bn);
|
|
144
|
+
|
|
145
|
+
if (!ignorePrecision && !comparisonVal.val.eq(ZERO)) {
|
|
146
|
+
assert(
|
|
147
|
+
comparisonVal.precision.eq(this.precision),
|
|
148
|
+
'Trying to compare numbers with different precision. Yo can opt to ignore precision using the ignorePrecision parameter'
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return this.val.lte(comparisonVal.val);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
public eq(bn: BigNum | BN, ignorePrecision?: boolean): boolean {
|
|
156
|
+
const comparisonVal = bn instanceof BigNum ? bn : BigNum.from(bn);
|
|
157
|
+
|
|
158
|
+
if (!ignorePrecision && !comparisonVal.val.eq(ZERO)) {
|
|
159
|
+
assert(
|
|
160
|
+
comparisonVal.precision.eq(this.precision),
|
|
161
|
+
'Trying to compare numbers with different precision. Yo can opt to ignore precision using the ignorePrecision parameter'
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
return this.val.eq(comparisonVal.val);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
public eqZero() {
|
|
169
|
+
return this.val.eq(ZERO);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
public gtZero() {
|
|
173
|
+
return this.val.gt(ZERO);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
public ltZero() {
|
|
177
|
+
return this.val.lt(ZERO);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
public gteZero() {
|
|
181
|
+
return this.val.gte(ZERO);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
public lteZero() {
|
|
185
|
+
return this.val.lte(ZERO);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
public abs(): BigNum {
|
|
189
|
+
return new BigNum(this.val.abs(), this.precision);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
public neg(): BigNum {
|
|
193
|
+
return new BigNum(this.val.neg(), this.precision);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
public toString = (base?: number | 'hex', length?: number): string =>
|
|
197
|
+
this.val.toString(base, length);
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Pretty print the underlying value in human-readable form. Depends on precision being correct for the output string to be correct
|
|
201
|
+
* @returns
|
|
202
|
+
*/
|
|
203
|
+
public print(): string {
|
|
204
|
+
assert(
|
|
205
|
+
this.precision.gte(ZERO),
|
|
206
|
+
'Tried to print a BN with precision lower than zero'
|
|
207
|
+
);
|
|
208
|
+
|
|
209
|
+
const plainString = this.toString();
|
|
210
|
+
const precisionNum = this.precision.toNumber();
|
|
211
|
+
|
|
212
|
+
// make a string with at least the precisionNum number of zeroes
|
|
213
|
+
let printString = [
|
|
214
|
+
...Array(this.precision.toNumber()).fill(0),
|
|
215
|
+
...plainString.split(''),
|
|
216
|
+
].join('');
|
|
217
|
+
|
|
218
|
+
// inject decimal
|
|
219
|
+
printString =
|
|
220
|
+
printString.substring(0, printString.length - precisionNum) +
|
|
221
|
+
BigNum.delim +
|
|
222
|
+
printString.substring(printString.length - precisionNum);
|
|
223
|
+
|
|
224
|
+
// remove leading zeroes
|
|
225
|
+
printString = printString.replace(/^0+/, '');
|
|
226
|
+
|
|
227
|
+
// add zero if leading delim
|
|
228
|
+
if (printString[0] === BigNum.delim) printString = `0${printString}`;
|
|
229
|
+
|
|
230
|
+
// remove trailing delim
|
|
231
|
+
if (printString[printString.length - 1] === BigNum.delim)
|
|
232
|
+
printString = printString.slice(0, printString.length - 1);
|
|
233
|
+
|
|
234
|
+
return printString;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
public prettyPrint(
|
|
238
|
+
useTradePrecision?: boolean,
|
|
239
|
+
precisionOverride?: number
|
|
240
|
+
): string {
|
|
241
|
+
const [leftSide, rightSide] = this.printShort(
|
|
242
|
+
useTradePrecision,
|
|
243
|
+
precisionOverride
|
|
244
|
+
).split(BigNum.delim);
|
|
245
|
+
|
|
246
|
+
let formattedLeftSide = leftSide;
|
|
247
|
+
|
|
248
|
+
const isNeg = formattedLeftSide.includes('-');
|
|
249
|
+
if (isNeg) {
|
|
250
|
+
formattedLeftSide = formattedLeftSide.replace('-', '');
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
let index = formattedLeftSide.length - 3;
|
|
254
|
+
|
|
255
|
+
while (index >= 1) {
|
|
256
|
+
const formattedLeftSideArray = formattedLeftSide.split('');
|
|
257
|
+
|
|
258
|
+
formattedLeftSideArray.splice(index, 0, BigNum.spacer);
|
|
259
|
+
|
|
260
|
+
formattedLeftSide = formattedLeftSideArray.join('');
|
|
261
|
+
|
|
262
|
+
index -= 3;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
return `${isNeg ? '-' : ''}${formattedLeftSide}${
|
|
266
|
+
rightSide ? `${BigNum.delim}${rightSide}` : ''
|
|
267
|
+
}`;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Print and remove unnecessary trailing zeroes
|
|
272
|
+
* @returns
|
|
273
|
+
*/
|
|
274
|
+
public printShort(
|
|
275
|
+
useTradePrecision?: boolean,
|
|
276
|
+
precisionOverride?: number
|
|
277
|
+
): string {
|
|
278
|
+
const printVal = precisionOverride
|
|
279
|
+
? this.toPrecision(precisionOverride)
|
|
280
|
+
: useTradePrecision
|
|
281
|
+
? this.toTradePrecision()
|
|
282
|
+
: this.print();
|
|
283
|
+
|
|
284
|
+
return printVal.replace(/0+$/g, '').replace(/\.$/, '').replace(/,$/, '');
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
public debug() {
|
|
288
|
+
console.log(
|
|
289
|
+
`${this.toString()} | ${this.print()} | ${this.precision.toString()}`
|
|
290
|
+
);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* Pretty print with the specified number of decimal places
|
|
295
|
+
* @param fixedPrecision
|
|
296
|
+
* @returns
|
|
297
|
+
*/
|
|
298
|
+
public toFixed(fixedPrecision: number): string {
|
|
299
|
+
const printString = this.print();
|
|
300
|
+
|
|
301
|
+
const [leftSide, rightSide] = printString.split(BigNum.delim);
|
|
302
|
+
|
|
303
|
+
const filledRightSide = [
|
|
304
|
+
...(rightSide ?? '').slice(0, fixedPrecision),
|
|
305
|
+
...Array(fixedPrecision).fill('0'),
|
|
306
|
+
]
|
|
307
|
+
.slice(0, fixedPrecision)
|
|
308
|
+
.join('');
|
|
309
|
+
|
|
310
|
+
return `${leftSide}${BigNum.delim}${filledRightSide}`;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* Pretty print to the specified number of significant figures
|
|
315
|
+
* @param fixedPrecision
|
|
316
|
+
* @returns
|
|
317
|
+
*/
|
|
318
|
+
public toPrecision(fixedPrecision: number, trailingZeroes = false): string {
|
|
319
|
+
const printString = this.print();
|
|
320
|
+
|
|
321
|
+
let precisionPrintString = printString.slice(0, fixedPrecision + 1);
|
|
322
|
+
|
|
323
|
+
if (
|
|
324
|
+
!precisionPrintString.includes(BigNum.delim) ||
|
|
325
|
+
precisionPrintString[precisionPrintString.length - 1] === BigNum.delim
|
|
326
|
+
) {
|
|
327
|
+
precisionPrintString = printString.slice(0, fixedPrecision);
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
const pointsOfPrecision = precisionPrintString.replace(
|
|
331
|
+
BigNum.delim,
|
|
332
|
+
''
|
|
333
|
+
).length;
|
|
334
|
+
|
|
335
|
+
if (pointsOfPrecision < fixedPrecision) {
|
|
336
|
+
precisionPrintString = [
|
|
337
|
+
...precisionPrintString.split(''),
|
|
338
|
+
...Array(fixedPrecision - pointsOfPrecision).fill('0'),
|
|
339
|
+
].join('');
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
if (!precisionPrintString.includes(BigNum.delim)) {
|
|
343
|
+
const delimFullStringLocation = printString.indexOf(BigNum.delim);
|
|
344
|
+
|
|
345
|
+
let skipExponent = false;
|
|
346
|
+
|
|
347
|
+
if (delimFullStringLocation === -1) {
|
|
348
|
+
// no decimal, not missing any precision
|
|
349
|
+
skipExponent = true;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
if (
|
|
353
|
+
precisionPrintString[precisionPrintString.length - 1] === BigNum.delim
|
|
354
|
+
) {
|
|
355
|
+
// decimal is at end of string, not missing any precision, do nothing
|
|
356
|
+
skipExponent = true;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
if (printString.indexOf(BigNum.delim) === fixedPrecision) {
|
|
360
|
+
// decimal is at end of string, not missing any precision, do nothing
|
|
361
|
+
skipExponent = true;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
if (!skipExponent) {
|
|
365
|
+
const exponent = delimFullStringLocation - fixedPrecision;
|
|
366
|
+
if (trailingZeroes) {
|
|
367
|
+
precisionPrintString = `${precisionPrintString}${Array(exponent)
|
|
368
|
+
.fill('0')
|
|
369
|
+
.join('')}`;
|
|
370
|
+
} else {
|
|
371
|
+
precisionPrintString = `${precisionPrintString}e${exponent}`;
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
return precisionPrintString;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
public toTradePrecision(): string {
|
|
380
|
+
return this.toPrecision(6, true);
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/**
|
|
384
|
+
* Print dollar formatted value. Defaults to fixed decimals two unless a given precision is given.
|
|
385
|
+
* @param useTradePrecision
|
|
386
|
+
* @param precisionOverride
|
|
387
|
+
* @returns
|
|
388
|
+
*/
|
|
389
|
+
public toNotional(
|
|
390
|
+
useTradePrecision?: boolean,
|
|
391
|
+
precisionOverride?: number
|
|
392
|
+
): string {
|
|
393
|
+
const prefix = `${this.lt(BigNum.zero()) ? `-` : ``}$`;
|
|
394
|
+
|
|
395
|
+
const val =
|
|
396
|
+
useTradePrecision || precisionOverride
|
|
397
|
+
? this.prettyPrint(useTradePrecision, precisionOverride)
|
|
398
|
+
: BigNum.fromPrint(this.toFixed(2), new BN(2)).prettyPrint();
|
|
399
|
+
|
|
400
|
+
return `${prefix}${val.replace('-', '')}`;
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
public toMillified(precision = 3): string {
|
|
404
|
+
const stringVal = this.print();
|
|
405
|
+
|
|
406
|
+
const [leftSide] = stringVal.split(BigNum.delim);
|
|
407
|
+
|
|
408
|
+
if (!leftSide) {
|
|
409
|
+
return this.shift(new BN(precision)).toPrecision(precision, true);
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
if (leftSide.length <= 3) {
|
|
413
|
+
return this.shift(new BN(precision)).toPrecision(precision, true);
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
const unitTicks = ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'];
|
|
417
|
+
const unitNumber = Math.floor((leftSide.length - 1) / 3);
|
|
418
|
+
const unit = unitTicks[unitNumber];
|
|
419
|
+
|
|
420
|
+
let leadDigits = leftSide.slice(0, precision);
|
|
421
|
+
|
|
422
|
+
if (leadDigits.length < precision) {
|
|
423
|
+
leadDigits = [
|
|
424
|
+
...leadDigits.split(''),
|
|
425
|
+
...Array(precision - leadDigits.length).fill('0'),
|
|
426
|
+
].join('');
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
const decimalLocation = leftSide.length - 3 * unitNumber;
|
|
430
|
+
|
|
431
|
+
let leadString = '';
|
|
432
|
+
|
|
433
|
+
if (decimalLocation >= precision) {
|
|
434
|
+
leadString = `${leadDigits}`;
|
|
435
|
+
} else {
|
|
436
|
+
leadString = `${leadDigits.slice(0, decimalLocation)}${
|
|
437
|
+
BigNum.delim
|
|
438
|
+
}${leadDigits.slice(decimalLocation)}`;
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
return `${leadString}${unit}`;
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
public toJSON() {
|
|
445
|
+
return {
|
|
446
|
+
val: this.val.toString(),
|
|
447
|
+
precision: this.precision.toString(),
|
|
448
|
+
};
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
public isNeg() {
|
|
452
|
+
return this.lt(ZERO, true);
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
public isPos() {
|
|
456
|
+
return !this.isNeg();
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
/**
|
|
460
|
+
* Get the numerical value of the BigNum. This can break if the BigNum is too large.
|
|
461
|
+
* @returns
|
|
462
|
+
*/
|
|
463
|
+
public toNum() {
|
|
464
|
+
return parseFloat(this.print());
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
static fromJSON(json: { val: string; precision: string }) {
|
|
468
|
+
return BigNum.from(new BN(json.val), new BN(json.precision));
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
/**
|
|
472
|
+
* Create a BigNum instance
|
|
473
|
+
* @param val
|
|
474
|
+
* @param precision
|
|
475
|
+
* @returns
|
|
476
|
+
*/
|
|
477
|
+
static from(
|
|
478
|
+
val: BN | number | string = ZERO,
|
|
479
|
+
precision?: BN | number | string
|
|
480
|
+
): BigNum {
|
|
481
|
+
assert(
|
|
482
|
+
new BN(precision).lt(new BN(100)),
|
|
483
|
+
'Tried to create a bignum with precision higher than 10^100'
|
|
484
|
+
);
|
|
485
|
+
return new BigNum(val, precision);
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
/**
|
|
489
|
+
* Create a BigNum instance from a printed BigNum
|
|
490
|
+
* @param val
|
|
491
|
+
* @param precisionOverride
|
|
492
|
+
* @returns
|
|
493
|
+
*/
|
|
494
|
+
static fromPrint(val: string, precisionShift?: BN): BigNum {
|
|
495
|
+
// Handle empty number edge cases
|
|
496
|
+
if (!val) return BigNum.from(ZERO, precisionShift);
|
|
497
|
+
if (!val.replace(BigNum.delim, ''))
|
|
498
|
+
return BigNum.from(ZERO, precisionShift);
|
|
499
|
+
|
|
500
|
+
const [leftSide, rightSide] = val.split(BigNum.delim);
|
|
501
|
+
|
|
502
|
+
const rawBn = new BN(`${leftSide ?? ''}${rightSide ?? ''}`);
|
|
503
|
+
|
|
504
|
+
const rightSideLength = rightSide?.length ?? 0;
|
|
505
|
+
|
|
506
|
+
const totalShift = precisionShift
|
|
507
|
+
? precisionShift.sub(new BN(rightSideLength))
|
|
508
|
+
: ZERO;
|
|
509
|
+
|
|
510
|
+
return BigNum.from(rawBn, precisionShift).shift(totalShift, true);
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
static max(a: BigNum, b: BigNum): BigNum {
|
|
514
|
+
return a.gt(b) ? a : b;
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
static min(a: BigNum, b: BigNum): BigNum {
|
|
518
|
+
return a.lt(b) ? a : b;
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
static zero(precision?: BN | number): BigNum {
|
|
522
|
+
return BigNum.from(0, precision);
|
|
523
|
+
}
|
|
524
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
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");
|
|
8
|
+
function getOracleClient(oracleSource, connection) {
|
|
9
|
+
if (types_1.isVariant(oracleSource, 'pyth')) {
|
|
10
|
+
return new pythClient_1.PythClient(connection);
|
|
11
|
+
}
|
|
12
|
+
if (types_1.isVariant(oracleSource, 'switchboard')) {
|
|
13
|
+
return new switchboardClient_1.SwitchboardClient(connection);
|
|
14
|
+
}
|
|
15
|
+
if (types_1.isVariant(oracleSource, 'quoteAsset')) {
|
|
16
|
+
return new quoteAssetOracleClient_1.QuoteAssetOracleClient();
|
|
17
|
+
}
|
|
18
|
+
throw new Error(`Unknown oracle source ${oracleSource}`);
|
|
19
|
+
}
|
|
20
|
+
exports.getOracleClient = getOracleClient;
|
|
@@ -1,21 +1,24 @@
|
|
|
1
1
|
import { isVariant, OracleSource } from '../types';
|
|
2
2
|
import { Connection } from '@solana/web3.js';
|
|
3
|
-
import { DriftEnv } from '../config';
|
|
4
3
|
import { OracleClient } from '../oracles/types';
|
|
5
4
|
import { PythClient } from '../oracles/pythClient';
|
|
6
5
|
import { SwitchboardClient } from '../oracles/switchboardClient';
|
|
6
|
+
import { QuoteAssetOracleClient } from '../oracles/quoteAssetOracleClient';
|
|
7
7
|
|
|
8
8
|
export function getOracleClient(
|
|
9
9
|
oracleSource: OracleSource,
|
|
10
|
-
connection: Connection
|
|
11
|
-
env: DriftEnv
|
|
10
|
+
connection: Connection
|
|
12
11
|
): OracleClient {
|
|
13
12
|
if (isVariant(oracleSource, 'pyth')) {
|
|
14
13
|
return new PythClient(connection);
|
|
15
14
|
}
|
|
16
15
|
|
|
17
16
|
if (isVariant(oracleSource, 'switchboard')) {
|
|
18
|
-
return new SwitchboardClient(connection
|
|
17
|
+
return new SwitchboardClient(connection);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (isVariant(oracleSource, 'quoteAsset')) {
|
|
21
|
+
return new QuoteAssetOracleClient();
|
|
19
22
|
}
|
|
20
23
|
|
|
21
24
|
throw new Error(`Unknown oracle source ${oracleSource}`);
|