@drift-labs/sdk 0.2.0-master.25 → 0.2.0-master.26
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +13 -13
- package/lib/accounts/pollingClearingHouseAccountSubscriber.js +27 -27
- package/lib/accounts/types.d.ts +8 -9
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +14 -14
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +35 -34
- package/lib/addresses/pda.d.ts +7 -6
- package/lib/addresses/pda.js +31 -27
- package/lib/admin.d.ts +9 -6
- package/lib/admin.js +83 -42
- package/lib/clearingHouse.d.ts +69 -42
- package/lib/clearingHouse.js +753 -277
- package/lib/clearingHouseConfig.d.ts +2 -2
- package/lib/clearingHouseUser.d.ts +16 -16
- package/lib/clearingHouseUser.js +139 -119
- package/lib/config.d.ts +7 -7
- package/lib/config.js +20 -20
- package/lib/constants/numericConstants.d.ts +12 -12
- package/lib/constants/numericConstants.js +13 -13
- package/lib/constants/{markets.d.ts → perpMarkets.d.ts} +5 -5
- package/{src/constants/markets.js → lib/constants/perpMarkets.js} +4 -4
- package/lib/constants/{banks.d.ts → spotMarkets.d.ts} +6 -6
- package/lib/constants/{banks.js → spotMarkets.js} +16 -16
- package/lib/events/types.d.ts +2 -1
- package/lib/events/types.js +1 -0
- package/lib/examples/makeTradeExample.js +7 -7
- package/lib/idl/clearing_house.json +1008 -279
- package/lib/index.d.ts +5 -3
- package/lib/index.js +5 -3
- package/lib/math/amm.d.ts +2 -2
- package/lib/math/amm.js +1 -1
- package/lib/math/funding.d.ts +6 -6
- package/lib/math/funding.js +2 -1
- package/lib/math/margin.d.ts +4 -4
- package/lib/math/margin.js +18 -11
- package/lib/math/market.d.ts +10 -9
- package/lib/math/market.js +29 -6
- package/lib/math/oracles.d.ts +2 -1
- package/lib/math/oracles.js +11 -1
- package/lib/math/orders.d.ts +5 -5
- package/lib/math/position.d.ts +13 -13
- package/lib/math/position.js +19 -19
- package/lib/math/spotBalance.d.ts +19 -0
- package/lib/math/spotBalance.js +176 -0
- package/lib/math/spotMarket.d.ts +4 -0
- package/lib/math/spotMarket.js +8 -0
- package/lib/math/spotPosition.d.ts +2 -0
- package/lib/math/spotPosition.js +8 -0
- package/lib/math/state.js +2 -2
- package/lib/math/trade.d.ts +4 -4
- package/lib/orderParams.d.ts +4 -4
- package/lib/orderParams.js +12 -4
- package/lib/serum/serumSubscriber.d.ts +23 -0
- package/lib/serum/serumSubscriber.js +41 -0
- package/lib/serum/types.d.ts +11 -0
- package/lib/serum/types.js +2 -0
- package/lib/tx/retryTxSender.d.ts +1 -1
- package/lib/tx/retryTxSender.js +4 -2
- package/lib/tx/types.d.ts +1 -1
- package/lib/types.d.ts +123 -33
- package/lib/types.js +31 -9
- package/my-script/.env +7 -0
- package/my-script/getUserStats.ts +106 -0
- package/my-script/multiConnections.ts +119 -0
- package/my-script/test-regex.ts +11 -0
- package/my-script/utils.ts +52 -0
- package/package.json +1 -1
- package/src/accounts/bulkAccountLoader.js +249 -0
- package/src/accounts/bulkUserStatsSubscription.js +75 -0
- package/src/accounts/bulkUserSubscription.js +75 -0
- package/src/accounts/fetch.js +92 -0
- package/src/accounts/pollingClearingHouseAccountSubscriber.js +465 -0
- package/src/accounts/pollingClearingHouseAccountSubscriber.ts +38 -38
- package/src/accounts/pollingOracleSubscriber.js +156 -0
- package/src/accounts/pollingTokenAccountSubscriber.js +141 -0
- package/src/accounts/pollingUserAccountSubscriber.js +208 -0
- package/src/accounts/pollingUserStatsAccountSubscriber.js +208 -0
- package/src/accounts/types.js +28 -0
- package/src/accounts/types.ts +11 -9
- package/src/accounts/utils.js +7 -0
- package/src/accounts/webSocketAccountSubscriber.js +138 -0
- package/src/accounts/webSocketClearingHouseAccountSubscriber.js +433 -0
- package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +59 -52
- package/src/accounts/webSocketUserAccountSubscriber.js +113 -0
- package/src/accounts/webSocketUserStatsAccountSubsriber.js +113 -0
- package/src/addresses/pda.js +186 -0
- package/src/addresses/pda.ts +49 -44
- package/src/admin.js +1284 -0
- package/src/admin.ts +140 -47
- package/src/assert/assert.js +1 -1
- package/src/clearingHouse.js +3433 -0
- package/src/clearingHouse.ts +1083 -378
- package/src/clearingHouseConfig.js +2 -0
- package/src/clearingHouseConfig.ts +2 -2
- package/src/clearingHouseUser.js +874 -0
- package/src/clearingHouseUser.ts +232 -168
- package/src/clearingHouseUserConfig.js +2 -0
- package/src/clearingHouseUserStats.js +115 -0
- package/src/clearingHouseUserStatsConfig.js +2 -0
- package/src/config.js +80 -0
- package/src/config.ts +29 -29
- package/src/constants/numericConstants.js +18 -11
- package/src/constants/numericConstants.ts +17 -15
- package/{lib/constants/markets.js → src/constants/perpMarkets.js} +11 -11
- package/src/constants/{markets.ts → perpMarkets.ts} +5 -5
- package/src/constants/spotMarkets.js +51 -0
- package/src/constants/{banks.ts → spotMarkets.ts} +19 -19
- package/src/events/eventList.js +66 -23
- package/src/events/eventSubscriber.js +202 -0
- package/src/events/fetchLogs.js +117 -0
- package/src/events/pollingLogProvider.js +113 -0
- package/src/events/sort.js +41 -0
- package/src/events/txEventCache.js +22 -19
- package/src/events/types.js +25 -0
- package/src/events/types.ts +3 -0
- package/src/events/webSocketLogProvider.js +76 -0
- package/src/examples/makeTradeExample.ts +10 -8
- package/src/factory/bigNum.js +183 -180
- package/src/factory/oracleClient.js +9 -9
- package/src/idl/clearing_house.json +1008 -279
- package/src/index.js +75 -0
- package/src/index.ts +5 -3
- package/src/math/amm.js +422 -0
- package/src/math/amm.ts +6 -3
- package/src/math/auction.js +10 -10
- package/src/math/conversion.js +4 -3
- package/src/math/funding.js +223 -175
- package/src/math/funding.ts +7 -7
- package/src/math/insurance.js +27 -0
- package/src/math/margin.js +77 -0
- package/src/math/margin.ts +34 -23
- package/src/math/market.js +105 -0
- package/src/math/market.ts +71 -19
- package/src/math/oracles.js +40 -10
- package/src/math/oracles.ts +18 -1
- package/src/math/orders.js +153 -0
- package/src/math/orders.ts +5 -5
- package/src/math/position.js +172 -0
- package/src/math/position.ts +31 -31
- package/src/math/repeg.js +40 -40
- package/src/math/spotBalance.js +176 -0
- package/src/math/spotBalance.ts +290 -0
- package/src/math/spotMarket.js +8 -0
- package/src/math/spotMarket.ts +9 -0
- package/src/math/spotPosition.js +8 -0
- package/src/math/spotPosition.ts +6 -0
- package/src/math/state.ts +2 -2
- package/src/math/trade.js +81 -74
- package/src/math/trade.ts +4 -4
- package/src/math/utils.js +8 -7
- package/src/oracles/oracleClientCache.js +10 -9
- package/src/oracles/pythClient.js +52 -17
- package/src/oracles/quoteAssetOracleClient.js +44 -13
- package/src/oracles/switchboardClient.js +69 -37
- package/src/oracles/types.js +1 -1
- package/src/orderParams.js +14 -6
- package/src/orderParams.ts +16 -8
- package/src/serum/serumSubscriber.js +102 -0
- package/src/serum/serumSubscriber.ts +80 -0
- package/src/serum/types.js +2 -0
- package/src/serum/types.ts +13 -0
- package/src/slot/SlotSubscriber.js +67 -20
- package/src/token/index.js +4 -4
- package/src/tokenFaucet.js +288 -154
- package/src/tx/retryTxSender.js +280 -0
- package/src/tx/retryTxSender.ts +5 -2
- package/src/tx/types.js +1 -1
- package/src/tx/types.ts +2 -1
- package/src/tx/utils.js +7 -6
- package/src/types.js +216 -0
- package/src/types.ts +110 -33
- package/src/userName.js +5 -5
- package/src/util/computeUnits.js +46 -11
- package/src/util/promiseTimeout.js +5 -5
- package/src/util/tps.js +46 -12
- package/src/wallet.js +55 -18
- package/lib/math/bankBalance.d.ts +0 -15
- package/lib/math/bankBalance.js +0 -150
- package/src/addresses/marketAddresses.js +0 -26
- package/src/constants/banks.js +0 -42
- package/src/examples/makeTradeExample.js +0 -80
- package/src/math/bankBalance.ts +0 -258
- package/src/math/state.js +0 -15
- package/src/math/utils.js.map +0 -1
- package/src/util/getTokenAddress.js +0 -9
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';
|
|
@@ -31,6 +31,7 @@ export * from './events/types';
|
|
|
31
31
|
export * from './events/eventSubscriber';
|
|
32
32
|
export * from './events/fetchLogs';
|
|
33
33
|
export * from './math/auction';
|
|
34
|
+
export * from './math/spotMarket';
|
|
34
35
|
export * from './math/conversion';
|
|
35
36
|
export * from './math/funding';
|
|
36
37
|
export * from './math/market';
|
|
@@ -49,10 +50,11 @@ export * from './types';
|
|
|
49
50
|
export * from './math/utils';
|
|
50
51
|
export * from './config';
|
|
51
52
|
export * from './constants/numericConstants';
|
|
53
|
+
export * from './serum/serumSubscriber';
|
|
52
54
|
export * from './tx/retryTxSender';
|
|
53
55
|
export * from './util/computeUnits';
|
|
54
56
|
export * from './util/tps';
|
|
55
|
-
export * from './math/
|
|
56
|
-
export * from './constants/
|
|
57
|
+
export * from './math/spotBalance';
|
|
58
|
+
export * from './constants/spotMarkets';
|
|
57
59
|
export * from './clearingHouseConfig';
|
|
58
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);
|
|
@@ -54,6 +54,7 @@ __exportStar(require("./events/types"), exports);
|
|
|
54
54
|
__exportStar(require("./events/eventSubscriber"), exports);
|
|
55
55
|
__exportStar(require("./events/fetchLogs"), exports);
|
|
56
56
|
__exportStar(require("./math/auction"), exports);
|
|
57
|
+
__exportStar(require("./math/spotMarket"), exports);
|
|
57
58
|
__exportStar(require("./math/conversion"), exports);
|
|
58
59
|
__exportStar(require("./math/funding"), exports);
|
|
59
60
|
__exportStar(require("./math/market"), exports);
|
|
@@ -72,9 +73,10 @@ __exportStar(require("./types"), exports);
|
|
|
72
73
|
__exportStar(require("./math/utils"), exports);
|
|
73
74
|
__exportStar(require("./config"), exports);
|
|
74
75
|
__exportStar(require("./constants/numericConstants"), exports);
|
|
76
|
+
__exportStar(require("./serum/serumSubscriber"), exports);
|
|
75
77
|
__exportStar(require("./tx/retryTxSender"), exports);
|
|
76
78
|
__exportStar(require("./util/computeUnits"), exports);
|
|
77
79
|
__exportStar(require("./util/tps"), exports);
|
|
78
|
-
__exportStar(require("./math/
|
|
79
|
-
__exportStar(require("./constants/
|
|
80
|
+
__exportStar(require("./math/spotBalance"), exports);
|
|
81
|
+
__exportStar(require("./constants/spotMarkets"), exports);
|
|
80
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];
|
|
@@ -66,7 +66,7 @@ export declare function getSwapDirection(inputAssetType: AssetType, positionDire
|
|
|
66
66
|
* @param market
|
|
67
67
|
* @returns cost : Precision MARK_PRICE_PRECISION
|
|
68
68
|
*/
|
|
69
|
-
export declare function calculateTerminalPrice(market:
|
|
69
|
+
export declare function calculateTerminalPrice(market: PerpMarketAccount): BN;
|
|
70
70
|
export declare function calculateMaxBaseAssetAmountToTrade(amm: AMM, limit_price: BN, direction: PositionDirection, oraclePriceData?: OraclePriceData): [BN, PositionDirection];
|
|
71
71
|
export declare function calculateQuoteAssetAmountSwapped(quoteAssetReserves: BN, pegMultiplier: BN, swapDirection: SwapDirection): BN;
|
|
72
72
|
export declare function calculateMaxBaseAssetAmountFillable(amm: AMM, orderDirection: PositionDirection): BN;
|
package/lib/math/amm.js
CHANGED
|
@@ -226,7 +226,7 @@ function calculateSpreadBN(baseSpread, lastOracleMarkSpreadPct, lastOracleConfPc
|
|
|
226
226
|
else if (lastOracleMarkSpreadPct.lt(numericConstants_1.ZERO)) {
|
|
227
227
|
longSpread = Math.max(longSpread, lastOracleMarkSpreadPct.abs().toNumber() + lastOracleConfPct.toNumber());
|
|
228
228
|
}
|
|
229
|
-
const maxTargetSpread = maxSpread;
|
|
229
|
+
const maxTargetSpread = Math.max(maxSpread, lastOracleMarkSpreadPct.abs().toNumber());
|
|
230
230
|
const MAX_INVENTORY_SKEW = 5;
|
|
231
231
|
const inventoryScale = calculateInventoryScale(netBaseAssetAmount, baseAssetReserve, minBaseAssetReserve, maxBaseAssetReserve);
|
|
232
232
|
const inventorySpreadScale = Math.min(MAX_INVENTORY_SKEW, 1 + inventoryScale);
|
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;
|
package/lib/math/position.js
CHANGED
|
@@ -52,36 +52,36 @@ exports.calculateBaseAssetValue = calculateBaseAssetValue;
|
|
|
52
52
|
* calculatePositionPNL
|
|
53
53
|
* = BaseAssetAmount * (Avg Exit Price - Avg Entry Price)
|
|
54
54
|
* @param market
|
|
55
|
-
* @param
|
|
55
|
+
* @param PerpPosition
|
|
56
56
|
* @param withFunding (adds unrealized funding payment pnl to result)
|
|
57
57
|
* @param oraclePriceData
|
|
58
58
|
* @returns BaseAssetAmount : Precision QUOTE_PRECISION
|
|
59
59
|
*/
|
|
60
|
-
function calculatePositionPNL(market,
|
|
61
|
-
if (
|
|
62
|
-
return
|
|
60
|
+
function calculatePositionPNL(market, perpPosition, withFunding = false, oraclePriceData) {
|
|
61
|
+
if (perpPosition.baseAssetAmount.eq(numericConstants_1.ZERO)) {
|
|
62
|
+
return perpPosition.quoteAssetAmount;
|
|
63
63
|
}
|
|
64
|
-
const baseAssetValue = (0, margin_1.calculateBaseAssetValueWithOracle)(market,
|
|
65
|
-
const baseAssetValueSign =
|
|
64
|
+
const baseAssetValue = (0, margin_1.calculateBaseAssetValueWithOracle)(market, perpPosition, oraclePriceData);
|
|
65
|
+
const baseAssetValueSign = perpPosition.baseAssetAmount.isNeg()
|
|
66
66
|
? new __1.BN(-1)
|
|
67
67
|
: new __1.BN(1);
|
|
68
68
|
let pnl = baseAssetValue
|
|
69
69
|
.mul(baseAssetValueSign)
|
|
70
|
-
.add(
|
|
70
|
+
.add(perpPosition.quoteAssetAmount);
|
|
71
71
|
if (withFunding) {
|
|
72
|
-
const fundingRatePnL = calculatePositionFundingPNL(market,
|
|
72
|
+
const fundingRatePnL = calculatePositionFundingPNL(market, perpPosition).div(numericConstants_1.PRICE_TO_QUOTE_PRECISION);
|
|
73
73
|
pnl = pnl.add(fundingRatePnL);
|
|
74
74
|
}
|
|
75
75
|
return pnl;
|
|
76
76
|
}
|
|
77
77
|
exports.calculatePositionPNL = calculatePositionPNL;
|
|
78
|
-
function calculateUnsettledPnl(market,
|
|
79
|
-
const unrealizedPnl = calculatePositionPNL(market,
|
|
78
|
+
function calculateUnsettledPnl(market, perpPosition, oraclePriceData) {
|
|
79
|
+
const unrealizedPnl = calculatePositionPNL(market, perpPosition, true, oraclePriceData);
|
|
80
80
|
let unsettledPnl = unrealizedPnl;
|
|
81
81
|
if (unrealizedPnl.gt(numericConstants_1.ZERO)) {
|
|
82
|
-
const fundingPnL = calculatePositionFundingPNL(market,
|
|
83
|
-
const maxPositivePnl = __1.BN.max(
|
|
84
|
-
.sub(
|
|
82
|
+
const fundingPnL = calculatePositionFundingPNL(market, perpPosition).div(numericConstants_1.PRICE_TO_QUOTE_PRECISION);
|
|
83
|
+
const maxPositivePnl = __1.BN.max(perpPosition.quoteAssetAmount
|
|
84
|
+
.sub(perpPosition.quoteEntryAmount)
|
|
85
85
|
.add(fundingPnL), numericConstants_1.ZERO);
|
|
86
86
|
unsettledPnl = __1.BN.min(maxPositivePnl, unrealizedPnl);
|
|
87
87
|
}
|
|
@@ -91,23 +91,23 @@ exports.calculateUnsettledPnl = calculateUnsettledPnl;
|
|
|
91
91
|
/**
|
|
92
92
|
*
|
|
93
93
|
* @param market
|
|
94
|
-
* @param
|
|
94
|
+
* @param PerpPosition
|
|
95
95
|
* @returns // TODO-PRECISION
|
|
96
96
|
*/
|
|
97
|
-
function calculatePositionFundingPNL(market,
|
|
98
|
-
if (
|
|
97
|
+
function calculatePositionFundingPNL(market, perpPosition) {
|
|
98
|
+
if (perpPosition.baseAssetAmount.eq(numericConstants_1.ZERO)) {
|
|
99
99
|
return numericConstants_1.ZERO;
|
|
100
100
|
}
|
|
101
101
|
let ammCumulativeFundingRate;
|
|
102
|
-
if (
|
|
102
|
+
if (perpPosition.baseAssetAmount.gt(numericConstants_1.ZERO)) {
|
|
103
103
|
ammCumulativeFundingRate = market.amm.cumulativeFundingRateLong;
|
|
104
104
|
}
|
|
105
105
|
else {
|
|
106
106
|
ammCumulativeFundingRate = market.amm.cumulativeFundingRateShort;
|
|
107
107
|
}
|
|
108
108
|
const perPositionFundingRate = ammCumulativeFundingRate
|
|
109
|
-
.sub(
|
|
110
|
-
.mul(
|
|
109
|
+
.sub(perpPosition.lastCumulativeFundingRate)
|
|
110
|
+
.mul(perpPosition.baseAssetAmount)
|
|
111
111
|
.div(numericConstants_1.AMM_RESERVE_PRECISION)
|
|
112
112
|
.div(numericConstants_1.FUNDING_PAYMENT_PRECISION)
|
|
113
113
|
.mul(new __1.BN(-1));
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/// <reference types="bn.js" />
|
|
2
|
+
import { SpotMarketAccount, SpotBalanceType, MarginCategory } from '../types';
|
|
3
|
+
import { BN } from '@project-serum/anchor';
|
|
4
|
+
export declare function getBalance(tokenAmount: BN, spotMarket: SpotMarketAccount, balanceType: SpotBalanceType): BN;
|
|
5
|
+
export declare function getTokenAmount(balanceAmount: BN, spotMarket: SpotMarketAccount, balanceType: SpotBalanceType): BN;
|
|
6
|
+
export declare function calculateAssetWeight(balanceAmount: BN, spotMarket: SpotMarketAccount, marginCategory: MarginCategory): BN;
|
|
7
|
+
export declare function calculateLiabilityWeight(balanceAmount: BN, spotMarket: SpotMarketAccount, marginCategory: MarginCategory): BN;
|
|
8
|
+
export declare function calculateUtilization(bank: SpotMarketAccount): BN;
|
|
9
|
+
export declare function calculateInterestRate(bank: SpotMarketAccount): BN;
|
|
10
|
+
export declare function calculateDepositRate(bank: SpotMarketAccount): BN;
|
|
11
|
+
export declare function calculateBorrowRate(bank: SpotMarketAccount): BN;
|
|
12
|
+
export declare function calculateInterestAccumulated(bank: SpotMarketAccount, now: BN): {
|
|
13
|
+
borrowInterest: BN;
|
|
14
|
+
depositInterest: BN;
|
|
15
|
+
};
|
|
16
|
+
export declare function calculateWithdrawLimit(spotMarket: SpotMarketAccount, now: BN): {
|
|
17
|
+
borrowLimit: BN;
|
|
18
|
+
withdrawLimit: BN;
|
|
19
|
+
};
|