@drift-labs/sdk 0.2.0-master.3 → 0.2.0-master.30
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/README.md +13 -13
- 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/pollingClearingHouseAccountSubscriber.d.ts +16 -16
- package/lib/accounts/pollingClearingHouseAccountSubscriber.js +31 -28
- package/lib/accounts/pollingUserStatsAccountSubscriber.d.ts +27 -0
- package/lib/accounts/pollingUserStatsAccountSubscriber.js +113 -0
- package/lib/accounts/types.d.ts +22 -11
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +17 -17
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +37 -35
- package/lib/accounts/webSocketUserStatsAccountSubsriber.d.ts +20 -0
- package/lib/accounts/webSocketUserStatsAccountSubsriber.js +47 -0
- package/lib/addresses/marketAddresses.d.ts +1 -3
- package/lib/addresses/pda.d.ts +10 -4
- package/lib/addresses/pda.js +51 -14
- package/lib/admin.d.ts +34 -22
- package/lib/admin.js +182 -73
- package/lib/clearingHouse.d.ts +120 -42
- package/lib/clearingHouse.js +1505 -254
- package/lib/clearingHouseConfig.d.ts +4 -4
- package/lib/clearingHouseUser.d.ts +50 -38
- package/lib/clearingHouseUser.js +410 -190
- package/lib/clearingHouseUserStats.d.ts +18 -0
- package/lib/clearingHouseUserStats.js +49 -0
- package/lib/clearingHouseUserStatsConfig.d.ts +14 -0
- package/lib/clearingHouseUserStatsConfig.js +2 -0
- package/lib/config.d.ts +7 -9
- package/lib/config.js +21 -21
- package/lib/constants/numericConstants.d.ts +18 -12
- package/lib/constants/numericConstants.js +28 -21
- package/lib/constants/perpMarkets.d.ts +18 -0
- package/lib/constants/{markets.js → perpMarkets.js} +7 -7
- package/lib/constants/spotMarkets.d.ts +19 -0
- package/lib/constants/spotMarkets.js +53 -0
- package/lib/dlob/DLOB.d.ts +73 -0
- package/lib/dlob/DLOB.js +553 -0
- package/lib/dlob/DLOBNode.d.ts +52 -0
- package/lib/dlob/DLOBNode.js +82 -0
- package/lib/dlob/NodeList.d.ts +26 -0
- package/lib/dlob/NodeList.js +138 -0
- package/lib/events/eventList.js +3 -0
- package/lib/events/eventSubscriber.d.ts +4 -2
- package/lib/events/eventSubscriber.js +16 -9
- package/lib/events/fetchLogs.d.ts +10 -1
- package/lib/events/fetchLogs.js +27 -7
- package/lib/events/pollingLogProvider.d.ts +2 -1
- package/lib/events/pollingLogProvider.js +6 -2
- package/lib/events/sort.js +8 -11
- package/lib/events/types.d.ts +7 -2
- package/lib/events/types.js +5 -0
- package/lib/examples/makeTradeExample.js +20 -8
- package/lib/factory/bigNum.d.ts +1 -0
- package/lib/factory/bigNum.js +34 -10
- package/lib/idl/clearing_house.json +4313 -1433
- package/lib/idl/{mock_usdc_faucet.json → token_faucet.json} +46 -23
- package/lib/index.d.ts +21 -6
- package/lib/index.js +25 -6
- package/lib/math/amm.d.ts +8 -5
- package/lib/math/amm.js +68 -46
- package/lib/math/auction.js +4 -1
- package/lib/math/conversion.js +1 -1
- package/lib/math/funding.d.ts +6 -6
- package/lib/math/funding.js +11 -10
- package/lib/math/insurance.d.ts +4 -0
- package/lib/math/insurance.js +27 -0
- package/lib/math/margin.d.ts +11 -0
- package/lib/math/margin.js +77 -0
- package/lib/math/market.d.ts +14 -9
- package/lib/math/market.js +69 -7
- package/lib/math/oracles.d.ts +4 -0
- package/lib/math/oracles.js +36 -8
- package/lib/math/orders.d.ts +6 -2
- package/lib/math/orders.js +78 -3
- package/lib/math/position.d.ts +21 -13
- package/lib/math/position.js +76 -36
- package/lib/math/repeg.js +14 -5
- package/lib/math/spotBalance.d.ts +22 -0
- package/lib/math/spotBalance.js +193 -0
- package/lib/math/spotMarket.d.ts +4 -0
- package/lib/math/spotMarket.js +8 -0
- package/lib/math/spotPosition.d.ts +6 -0
- package/lib/math/spotPosition.js +23 -0
- package/lib/math/trade.d.ts +10 -10
- package/lib/math/trade.js +22 -29
- package/lib/oracles/pythClient.js +1 -1
- package/lib/oracles/quoteAssetOracleClient.js +1 -1
- package/lib/oracles/switchboardClient.js +1 -1
- package/lib/orderParams.d.ts +14 -5
- package/lib/orderParams.js +12 -92
- 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/slot/SlotSubscriber.d.ts +7 -0
- package/lib/slot/SlotSubscriber.js +3 -0
- package/lib/{mockUSDCFaucet.d.ts → tokenFaucet.d.ts} +8 -5
- package/lib/{mockUSDCFaucet.js → tokenFaucet.js} +63 -51
- package/lib/tx/retryTxSender.d.ts +1 -1
- package/lib/tx/retryTxSender.js +13 -4
- package/lib/tx/types.d.ts +1 -1
- package/lib/tx/utils.js +1 -1
- package/lib/types.d.ts +474 -123
- package/lib/types.js +99 -5
- package/lib/userMap/userMap.d.ts +25 -0
- package/lib/userMap/userMap.js +73 -0
- package/lib/userMap/userStatsMap.d.ts +19 -0
- package/lib/userMap/userStatsMap.js +68 -0
- package/lib/util/computeUnits.js +1 -1
- package/lib/util/getTokenAddress.d.ts +2 -0
- package/lib/util/getTokenAddress.js +9 -0
- package/package.json +9 -5
- package/src/accounts/bulkUserStatsSubscription.ts +33 -0
- package/src/accounts/bulkUserSubscription.ts +0 -1
- package/src/accounts/fetch.ts +27 -2
- package/src/accounts/pollingClearingHouseAccountSubscriber.ts +46 -42
- package/src/accounts/pollingUserStatsAccountSubscriber.ts +172 -0
- package/src/accounts/types.ts +31 -11
- package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +64 -59
- package/src/accounts/webSocketUserStatsAccountSubsriber.ts +80 -0
- package/src/addresses/marketAddresses.ts +1 -2
- package/src/addresses/pda.ts +88 -14
- package/src/admin.ts +333 -128
- package/src/assert/assert.js +9 -0
- package/src/clearingHouse.ts +2464 -458
- package/src/clearingHouseConfig.ts +4 -3
- package/src/clearingHouseUser.ts +747 -291
- package/src/clearingHouseUserStats.ts +75 -0
- package/src/clearingHouseUserStatsConfig.ts +18 -0
- package/src/config.ts +30 -31
- package/src/constants/numericConstants.ts +41 -25
- package/src/constants/{markets.ts → perpMarkets.ts} +10 -10
- package/src/constants/spotMarkets.ts +72 -0
- package/src/dlob/DLOB.ts +868 -0
- package/src/dlob/DLOBNode.ts +162 -0
- package/src/dlob/NodeList.ts +185 -0
- package/src/events/eventList.js +77 -0
- package/src/events/eventList.ts +3 -0
- package/src/events/eventSubscriber.ts +20 -12
- package/src/events/fetchLogs.ts +35 -8
- package/src/events/pollingLogProvider.ts +10 -2
- package/src/events/sort.ts +11 -15
- package/src/events/types.ts +16 -1
- package/src/examples/makeTradeExample.js +157 -0
- package/src/examples/makeTradeExample.ts +32 -14
- package/src/factory/bigNum.ts +42 -13
- package/src/idl/clearing_house.json +4313 -1433
- package/src/idl/{mock_usdc_faucet.json → token_faucet.json} +46 -23
- package/src/index.ts +21 -6
- package/src/math/amm.ts +136 -66
- package/src/math/auction.ts +5 -1
- package/src/math/conversion.ts +2 -2
- package/src/math/funding.ts +20 -18
- package/src/math/insurance.ts +35 -0
- package/src/math/margin.ts +127 -0
- package/src/math/market.ts +138 -12
- package/src/math/oracles.ts +63 -9
- package/src/math/orders.ts +138 -4
- package/src/math/position.ts +119 -58
- package/src/math/repeg.ts +16 -6
- package/src/math/spotBalance.ts +316 -0
- package/src/math/spotMarket.ts +9 -0
- package/src/math/spotPosition.ts +47 -0
- package/src/math/trade.ts +43 -49
- package/src/oracles/pythClient.ts +2 -2
- package/src/oracles/quoteAssetOracleClient.ts +2 -2
- package/src/oracles/switchboardClient.ts +2 -2
- package/src/orderParams.ts +24 -137
- package/src/serum/serumSubscriber.ts +80 -0
- package/src/serum/types.ts +13 -0
- package/src/slot/SlotSubscriber.ts +11 -1
- package/src/token/index.js +38 -0
- package/src/{mockUSDCFaucet.ts → tokenFaucet.ts} +82 -70
- package/src/tx/retryTxSender.ts +16 -5
- package/src/tx/types.js +2 -0
- package/src/tx/types.ts +2 -1
- package/src/tx/utils.js +17 -0
- package/src/tx/utils.ts +1 -1
- package/src/types.ts +477 -125
- package/src/userMap/userMap.ts +100 -0
- package/src/userMap/userStatsMap.ts +110 -0
- package/src/util/computeUnits.js +21 -11
- package/src/util/computeUnits.ts +1 -1
- package/src/util/getTokenAddress.js +9 -0
- package/src/util/getTokenAddress.ts +18 -0
- package/src/util/promiseTimeout.js +14 -0
- package/src/util/tps.js +27 -0
- package/tests/bn/test.ts +12 -3
- package/tests/dlob/helpers.ts +374 -0
- package/tests/dlob/test.ts +2865 -0
- package/lib/constants/banks.d.ts +0 -16
- package/lib/constants/banks.js +0 -34
- package/lib/constants/markets.d.ts +0 -19
- package/lib/math/bankBalance.d.ts +0 -9
- package/lib/math/bankBalance.js +0 -75
- package/lib/math/state.d.ts +0 -8
- package/lib/math/state.js +0 -15
- package/lib/orders.d.ts +0 -8
- package/lib/orders.js +0 -134
- package/src/constants/banks.ts +0 -43
- package/src/math/bankBalance.ts +0 -112
- package/src/math/state.ts +0 -14
- package/src/math/utils.js +0 -27
- package/src/math/utils.js.map +0 -1
- package/src/orders.ts +0 -244
- package/src/util/computeUnits.js.map +0 -1
package/lib/math/position.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isEmptyPosition = exports.positionCurrentDirection = exports.findDirectionToClose = exports.calculateEntryPrice = exports.positionIsAvailable = exports.calculatePositionFundingPNL = exports.calculatePositionPNL = exports.calculateBaseAssetValue = void 0;
|
|
3
|
+
exports.isEmptyPosition = exports.positionCurrentDirection = exports.findDirectionToClose = exports.calculateCostBasis = exports.calculateEntryPrice = exports.positionIsAvailable = exports.calculatePositionFundingPNL = exports.calculateClaimablePnl = exports.calculatePositionPNL = exports.calculateBaseAssetValue = void 0;
|
|
4
4
|
const __1 = require("../");
|
|
5
5
|
const numericConstants_1 = require("../constants/numericConstants");
|
|
6
6
|
const types_1 = require("../types");
|
|
7
7
|
const amm_1 = require("./amm");
|
|
8
|
+
const margin_1 = require("./margin");
|
|
9
|
+
const market_1 = require("./market");
|
|
8
10
|
/**
|
|
9
11
|
* calculateBaseAssetValue
|
|
10
12
|
* = market value of closing entire position
|
|
@@ -13,23 +15,28 @@ const amm_1 = require("./amm");
|
|
|
13
15
|
* @param oraclePriceData
|
|
14
16
|
* @returns Base Asset Value. : Precision QUOTE_PRECISION
|
|
15
17
|
*/
|
|
16
|
-
function calculateBaseAssetValue(market, userPosition, oraclePriceData) {
|
|
18
|
+
function calculateBaseAssetValue(market, userPosition, oraclePriceData, useSpread = true, skipUpdate = false) {
|
|
17
19
|
if (userPosition.baseAssetAmount.eq(numericConstants_1.ZERO)) {
|
|
18
20
|
return numericConstants_1.ZERO;
|
|
19
21
|
}
|
|
20
22
|
const directionToClose = findDirectionToClose(userPosition);
|
|
21
23
|
let prepegAmm;
|
|
22
|
-
if (
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
24
|
+
if (!skipUpdate) {
|
|
25
|
+
if (market.amm.baseSpread > 0 && useSpread) {
|
|
26
|
+
const { baseAssetReserve, quoteAssetReserve, sqrtK, newPeg } = (0, amm_1.calculateUpdatedAMMSpreadReserves)(market.amm, directionToClose, oraclePriceData);
|
|
27
|
+
prepegAmm = {
|
|
28
|
+
baseAssetReserve,
|
|
29
|
+
quoteAssetReserve,
|
|
30
|
+
sqrtK: sqrtK,
|
|
31
|
+
pegMultiplier: newPeg,
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
prepegAmm = (0, amm_1.calculateUpdatedAMM)(market.amm, oraclePriceData);
|
|
36
|
+
}
|
|
30
37
|
}
|
|
31
38
|
else {
|
|
32
|
-
prepegAmm =
|
|
39
|
+
prepegAmm = market.amm;
|
|
33
40
|
}
|
|
34
41
|
const [newQuoteAssetReserve, _] = (0, amm_1.calculateAmmReservesAfterSwap)(prepegAmm, 'base', userPosition.baseAssetAmount.abs(), (0, amm_1.getSwapDirection)('base', directionToClose));
|
|
35
42
|
switch (directionToClose) {
|
|
@@ -51,75 +58,108 @@ exports.calculateBaseAssetValue = calculateBaseAssetValue;
|
|
|
51
58
|
* calculatePositionPNL
|
|
52
59
|
* = BaseAssetAmount * (Avg Exit Price - Avg Entry Price)
|
|
53
60
|
* @param market
|
|
54
|
-
* @param
|
|
61
|
+
* @param PerpPosition
|
|
55
62
|
* @param withFunding (adds unrealized funding payment pnl to result)
|
|
63
|
+
* @param oraclePriceData
|
|
56
64
|
* @returns BaseAssetAmount : Precision QUOTE_PRECISION
|
|
57
65
|
*/
|
|
58
|
-
function calculatePositionPNL(market,
|
|
59
|
-
if (
|
|
60
|
-
return
|
|
61
|
-
}
|
|
62
|
-
const baseAssetValue = calculateBaseAssetValue(market, marketPosition, oraclePriceData);
|
|
63
|
-
let pnl;
|
|
64
|
-
if (marketPosition.baseAssetAmount.gt(numericConstants_1.ZERO)) {
|
|
65
|
-
pnl = baseAssetValue.sub(marketPosition.quoteAssetAmount);
|
|
66
|
-
}
|
|
67
|
-
else {
|
|
68
|
-
pnl = marketPosition.quoteAssetAmount.sub(baseAssetValue);
|
|
66
|
+
function calculatePositionPNL(market, perpPosition, withFunding = false, oraclePriceData) {
|
|
67
|
+
if (perpPosition.baseAssetAmount.eq(numericConstants_1.ZERO)) {
|
|
68
|
+
return perpPosition.quoteAssetAmount;
|
|
69
69
|
}
|
|
70
|
+
const baseAssetValue = (0, margin_1.calculateBaseAssetValueWithOracle)(market, perpPosition, oraclePriceData);
|
|
71
|
+
const baseAssetValueSign = perpPosition.baseAssetAmount.isNeg()
|
|
72
|
+
? new __1.BN(-1)
|
|
73
|
+
: new __1.BN(1);
|
|
74
|
+
let pnl = baseAssetValue
|
|
75
|
+
.mul(baseAssetValueSign)
|
|
76
|
+
.add(perpPosition.quoteAssetAmount);
|
|
70
77
|
if (withFunding) {
|
|
71
|
-
const fundingRatePnL = calculatePositionFundingPNL(market,
|
|
78
|
+
const fundingRatePnL = calculatePositionFundingPNL(market, perpPosition);
|
|
72
79
|
pnl = pnl.add(fundingRatePnL);
|
|
73
80
|
}
|
|
74
81
|
return pnl;
|
|
75
82
|
}
|
|
76
83
|
exports.calculatePositionPNL = calculatePositionPNL;
|
|
84
|
+
function calculateClaimablePnl(market, spotMarket, perpPosition, oraclePriceData) {
|
|
85
|
+
const unrealizedPnl = calculatePositionPNL(market, perpPosition, true, oraclePriceData);
|
|
86
|
+
const fundingPnL = calculatePositionFundingPNL(market, perpPosition);
|
|
87
|
+
let unsettledPnl = unrealizedPnl.add(fundingPnL);
|
|
88
|
+
if (unrealizedPnl.gt(numericConstants_1.ZERO)) {
|
|
89
|
+
const excessPnlPool = __1.BN.max(numericConstants_1.ZERO, (0, market_1.calculateNetUserPnlImbalance)(market, spotMarket, oraclePriceData).mul(new __1.BN(-1)));
|
|
90
|
+
const maxPositivePnl = __1.BN.max(perpPosition.quoteAssetAmount
|
|
91
|
+
.sub(perpPosition.quoteEntryAmount)
|
|
92
|
+
.add(excessPnlPool), numericConstants_1.ZERO);
|
|
93
|
+
unsettledPnl = __1.BN.min(maxPositivePnl, unrealizedPnl);
|
|
94
|
+
}
|
|
95
|
+
return unsettledPnl;
|
|
96
|
+
}
|
|
97
|
+
exports.calculateClaimablePnl = calculateClaimablePnl;
|
|
77
98
|
/**
|
|
78
99
|
*
|
|
79
100
|
* @param market
|
|
80
|
-
* @param
|
|
101
|
+
* @param PerpPosition
|
|
81
102
|
* @returns // TODO-PRECISION
|
|
82
103
|
*/
|
|
83
|
-
function calculatePositionFundingPNL(market,
|
|
84
|
-
if (
|
|
104
|
+
function calculatePositionFundingPNL(market, perpPosition) {
|
|
105
|
+
if (perpPosition.baseAssetAmount.eq(numericConstants_1.ZERO)) {
|
|
85
106
|
return numericConstants_1.ZERO;
|
|
86
107
|
}
|
|
87
108
|
let ammCumulativeFundingRate;
|
|
88
|
-
if (
|
|
109
|
+
if (perpPosition.baseAssetAmount.gt(numericConstants_1.ZERO)) {
|
|
89
110
|
ammCumulativeFundingRate = market.amm.cumulativeFundingRateLong;
|
|
90
111
|
}
|
|
91
112
|
else {
|
|
92
113
|
ammCumulativeFundingRate = market.amm.cumulativeFundingRateShort;
|
|
93
114
|
}
|
|
94
115
|
const perPositionFundingRate = ammCumulativeFundingRate
|
|
95
|
-
.sub(
|
|
96
|
-
.mul(
|
|
116
|
+
.sub(perpPosition.lastCumulativeFundingRate)
|
|
117
|
+
.mul(perpPosition.baseAssetAmount)
|
|
97
118
|
.div(numericConstants_1.AMM_RESERVE_PRECISION)
|
|
98
|
-
.div(numericConstants_1.
|
|
119
|
+
.div(numericConstants_1.FUNDING_RATE_BUFFER_PRECISION)
|
|
99
120
|
.mul(new __1.BN(-1));
|
|
100
121
|
return perPositionFundingRate;
|
|
101
122
|
}
|
|
102
123
|
exports.calculatePositionFundingPNL = calculatePositionFundingPNL;
|
|
103
124
|
function positionIsAvailable(position) {
|
|
104
|
-
return position.baseAssetAmount.eq(numericConstants_1.ZERO) &&
|
|
125
|
+
return (position.baseAssetAmount.eq(numericConstants_1.ZERO) &&
|
|
126
|
+
position.openOrders === 0 &&
|
|
127
|
+
position.quoteAssetAmount.eq(numericConstants_1.ZERO) &&
|
|
128
|
+
position.lpShares.eq(numericConstants_1.ZERO));
|
|
105
129
|
}
|
|
106
130
|
exports.positionIsAvailable = positionIsAvailable;
|
|
107
131
|
/**
|
|
108
132
|
*
|
|
109
133
|
* @param userPosition
|
|
110
|
-
* @returns Precision:
|
|
134
|
+
* @returns Precision: PRICE_PRECISION (10^10)
|
|
111
135
|
*/
|
|
112
136
|
function calculateEntryPrice(userPosition) {
|
|
113
137
|
if (userPosition.baseAssetAmount.eq(numericConstants_1.ZERO)) {
|
|
114
138
|
return numericConstants_1.ZERO;
|
|
115
139
|
}
|
|
116
|
-
return userPosition.
|
|
117
|
-
.mul(numericConstants_1.
|
|
140
|
+
return userPosition.quoteEntryAmount
|
|
141
|
+
.mul(numericConstants_1.PRICE_PRECISION)
|
|
118
142
|
.mul(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO)
|
|
119
143
|
.div(userPosition.baseAssetAmount)
|
|
120
144
|
.abs();
|
|
121
145
|
}
|
|
122
146
|
exports.calculateEntryPrice = calculateEntryPrice;
|
|
147
|
+
/**
|
|
148
|
+
*
|
|
149
|
+
* @param userPosition
|
|
150
|
+
* @returns Precision: PRICE_PRECISION (10^10)
|
|
151
|
+
*/
|
|
152
|
+
function calculateCostBasis(userPosition) {
|
|
153
|
+
if (userPosition.baseAssetAmount.eq(numericConstants_1.ZERO)) {
|
|
154
|
+
return numericConstants_1.ZERO;
|
|
155
|
+
}
|
|
156
|
+
return userPosition.quoteAssetAmount
|
|
157
|
+
.mul(numericConstants_1.PRICE_PRECISION)
|
|
158
|
+
.mul(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO)
|
|
159
|
+
.div(userPosition.baseAssetAmount)
|
|
160
|
+
.abs();
|
|
161
|
+
}
|
|
162
|
+
exports.calculateCostBasis = calculateCostBasis;
|
|
123
163
|
function findDirectionToClose(userPosition) {
|
|
124
164
|
return userPosition.baseAssetAmount.gt(numericConstants_1.ZERO)
|
|
125
165
|
? types_1.PositionDirection.SHORT
|
|
@@ -133,6 +173,6 @@ function positionCurrentDirection(userPosition) {
|
|
|
133
173
|
}
|
|
134
174
|
exports.positionCurrentDirection = positionCurrentDirection;
|
|
135
175
|
function isEmptyPosition(userPosition) {
|
|
136
|
-
return
|
|
176
|
+
return userPosition.baseAssetAmount.eq(numericConstants_1.ZERO) && userPosition.openOrders === 0;
|
|
137
177
|
}
|
|
138
178
|
exports.isEmptyPosition = isEmptyPosition;
|
package/lib/math/repeg.js
CHANGED
|
@@ -18,13 +18,13 @@ function calculateAdjustKCost(amm, numerator, denomenator) {
|
|
|
18
18
|
const d = amm.netBaseAssetAmount;
|
|
19
19
|
const Q = amm.pegMultiplier;
|
|
20
20
|
const quoteScale = y.mul(d).mul(Q); //.div(AMM_RESERVE_PRECISION);
|
|
21
|
-
const p = numerator.mul(numericConstants_1.
|
|
21
|
+
const p = numerator.mul(numericConstants_1.PRICE_PRECISION).div(denomenator);
|
|
22
22
|
const cost = quoteScale
|
|
23
23
|
.div(x.add(d))
|
|
24
24
|
.sub(quoteScale
|
|
25
25
|
.mul(p)
|
|
26
|
-
.div(numericConstants_1.
|
|
27
|
-
.div(x.mul(p).div(numericConstants_1.
|
|
26
|
+
.div(numericConstants_1.PRICE_PRECISION)
|
|
27
|
+
.div(x.mul(p).div(numericConstants_1.PRICE_PRECISION).add(d)))
|
|
28
28
|
.div(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO)
|
|
29
29
|
.div(numericConstants_1.PEG_PRECISION);
|
|
30
30
|
return cost.mul(new anchor_1.BN(-1));
|
|
@@ -71,6 +71,15 @@ function calculateBudgetedKBN(x, y, budget, Q, d) {
|
|
|
71
71
|
.div(numericConstants_1.AMM_RESERVE_PRECISION)
|
|
72
72
|
.div(numericConstants_1.QUOTE_PRECISION);
|
|
73
73
|
const denom2 = pegged_y_d_d;
|
|
74
|
+
// protocol is spending to increase k
|
|
75
|
+
if (C.lt(numericConstants_1.ZERO)) {
|
|
76
|
+
// thus denom1 is negative and solution is unstable
|
|
77
|
+
if (denom1.abs().gt(denom2.abs())) {
|
|
78
|
+
console.log('denom1 > denom2', denom1.toString(), denom2.toString());
|
|
79
|
+
console.log('budget cost exceeds stable K solution');
|
|
80
|
+
return [new anchor_1.BN(10000), new anchor_1.BN(1)];
|
|
81
|
+
}
|
|
82
|
+
}
|
|
74
83
|
const numerator = numer1.sub(numer2).div(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO);
|
|
75
84
|
const denominator = denom1.add(denom2).div(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO);
|
|
76
85
|
return [numerator, denominator];
|
|
@@ -121,8 +130,8 @@ function calculateBudgetedPeg(amm, cost, targetPrice) {
|
|
|
121
130
|
if (deltaQuoteAssetReserves.eq(numericConstants_1.ZERO) || useTargetPeg) {
|
|
122
131
|
return targetPeg;
|
|
123
132
|
}
|
|
124
|
-
const deltaPegMultiplier = C.mul(numericConstants_1.
|
|
125
|
-
const newPeg = Q.sub(deltaPegMultiplier.mul(numericConstants_1.PEG_PRECISION).div(numericConstants_1.
|
|
133
|
+
const deltaPegMultiplier = C.mul(numericConstants_1.PRICE_PRECISION).div(deltaQuoteAssetReserves.div(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO));
|
|
134
|
+
const newPeg = Q.sub(deltaPegMultiplier.mul(numericConstants_1.PEG_PRECISION).div(numericConstants_1.PRICE_PRECISION));
|
|
126
135
|
return newPeg;
|
|
127
136
|
}
|
|
128
137
|
exports.calculateBudgetedPeg = calculateBudgetedPeg;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/// <reference types="bn.js" />
|
|
2
|
+
import { SpotMarketAccount, SpotBalanceType, MarginCategory } from '../types';
|
|
3
|
+
import { BN } from '@project-serum/anchor';
|
|
4
|
+
import { OraclePriceData } from '../oracles/types';
|
|
5
|
+
export declare function getBalance(tokenAmount: BN, spotMarket: SpotMarketAccount, balanceType: SpotBalanceType): BN;
|
|
6
|
+
export declare function getTokenAmount(balanceAmount: BN, spotMarket: SpotMarketAccount, balanceType: SpotBalanceType): BN;
|
|
7
|
+
export declare function getSignedTokenAmount(tokenAmount: BN, balanceType: SpotBalanceType): BN;
|
|
8
|
+
export declare function getTokenValue(tokenAmount: BN, spotDecimals: number, oraclePriceData: OraclePriceData): BN;
|
|
9
|
+
export declare function calculateAssetWeight(balanceAmount: BN, spotMarket: SpotMarketAccount, marginCategory: MarginCategory): BN;
|
|
10
|
+
export declare function calculateLiabilityWeight(balanceAmount: BN, spotMarket: SpotMarketAccount, marginCategory: MarginCategory): BN;
|
|
11
|
+
export declare function calculateUtilization(bank: SpotMarketAccount): BN;
|
|
12
|
+
export declare function calculateInterestRate(bank: SpotMarketAccount): BN;
|
|
13
|
+
export declare function calculateDepositRate(bank: SpotMarketAccount): BN;
|
|
14
|
+
export declare function calculateBorrowRate(bank: SpotMarketAccount): BN;
|
|
15
|
+
export declare function calculateInterestAccumulated(bank: SpotMarketAccount, now: BN): {
|
|
16
|
+
borrowInterest: BN;
|
|
17
|
+
depositInterest: BN;
|
|
18
|
+
};
|
|
19
|
+
export declare function calculateWithdrawLimit(spotMarket: SpotMarketAccount, now: BN): {
|
|
20
|
+
borrowLimit: BN;
|
|
21
|
+
withdrawLimit: BN;
|
|
22
|
+
};
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.calculateWithdrawLimit = exports.calculateInterestAccumulated = exports.calculateBorrowRate = exports.calculateDepositRate = exports.calculateInterestRate = exports.calculateUtilization = exports.calculateLiabilityWeight = exports.calculateAssetWeight = exports.getTokenValue = exports.getSignedTokenAmount = exports.getTokenAmount = exports.getBalance = void 0;
|
|
4
|
+
const types_1 = require("../types");
|
|
5
|
+
const anchor_1 = require("@project-serum/anchor");
|
|
6
|
+
const numericConstants_1 = require("../constants/numericConstants");
|
|
7
|
+
const margin_1 = require("./margin");
|
|
8
|
+
function getBalance(tokenAmount, spotMarket, balanceType) {
|
|
9
|
+
const precisionIncrease = numericConstants_1.TEN.pow(new anchor_1.BN(19 - spotMarket.decimals));
|
|
10
|
+
const cumulativeInterest = (0, types_1.isVariant)(balanceType, 'deposit')
|
|
11
|
+
? spotMarket.cumulativeDepositInterest
|
|
12
|
+
: spotMarket.cumulativeBorrowInterest;
|
|
13
|
+
let balance = tokenAmount.mul(precisionIncrease).div(cumulativeInterest);
|
|
14
|
+
if (!balance.eq(numericConstants_1.ZERO) && (0, types_1.isVariant)(balanceType, 'borrow')) {
|
|
15
|
+
balance = balance.add(numericConstants_1.ONE);
|
|
16
|
+
}
|
|
17
|
+
return balance;
|
|
18
|
+
}
|
|
19
|
+
exports.getBalance = getBalance;
|
|
20
|
+
function getTokenAmount(balanceAmount, spotMarket, balanceType) {
|
|
21
|
+
const precisionDecrease = numericConstants_1.TEN.pow(new anchor_1.BN(19 - spotMarket.decimals));
|
|
22
|
+
const cumulativeInterest = (0, types_1.isVariant)(balanceType, 'deposit')
|
|
23
|
+
? spotMarket.cumulativeDepositInterest
|
|
24
|
+
: spotMarket.cumulativeBorrowInterest;
|
|
25
|
+
return balanceAmount.mul(cumulativeInterest).div(precisionDecrease);
|
|
26
|
+
}
|
|
27
|
+
exports.getTokenAmount = getTokenAmount;
|
|
28
|
+
function getSignedTokenAmount(tokenAmount, balanceType) {
|
|
29
|
+
if ((0, types_1.isVariant)(balanceType, 'deposit')) {
|
|
30
|
+
return tokenAmount;
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
return tokenAmount.abs().neg();
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
exports.getSignedTokenAmount = getSignedTokenAmount;
|
|
37
|
+
function getTokenValue(tokenAmount, spotDecimals, oraclePriceData) {
|
|
38
|
+
if (tokenAmount.eq(numericConstants_1.ZERO)) {
|
|
39
|
+
return numericConstants_1.ZERO;
|
|
40
|
+
}
|
|
41
|
+
const precisionDecrease = numericConstants_1.TEN.pow(new anchor_1.BN(spotDecimals));
|
|
42
|
+
return tokenAmount.mul(oraclePriceData.price).div(precisionDecrease);
|
|
43
|
+
}
|
|
44
|
+
exports.getTokenValue = getTokenValue;
|
|
45
|
+
function calculateAssetWeight(balanceAmount, spotMarket, marginCategory) {
|
|
46
|
+
const sizePrecision = numericConstants_1.TEN.pow(new anchor_1.BN(spotMarket.decimals));
|
|
47
|
+
let sizeInAmmReservePrecision;
|
|
48
|
+
if (sizePrecision.gt(numericConstants_1.AMM_RESERVE_PRECISION)) {
|
|
49
|
+
sizeInAmmReservePrecision = balanceAmount.div(sizePrecision.div(numericConstants_1.AMM_RESERVE_PRECISION));
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
sizeInAmmReservePrecision = balanceAmount
|
|
53
|
+
.mul(numericConstants_1.AMM_RESERVE_PRECISION)
|
|
54
|
+
.div(sizePrecision);
|
|
55
|
+
}
|
|
56
|
+
let assetWeight;
|
|
57
|
+
switch (marginCategory) {
|
|
58
|
+
case 'Initial':
|
|
59
|
+
assetWeight = (0, margin_1.calculateSizeDiscountAssetWeight)(sizeInAmmReservePrecision, spotMarket.imfFactor, spotMarket.initialAssetWeight);
|
|
60
|
+
break;
|
|
61
|
+
case 'Maintenance':
|
|
62
|
+
assetWeight = (0, margin_1.calculateSizeDiscountAssetWeight)(sizeInAmmReservePrecision, spotMarket.imfFactor, spotMarket.maintenanceAssetWeight);
|
|
63
|
+
break;
|
|
64
|
+
default:
|
|
65
|
+
assetWeight = spotMarket.initialAssetWeight;
|
|
66
|
+
break;
|
|
67
|
+
}
|
|
68
|
+
return assetWeight;
|
|
69
|
+
}
|
|
70
|
+
exports.calculateAssetWeight = calculateAssetWeight;
|
|
71
|
+
function calculateLiabilityWeight(balanceAmount, spotMarket, marginCategory) {
|
|
72
|
+
const sizePrecision = numericConstants_1.TEN.pow(new anchor_1.BN(spotMarket.decimals));
|
|
73
|
+
let sizeInAmmReservePrecision;
|
|
74
|
+
if (sizePrecision.gt(numericConstants_1.AMM_RESERVE_PRECISION)) {
|
|
75
|
+
sizeInAmmReservePrecision = balanceAmount.div(sizePrecision.div(numericConstants_1.AMM_RESERVE_PRECISION));
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
sizeInAmmReservePrecision = balanceAmount
|
|
79
|
+
.mul(numericConstants_1.AMM_RESERVE_PRECISION)
|
|
80
|
+
.div(sizePrecision);
|
|
81
|
+
}
|
|
82
|
+
let assetWeight;
|
|
83
|
+
switch (marginCategory) {
|
|
84
|
+
case 'Initial':
|
|
85
|
+
assetWeight = (0, margin_1.calculateSizePremiumLiabilityWeight)(sizeInAmmReservePrecision, spotMarket.imfFactor, spotMarket.initialLiabilityWeight, numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION);
|
|
86
|
+
break;
|
|
87
|
+
case 'Maintenance':
|
|
88
|
+
assetWeight = (0, margin_1.calculateSizePremiumLiabilityWeight)(sizeInAmmReservePrecision, spotMarket.imfFactor, spotMarket.maintenanceLiabilityWeight, numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION);
|
|
89
|
+
break;
|
|
90
|
+
default:
|
|
91
|
+
assetWeight = spotMarket.initialLiabilityWeight;
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
return assetWeight;
|
|
95
|
+
}
|
|
96
|
+
exports.calculateLiabilityWeight = calculateLiabilityWeight;
|
|
97
|
+
function calculateUtilization(bank) {
|
|
98
|
+
const tokenDepositAmount = getTokenAmount(bank.depositBalance, bank, types_1.SpotBalanceType.DEPOSIT);
|
|
99
|
+
const tokenBorrowAmount = getTokenAmount(bank.borrowBalance, bank, types_1.SpotBalanceType.BORROW);
|
|
100
|
+
let utilization;
|
|
101
|
+
if (tokenBorrowAmount.eq(numericConstants_1.ZERO) && tokenDepositAmount.eq(numericConstants_1.ZERO)) {
|
|
102
|
+
utilization = numericConstants_1.ZERO;
|
|
103
|
+
}
|
|
104
|
+
else if (tokenDepositAmount.eq(numericConstants_1.ZERO)) {
|
|
105
|
+
utilization = numericConstants_1.SPOT_MARKET_UTILIZATION_PRECISION;
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
utilization = tokenBorrowAmount
|
|
109
|
+
.mul(numericConstants_1.SPOT_MARKET_UTILIZATION_PRECISION)
|
|
110
|
+
.div(tokenDepositAmount);
|
|
111
|
+
}
|
|
112
|
+
return utilization;
|
|
113
|
+
}
|
|
114
|
+
exports.calculateUtilization = calculateUtilization;
|
|
115
|
+
function calculateInterestRate(bank) {
|
|
116
|
+
const utilization = calculateUtilization(bank);
|
|
117
|
+
let interestRate;
|
|
118
|
+
if (utilization.gt(bank.optimalUtilization)) {
|
|
119
|
+
const surplusUtilization = utilization.sub(bank.optimalUtilization);
|
|
120
|
+
const borrowRateSlope = bank.maxBorrowRate
|
|
121
|
+
.sub(bank.optimalBorrowRate)
|
|
122
|
+
.mul(numericConstants_1.SPOT_MARKET_UTILIZATION_PRECISION)
|
|
123
|
+
.div(numericConstants_1.SPOT_MARKET_UTILIZATION_PRECISION.sub(bank.optimalUtilization));
|
|
124
|
+
interestRate = bank.optimalBorrowRate.add(surplusUtilization
|
|
125
|
+
.mul(borrowRateSlope)
|
|
126
|
+
.div(numericConstants_1.SPOT_MARKET_UTILIZATION_PRECISION));
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
const borrowRateSlope = bank.optimalBorrowRate
|
|
130
|
+
.mul(numericConstants_1.SPOT_MARKET_UTILIZATION_PRECISION)
|
|
131
|
+
.div(numericConstants_1.SPOT_MARKET_UTILIZATION_PRECISION.sub(bank.optimalUtilization));
|
|
132
|
+
interestRate = utilization
|
|
133
|
+
.mul(borrowRateSlope)
|
|
134
|
+
.div(numericConstants_1.SPOT_MARKET_UTILIZATION_PRECISION);
|
|
135
|
+
}
|
|
136
|
+
return interestRate;
|
|
137
|
+
}
|
|
138
|
+
exports.calculateInterestRate = calculateInterestRate;
|
|
139
|
+
function calculateDepositRate(bank) {
|
|
140
|
+
const utilization = calculateUtilization(bank);
|
|
141
|
+
const borrowRate = calculateBorrowRate(bank);
|
|
142
|
+
const depositRate = borrowRate
|
|
143
|
+
.mul(utilization)
|
|
144
|
+
.div(numericConstants_1.SPOT_MARKET_UTILIZATION_PRECISION);
|
|
145
|
+
return depositRate;
|
|
146
|
+
}
|
|
147
|
+
exports.calculateDepositRate = calculateDepositRate;
|
|
148
|
+
function calculateBorrowRate(bank) {
|
|
149
|
+
return calculateInterestRate(bank);
|
|
150
|
+
}
|
|
151
|
+
exports.calculateBorrowRate = calculateBorrowRate;
|
|
152
|
+
function calculateInterestAccumulated(bank, now) {
|
|
153
|
+
const interestRate = calculateInterestRate(bank);
|
|
154
|
+
const timeSinceLastUpdate = now.sub(bank.lastInterestTs);
|
|
155
|
+
const modifiedBorrowRate = interestRate.mul(timeSinceLastUpdate);
|
|
156
|
+
const utilization = calculateUtilization(bank);
|
|
157
|
+
const modifiedDepositRate = modifiedBorrowRate
|
|
158
|
+
.mul(utilization)
|
|
159
|
+
.div(numericConstants_1.SPOT_MARKET_UTILIZATION_PRECISION);
|
|
160
|
+
const borrowInterest = bank.cumulativeBorrowInterest
|
|
161
|
+
.mul(modifiedBorrowRate)
|
|
162
|
+
.div(numericConstants_1.ONE_YEAR)
|
|
163
|
+
.div(numericConstants_1.SPOT_MARKET_RATE_PRECISION)
|
|
164
|
+
.add(numericConstants_1.ONE);
|
|
165
|
+
const depositInterest = bank.cumulativeDepositInterest
|
|
166
|
+
.mul(modifiedDepositRate)
|
|
167
|
+
.div(numericConstants_1.ONE_YEAR)
|
|
168
|
+
.div(numericConstants_1.SPOT_MARKET_RATE_PRECISION);
|
|
169
|
+
return { borrowInterest, depositInterest };
|
|
170
|
+
}
|
|
171
|
+
exports.calculateInterestAccumulated = calculateInterestAccumulated;
|
|
172
|
+
function calculateWithdrawLimit(spotMarket, now) {
|
|
173
|
+
const marketDepositTokenAmount = getTokenAmount(spotMarket.depositBalance, spotMarket, types_1.SpotBalanceType.DEPOSIT);
|
|
174
|
+
const marketBorrowTokenAmount = getTokenAmount(spotMarket.borrowBalance, spotMarket, types_1.SpotBalanceType.BORROW);
|
|
175
|
+
const twentyFourHours = new anchor_1.BN(60 * 60 * 24);
|
|
176
|
+
const sinceLast = now.sub(spotMarket.lastTwapTs);
|
|
177
|
+
const sinceStart = anchor_1.BN.max(numericConstants_1.ZERO, twentyFourHours.sub(sinceLast));
|
|
178
|
+
const borrowTokenTwapLive = spotMarket.borrowTokenTwap
|
|
179
|
+
.mul(sinceStart)
|
|
180
|
+
.add(marketBorrowTokenAmount.mul(sinceLast))
|
|
181
|
+
.div(sinceLast.add(sinceLast));
|
|
182
|
+
const depositTokenTwapLive = spotMarket.depositTokenTwap
|
|
183
|
+
.mul(sinceStart)
|
|
184
|
+
.add(marketDepositTokenAmount.mul(sinceLast))
|
|
185
|
+
.div(sinceLast.add(sinceLast));
|
|
186
|
+
const maxBorrowTokens = anchor_1.BN.min(anchor_1.BN.max(marketDepositTokenAmount.div(new anchor_1.BN(6)), borrowTokenTwapLive.add(borrowTokenTwapLive.div(new anchor_1.BN(5)))), marketDepositTokenAmount.sub(marketDepositTokenAmount.div(new anchor_1.BN(10)))); // between ~15-90% utilization with friction on twap
|
|
187
|
+
const minDepositTokens = depositTokenTwapLive.sub(anchor_1.BN.min(anchor_1.BN.max(depositTokenTwapLive.div(new anchor_1.BN(5)), spotMarket.withdrawGuardThreshold), depositTokenTwapLive));
|
|
188
|
+
return {
|
|
189
|
+
borrowLimit: maxBorrowTokens.sub(marketBorrowTokenAmount),
|
|
190
|
+
withdrawLimit: marketDepositTokenAmount.sub(minDepositTokens),
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
exports.calculateWithdrawLimit = calculateWithdrawLimit;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.castNumberToSpotPrecision = void 0;
|
|
4
|
+
const 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,6 @@
|
|
|
1
|
+
/// <reference types="bn.js" />
|
|
2
|
+
import { SpotMarketAccount, SpotPosition } from '../types';
|
|
3
|
+
import { BN } from '@project-serum/anchor';
|
|
4
|
+
import { OraclePriceData } from '../oracles/types';
|
|
5
|
+
export declare function isSpotPositionAvailable(position: SpotPosition): boolean;
|
|
6
|
+
export declare function getWorstCaseTokenAmounts(spotPosition: SpotPosition, spotMarketAccount: SpotMarketAccount, oraclePriceData: OraclePriceData): [BN, BN];
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getWorstCaseTokenAmounts = exports.isSpotPositionAvailable = void 0;
|
|
4
|
+
const numericConstants_1 = require("../constants/numericConstants");
|
|
5
|
+
const spotBalance_1 = require("./spotBalance");
|
|
6
|
+
function isSpotPositionAvailable(position) {
|
|
7
|
+
return position.balance.eq(numericConstants_1.ZERO) && position.openOrders === 0;
|
|
8
|
+
}
|
|
9
|
+
exports.isSpotPositionAvailable = isSpotPositionAvailable;
|
|
10
|
+
function getWorstCaseTokenAmounts(spotPosition, spotMarketAccount, oraclePriceData) {
|
|
11
|
+
const tokenAmount = (0, spotBalance_1.getSignedTokenAmount)((0, spotBalance_1.getTokenAmount)(spotPosition.balance, spotMarketAccount, spotPosition.balanceType), spotPosition.balanceType);
|
|
12
|
+
const tokenAmountAllBidsFill = tokenAmount.add(spotPosition.openBids);
|
|
13
|
+
const tokenAmountAllAsksFill = tokenAmount.add(spotPosition.openAsks);
|
|
14
|
+
if (tokenAmountAllAsksFill.abs().gt(tokenAmountAllBidsFill.abs())) {
|
|
15
|
+
const worstCaseQuoteTokenAmount = (0, spotBalance_1.getTokenValue)(spotPosition.openAsks.neg(), spotMarketAccount.decimals, oraclePriceData);
|
|
16
|
+
return [tokenAmountAllBidsFill, worstCaseQuoteTokenAmount];
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
const worstCaseQuoteTokenAmount = (0, spotBalance_1.getTokenValue)(spotPosition.openBids.neg(), spotMarketAccount.decimals, oraclePriceData);
|
|
20
|
+
return [tokenAmountAllAsksFill, worstCaseQuoteTokenAmount];
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
exports.getWorstCaseTokenAmounts = getWorstCaseTokenAmounts;
|
package/lib/math/trade.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/// <reference types="bn.js" />
|
|
2
|
-
import {
|
|
2
|
+
import { PerpMarketAccount, PositionDirection } from '../types';
|
|
3
3
|
import { BN } from '@project-serum/anchor';
|
|
4
4
|
import { AssetType } from './amm';
|
|
5
5
|
import { OraclePriceData } from '../oracles/types';
|
|
@@ -13,15 +13,15 @@ export declare type PriceImpactUnit = 'entryPrice' | 'maxPrice' | 'priceDelta' |
|
|
|
13
13
|
* @param useSpread whether to consider spread with calculating slippage
|
|
14
14
|
* @return [pctAvgSlippage, pctMaxSlippage, entryPrice, newPrice]
|
|
15
15
|
*
|
|
16
|
-
* 'pctAvgSlippage' => the percentage change to entryPrice (average est slippage in execution) : Precision
|
|
16
|
+
* 'pctAvgSlippage' => the percentage change to entryPrice (average est slippage in execution) : Precision PRICE_PRECISION
|
|
17
17
|
*
|
|
18
|
-
* 'pctMaxSlippage' => the percentage change to maxPrice (highest est slippage in execution) : Precision
|
|
18
|
+
* 'pctMaxSlippage' => the percentage change to maxPrice (highest est slippage in execution) : Precision PRICE_PRECISION
|
|
19
19
|
*
|
|
20
|
-
* 'entryPrice' => the average price of the trade : Precision
|
|
20
|
+
* 'entryPrice' => the average price of the trade : Precision PRICE_PRECISION
|
|
21
21
|
*
|
|
22
|
-
* 'newPrice' => the price of the asset after the trade : Precision
|
|
22
|
+
* 'newPrice' => the price of the asset after the trade : Precision PRICE_PRECISION
|
|
23
23
|
*/
|
|
24
|
-
export declare function calculateTradeSlippage(direction: PositionDirection, amount: BN, market:
|
|
24
|
+
export declare function calculateTradeSlippage(direction: PositionDirection, amount: BN, market: PerpMarketAccount, inputAssetType?: AssetType, oraclePriceData?: OraclePriceData, useSpread?: boolean): [BN, BN, BN, BN];
|
|
25
25
|
/**
|
|
26
26
|
* Calculates acquired amounts for trade executed
|
|
27
27
|
* @param direction
|
|
@@ -33,7 +33,7 @@ export declare function calculateTradeSlippage(direction: PositionDirection, amo
|
|
|
33
33
|
* | 'acquiredBase' => positive/negative change in user's base : BN AMM_RESERVE_PRECISION
|
|
34
34
|
* | 'acquiredQuote' => positive/negative change in user's quote : BN TODO-PRECISION
|
|
35
35
|
*/
|
|
36
|
-
export declare function calculateTradeAcquiredAmounts(direction: PositionDirection, amount: BN, market:
|
|
36
|
+
export declare function calculateTradeAcquiredAmounts(direction: PositionDirection, amount: BN, market: PerpMarketAccount, inputAssetType: AssetType, oraclePriceData: OraclePriceData, useSpread?: boolean): [BN, BN, BN];
|
|
37
37
|
/**
|
|
38
38
|
* calculateTargetPriceTrade
|
|
39
39
|
* simple function for finding arbitraging trades
|
|
@@ -47,8 +47,8 @@ export declare function calculateTradeAcquiredAmounts(direction: PositionDirecti
|
|
|
47
47
|
* [
|
|
48
48
|
* direction => direction of trade required, PositionDirection
|
|
49
49
|
* tradeSize => size of trade required, TODO-PRECISION
|
|
50
|
-
* entryPrice => the entry price for the trade,
|
|
51
|
-
* targetPrice => the target price
|
|
50
|
+
* entryPrice => the entry price for the trade, PRICE_PRECISION
|
|
51
|
+
* targetPrice => the target price PRICE_PRECISION
|
|
52
52
|
* ]
|
|
53
53
|
*/
|
|
54
|
-
export declare function calculateTargetPriceTrade(market:
|
|
54
|
+
export declare function calculateTargetPriceTrade(market: PerpMarketAccount, targetPrice: BN, pct?: BN, outputAssetType?: AssetType, oraclePriceData?: OraclePriceData, useSpread?: boolean): [PositionDirection, BN, BN, BN];
|