@drift-labs/sdk 2.20.0-beta.0 → 2.20.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/accounts/bulkAccountLoader.d.ts +2 -1
- package/lib/accounts/bulkAccountLoader.js +7 -7
- package/lib/accounts/fetch.d.ts +1 -0
- package/lib/accounts/fetch.js +8 -4
- package/lib/accounts/pollingDriftClientAccountSubscriber.js +7 -7
- package/lib/accounts/pollingTokenAccountSubscriber.js +2 -2
- package/lib/accounts/pollingUserAccountSubscriber.js +2 -2
- package/lib/accounts/pollingUserStatsAccountSubscriber.js +2 -2
- package/lib/accounts/types.d.ts +5 -4
- package/lib/accounts/webSocketAccountSubscriber.js +1 -1
- package/lib/accounts/webSocketDriftClientAccountSubscriber.js +3 -3
- package/lib/addresses/marketAddresses.js +1 -1
- package/lib/addresses/pda.js +5 -1
- package/lib/adminClient.js +61 -57
- package/lib/config.d.ts +2 -2
- package/lib/constants/perpMarkets.d.ts +1 -1
- package/lib/constants/spotMarkets.d.ts +1 -1
- package/lib/dlob/DLOB.d.ts +6 -5
- package/lib/dlob/DLOB.js +105 -75
- package/lib/dlob/DLOBNode.d.ts +2 -2
- package/lib/dlob/DLOBNode.js +7 -7
- package/lib/dlob/DLOBOrders.d.ts +2 -2
- package/lib/dlob/NodeList.d.ts +1 -1
- package/lib/dlob/NodeList.js +2 -2
- package/lib/driftClient.d.ts +3 -2
- package/lib/driftClient.js +114 -95
- package/lib/driftClientConfig.d.ts +3 -3
- package/lib/events/eventSubscriber.js +2 -2
- package/lib/events/fetchLogs.d.ts +2 -2
- package/lib/events/pollingLogProvider.js +1 -1
- package/lib/events/types.d.ts +14 -14
- package/lib/examples/loadDlob.js +2 -2
- package/lib/examples/makeTradeExample.js +9 -9
- package/lib/factory/bigNum.js +13 -13
- package/lib/factory/oracleClient.js +4 -4
- package/lib/idl/drift.json +1 -1
- package/lib/index.js +5 -1
- package/lib/math/amm.d.ts +1 -1
- package/lib/math/amm.js +23 -23
- package/lib/math/auction.js +6 -6
- package/lib/math/exchangeStatus.js +2 -2
- package/lib/math/funding.js +2 -2
- package/lib/math/margin.js +4 -4
- package/lib/math/market.js +15 -15
- package/lib/math/oracles.js +1 -1
- package/lib/math/orders.js +27 -24
- package/lib/math/position.js +5 -5
- package/lib/math/repeg.js +1 -1
- package/lib/math/spotBalance.js +9 -9
- package/lib/math/spotMarket.js +3 -3
- package/lib/math/spotPosition.js +3 -3
- package/lib/math/trade.d.ts +1 -1
- package/lib/math/trade.js +42 -42
- package/lib/math/utils.js +1 -1
- package/lib/oracles/oracleClientCache.js +1 -1
- package/lib/oracles/pythClient.js +1 -1
- package/lib/oracles/types.d.ts +2 -2
- package/lib/serum/types.d.ts +1 -1
- package/lib/slot/SlotSubscriber.d.ts +1 -1
- package/lib/tokenFaucet.js +5 -1
- package/lib/tx/retryTxSender.d.ts +1 -1
- package/lib/tx/retryTxSender.js +1 -1
- package/lib/tx/types.d.ts +1 -1
- package/lib/types.d.ts +65 -44
- package/lib/user.js +63 -63
- package/lib/userConfig.d.ts +2 -2
- package/lib/userMap/userMap.js +1 -1
- package/lib/userMap/userStatsMap.js +3 -3
- package/lib/userStats.js +2 -2
- package/lib/userStatsConfig.d.ts +2 -2
- package/package.json +1 -1
- package/src/accounts/bulkAccountLoader.ts +5 -5
- package/src/accounts/fetch.ts +8 -0
- package/src/dlob/DLOB.ts +55 -8
- package/src/driftClient.ts +28 -0
- package/src/idl/drift.json +1 -1
- package/src/math/orders.ts +5 -1
- package/src/types.ts +22 -0
- package/src/userMap/userStatsMap.ts +1 -4
- package/tests/amm/test.ts +24 -28
- package/tests/dlob/test.ts +67 -73
package/lib/math/market.js
CHANGED
|
@@ -15,8 +15,8 @@ const assert_1 = require("../assert/assert");
|
|
|
15
15
|
* @return markPrice : Precision PRICE_PRECISION
|
|
16
16
|
*/
|
|
17
17
|
function calculateReservePrice(market, oraclePriceData) {
|
|
18
|
-
const newAmm = amm_1.calculateUpdatedAMM(market.amm, oraclePriceData);
|
|
19
|
-
return amm_1.calculatePrice(newAmm.baseAssetReserve, newAmm.quoteAssetReserve, newAmm.pegMultiplier);
|
|
18
|
+
const newAmm = (0, amm_1.calculateUpdatedAMM)(market.amm, oraclePriceData);
|
|
19
|
+
return (0, amm_1.calculatePrice)(newAmm.baseAssetReserve, newAmm.quoteAssetReserve, newAmm.pegMultiplier);
|
|
20
20
|
}
|
|
21
21
|
exports.calculateReservePrice = calculateReservePrice;
|
|
22
22
|
/**
|
|
@@ -26,8 +26,8 @@ exports.calculateReservePrice = calculateReservePrice;
|
|
|
26
26
|
* @return bidPrice : Precision PRICE_PRECISION
|
|
27
27
|
*/
|
|
28
28
|
function calculateBidPrice(market, oraclePriceData) {
|
|
29
|
-
const { baseAssetReserve, quoteAssetReserve, newPeg } = amm_1.calculateUpdatedAMMSpreadReserves(market.amm, types_1.PositionDirection.SHORT, oraclePriceData);
|
|
30
|
-
return amm_1.calculatePrice(baseAssetReserve, quoteAssetReserve, newPeg);
|
|
29
|
+
const { baseAssetReserve, quoteAssetReserve, newPeg } = (0, amm_1.calculateUpdatedAMMSpreadReserves)(market.amm, types_1.PositionDirection.SHORT, oraclePriceData);
|
|
30
|
+
return (0, amm_1.calculatePrice)(baseAssetReserve, quoteAssetReserve, newPeg);
|
|
31
31
|
}
|
|
32
32
|
exports.calculateBidPrice = calculateBidPrice;
|
|
33
33
|
/**
|
|
@@ -37,12 +37,12 @@ exports.calculateBidPrice = calculateBidPrice;
|
|
|
37
37
|
* @return askPrice : Precision PRICE_PRECISION
|
|
38
38
|
*/
|
|
39
39
|
function calculateAskPrice(market, oraclePriceData) {
|
|
40
|
-
const { baseAssetReserve, quoteAssetReserve, newPeg } = amm_1.calculateUpdatedAMMSpreadReserves(market.amm, types_1.PositionDirection.LONG, oraclePriceData);
|
|
41
|
-
return amm_1.calculatePrice(baseAssetReserve, quoteAssetReserve, newPeg);
|
|
40
|
+
const { baseAssetReserve, quoteAssetReserve, newPeg } = (0, amm_1.calculateUpdatedAMMSpreadReserves)(market.amm, types_1.PositionDirection.LONG, oraclePriceData);
|
|
41
|
+
return (0, amm_1.calculatePrice)(baseAssetReserve, quoteAssetReserve, newPeg);
|
|
42
42
|
}
|
|
43
43
|
exports.calculateAskPrice = calculateAskPrice;
|
|
44
44
|
function calculateNewMarketAfterTrade(baseAssetAmount, direction, market) {
|
|
45
|
-
const [newQuoteAssetReserve, newBaseAssetReserve] = amm_1.calculateAmmReservesAfterSwap(market.amm, 'base', baseAssetAmount.abs(), amm_1.getSwapDirection('base', direction));
|
|
45
|
+
const [newQuoteAssetReserve, newBaseAssetReserve] = (0, amm_1.calculateAmmReservesAfterSwap)(market.amm, 'base', baseAssetAmount.abs(), (0, amm_1.getSwapDirection)('base', direction));
|
|
46
46
|
const newAmm = Object.assign({}, market.amm);
|
|
47
47
|
const newMarket = Object.assign({}, market);
|
|
48
48
|
newMarket.amm = newAmm;
|
|
@@ -64,11 +64,11 @@ function calculateMarketMarginRatio(market, size, marginCategory) {
|
|
|
64
64
|
let marginRatio;
|
|
65
65
|
switch (marginCategory) {
|
|
66
66
|
case 'Initial': {
|
|
67
|
-
marginRatio = margin_1.calculateSizePremiumLiabilityWeight(size, new anchor_1.BN(market.imfFactor), new anchor_1.BN(market.marginRatioInitial), numericConstants_1.MARGIN_PRECISION).toNumber();
|
|
67
|
+
marginRatio = (0, margin_1.calculateSizePremiumLiabilityWeight)(size, new anchor_1.BN(market.imfFactor), new anchor_1.BN(market.marginRatioInitial), numericConstants_1.MARGIN_PRECISION).toNumber();
|
|
68
68
|
break;
|
|
69
69
|
}
|
|
70
70
|
case 'Maintenance': {
|
|
71
|
-
marginRatio = margin_1.calculateSizePremiumLiabilityWeight(size, new anchor_1.BN(market.imfFactor), new anchor_1.BN(market.marginRatioMaintenance), numericConstants_1.MARGIN_PRECISION).toNumber();
|
|
71
|
+
marginRatio = (0, margin_1.calculateSizePremiumLiabilityWeight)(size, new anchor_1.BN(market.imfFactor), new anchor_1.BN(market.marginRatioMaintenance), numericConstants_1.MARGIN_PRECISION).toNumber();
|
|
72
72
|
break;
|
|
73
73
|
}
|
|
74
74
|
}
|
|
@@ -88,7 +88,7 @@ function calculateUnrealizedAssetWeight(market, quoteSpotMarket, unrealizedPnl,
|
|
|
88
88
|
.div(netUnsettledPnl);
|
|
89
89
|
}
|
|
90
90
|
}
|
|
91
|
-
assetWeight = margin_1.calculateSizeDiscountAssetWeight(unrealizedPnl, new anchor_1.BN(market.unrealizedPnlImfFactor), assetWeight);
|
|
91
|
+
assetWeight = (0, margin_1.calculateSizeDiscountAssetWeight)(unrealizedPnl, new anchor_1.BN(market.unrealizedPnlImfFactor), assetWeight);
|
|
92
92
|
break;
|
|
93
93
|
case 'Maintenance':
|
|
94
94
|
assetWeight = new anchor_1.BN(market.unrealizedPnlMaintenanceAssetWeight);
|
|
@@ -98,14 +98,14 @@ function calculateUnrealizedAssetWeight(market, quoteSpotMarket, unrealizedPnl,
|
|
|
98
98
|
}
|
|
99
99
|
exports.calculateUnrealizedAssetWeight = calculateUnrealizedAssetWeight;
|
|
100
100
|
function calculateMarketAvailablePNL(perpMarket, spotMarket) {
|
|
101
|
-
return spotBalance_1.getTokenAmount(perpMarket.pnlPool.scaledBalance, spotMarket, types_1.SpotBalanceType.DEPOSIT);
|
|
101
|
+
return (0, spotBalance_1.getTokenAmount)(perpMarket.pnlPool.scaledBalance, spotMarket, types_1.SpotBalanceType.DEPOSIT);
|
|
102
102
|
}
|
|
103
103
|
exports.calculateMarketAvailablePNL = calculateMarketAvailablePNL;
|
|
104
104
|
function calculateMarketMaxAvailableInsurance(perpMarket, spotMarket) {
|
|
105
|
-
assert_1.assert(spotMarket.marketIndex == numericConstants_1.QUOTE_SPOT_MARKET_INDEX);
|
|
105
|
+
(0, assert_1.assert)(spotMarket.marketIndex == numericConstants_1.QUOTE_SPOT_MARKET_INDEX);
|
|
106
106
|
// todo: insuranceFundAllocation technically not guaranteed to be in Insurance Fund
|
|
107
107
|
const insuranceFundAllocation = perpMarket.insuranceClaim.quoteMaxInsurance.sub(perpMarket.insuranceClaim.quoteSettledInsurance);
|
|
108
|
-
const ammFeePool = spotBalance_1.getTokenAmount(perpMarket.amm.feePool.scaledBalance, spotMarket, types_1.SpotBalanceType.DEPOSIT);
|
|
108
|
+
const ammFeePool = (0, spotBalance_1.getTokenAmount)(perpMarket.amm.feePool.scaledBalance, spotMarket, types_1.SpotBalanceType.DEPOSIT);
|
|
109
109
|
return insuranceFundAllocation.add(ammFeePool);
|
|
110
110
|
}
|
|
111
111
|
exports.calculateMarketMaxAvailableInsurance = calculateMarketMaxAvailableInsurance;
|
|
@@ -121,13 +121,13 @@ function calculateNetUserPnl(perpMarket, oraclePriceData) {
|
|
|
121
121
|
exports.calculateNetUserPnl = calculateNetUserPnl;
|
|
122
122
|
function calculateNetUserPnlImbalance(perpMarket, spotMarket, oraclePriceData) {
|
|
123
123
|
const netUserPnl = calculateNetUserPnl(perpMarket, oraclePriceData);
|
|
124
|
-
const pnlPool = spotBalance_1.getTokenAmount(perpMarket.pnlPool.scaledBalance, spotMarket, types_1.SpotBalanceType.DEPOSIT);
|
|
124
|
+
const pnlPool = (0, spotBalance_1.getTokenAmount)(perpMarket.pnlPool.scaledBalance, spotMarket, types_1.SpotBalanceType.DEPOSIT);
|
|
125
125
|
const imbalance = netUserPnl.sub(pnlPool);
|
|
126
126
|
return imbalance;
|
|
127
127
|
}
|
|
128
128
|
exports.calculateNetUserPnlImbalance = calculateNetUserPnlImbalance;
|
|
129
129
|
function calculateAvailablePerpLiquidity(market, oraclePriceData, dlob, slot) {
|
|
130
|
-
let [bids, asks] = amm_1.calculateMarketOpenBidAsk(market.amm.baseAssetReserve, market.amm.minBaseAssetReserve, market.amm.maxBaseAssetReserve, market.amm.orderStepSize);
|
|
130
|
+
let [bids, asks] = (0, amm_1.calculateMarketOpenBidAsk)(market.amm.baseAssetReserve, market.amm.minBaseAssetReserve, market.amm.maxBaseAssetReserve, market.amm.orderStepSize);
|
|
131
131
|
asks = asks.abs();
|
|
132
132
|
const bidPrice = calculateBidPrice(market, oraclePriceData);
|
|
133
133
|
const askPrice = calculateAskPrice(market, oraclePriceData);
|
package/lib/math/oracles.js
CHANGED
|
@@ -9,7 +9,7 @@ function oraclePriceBands(market, oraclePriceData) {
|
|
|
9
9
|
const offset = oraclePriceData.price
|
|
10
10
|
.mul(new index_1.BN(maxPercentDiff))
|
|
11
11
|
.div(numericConstants_1.MARGIN_PRECISION);
|
|
12
|
-
assert_1.assert(offset.gt(numericConstants_1.ZERO));
|
|
12
|
+
(0, assert_1.assert)(offset.gt(numericConstants_1.ZERO));
|
|
13
13
|
return [oraclePriceData.price.sub(offset), oraclePriceData.price.add(offset)];
|
|
14
14
|
}
|
|
15
15
|
exports.oraclePriceBands = oraclePriceBands;
|
package/lib/math/orders.js
CHANGED
|
@@ -7,7 +7,7 @@ const anchor_1 = require("@project-serum/anchor");
|
|
|
7
7
|
const auction_1 = require("./auction");
|
|
8
8
|
const amm_1 = require("./amm");
|
|
9
9
|
function isOrderRiskIncreasing(user, order) {
|
|
10
|
-
if (types_1.isVariant(order.status, 'init')) {
|
|
10
|
+
if ((0, types_1.isVariant)(order.status, 'init')) {
|
|
11
11
|
return false;
|
|
12
12
|
}
|
|
13
13
|
const position = user.getPerpPosition(order.marketIndex) ||
|
|
@@ -17,12 +17,12 @@ function isOrderRiskIncreasing(user, order) {
|
|
|
17
17
|
return true;
|
|
18
18
|
}
|
|
19
19
|
// if position is long and order is long
|
|
20
|
-
if (position.baseAssetAmount.gt(numericConstants_1.ZERO) && types_1.isVariant(order.direction, 'long')) {
|
|
20
|
+
if (position.baseAssetAmount.gt(numericConstants_1.ZERO) && (0, types_1.isVariant)(order.direction, 'long')) {
|
|
21
21
|
return true;
|
|
22
22
|
}
|
|
23
23
|
// if position is short and order is short
|
|
24
24
|
if (position.baseAssetAmount.lt(numericConstants_1.ZERO) &&
|
|
25
|
-
types_1.isVariant(order.direction, 'short')) {
|
|
25
|
+
(0, types_1.isVariant)(order.direction, 'short')) {
|
|
26
26
|
return true;
|
|
27
27
|
}
|
|
28
28
|
const baseAssetAmountToFill = order.baseAssetAmount.sub(order.baseAssetAmountFilled);
|
|
@@ -34,7 +34,7 @@ function isOrderRiskIncreasing(user, order) {
|
|
|
34
34
|
}
|
|
35
35
|
exports.isOrderRiskIncreasing = isOrderRiskIncreasing;
|
|
36
36
|
function isOrderRiskIncreasingInSameDirection(user, order) {
|
|
37
|
-
if (types_1.isVariant(order.status, 'init')) {
|
|
37
|
+
if ((0, types_1.isVariant)(order.status, 'init')) {
|
|
38
38
|
return false;
|
|
39
39
|
}
|
|
40
40
|
const position = user.getPerpPosition(order.marketIndex) ||
|
|
@@ -44,31 +44,31 @@ function isOrderRiskIncreasingInSameDirection(user, order) {
|
|
|
44
44
|
return true;
|
|
45
45
|
}
|
|
46
46
|
// if position is long and order is long
|
|
47
|
-
if (position.baseAssetAmount.gt(numericConstants_1.ZERO) && types_1.isVariant(order.direction, 'long')) {
|
|
47
|
+
if (position.baseAssetAmount.gt(numericConstants_1.ZERO) && (0, types_1.isVariant)(order.direction, 'long')) {
|
|
48
48
|
return true;
|
|
49
49
|
}
|
|
50
50
|
// if position is short and order is short
|
|
51
51
|
if (position.baseAssetAmount.lt(numericConstants_1.ZERO) &&
|
|
52
|
-
types_1.isVariant(order.direction, 'short')) {
|
|
52
|
+
(0, types_1.isVariant)(order.direction, 'short')) {
|
|
53
53
|
return true;
|
|
54
54
|
}
|
|
55
55
|
return false;
|
|
56
56
|
}
|
|
57
57
|
exports.isOrderRiskIncreasingInSameDirection = isOrderRiskIncreasingInSameDirection;
|
|
58
58
|
function isOrderReduceOnly(user, order) {
|
|
59
|
-
if (types_1.isVariant(order.status, 'init')) {
|
|
59
|
+
if ((0, types_1.isVariant)(order.status, 'init')) {
|
|
60
60
|
return false;
|
|
61
61
|
}
|
|
62
62
|
const position = user.getPerpPosition(order.marketIndex) ||
|
|
63
63
|
user.getEmptyPosition(order.marketIndex);
|
|
64
64
|
// if position is long and order is long
|
|
65
65
|
if (position.baseAssetAmount.gte(numericConstants_1.ZERO) &&
|
|
66
|
-
types_1.isVariant(order.direction, 'long')) {
|
|
66
|
+
(0, types_1.isVariant)(order.direction, 'long')) {
|
|
67
67
|
return false;
|
|
68
68
|
}
|
|
69
69
|
// if position is short and order is short
|
|
70
70
|
if (position.baseAssetAmount.lte(numericConstants_1.ZERO) &&
|
|
71
|
-
types_1.isVariant(order.direction, 'short')) {
|
|
71
|
+
(0, types_1.isVariant)(order.direction, 'short')) {
|
|
72
72
|
return false;
|
|
73
73
|
}
|
|
74
74
|
return true;
|
|
@@ -82,7 +82,7 @@ exports.standardizeBaseAssetAmount = standardizeBaseAssetAmount;
|
|
|
82
82
|
function getLimitPrice(order, oraclePriceData, slot, fallbackPrice) {
|
|
83
83
|
let limitPrice;
|
|
84
84
|
if (hasAuctionPrice(order, slot)) {
|
|
85
|
-
limitPrice = auction_1.getAuctionPrice(order, slot, oraclePriceData.price);
|
|
85
|
+
limitPrice = (0, auction_1.getAuctionPrice)(order, slot, oraclePriceData.price);
|
|
86
86
|
}
|
|
87
87
|
else if (order.oraclePriceOffset !== 0) {
|
|
88
88
|
limitPrice = oraclePriceData.price.add(new anchor_1.BN(order.oraclePriceOffset));
|
|
@@ -99,16 +99,16 @@ exports.getLimitPrice = getLimitPrice;
|
|
|
99
99
|
function hasLimitPrice(order, slot) {
|
|
100
100
|
return (order.price.gt(numericConstants_1.ZERO) ||
|
|
101
101
|
order.oraclePriceOffset != 0 ||
|
|
102
|
-
!auction_1.isAuctionComplete(order, slot));
|
|
102
|
+
!(0, auction_1.isAuctionComplete)(order, slot));
|
|
103
103
|
}
|
|
104
104
|
exports.hasLimitPrice = hasLimitPrice;
|
|
105
105
|
function hasAuctionPrice(order, slot) {
|
|
106
|
-
return (!auction_1.isAuctionComplete(order, slot) &&
|
|
106
|
+
return (!(0, auction_1.isAuctionComplete)(order, slot) &&
|
|
107
107
|
(!order.auctionStartPrice.eq(numericConstants_1.ZERO) || !order.auctionEndPrice.eq(numericConstants_1.ZERO)));
|
|
108
108
|
}
|
|
109
109
|
exports.hasAuctionPrice = hasAuctionPrice;
|
|
110
110
|
function isFillableByVAMM(order, market, oraclePriceData, slot, ts) {
|
|
111
|
-
return ((auction_1.isAuctionComplete(order, slot) &&
|
|
111
|
+
return (((0, auction_1.isAuctionComplete)(order, slot) &&
|
|
112
112
|
calculateBaseAssetAmountForAmmToFulfill(order, market, oraclePriceData, slot).gte(market.amm.minOrderSize)) ||
|
|
113
113
|
isOrderExpired(order, ts));
|
|
114
114
|
}
|
|
@@ -119,19 +119,22 @@ function calculateBaseAssetAmountForAmmToFulfill(order, market, oraclePriceData,
|
|
|
119
119
|
}
|
|
120
120
|
const limitPrice = getLimitPrice(order, oraclePriceData, slot);
|
|
121
121
|
let baseAssetAmount;
|
|
122
|
-
const updatedAMM = amm_1.calculateUpdatedAMM(market.amm, oraclePriceData);
|
|
122
|
+
const updatedAMM = (0, amm_1.calculateUpdatedAMM)(market.amm, oraclePriceData);
|
|
123
123
|
if (limitPrice !== undefined) {
|
|
124
124
|
baseAssetAmount = calculateBaseAssetAmountToFillUpToLimitPrice(order, updatedAMM, limitPrice, oraclePriceData);
|
|
125
125
|
}
|
|
126
126
|
else {
|
|
127
127
|
baseAssetAmount = order.baseAssetAmount.sub(order.baseAssetAmountFilled);
|
|
128
128
|
}
|
|
129
|
-
const maxBaseAssetAmount = amm_1.calculateMaxBaseAssetAmountFillable(updatedAMM, order.direction);
|
|
129
|
+
const maxBaseAssetAmount = (0, amm_1.calculateMaxBaseAssetAmountFillable)(updatedAMM, order.direction);
|
|
130
130
|
return anchor_1.BN.min(maxBaseAssetAmount, baseAssetAmount);
|
|
131
131
|
}
|
|
132
132
|
exports.calculateBaseAssetAmountForAmmToFulfill = calculateBaseAssetAmountForAmmToFulfill;
|
|
133
133
|
function calculateBaseAssetAmountToFillUpToLimitPrice(order, amm, limitPrice, oraclePriceData) {
|
|
134
|
-
const
|
|
134
|
+
const adjustedLimitPrice = (0, types_1.isVariant)(order.direction, 'long')
|
|
135
|
+
? limitPrice.sub(amm.orderTickSize)
|
|
136
|
+
: limitPrice.add(amm.orderTickSize);
|
|
137
|
+
const [maxAmountToTrade, direction] = (0, amm_1.calculateMaxBaseAssetAmountToTrade)(amm, adjustedLimitPrice, order.direction, oraclePriceData);
|
|
135
138
|
const baseAssetAmount = standardizeBaseAssetAmount(maxAmountToTrade, amm.orderStepSize);
|
|
136
139
|
// Check that directions are the same
|
|
137
140
|
const sameDirection = isSameDirection(direction, order.direction);
|
|
@@ -145,12 +148,12 @@ function calculateBaseAssetAmountToFillUpToLimitPrice(order, amm, limitPrice, or
|
|
|
145
148
|
}
|
|
146
149
|
exports.calculateBaseAssetAmountToFillUpToLimitPrice = calculateBaseAssetAmountToFillUpToLimitPrice;
|
|
147
150
|
function isSameDirection(firstDirection, secondDirection) {
|
|
148
|
-
return ((types_1.isVariant(firstDirection, 'long') && types_1.isVariant(secondDirection, 'long')) ||
|
|
149
|
-
(types_1.isVariant(firstDirection, 'short') && types_1.isVariant(secondDirection, 'short')));
|
|
151
|
+
return (((0, types_1.isVariant)(firstDirection, 'long') && (0, types_1.isVariant)(secondDirection, 'long')) ||
|
|
152
|
+
((0, types_1.isVariant)(firstDirection, 'short') && (0, types_1.isVariant)(secondDirection, 'short')));
|
|
150
153
|
}
|
|
151
154
|
function isOrderExpired(order, ts) {
|
|
152
155
|
if (mustBeTriggered(order) ||
|
|
153
|
-
!types_1.isVariant(order.status, 'open') ||
|
|
156
|
+
!(0, types_1.isVariant)(order.status, 'open') ||
|
|
154
157
|
order.maxTs.eq(numericConstants_1.ZERO)) {
|
|
155
158
|
return false;
|
|
156
159
|
}
|
|
@@ -158,26 +161,26 @@ function isOrderExpired(order, ts) {
|
|
|
158
161
|
}
|
|
159
162
|
exports.isOrderExpired = isOrderExpired;
|
|
160
163
|
function isMarketOrder(order) {
|
|
161
|
-
return types_1.isOneOfVariant(order.orderType, ['market', 'triggerMarket', 'oracle']);
|
|
164
|
+
return (0, types_1.isOneOfVariant)(order.orderType, ['market', 'triggerMarket', 'oracle']);
|
|
162
165
|
}
|
|
163
166
|
exports.isMarketOrder = isMarketOrder;
|
|
164
167
|
function isLimitOrder(order) {
|
|
165
|
-
return types_1.isOneOfVariant(order.orderType, ['limit', 'triggerLimit']);
|
|
168
|
+
return (0, types_1.isOneOfVariant)(order.orderType, ['limit', 'triggerLimit']);
|
|
166
169
|
}
|
|
167
170
|
exports.isLimitOrder = isLimitOrder;
|
|
168
171
|
function mustBeTriggered(order) {
|
|
169
|
-
return types_1.isOneOfVariant(order.orderType, ['triggerMarket', 'triggerLimit']);
|
|
172
|
+
return (0, types_1.isOneOfVariant)(order.orderType, ['triggerMarket', 'triggerLimit']);
|
|
170
173
|
}
|
|
171
174
|
exports.mustBeTriggered = mustBeTriggered;
|
|
172
175
|
function isTriggered(order) {
|
|
173
|
-
return types_1.isOneOfVariant(order.triggerCondition, [
|
|
176
|
+
return (0, types_1.isOneOfVariant)(order.triggerCondition, [
|
|
174
177
|
'triggeredAbove',
|
|
175
178
|
'triggeredBelow',
|
|
176
179
|
]);
|
|
177
180
|
}
|
|
178
181
|
exports.isTriggered = isTriggered;
|
|
179
182
|
function isRestingLimitOrder(order, slot) {
|
|
180
|
-
return (isLimitOrder(order) && (order.postOnly || auction_1.isAuctionComplete(order, slot)));
|
|
183
|
+
return (isLimitOrder(order) && (order.postOnly || (0, auction_1.isAuctionComplete)(order, slot)));
|
|
181
184
|
}
|
|
182
185
|
exports.isRestingLimitOrder = isRestingLimitOrder;
|
|
183
186
|
function isTakingOrder(order, slot) {
|
package/lib/math/position.js
CHANGED
|
@@ -23,7 +23,7 @@ function calculateBaseAssetValue(market, userPosition, oraclePriceData, useSprea
|
|
|
23
23
|
let prepegAmm;
|
|
24
24
|
if (!skipUpdate) {
|
|
25
25
|
if (market.amm.baseSpread > 0 && useSpread) {
|
|
26
|
-
const { baseAssetReserve, quoteAssetReserve, sqrtK, newPeg } = amm_1.calculateUpdatedAMMSpreadReserves(market.amm, directionToClose, oraclePriceData);
|
|
26
|
+
const { baseAssetReserve, quoteAssetReserve, sqrtK, newPeg } = (0, amm_1.calculateUpdatedAMMSpreadReserves)(market.amm, directionToClose, oraclePriceData);
|
|
27
27
|
prepegAmm = {
|
|
28
28
|
baseAssetReserve,
|
|
29
29
|
quoteAssetReserve,
|
|
@@ -32,13 +32,13 @@ function calculateBaseAssetValue(market, userPosition, oraclePriceData, useSprea
|
|
|
32
32
|
};
|
|
33
33
|
}
|
|
34
34
|
else {
|
|
35
|
-
prepegAmm = amm_1.calculateUpdatedAMM(market.amm, oraclePriceData);
|
|
35
|
+
prepegAmm = (0, amm_1.calculateUpdatedAMM)(market.amm, oraclePriceData);
|
|
36
36
|
}
|
|
37
37
|
}
|
|
38
38
|
else {
|
|
39
39
|
prepegAmm = market.amm;
|
|
40
40
|
}
|
|
41
|
-
const [newQuoteAssetReserve, _] = amm_1.calculateAmmReservesAfterSwap(prepegAmm, 'base', userPosition.baseAssetAmount.abs(), amm_1.getSwapDirection('base', directionToClose));
|
|
41
|
+
const [newQuoteAssetReserve, _] = (0, amm_1.calculateAmmReservesAfterSwap)(prepegAmm, 'base', userPosition.baseAssetAmount.abs(), (0, amm_1.getSwapDirection)('base', directionToClose));
|
|
42
42
|
switch (directionToClose) {
|
|
43
43
|
case types_1.PositionDirection.SHORT:
|
|
44
44
|
return prepegAmm.quoteAssetReserve
|
|
@@ -67,7 +67,7 @@ function calculatePositionPNL(market, perpPosition, withFunding = false, oracleP
|
|
|
67
67
|
if (perpPosition.baseAssetAmount.eq(numericConstants_1.ZERO)) {
|
|
68
68
|
return perpPosition.quoteAssetAmount;
|
|
69
69
|
}
|
|
70
|
-
const baseAssetValue = margin_1.calculateBaseAssetValueWithOracle(market, perpPosition, oraclePriceData);
|
|
70
|
+
const baseAssetValue = (0, margin_1.calculateBaseAssetValueWithOracle)(market, perpPosition, oraclePriceData);
|
|
71
71
|
const baseAssetValueSign = perpPosition.baseAssetAmount.isNeg()
|
|
72
72
|
? new __1.BN(-1)
|
|
73
73
|
: new __1.BN(1);
|
|
@@ -86,7 +86,7 @@ function calculateClaimablePnl(market, spotMarket, perpPosition, oraclePriceData
|
|
|
86
86
|
const fundingPnL = calculatePositionFundingPNL(market, perpPosition);
|
|
87
87
|
let unsettledPnl = unrealizedPnl.add(fundingPnL);
|
|
88
88
|
if (unrealizedPnl.gt(numericConstants_1.ZERO)) {
|
|
89
|
-
const excessPnlPool = __1.BN.max(numericConstants_1.ZERO, market_1.calculateNetUserPnlImbalance(market, spotMarket, oraclePriceData).mul(new __1.BN(-1)));
|
|
89
|
+
const excessPnlPool = __1.BN.max(numericConstants_1.ZERO, (0, market_1.calculateNetUserPnlImbalance)(market, spotMarket, oraclePriceData).mul(new __1.BN(-1)));
|
|
90
90
|
const maxPositivePnl = __1.BN.max(perpPosition.quoteAssetAmount.sub(perpPosition.quoteEntryAmount), numericConstants_1.ZERO).add(excessPnlPool);
|
|
91
91
|
unsettledPnl = __1.BN.min(maxPositivePnl, unrealizedPnl);
|
|
92
92
|
}
|
package/lib/math/repeg.js
CHANGED
|
@@ -77,7 +77,7 @@ function calculateRepegCost(amm, newPeg) {
|
|
|
77
77
|
}
|
|
78
78
|
exports.calculateRepegCost = calculateRepegCost;
|
|
79
79
|
function calculateBudgetedKBN(x, y, budget, Q, d) {
|
|
80
|
-
assert_1.assert(Q.gt(new anchor_1.BN(0)));
|
|
80
|
+
(0, assert_1.assert)(Q.gt(new anchor_1.BN(0)));
|
|
81
81
|
const C = budget.mul(new anchor_1.BN(-1));
|
|
82
82
|
let dSign = new anchor_1.BN(1);
|
|
83
83
|
if (d.lt(new anchor_1.BN(0))) {
|
package/lib/math/spotBalance.js
CHANGED
|
@@ -9,11 +9,11 @@ const numericConstants_2 = require("../constants/numericConstants");
|
|
|
9
9
|
const utils_1 = require("./utils");
|
|
10
10
|
function getBalance(tokenAmount, spotMarket, balanceType) {
|
|
11
11
|
const precisionIncrease = numericConstants_1.TEN.pow(new anchor_1.BN(19 - spotMarket.decimals));
|
|
12
|
-
const cumulativeInterest = types_1.isVariant(balanceType, 'deposit')
|
|
12
|
+
const cumulativeInterest = (0, types_1.isVariant)(balanceType, 'deposit')
|
|
13
13
|
? spotMarket.cumulativeDepositInterest
|
|
14
14
|
: spotMarket.cumulativeBorrowInterest;
|
|
15
15
|
let balance = tokenAmount.mul(precisionIncrease).div(cumulativeInterest);
|
|
16
|
-
if (!balance.eq(numericConstants_1.ZERO) && types_1.isVariant(balanceType, 'borrow')) {
|
|
16
|
+
if (!balance.eq(numericConstants_1.ZERO) && (0, types_1.isVariant)(balanceType, 'borrow')) {
|
|
17
17
|
balance = balance.add(numericConstants_1.ONE);
|
|
18
18
|
}
|
|
19
19
|
return balance;
|
|
@@ -21,18 +21,18 @@ function getBalance(tokenAmount, spotMarket, balanceType) {
|
|
|
21
21
|
exports.getBalance = getBalance;
|
|
22
22
|
function getTokenAmount(balanceAmount, spotMarket, balanceType) {
|
|
23
23
|
const precisionDecrease = numericConstants_1.TEN.pow(new anchor_1.BN(19 - spotMarket.decimals));
|
|
24
|
-
if (types_1.isVariant(balanceType, 'deposit')) {
|
|
24
|
+
if ((0, types_1.isVariant)(balanceType, 'deposit')) {
|
|
25
25
|
return balanceAmount
|
|
26
26
|
.mul(spotMarket.cumulativeDepositInterest)
|
|
27
27
|
.div(precisionDecrease);
|
|
28
28
|
}
|
|
29
29
|
else {
|
|
30
|
-
return utils_1.divCeil(balanceAmount.mul(spotMarket.cumulativeBorrowInterest), precisionDecrease);
|
|
30
|
+
return (0, utils_1.divCeil)(balanceAmount.mul(spotMarket.cumulativeBorrowInterest), precisionDecrease);
|
|
31
31
|
}
|
|
32
32
|
}
|
|
33
33
|
exports.getTokenAmount = getTokenAmount;
|
|
34
34
|
function getSignedTokenAmount(tokenAmount, balanceType) {
|
|
35
|
-
if (types_1.isVariant(balanceType, 'deposit')) {
|
|
35
|
+
if ((0, types_1.isVariant)(balanceType, 'deposit')) {
|
|
36
36
|
return tokenAmount;
|
|
37
37
|
}
|
|
38
38
|
else {
|
|
@@ -77,10 +77,10 @@ function calculateAssetWeight(balanceAmount, spotMarket, marginCategory) {
|
|
|
77
77
|
let assetWeight;
|
|
78
78
|
switch (marginCategory) {
|
|
79
79
|
case 'Initial':
|
|
80
|
-
assetWeight = margin_1.calculateSizeDiscountAssetWeight(sizeInAmmReservePrecision, new anchor_1.BN(spotMarket.imfFactor), new anchor_1.BN(spotMarket.initialAssetWeight));
|
|
80
|
+
assetWeight = (0, margin_1.calculateSizeDiscountAssetWeight)(sizeInAmmReservePrecision, new anchor_1.BN(spotMarket.imfFactor), new anchor_1.BN(spotMarket.initialAssetWeight));
|
|
81
81
|
break;
|
|
82
82
|
case 'Maintenance':
|
|
83
|
-
assetWeight = margin_1.calculateSizeDiscountAssetWeight(sizeInAmmReservePrecision, new anchor_1.BN(spotMarket.imfFactor), new anchor_1.BN(spotMarket.maintenanceAssetWeight));
|
|
83
|
+
assetWeight = (0, margin_1.calculateSizeDiscountAssetWeight)(sizeInAmmReservePrecision, new anchor_1.BN(spotMarket.imfFactor), new anchor_1.BN(spotMarket.maintenanceAssetWeight));
|
|
84
84
|
break;
|
|
85
85
|
default:
|
|
86
86
|
assetWeight = new anchor_1.BN(spotMarket.initialAssetWeight);
|
|
@@ -103,10 +103,10 @@ function calculateLiabilityWeight(size, spotMarket, marginCategory) {
|
|
|
103
103
|
let liabilityWeight;
|
|
104
104
|
switch (marginCategory) {
|
|
105
105
|
case 'Initial':
|
|
106
|
-
liabilityWeight = margin_1.calculateSizePremiumLiabilityWeight(sizeInAmmReservePrecision, new anchor_1.BN(spotMarket.imfFactor), new anchor_1.BN(spotMarket.initialLiabilityWeight), numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION);
|
|
106
|
+
liabilityWeight = (0, margin_1.calculateSizePremiumLiabilityWeight)(sizeInAmmReservePrecision, new anchor_1.BN(spotMarket.imfFactor), new anchor_1.BN(spotMarket.initialLiabilityWeight), numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION);
|
|
107
107
|
break;
|
|
108
108
|
case 'Maintenance':
|
|
109
|
-
liabilityWeight = margin_1.calculateSizePremiumLiabilityWeight(sizeInAmmReservePrecision, new anchor_1.BN(spotMarket.imfFactor), new anchor_1.BN(spotMarket.maintenanceLiabilityWeight), numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION);
|
|
109
|
+
liabilityWeight = (0, margin_1.calculateSizePremiumLiabilityWeight)(sizeInAmmReservePrecision, new anchor_1.BN(spotMarket.imfFactor), new anchor_1.BN(spotMarket.maintenanceLiabilityWeight), numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION);
|
|
110
110
|
break;
|
|
111
111
|
default:
|
|
112
112
|
liabilityWeight = spotMarket.initialLiabilityWeight;
|
package/lib/math/spotMarket.js
CHANGED
|
@@ -10,12 +10,12 @@ function castNumberToSpotPrecision(value, spotMarket) {
|
|
|
10
10
|
}
|
|
11
11
|
exports.castNumberToSpotPrecision = castNumberToSpotPrecision;
|
|
12
12
|
function calculateSpotMarketMarginRatio(market, marginCategory, size, balanceType) {
|
|
13
|
-
if (types_1.isVariant(balanceType, 'deposit')) {
|
|
14
|
-
const assetWeight = spotBalance_1.calculateAssetWeight(size, market, marginCategory);
|
|
13
|
+
if ((0, types_1.isVariant)(balanceType, 'deposit')) {
|
|
14
|
+
const assetWeight = (0, spotBalance_1.calculateAssetWeight)(size, market, marginCategory);
|
|
15
15
|
return numericConstants_1.MARGIN_PRECISION.sub(assetWeight).toNumber();
|
|
16
16
|
}
|
|
17
17
|
else {
|
|
18
|
-
const liabilityWeight = spotBalance_1.calculateLiabilityWeight(size, market, marginCategory);
|
|
18
|
+
const liabilityWeight = (0, spotBalance_1.calculateLiabilityWeight)(size, market, marginCategory);
|
|
19
19
|
return liabilityWeight.sub(numericConstants_1.MARGIN_PRECISION).toNumber();
|
|
20
20
|
}
|
|
21
21
|
}
|
package/lib/math/spotPosition.js
CHANGED
|
@@ -8,15 +8,15 @@ function isSpotPositionAvailable(position) {
|
|
|
8
8
|
}
|
|
9
9
|
exports.isSpotPositionAvailable = isSpotPositionAvailable;
|
|
10
10
|
function getWorstCaseTokenAmounts(spotPosition, spotMarketAccount, oraclePriceData) {
|
|
11
|
-
const tokenAmount = spotBalance_1.getSignedTokenAmount(spotBalance_1.getTokenAmount(spotPosition.scaledBalance, spotMarketAccount, spotPosition.balanceType), spotPosition.balanceType);
|
|
11
|
+
const tokenAmount = (0, spotBalance_1.getSignedTokenAmount)((0, spotBalance_1.getTokenAmount)(spotPosition.scaledBalance, spotMarketAccount, spotPosition.balanceType), spotPosition.balanceType);
|
|
12
12
|
const tokenAmountAllBidsFill = tokenAmount.add(spotPosition.openBids);
|
|
13
13
|
const tokenAmountAllAsksFill = tokenAmount.add(spotPosition.openAsks);
|
|
14
14
|
if (tokenAmountAllBidsFill.abs().gt(tokenAmountAllAsksFill.abs())) {
|
|
15
|
-
const worstCaseQuoteTokenAmount = spotBalance_1.getTokenValue(spotPosition.openBids.neg(), spotMarketAccount.decimals, oraclePriceData);
|
|
15
|
+
const worstCaseQuoteTokenAmount = (0, spotBalance_1.getTokenValue)(spotPosition.openBids.neg(), spotMarketAccount.decimals, oraclePriceData);
|
|
16
16
|
return [tokenAmountAllBidsFill, worstCaseQuoteTokenAmount];
|
|
17
17
|
}
|
|
18
18
|
else {
|
|
19
|
-
const worstCaseQuoteTokenAmount = spotBalance_1.getTokenValue(spotPosition.openAsks.neg(), spotMarketAccount.decimals, oraclePriceData);
|
|
19
|
+
const worstCaseQuoteTokenAmount = (0, spotBalance_1.getTokenValue)(spotPosition.openAsks.neg(), spotMarketAccount.decimals, oraclePriceData);
|
|
20
20
|
return [tokenAmountAllAsksFill, worstCaseQuoteTokenAmount];
|
|
21
21
|
}
|
|
22
22
|
}
|
package/lib/math/trade.d.ts
CHANGED
|
@@ -6,7 +6,7 @@ import { OraclePriceData } from '../oracles/types';
|
|
|
6
6
|
import { DLOB } from '../dlob/DLOB';
|
|
7
7
|
import { PublicKey } from '@solana/web3.js';
|
|
8
8
|
import { Orderbook } from '@project-serum/serum';
|
|
9
|
-
export
|
|
9
|
+
export type PriceImpactUnit = 'entryPrice' | 'maxPrice' | 'priceDelta' | 'priceDeltaAsNumber' | 'pctAvg' | 'pctMax' | 'quoteAssetAmount' | 'quoteAssetAmountPeg' | 'acquiredBaseAssetAmount' | 'acquiredQuoteAssetAmount' | 'all';
|
|
10
10
|
/**
|
|
11
11
|
* Calculates avg/max slippage (price impact) for candidate trade
|
|
12
12
|
*
|