@drift-labs/sdk 0.2.0-master.12 → 0.2.0-master.15
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/bulkUserStatsSubscription.d.ts +7 -0
- package/lib/accounts/bulkUserStatsSubscription.js +21 -0
- package/lib/accounts/bulkUserSubscription.js +0 -1
- package/lib/accounts/fetch.d.ts +2 -1
- package/lib/accounts/fetch.js +9 -1
- package/lib/accounts/pollingUserStatsAccountSubscriber.d.ts +27 -0
- package/lib/accounts/pollingUserStatsAccountSubscriber.js +113 -0
- package/lib/accounts/types.d.ts +14 -2
- package/lib/accounts/webSocketUserStatsAccountSubsriber.d.ts +20 -0
- package/lib/accounts/webSocketUserStatsAccountSubsriber.js +47 -0
- package/lib/addresses/pda.d.ts +1 -0
- package/lib/addresses/pda.js +8 -1
- package/lib/admin.d.ts +2 -0
- package/lib/admin.js +18 -0
- package/lib/clearingHouse.d.ts +17 -1
- package/lib/clearingHouse.js +195 -14
- package/lib/clearingHouseConfig.d.ts +1 -0
- package/lib/clearingHouseUser.d.ts +5 -0
- package/lib/clearingHouseUser.js +97 -3
- package/lib/clearingHouseUserStats.d.ts +17 -0
- package/lib/clearingHouseUserStats.js +36 -0
- package/lib/clearingHouseUserStatsConfig.d.ts +14 -0
- package/{src/clearingHouseConfig.js → lib/clearingHouseUserStatsConfig.js} +0 -0
- package/lib/config.js +1 -1
- package/lib/factory/bigNum.js +7 -9
- package/lib/idl/clearing_house.json +620 -64
- package/lib/math/amm.js +2 -2
- package/lib/math/bankBalance.d.ts +4 -0
- package/lib/math/bankBalance.js +24 -2
- package/lib/math/oracles.d.ts +3 -0
- package/lib/math/oracles.js +25 -5
- package/lib/math/position.js +2 -1
- package/lib/math/trade.js +2 -2
- package/lib/types.d.ts +57 -9
- package/lib/types.js +6 -0
- package/package.json +1 -1
- package/src/accounts/bulkUserStatsSubscription.ts +33 -0
- package/src/accounts/bulkUserSubscription.ts +0 -1
- package/src/accounts/fetch.ts +27 -2
- package/src/accounts/pollingUserStatsAccountSubscriber.ts +172 -0
- package/src/accounts/types.ts +18 -0
- package/src/accounts/webSocketUserStatsAccountSubsriber.ts +80 -0
- package/src/addresses/pda.ts +13 -0
- package/src/admin.ts +29 -1
- package/src/clearingHouse.ts +318 -15
- package/src/clearingHouseConfig.ts +1 -0
- package/src/clearingHouseUser.ts +113 -10
- package/src/clearingHouseUserStats.ts +53 -0
- package/src/clearingHouseUserStatsConfig.ts +18 -0
- package/src/config.ts +1 -1
- package/src/factory/bigNum.ts +6 -7
- package/src/idl/clearing_house.json +620 -64
- package/src/math/amm.ts +8 -5
- package/src/math/bankBalance.ts +50 -1
- package/src/math/oracles.ts +42 -5
- package/src/math/position.ts +2 -1
- package/src/math/trade.ts +2 -2
- package/src/types.ts +61 -9
- package/tests/bn/test.ts +8 -0
- package/src/accounts/bulkAccountLoader.js +0 -197
- package/src/accounts/bulkUserSubscription.js +0 -33
- package/src/accounts/pollingClearingHouseAccountSubscriber.js +0 -311
- package/src/accounts/pollingOracleSubscriber.js +0 -93
- package/src/accounts/pollingTokenAccountSubscriber.js +0 -90
- package/src/accounts/pollingUserAccountSubscriber.js +0 -132
- package/src/accounts/types.js +0 -10
- package/src/accounts/utils.js +0 -7
- package/src/accounts/webSocketAccountSubscriber.js +0 -93
- package/src/accounts/webSocketClearingHouseAccountSubscriber.js +0 -233
- package/src/accounts/webSocketUserAccountSubscriber.js +0 -62
- package/src/addresses/marketAddresses.js +0 -26
- package/src/assert/assert.js +0 -9
- package/src/clearingHouseUserConfig.js +0 -2
- package/src/constants/banks.js +0 -42
- package/src/constants/markets.js +0 -42
- package/src/constants/numericConstants.js +0 -41
- package/src/events/eventList.js +0 -77
- package/src/events/eventSubscriber.js +0 -139
- package/src/events/fetchLogs.js +0 -50
- package/src/events/pollingLogProvider.js +0 -64
- package/src/events/sort.js +0 -44
- package/src/events/txEventCache.js +0 -71
- package/src/events/types.js +0 -20
- package/src/events/webSocketLogProvider.js +0 -41
- package/src/examples/makeTradeExample.js +0 -80
- package/src/factory/bigNum.js +0 -390
- package/src/factory/oracleClient.js +0 -20
- package/src/index.js +0 -69
- package/src/math/amm.js +0 -369
- package/src/math/auction.js +0 -42
- package/src/math/conversion.js +0 -11
- package/src/math/funding.js +0 -248
- package/src/math/oracles.js +0 -26
- package/src/math/repeg.js +0 -128
- package/src/math/state.js +0 -15
- package/src/math/trade.js +0 -253
- package/src/math/utils.js +0 -26
- package/src/math/utils.js.map +0 -1
- package/src/mockUSDCFaucet.js +0 -280
- package/src/oracles/oracleClientCache.js +0 -19
- package/src/oracles/pythClient.js +0 -46
- package/src/oracles/quoteAssetOracleClient.js +0 -32
- package/src/oracles/switchboardClient.js +0 -69
- package/src/oracles/types.js +0 -2
- package/src/orderParams.js +0 -20
- package/src/slot/SlotSubscriber.js +0 -39
- package/src/token/index.js +0 -38
- package/src/tokenFaucet.js +0 -189
- package/src/tx/types.js +0 -2
- package/src/tx/utils.js +0 -17
- package/src/types.js +0 -125
- package/src/userName.js +0 -20
- package/src/util/computeUnits.js +0 -27
- package/src/util/getTokenAddress.js +0 -9
- package/src/util/promiseTimeout.js +0 -14
- package/src/util/tps.js +0 -27
- package/src/wallet.js +0 -35
package/src/math/amm.js
DELETED
|
@@ -1,369 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.calculateQuoteAssetAmountSwapped = exports.calculateMaxBaseAssetAmountToTrade = exports.calculateTerminalPrice = exports.getSwapDirection = exports.calculateSwapOutput = exports.calculateSpreadReserves = exports.calculateSpread = exports.calculateSpreadBN = exports.calculateMaxSpread = exports.calculateEffectiveLeverage = exports.calculateAmmReservesAfterSwap = exports.calculatePrice = exports.calculateBidAskPrice = exports.calculateUpdatedAMMSpreadReserves = exports.calculateUpdatedAMM = exports.calculateNewAmm = exports.calculateOptimalPegAndBudget = exports.calculatePegFromTargetPrice = void 0;
|
|
4
|
-
const anchor_1 = require("@project-serum/anchor");
|
|
5
|
-
const numericConstants_1 = require("../constants/numericConstants");
|
|
6
|
-
const types_1 = require("../types");
|
|
7
|
-
const assert_1 = require("../assert/assert");
|
|
8
|
-
const __1 = require("..");
|
|
9
|
-
const repeg_1 = require("./repeg");
|
|
10
|
-
function calculatePegFromTargetPrice(targetPrice, baseAssetReserve, quoteAssetReserve) {
|
|
11
|
-
return targetPrice
|
|
12
|
-
.mul(baseAssetReserve)
|
|
13
|
-
.div(quoteAssetReserve)
|
|
14
|
-
.add(numericConstants_1.PRICE_DIV_PEG.div(new anchor_1.BN(2)))
|
|
15
|
-
.div(numericConstants_1.PRICE_DIV_PEG);
|
|
16
|
-
}
|
|
17
|
-
exports.calculatePegFromTargetPrice = calculatePegFromTargetPrice;
|
|
18
|
-
function calculateOptimalPegAndBudget(amm, oraclePriceData) {
|
|
19
|
-
const markPriceBefore = calculatePrice(amm.baseAssetReserve, amm.quoteAssetReserve, amm.pegMultiplier);
|
|
20
|
-
const targetPrice = oraclePriceData.price;
|
|
21
|
-
const newPeg = calculatePegFromTargetPrice(targetPrice, amm.baseAssetReserve, amm.quoteAssetReserve);
|
|
22
|
-
const prePegCost = repeg_1.calculateRepegCost(amm, newPeg);
|
|
23
|
-
const totalFeeLB = amm.totalExchangeFee.div(new anchor_1.BN(2));
|
|
24
|
-
const budget = anchor_1.BN.max(numericConstants_1.ZERO, amm.totalFeeMinusDistributions.sub(totalFeeLB));
|
|
25
|
-
if (budget.lt(prePegCost)) {
|
|
26
|
-
const maxPriceSpread = new anchor_1.BN(amm.maxSpread)
|
|
27
|
-
.mul(targetPrice)
|
|
28
|
-
.div(numericConstants_1.BID_ASK_SPREAD_PRECISION);
|
|
29
|
-
let newTargetPrice;
|
|
30
|
-
let newOptimalPeg;
|
|
31
|
-
let newBudget;
|
|
32
|
-
const targetPriceGap = markPriceBefore.sub(targetPrice);
|
|
33
|
-
if (targetPriceGap.abs().gt(maxPriceSpread)) {
|
|
34
|
-
const markAdj = targetPriceGap.abs().sub(maxPriceSpread);
|
|
35
|
-
if (targetPriceGap.lt(new anchor_1.BN(0))) {
|
|
36
|
-
newTargetPrice = markPriceBefore.add(markAdj);
|
|
37
|
-
}
|
|
38
|
-
else {
|
|
39
|
-
newTargetPrice = markPriceBefore.sub(markAdj);
|
|
40
|
-
}
|
|
41
|
-
newOptimalPeg = calculatePegFromTargetPrice(newTargetPrice, amm.baseAssetReserve, amm.quoteAssetReserve);
|
|
42
|
-
newBudget = repeg_1.calculateRepegCost(amm, newOptimalPeg);
|
|
43
|
-
return [newTargetPrice, newOptimalPeg, newBudget, false];
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
return [targetPrice, newPeg, budget, true];
|
|
47
|
-
}
|
|
48
|
-
exports.calculateOptimalPegAndBudget = calculateOptimalPegAndBudget;
|
|
49
|
-
function calculateNewAmm(amm, oraclePriceData) {
|
|
50
|
-
let pKNumer = new anchor_1.BN(1);
|
|
51
|
-
let pKDenom = new anchor_1.BN(1);
|
|
52
|
-
const [targetPrice, _newPeg, budget, checkLowerBound] = calculateOptimalPegAndBudget(amm, oraclePriceData);
|
|
53
|
-
let prePegCost = repeg_1.calculateRepegCost(amm, _newPeg);
|
|
54
|
-
let newPeg = _newPeg;
|
|
55
|
-
if (prePegCost.gt(budget) && checkLowerBound) {
|
|
56
|
-
[pKNumer, pKDenom] = [new anchor_1.BN(999), new anchor_1.BN(1000)];
|
|
57
|
-
const deficitMadeup = repeg_1.calculateAdjustKCost(amm, pKNumer, pKDenom);
|
|
58
|
-
assert_1.assert(deficitMadeup.lte(new anchor_1.BN(0)));
|
|
59
|
-
prePegCost = budget.add(deficitMadeup.abs());
|
|
60
|
-
const newAmm = Object.assign({}, amm);
|
|
61
|
-
newAmm.baseAssetReserve = newAmm.baseAssetReserve.mul(pKNumer).div(pKDenom);
|
|
62
|
-
newAmm.sqrtK = newAmm.sqrtK.mul(pKNumer).div(pKDenom);
|
|
63
|
-
const invariant = newAmm.sqrtK.mul(newAmm.sqrtK);
|
|
64
|
-
newAmm.quoteAssetReserve = invariant.div(newAmm.baseAssetReserve);
|
|
65
|
-
const directionToClose = amm.netBaseAssetAmount.gt(numericConstants_1.ZERO)
|
|
66
|
-
? types_1.PositionDirection.SHORT
|
|
67
|
-
: types_1.PositionDirection.LONG;
|
|
68
|
-
const [newQuoteAssetReserve, _newBaseAssetReserve] = calculateAmmReservesAfterSwap(newAmm, 'base', amm.netBaseAssetAmount.abs(), getSwapDirection('base', directionToClose));
|
|
69
|
-
newAmm.terminalQuoteAssetReserve = newQuoteAssetReserve;
|
|
70
|
-
newPeg = repeg_1.calculateBudgetedPeg(newAmm, prePegCost, targetPrice);
|
|
71
|
-
prePegCost = repeg_1.calculateRepegCost(newAmm, newPeg);
|
|
72
|
-
}
|
|
73
|
-
return [prePegCost, pKNumer, pKDenom, newPeg];
|
|
74
|
-
}
|
|
75
|
-
exports.calculateNewAmm = calculateNewAmm;
|
|
76
|
-
function calculateUpdatedAMM(amm, oraclePriceData) {
|
|
77
|
-
if (amm.curveUpdateIntensity == 0) {
|
|
78
|
-
return amm;
|
|
79
|
-
}
|
|
80
|
-
const newAmm = Object.assign({}, amm);
|
|
81
|
-
const [prepegCost, pKNumer, pKDenom, newPeg] = calculateNewAmm(amm, oraclePriceData);
|
|
82
|
-
newAmm.baseAssetReserve = newAmm.baseAssetReserve.mul(pKNumer).div(pKDenom);
|
|
83
|
-
newAmm.sqrtK = newAmm.sqrtK.mul(pKNumer).div(pKDenom);
|
|
84
|
-
const invariant = newAmm.sqrtK.mul(newAmm.sqrtK);
|
|
85
|
-
newAmm.quoteAssetReserve = invariant.div(newAmm.baseAssetReserve);
|
|
86
|
-
newAmm.pegMultiplier = newPeg;
|
|
87
|
-
const directionToClose = amm.netBaseAssetAmount.gt(numericConstants_1.ZERO)
|
|
88
|
-
? types_1.PositionDirection.SHORT
|
|
89
|
-
: types_1.PositionDirection.LONG;
|
|
90
|
-
const [newQuoteAssetReserve, _newBaseAssetReserve] = calculateAmmReservesAfterSwap(newAmm, 'base', amm.netBaseAssetAmount.abs(), getSwapDirection('base', directionToClose));
|
|
91
|
-
newAmm.terminalQuoteAssetReserve = newQuoteAssetReserve;
|
|
92
|
-
newAmm.totalFeeMinusDistributions =
|
|
93
|
-
newAmm.totalFeeMinusDistributions.sub(prepegCost);
|
|
94
|
-
return newAmm;
|
|
95
|
-
}
|
|
96
|
-
exports.calculateUpdatedAMM = calculateUpdatedAMM;
|
|
97
|
-
function calculateUpdatedAMMSpreadReserves(amm, direction, oraclePriceData) {
|
|
98
|
-
const newAmm = calculateUpdatedAMM(amm, oraclePriceData);
|
|
99
|
-
const dirReserves = calculateSpreadReserves(newAmm, direction, oraclePriceData);
|
|
100
|
-
const result = {
|
|
101
|
-
baseAssetReserve: dirReserves.baseAssetReserve,
|
|
102
|
-
quoteAssetReserve: dirReserves.quoteAssetReserve,
|
|
103
|
-
sqrtK: newAmm.sqrtK,
|
|
104
|
-
newPeg: newAmm.pegMultiplier,
|
|
105
|
-
};
|
|
106
|
-
return result;
|
|
107
|
-
}
|
|
108
|
-
exports.calculateUpdatedAMMSpreadReserves = calculateUpdatedAMMSpreadReserves;
|
|
109
|
-
function calculateBidAskPrice(amm, oraclePriceData, withUpdate = true) {
|
|
110
|
-
let newAmm;
|
|
111
|
-
if (withUpdate) {
|
|
112
|
-
newAmm = calculateUpdatedAMM(amm, oraclePriceData);
|
|
113
|
-
}
|
|
114
|
-
else {
|
|
115
|
-
newAmm = amm;
|
|
116
|
-
}
|
|
117
|
-
const askReserves = calculateSpreadReserves(newAmm, types_1.PositionDirection.LONG, oraclePriceData);
|
|
118
|
-
const bidReserves = calculateSpreadReserves(newAmm, types_1.PositionDirection.SHORT, oraclePriceData);
|
|
119
|
-
const askPrice = calculatePrice(askReserves.baseAssetReserve, askReserves.quoteAssetReserve, newAmm.pegMultiplier);
|
|
120
|
-
const bidPrice = calculatePrice(bidReserves.baseAssetReserve, bidReserves.quoteAssetReserve, newAmm.pegMultiplier);
|
|
121
|
-
return [bidPrice, askPrice];
|
|
122
|
-
}
|
|
123
|
-
exports.calculateBidAskPrice = calculateBidAskPrice;
|
|
124
|
-
/**
|
|
125
|
-
* Calculates a price given an arbitrary base and quote amount (they must have the same precision)
|
|
126
|
-
*
|
|
127
|
-
* @param baseAssetReserves
|
|
128
|
-
* @param quoteAssetReserves
|
|
129
|
-
* @param pegMultiplier
|
|
130
|
-
* @returns price : Precision MARK_PRICE_PRECISION
|
|
131
|
-
*/
|
|
132
|
-
function calculatePrice(baseAssetReserves, quoteAssetReserves, pegMultiplier) {
|
|
133
|
-
if (baseAssetReserves.abs().lte(numericConstants_1.ZERO)) {
|
|
134
|
-
return new anchor_1.BN(0);
|
|
135
|
-
}
|
|
136
|
-
return quoteAssetReserves
|
|
137
|
-
.mul(numericConstants_1.MARK_PRICE_PRECISION)
|
|
138
|
-
.mul(pegMultiplier)
|
|
139
|
-
.div(numericConstants_1.PEG_PRECISION)
|
|
140
|
-
.div(baseAssetReserves);
|
|
141
|
-
}
|
|
142
|
-
exports.calculatePrice = calculatePrice;
|
|
143
|
-
/**
|
|
144
|
-
* Calculates what the amm reserves would be after swapping a quote or base asset amount.
|
|
145
|
-
*
|
|
146
|
-
* @param amm
|
|
147
|
-
* @param inputAssetType
|
|
148
|
-
* @param swapAmount
|
|
149
|
-
* @param swapDirection
|
|
150
|
-
* @returns quoteAssetReserve and baseAssetReserve after swap. : Precision AMM_RESERVE_PRECISION
|
|
151
|
-
*/
|
|
152
|
-
function calculateAmmReservesAfterSwap(amm, inputAssetType, swapAmount, swapDirection) {
|
|
153
|
-
assert_1.assert(swapAmount.gte(numericConstants_1.ZERO), 'swapAmount must be greater than 0');
|
|
154
|
-
let newQuoteAssetReserve;
|
|
155
|
-
let newBaseAssetReserve;
|
|
156
|
-
if (inputAssetType === 'quote') {
|
|
157
|
-
swapAmount = swapAmount
|
|
158
|
-
.mul(numericConstants_1.AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO)
|
|
159
|
-
.div(amm.pegMultiplier);
|
|
160
|
-
[newQuoteAssetReserve, newBaseAssetReserve] = calculateSwapOutput(amm.quoteAssetReserve, swapAmount, swapDirection, amm.sqrtK.mul(amm.sqrtK));
|
|
161
|
-
}
|
|
162
|
-
else {
|
|
163
|
-
[newBaseAssetReserve, newQuoteAssetReserve] = calculateSwapOutput(amm.baseAssetReserve, swapAmount, swapDirection, amm.sqrtK.mul(amm.sqrtK));
|
|
164
|
-
}
|
|
165
|
-
return [newQuoteAssetReserve, newBaseAssetReserve];
|
|
166
|
-
}
|
|
167
|
-
exports.calculateAmmReservesAfterSwap = calculateAmmReservesAfterSwap;
|
|
168
|
-
function calculateEffectiveLeverage(baseSpread, quoteAssetReserve, terminalQuoteAssetReserve, pegMultiplier, netBaseAssetAmount, markPrice, totalFeeMinusDistributions) {
|
|
169
|
-
// inventory skew
|
|
170
|
-
const netBaseAssetValue = quoteAssetReserve
|
|
171
|
-
.sub(terminalQuoteAssetReserve)
|
|
172
|
-
.mul(pegMultiplier)
|
|
173
|
-
.div(numericConstants_1.AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO);
|
|
174
|
-
const localBaseAssetValue = netBaseAssetAmount
|
|
175
|
-
.mul(markPrice)
|
|
176
|
-
.div(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO.mul(numericConstants_1.MARK_PRICE_PRECISION));
|
|
177
|
-
const effectiveLeverage = localBaseAssetValue.sub(netBaseAssetValue).toNumber() /
|
|
178
|
-
(Math.max(0, totalFeeMinusDistributions.toNumber()) + 1) +
|
|
179
|
-
1 / numericConstants_1.QUOTE_PRECISION.toNumber();
|
|
180
|
-
return effectiveLeverage;
|
|
181
|
-
}
|
|
182
|
-
exports.calculateEffectiveLeverage = calculateEffectiveLeverage;
|
|
183
|
-
function calculateMaxSpread(marginRatioInitial) {
|
|
184
|
-
const maxTargetSpread = new anchor_1.BN(marginRatioInitial)
|
|
185
|
-
.mul(numericConstants_1.BID_ASK_SPREAD_PRECISION.div(numericConstants_1.MARGIN_PRECISION))
|
|
186
|
-
.toNumber();
|
|
187
|
-
return maxTargetSpread;
|
|
188
|
-
}
|
|
189
|
-
exports.calculateMaxSpread = calculateMaxSpread;
|
|
190
|
-
function calculateSpreadBN(baseSpread, lastOracleMarkSpreadPct, lastOracleConfPct, maxSpread, quoteAssetReserve, terminalQuoteAssetReserve, pegMultiplier, netBaseAssetAmount, markPrice, totalFeeMinusDistributions) {
|
|
191
|
-
let longSpread = baseSpread / 2;
|
|
192
|
-
let shortSpread = baseSpread / 2;
|
|
193
|
-
if (lastOracleMarkSpreadPct.gt(numericConstants_1.ZERO)) {
|
|
194
|
-
shortSpread = Math.max(shortSpread, lastOracleMarkSpreadPct.abs().toNumber() + lastOracleConfPct.toNumber());
|
|
195
|
-
}
|
|
196
|
-
else if (lastOracleMarkSpreadPct.lt(numericConstants_1.ZERO)) {
|
|
197
|
-
longSpread = Math.max(longSpread, lastOracleMarkSpreadPct.abs().toNumber() + lastOracleConfPct.toNumber());
|
|
198
|
-
}
|
|
199
|
-
const maxTargetSpread = maxSpread;
|
|
200
|
-
const MAX_INVENTORY_SKEW = 5;
|
|
201
|
-
const effectiveLeverage = calculateEffectiveLeverage(baseSpread, quoteAssetReserve, terminalQuoteAssetReserve, pegMultiplier, netBaseAssetAmount, markPrice, totalFeeMinusDistributions);
|
|
202
|
-
if (totalFeeMinusDistributions.gt(numericConstants_1.ZERO)) {
|
|
203
|
-
const spreadScale = Math.min(MAX_INVENTORY_SKEW, 1 + effectiveLeverage);
|
|
204
|
-
if (netBaseAssetAmount.gt(numericConstants_1.ZERO)) {
|
|
205
|
-
longSpread *= spreadScale;
|
|
206
|
-
}
|
|
207
|
-
else {
|
|
208
|
-
shortSpread *= spreadScale;
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
else {
|
|
212
|
-
longSpread *= MAX_INVENTORY_SKEW;
|
|
213
|
-
shortSpread *= MAX_INVENTORY_SKEW;
|
|
214
|
-
}
|
|
215
|
-
const totalSpread = longSpread + shortSpread;
|
|
216
|
-
if (totalSpread > maxTargetSpread) {
|
|
217
|
-
if (longSpread > shortSpread) {
|
|
218
|
-
longSpread = Math.min(longSpread, maxTargetSpread);
|
|
219
|
-
shortSpread = maxTargetSpread - longSpread;
|
|
220
|
-
}
|
|
221
|
-
else {
|
|
222
|
-
shortSpread = Math.min(shortSpread, maxTargetSpread);
|
|
223
|
-
longSpread = maxTargetSpread - shortSpread;
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
return [longSpread, shortSpread];
|
|
227
|
-
}
|
|
228
|
-
exports.calculateSpreadBN = calculateSpreadBN;
|
|
229
|
-
function calculateSpread(amm, direction, oraclePriceData) {
|
|
230
|
-
if (amm.baseSpread == 0 || amm.curveUpdateIntensity == 0) {
|
|
231
|
-
return amm.baseSpread / 2;
|
|
232
|
-
}
|
|
233
|
-
const markPrice = calculatePrice(amm.baseAssetReserve, amm.quoteAssetReserve, amm.pegMultiplier);
|
|
234
|
-
const targetPrice = (oraclePriceData === null || oraclePriceData === void 0 ? void 0 : oraclePriceData.price) || markPrice;
|
|
235
|
-
const confInterval = oraclePriceData.confidence || numericConstants_1.ZERO;
|
|
236
|
-
const targetMarkSpreadPct = markPrice
|
|
237
|
-
.sub(targetPrice)
|
|
238
|
-
.mul(numericConstants_1.BID_ASK_SPREAD_PRECISION)
|
|
239
|
-
.div(markPrice);
|
|
240
|
-
const confIntervalPct = confInterval
|
|
241
|
-
.mul(numericConstants_1.BID_ASK_SPREAD_PRECISION)
|
|
242
|
-
.div(markPrice);
|
|
243
|
-
const [longSpread, shortSpread] = calculateSpreadBN(amm.baseSpread, targetMarkSpreadPct, confIntervalPct, amm.maxSpread, amm.quoteAssetReserve, amm.terminalQuoteAssetReserve, amm.pegMultiplier, amm.netBaseAssetAmount, markPrice, amm.totalFeeMinusDistributions);
|
|
244
|
-
let spread;
|
|
245
|
-
if (types_1.isVariant(direction, 'long')) {
|
|
246
|
-
spread = longSpread;
|
|
247
|
-
}
|
|
248
|
-
else {
|
|
249
|
-
spread = shortSpread;
|
|
250
|
-
}
|
|
251
|
-
return spread;
|
|
252
|
-
}
|
|
253
|
-
exports.calculateSpread = calculateSpread;
|
|
254
|
-
function calculateSpreadReserves(amm, direction, oraclePriceData) {
|
|
255
|
-
const spread = calculateSpread(amm, direction, oraclePriceData);
|
|
256
|
-
if (spread === 0) {
|
|
257
|
-
return {
|
|
258
|
-
baseAssetReserve: amm.baseAssetReserve,
|
|
259
|
-
quoteAssetReserve: amm.quoteAssetReserve,
|
|
260
|
-
};
|
|
261
|
-
}
|
|
262
|
-
const quoteAsserReserveDelta = amm.quoteAssetReserve.div(numericConstants_1.BID_ASK_SPREAD_PRECISION.div(new anchor_1.BN(spread / 2)));
|
|
263
|
-
let quoteAssetReserve;
|
|
264
|
-
if (types_1.isVariant(direction, 'long')) {
|
|
265
|
-
quoteAssetReserve = amm.quoteAssetReserve.add(quoteAsserReserveDelta);
|
|
266
|
-
}
|
|
267
|
-
else {
|
|
268
|
-
quoteAssetReserve = amm.quoteAssetReserve.sub(quoteAsserReserveDelta);
|
|
269
|
-
}
|
|
270
|
-
const baseAssetReserve = amm.sqrtK.mul(amm.sqrtK).div(quoteAssetReserve);
|
|
271
|
-
return {
|
|
272
|
-
baseAssetReserve,
|
|
273
|
-
quoteAssetReserve,
|
|
274
|
-
};
|
|
275
|
-
}
|
|
276
|
-
exports.calculateSpreadReserves = calculateSpreadReserves;
|
|
277
|
-
/**
|
|
278
|
-
* Helper function calculating constant product curve output. Agnostic to whether input asset is quote or base
|
|
279
|
-
*
|
|
280
|
-
* @param inputAssetReserve
|
|
281
|
-
* @param swapAmount
|
|
282
|
-
* @param swapDirection
|
|
283
|
-
* @param invariant
|
|
284
|
-
* @returns newInputAssetReserve and newOutputAssetReserve after swap. : Precision AMM_RESERVE_PRECISION
|
|
285
|
-
*/
|
|
286
|
-
function calculateSwapOutput(inputAssetReserve, swapAmount, swapDirection, invariant) {
|
|
287
|
-
let newInputAssetReserve;
|
|
288
|
-
if (swapDirection === types_1.SwapDirection.ADD) {
|
|
289
|
-
newInputAssetReserve = inputAssetReserve.add(swapAmount);
|
|
290
|
-
}
|
|
291
|
-
else {
|
|
292
|
-
newInputAssetReserve = inputAssetReserve.sub(swapAmount);
|
|
293
|
-
}
|
|
294
|
-
const newOutputAssetReserve = invariant.div(newInputAssetReserve);
|
|
295
|
-
return [newInputAssetReserve, newOutputAssetReserve];
|
|
296
|
-
}
|
|
297
|
-
exports.calculateSwapOutput = calculateSwapOutput;
|
|
298
|
-
/**
|
|
299
|
-
* Translate long/shorting quote/base asset into amm operation
|
|
300
|
-
*
|
|
301
|
-
* @param inputAssetType
|
|
302
|
-
* @param positionDirection
|
|
303
|
-
*/
|
|
304
|
-
function getSwapDirection(inputAssetType, positionDirection) {
|
|
305
|
-
if (types_1.isVariant(positionDirection, 'long') && inputAssetType === 'base') {
|
|
306
|
-
return types_1.SwapDirection.REMOVE;
|
|
307
|
-
}
|
|
308
|
-
if (types_1.isVariant(positionDirection, 'short') && inputAssetType === 'quote') {
|
|
309
|
-
return types_1.SwapDirection.REMOVE;
|
|
310
|
-
}
|
|
311
|
-
return types_1.SwapDirection.ADD;
|
|
312
|
-
}
|
|
313
|
-
exports.getSwapDirection = getSwapDirection;
|
|
314
|
-
/**
|
|
315
|
-
* Helper function calculating terminal price of amm
|
|
316
|
-
*
|
|
317
|
-
* @param market
|
|
318
|
-
* @returns cost : Precision MARK_PRICE_PRECISION
|
|
319
|
-
*/
|
|
320
|
-
function calculateTerminalPrice(market) {
|
|
321
|
-
const directionToClose = market.amm.netBaseAssetAmount.gt(numericConstants_1.ZERO)
|
|
322
|
-
? types_1.PositionDirection.SHORT
|
|
323
|
-
: types_1.PositionDirection.LONG;
|
|
324
|
-
const [newQuoteAssetReserve, newBaseAssetReserve] = calculateAmmReservesAfterSwap(market.amm, 'base', market.amm.netBaseAssetAmount.abs(), getSwapDirection('base', directionToClose));
|
|
325
|
-
const terminalPrice = newQuoteAssetReserve
|
|
326
|
-
.mul(numericConstants_1.MARK_PRICE_PRECISION)
|
|
327
|
-
.mul(market.amm.pegMultiplier)
|
|
328
|
-
.div(numericConstants_1.PEG_PRECISION)
|
|
329
|
-
.div(newBaseAssetReserve);
|
|
330
|
-
return terminalPrice;
|
|
331
|
-
}
|
|
332
|
-
exports.calculateTerminalPrice = calculateTerminalPrice;
|
|
333
|
-
function calculateMaxBaseAssetAmountToTrade(amm, limit_price, direction, oraclePriceData) {
|
|
334
|
-
const invariant = amm.sqrtK.mul(amm.sqrtK);
|
|
335
|
-
const newBaseAssetReserveSquared = invariant
|
|
336
|
-
.mul(numericConstants_1.MARK_PRICE_PRECISION)
|
|
337
|
-
.mul(amm.pegMultiplier)
|
|
338
|
-
.div(limit_price)
|
|
339
|
-
.div(numericConstants_1.PEG_PRECISION);
|
|
340
|
-
const newBaseAssetReserve = __1.squareRootBN(newBaseAssetReserveSquared);
|
|
341
|
-
const baseAssetReserveBefore = calculateSpreadReserves(amm, direction, oraclePriceData).baseAssetReserve;
|
|
342
|
-
if (newBaseAssetReserve.gt(baseAssetReserveBefore)) {
|
|
343
|
-
return [
|
|
344
|
-
newBaseAssetReserve.sub(baseAssetReserveBefore),
|
|
345
|
-
types_1.PositionDirection.SHORT,
|
|
346
|
-
];
|
|
347
|
-
}
|
|
348
|
-
else if (newBaseAssetReserve.lt(baseAssetReserveBefore)) {
|
|
349
|
-
return [
|
|
350
|
-
baseAssetReserveBefore.sub(newBaseAssetReserve),
|
|
351
|
-
types_1.PositionDirection.LONG,
|
|
352
|
-
];
|
|
353
|
-
}
|
|
354
|
-
else {
|
|
355
|
-
console.log('tradeSize Too Small');
|
|
356
|
-
return [new anchor_1.BN(0), types_1.PositionDirection.LONG];
|
|
357
|
-
}
|
|
358
|
-
}
|
|
359
|
-
exports.calculateMaxBaseAssetAmountToTrade = calculateMaxBaseAssetAmountToTrade;
|
|
360
|
-
function calculateQuoteAssetAmountSwapped(quoteAssetReserves, pegMultiplier, swapDirection) {
|
|
361
|
-
let quoteAssetAmount = quoteAssetReserves
|
|
362
|
-
.mul(pegMultiplier)
|
|
363
|
-
.div(numericConstants_1.AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO);
|
|
364
|
-
if (types_1.isVariant(swapDirection, 'remove')) {
|
|
365
|
-
quoteAssetAmount = quoteAssetAmount.add(numericConstants_1.ONE);
|
|
366
|
-
}
|
|
367
|
-
return quoteAssetAmount;
|
|
368
|
-
}
|
|
369
|
-
exports.calculateQuoteAssetAmountSwapped = calculateQuoteAssetAmountSwapped;
|
package/src/math/auction.js
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getAuctionPrice = exports.isAuctionComplete = void 0;
|
|
4
|
-
const types_1 = require("../types");
|
|
5
|
-
const _1 = require("../.");
|
|
6
|
-
function isAuctionComplete(order, slot) {
|
|
7
|
-
if (order.auctionDuration === 0) {
|
|
8
|
-
return true;
|
|
9
|
-
}
|
|
10
|
-
return new _1.BN(slot).sub(order.slot).gt(new _1.BN(order.auctionDuration));
|
|
11
|
-
}
|
|
12
|
-
exports.isAuctionComplete = isAuctionComplete;
|
|
13
|
-
function getAuctionPrice(order, slot) {
|
|
14
|
-
const slotsElapsed = new _1.BN(slot).sub(order.slot);
|
|
15
|
-
const deltaDenominator = new _1.BN(order.auctionDuration);
|
|
16
|
-
const deltaNumerator = _1.BN.min(slotsElapsed, deltaDenominator);
|
|
17
|
-
if (deltaDenominator.eq(_1.ZERO)) {
|
|
18
|
-
return order.auctionEndPrice;
|
|
19
|
-
}
|
|
20
|
-
let priceDelta;
|
|
21
|
-
if (types_1.isVariant(order.direction, 'long')) {
|
|
22
|
-
priceDelta = order.auctionEndPrice
|
|
23
|
-
.sub(order.auctionStartPrice)
|
|
24
|
-
.mul(deltaNumerator)
|
|
25
|
-
.div(deltaDenominator);
|
|
26
|
-
}
|
|
27
|
-
else {
|
|
28
|
-
priceDelta = order.auctionStartPrice
|
|
29
|
-
.sub(order.auctionEndPrice)
|
|
30
|
-
.mul(deltaNumerator)
|
|
31
|
-
.div(deltaDenominator);
|
|
32
|
-
}
|
|
33
|
-
let price;
|
|
34
|
-
if (types_1.isVariant(order.direction, 'long')) {
|
|
35
|
-
price = order.auctionStartPrice.add(priceDelta);
|
|
36
|
-
}
|
|
37
|
-
else {
|
|
38
|
-
price = order.auctionStartPrice.sub(priceDelta);
|
|
39
|
-
}
|
|
40
|
-
return price;
|
|
41
|
-
}
|
|
42
|
-
exports.getAuctionPrice = getAuctionPrice;
|
package/src/math/conversion.js
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.convertToNumber = void 0;
|
|
4
|
-
const numericConstants_1 = require("../constants/numericConstants");
|
|
5
|
-
const convertToNumber = (bigNumber, precision = numericConstants_1.MARK_PRICE_PRECISION) => {
|
|
6
|
-
if (!bigNumber)
|
|
7
|
-
return 0;
|
|
8
|
-
return (bigNumber.div(precision).toNumber() +
|
|
9
|
-
bigNumber.mod(precision).toNumber() / precision.toNumber());
|
|
10
|
-
};
|
|
11
|
-
exports.convertToNumber = convertToNumber;
|
package/src/math/funding.js
DELETED
|
@@ -1,248 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.calculateFundingPool = exports.calculateLongShortFundingRateAndLiveTwaps = exports.calculateLongShortFundingRate = exports.calculateEstimatedFundingRate = exports.calculateAllEstimatedFundingRate = void 0;
|
|
13
|
-
const anchor_1 = require("@project-serum/anchor");
|
|
14
|
-
const numericConstants_1 = require("../constants/numericConstants");
|
|
15
|
-
const market_1 = require("./market");
|
|
16
|
-
/**
|
|
17
|
-
*
|
|
18
|
-
* @param market
|
|
19
|
-
* @param oraclePriceData
|
|
20
|
-
* @param periodAdjustment
|
|
21
|
-
* @returns Estimated funding rate. : Precision //TODO-PRECISION
|
|
22
|
-
*/
|
|
23
|
-
function calculateAllEstimatedFundingRate(market, oraclePriceData, periodAdjustment = new anchor_1.BN(1)) {
|
|
24
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
25
|
-
// periodAdjustment
|
|
26
|
-
// 1: hourly
|
|
27
|
-
// 24: daily
|
|
28
|
-
// 24 * 365.25: annualized
|
|
29
|
-
const secondsInHour = new anchor_1.BN(3600);
|
|
30
|
-
const hoursInDay = new anchor_1.BN(24);
|
|
31
|
-
const ONE = new anchor_1.BN(1);
|
|
32
|
-
if (!market.initialized) {
|
|
33
|
-
return [numericConstants_1.ZERO, numericConstants_1.ZERO, numericConstants_1.ZERO, numericConstants_1.ZERO, numericConstants_1.ZERO];
|
|
34
|
-
}
|
|
35
|
-
const payFreq = new anchor_1.BN(market.amm.fundingPeriod);
|
|
36
|
-
// todo: sufficiently differs from blockchain timestamp?
|
|
37
|
-
const now = new anchor_1.BN((Date.now() / 1000).toFixed(0));
|
|
38
|
-
const timeSinceLastUpdate = now.sub(market.amm.lastFundingRateTs);
|
|
39
|
-
// calculate real-time mark twap
|
|
40
|
-
const lastMarkTwapWithMantissa = market.amm.lastMarkPriceTwap;
|
|
41
|
-
const lastMarkPriceTwapTs = market.amm.lastMarkPriceTwapTs;
|
|
42
|
-
const timeSinceLastMarkChange = now.sub(lastMarkPriceTwapTs);
|
|
43
|
-
const markTwapTimeSinceLastUpdate = anchor_1.BN.max(secondsInHour, anchor_1.BN.max(numericConstants_1.ZERO, secondsInHour.sub(timeSinceLastMarkChange)));
|
|
44
|
-
const baseAssetPriceWithMantissa = market_1.calculateMarkPrice(market, oraclePriceData);
|
|
45
|
-
const markTwapWithMantissa = markTwapTimeSinceLastUpdate
|
|
46
|
-
.mul(lastMarkTwapWithMantissa)
|
|
47
|
-
.add(timeSinceLastMarkChange.mul(baseAssetPriceWithMantissa))
|
|
48
|
-
.div(timeSinceLastMarkChange.add(markTwapTimeSinceLastUpdate));
|
|
49
|
-
// calculate real-time (predicted) oracle twap
|
|
50
|
-
// note: oracle twap depends on `when the chord is struck` (market is trade)
|
|
51
|
-
const lastOracleTwapWithMantissa = market.amm.lastOraclePriceTwap;
|
|
52
|
-
const lastOraclePriceTwapTs = market.amm.lastOraclePriceTwapTs;
|
|
53
|
-
const oracleInvalidDuration = anchor_1.BN.max(numericConstants_1.ZERO, lastMarkPriceTwapTs.sub(lastOraclePriceTwapTs));
|
|
54
|
-
const timeSinceLastOracleTwapUpdate = now.sub(lastOraclePriceTwapTs);
|
|
55
|
-
const oracleTwapTimeSinceLastUpdate = anchor_1.BN.max(ONE, anchor_1.BN.min(secondsInHour, anchor_1.BN.max(ONE, secondsInHour.sub(timeSinceLastOracleTwapUpdate))));
|
|
56
|
-
let oracleTwapWithMantissa = lastOracleTwapWithMantissa;
|
|
57
|
-
// if passing live oracle data, improve predicted calc estimate
|
|
58
|
-
if (oraclePriceData) {
|
|
59
|
-
const oraclePrice = oraclePriceData.price;
|
|
60
|
-
const oracleLiveVsTwap = oraclePrice
|
|
61
|
-
.sub(lastOracleTwapWithMantissa)
|
|
62
|
-
.abs()
|
|
63
|
-
.mul(numericConstants_1.MARK_PRICE_PRECISION)
|
|
64
|
-
.mul(new anchor_1.BN(100))
|
|
65
|
-
.div(lastOracleTwapWithMantissa);
|
|
66
|
-
// verify pyth live input is within 10% of last twap for live update
|
|
67
|
-
if (oracleLiveVsTwap.lte(numericConstants_1.MARK_PRICE_PRECISION.mul(new anchor_1.BN(10)))) {
|
|
68
|
-
oracleTwapWithMantissa = oracleTwapTimeSinceLastUpdate
|
|
69
|
-
.mul(lastOracleTwapWithMantissa)
|
|
70
|
-
.add(timeSinceLastMarkChange.mul(oraclePrice))
|
|
71
|
-
.div(timeSinceLastMarkChange.add(oracleTwapTimeSinceLastUpdate));
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
const shrunkLastOracleTwapwithMantissa = oracleTwapTimeSinceLastUpdate
|
|
75
|
-
.mul(lastOracleTwapWithMantissa)
|
|
76
|
-
.add(oracleInvalidDuration.mul(lastMarkTwapWithMantissa))
|
|
77
|
-
.div(oracleTwapTimeSinceLastUpdate.add(oracleInvalidDuration));
|
|
78
|
-
const twapSpread = lastMarkTwapWithMantissa.sub(shrunkLastOracleTwapwithMantissa);
|
|
79
|
-
const twapSpreadPct = twapSpread
|
|
80
|
-
.mul(numericConstants_1.MARK_PRICE_PRECISION)
|
|
81
|
-
.mul(new anchor_1.BN(100))
|
|
82
|
-
.div(shrunkLastOracleTwapwithMantissa);
|
|
83
|
-
const lowerboundEst = twapSpreadPct
|
|
84
|
-
.mul(payFreq)
|
|
85
|
-
.mul(anchor_1.BN.min(secondsInHour, timeSinceLastUpdate))
|
|
86
|
-
.mul(periodAdjustment)
|
|
87
|
-
.div(secondsInHour)
|
|
88
|
-
.div(secondsInHour)
|
|
89
|
-
.div(hoursInDay);
|
|
90
|
-
const interpEst = twapSpreadPct.mul(periodAdjustment).div(hoursInDay);
|
|
91
|
-
const interpRateQuote = twapSpreadPct
|
|
92
|
-
.mul(periodAdjustment)
|
|
93
|
-
.div(hoursInDay)
|
|
94
|
-
.div(numericConstants_1.MARK_PRICE_PRECISION.div(numericConstants_1.QUOTE_PRECISION));
|
|
95
|
-
let feePoolSize = calculateFundingPool(market);
|
|
96
|
-
if (interpRateQuote.lt(new anchor_1.BN(0))) {
|
|
97
|
-
feePoolSize = feePoolSize.mul(new anchor_1.BN(-1));
|
|
98
|
-
}
|
|
99
|
-
let cappedAltEst;
|
|
100
|
-
let largerSide;
|
|
101
|
-
let smallerSide;
|
|
102
|
-
if (market.baseAssetAmountLong.gt(market.baseAssetAmountShort.abs())) {
|
|
103
|
-
largerSide = market.baseAssetAmountLong.abs();
|
|
104
|
-
smallerSide = market.baseAssetAmountShort.abs();
|
|
105
|
-
if (twapSpread.gt(new anchor_1.BN(0))) {
|
|
106
|
-
return [
|
|
107
|
-
markTwapWithMantissa,
|
|
108
|
-
oracleTwapWithMantissa,
|
|
109
|
-
lowerboundEst,
|
|
110
|
-
interpEst,
|
|
111
|
-
interpEst,
|
|
112
|
-
];
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
else if (market.baseAssetAmountLong.lt(market.baseAssetAmountShort.abs())) {
|
|
116
|
-
largerSide = market.baseAssetAmountShort.abs();
|
|
117
|
-
smallerSide = market.baseAssetAmountLong.abs();
|
|
118
|
-
if (twapSpread.lt(new anchor_1.BN(0))) {
|
|
119
|
-
return [
|
|
120
|
-
markTwapWithMantissa,
|
|
121
|
-
oracleTwapWithMantissa,
|
|
122
|
-
lowerboundEst,
|
|
123
|
-
interpEst,
|
|
124
|
-
interpEst,
|
|
125
|
-
];
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
else {
|
|
129
|
-
return [
|
|
130
|
-
markTwapWithMantissa,
|
|
131
|
-
oracleTwapWithMantissa,
|
|
132
|
-
lowerboundEst,
|
|
133
|
-
interpEst,
|
|
134
|
-
interpEst,
|
|
135
|
-
];
|
|
136
|
-
}
|
|
137
|
-
if (largerSide.gt(numericConstants_1.ZERO)) {
|
|
138
|
-
// funding smaller flow
|
|
139
|
-
cappedAltEst = smallerSide.mul(twapSpread).div(hoursInDay);
|
|
140
|
-
const feePoolTopOff = feePoolSize
|
|
141
|
-
.mul(numericConstants_1.MARK_PRICE_PRECISION.div(numericConstants_1.QUOTE_PRECISION))
|
|
142
|
-
.mul(numericConstants_1.AMM_RESERVE_PRECISION);
|
|
143
|
-
cappedAltEst = cappedAltEst.add(feePoolTopOff).div(largerSide);
|
|
144
|
-
cappedAltEst = cappedAltEst
|
|
145
|
-
.mul(numericConstants_1.MARK_PRICE_PRECISION)
|
|
146
|
-
.mul(new anchor_1.BN(100))
|
|
147
|
-
.div(oracleTwapWithMantissa)
|
|
148
|
-
.mul(periodAdjustment);
|
|
149
|
-
if (cappedAltEst.abs().gte(interpEst.abs())) {
|
|
150
|
-
cappedAltEst = interpEst;
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
else {
|
|
154
|
-
cappedAltEst = interpEst;
|
|
155
|
-
}
|
|
156
|
-
return [
|
|
157
|
-
markTwapWithMantissa,
|
|
158
|
-
oracleTwapWithMantissa,
|
|
159
|
-
lowerboundEst,
|
|
160
|
-
cappedAltEst,
|
|
161
|
-
interpEst,
|
|
162
|
-
];
|
|
163
|
-
});
|
|
164
|
-
}
|
|
165
|
-
exports.calculateAllEstimatedFundingRate = calculateAllEstimatedFundingRate;
|
|
166
|
-
/**
|
|
167
|
-
*
|
|
168
|
-
* @param market
|
|
169
|
-
* @param oraclePriceData
|
|
170
|
-
* @param periodAdjustment
|
|
171
|
-
* @param estimationMethod
|
|
172
|
-
* @returns Estimated funding rate. : Precision //TODO-PRECISION
|
|
173
|
-
*/
|
|
174
|
-
function calculateEstimatedFundingRate(market, oraclePriceData, periodAdjustment = new anchor_1.BN(1), estimationMethod) {
|
|
175
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
176
|
-
const [_1, _2, lowerboundEst, cappedAltEst, interpEst] = yield calculateAllEstimatedFundingRate(market, oraclePriceData, periodAdjustment);
|
|
177
|
-
if (estimationMethod == 'lowerbound') {
|
|
178
|
-
//assuming remaining funding period has no gap
|
|
179
|
-
return lowerboundEst;
|
|
180
|
-
}
|
|
181
|
-
else if (estimationMethod == 'capped') {
|
|
182
|
-
return cappedAltEst;
|
|
183
|
-
}
|
|
184
|
-
else {
|
|
185
|
-
return interpEst;
|
|
186
|
-
}
|
|
187
|
-
});
|
|
188
|
-
}
|
|
189
|
-
exports.calculateEstimatedFundingRate = calculateEstimatedFundingRate;
|
|
190
|
-
/**
|
|
191
|
-
*
|
|
192
|
-
* @param market
|
|
193
|
-
* @param oraclePriceData
|
|
194
|
-
* @param periodAdjustment
|
|
195
|
-
* @returns Estimated funding rate. : Precision //TODO-PRECISION
|
|
196
|
-
*/
|
|
197
|
-
function calculateLongShortFundingRate(market, oraclePriceData, periodAdjustment = new anchor_1.BN(1)) {
|
|
198
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
199
|
-
const [_1, _2, _, cappedAltEst, interpEst] = yield calculateAllEstimatedFundingRate(market, oraclePriceData, periodAdjustment);
|
|
200
|
-
if (market.baseAssetAmountLong.gt(market.baseAssetAmountShort)) {
|
|
201
|
-
return [cappedAltEst, interpEst];
|
|
202
|
-
}
|
|
203
|
-
else if (market.baseAssetAmountLong.lt(market.baseAssetAmountShort)) {
|
|
204
|
-
return [interpEst, cappedAltEst];
|
|
205
|
-
}
|
|
206
|
-
else {
|
|
207
|
-
return [interpEst, interpEst];
|
|
208
|
-
}
|
|
209
|
-
});
|
|
210
|
-
}
|
|
211
|
-
exports.calculateLongShortFundingRate = calculateLongShortFundingRate;
|
|
212
|
-
/**
|
|
213
|
-
*
|
|
214
|
-
* @param market
|
|
215
|
-
* @param oraclePriceData
|
|
216
|
-
* @param periodAdjustment
|
|
217
|
-
* @returns Estimated funding rate. : Precision //TODO-PRECISION
|
|
218
|
-
*/
|
|
219
|
-
function calculateLongShortFundingRateAndLiveTwaps(market, oraclePriceData, periodAdjustment = new anchor_1.BN(1)) {
|
|
220
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
221
|
-
const [markTwapLive, oracleTwapLive, _2, cappedAltEst, interpEst] = yield calculateAllEstimatedFundingRate(market, oraclePriceData, periodAdjustment);
|
|
222
|
-
if (market.baseAssetAmountLong.gt(market.baseAssetAmountShort.abs())) {
|
|
223
|
-
return [markTwapLive, oracleTwapLive, cappedAltEst, interpEst];
|
|
224
|
-
}
|
|
225
|
-
else if (market.baseAssetAmountLong.lt(market.baseAssetAmountShort.abs())) {
|
|
226
|
-
return [markTwapLive, oracleTwapLive, interpEst, cappedAltEst];
|
|
227
|
-
}
|
|
228
|
-
else {
|
|
229
|
-
return [markTwapLive, oracleTwapLive, interpEst, interpEst];
|
|
230
|
-
}
|
|
231
|
-
});
|
|
232
|
-
}
|
|
233
|
-
exports.calculateLongShortFundingRateAndLiveTwaps = calculateLongShortFundingRateAndLiveTwaps;
|
|
234
|
-
/**
|
|
235
|
-
*
|
|
236
|
-
* @param market
|
|
237
|
-
* @returns Estimated fee pool size
|
|
238
|
-
*/
|
|
239
|
-
function calculateFundingPool(market) {
|
|
240
|
-
// todo
|
|
241
|
-
const totalFeeLB = market.amm.totalExchangeFee.div(new anchor_1.BN(2));
|
|
242
|
-
const feePool = anchor_1.BN.max(numericConstants_1.ZERO, market.amm.totalFeeMinusDistributions
|
|
243
|
-
.sub(totalFeeLB)
|
|
244
|
-
.mul(new anchor_1.BN(1))
|
|
245
|
-
.div(new anchor_1.BN(3)));
|
|
246
|
-
return feePool;
|
|
247
|
-
}
|
|
248
|
-
exports.calculateFundingPool = calculateFundingPool;
|