@drift-labs/sdk 0.2.0-master.24 → 0.2.0-master.26
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +13 -13
- package/lib/accounts/pollingClearingHouseAccountSubscriber.js +27 -27
- package/lib/accounts/types.d.ts +8 -9
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +14 -14
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +35 -34
- package/lib/addresses/pda.d.ts +8 -6
- package/lib/addresses/pda.js +35 -27
- package/lib/admin.d.ts +9 -6
- package/lib/admin.js +90 -54
- package/lib/clearingHouse.d.ts +71 -42
- package/lib/clearingHouse.js +765 -282
- package/lib/clearingHouseConfig.d.ts +2 -2
- package/lib/clearingHouseUser.d.ts +19 -17
- package/lib/clearingHouseUser.js +145 -123
- package/lib/config.d.ts +7 -7
- package/lib/config.js +21 -21
- package/lib/constants/numericConstants.d.ts +12 -12
- package/lib/constants/numericConstants.js +13 -13
- package/lib/constants/{markets.d.ts → perpMarkets.d.ts} +5 -5
- package/{src/constants/markets.js → lib/constants/perpMarkets.js} +4 -4
- package/lib/constants/{banks.d.ts → spotMarkets.d.ts} +6 -6
- package/lib/constants/{banks.js → spotMarkets.js} +16 -16
- package/lib/events/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/types.d.ts +4 -2
- package/lib/events/types.js +2 -0
- package/lib/examples/makeTradeExample.js +18 -6
- package/lib/idl/clearing_house.json +1128 -347
- package/lib/index.d.ts +6 -3
- package/lib/index.js +6 -3
- package/lib/math/amm.d.ts +5 -3
- package/lib/math/amm.js +42 -4
- package/lib/math/funding.d.ts +6 -6
- package/lib/math/funding.js +2 -1
- package/lib/math/margin.d.ts +4 -4
- package/lib/math/margin.js +18 -11
- package/lib/math/market.d.ts +10 -9
- package/lib/math/market.js +29 -6
- package/lib/math/oracles.d.ts +2 -1
- package/lib/math/oracles.js +11 -1
- package/lib/math/orders.d.ts +5 -5
- package/lib/math/position.d.ts +13 -13
- package/lib/math/position.js +19 -19
- package/lib/math/spotBalance.d.ts +19 -0
- package/lib/math/spotBalance.js +176 -0
- package/lib/math/spotMarket.d.ts +4 -0
- package/lib/math/spotMarket.js +8 -0
- package/lib/math/spotPosition.d.ts +2 -0
- package/lib/math/spotPosition.js +8 -0
- package/lib/math/state.js +2 -2
- package/lib/math/trade.d.ts +4 -4
- package/lib/orderParams.d.ts +4 -4
- package/lib/orderParams.js +12 -4
- package/lib/serum/serumSubscriber.d.ts +23 -0
- package/lib/serum/serumSubscriber.js +41 -0
- package/lib/serum/types.d.ts +11 -0
- package/lib/serum/types.js +2 -0
- package/lib/tx/retryTxSender.d.ts +1 -1
- package/lib/tx/retryTxSender.js +4 -2
- package/lib/tx/types.d.ts +1 -1
- package/lib/types.d.ts +148 -39
- package/lib/types.js +37 -9
- package/my-script/.env +7 -0
- package/my-script/getUserStats.ts +106 -0
- package/my-script/multiConnections.ts +119 -0
- package/my-script/test-regex.ts +11 -0
- package/my-script/utils.ts +52 -0
- package/package.json +1 -1
- package/src/accounts/bulkAccountLoader.js +249 -0
- package/src/accounts/bulkUserStatsSubscription.js +75 -0
- package/src/accounts/bulkUserSubscription.js +75 -0
- package/src/accounts/fetch.js +92 -0
- package/src/accounts/pollingClearingHouseAccountSubscriber.js +465 -0
- package/src/accounts/pollingClearingHouseAccountSubscriber.ts +38 -38
- package/src/accounts/pollingOracleSubscriber.js +156 -0
- package/src/accounts/pollingTokenAccountSubscriber.js +141 -0
- package/src/accounts/pollingUserAccountSubscriber.js +208 -0
- package/src/accounts/pollingUserStatsAccountSubscriber.js +208 -0
- package/src/accounts/types.js +28 -0
- package/src/accounts/types.ts +11 -9
- package/src/accounts/utils.js +7 -0
- package/src/accounts/webSocketAccountSubscriber.js +138 -0
- package/src/accounts/webSocketClearingHouseAccountSubscriber.js +433 -0
- package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +59 -52
- package/src/accounts/webSocketUserAccountSubscriber.js +113 -0
- package/src/accounts/webSocketUserStatsAccountSubsriber.js +113 -0
- package/src/addresses/pda.js +186 -0
- package/src/addresses/pda.ts +56 -42
- package/src/admin.js +1284 -0
- package/src/admin.ts +149 -75
- package/src/assert/assert.js +1 -1
- package/src/clearingHouse.js +3433 -0
- package/src/clearingHouse.ts +1097 -380
- package/src/clearingHouseConfig.js +2 -0
- package/src/clearingHouseConfig.ts +2 -2
- package/src/clearingHouseUser.js +874 -0
- package/src/clearingHouseUser.ts +237 -172
- package/src/clearingHouseUserConfig.js +2 -0
- package/src/clearingHouseUserStats.js +115 -0
- package/src/clearingHouseUserStatsConfig.js +2 -0
- package/src/config.js +80 -0
- package/src/config.ts +30 -30
- package/src/constants/numericConstants.js +18 -11
- package/src/constants/numericConstants.ts +17 -15
- package/{lib/constants/markets.js → src/constants/perpMarkets.js} +11 -11
- package/src/constants/{markets.ts → perpMarkets.ts} +5 -5
- package/src/constants/spotMarkets.js +51 -0
- package/src/constants/{banks.ts → spotMarkets.ts} +19 -19
- package/src/events/eventList.js +66 -23
- package/src/events/eventSubscriber.js +202 -0
- package/src/events/eventSubscriber.ts +20 -12
- package/src/events/fetchLogs.js +117 -0
- package/src/events/fetchLogs.ts +35 -8
- package/src/events/pollingLogProvider.js +113 -0
- package/src/events/pollingLogProvider.ts +10 -2
- package/src/events/sort.js +41 -0
- package/src/events/txEventCache.js +22 -19
- package/src/events/types.js +25 -0
- package/src/events/types.ts +7 -1
- package/src/events/webSocketLogProvider.js +76 -0
- package/src/examples/makeTradeExample.ts +27 -6
- package/src/factory/bigNum.js +183 -180
- package/src/factory/oracleClient.js +9 -9
- package/src/idl/clearing_house.json +1128 -347
- package/src/index.js +75 -0
- package/src/index.ts +6 -3
- package/src/math/amm.js +422 -0
- package/src/math/amm.ts +73 -5
- package/src/math/auction.js +10 -10
- package/src/math/conversion.js +4 -3
- package/src/math/funding.js +223 -175
- package/src/math/funding.ts +7 -7
- package/src/math/insurance.js +27 -0
- package/src/math/margin.js +77 -0
- package/src/math/margin.ts +34 -23
- package/src/math/market.js +105 -0
- package/src/math/market.ts +71 -19
- package/src/math/oracles.js +40 -10
- package/src/math/oracles.ts +18 -1
- package/src/math/orders.js +153 -0
- package/src/math/orders.ts +5 -5
- package/src/math/position.js +172 -0
- package/src/math/position.ts +31 -31
- package/src/math/repeg.js +40 -40
- package/src/math/spotBalance.js +176 -0
- package/src/math/spotBalance.ts +290 -0
- package/src/math/spotMarket.js +8 -0
- package/src/math/spotMarket.ts +9 -0
- package/src/math/spotPosition.js +8 -0
- package/src/math/spotPosition.ts +6 -0
- package/src/math/state.ts +2 -2
- package/src/math/trade.js +81 -74
- package/src/math/trade.ts +4 -4
- package/src/math/utils.js +8 -7
- package/src/oracles/oracleClientCache.js +10 -9
- package/src/oracles/pythClient.js +52 -17
- package/src/oracles/quoteAssetOracleClient.js +44 -13
- package/src/oracles/switchboardClient.js +69 -37
- package/src/oracles/types.js +1 -1
- package/src/orderParams.js +14 -6
- package/src/orderParams.ts +16 -8
- package/src/serum/serumSubscriber.js +102 -0
- package/src/serum/serumSubscriber.ts +80 -0
- package/src/serum/types.js +2 -0
- package/src/serum/types.ts +13 -0
- package/src/slot/SlotSubscriber.js +67 -20
- package/src/token/index.js +4 -4
- package/src/tokenFaucet.js +288 -154
- package/src/tx/retryTxSender.js +280 -0
- package/src/tx/retryTxSender.ts +5 -2
- package/src/tx/types.js +1 -1
- package/src/tx/types.ts +2 -1
- package/src/tx/utils.js +7 -6
- package/src/types.js +216 -0
- package/src/types.ts +131 -39
- package/src/userName.js +5 -5
- package/src/util/computeUnits.js +46 -11
- package/src/util/promiseTimeout.js +5 -5
- package/src/util/tps.js +46 -12
- package/src/wallet.js +55 -18
- package/lib/math/bankBalance.d.ts +0 -15
- package/lib/math/bankBalance.js +0 -150
- package/src/addresses/marketAddresses.js +0 -26
- package/src/constants/banks.js +0 -42
- package/src/examples/makeTradeExample.js +0 -80
- package/src/math/bankBalance.ts +0 -258
- package/src/math/state.js +0 -15
- package/src/math/utils.js.map +0 -1
- package/src/util/getTokenAddress.js +0 -9
package/lib/index.d.ts
CHANGED
|
@@ -6,7 +6,7 @@ export * from './oracles/types';
|
|
|
6
6
|
export * from './oracles/pythClient';
|
|
7
7
|
export * from './oracles/switchboardClient';
|
|
8
8
|
export * from './types';
|
|
9
|
-
export * from './constants/
|
|
9
|
+
export * from './constants/perpMarkets';
|
|
10
10
|
export * from './accounts/fetch';
|
|
11
11
|
export * from './accounts/webSocketClearingHouseAccountSubscriber';
|
|
12
12
|
export * from './accounts/bulkAccountLoader';
|
|
@@ -29,7 +29,9 @@ export * from './factory/oracleClient';
|
|
|
29
29
|
export * from './factory/bigNum';
|
|
30
30
|
export * from './events/types';
|
|
31
31
|
export * from './events/eventSubscriber';
|
|
32
|
+
export * from './events/fetchLogs';
|
|
32
33
|
export * from './math/auction';
|
|
34
|
+
export * from './math/spotMarket';
|
|
33
35
|
export * from './math/conversion';
|
|
34
36
|
export * from './math/funding';
|
|
35
37
|
export * from './math/market';
|
|
@@ -48,10 +50,11 @@ export * from './types';
|
|
|
48
50
|
export * from './math/utils';
|
|
49
51
|
export * from './config';
|
|
50
52
|
export * from './constants/numericConstants';
|
|
53
|
+
export * from './serum/serumSubscriber';
|
|
51
54
|
export * from './tx/retryTxSender';
|
|
52
55
|
export * from './util/computeUnits';
|
|
53
56
|
export * from './util/tps';
|
|
54
|
-
export * from './math/
|
|
55
|
-
export * from './constants/
|
|
57
|
+
export * from './math/spotBalance';
|
|
58
|
+
export * from './constants/spotMarkets';
|
|
56
59
|
export * from './clearingHouseConfig';
|
|
57
60
|
export { BN, PublicKey, pyth };
|
package/lib/index.js
CHANGED
|
@@ -29,7 +29,7 @@ __exportStar(require("./oracles/types"), exports);
|
|
|
29
29
|
__exportStar(require("./oracles/pythClient"), exports);
|
|
30
30
|
__exportStar(require("./oracles/switchboardClient"), exports);
|
|
31
31
|
__exportStar(require("./types"), exports);
|
|
32
|
-
__exportStar(require("./constants/
|
|
32
|
+
__exportStar(require("./constants/perpMarkets"), exports);
|
|
33
33
|
__exportStar(require("./accounts/fetch"), exports);
|
|
34
34
|
__exportStar(require("./accounts/webSocketClearingHouseAccountSubscriber"), exports);
|
|
35
35
|
__exportStar(require("./accounts/bulkAccountLoader"), exports);
|
|
@@ -52,7 +52,9 @@ __exportStar(require("./factory/oracleClient"), exports);
|
|
|
52
52
|
__exportStar(require("./factory/bigNum"), exports);
|
|
53
53
|
__exportStar(require("./events/types"), exports);
|
|
54
54
|
__exportStar(require("./events/eventSubscriber"), exports);
|
|
55
|
+
__exportStar(require("./events/fetchLogs"), exports);
|
|
55
56
|
__exportStar(require("./math/auction"), exports);
|
|
57
|
+
__exportStar(require("./math/spotMarket"), exports);
|
|
56
58
|
__exportStar(require("./math/conversion"), exports);
|
|
57
59
|
__exportStar(require("./math/funding"), exports);
|
|
58
60
|
__exportStar(require("./math/market"), exports);
|
|
@@ -71,9 +73,10 @@ __exportStar(require("./types"), exports);
|
|
|
71
73
|
__exportStar(require("./math/utils"), exports);
|
|
72
74
|
__exportStar(require("./config"), exports);
|
|
73
75
|
__exportStar(require("./constants/numericConstants"), exports);
|
|
76
|
+
__exportStar(require("./serum/serumSubscriber"), exports);
|
|
74
77
|
__exportStar(require("./tx/retryTxSender"), exports);
|
|
75
78
|
__exportStar(require("./util/computeUnits"), exports);
|
|
76
79
|
__exportStar(require("./util/tps"), exports);
|
|
77
|
-
__exportStar(require("./math/
|
|
78
|
-
__exportStar(require("./constants/
|
|
80
|
+
__exportStar(require("./math/spotBalance"), exports);
|
|
81
|
+
__exportStar(require("./constants/spotMarkets"), exports);
|
|
79
82
|
__exportStar(require("./clearingHouseConfig"), exports);
|
package/lib/math/amm.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="bn.js" />
|
|
2
2
|
import { BN } from '@project-serum/anchor';
|
|
3
|
-
import { AMM, PositionDirection, SwapDirection,
|
|
3
|
+
import { AMM, PositionDirection, SwapDirection, PerpMarketAccount } from '../types';
|
|
4
4
|
import { OraclePriceData } from '../oracles/types';
|
|
5
5
|
export declare function calculatePegFromTargetPrice(targetPrice: BN, baseAssetReserve: BN, quoteAssetReserve: BN): BN;
|
|
6
6
|
export declare function calculateOptimalPegAndBudget(amm: AMM, oraclePriceData: OraclePriceData): [BN, BN, BN, boolean];
|
|
@@ -33,9 +33,11 @@ export declare type AssetType = 'quote' | 'base';
|
|
|
33
33
|
* @returns quoteAssetReserve and baseAssetReserve after swap. : Precision AMM_RESERVE_PRECISION
|
|
34
34
|
*/
|
|
35
35
|
export declare function calculateAmmReservesAfterSwap(amm: Pick<AMM, 'pegMultiplier' | 'quoteAssetReserve' | 'sqrtK' | 'baseAssetReserve'>, inputAssetType: AssetType, swapAmount: BN, swapDirection: SwapDirection): [BN, BN];
|
|
36
|
+
export declare function calculateMarketOpenBidAsk(baseAssetReserve: BN, minBaseAssetReserve: BN, maxBaseAssetReserve: BN): [BN, BN];
|
|
37
|
+
export declare function calculateInventoryScale(netBaseAssetAmount: BN, baseAssetReserve: BN, minBaseAssetReserve: BN, maxBaseAssetReserve: BN): number;
|
|
36
38
|
export declare function calculateEffectiveLeverage(baseSpread: number, quoteAssetReserve: BN, terminalQuoteAssetReserve: BN, pegMultiplier: BN, netBaseAssetAmount: BN, markPrice: BN, totalFeeMinusDistributions: BN): number;
|
|
37
39
|
export declare function calculateMaxSpread(marginRatioInitial: number): number;
|
|
38
|
-
export declare function calculateSpreadBN(baseSpread: number, lastOracleMarkSpreadPct: BN, lastOracleConfPct: BN, maxSpread: number, quoteAssetReserve: BN, terminalQuoteAssetReserve: BN, pegMultiplier: BN, netBaseAssetAmount: BN, markPrice: BN, totalFeeMinusDistributions: BN): [number, number];
|
|
40
|
+
export declare function calculateSpreadBN(baseSpread: number, lastOracleMarkSpreadPct: BN, lastOracleConfPct: BN, maxSpread: number, quoteAssetReserve: BN, terminalQuoteAssetReserve: BN, pegMultiplier: BN, netBaseAssetAmount: BN, markPrice: BN, totalFeeMinusDistributions: BN, baseAssetReserve: BN, minBaseAssetReserve: BN, maxBaseAssetReserve: BN): [number, number];
|
|
39
41
|
export declare function calculateSpread(amm: AMM, direction: PositionDirection, oraclePriceData: OraclePriceData): number;
|
|
40
42
|
export declare function calculateSpreadReserves(amm: AMM, direction: PositionDirection, oraclePriceData: OraclePriceData): {
|
|
41
43
|
baseAssetReserve: BN;
|
|
@@ -64,7 +66,7 @@ export declare function getSwapDirection(inputAssetType: AssetType, positionDire
|
|
|
64
66
|
* @param market
|
|
65
67
|
* @returns cost : Precision MARK_PRICE_PRECISION
|
|
66
68
|
*/
|
|
67
|
-
export declare function calculateTerminalPrice(market:
|
|
69
|
+
export declare function calculateTerminalPrice(market: PerpMarketAccount): BN;
|
|
68
70
|
export declare function calculateMaxBaseAssetAmountToTrade(amm: AMM, limit_price: BN, direction: PositionDirection, oraclePriceData?: OraclePriceData): [BN, PositionDirection];
|
|
69
71
|
export declare function calculateQuoteAssetAmountSwapped(quoteAssetReserves: BN, pegMultiplier: BN, swapDirection: SwapDirection): BN;
|
|
70
72
|
export declare function calculateMaxBaseAssetAmountFillable(amm: AMM, orderDirection: PositionDirection): BN;
|
package/lib/math/amm.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.calculateMaxBaseAssetAmountFillable = 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;
|
|
3
|
+
exports.calculateMaxBaseAssetAmountFillable = exports.calculateQuoteAssetAmountSwapped = exports.calculateMaxBaseAssetAmountToTrade = exports.calculateTerminalPrice = exports.getSwapDirection = exports.calculateSwapOutput = exports.calculateSpreadReserves = exports.calculateSpread = exports.calculateSpreadBN = exports.calculateMaxSpread = exports.calculateEffectiveLeverage = exports.calculateInventoryScale = exports.calculateMarketOpenBidAsk = exports.calculateAmmReservesAfterSwap = exports.calculatePrice = exports.calculateBidAskPrice = exports.calculateUpdatedAMMSpreadReserves = exports.calculateUpdatedAMM = exports.calculateNewAmm = exports.calculateOptimalPegAndBudget = exports.calculatePegFromTargetPrice = void 0;
|
|
4
4
|
const anchor_1 = require("@project-serum/anchor");
|
|
5
5
|
const numericConstants_1 = require("../constants/numericConstants");
|
|
6
6
|
const types_1 = require("../types");
|
|
@@ -165,6 +165,36 @@ function calculateAmmReservesAfterSwap(amm, inputAssetType, swapAmount, swapDire
|
|
|
165
165
|
return [newQuoteAssetReserve, newBaseAssetReserve];
|
|
166
166
|
}
|
|
167
167
|
exports.calculateAmmReservesAfterSwap = calculateAmmReservesAfterSwap;
|
|
168
|
+
function calculateMarketOpenBidAsk(baseAssetReserve, minBaseAssetReserve, maxBaseAssetReserve) {
|
|
169
|
+
// open orders
|
|
170
|
+
let openAsks;
|
|
171
|
+
if (maxBaseAssetReserve > baseAssetReserve) {
|
|
172
|
+
openAsks = maxBaseAssetReserve.sub(baseAssetReserve).mul(new anchor_1.BN(-1));
|
|
173
|
+
}
|
|
174
|
+
else {
|
|
175
|
+
openAsks = numericConstants_1.ZERO;
|
|
176
|
+
}
|
|
177
|
+
let openBids;
|
|
178
|
+
if (minBaseAssetReserve < baseAssetReserve) {
|
|
179
|
+
openBids = baseAssetReserve.sub(minBaseAssetReserve);
|
|
180
|
+
}
|
|
181
|
+
else {
|
|
182
|
+
openBids = numericConstants_1.ZERO;
|
|
183
|
+
}
|
|
184
|
+
return [openBids, openAsks];
|
|
185
|
+
}
|
|
186
|
+
exports.calculateMarketOpenBidAsk = calculateMarketOpenBidAsk;
|
|
187
|
+
function calculateInventoryScale(netBaseAssetAmount, baseAssetReserve, minBaseAssetReserve, maxBaseAssetReserve) {
|
|
188
|
+
// inventory skew
|
|
189
|
+
const [openBids, openAsks] = calculateMarketOpenBidAsk(baseAssetReserve, minBaseAssetReserve, maxBaseAssetReserve);
|
|
190
|
+
const totalLiquidity = anchor_1.BN.max(openBids.abs().add(openAsks.abs()), new anchor_1.BN(1));
|
|
191
|
+
const inventoryScale = anchor_1.BN.min(netBaseAssetAmount.abs(), totalLiquidity)
|
|
192
|
+
.mul(numericConstants_1.BID_ASK_SPREAD_PRECISION.mul(new anchor_1.BN(5)))
|
|
193
|
+
.div(totalLiquidity)
|
|
194
|
+
.toNumber() / numericConstants_1.BID_ASK_SPREAD_PRECISION.toNumber();
|
|
195
|
+
return inventoryScale;
|
|
196
|
+
}
|
|
197
|
+
exports.calculateInventoryScale = calculateInventoryScale;
|
|
168
198
|
function calculateEffectiveLeverage(baseSpread, quoteAssetReserve, terminalQuoteAssetReserve, pegMultiplier, netBaseAssetAmount, markPrice, totalFeeMinusDistributions) {
|
|
169
199
|
// inventory skew
|
|
170
200
|
const netBaseAssetValue = quoteAssetReserve
|
|
@@ -187,7 +217,7 @@ function calculateMaxSpread(marginRatioInitial) {
|
|
|
187
217
|
return maxTargetSpread;
|
|
188
218
|
}
|
|
189
219
|
exports.calculateMaxSpread = calculateMaxSpread;
|
|
190
|
-
function calculateSpreadBN(baseSpread, lastOracleMarkSpreadPct, lastOracleConfPct, maxSpread, quoteAssetReserve, terminalQuoteAssetReserve, pegMultiplier, netBaseAssetAmount, markPrice, totalFeeMinusDistributions) {
|
|
220
|
+
function calculateSpreadBN(baseSpread, lastOracleMarkSpreadPct, lastOracleConfPct, maxSpread, quoteAssetReserve, terminalQuoteAssetReserve, pegMultiplier, netBaseAssetAmount, markPrice, totalFeeMinusDistributions, baseAssetReserve, minBaseAssetReserve, maxBaseAssetReserve) {
|
|
191
221
|
let longSpread = baseSpread / 2;
|
|
192
222
|
let shortSpread = baseSpread / 2;
|
|
193
223
|
if (lastOracleMarkSpreadPct.gt(numericConstants_1.ZERO)) {
|
|
@@ -196,8 +226,16 @@ function calculateSpreadBN(baseSpread, lastOracleMarkSpreadPct, lastOracleConfPc
|
|
|
196
226
|
else if (lastOracleMarkSpreadPct.lt(numericConstants_1.ZERO)) {
|
|
197
227
|
longSpread = Math.max(longSpread, lastOracleMarkSpreadPct.abs().toNumber() + lastOracleConfPct.toNumber());
|
|
198
228
|
}
|
|
199
|
-
const maxTargetSpread = maxSpread;
|
|
229
|
+
const maxTargetSpread = Math.max(maxSpread, lastOracleMarkSpreadPct.abs().toNumber());
|
|
200
230
|
const MAX_INVENTORY_SKEW = 5;
|
|
231
|
+
const inventoryScale = calculateInventoryScale(netBaseAssetAmount, baseAssetReserve, minBaseAssetReserve, maxBaseAssetReserve);
|
|
232
|
+
const inventorySpreadScale = Math.min(MAX_INVENTORY_SKEW, 1 + inventoryScale);
|
|
233
|
+
if (netBaseAssetAmount.gt(numericConstants_1.ZERO)) {
|
|
234
|
+
longSpread *= inventorySpreadScale;
|
|
235
|
+
}
|
|
236
|
+
else if (netBaseAssetAmount.lt(numericConstants_1.ZERO)) {
|
|
237
|
+
shortSpread *= inventorySpreadScale;
|
|
238
|
+
}
|
|
201
239
|
const effectiveLeverage = calculateEffectiveLeverage(baseSpread, quoteAssetReserve, terminalQuoteAssetReserve, pegMultiplier, netBaseAssetAmount, markPrice, totalFeeMinusDistributions);
|
|
202
240
|
if (totalFeeMinusDistributions.gt(numericConstants_1.ZERO)) {
|
|
203
241
|
const spreadScale = Math.min(MAX_INVENTORY_SKEW, 1 + effectiveLeverage);
|
|
@@ -240,7 +278,7 @@ function calculateSpread(amm, direction, oraclePriceData) {
|
|
|
240
278
|
const confIntervalPct = confInterval
|
|
241
279
|
.mul(numericConstants_1.BID_ASK_SPREAD_PRECISION)
|
|
242
280
|
.div(markPrice);
|
|
243
|
-
const [longSpread, shortSpread] = calculateSpreadBN(amm.baseSpread, targetMarkSpreadPct, confIntervalPct, amm.maxSpread, amm.quoteAssetReserve, amm.terminalQuoteAssetReserve, amm.pegMultiplier, amm.netBaseAssetAmount, markPrice, amm.totalFeeMinusDistributions);
|
|
281
|
+
const [longSpread, shortSpread] = calculateSpreadBN(amm.baseSpread, targetMarkSpreadPct, confIntervalPct, amm.maxSpread, amm.quoteAssetReserve, amm.terminalQuoteAssetReserve, amm.pegMultiplier, amm.netBaseAssetAmount, markPrice, amm.totalFeeMinusDistributions, amm.baseAssetReserve, amm.minBaseAssetReserve, amm.maxBaseAssetReserve);
|
|
244
282
|
let spread;
|
|
245
283
|
if ((0, types_1.isVariant)(direction, 'long')) {
|
|
246
284
|
spread = longSpread;
|
package/lib/math/funding.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="bn.js" />
|
|
2
2
|
import { BN } from '@project-serum/anchor';
|
|
3
|
-
import {
|
|
3
|
+
import { PerpMarketAccount } from '../types';
|
|
4
4
|
import { OraclePriceData } from '../oracles/types';
|
|
5
5
|
/**
|
|
6
6
|
*
|
|
@@ -9,7 +9,7 @@ import { OraclePriceData } from '../oracles/types';
|
|
|
9
9
|
* @param periodAdjustment
|
|
10
10
|
* @returns Estimated funding rate. : Precision //TODO-PRECISION
|
|
11
11
|
*/
|
|
12
|
-
export declare function calculateAllEstimatedFundingRate(market:
|
|
12
|
+
export declare function calculateAllEstimatedFundingRate(market: PerpMarketAccount, oraclePriceData?: OraclePriceData, periodAdjustment?: BN): Promise<[BN, BN, BN, BN, BN]>;
|
|
13
13
|
/**
|
|
14
14
|
*
|
|
15
15
|
* @param market
|
|
@@ -18,7 +18,7 @@ export declare function calculateAllEstimatedFundingRate(market: MarketAccount,
|
|
|
18
18
|
* @param estimationMethod
|
|
19
19
|
* @returns Estimated funding rate. : Precision //TODO-PRECISION
|
|
20
20
|
*/
|
|
21
|
-
export declare function calculateEstimatedFundingRate(market:
|
|
21
|
+
export declare function calculateEstimatedFundingRate(market: PerpMarketAccount, oraclePriceData?: OraclePriceData, periodAdjustment?: BN, estimationMethod?: 'interpolated' | 'lowerbound' | 'capped'): Promise<BN>;
|
|
22
22
|
/**
|
|
23
23
|
*
|
|
24
24
|
* @param market
|
|
@@ -26,7 +26,7 @@ export declare function calculateEstimatedFundingRate(market: MarketAccount, ora
|
|
|
26
26
|
* @param periodAdjustment
|
|
27
27
|
* @returns Estimated funding rate. : Precision //TODO-PRECISION
|
|
28
28
|
*/
|
|
29
|
-
export declare function calculateLongShortFundingRate(market:
|
|
29
|
+
export declare function calculateLongShortFundingRate(market: PerpMarketAccount, oraclePriceData?: OraclePriceData, periodAdjustment?: BN): Promise<[BN, BN]>;
|
|
30
30
|
/**
|
|
31
31
|
*
|
|
32
32
|
* @param market
|
|
@@ -34,10 +34,10 @@ export declare function calculateLongShortFundingRate(market: MarketAccount, ora
|
|
|
34
34
|
* @param periodAdjustment
|
|
35
35
|
* @returns Estimated funding rate. : Precision //TODO-PRECISION
|
|
36
36
|
*/
|
|
37
|
-
export declare function calculateLongShortFundingRateAndLiveTwaps(market:
|
|
37
|
+
export declare function calculateLongShortFundingRateAndLiveTwaps(market: PerpMarketAccount, oraclePriceData?: OraclePriceData, periodAdjustment?: BN): Promise<[BN, BN, BN, BN]>;
|
|
38
38
|
/**
|
|
39
39
|
*
|
|
40
40
|
* @param market
|
|
41
41
|
* @returns Estimated fee pool size
|
|
42
42
|
*/
|
|
43
|
-
export declare function calculateFundingPool(market:
|
|
43
|
+
export declare function calculateFundingPool(market: PerpMarketAccount): BN;
|
package/lib/math/funding.js
CHANGED
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.calculateFundingPool = exports.calculateLongShortFundingRateAndLiveTwaps = exports.calculateLongShortFundingRate = exports.calculateEstimatedFundingRate = exports.calculateAllEstimatedFundingRate = void 0;
|
|
4
4
|
const anchor_1 = require("@project-serum/anchor");
|
|
5
5
|
const numericConstants_1 = require("../constants/numericConstants");
|
|
6
|
+
const types_1 = require("../types");
|
|
6
7
|
const market_1 = require("./market");
|
|
7
8
|
/**
|
|
8
9
|
*
|
|
@@ -19,7 +20,7 @@ async function calculateAllEstimatedFundingRate(market, oraclePriceData, periodA
|
|
|
19
20
|
const secondsInHour = new anchor_1.BN(3600);
|
|
20
21
|
const hoursInDay = new anchor_1.BN(24);
|
|
21
22
|
const ONE = new anchor_1.BN(1);
|
|
22
|
-
if (
|
|
23
|
+
if ((0, types_1.isVariant)(market.status, 'uninitialized')) {
|
|
23
24
|
return [numericConstants_1.ZERO, numericConstants_1.ZERO, numericConstants_1.ZERO, numericConstants_1.ZERO, numericConstants_1.ZERO];
|
|
24
25
|
}
|
|
25
26
|
const payFreq = new anchor_1.BN(market.amm.fundingPeriod);
|
package/lib/math/margin.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
/// <reference types="bn.js" />
|
|
2
2
|
import { BN } from '@project-serum/anchor';
|
|
3
3
|
import { OraclePriceData } from '../oracles/types';
|
|
4
|
-
import {
|
|
4
|
+
import { PerpMarketAccount, PerpPosition } from '..';
|
|
5
5
|
export declare function calculateSizePremiumLiabilityWeight(size: BN, // AMM_RESERVE_PRECISION
|
|
6
6
|
imfFactor: BN, liabilityWeight: BN, precision: BN): BN;
|
|
7
7
|
export declare function calculateSizeDiscountAssetWeight(size: BN, // AMM_RESERVE_PRECISION
|
|
8
8
|
imfFactor: BN, assetWeight: BN): BN;
|
|
9
|
-
export declare function calculateOraclePriceForPerpMargin(
|
|
10
|
-
export declare function calculateBaseAssetValueWithOracle(market:
|
|
11
|
-
export declare function calculateWorstCaseBaseAssetAmount(
|
|
9
|
+
export declare function calculateOraclePriceForPerpMargin(perpPosition: PerpPosition, market: PerpMarketAccount, oraclePriceData: OraclePriceData): BN;
|
|
10
|
+
export declare function calculateBaseAssetValueWithOracle(market: PerpMarketAccount, perpPosition: PerpPosition, oraclePriceData: OraclePriceData): BN;
|
|
11
|
+
export declare function calculateWorstCaseBaseAssetAmount(perpPosition: PerpPosition): BN;
|
package/lib/math/margin.js
CHANGED
|
@@ -4,16 +4,21 @@ exports.calculateWorstCaseBaseAssetAmount = exports.calculateBaseAssetValueWithO
|
|
|
4
4
|
const utils_1 = require("./utils");
|
|
5
5
|
const numericConstants_1 = require("../constants/numericConstants");
|
|
6
6
|
const anchor_1 = require("@project-serum/anchor");
|
|
7
|
+
const assert_1 = require("../assert/assert");
|
|
7
8
|
function calculateSizePremiumLiabilityWeight(size, // AMM_RESERVE_PRECISION
|
|
8
9
|
imfFactor, liabilityWeight, precision) {
|
|
9
10
|
if (imfFactor.eq(numericConstants_1.ZERO)) {
|
|
10
11
|
return liabilityWeight;
|
|
11
12
|
}
|
|
12
13
|
const sizeSqrt = (0, utils_1.squareRootBN)(size.div(new anchor_1.BN(1000)).add(new anchor_1.BN(1))); //1e13 -> 1e10 -> 1e5
|
|
13
|
-
const
|
|
14
|
+
const denom0 = anchor_1.BN.max(new anchor_1.BN(1), numericConstants_1.SPOT_MARKET_IMF_PRECISION.div(imfFactor));
|
|
15
|
+
(0, assert_1.assert)(denom0.gt(numericConstants_1.ZERO));
|
|
16
|
+
const liabilityWeightNumerator = liabilityWeight.sub(liabilityWeight.div(anchor_1.BN.max(new anchor_1.BN(1), numericConstants_1.SPOT_MARKET_IMF_PRECISION.div(imfFactor))));
|
|
17
|
+
const denom = new anchor_1.BN(100000).mul(numericConstants_1.SPOT_MARKET_IMF_PRECISION).div(precision);
|
|
18
|
+
(0, assert_1.assert)(denom.gt(numericConstants_1.ZERO));
|
|
14
19
|
const sizePremiumLiabilityWeight = liabilityWeightNumerator.add(sizeSqrt // 1e5
|
|
15
20
|
.mul(imfFactor)
|
|
16
|
-
.div(
|
|
21
|
+
.div(denom) // 1e5
|
|
17
22
|
);
|
|
18
23
|
const maxLiabilityWeight = anchor_1.BN.max(liabilityWeight, sizePremiumLiabilityWeight);
|
|
19
24
|
return maxLiabilityWeight;
|
|
@@ -25,8 +30,10 @@ imfFactor, assetWeight) {
|
|
|
25
30
|
return assetWeight;
|
|
26
31
|
}
|
|
27
32
|
const sizeSqrt = (0, utils_1.squareRootBN)(size.div(new anchor_1.BN(1000)).add(new anchor_1.BN(1))); //1e13 -> 1e10 -> 1e5
|
|
28
|
-
const imfNumerator = numericConstants_1.
|
|
29
|
-
const sizeDiscountAssetWeight = imfNumerator
|
|
33
|
+
const imfNumerator = numericConstants_1.SPOT_MARKET_IMF_PRECISION.add(numericConstants_1.SPOT_MARKET_IMF_PRECISION.div(new anchor_1.BN(10)));
|
|
34
|
+
const sizeDiscountAssetWeight = imfNumerator
|
|
35
|
+
.mul(numericConstants_1.SPOT_MARKET_WEIGHT_PRECISION)
|
|
36
|
+
.div(numericConstants_1.SPOT_MARKET_IMF_PRECISION.add(sizeSqrt // 1e5
|
|
30
37
|
.mul(imfFactor)
|
|
31
38
|
.div(new anchor_1.BN(100000)) // 1e5
|
|
32
39
|
));
|
|
@@ -34,14 +41,14 @@ imfFactor, assetWeight) {
|
|
|
34
41
|
return minAssetWeight;
|
|
35
42
|
}
|
|
36
43
|
exports.calculateSizeDiscountAssetWeight = calculateSizeDiscountAssetWeight;
|
|
37
|
-
function calculateOraclePriceForPerpMargin(
|
|
44
|
+
function calculateOraclePriceForPerpMargin(perpPosition, market, oraclePriceData) {
|
|
38
45
|
const oraclePriceOffset = anchor_1.BN.min(new anchor_1.BN(market.amm.maxSpread)
|
|
39
46
|
.mul(oraclePriceData.price)
|
|
40
47
|
.div(numericConstants_1.BID_ASK_SPREAD_PRECISION), oraclePriceData.confidence.add(new anchor_1.BN(market.amm.baseSpread)
|
|
41
48
|
.mul(oraclePriceData.price)
|
|
42
49
|
.div(numericConstants_1.BID_ASK_SPREAD_PRECISION)));
|
|
43
50
|
let marginPrice;
|
|
44
|
-
if (
|
|
51
|
+
if (perpPosition.baseAssetAmount.gt(numericConstants_1.ZERO)) {
|
|
45
52
|
marginPrice = oraclePriceData.price.sub(oraclePriceOffset);
|
|
46
53
|
}
|
|
47
54
|
else {
|
|
@@ -50,16 +57,16 @@ function calculateOraclePriceForPerpMargin(marketPosition, market, oraclePriceDa
|
|
|
50
57
|
return marginPrice;
|
|
51
58
|
}
|
|
52
59
|
exports.calculateOraclePriceForPerpMargin = calculateOraclePriceForPerpMargin;
|
|
53
|
-
function calculateBaseAssetValueWithOracle(market,
|
|
54
|
-
return
|
|
60
|
+
function calculateBaseAssetValueWithOracle(market, perpPosition, oraclePriceData) {
|
|
61
|
+
return perpPosition.baseAssetAmount
|
|
55
62
|
.abs()
|
|
56
63
|
.mul(oraclePriceData.price)
|
|
57
64
|
.div(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO.mul(numericConstants_1.MARK_PRICE_PRECISION));
|
|
58
65
|
}
|
|
59
66
|
exports.calculateBaseAssetValueWithOracle = calculateBaseAssetValueWithOracle;
|
|
60
|
-
function calculateWorstCaseBaseAssetAmount(
|
|
61
|
-
const allBids =
|
|
62
|
-
const allAsks =
|
|
67
|
+
function calculateWorstCaseBaseAssetAmount(perpPosition) {
|
|
68
|
+
const allBids = perpPosition.baseAssetAmount.add(perpPosition.openBids);
|
|
69
|
+
const allAsks = perpPosition.baseAssetAmount.add(perpPosition.openAsks);
|
|
63
70
|
if (allBids.abs().gt(allAsks.abs())) {
|
|
64
71
|
return allBids;
|
|
65
72
|
}
|
package/lib/math/market.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="bn.js" />
|
|
2
2
|
import { BN } from '@project-serum/anchor';
|
|
3
|
-
import {
|
|
3
|
+
import { PerpMarketAccount, PositionDirection, MarginCategory, SpotMarketAccount } from '../types';
|
|
4
4
|
import { OraclePriceData } from '../oracles/types';
|
|
5
5
|
/**
|
|
6
6
|
* Calculates market mark price
|
|
@@ -8,24 +8,25 @@ import { OraclePriceData } from '../oracles/types';
|
|
|
8
8
|
* @param market
|
|
9
9
|
* @return markPrice : Precision MARK_PRICE_PRECISION
|
|
10
10
|
*/
|
|
11
|
-
export declare function calculateMarkPrice(market:
|
|
11
|
+
export declare function calculateMarkPrice(market: PerpMarketAccount, oraclePriceData: OraclePriceData): BN;
|
|
12
12
|
/**
|
|
13
13
|
* Calculates market bid price
|
|
14
14
|
*
|
|
15
15
|
* @param market
|
|
16
16
|
* @return bidPrice : Precision MARK_PRICE_PRECISION
|
|
17
17
|
*/
|
|
18
|
-
export declare function calculateBidPrice(market:
|
|
18
|
+
export declare function calculateBidPrice(market: PerpMarketAccount, oraclePriceData: OraclePriceData): BN;
|
|
19
19
|
/**
|
|
20
20
|
* Calculates market ask price
|
|
21
21
|
*
|
|
22
22
|
* @param market
|
|
23
23
|
* @return bidPrice : Precision MARK_PRICE_PRECISION
|
|
24
24
|
*/
|
|
25
|
-
export declare function calculateAskPrice(market:
|
|
26
|
-
export declare function calculateNewMarketAfterTrade(baseAssetAmount: BN, direction: PositionDirection, market:
|
|
27
|
-
export declare function calculateMarkOracleSpread(market:
|
|
25
|
+
export declare function calculateAskPrice(market: PerpMarketAccount, oraclePriceData: OraclePriceData): BN;
|
|
26
|
+
export declare function calculateNewMarketAfterTrade(baseAssetAmount: BN, direction: PositionDirection, market: PerpMarketAccount): PerpMarketAccount;
|
|
27
|
+
export declare function calculateMarkOracleSpread(market: PerpMarketAccount, oraclePriceData: OraclePriceData): BN;
|
|
28
28
|
export declare function calculateOracleSpread(price: BN, oraclePriceData: OraclePriceData): BN;
|
|
29
|
-
export declare function calculateMarketMarginRatio(market:
|
|
30
|
-
export declare function calculateUnrealizedAssetWeight(market:
|
|
31
|
-
export declare function calculateMarketAvailablePNL(
|
|
29
|
+
export declare function calculateMarketMarginRatio(market: PerpMarketAccount, size: BN, marginCategory: MarginCategory): number;
|
|
30
|
+
export declare function calculateUnrealizedAssetWeight(market: PerpMarketAccount, quoteSpotMarket: SpotMarketAccount, unrealizedPnl: BN, marginCategory: MarginCategory, oraclePriceData: OraclePriceData): BN;
|
|
31
|
+
export declare function calculateMarketAvailablePNL(perpMarket: PerpMarketAccount, spotMarket: SpotMarketAccount): BN;
|
|
32
|
+
export declare function calculateNetUserImbalance(market: PerpMarketAccount, bank: SpotMarketAccount, oraclePriceData: OraclePriceData): BN;
|
package/lib/math/market.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.calculateMarketAvailablePNL = exports.calculateUnrealizedAssetWeight = exports.calculateMarketMarginRatio = exports.calculateOracleSpread = exports.calculateMarkOracleSpread = exports.calculateNewMarketAfterTrade = exports.calculateAskPrice = exports.calculateBidPrice = exports.calculateMarkPrice = void 0;
|
|
3
|
+
exports.calculateNetUserImbalance = exports.calculateMarketAvailablePNL = exports.calculateUnrealizedAssetWeight = exports.calculateMarketMarginRatio = exports.calculateOracleSpread = exports.calculateMarkOracleSpread = exports.calculateNewMarketAfterTrade = exports.calculateAskPrice = exports.calculateBidPrice = exports.calculateMarkPrice = void 0;
|
|
4
4
|
const anchor_1 = require("@project-serum/anchor");
|
|
5
5
|
const types_1 = require("../types");
|
|
6
6
|
const amm_1 = require("./amm");
|
|
7
7
|
const margin_1 = require("./margin");
|
|
8
8
|
const numericConstants_1 = require("../constants/numericConstants");
|
|
9
|
-
const
|
|
9
|
+
const spotBalance_1 = require("./spotBalance");
|
|
10
10
|
/**
|
|
11
11
|
* Calculates market mark price
|
|
12
12
|
*
|
|
@@ -72,11 +72,20 @@ function calculateMarketMarginRatio(market, size, marginCategory) {
|
|
|
72
72
|
return marginRatio;
|
|
73
73
|
}
|
|
74
74
|
exports.calculateMarketMarginRatio = calculateMarketMarginRatio;
|
|
75
|
-
function calculateUnrealizedAssetWeight(market, unrealizedPnl, marginCategory) {
|
|
75
|
+
function calculateUnrealizedAssetWeight(market, quoteSpotMarket, unrealizedPnl, marginCategory, oraclePriceData) {
|
|
76
76
|
let assetWeight;
|
|
77
77
|
switch (marginCategory) {
|
|
78
78
|
case 'Initial':
|
|
79
|
-
assetWeight =
|
|
79
|
+
assetWeight = new anchor_1.BN(market.unrealizedInitialAssetWeight);
|
|
80
|
+
if (market.unrealizedMaxImbalance.gt(numericConstants_1.ZERO)) {
|
|
81
|
+
const netUnsettledPnl = calculateNetUserImbalance(market, quoteSpotMarket, oraclePriceData);
|
|
82
|
+
if (netUnsettledPnl.gt(market.unrealizedMaxImbalance)) {
|
|
83
|
+
assetWeight = assetWeight
|
|
84
|
+
.mul(market.unrealizedMaxImbalance)
|
|
85
|
+
.div(netUnsettledPnl);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
assetWeight = (0, margin_1.calculateSizeDiscountAssetWeight)(unrealizedPnl, market.unrealizedImfFactor, assetWeight);
|
|
80
89
|
break;
|
|
81
90
|
case 'Maintenance':
|
|
82
91
|
assetWeight = new anchor_1.BN(market.unrealizedMaintenanceAssetWeight);
|
|
@@ -85,7 +94,21 @@ function calculateUnrealizedAssetWeight(market, unrealizedPnl, marginCategory) {
|
|
|
85
94
|
return assetWeight;
|
|
86
95
|
}
|
|
87
96
|
exports.calculateUnrealizedAssetWeight = calculateUnrealizedAssetWeight;
|
|
88
|
-
function calculateMarketAvailablePNL(
|
|
89
|
-
return (0,
|
|
97
|
+
function calculateMarketAvailablePNL(perpMarket, spotMarket) {
|
|
98
|
+
return (0, spotBalance_1.getTokenAmount)(perpMarket.pnlPool.balance, spotMarket, types_1.SpotBalanceType.DEPOSIT);
|
|
90
99
|
}
|
|
91
100
|
exports.calculateMarketAvailablePNL = calculateMarketAvailablePNL;
|
|
101
|
+
function calculateNetUserImbalance(market, bank, oraclePriceData) {
|
|
102
|
+
const netUserPositionValue = market.amm.netBaseAssetAmount
|
|
103
|
+
.mul(oraclePriceData.price)
|
|
104
|
+
.div(numericConstants_1.BASE_PRECISION)
|
|
105
|
+
.div(numericConstants_1.PRICE_TO_QUOTE_PRECISION);
|
|
106
|
+
const netUserCostBasis = market.amm.quoteAssetAmountLong
|
|
107
|
+
.add(market.amm.quoteAssetAmountShort)
|
|
108
|
+
.sub(market.amm.cumulativeSocialLoss);
|
|
109
|
+
const userEntitledPnl = netUserPositionValue.add(netUserCostBasis);
|
|
110
|
+
const pnlPool = (0, spotBalance_1.getTokenAmount)(market.pnlPool.balance, bank, types_1.SpotBalanceType.DEPOSIT);
|
|
111
|
+
const imbalance = userEntitledPnl.sub(pnlPool);
|
|
112
|
+
return imbalance;
|
|
113
|
+
}
|
|
114
|
+
exports.calculateNetUserImbalance = calculateNetUserImbalance;
|
package/lib/math/oracles.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/// <reference types="bn.js" />
|
|
2
2
|
import { AMM, OracleGuardRails } from '../types';
|
|
3
3
|
import { OraclePriceData } from '../oracles/types';
|
|
4
|
-
import { BN } from '../index';
|
|
4
|
+
import { BN, PerpMarketAccount } from '../index';
|
|
5
|
+
export declare function oraclePriceBands(market: PerpMarketAccount, oraclePriceData: OraclePriceData): [BN, BN];
|
|
5
6
|
export declare function isOracleValid(amm: AMM, oraclePriceData: OraclePriceData, oracleGuardRails: OracleGuardRails, slot: number): boolean;
|
|
6
7
|
export declare function isOracleTooDivergent(amm: AMM, oraclePriceData: OraclePriceData, oracleGuardRails: OracleGuardRails, now: BN): boolean;
|
package/lib/math/oracles.js
CHANGED
|
@@ -1,8 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isOracleTooDivergent = exports.isOracleValid = void 0;
|
|
3
|
+
exports.isOracleTooDivergent = exports.isOracleValid = exports.oraclePriceBands = void 0;
|
|
4
4
|
const numericConstants_1 = require("../constants/numericConstants");
|
|
5
5
|
const index_1 = require("../index");
|
|
6
|
+
const assert_1 = require("../assert/assert");
|
|
7
|
+
function oraclePriceBands(market, oraclePriceData) {
|
|
8
|
+
const maxPercentDiff = market.marginRatioInitial - market.marginRatioMaintenance;
|
|
9
|
+
const offset = oraclePriceData.price
|
|
10
|
+
.mul(new index_1.BN(maxPercentDiff))
|
|
11
|
+
.div(numericConstants_1.MARGIN_PRECISION);
|
|
12
|
+
(0, assert_1.assert)(offset.gt(numericConstants_1.ZERO));
|
|
13
|
+
return [oraclePriceData.price.sub(offset), oraclePriceData.price.add(offset)];
|
|
14
|
+
}
|
|
15
|
+
exports.oraclePriceBands = oraclePriceBands;
|
|
6
16
|
function isOracleValid(amm, oraclePriceData, oracleGuardRails, slot) {
|
|
7
17
|
const isOraclePriceNonPositive = oraclePriceData.price.lte(numericConstants_1.ZERO);
|
|
8
18
|
const isOraclePriceTooVolatile = oraclePriceData.price
|
package/lib/math/orders.d.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
/// <reference types="bn.js" />
|
|
2
2
|
import { ClearingHouseUser } from '../clearingHouseUser';
|
|
3
|
-
import {
|
|
3
|
+
import { PerpMarketAccount, Order } from '../types';
|
|
4
4
|
import { BN } from '@project-serum/anchor';
|
|
5
5
|
import { OraclePriceData } from '../oracles/types';
|
|
6
6
|
export declare function isOrderRiskIncreasing(user: ClearingHouseUser, order: Order): boolean;
|
|
7
7
|
export declare function isOrderRiskIncreasingInSameDirection(user: ClearingHouseUser, order: Order): boolean;
|
|
8
8
|
export declare function isOrderReduceOnly(user: ClearingHouseUser, order: Order): boolean;
|
|
9
9
|
export declare function standardizeBaseAssetAmount(baseAssetAmount: BN, stepSize: BN): BN;
|
|
10
|
-
export declare function getLimitPrice(order: Order, market:
|
|
11
|
-
export declare function isFillableByVAMM(order: Order, market:
|
|
12
|
-
export declare function calculateBaseAssetAmountForAmmToFulfill(order: Order, market:
|
|
13
|
-
export declare function calculateBaseAssetAmountToFillUpToLimitPrice(order: Order, market:
|
|
10
|
+
export declare function getLimitPrice(order: Order, market: PerpMarketAccount, oraclePriceData: OraclePriceData, slot: number): BN;
|
|
11
|
+
export declare function isFillableByVAMM(order: Order, market: PerpMarketAccount, oraclePriceData: OraclePriceData, slot: number, maxAuctionDuration: number): boolean;
|
|
12
|
+
export declare function calculateBaseAssetAmountForAmmToFulfill(order: Order, market: PerpMarketAccount, oraclePriceData: OraclePriceData, slot: number): BN;
|
|
13
|
+
export declare function calculateBaseAssetAmountToFillUpToLimitPrice(order: Order, market: PerpMarketAccount, limitPrice: BN, oraclePriceData: OraclePriceData): BN;
|
|
14
14
|
export declare function isOrderExpired(order: Order, slot: number, maxAuctionDuration: number): boolean;
|
package/lib/math/position.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/// <reference types="bn.js" />
|
|
2
2
|
import { BN } from '../';
|
|
3
3
|
import { OraclePriceData } from '../oracles/types';
|
|
4
|
-
import {
|
|
4
|
+
import { PerpMarketAccount, PositionDirection, PerpPosition } from '../types';
|
|
5
5
|
/**
|
|
6
6
|
* calculateBaseAssetValue
|
|
7
7
|
* = market value of closing entire position
|
|
@@ -10,38 +10,38 @@ import { MarketAccount, PositionDirection, UserPosition } from '../types';
|
|
|
10
10
|
* @param oraclePriceData
|
|
11
11
|
* @returns Base Asset Value. : Precision QUOTE_PRECISION
|
|
12
12
|
*/
|
|
13
|
-
export declare function calculateBaseAssetValue(market:
|
|
13
|
+
export declare function calculateBaseAssetValue(market: PerpMarketAccount, userPosition: PerpPosition, oraclePriceData: OraclePriceData): BN;
|
|
14
14
|
/**
|
|
15
15
|
* calculatePositionPNL
|
|
16
16
|
* = BaseAssetAmount * (Avg Exit Price - Avg Entry Price)
|
|
17
17
|
* @param market
|
|
18
|
-
* @param
|
|
18
|
+
* @param PerpPosition
|
|
19
19
|
* @param withFunding (adds unrealized funding payment pnl to result)
|
|
20
20
|
* @param oraclePriceData
|
|
21
21
|
* @returns BaseAssetAmount : Precision QUOTE_PRECISION
|
|
22
22
|
*/
|
|
23
|
-
export declare function calculatePositionPNL(market:
|
|
24
|
-
export declare function calculateUnsettledPnl(market:
|
|
23
|
+
export declare function calculatePositionPNL(market: PerpMarketAccount, perpPosition: PerpPosition, withFunding: boolean, oraclePriceData: OraclePriceData): BN;
|
|
24
|
+
export declare function calculateUnsettledPnl(market: PerpMarketAccount, perpPosition: PerpPosition, oraclePriceData: OraclePriceData): BN;
|
|
25
25
|
/**
|
|
26
26
|
*
|
|
27
27
|
* @param market
|
|
28
|
-
* @param
|
|
28
|
+
* @param PerpPosition
|
|
29
29
|
* @returns // TODO-PRECISION
|
|
30
30
|
*/
|
|
31
|
-
export declare function calculatePositionFundingPNL(market:
|
|
32
|
-
export declare function positionIsAvailable(position:
|
|
31
|
+
export declare function calculatePositionFundingPNL(market: PerpMarketAccount, perpPosition: PerpPosition): BN;
|
|
32
|
+
export declare function positionIsAvailable(position: PerpPosition): boolean;
|
|
33
33
|
/**
|
|
34
34
|
*
|
|
35
35
|
* @param userPosition
|
|
36
36
|
* @returns Precision: MARK_PRICE_PRECISION (10^10)
|
|
37
37
|
*/
|
|
38
|
-
export declare function calculateEntryPrice(userPosition:
|
|
38
|
+
export declare function calculateEntryPrice(userPosition: PerpPosition): BN;
|
|
39
39
|
/**
|
|
40
40
|
*
|
|
41
41
|
* @param userPosition
|
|
42
42
|
* @returns Precision: MARK_PRICE_PRECISION (10^10)
|
|
43
43
|
*/
|
|
44
|
-
export declare function calculateCostBasis(userPosition:
|
|
45
|
-
export declare function findDirectionToClose(userPosition:
|
|
46
|
-
export declare function positionCurrentDirection(userPosition:
|
|
47
|
-
export declare function isEmptyPosition(userPosition:
|
|
44
|
+
export declare function calculateCostBasis(userPosition: PerpPosition): BN;
|
|
45
|
+
export declare function findDirectionToClose(userPosition: PerpPosition): PositionDirection;
|
|
46
|
+
export declare function positionCurrentDirection(userPosition: PerpPosition): PositionDirection;
|
|
47
|
+
export declare function isEmptyPosition(userPosition: PerpPosition): boolean;
|