@drift-labs/sdk 0.1.36-master.7 → 0.2.0-master.10
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 +6 -6
- package/lib/accounts/bulkAccountLoader.js +81 -95
- package/lib/accounts/bulkUserSubscription.js +13 -57
- package/lib/accounts/fetch.d.ts +4 -0
- package/lib/accounts/fetch.js +18 -0
- package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +34 -38
- package/lib/accounts/pollingClearingHouseAccountSubscriber.js +224 -224
- package/lib/accounts/pollingOracleSubscriber.d.ts +3 -3
- package/lib/accounts/pollingOracleSubscriber.js +37 -49
- package/lib/accounts/pollingTokenAccountSubscriber.d.ts +3 -3
- package/lib/accounts/pollingTokenAccountSubscriber.js +35 -50
- package/lib/accounts/pollingUserAccountSubscriber.d.ts +7 -13
- package/lib/accounts/pollingUserAccountSubscriber.js +71 -134
- package/lib/accounts/types.d.ts +34 -41
- package/lib/accounts/webSocketAccountSubscriber.d.ts +6 -4
- package/lib/accounts/webSocketAccountSubscriber.js +39 -35
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +33 -28
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +153 -185
- package/lib/accounts/webSocketUserAccountSubscriber.d.ts +5 -11
- package/lib/accounts/webSocketUserAccountSubscriber.js +22 -67
- package/lib/addresses/marketAddresses.d.ts +4 -0
- package/lib/addresses/marketAddresses.js +15 -0
- package/lib/addresses/pda.d.ts +12 -0
- package/lib/addresses/pda.js +83 -0
- package/lib/admin.d.ts +12 -18
- package/lib/admin.js +398 -558
- package/lib/clearingHouse.d.ts +83 -109
- package/lib/clearingHouse.js +832 -893
- package/lib/clearingHouseConfig.d.ts +34 -0
- package/lib/clearingHouseConfig.js +2 -0
- package/lib/clearingHouseUser.d.ts +19 -22
- package/lib/clearingHouseUser.js +155 -141
- package/lib/clearingHouseUserConfig.d.ts +14 -0
- package/lib/clearingHouseUserConfig.js +2 -0
- package/lib/config.d.ts +12 -0
- package/lib/config.js +35 -4
- package/lib/constants/banks.d.ts +16 -0
- package/lib/constants/banks.js +41 -0
- package/lib/constants/markets.d.ts +8 -3
- package/lib/constants/markets.js +13 -206
- package/lib/constants/numericConstants.d.ts +17 -0
- package/lib/constants/numericConstants.js +23 -6
- package/lib/events/eventList.d.ts +22 -0
- package/lib/events/eventList.js +77 -0
- package/lib/events/eventSubscriber.d.ts +34 -0
- package/lib/events/eventSubscriber.js +126 -0
- package/lib/events/fetchLogs.d.ts +13 -0
- package/lib/events/fetchLogs.js +39 -0
- package/lib/events/pollingLogProvider.d.ts +15 -0
- package/lib/events/pollingLogProvider.js +53 -0
- package/lib/events/sort.d.ts +2 -0
- package/lib/events/sort.js +44 -0
- package/lib/events/txEventCache.d.ts +24 -0
- package/lib/events/txEventCache.js +71 -0
- package/lib/events/types.d.ts +49 -0
- package/lib/events/types.js +20 -0
- package/lib/events/webSocketLogProvider.d.ts +12 -0
- package/lib/events/webSocketLogProvider.js +30 -0
- package/lib/examples/makeTradeExample.js +26 -27
- package/lib/factory/bigNum.d.ts +118 -0
- package/lib/factory/bigNum.js +364 -0
- package/lib/factory/oracleClient.d.ts +1 -2
- package/lib/factory/oracleClient.js +6 -2
- package/lib/idl/clearing_house.json +2258 -2774
- package/lib/index.d.ts +14 -5
- package/lib/index.js +18 -5
- package/lib/math/amm.d.ts +24 -29
- package/lib/math/amm.js +209 -176
- package/lib/math/auction.d.ts +5 -0
- package/lib/math/auction.js +42 -0
- package/lib/math/bankBalance.d.ts +9 -0
- package/lib/math/bankBalance.js +75 -0
- package/lib/math/conversion.d.ts +0 -1
- package/lib/math/conversion.js +1 -5
- package/lib/math/funding.d.ts +6 -6
- package/lib/math/funding.js +158 -175
- package/lib/math/market.d.ts +6 -6
- package/lib/math/market.js +10 -9
- package/lib/math/orders.d.ts +6 -1
- package/lib/math/orders.js +38 -1
- package/lib/math/position.d.ts +7 -5
- package/lib/math/position.js +29 -27
- package/lib/math/repeg.d.ts +22 -0
- package/lib/math/repeg.js +128 -0
- package/lib/math/trade.d.ts +5 -4
- package/lib/math/trade.js +29 -24
- package/lib/mockUSDCFaucet.js +87 -116
- package/lib/oracles/oracleClientCache.d.ts +8 -0
- package/lib/oracles/oracleClientCache.js +19 -0
- package/lib/oracles/pythClient.d.ts +3 -5
- package/lib/oracles/pythClient.js +12 -31
- package/lib/oracles/quoteAssetOracleClient.d.ts +9 -0
- package/lib/oracles/quoteAssetOracleClient.js +21 -0
- package/lib/oracles/switchboardClient.d.ts +3 -5
- package/lib/oracles/switchboardClient.js +29 -50
- package/lib/oracles/types.d.ts +6 -1
- package/lib/orderParams.d.ts +14 -5
- package/lib/orderParams.js +8 -96
- package/lib/orders.d.ts +6 -7
- package/lib/orders.js +11 -89
- package/lib/slot/SlotSubscriber.d.ts +19 -0
- package/lib/slot/SlotSubscriber.js +26 -0
- package/lib/tx/retryTxSender.d.ts +2 -2
- package/lib/tx/retryTxSender.js +108 -123
- package/lib/tx/types.d.ts +5 -1
- package/lib/tx/utils.d.ts +1 -1
- package/lib/tx/utils.js +11 -2
- package/lib/types.d.ts +180 -110
- package/lib/types.js +54 -1
- package/lib/userName.d.ts +4 -0
- package/lib/userName.js +20 -0
- package/lib/util/computeUnits.js +10 -21
- package/lib/util/tps.js +11 -22
- package/lib/wallet.js +7 -20
- package/package.json +11 -4
- package/src/accounts/bulkAccountLoader.js +197 -0
- package/src/accounts/bulkAccountLoader.ts +21 -15
- package/src/accounts/bulkUserSubscription.js +33 -0
- package/src/accounts/bulkUserSubscription.ts +1 -45
- package/src/accounts/fetch.js +29 -0
- package/src/accounts/fetch.ts +33 -0
- package/src/accounts/pollingClearingHouseAccountSubscriber.js +311 -0
- package/src/accounts/pollingClearingHouseAccountSubscriber.ts +249 -214
- package/src/accounts/pollingOracleSubscriber.js +93 -0
- package/src/accounts/pollingOracleSubscriber.ts +16 -8
- package/src/accounts/pollingTokenAccountSubscriber.js +90 -0
- package/src/accounts/pollingTokenAccountSubscriber.ts +11 -8
- package/src/accounts/pollingUserAccountSubscriber.js +132 -0
- package/src/accounts/pollingUserAccountSubscriber.ts +21 -86
- package/src/accounts/types.js +10 -0
- package/src/accounts/types.ts +41 -70
- package/src/accounts/utils.js +7 -0
- package/src/accounts/webSocketAccountSubscriber.js +93 -0
- package/src/accounts/webSocketAccountSubscriber.ts +33 -16
- package/src/accounts/webSocketClearingHouseAccountSubscriber.js +233 -0
- package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +190 -240
- package/src/accounts/webSocketUserAccountSubscriber.js +62 -0
- package/src/accounts/webSocketUserAccountSubscriber.ts +11 -71
- package/src/addresses/marketAddresses.js +26 -0
- package/src/addresses/marketAddresses.ts +18 -0
- package/{lib/addresses.js → src/addresses/pda.js} +45 -34
- package/src/addresses/pda.ts +118 -0
- package/src/admin.ts +247 -336
- package/src/assert/assert.js +9 -0
- package/src/clearingHouse.ts +1023 -1026
- package/src/clearingHouseConfig.ts +37 -0
- package/src/clearingHouseUser.ts +275 -185
- package/src/clearingHouseUserConfig.ts +18 -0
- package/src/config.ts +54 -1
- package/src/constants/banks.ts +50 -0
- package/src/constants/markets.ts +16 -207
- package/src/constants/numericConstants.ts +34 -5
- package/src/events/eventList.js +77 -0
- package/src/events/eventList.ts +94 -0
- package/src/events/eventSubscriber.js +139 -0
- package/src/events/eventSubscriber.ts +194 -0
- package/src/events/fetchLogs.js +50 -0
- package/src/events/fetchLogs.ts +80 -0
- package/src/events/pollingLogProvider.js +64 -0
- package/src/events/pollingLogProvider.ts +79 -0
- package/src/events/sort.js +44 -0
- package/src/events/sort.ts +65 -0
- package/src/events/txEventCache.js +71 -0
- package/src/events/txEventCache.ts +74 -0
- package/src/events/types.js +20 -0
- package/src/events/types.ts +98 -0
- package/src/events/webSocketLogProvider.js +41 -0
- package/src/events/webSocketLogProvider.ts +38 -0
- package/src/examples/makeTradeExample.js +80 -0
- package/src/examples/makeTradeExample.ts +20 -11
- package/src/factory/bigNum.js +364 -0
- package/src/factory/bigNum.ts +524 -0
- package/src/factory/oracleClient.js +20 -0
- package/src/factory/oracleClient.ts +7 -4
- package/src/idl/clearing_house.json +2258 -2774
- package/src/index.js +69 -0
- package/src/index.ts +14 -5
- package/src/math/amm.js +369 -0
- package/src/math/amm.ts +382 -243
- package/src/math/auction.js +42 -0
- package/src/math/auction.ts +43 -0
- package/src/math/bankBalance.js +75 -0
- package/src/math/bankBalance.ts +112 -0
- package/src/math/conversion.js +11 -0
- package/src/math/conversion.ts +1 -11
- package/src/math/funding.js +248 -0
- package/src/math/funding.ts +12 -9
- package/src/math/market.js +57 -0
- package/src/math/market.ts +37 -30
- package/src/math/oracles.js +26 -0
- package/src/math/orders.js +110 -0
- package/src/math/orders.ts +43 -1
- package/src/math/position.js +140 -0
- package/src/math/position.ts +52 -35
- package/src/math/repeg.js +128 -0
- package/src/math/repeg.ts +176 -0
- package/src/math/state.js +15 -0
- package/src/math/trade.js +253 -0
- package/src/math/trade.ts +55 -47
- package/src/math/utils.js +26 -0
- package/src/math/utils.js.map +1 -0
- package/src/mockUSDCFaucet.js +171 -0
- package/src/oracles/oracleClientCache.js +19 -0
- package/src/oracles/oracleClientCache.ts +20 -0
- package/src/oracles/pythClient.js +46 -0
- package/src/oracles/pythClient.ts +4 -11
- package/src/oracles/quoteAssetOracleClient.js +32 -0
- package/src/oracles/quoteAssetOracleClient.ts +25 -0
- package/src/oracles/switchboardClient.js +69 -0
- package/src/oracles/switchboardClient.ts +11 -24
- package/src/oracles/types.js +2 -0
- package/src/oracles/types.ts +7 -1
- package/src/orderParams.js +20 -0
- package/src/orderParams.ts +20 -141
- package/src/orders.js +134 -0
- package/src/orders.ts +25 -134
- package/src/slot/SlotSubscriber.js +39 -0
- package/src/slot/SlotSubscriber.ts +42 -0
- package/src/token/index.js +38 -0
- package/src/tx/retryTxSender.js +188 -0
- package/src/tx/retryTxSender.ts +6 -4
- package/src/tx/types.js +2 -0
- package/src/tx/types.ts +6 -1
- package/src/tx/utils.js +17 -0
- package/src/tx/utils.ts +22 -3
- package/src/types.js +114 -0
- package/src/types.ts +176 -124
- package/src/userName.js +20 -0
- package/src/userName.ts +20 -0
- package/src/util/promiseTimeout.js +14 -0
- package/src/util/tps.js +27 -0
- package/src/wallet.js +35 -0
- package/tests/bn/test.ts +255 -0
- package/tsconfig.json +12 -12
- package/lib/addresses.d.ts +0 -10
- package/lib/constants/accounts.d.ts +0 -15
- package/lib/constants/accounts.js +0 -18
- package/lib/factory/clearingHouse.d.ts +0 -35
- package/lib/factory/clearingHouse.js +0 -81
- package/lib/factory/clearingHouseUser.d.ts +0 -19
- package/lib/factory/clearingHouseUser.js +0 -34
- package/lib/math/insuranceFund.d.ts +0 -15
- package/lib/math/insuranceFund.js +0 -33
- package/lib/settlement.d.ts +0 -4
- package/lib/settlement.js +0 -10
- package/lib/tx/defaultTxSender.d.ts +0 -8
- package/lib/tx/defaultTxSender.js +0 -12
- package/src/addresses.ts +0 -82
- package/src/constants/accounts.ts +0 -26
- package/src/factory/clearingHouse.ts +0 -173
- package/src/factory/clearingHouseUser.ts +0 -73
- package/src/math/insuranceFund.ts +0 -29
- package/src/settlement.ts +0 -9
- package/src/tx/defaultTxSender.ts +0 -24
package/lib/math/market.js
CHANGED
|
@@ -9,8 +9,9 @@ const amm_1 = require("./amm");
|
|
|
9
9
|
* @param market
|
|
10
10
|
* @return markPrice : Precision MARK_PRICE_PRECISION
|
|
11
11
|
*/
|
|
12
|
-
function calculateMarkPrice(market) {
|
|
13
|
-
|
|
12
|
+
function calculateMarkPrice(market, oraclePriceData) {
|
|
13
|
+
const newAmm = (0, amm_1.calculateUpdatedAMM)(market.amm, oraclePriceData);
|
|
14
|
+
return (0, amm_1.calculatePrice)(newAmm.baseAssetReserve, newAmm.quoteAssetReserve, newAmm.pegMultiplier);
|
|
14
15
|
}
|
|
15
16
|
exports.calculateMarkPrice = calculateMarkPrice;
|
|
16
17
|
/**
|
|
@@ -19,9 +20,9 @@ exports.calculateMarkPrice = calculateMarkPrice;
|
|
|
19
20
|
* @param market
|
|
20
21
|
* @return bidPrice : Precision MARK_PRICE_PRECISION
|
|
21
22
|
*/
|
|
22
|
-
function calculateBidPrice(market) {
|
|
23
|
-
const { baseAssetReserve, quoteAssetReserve } = (0, amm_1.
|
|
24
|
-
return (0, amm_1.calculatePrice)(baseAssetReserve, quoteAssetReserve,
|
|
23
|
+
function calculateBidPrice(market, oraclePriceData) {
|
|
24
|
+
const { baseAssetReserve, quoteAssetReserve, newPeg } = (0, amm_1.calculateUpdatedAMMSpreadReserves)(market.amm, types_1.PositionDirection.SHORT, oraclePriceData);
|
|
25
|
+
return (0, amm_1.calculatePrice)(baseAssetReserve, quoteAssetReserve, newPeg);
|
|
25
26
|
}
|
|
26
27
|
exports.calculateBidPrice = calculateBidPrice;
|
|
27
28
|
/**
|
|
@@ -30,9 +31,9 @@ exports.calculateBidPrice = calculateBidPrice;
|
|
|
30
31
|
* @param market
|
|
31
32
|
* @return bidPrice : Precision MARK_PRICE_PRECISION
|
|
32
33
|
*/
|
|
33
|
-
function calculateAskPrice(market) {
|
|
34
|
-
const { baseAssetReserve, quoteAssetReserve } = (0, amm_1.
|
|
35
|
-
return (0, amm_1.calculatePrice)(baseAssetReserve, quoteAssetReserve,
|
|
34
|
+
function calculateAskPrice(market, oraclePriceData) {
|
|
35
|
+
const { baseAssetReserve, quoteAssetReserve, newPeg } = (0, amm_1.calculateUpdatedAMMSpreadReserves)(market.amm, types_1.PositionDirection.LONG, oraclePriceData);
|
|
36
|
+
return (0, amm_1.calculatePrice)(baseAssetReserve, quoteAssetReserve, newPeg);
|
|
36
37
|
}
|
|
37
38
|
exports.calculateAskPrice = calculateAskPrice;
|
|
38
39
|
function calculateNewMarketAfterTrade(baseAssetAmount, direction, market) {
|
|
@@ -46,7 +47,7 @@ function calculateNewMarketAfterTrade(baseAssetAmount, direction, market) {
|
|
|
46
47
|
}
|
|
47
48
|
exports.calculateNewMarketAfterTrade = calculateNewMarketAfterTrade;
|
|
48
49
|
function calculateMarkOracleSpread(market, oraclePriceData) {
|
|
49
|
-
const markPrice = calculateMarkPrice(market);
|
|
50
|
+
const markPrice = calculateMarkPrice(market, oraclePriceData);
|
|
50
51
|
return calculateOracleSpread(markPrice, oraclePriceData);
|
|
51
52
|
}
|
|
52
53
|
exports.calculateMarkOracleSpread = calculateMarkOracleSpread;
|
package/lib/math/orders.d.ts
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
|
+
/// <reference types="bn.js" />
|
|
1
2
|
import { ClearingHouseUser } from '../clearingHouseUser';
|
|
2
|
-
import { Order } from '../types';
|
|
3
|
+
import { MarketAccount, Order } from '../types';
|
|
4
|
+
import { BN } from '@project-serum/anchor';
|
|
5
|
+
import { OraclePriceData } from '../oracles/types';
|
|
3
6
|
export declare function isOrderRiskIncreasing(user: ClearingHouseUser, order: Order): boolean;
|
|
4
7
|
export declare function isOrderRiskIncreasingInSameDirection(user: ClearingHouseUser, order: Order): boolean;
|
|
5
8
|
export declare function isOrderReduceOnly(user: ClearingHouseUser, order: Order): boolean;
|
|
9
|
+
export declare function standardizeBaseAssetAmount(baseAssetAmount: BN, stepSize: BN): BN;
|
|
10
|
+
export declare function getLimitPrice(order: Order, market: MarketAccount, oraclePriceData: OraclePriceData, slot: number): BN;
|
package/lib/math/orders.js
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isOrderReduceOnly = exports.isOrderRiskIncreasingInSameDirection = exports.isOrderRiskIncreasing = void 0;
|
|
3
|
+
exports.getLimitPrice = exports.standardizeBaseAssetAmount = exports.isOrderReduceOnly = exports.isOrderRiskIncreasingInSameDirection = exports.isOrderRiskIncreasing = void 0;
|
|
4
4
|
const types_1 = require("../types");
|
|
5
5
|
const numericConstants_1 = require("../constants/numericConstants");
|
|
6
|
+
const anchor_1 = require("@project-serum/anchor");
|
|
7
|
+
const auction_1 = require("./auction");
|
|
8
|
+
const market_1 = require("./market");
|
|
6
9
|
function isOrderRiskIncreasing(user, order) {
|
|
7
10
|
if ((0, types_1.isVariant)(order.status, 'init')) {
|
|
8
11
|
return false;
|
|
@@ -71,3 +74,37 @@ function isOrderReduceOnly(user, order) {
|
|
|
71
74
|
return true;
|
|
72
75
|
}
|
|
73
76
|
exports.isOrderReduceOnly = isOrderReduceOnly;
|
|
77
|
+
function standardizeBaseAssetAmount(baseAssetAmount, stepSize) {
|
|
78
|
+
const remainder = baseAssetAmount.mod(stepSize);
|
|
79
|
+
return baseAssetAmount.sub(remainder);
|
|
80
|
+
}
|
|
81
|
+
exports.standardizeBaseAssetAmount = standardizeBaseAssetAmount;
|
|
82
|
+
function getLimitPrice(order, market, oraclePriceData, slot) {
|
|
83
|
+
let limitPrice;
|
|
84
|
+
if (!order.oraclePriceOffset.eq(numericConstants_1.ZERO)) {
|
|
85
|
+
limitPrice = oraclePriceData.price.add(order.oraclePriceOffset);
|
|
86
|
+
}
|
|
87
|
+
else if ((0, types_1.isOneOfVariant)(order.orderType, ['market', 'triggerMarket'])) {
|
|
88
|
+
if ((0, auction_1.isAuctionComplete)(order, slot)) {
|
|
89
|
+
limitPrice = (0, auction_1.getAuctionPrice)(order, slot);
|
|
90
|
+
}
|
|
91
|
+
else if (!order.price.eq(numericConstants_1.ZERO)) {
|
|
92
|
+
limitPrice = order.price;
|
|
93
|
+
}
|
|
94
|
+
else if ((0, types_1.isVariant)(order.direction, 'long')) {
|
|
95
|
+
const askPrice = (0, market_1.calculateAskPrice)(market, oraclePriceData);
|
|
96
|
+
const delta = askPrice.div(new anchor_1.BN(market.amm.maxSlippageRatio));
|
|
97
|
+
limitPrice = askPrice.add(delta);
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
const bidPrice = (0, market_1.calculateBidPrice)(market, oraclePriceData);
|
|
101
|
+
const delta = bidPrice.div(new anchor_1.BN(market.amm.maxSlippageRatio));
|
|
102
|
+
limitPrice = bidPrice.sub(delta);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
limitPrice = order.price;
|
|
107
|
+
}
|
|
108
|
+
return limitPrice;
|
|
109
|
+
}
|
|
110
|
+
exports.getLimitPrice = getLimitPrice;
|
package/lib/math/position.d.ts
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
/// <reference types="bn.js" />
|
|
2
2
|
import { BN } from '../';
|
|
3
|
-
import {
|
|
3
|
+
import { OraclePriceData } from '../oracles/types';
|
|
4
|
+
import { MarketAccount, PositionDirection, UserPosition } from '../types';
|
|
4
5
|
/**
|
|
5
6
|
* calculateBaseAssetValue
|
|
6
7
|
* = market value of closing entire position
|
|
7
8
|
* @param market
|
|
8
9
|
* @param userPosition
|
|
10
|
+
* @param oraclePriceData
|
|
9
11
|
* @returns Base Asset Value. : Precision QUOTE_PRECISION
|
|
10
12
|
*/
|
|
11
|
-
export declare function calculateBaseAssetValue(market:
|
|
13
|
+
export declare function calculateBaseAssetValue(market: MarketAccount, userPosition: UserPosition, oraclePriceData: OraclePriceData): BN;
|
|
12
14
|
/**
|
|
13
15
|
* calculatePositionPNL
|
|
14
16
|
* = BaseAssetAmount * (Avg Exit Price - Avg Entry Price)
|
|
@@ -17,15 +19,15 @@ export declare function calculateBaseAssetValue(market: Market, userPosition: Us
|
|
|
17
19
|
* @param withFunding (adds unrealized funding payment pnl to result)
|
|
18
20
|
* @returns BaseAssetAmount : Precision QUOTE_PRECISION
|
|
19
21
|
*/
|
|
20
|
-
export declare function calculatePositionPNL(market:
|
|
21
|
-
export declare function calculateSettledPositionPNL(market: Market, marketPosition: UserPosition): BN;
|
|
22
|
+
export declare function calculatePositionPNL(market: MarketAccount, marketPosition: UserPosition, withFunding: boolean, oraclePriceData: OraclePriceData): BN;
|
|
22
23
|
/**
|
|
23
24
|
*
|
|
24
25
|
* @param market
|
|
25
26
|
* @param marketPosition
|
|
26
27
|
* @returns // TODO-PRECISION
|
|
27
28
|
*/
|
|
28
|
-
export declare function calculatePositionFundingPNL(market:
|
|
29
|
+
export declare function calculatePositionFundingPNL(market: MarketAccount, marketPosition: UserPosition): BN;
|
|
30
|
+
export declare function positionIsAvailable(position: UserPosition): boolean;
|
|
29
31
|
/**
|
|
30
32
|
*
|
|
31
33
|
* @param userPosition
|
package/lib/math/position.js
CHANGED
|
@@ -1,34 +1,47 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isEmptyPosition = exports.positionCurrentDirection = exports.findDirectionToClose = exports.calculateEntryPrice = exports.
|
|
3
|
+
exports.isEmptyPosition = exports.positionCurrentDirection = exports.findDirectionToClose = exports.calculateEntryPrice = exports.positionIsAvailable = exports.calculatePositionFundingPNL = exports.calculatePositionPNL = exports.calculateBaseAssetValue = void 0;
|
|
4
4
|
const __1 = require("../");
|
|
5
5
|
const numericConstants_1 = require("../constants/numericConstants");
|
|
6
6
|
const types_1 = require("../types");
|
|
7
7
|
const amm_1 = require("./amm");
|
|
8
|
-
const settlement_1 = require("../settlement");
|
|
9
8
|
/**
|
|
10
9
|
* calculateBaseAssetValue
|
|
11
10
|
* = market value of closing entire position
|
|
12
11
|
* @param market
|
|
13
12
|
* @param userPosition
|
|
13
|
+
* @param oraclePriceData
|
|
14
14
|
* @returns Base Asset Value. : Precision QUOTE_PRECISION
|
|
15
15
|
*/
|
|
16
|
-
function calculateBaseAssetValue(market, userPosition) {
|
|
16
|
+
function calculateBaseAssetValue(market, userPosition, oraclePriceData) {
|
|
17
17
|
if (userPosition.baseAssetAmount.eq(numericConstants_1.ZERO)) {
|
|
18
18
|
return numericConstants_1.ZERO;
|
|
19
19
|
}
|
|
20
20
|
const directionToClose = findDirectionToClose(userPosition);
|
|
21
|
-
|
|
21
|
+
let prepegAmm;
|
|
22
|
+
if (market.amm.baseSpread > 0) {
|
|
23
|
+
const { baseAssetReserve, quoteAssetReserve, sqrtK, newPeg } = (0, amm_1.calculateUpdatedAMMSpreadReserves)(market.amm, directionToClose, oraclePriceData);
|
|
24
|
+
prepegAmm = {
|
|
25
|
+
baseAssetReserve,
|
|
26
|
+
quoteAssetReserve,
|
|
27
|
+
sqrtK: sqrtK,
|
|
28
|
+
pegMultiplier: newPeg,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
prepegAmm = (0, amm_1.calculateUpdatedAMM)(market.amm, oraclePriceData);
|
|
33
|
+
}
|
|
34
|
+
const [newQuoteAssetReserve, _] = (0, amm_1.calculateAmmReservesAfterSwap)(prepegAmm, 'base', userPosition.baseAssetAmount.abs(), (0, amm_1.getSwapDirection)('base', directionToClose));
|
|
22
35
|
switch (directionToClose) {
|
|
23
36
|
case types_1.PositionDirection.SHORT:
|
|
24
|
-
return
|
|
37
|
+
return prepegAmm.quoteAssetReserve
|
|
25
38
|
.sub(newQuoteAssetReserve)
|
|
26
|
-
.mul(
|
|
39
|
+
.mul(prepegAmm.pegMultiplier)
|
|
27
40
|
.div(numericConstants_1.AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO);
|
|
28
41
|
case types_1.PositionDirection.LONG:
|
|
29
42
|
return newQuoteAssetReserve
|
|
30
|
-
.sub(
|
|
31
|
-
.mul(
|
|
43
|
+
.sub(prepegAmm.quoteAssetReserve)
|
|
44
|
+
.mul(prepegAmm.pegMultiplier)
|
|
32
45
|
.div(numericConstants_1.AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO)
|
|
33
46
|
.add(numericConstants_1.ONE);
|
|
34
47
|
}
|
|
@@ -42,11 +55,11 @@ exports.calculateBaseAssetValue = calculateBaseAssetValue;
|
|
|
42
55
|
* @param withFunding (adds unrealized funding payment pnl to result)
|
|
43
56
|
* @returns BaseAssetAmount : Precision QUOTE_PRECISION
|
|
44
57
|
*/
|
|
45
|
-
function calculatePositionPNL(market, marketPosition, withFunding = false) {
|
|
58
|
+
function calculatePositionPNL(market, marketPosition, withFunding = false, oraclePriceData) {
|
|
46
59
|
if (marketPosition.baseAssetAmount.eq(numericConstants_1.ZERO)) {
|
|
47
60
|
return numericConstants_1.ZERO;
|
|
48
61
|
}
|
|
49
|
-
const baseAssetValue = calculateBaseAssetValue(market, marketPosition);
|
|
62
|
+
const baseAssetValue = calculateBaseAssetValue(market, marketPosition, oraclePriceData);
|
|
50
63
|
let pnl;
|
|
51
64
|
if (marketPosition.baseAssetAmount.gt(numericConstants_1.ZERO)) {
|
|
52
65
|
pnl = baseAssetValue.sub(marketPosition.quoteAssetAmount);
|
|
@@ -61,23 +74,6 @@ function calculatePositionPNL(market, marketPosition, withFunding = false) {
|
|
|
61
74
|
return pnl;
|
|
62
75
|
}
|
|
63
76
|
exports.calculatePositionPNL = calculatePositionPNL;
|
|
64
|
-
function calculateSettledPositionPNL(market, marketPosition) {
|
|
65
|
-
let pnl = calculatePositionPNL(market, marketPosition);
|
|
66
|
-
if (pnl.gt(numericConstants_1.ZERO)) {
|
|
67
|
-
try {
|
|
68
|
-
pnl = pnl
|
|
69
|
-
.mul(new __1.BN(settlement_1.SETTLEMENT_RATIOS[marketPosition.marketIndex.toNumber()]))
|
|
70
|
-
.div(settlement_1.SETTLEMENT_RATIO_PRECISION);
|
|
71
|
-
}
|
|
72
|
-
catch (e) {
|
|
73
|
-
console.log(pnl.toString());
|
|
74
|
-
console.log(marketPosition.marketIndex.toNumber());
|
|
75
|
-
throw e;
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
return pnl;
|
|
79
|
-
}
|
|
80
|
-
exports.calculateSettledPositionPNL = calculateSettledPositionPNL;
|
|
81
77
|
/**
|
|
82
78
|
*
|
|
83
79
|
* @param market
|
|
@@ -104,6 +100,12 @@ function calculatePositionFundingPNL(market, marketPosition) {
|
|
|
104
100
|
return perPositionFundingRate;
|
|
105
101
|
}
|
|
106
102
|
exports.calculatePositionFundingPNL = calculatePositionFundingPNL;
|
|
103
|
+
function positionIsAvailable(position) {
|
|
104
|
+
return (position.baseAssetAmount.eq(numericConstants_1.ZERO) &&
|
|
105
|
+
position.openOrders.eq(numericConstants_1.ZERO) &&
|
|
106
|
+
position.unsettledPnl.eq(numericConstants_1.ZERO));
|
|
107
|
+
}
|
|
108
|
+
exports.positionIsAvailable = positionIsAvailable;
|
|
107
109
|
/**
|
|
108
110
|
*
|
|
109
111
|
* @param userPosition
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/// <reference types="bn.js" />
|
|
2
|
+
import { BN } from '@project-serum/anchor';
|
|
3
|
+
import { AMM } from '../types';
|
|
4
|
+
/**
|
|
5
|
+
* Helper function calculating adjust k cost
|
|
6
|
+
* @param amm
|
|
7
|
+
* @param numerator
|
|
8
|
+
* @param denomenator
|
|
9
|
+
* @returns cost : Precision QUOTE_ASSET_PRECISION
|
|
10
|
+
*/
|
|
11
|
+
export declare function calculateAdjustKCost(amm: AMM, numerator: BN, denomenator: BN): BN;
|
|
12
|
+
/**
|
|
13
|
+
* Helper function calculating adjust pegMultiplier (repeg) cost
|
|
14
|
+
*
|
|
15
|
+
* @param amm
|
|
16
|
+
* @param newPeg
|
|
17
|
+
* @returns cost : Precision QUOTE_ASSET_PRECISION
|
|
18
|
+
*/
|
|
19
|
+
export declare function calculateRepegCost(amm: AMM, newPeg: BN): BN;
|
|
20
|
+
export declare function calculateBudgetedKBN(x: BN, y: BN, budget: BN, Q: BN, d: BN): [BN, BN];
|
|
21
|
+
export declare function calculateBudgetedK(amm: AMM, cost: BN): [BN, BN];
|
|
22
|
+
export declare function calculateBudgetedPeg(amm: AMM, cost: BN, targetPrice: BN): BN;
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.calculateBudgetedPeg = exports.calculateBudgetedK = exports.calculateBudgetedKBN = exports.calculateRepegCost = exports.calculateAdjustKCost = void 0;
|
|
4
|
+
const anchor_1 = require("@project-serum/anchor");
|
|
5
|
+
const assert_1 = require("../assert/assert");
|
|
6
|
+
const numericConstants_1 = require("../constants/numericConstants");
|
|
7
|
+
/**
|
|
8
|
+
* Helper function calculating adjust k cost
|
|
9
|
+
* @param amm
|
|
10
|
+
* @param numerator
|
|
11
|
+
* @param denomenator
|
|
12
|
+
* @returns cost : Precision QUOTE_ASSET_PRECISION
|
|
13
|
+
*/
|
|
14
|
+
function calculateAdjustKCost(amm, numerator, denomenator) {
|
|
15
|
+
// const k = market.amm.sqrtK.mul(market.amm.sqrtK);
|
|
16
|
+
const x = amm.baseAssetReserve;
|
|
17
|
+
const y = amm.quoteAssetReserve;
|
|
18
|
+
const d = amm.netBaseAssetAmount;
|
|
19
|
+
const Q = amm.pegMultiplier;
|
|
20
|
+
const quoteScale = y.mul(d).mul(Q); //.div(AMM_RESERVE_PRECISION);
|
|
21
|
+
const p = numerator.mul(numericConstants_1.MARK_PRICE_PRECISION).div(denomenator);
|
|
22
|
+
const cost = quoteScale
|
|
23
|
+
.div(x.add(d))
|
|
24
|
+
.sub(quoteScale
|
|
25
|
+
.mul(p)
|
|
26
|
+
.div(numericConstants_1.MARK_PRICE_PRECISION)
|
|
27
|
+
.div(x.mul(p).div(numericConstants_1.MARK_PRICE_PRECISION).add(d)))
|
|
28
|
+
.div(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO)
|
|
29
|
+
.div(numericConstants_1.PEG_PRECISION);
|
|
30
|
+
return cost.mul(new anchor_1.BN(-1));
|
|
31
|
+
}
|
|
32
|
+
exports.calculateAdjustKCost = calculateAdjustKCost;
|
|
33
|
+
/**
|
|
34
|
+
* Helper function calculating adjust pegMultiplier (repeg) cost
|
|
35
|
+
*
|
|
36
|
+
* @param amm
|
|
37
|
+
* @param newPeg
|
|
38
|
+
* @returns cost : Precision QUOTE_ASSET_PRECISION
|
|
39
|
+
*/
|
|
40
|
+
function calculateRepegCost(amm, newPeg) {
|
|
41
|
+
const dqar = amm.quoteAssetReserve.sub(amm.terminalQuoteAssetReserve);
|
|
42
|
+
const cost = dqar
|
|
43
|
+
.mul(newPeg.sub(amm.pegMultiplier))
|
|
44
|
+
.div(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO)
|
|
45
|
+
.div(numericConstants_1.PEG_PRECISION);
|
|
46
|
+
return cost;
|
|
47
|
+
}
|
|
48
|
+
exports.calculateRepegCost = calculateRepegCost;
|
|
49
|
+
function calculateBudgetedKBN(x, y, budget, Q, d) {
|
|
50
|
+
(0, assert_1.assert)(Q.gt(new anchor_1.BN(0)));
|
|
51
|
+
const C = budget.mul(new anchor_1.BN(-1));
|
|
52
|
+
let dSign = new anchor_1.BN(1);
|
|
53
|
+
if (d.lt(new anchor_1.BN(0))) {
|
|
54
|
+
dSign = new anchor_1.BN(-1);
|
|
55
|
+
}
|
|
56
|
+
const pegged_y_d_d = y
|
|
57
|
+
.mul(d)
|
|
58
|
+
.mul(d)
|
|
59
|
+
.mul(Q)
|
|
60
|
+
.div(numericConstants_1.AMM_RESERVE_PRECISION)
|
|
61
|
+
.div(numericConstants_1.AMM_RESERVE_PRECISION)
|
|
62
|
+
.div(numericConstants_1.PEG_PRECISION);
|
|
63
|
+
const numer1 = pegged_y_d_d;
|
|
64
|
+
const numer2 = C.mul(d)
|
|
65
|
+
.div(numericConstants_1.QUOTE_PRECISION)
|
|
66
|
+
.mul(x.add(d))
|
|
67
|
+
.div(numericConstants_1.AMM_RESERVE_PRECISION)
|
|
68
|
+
.mul(dSign);
|
|
69
|
+
const denom1 = C.mul(x)
|
|
70
|
+
.mul(x.add(d))
|
|
71
|
+
.div(numericConstants_1.AMM_RESERVE_PRECISION)
|
|
72
|
+
.div(numericConstants_1.QUOTE_PRECISION);
|
|
73
|
+
const denom2 = pegged_y_d_d;
|
|
74
|
+
const numerator = numer1.sub(numer2).div(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO);
|
|
75
|
+
const denominator = denom1.add(denom2).div(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO);
|
|
76
|
+
return [numerator, denominator];
|
|
77
|
+
}
|
|
78
|
+
exports.calculateBudgetedKBN = calculateBudgetedKBN;
|
|
79
|
+
function calculateBudgetedK(amm, cost) {
|
|
80
|
+
// wolframalpha.com
|
|
81
|
+
// (1/(x+d) - p/(x*p+d))*y*d*Q = C solve for p
|
|
82
|
+
// p = (d(y*d*Q - C(x+d))) / (C*x(x+d) + y*d*d*Q)
|
|
83
|
+
// numer
|
|
84
|
+
// = y*d*d*Q - Cxd - Cdd
|
|
85
|
+
// = y/x*Q*d*d - Cd - Cd/x
|
|
86
|
+
// = mark - C/d - C/(x)
|
|
87
|
+
// = mark/C - 1/d - 1/x
|
|
88
|
+
// denom
|
|
89
|
+
// = C*x*x + C*x*d + y*d*d*Q
|
|
90
|
+
// = x/d**2 + 1 / d + mark/C
|
|
91
|
+
// todo: assumes k = x * y
|
|
92
|
+
// otherwise use: (y(1-p) + (kp^2/(x*p+d)) - k/(x+d)) * Q = C solve for p
|
|
93
|
+
const x = amm.baseAssetReserve;
|
|
94
|
+
const y = amm.quoteAssetReserve;
|
|
95
|
+
const d = amm.netBaseAssetAmount;
|
|
96
|
+
const Q = amm.pegMultiplier;
|
|
97
|
+
const [numerator, denominator] = calculateBudgetedKBN(x, y, cost, Q, d);
|
|
98
|
+
return [numerator, denominator];
|
|
99
|
+
}
|
|
100
|
+
exports.calculateBudgetedK = calculateBudgetedK;
|
|
101
|
+
function calculateBudgetedPeg(amm, cost, targetPrice) {
|
|
102
|
+
// wolframalpha.com
|
|
103
|
+
// (1/(x+d) - p/(x*p+d))*y*d*Q = C solve for p
|
|
104
|
+
// p = (d(y*d*Q - C(x+d))) / (C*x(x+d) + y*y*d*Q)
|
|
105
|
+
// todo: assumes k = x * y
|
|
106
|
+
// otherwise use: (y(1-p) + (kp^2/(x*p+d)) - k/(x+d)) * Q = C solve for p
|
|
107
|
+
const targetPeg = targetPrice
|
|
108
|
+
.mul(amm.baseAssetReserve)
|
|
109
|
+
.div(amm.quoteAssetReserve)
|
|
110
|
+
.div(numericConstants_1.PRICE_DIV_PEG);
|
|
111
|
+
const k = amm.sqrtK.mul(amm.sqrtK);
|
|
112
|
+
const x = amm.baseAssetReserve;
|
|
113
|
+
const y = amm.quoteAssetReserve;
|
|
114
|
+
const d = amm.netBaseAssetAmount;
|
|
115
|
+
const Q = amm.pegMultiplier;
|
|
116
|
+
const C = cost.mul(new anchor_1.BN(-1));
|
|
117
|
+
const deltaQuoteAssetReserves = y.sub(k.div(x.add(d)));
|
|
118
|
+
const pegChangeDirection = targetPeg.sub(Q);
|
|
119
|
+
const useTargetPeg = (deltaQuoteAssetReserves.lt(numericConstants_1.ZERO) && pegChangeDirection.gt(numericConstants_1.ZERO)) ||
|
|
120
|
+
(deltaQuoteAssetReserves.gt(numericConstants_1.ZERO) && pegChangeDirection.lt(numericConstants_1.ZERO));
|
|
121
|
+
if (deltaQuoteAssetReserves.eq(numericConstants_1.ZERO) || useTargetPeg) {
|
|
122
|
+
return targetPeg;
|
|
123
|
+
}
|
|
124
|
+
const deltaPegMultiplier = C.mul(numericConstants_1.MARK_PRICE_PRECISION).div(deltaQuoteAssetReserves.div(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO));
|
|
125
|
+
const newPeg = Q.sub(deltaPegMultiplier.mul(numericConstants_1.PEG_PRECISION).div(numericConstants_1.MARK_PRICE_PRECISION));
|
|
126
|
+
return newPeg;
|
|
127
|
+
}
|
|
128
|
+
exports.calculateBudgetedPeg = calculateBudgetedPeg;
|
package/lib/math/trade.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
/// <reference types="bn.js" />
|
|
2
|
-
import {
|
|
2
|
+
import { MarketAccount, PositionDirection } from '../types';
|
|
3
3
|
import { BN } from '@project-serum/anchor';
|
|
4
4
|
import { AssetType } from './amm';
|
|
5
|
+
import { OraclePriceData } from '../oracles/types';
|
|
5
6
|
export declare type PriceImpactUnit = 'entryPrice' | 'maxPrice' | 'priceDelta' | 'priceDeltaAsNumber' | 'pctAvg' | 'pctMax' | 'quoteAssetAmount' | 'quoteAssetAmountPeg' | 'acquiredBaseAssetAmount' | 'acquiredQuoteAssetAmount' | 'all';
|
|
6
7
|
/**
|
|
7
8
|
* Calculates avg/max slippage (price impact) for candidate trade
|
|
@@ -20,7 +21,7 @@ export declare type PriceImpactUnit = 'entryPrice' | 'maxPrice' | 'priceDelta' |
|
|
|
20
21
|
*
|
|
21
22
|
* 'newPrice' => the price of the asset after the trade : Precision MARK_PRICE_PRECISION
|
|
22
23
|
*/
|
|
23
|
-
export declare function calculateTradeSlippage(direction: PositionDirection, amount: BN, market:
|
|
24
|
+
export declare function calculateTradeSlippage(direction: PositionDirection, amount: BN, market: MarketAccount, inputAssetType?: AssetType, oraclePriceData?: OraclePriceData, useSpread?: boolean): [BN, BN, BN, BN];
|
|
24
25
|
/**
|
|
25
26
|
* Calculates acquired amounts for trade executed
|
|
26
27
|
* @param direction
|
|
@@ -32,7 +33,7 @@ export declare function calculateTradeSlippage(direction: PositionDirection, amo
|
|
|
32
33
|
* | 'acquiredBase' => positive/negative change in user's base : BN AMM_RESERVE_PRECISION
|
|
33
34
|
* | 'acquiredQuote' => positive/negative change in user's quote : BN TODO-PRECISION
|
|
34
35
|
*/
|
|
35
|
-
export declare function calculateTradeAcquiredAmounts(direction: PositionDirection, amount: BN, market:
|
|
36
|
+
export declare function calculateTradeAcquiredAmounts(direction: PositionDirection, amount: BN, market: MarketAccount, inputAssetType: AssetType, oraclePriceData: OraclePriceData, useSpread?: boolean): [BN, BN, BN];
|
|
36
37
|
/**
|
|
37
38
|
* calculateTargetPriceTrade
|
|
38
39
|
* simple function for finding arbitraging trades
|
|
@@ -50,4 +51,4 @@ export declare function calculateTradeAcquiredAmounts(direction: PositionDirecti
|
|
|
50
51
|
* targetPrice => the target price MARK_PRICE_PRECISION
|
|
51
52
|
* ]
|
|
52
53
|
*/
|
|
53
|
-
export declare function calculateTargetPriceTrade(market:
|
|
54
|
+
export declare function calculateTargetPriceTrade(market: MarketAccount, targetPrice: BN, pct?: BN, outputAssetType?: AssetType, oraclePriceData?: OraclePriceData, useSpread?: boolean): [PositionDirection, BN, BN, BN];
|
package/lib/math/trade.js
CHANGED
|
@@ -27,43 +27,46 @@ const MAXPCT = new anchor_1.BN(1000); //percentage units are [0,1000] => [0,1]
|
|
|
27
27
|
*
|
|
28
28
|
* 'newPrice' => the price of the asset after the trade : Precision MARK_PRICE_PRECISION
|
|
29
29
|
*/
|
|
30
|
-
function calculateTradeSlippage(direction, amount, market, inputAssetType = 'quote', useSpread = true) {
|
|
30
|
+
function calculateTradeSlippage(direction, amount, market, inputAssetType = 'quote', oraclePriceData, useSpread = true) {
|
|
31
31
|
let oldPrice;
|
|
32
32
|
if (useSpread && market.amm.baseSpread > 0) {
|
|
33
33
|
if ((0, types_2.isVariant)(direction, 'long')) {
|
|
34
|
-
oldPrice = (0, market_1.calculateAskPrice)(market);
|
|
34
|
+
oldPrice = (0, market_1.calculateAskPrice)(market, oraclePriceData);
|
|
35
35
|
}
|
|
36
36
|
else {
|
|
37
|
-
oldPrice = (0, market_1.calculateBidPrice)(market);
|
|
37
|
+
oldPrice = (0, market_1.calculateBidPrice)(market, oraclePriceData);
|
|
38
38
|
}
|
|
39
39
|
}
|
|
40
40
|
else {
|
|
41
|
-
oldPrice = (0, market_1.calculateMarkPrice)(market);
|
|
41
|
+
oldPrice = (0, market_1.calculateMarkPrice)(market, oraclePriceData);
|
|
42
42
|
}
|
|
43
43
|
if (amount.eq(numericConstants_1.ZERO)) {
|
|
44
44
|
return [numericConstants_1.ZERO, numericConstants_1.ZERO, oldPrice, oldPrice];
|
|
45
45
|
}
|
|
46
|
-
const [
|
|
47
|
-
const entryPrice =
|
|
46
|
+
const [acquiredBaseReserve, acquiredQuoteReserve, acquiredQuoteAssetAmount] = calculateTradeAcquiredAmounts(direction, amount, market, inputAssetType, oraclePriceData, useSpread);
|
|
47
|
+
const entryPrice = acquiredQuoteAssetAmount
|
|
48
|
+
.mul(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO)
|
|
49
|
+
.mul(numericConstants_1.MARK_PRICE_PRECISION)
|
|
50
|
+
.div(acquiredBaseReserve.abs());
|
|
48
51
|
let amm;
|
|
49
52
|
if (useSpread && market.amm.baseSpread > 0) {
|
|
50
|
-
const { baseAssetReserve, quoteAssetReserve } = (0, amm_1.
|
|
53
|
+
const { baseAssetReserve, quoteAssetReserve, sqrtK, newPeg } = (0, amm_1.calculateUpdatedAMMSpreadReserves)(market.amm, direction, oraclePriceData);
|
|
51
54
|
amm = {
|
|
52
55
|
baseAssetReserve,
|
|
53
56
|
quoteAssetReserve,
|
|
54
|
-
sqrtK:
|
|
55
|
-
pegMultiplier:
|
|
57
|
+
sqrtK: sqrtK,
|
|
58
|
+
pegMultiplier: newPeg,
|
|
56
59
|
};
|
|
57
60
|
}
|
|
58
61
|
else {
|
|
59
62
|
amm = market.amm;
|
|
60
63
|
}
|
|
61
|
-
const newPrice = (0, amm_1.calculatePrice)(amm.baseAssetReserve.sub(
|
|
64
|
+
const newPrice = (0, amm_1.calculatePrice)(amm.baseAssetReserve.sub(acquiredBaseReserve), amm.quoteAssetReserve.sub(acquiredQuoteReserve), amm.pegMultiplier);
|
|
62
65
|
if (direction == types_1.PositionDirection.SHORT) {
|
|
63
|
-
(0, assert_1.assert)(newPrice.
|
|
66
|
+
(0, assert_1.assert)(newPrice.lte(oldPrice));
|
|
64
67
|
}
|
|
65
68
|
else {
|
|
66
|
-
(0, assert_1.assert)(oldPrice.
|
|
69
|
+
(0, assert_1.assert)(oldPrice.lte(newPrice));
|
|
67
70
|
}
|
|
68
71
|
const pctMaxSlippage = newPrice
|
|
69
72
|
.sub(oldPrice)
|
|
@@ -89,19 +92,19 @@ exports.calculateTradeSlippage = calculateTradeSlippage;
|
|
|
89
92
|
* | 'acquiredBase' => positive/negative change in user's base : BN AMM_RESERVE_PRECISION
|
|
90
93
|
* | 'acquiredQuote' => positive/negative change in user's quote : BN TODO-PRECISION
|
|
91
94
|
*/
|
|
92
|
-
function calculateTradeAcquiredAmounts(direction, amount, market, inputAssetType = 'quote', useSpread = true) {
|
|
95
|
+
function calculateTradeAcquiredAmounts(direction, amount, market, inputAssetType = 'quote', oraclePriceData, useSpread = true) {
|
|
93
96
|
if (amount.eq(numericConstants_1.ZERO)) {
|
|
94
|
-
return [numericConstants_1.ZERO, numericConstants_1.ZERO];
|
|
97
|
+
return [numericConstants_1.ZERO, numericConstants_1.ZERO, numericConstants_1.ZERO];
|
|
95
98
|
}
|
|
96
99
|
const swapDirection = (0, amm_1.getSwapDirection)(inputAssetType, direction);
|
|
97
100
|
let amm;
|
|
98
101
|
if (useSpread && market.amm.baseSpread > 0) {
|
|
99
|
-
const { baseAssetReserve, quoteAssetReserve } = (0, amm_1.
|
|
102
|
+
const { baseAssetReserve, quoteAssetReserve, sqrtK, newPeg } = (0, amm_1.calculateUpdatedAMMSpreadReserves)(market.amm, direction, oraclePriceData);
|
|
100
103
|
amm = {
|
|
101
104
|
baseAssetReserve,
|
|
102
105
|
quoteAssetReserve,
|
|
103
|
-
sqrtK:
|
|
104
|
-
pegMultiplier:
|
|
106
|
+
sqrtK: sqrtK,
|
|
107
|
+
pegMultiplier: newPeg,
|
|
105
108
|
};
|
|
106
109
|
}
|
|
107
110
|
else {
|
|
@@ -110,7 +113,8 @@ function calculateTradeAcquiredAmounts(direction, amount, market, inputAssetType
|
|
|
110
113
|
const [newQuoteAssetReserve, newBaseAssetReserve] = (0, amm_1.calculateAmmReservesAfterSwap)(amm, inputAssetType, amount, swapDirection);
|
|
111
114
|
const acquiredBase = amm.baseAssetReserve.sub(newBaseAssetReserve);
|
|
112
115
|
const acquiredQuote = amm.quoteAssetReserve.sub(newQuoteAssetReserve);
|
|
113
|
-
|
|
116
|
+
const acquiredQuoteAssetamount = (0, amm_1.calculateQuoteAssetAmountSwapped)(acquiredQuote.abs(), amm.pegMultiplier, swapDirection);
|
|
117
|
+
return [acquiredBase, acquiredQuote, acquiredQuoteAssetamount];
|
|
114
118
|
}
|
|
115
119
|
exports.calculateTradeAcquiredAmounts = calculateTradeAcquiredAmounts;
|
|
116
120
|
/**
|
|
@@ -130,13 +134,13 @@ exports.calculateTradeAcquiredAmounts = calculateTradeAcquiredAmounts;
|
|
|
130
134
|
* targetPrice => the target price MARK_PRICE_PRECISION
|
|
131
135
|
* ]
|
|
132
136
|
*/
|
|
133
|
-
function calculateTargetPriceTrade(market, targetPrice, pct = MAXPCT, outputAssetType = 'quote', useSpread = true) {
|
|
137
|
+
function calculateTargetPriceTrade(market, targetPrice, pct = MAXPCT, outputAssetType = 'quote', oraclePriceData, useSpread = true) {
|
|
134
138
|
(0, assert_1.assert)(market.amm.baseAssetReserve.gt(numericConstants_1.ZERO));
|
|
135
139
|
(0, assert_1.assert)(targetPrice.gt(numericConstants_1.ZERO));
|
|
136
140
|
(0, assert_1.assert)(pct.lte(MAXPCT) && pct.gt(numericConstants_1.ZERO));
|
|
137
|
-
const markPriceBefore = (0, market_1.calculateMarkPrice)(market);
|
|
138
|
-
const bidPriceBefore = (0, market_1.calculateBidPrice)(market);
|
|
139
|
-
const askPriceBefore = (0, market_1.calculateAskPrice)(market);
|
|
141
|
+
const markPriceBefore = (0, market_1.calculateMarkPrice)(market, oraclePriceData);
|
|
142
|
+
const bidPriceBefore = (0, market_1.calculateBidPrice)(market, oraclePriceData);
|
|
143
|
+
const askPriceBefore = (0, market_1.calculateAskPrice)(market, oraclePriceData);
|
|
140
144
|
let direction;
|
|
141
145
|
if (targetPrice.gt(markPriceBefore)) {
|
|
142
146
|
const priceGap = targetPrice.sub(markPriceBefore);
|
|
@@ -154,16 +158,17 @@ function calculateTargetPriceTrade(market, targetPrice, pct = MAXPCT, outputAsse
|
|
|
154
158
|
let baseSize;
|
|
155
159
|
let baseAssetReserveBefore;
|
|
156
160
|
let quoteAssetReserveBefore;
|
|
161
|
+
let peg = market.amm.pegMultiplier;
|
|
157
162
|
if (useSpread && market.amm.baseSpread > 0) {
|
|
158
|
-
const { baseAssetReserve, quoteAssetReserve } = (0, amm_1.
|
|
163
|
+
const { baseAssetReserve, quoteAssetReserve, newPeg } = (0, amm_1.calculateUpdatedAMMSpreadReserves)(market.amm, direction, oraclePriceData);
|
|
159
164
|
baseAssetReserveBefore = baseAssetReserve;
|
|
160
165
|
quoteAssetReserveBefore = quoteAssetReserve;
|
|
166
|
+
peg = newPeg;
|
|
161
167
|
}
|
|
162
168
|
else {
|
|
163
169
|
baseAssetReserveBefore = market.amm.baseAssetReserve;
|
|
164
170
|
quoteAssetReserveBefore = market.amm.quoteAssetReserve;
|
|
165
171
|
}
|
|
166
|
-
const peg = market.amm.pegMultiplier;
|
|
167
172
|
const invariant = market.amm.sqrtK.mul(market.amm.sqrtK);
|
|
168
173
|
const k = invariant.mul(numericConstants_1.MARK_PRICE_PRECISION);
|
|
169
174
|
let baseAssetReserveAfter;
|