@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
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
import {
|
|
2
|
+
SpotMarketAccount,
|
|
3
|
+
SpotBalanceType,
|
|
4
|
+
isVariant,
|
|
5
|
+
MarginCategory,
|
|
6
|
+
} from '../types';
|
|
7
|
+
import { BN } from '@project-serum/anchor';
|
|
8
|
+
import {
|
|
9
|
+
SPOT_MARKET_UTILIZATION_PRECISION,
|
|
10
|
+
ONE,
|
|
11
|
+
TEN,
|
|
12
|
+
ZERO,
|
|
13
|
+
SPOT_MARKET_INTEREST_PRECISION,
|
|
14
|
+
SPOT_MARKET_WEIGHT_PRECISION,
|
|
15
|
+
ONE_YEAR,
|
|
16
|
+
AMM_RESERVE_PRECISION,
|
|
17
|
+
} from '../constants/numericConstants';
|
|
18
|
+
import {
|
|
19
|
+
calculateSizeDiscountAssetWeight,
|
|
20
|
+
calculateSizePremiumLiabilityWeight,
|
|
21
|
+
} from './margin';
|
|
22
|
+
|
|
23
|
+
export function getBalance(
|
|
24
|
+
tokenAmount: BN,
|
|
25
|
+
spotMarket: SpotMarketAccount,
|
|
26
|
+
balanceType: SpotBalanceType
|
|
27
|
+
): BN {
|
|
28
|
+
const precisionIncrease = TEN.pow(new BN(16 - spotMarket.decimals));
|
|
29
|
+
|
|
30
|
+
const cumulativeInterest = isVariant(balanceType, 'deposit')
|
|
31
|
+
? spotMarket.cumulativeDepositInterest
|
|
32
|
+
: spotMarket.cumulativeBorrowInterest;
|
|
33
|
+
|
|
34
|
+
let balance = tokenAmount.mul(precisionIncrease).div(cumulativeInterest);
|
|
35
|
+
|
|
36
|
+
if (!balance.eq(ZERO) && isVariant(balanceType, 'borrow')) {
|
|
37
|
+
balance = balance.add(ONE);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return balance;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function getTokenAmount(
|
|
44
|
+
balanceAmount: BN,
|
|
45
|
+
spotMarket: SpotMarketAccount,
|
|
46
|
+
balanceType: SpotBalanceType
|
|
47
|
+
): BN {
|
|
48
|
+
const precisionDecrease = TEN.pow(new BN(16 - spotMarket.decimals));
|
|
49
|
+
|
|
50
|
+
const cumulativeInterest = isVariant(balanceType, 'deposit')
|
|
51
|
+
? spotMarket.cumulativeDepositInterest
|
|
52
|
+
: spotMarket.cumulativeBorrowInterest;
|
|
53
|
+
|
|
54
|
+
return balanceAmount.mul(cumulativeInterest).div(precisionDecrease);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function calculateAssetWeight(
|
|
58
|
+
balanceAmount: BN,
|
|
59
|
+
spotMarket: SpotMarketAccount,
|
|
60
|
+
marginCategory: MarginCategory
|
|
61
|
+
): BN {
|
|
62
|
+
const sizePrecision = TEN.pow(new BN(spotMarket.decimals));
|
|
63
|
+
let sizeInAmmReservePrecision;
|
|
64
|
+
if (sizePrecision.gt(AMM_RESERVE_PRECISION)) {
|
|
65
|
+
sizeInAmmReservePrecision = balanceAmount.div(
|
|
66
|
+
sizePrecision.div(AMM_RESERVE_PRECISION)
|
|
67
|
+
);
|
|
68
|
+
} else {
|
|
69
|
+
sizeInAmmReservePrecision = balanceAmount
|
|
70
|
+
.mul(AMM_RESERVE_PRECISION)
|
|
71
|
+
.div(sizePrecision);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
let assetWeight;
|
|
75
|
+
|
|
76
|
+
switch (marginCategory) {
|
|
77
|
+
case 'Initial':
|
|
78
|
+
assetWeight = calculateSizeDiscountAssetWeight(
|
|
79
|
+
sizeInAmmReservePrecision,
|
|
80
|
+
spotMarket.imfFactor,
|
|
81
|
+
spotMarket.initialAssetWeight
|
|
82
|
+
);
|
|
83
|
+
break;
|
|
84
|
+
case 'Maintenance':
|
|
85
|
+
assetWeight = calculateSizeDiscountAssetWeight(
|
|
86
|
+
sizeInAmmReservePrecision,
|
|
87
|
+
spotMarket.imfFactor,
|
|
88
|
+
spotMarket.maintenanceAssetWeight
|
|
89
|
+
);
|
|
90
|
+
break;
|
|
91
|
+
default:
|
|
92
|
+
assetWeight = spotMarket.initialAssetWeight;
|
|
93
|
+
break;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return assetWeight;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function calculateLiabilityWeight(
|
|
100
|
+
balanceAmount: BN,
|
|
101
|
+
spotMarket: SpotMarketAccount,
|
|
102
|
+
marginCategory: MarginCategory
|
|
103
|
+
): BN {
|
|
104
|
+
const sizePrecision = TEN.pow(new BN(spotMarket.decimals));
|
|
105
|
+
let sizeInAmmReservePrecision;
|
|
106
|
+
if (sizePrecision.gt(AMM_RESERVE_PRECISION)) {
|
|
107
|
+
sizeInAmmReservePrecision = balanceAmount.div(
|
|
108
|
+
sizePrecision.div(AMM_RESERVE_PRECISION)
|
|
109
|
+
);
|
|
110
|
+
} else {
|
|
111
|
+
sizeInAmmReservePrecision = balanceAmount
|
|
112
|
+
.mul(AMM_RESERVE_PRECISION)
|
|
113
|
+
.div(sizePrecision);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
let assetWeight;
|
|
117
|
+
|
|
118
|
+
switch (marginCategory) {
|
|
119
|
+
case 'Initial':
|
|
120
|
+
assetWeight = calculateSizePremiumLiabilityWeight(
|
|
121
|
+
sizeInAmmReservePrecision,
|
|
122
|
+
spotMarket.imfFactor,
|
|
123
|
+
spotMarket.initialLiabilityWeight,
|
|
124
|
+
SPOT_MARKET_WEIGHT_PRECISION
|
|
125
|
+
);
|
|
126
|
+
break;
|
|
127
|
+
case 'Maintenance':
|
|
128
|
+
assetWeight = calculateSizePremiumLiabilityWeight(
|
|
129
|
+
sizeInAmmReservePrecision,
|
|
130
|
+
spotMarket.imfFactor,
|
|
131
|
+
spotMarket.maintenanceLiabilityWeight,
|
|
132
|
+
SPOT_MARKET_WEIGHT_PRECISION
|
|
133
|
+
);
|
|
134
|
+
break;
|
|
135
|
+
default:
|
|
136
|
+
assetWeight = spotMarket.initialLiabilityWeight;
|
|
137
|
+
break;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
return assetWeight;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export function calculateUtilization(bank: SpotMarketAccount): BN {
|
|
144
|
+
const tokenDepositAmount = getTokenAmount(
|
|
145
|
+
bank.depositBalance,
|
|
146
|
+
bank,
|
|
147
|
+
SpotBalanceType.DEPOSIT
|
|
148
|
+
);
|
|
149
|
+
const tokenBorrowAmount = getTokenAmount(
|
|
150
|
+
bank.borrowBalance,
|
|
151
|
+
bank,
|
|
152
|
+
SpotBalanceType.BORROW
|
|
153
|
+
);
|
|
154
|
+
|
|
155
|
+
let utilization: BN;
|
|
156
|
+
if (tokenBorrowAmount.eq(ZERO) && tokenDepositAmount.eq(ZERO)) {
|
|
157
|
+
utilization = ZERO;
|
|
158
|
+
} else if (tokenDepositAmount.eq(ZERO)) {
|
|
159
|
+
utilization = SPOT_MARKET_UTILIZATION_PRECISION;
|
|
160
|
+
} else {
|
|
161
|
+
utilization = tokenBorrowAmount
|
|
162
|
+
.mul(SPOT_MARKET_UTILIZATION_PRECISION)
|
|
163
|
+
.div(tokenDepositAmount);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
return utilization;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export function calculateInterestRate(bank: SpotMarketAccount): BN {
|
|
170
|
+
const utilization = calculateUtilization(bank);
|
|
171
|
+
|
|
172
|
+
let interestRate: BN;
|
|
173
|
+
if (utilization.gt(bank.optimalUtilization)) {
|
|
174
|
+
const surplusUtilization = utilization.sub(bank.optimalUtilization);
|
|
175
|
+
const borrowRateSlope = bank.maxBorrowRate
|
|
176
|
+
.sub(bank.optimalBorrowRate)
|
|
177
|
+
.mul(SPOT_MARKET_UTILIZATION_PRECISION)
|
|
178
|
+
.div(SPOT_MARKET_UTILIZATION_PRECISION.sub(bank.optimalUtilization));
|
|
179
|
+
|
|
180
|
+
interestRate = bank.optimalBorrowRate.add(
|
|
181
|
+
surplusUtilization
|
|
182
|
+
.mul(borrowRateSlope)
|
|
183
|
+
.div(SPOT_MARKET_UTILIZATION_PRECISION)
|
|
184
|
+
);
|
|
185
|
+
} else {
|
|
186
|
+
const borrowRateSlope = bank.optimalBorrowRate
|
|
187
|
+
.mul(SPOT_MARKET_UTILIZATION_PRECISION)
|
|
188
|
+
.div(SPOT_MARKET_UTILIZATION_PRECISION.sub(bank.optimalUtilization));
|
|
189
|
+
|
|
190
|
+
interestRate = utilization
|
|
191
|
+
.mul(borrowRateSlope)
|
|
192
|
+
.div(SPOT_MARKET_UTILIZATION_PRECISION);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
return interestRate;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
export function calculateDepositRate(bank: SpotMarketAccount): BN {
|
|
199
|
+
const utilization = calculateUtilization(bank);
|
|
200
|
+
const borrowRate = calculateBorrowRate(bank);
|
|
201
|
+
const depositRate = borrowRate
|
|
202
|
+
.mul(utilization)
|
|
203
|
+
.div(SPOT_MARKET_UTILIZATION_PRECISION);
|
|
204
|
+
return depositRate;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
export function calculateBorrowRate(bank: SpotMarketAccount): BN {
|
|
208
|
+
return calculateInterestRate(bank);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
export function calculateInterestAccumulated(
|
|
212
|
+
bank: SpotMarketAccount,
|
|
213
|
+
now: BN
|
|
214
|
+
): { borrowInterest: BN; depositInterest: BN } {
|
|
215
|
+
const interestRate = calculateInterestRate(bank);
|
|
216
|
+
|
|
217
|
+
const timeSinceLastUpdate = now.sub(bank.lastInterestTs);
|
|
218
|
+
|
|
219
|
+
const modifiedBorrowRate = interestRate.mul(timeSinceLastUpdate);
|
|
220
|
+
|
|
221
|
+
const utilization = calculateUtilization(bank);
|
|
222
|
+
|
|
223
|
+
const modifiedDepositRate = modifiedBorrowRate
|
|
224
|
+
.mul(utilization)
|
|
225
|
+
.div(SPOT_MARKET_UTILIZATION_PRECISION);
|
|
226
|
+
|
|
227
|
+
const borrowInterest = bank.cumulativeBorrowInterest
|
|
228
|
+
.mul(modifiedBorrowRate)
|
|
229
|
+
.div(ONE_YEAR)
|
|
230
|
+
.div(SPOT_MARKET_INTEREST_PRECISION)
|
|
231
|
+
.add(ONE);
|
|
232
|
+
const depositInterest = bank.cumulativeDepositInterest
|
|
233
|
+
.mul(modifiedDepositRate)
|
|
234
|
+
.div(ONE_YEAR)
|
|
235
|
+
.div(SPOT_MARKET_INTEREST_PRECISION);
|
|
236
|
+
|
|
237
|
+
return { borrowInterest, depositInterest };
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
export function calculateWithdrawLimit(
|
|
241
|
+
spotMarket: SpotMarketAccount,
|
|
242
|
+
now: BN
|
|
243
|
+
): { borrowLimit: BN; withdrawLimit: BN } {
|
|
244
|
+
const marketDepositTokenAmount = getTokenAmount(
|
|
245
|
+
spotMarket.depositBalance,
|
|
246
|
+
spotMarket,
|
|
247
|
+
SpotBalanceType.DEPOSIT
|
|
248
|
+
);
|
|
249
|
+
const marketBorrowTokenAmount = getTokenAmount(
|
|
250
|
+
spotMarket.borrowBalance,
|
|
251
|
+
spotMarket,
|
|
252
|
+
SpotBalanceType.BORROW
|
|
253
|
+
);
|
|
254
|
+
|
|
255
|
+
const twentyFourHours = new BN(60 * 60 * 24);
|
|
256
|
+
const sinceLast = now.sub(spotMarket.lastTwapTs);
|
|
257
|
+
const sinceStart = BN.max(ZERO, twentyFourHours.sub(sinceLast));
|
|
258
|
+
const borrowTokenTwapLive = spotMarket.borrowTokenTwap
|
|
259
|
+
.mul(sinceStart)
|
|
260
|
+
.add(marketBorrowTokenAmount.mul(sinceLast))
|
|
261
|
+
.div(sinceLast.add(sinceLast));
|
|
262
|
+
|
|
263
|
+
const depositTokenTwapLive = spotMarket.depositTokenTwap
|
|
264
|
+
.mul(sinceStart)
|
|
265
|
+
.add(marketDepositTokenAmount.mul(sinceLast))
|
|
266
|
+
.div(sinceLast.add(sinceLast));
|
|
267
|
+
|
|
268
|
+
const maxBorrowTokens = BN.min(
|
|
269
|
+
BN.max(
|
|
270
|
+
marketDepositTokenAmount.div(new BN(6)),
|
|
271
|
+
borrowTokenTwapLive.add(borrowTokenTwapLive.div(new BN(5)))
|
|
272
|
+
),
|
|
273
|
+
marketDepositTokenAmount.sub(marketDepositTokenAmount.div(new BN(10)))
|
|
274
|
+
); // between ~15-90% utilization with friction on twap
|
|
275
|
+
|
|
276
|
+
const minDepositTokens = depositTokenTwapLive.sub(
|
|
277
|
+
BN.min(
|
|
278
|
+
BN.max(
|
|
279
|
+
depositTokenTwapLive.div(new BN(5)),
|
|
280
|
+
spotMarket.withdrawGuardThreshold
|
|
281
|
+
),
|
|
282
|
+
depositTokenTwapLive
|
|
283
|
+
)
|
|
284
|
+
);
|
|
285
|
+
|
|
286
|
+
return {
|
|
287
|
+
borrowLimit: maxBorrowTokens.sub(marketBorrowTokenAmount),
|
|
288
|
+
withdrawLimit: marketDepositTokenAmount.sub(minDepositTokens),
|
|
289
|
+
};
|
|
290
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
exports.__esModule = true;
|
|
3
|
+
exports.castNumberToSpotPrecision = void 0;
|
|
4
|
+
var anchor_1 = require("@project-serum/anchor");
|
|
5
|
+
function castNumberToSpotPrecision(value, spotMarket) {
|
|
6
|
+
return new anchor_1.BN(value * Math.pow(10, spotMarket.decimals));
|
|
7
|
+
}
|
|
8
|
+
exports.castNumberToSpotPrecision = castNumberToSpotPrecision;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
exports.__esModule = true;
|
|
3
|
+
exports.isSpotPositionAvailable = void 0;
|
|
4
|
+
var numericConstants_1 = require("../constants/numericConstants");
|
|
5
|
+
function isSpotPositionAvailable(position) {
|
|
6
|
+
return position.balance.eq(numericConstants_1.ZERO) && position.openOrders === 0;
|
|
7
|
+
}
|
|
8
|
+
exports.isSpotPositionAvailable = isSpotPositionAvailable;
|
package/src/math/state.ts
CHANGED
|
@@ -8,7 +8,7 @@ import { StateAccount } from '../types';
|
|
|
8
8
|
*/
|
|
9
9
|
export function getExchangeFee(state: StateAccount): number {
|
|
10
10
|
const exchangeFee =
|
|
11
|
-
state.
|
|
12
|
-
state.
|
|
11
|
+
state.perpFeeStructure.feeNumerator.toNumber() /
|
|
12
|
+
state.perpFeeStructure.feeDenominator.toNumber();
|
|
13
13
|
return exchangeFee;
|
|
14
14
|
}
|
package/src/math/trade.js
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
2
|
+
exports.__esModule = true;
|
|
3
3
|
exports.calculateTargetPriceTrade = exports.calculateTradeAcquiredAmounts = exports.calculateTradeSlippage = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
4
|
+
var types_1 = require("../types");
|
|
5
|
+
var anchor_1 = require("@project-serum/anchor");
|
|
6
|
+
var assert_1 = require("../assert/assert");
|
|
7
|
+
var numericConstants_1 = require("../constants/numericConstants");
|
|
8
|
+
var market_1 = require("./market");
|
|
9
|
+
var amm_1 = require("./amm");
|
|
10
|
+
var utils_1 = require("./utils");
|
|
11
|
+
var types_2 = require("../types");
|
|
12
|
+
var MAXPCT = new anchor_1.BN(1000); //percentage units are [0,1000] => [0,1]
|
|
13
13
|
/**
|
|
14
14
|
* Calculates avg/max slippage (price impact) for candidate trade
|
|
15
15
|
* @param direction
|
|
@@ -27,53 +27,55 @@ const MAXPCT = new anchor_1.BN(1000); //percentage units are [0,1000] => [0,1]
|
|
|
27
27
|
*
|
|
28
28
|
* 'newPrice' => the price of the asset after the trade : Precision MARK_PRICE_PRECISION
|
|
29
29
|
*/
|
|
30
|
-
function calculateTradeSlippage(direction, amount, market, inputAssetType
|
|
31
|
-
|
|
30
|
+
function calculateTradeSlippage(direction, amount, market, inputAssetType, oraclePriceData, useSpread) {
|
|
31
|
+
if (inputAssetType === void 0) { inputAssetType = 'quote'; }
|
|
32
|
+
if (useSpread === void 0) { useSpread = true; }
|
|
33
|
+
var oldPrice;
|
|
32
34
|
if (useSpread && market.amm.baseSpread > 0) {
|
|
33
|
-
if (types_2.isVariant(direction, 'long')) {
|
|
34
|
-
oldPrice = market_1.calculateAskPrice(market, oraclePriceData);
|
|
35
|
+
if ((0, types_2.isVariant)(direction, 'long')) {
|
|
36
|
+
oldPrice = (0, market_1.calculateAskPrice)(market, oraclePriceData);
|
|
35
37
|
}
|
|
36
38
|
else {
|
|
37
|
-
oldPrice = market_1.calculateBidPrice(market, oraclePriceData);
|
|
39
|
+
oldPrice = (0, market_1.calculateBidPrice)(market, oraclePriceData);
|
|
38
40
|
}
|
|
39
41
|
}
|
|
40
42
|
else {
|
|
41
|
-
oldPrice = market_1.calculateMarkPrice(market, oraclePriceData);
|
|
43
|
+
oldPrice = (0, market_1.calculateMarkPrice)(market, oraclePriceData);
|
|
42
44
|
}
|
|
43
45
|
if (amount.eq(numericConstants_1.ZERO)) {
|
|
44
46
|
return [numericConstants_1.ZERO, numericConstants_1.ZERO, oldPrice, oldPrice];
|
|
45
47
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
+
var _a = calculateTradeAcquiredAmounts(direction, amount, market, inputAssetType, oraclePriceData, useSpread), acquiredBaseReserve = _a[0], acquiredQuoteReserve = _a[1], acquiredQuoteAssetAmount = _a[2];
|
|
49
|
+
var entryPrice = acquiredQuoteAssetAmount
|
|
48
50
|
.mul(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO)
|
|
49
51
|
.mul(numericConstants_1.MARK_PRICE_PRECISION)
|
|
50
52
|
.div(acquiredBaseReserve.abs());
|
|
51
|
-
|
|
53
|
+
var amm;
|
|
52
54
|
if (useSpread && market.amm.baseSpread > 0) {
|
|
53
|
-
|
|
55
|
+
var _b = (0, amm_1.calculateUpdatedAMMSpreadReserves)(market.amm, direction, oraclePriceData), baseAssetReserve = _b.baseAssetReserve, quoteAssetReserve = _b.quoteAssetReserve, sqrtK = _b.sqrtK, newPeg = _b.newPeg;
|
|
54
56
|
amm = {
|
|
55
|
-
baseAssetReserve,
|
|
56
|
-
quoteAssetReserve,
|
|
57
|
+
baseAssetReserve: baseAssetReserve,
|
|
58
|
+
quoteAssetReserve: quoteAssetReserve,
|
|
57
59
|
sqrtK: sqrtK,
|
|
58
|
-
pegMultiplier: newPeg
|
|
60
|
+
pegMultiplier: newPeg
|
|
59
61
|
};
|
|
60
62
|
}
|
|
61
63
|
else {
|
|
62
64
|
amm = market.amm;
|
|
63
65
|
}
|
|
64
|
-
|
|
66
|
+
var newPrice = (0, amm_1.calculatePrice)(amm.baseAssetReserve.sub(acquiredBaseReserve), amm.quoteAssetReserve.sub(acquiredQuoteReserve), amm.pegMultiplier);
|
|
65
67
|
if (direction == types_1.PositionDirection.SHORT) {
|
|
66
|
-
assert_1.assert(newPrice.lte(oldPrice));
|
|
68
|
+
(0, assert_1.assert)(newPrice.lte(oldPrice));
|
|
67
69
|
}
|
|
68
70
|
else {
|
|
69
|
-
assert_1.assert(oldPrice.lte(newPrice));
|
|
71
|
+
(0, assert_1.assert)(oldPrice.lte(newPrice));
|
|
70
72
|
}
|
|
71
|
-
|
|
73
|
+
var pctMaxSlippage = newPrice
|
|
72
74
|
.sub(oldPrice)
|
|
73
75
|
.mul(numericConstants_1.MARK_PRICE_PRECISION)
|
|
74
76
|
.div(oldPrice)
|
|
75
77
|
.abs();
|
|
76
|
-
|
|
78
|
+
var pctAvgSlippage = entryPrice
|
|
77
79
|
.sub(oldPrice)
|
|
78
80
|
.mul(numericConstants_1.MARK_PRICE_PRECISION)
|
|
79
81
|
.div(oldPrice)
|
|
@@ -92,29 +94,31 @@ exports.calculateTradeSlippage = calculateTradeSlippage;
|
|
|
92
94
|
* | 'acquiredBase' => positive/negative change in user's base : BN AMM_RESERVE_PRECISION
|
|
93
95
|
* | 'acquiredQuote' => positive/negative change in user's quote : BN TODO-PRECISION
|
|
94
96
|
*/
|
|
95
|
-
function calculateTradeAcquiredAmounts(direction, amount, market, inputAssetType
|
|
97
|
+
function calculateTradeAcquiredAmounts(direction, amount, market, inputAssetType, oraclePriceData, useSpread) {
|
|
98
|
+
if (inputAssetType === void 0) { inputAssetType = 'quote'; }
|
|
99
|
+
if (useSpread === void 0) { useSpread = true; }
|
|
96
100
|
if (amount.eq(numericConstants_1.ZERO)) {
|
|
97
101
|
return [numericConstants_1.ZERO, numericConstants_1.ZERO, numericConstants_1.ZERO];
|
|
98
102
|
}
|
|
99
|
-
|
|
100
|
-
|
|
103
|
+
var swapDirection = (0, amm_1.getSwapDirection)(inputAssetType, direction);
|
|
104
|
+
var amm;
|
|
101
105
|
if (useSpread && market.amm.baseSpread > 0) {
|
|
102
|
-
|
|
106
|
+
var _a = (0, amm_1.calculateUpdatedAMMSpreadReserves)(market.amm, direction, oraclePriceData), baseAssetReserve = _a.baseAssetReserve, quoteAssetReserve = _a.quoteAssetReserve, sqrtK = _a.sqrtK, newPeg = _a.newPeg;
|
|
103
107
|
amm = {
|
|
104
|
-
baseAssetReserve,
|
|
105
|
-
quoteAssetReserve,
|
|
108
|
+
baseAssetReserve: baseAssetReserve,
|
|
109
|
+
quoteAssetReserve: quoteAssetReserve,
|
|
106
110
|
sqrtK: sqrtK,
|
|
107
|
-
pegMultiplier: newPeg
|
|
111
|
+
pegMultiplier: newPeg
|
|
108
112
|
};
|
|
109
113
|
}
|
|
110
114
|
else {
|
|
111
115
|
amm = market.amm;
|
|
112
116
|
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
return [acquiredBase, acquiredQuote,
|
|
117
|
+
var _b = (0, amm_1.calculateAmmReservesAfterSwap)(amm, inputAssetType, amount, swapDirection), newQuoteAssetReserve = _b[0], newBaseAssetReserve = _b[1];
|
|
118
|
+
var acquiredBase = amm.baseAssetReserve.sub(newBaseAssetReserve);
|
|
119
|
+
var acquiredQuote = amm.quoteAssetReserve.sub(newQuoteAssetReserve);
|
|
120
|
+
var acquiredQuoteAssetAmount = (0, amm_1.calculateQuoteAssetAmountSwapped)(acquiredQuote.abs(), amm.pegMultiplier, swapDirection);
|
|
121
|
+
return [acquiredBase, acquiredQuote, acquiredQuoteAssetAmount];
|
|
118
122
|
}
|
|
119
123
|
exports.calculateTradeAcquiredAmounts = calculateTradeAcquiredAmounts;
|
|
120
124
|
/**
|
|
@@ -134,33 +138,36 @@ exports.calculateTradeAcquiredAmounts = calculateTradeAcquiredAmounts;
|
|
|
134
138
|
* targetPrice => the target price MARK_PRICE_PRECISION
|
|
135
139
|
* ]
|
|
136
140
|
*/
|
|
137
|
-
function calculateTargetPriceTrade(market, targetPrice, pct
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
141
|
+
function calculateTargetPriceTrade(market, targetPrice, pct, outputAssetType, oraclePriceData, useSpread) {
|
|
142
|
+
if (pct === void 0) { pct = MAXPCT; }
|
|
143
|
+
if (outputAssetType === void 0) { outputAssetType = 'quote'; }
|
|
144
|
+
if (useSpread === void 0) { useSpread = true; }
|
|
145
|
+
(0, assert_1.assert)(market.amm.baseAssetReserve.gt(numericConstants_1.ZERO));
|
|
146
|
+
(0, assert_1.assert)(targetPrice.gt(numericConstants_1.ZERO));
|
|
147
|
+
(0, assert_1.assert)(pct.lte(MAXPCT) && pct.gt(numericConstants_1.ZERO));
|
|
148
|
+
var markPriceBefore = (0, market_1.calculateMarkPrice)(market, oraclePriceData);
|
|
149
|
+
var bidPriceBefore = (0, market_1.calculateBidPrice)(market, oraclePriceData);
|
|
150
|
+
var askPriceBefore = (0, market_1.calculateAskPrice)(market, oraclePriceData);
|
|
151
|
+
var direction;
|
|
145
152
|
if (targetPrice.gt(markPriceBefore)) {
|
|
146
|
-
|
|
147
|
-
|
|
153
|
+
var priceGap = targetPrice.sub(markPriceBefore);
|
|
154
|
+
var priceGapScaled = priceGap.mul(pct).div(MAXPCT);
|
|
148
155
|
targetPrice = markPriceBefore.add(priceGapScaled);
|
|
149
156
|
direction = types_1.PositionDirection.LONG;
|
|
150
157
|
}
|
|
151
158
|
else {
|
|
152
|
-
|
|
153
|
-
|
|
159
|
+
var priceGap = markPriceBefore.sub(targetPrice);
|
|
160
|
+
var priceGapScaled = priceGap.mul(pct).div(MAXPCT);
|
|
154
161
|
targetPrice = markPriceBefore.sub(priceGapScaled);
|
|
155
162
|
direction = types_1.PositionDirection.SHORT;
|
|
156
163
|
}
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
164
|
+
var tradeSize;
|
|
165
|
+
var baseSize;
|
|
166
|
+
var baseAssetReserveBefore;
|
|
167
|
+
var quoteAssetReserveBefore;
|
|
168
|
+
var peg = market.amm.pegMultiplier;
|
|
162
169
|
if (useSpread && market.amm.baseSpread > 0) {
|
|
163
|
-
|
|
170
|
+
var _a = (0, amm_1.calculateUpdatedAMMSpreadReserves)(market.amm, direction, oraclePriceData), baseAssetReserve = _a.baseAssetReserve, quoteAssetReserve = _a.quoteAssetReserve, newPeg = _a.newPeg;
|
|
164
171
|
baseAssetReserveBefore = baseAssetReserve;
|
|
165
172
|
quoteAssetReserveBefore = quoteAssetReserve;
|
|
166
173
|
peg = newPeg;
|
|
@@ -169,12 +176,12 @@ function calculateTargetPriceTrade(market, targetPrice, pct = MAXPCT, outputAsse
|
|
|
169
176
|
baseAssetReserveBefore = market.amm.baseAssetReserve;
|
|
170
177
|
quoteAssetReserveBefore = market.amm.quoteAssetReserve;
|
|
171
178
|
}
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
179
|
+
var invariant = market.amm.sqrtK.mul(market.amm.sqrtK);
|
|
180
|
+
var k = invariant.mul(numericConstants_1.MARK_PRICE_PRECISION);
|
|
181
|
+
var baseAssetReserveAfter;
|
|
182
|
+
var quoteAssetReserveAfter;
|
|
183
|
+
var biasModifier = new anchor_1.BN(1);
|
|
184
|
+
var markPriceAfter;
|
|
178
185
|
if (useSpread &&
|
|
179
186
|
targetPrice.lt(askPriceBefore) &&
|
|
180
187
|
targetPrice.gt(bidPriceBefore)) {
|
|
@@ -190,11 +197,11 @@ function calculateTargetPriceTrade(market, targetPrice, pct = MAXPCT, outputAsse
|
|
|
190
197
|
}
|
|
191
198
|
else if (markPriceBefore.gt(targetPrice)) {
|
|
192
199
|
// overestimate y2
|
|
193
|
-
baseAssetReserveAfter = utils_1.squareRootBN(k.div(targetPrice).mul(peg).div(numericConstants_1.PEG_PRECISION).sub(biasModifier)).sub(new anchor_1.BN(1));
|
|
200
|
+
baseAssetReserveAfter = (0, utils_1.squareRootBN)(k.div(targetPrice).mul(peg).div(numericConstants_1.PEG_PRECISION).sub(biasModifier)).sub(new anchor_1.BN(1));
|
|
194
201
|
quoteAssetReserveAfter = k
|
|
195
202
|
.div(numericConstants_1.MARK_PRICE_PRECISION)
|
|
196
203
|
.div(baseAssetReserveAfter);
|
|
197
|
-
markPriceAfter = amm_1.calculatePrice(baseAssetReserveAfter, quoteAssetReserveAfter, peg);
|
|
204
|
+
markPriceAfter = (0, amm_1.calculatePrice)(baseAssetReserveAfter, quoteAssetReserveAfter, peg);
|
|
198
205
|
direction = types_1.PositionDirection.SHORT;
|
|
199
206
|
tradeSize = quoteAssetReserveBefore
|
|
200
207
|
.sub(quoteAssetReserveAfter)
|
|
@@ -205,11 +212,11 @@ function calculateTargetPriceTrade(market, targetPrice, pct = MAXPCT, outputAsse
|
|
|
205
212
|
}
|
|
206
213
|
else if (markPriceBefore.lt(targetPrice)) {
|
|
207
214
|
// underestimate y2
|
|
208
|
-
baseAssetReserveAfter = utils_1.squareRootBN(k.div(targetPrice).mul(peg).div(numericConstants_1.PEG_PRECISION).add(biasModifier)).add(new anchor_1.BN(1));
|
|
215
|
+
baseAssetReserveAfter = (0, utils_1.squareRootBN)(k.div(targetPrice).mul(peg).div(numericConstants_1.PEG_PRECISION).add(biasModifier)).add(new anchor_1.BN(1));
|
|
209
216
|
quoteAssetReserveAfter = k
|
|
210
217
|
.div(numericConstants_1.MARK_PRICE_PRECISION)
|
|
211
218
|
.div(baseAssetReserveAfter);
|
|
212
|
-
markPriceAfter = amm_1.calculatePrice(baseAssetReserveAfter, quoteAssetReserveAfter, peg);
|
|
219
|
+
markPriceAfter = (0, amm_1.calculatePrice)(baseAssetReserveAfter, quoteAssetReserveAfter, peg);
|
|
213
220
|
direction = types_1.PositionDirection.LONG;
|
|
214
221
|
tradeSize = quoteAssetReserveAfter
|
|
215
222
|
.sub(quoteAssetReserveBefore)
|
|
@@ -224,20 +231,20 @@ function calculateTargetPriceTrade(market, targetPrice, pct = MAXPCT, outputAsse
|
|
|
224
231
|
tradeSize = numericConstants_1.ZERO;
|
|
225
232
|
return [direction, tradeSize, targetPrice, targetPrice];
|
|
226
233
|
}
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
234
|
+
var tp1 = targetPrice;
|
|
235
|
+
var tp2 = markPriceAfter;
|
|
236
|
+
var originalDiff = targetPrice.sub(markPriceBefore);
|
|
230
237
|
if (direction == types_1.PositionDirection.SHORT) {
|
|
231
238
|
tp1 = markPriceAfter;
|
|
232
239
|
tp2 = targetPrice;
|
|
233
240
|
originalDiff = markPriceBefore.sub(targetPrice);
|
|
234
241
|
}
|
|
235
|
-
|
|
242
|
+
var entryPrice = tradeSize
|
|
236
243
|
.mul(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO)
|
|
237
244
|
.mul(numericConstants_1.MARK_PRICE_PRECISION)
|
|
238
245
|
.div(baseSize.abs());
|
|
239
|
-
assert_1.assert(tp1.sub(tp2).lte(originalDiff), 'Target Price Calculation incorrect');
|
|
240
|
-
assert_1.assert(tp2.lte(tp1) || tp2.sub(tp1).abs() < 100000, 'Target Price Calculation incorrect' +
|
|
246
|
+
(0, assert_1.assert)(tp1.sub(tp2).lte(originalDiff), 'Target Price Calculation incorrect');
|
|
247
|
+
(0, assert_1.assert)(tp2.lte(tp1) || tp2.sub(tp1).abs() < 100000, 'Target Price Calculation incorrect' +
|
|
241
248
|
tp2.toString() +
|
|
242
249
|
'>=' +
|
|
243
250
|
tp1.toString() +
|
package/src/math/trade.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { PerpMarketAccount, PositionDirection } from '../types';
|
|
2
2
|
import { BN } from '@project-serum/anchor';
|
|
3
3
|
import { assert } from '../assert/assert';
|
|
4
4
|
import {
|
|
@@ -59,7 +59,7 @@ export type PriceImpactUnit =
|
|
|
59
59
|
export function calculateTradeSlippage(
|
|
60
60
|
direction: PositionDirection,
|
|
61
61
|
amount: BN,
|
|
62
|
-
market:
|
|
62
|
+
market: PerpMarketAccount,
|
|
63
63
|
inputAssetType: AssetType = 'quote',
|
|
64
64
|
oraclePriceData?: OraclePriceData,
|
|
65
65
|
useSpread = true
|
|
@@ -147,7 +147,7 @@ export function calculateTradeSlippage(
|
|
|
147
147
|
export function calculateTradeAcquiredAmounts(
|
|
148
148
|
direction: PositionDirection,
|
|
149
149
|
amount: BN,
|
|
150
|
-
market:
|
|
150
|
+
market: PerpMarketAccount,
|
|
151
151
|
inputAssetType: AssetType = 'quote',
|
|
152
152
|
oraclePriceData: OraclePriceData,
|
|
153
153
|
useSpread = true
|
|
@@ -204,7 +204,7 @@ export function calculateTradeAcquiredAmounts(
|
|
|
204
204
|
* ]
|
|
205
205
|
*/
|
|
206
206
|
export function calculateTargetPriceTrade(
|
|
207
|
-
market:
|
|
207
|
+
market: PerpMarketAccount,
|
|
208
208
|
targetPrice: BN,
|
|
209
209
|
pct: BN = MAXPCT,
|
|
210
210
|
outputAssetType: AssetType = 'quote',
|
package/src/math/utils.js
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
2
|
+
exports.__esModule = true;
|
|
3
3
|
exports.squareRootBN = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
var __1 = require("../");
|
|
5
|
+
var squareRootBN = function (n, closeness) {
|
|
6
|
+
if (closeness === void 0) { closeness = new __1.BN(1); }
|
|
6
7
|
// Assuming the sqrt of n as n only
|
|
7
|
-
|
|
8
|
+
var x = n;
|
|
8
9
|
// The closed guess will be stored in the root
|
|
9
|
-
|
|
10
|
+
var root;
|
|
10
11
|
// To count the number of iterations
|
|
11
|
-
|
|
12
|
-
|
|
12
|
+
var count = 0;
|
|
13
|
+
var TWO = new __1.BN(2);
|
|
13
14
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
14
15
|
while (count < Number.MAX_SAFE_INTEGER) {
|
|
15
16
|
count++;
|