@drift-labs/sdk 0.2.0-master.2 → 0.2.0-master.22
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/bulkUserStatsSubscription.d.ts +7 -0
- package/lib/accounts/bulkUserStatsSubscription.js +21 -0
- package/lib/accounts/bulkUserSubscription.js +0 -1
- package/lib/accounts/fetch.d.ts +2 -1
- package/lib/accounts/fetch.js +9 -1
- package/lib/accounts/pollingUserStatsAccountSubscriber.d.ts +27 -0
- package/lib/accounts/pollingUserStatsAccountSubscriber.js +113 -0
- package/lib/accounts/types.d.ts +14 -1
- package/lib/accounts/webSocketUserStatsAccountSubsriber.d.ts +20 -0
- package/lib/accounts/webSocketUserStatsAccountSubsriber.js +47 -0
- package/lib/addresses/pda.d.ts +1 -0
- package/lib/addresses/pda.js +8 -1
- package/lib/admin.d.ts +9 -5
- package/lib/admin.js +52 -11
- package/lib/clearingHouse.d.ts +52 -23
- package/lib/clearingHouse.js +736 -200
- package/lib/clearingHouseConfig.d.ts +1 -0
- package/lib/clearingHouseUser.d.ts +24 -16
- package/lib/clearingHouseUser.js +223 -101
- package/lib/clearingHouseUserStats.d.ts +18 -0
- package/lib/clearingHouseUserStats.js +49 -0
- package/lib/clearingHouseUserStatsConfig.d.ts +14 -0
- package/lib/clearingHouseUserStatsConfig.js +2 -0
- package/lib/config.js +1 -1
- package/lib/constants/banks.d.ts +2 -2
- package/lib/constants/banks.js +12 -4
- package/lib/constants/numericConstants.d.ts +5 -0
- package/lib/constants/numericConstants.js +8 -3
- package/lib/events/eventList.js +3 -0
- package/lib/events/types.d.ts +3 -1
- package/lib/events/types.js +2 -0
- package/lib/factory/bigNum.d.ts +1 -0
- package/lib/factory/bigNum.js +34 -10
- package/lib/idl/clearing_house.json +1603 -377
- package/lib/idl/{mock_usdc_faucet.json → token_faucet.json} +46 -23
- package/lib/index.d.ts +9 -3
- package/lib/index.js +13 -3
- package/lib/math/amm.d.ts +1 -0
- package/lib/math/amm.js +22 -38
- package/lib/math/auction.js +4 -1
- package/lib/math/bankBalance.d.ts +7 -1
- package/lib/math/bankBalance.js +77 -2
- package/lib/math/margin.d.ts +11 -0
- package/lib/math/margin.js +72 -0
- package/lib/math/market.d.ts +4 -1
- package/lib/math/market.js +35 -1
- package/lib/math/oracles.d.ts +3 -0
- package/lib/math/oracles.js +25 -5
- package/lib/math/orders.d.ts +6 -2
- package/lib/math/orders.js +62 -12
- package/lib/math/position.d.ts +8 -0
- package/lib/math/position.js +45 -12
- package/lib/math/trade.d.ts +1 -1
- package/lib/math/trade.js +7 -10
- package/lib/orderParams.d.ts +14 -5
- package/lib/orderParams.js +8 -96
- package/lib/slot/SlotSubscriber.d.ts +7 -0
- package/lib/slot/SlotSubscriber.js +3 -0
- package/lib/{mockUSDCFaucet.d.ts → tokenFaucet.d.ts} +8 -5
- package/lib/{mockUSDCFaucet.js → tokenFaucet.js} +63 -51
- package/lib/tx/retryTxSender.js +9 -2
- package/lib/tx/utils.js +1 -1
- package/lib/types.d.ts +236 -26
- package/lib/types.js +64 -1
- package/lib/util/computeUnits.js +1 -1
- package/lib/util/getTokenAddress.d.ts +2 -0
- package/lib/util/getTokenAddress.js +9 -0
- package/package.json +3 -3
- package/src/accounts/bulkUserStatsSubscription.ts +33 -0
- package/src/accounts/bulkUserSubscription.ts +0 -1
- package/src/accounts/fetch.ts +27 -2
- package/src/accounts/pollingUserStatsAccountSubscriber.ts +172 -0
- package/src/accounts/types.ts +18 -0
- package/src/accounts/webSocketUserStatsAccountSubsriber.ts +80 -0
- package/src/addresses/marketAddresses.js +26 -0
- package/src/addresses/pda.ts +13 -0
- package/src/admin.ts +82 -15
- package/src/assert/assert.js +9 -0
- package/src/clearingHouse.ts +1232 -323
- package/src/clearingHouseConfig.ts +1 -0
- package/src/clearingHouseUser.ts +343 -155
- package/src/clearingHouseUserStats.ts +75 -0
- package/src/clearingHouseUserStatsConfig.ts +18 -0
- package/src/config.ts +1 -1
- package/src/constants/banks.js +42 -0
- package/src/constants/banks.ts +14 -4
- package/src/constants/markets.js +42 -0
- package/src/constants/numericConstants.js +41 -0
- package/src/constants/numericConstants.ts +14 -2
- package/src/events/eventList.js +77 -0
- package/src/events/eventList.ts +3 -0
- package/src/events/eventSubscriber.js +139 -0
- package/src/events/sort.js +44 -0
- package/src/events/txEventCache.js +71 -0
- package/src/events/types.ts +6 -0
- package/src/examples/makeTradeExample.js +80 -0
- package/src/factory/bigNum.js +390 -0
- package/src/factory/bigNum.ts +42 -13
- package/src/factory/oracleClient.js +20 -0
- package/src/idl/clearing_house.json +1603 -377
- package/src/idl/{mock_usdc_faucet.json → token_faucet.json} +46 -23
- package/src/index.ts +9 -3
- package/src/math/amm.ts +54 -55
- package/src/math/auction.js +42 -0
- package/src/math/auction.ts +5 -1
- package/src/math/bankBalance.ts +148 -2
- package/src/math/conversion.js +11 -0
- package/src/math/funding.js +248 -0
- package/src/math/margin.ts +124 -0
- package/src/math/market.ts +66 -1
- package/src/math/oracles.js +26 -0
- package/src/math/oracles.ts +42 -5
- package/src/math/orders.ts +129 -13
- package/src/math/position.ts +64 -9
- package/src/math/repeg.js +128 -0
- package/src/math/state.js +15 -0
- package/src/math/trade.js +253 -0
- package/src/math/trade.ts +23 -25
- package/src/math/utils.js +0 -1
- package/src/oracles/oracleClientCache.js +19 -0
- package/src/oracles/pythClient.js +46 -0
- package/src/oracles/quoteAssetOracleClient.js +32 -0
- package/src/oracles/switchboardClient.js +69 -0
- package/src/oracles/types.js +2 -0
- package/src/orderParams.js +20 -0
- package/src/orderParams.ts +20 -141
- package/src/slot/SlotSubscriber.js +39 -0
- package/src/slot/SlotSubscriber.ts +11 -1
- package/src/token/index.js +38 -0
- package/src/tokenFaucet.js +189 -0
- package/src/{mockUSDCFaucet.ts → tokenFaucet.ts} +82 -70
- package/src/tx/retryTxSender.ts +11 -3
- package/src/tx/types.js +2 -0
- package/src/tx/utils.js +17 -0
- package/src/tx/utils.ts +1 -1
- package/src/types.ts +239 -27
- package/src/userName.js +20 -0
- package/src/util/computeUnits.js +21 -11
- package/src/util/computeUnits.ts +1 -1
- package/src/util/getTokenAddress.js +9 -0
- package/src/util/getTokenAddress.ts +18 -0
- package/src/util/promiseTimeout.js +14 -0
- package/src/util/tps.js +27 -0
- package/src/wallet.js +35 -0
- package/tests/bn/test.ts +10 -0
- package/lib/orders.d.ts +0 -8
- package/lib/orders.js +0 -142
- package/src/orders.ts +0 -251
- package/src/util/computeUnits.js.map +0 -1
package/lib/orders.js
DELETED
|
@@ -1,142 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.calculateAmountToTradeForTriggerLimit = exports.calculateAmountToTradeForLimit = exports.calculateBaseAssetAmountMarketCanExecute = exports.calculateNewStateAfterOrder = void 0;
|
|
4
|
-
const types_1 = require("./types");
|
|
5
|
-
const _1 = require(".");
|
|
6
|
-
const market_1 = require("./math/market");
|
|
7
|
-
const numericConstants_1 = require("./constants/numericConstants");
|
|
8
|
-
const amm_1 = require("./math/amm");
|
|
9
|
-
const position_1 = require("./math/position");
|
|
10
|
-
function calculateNewStateAfterOrder(userAccount, userPosition, market, order) {
|
|
11
|
-
if ((0, types_1.isVariant)(order.status, 'init')) {
|
|
12
|
-
return null;
|
|
13
|
-
}
|
|
14
|
-
const baseAssetAmountToTrade = calculateBaseAssetAmountMarketCanExecute(market, order);
|
|
15
|
-
if (baseAssetAmountToTrade.lt(market.amm.baseAssetAmountStepSize)) {
|
|
16
|
-
return null;
|
|
17
|
-
}
|
|
18
|
-
const userAccountAfter = Object.assign({}, userAccount);
|
|
19
|
-
const userPositionAfter = Object.assign({}, userPosition);
|
|
20
|
-
const currentPositionDirection = (0, position_1.positionCurrentDirection)(userPosition);
|
|
21
|
-
const increasePosition = userPosition.baseAssetAmount.eq(numericConstants_1.ZERO) ||
|
|
22
|
-
isSameDirection(order.direction, currentPositionDirection);
|
|
23
|
-
if (increasePosition) {
|
|
24
|
-
const marketAfter = (0, market_1.calculateNewMarketAfterTrade)(baseAssetAmountToTrade, order.direction, market);
|
|
25
|
-
const { quoteAssetAmountSwapped, baseAssetAmountSwapped } = calculateAmountSwapped(market, marketAfter);
|
|
26
|
-
userPositionAfter.baseAssetAmount = userPositionAfter.baseAssetAmount.add(baseAssetAmountSwapped);
|
|
27
|
-
userPositionAfter.quoteAssetAmount = userPositionAfter.quoteAssetAmount.add(quoteAssetAmountSwapped);
|
|
28
|
-
return [userAccountAfter, userPositionAfter, marketAfter];
|
|
29
|
-
}
|
|
30
|
-
else {
|
|
31
|
-
const reversePosition = baseAssetAmountToTrade.gt(userPosition.baseAssetAmount.abs());
|
|
32
|
-
if (reversePosition) {
|
|
33
|
-
const intermediateMarket = (0, market_1.calculateNewMarketAfterTrade)(userPosition.baseAssetAmount, (0, position_1.findDirectionToClose)(userPosition), market);
|
|
34
|
-
const { quoteAssetAmountSwapped: baseAssetValue } = calculateAmountSwapped(market, intermediateMarket);
|
|
35
|
-
let pnl;
|
|
36
|
-
if ((0, types_1.isVariant)(currentPositionDirection, 'long')) {
|
|
37
|
-
pnl = baseAssetValue.sub(userPosition.quoteAssetAmount);
|
|
38
|
-
}
|
|
39
|
-
else {
|
|
40
|
-
pnl = userPosition.quoteAssetAmount.sub(baseAssetValue);
|
|
41
|
-
}
|
|
42
|
-
userAccountAfter.collateral = userAccountAfter.collateral.add(pnl);
|
|
43
|
-
const baseAssetAmountLeft = baseAssetAmountToTrade.sub(userPosition.baseAssetAmount.abs());
|
|
44
|
-
const marketAfter = (0, market_1.calculateNewMarketAfterTrade)(baseAssetAmountLeft, order.direction, intermediateMarket);
|
|
45
|
-
const { quoteAssetAmountSwapped, baseAssetAmountSwapped } = calculateAmountSwapped(intermediateMarket, marketAfter);
|
|
46
|
-
userPositionAfter.quoteAssetAmount = quoteAssetAmountSwapped;
|
|
47
|
-
userPositionAfter.baseAssetAmount = baseAssetAmountSwapped;
|
|
48
|
-
return [userAccountAfter, userPositionAfter, marketAfter];
|
|
49
|
-
}
|
|
50
|
-
else {
|
|
51
|
-
const marketAfter = (0, market_1.calculateNewMarketAfterTrade)(baseAssetAmountToTrade, order.direction, market);
|
|
52
|
-
const { quoteAssetAmountSwapped: baseAssetValue, baseAssetAmountSwapped, } = calculateAmountSwapped(market, marketAfter);
|
|
53
|
-
const costBasisRealized = userPosition.quoteAssetAmount
|
|
54
|
-
.mul(baseAssetAmountSwapped.abs())
|
|
55
|
-
.div(userPosition.baseAssetAmount.abs());
|
|
56
|
-
let pnl;
|
|
57
|
-
if ((0, types_1.isVariant)(currentPositionDirection, 'long')) {
|
|
58
|
-
pnl = baseAssetValue.sub(costBasisRealized);
|
|
59
|
-
}
|
|
60
|
-
else {
|
|
61
|
-
pnl = costBasisRealized.sub(baseAssetValue);
|
|
62
|
-
}
|
|
63
|
-
userAccountAfter.collateral = userAccountAfter.collateral.add(pnl);
|
|
64
|
-
userPositionAfter.baseAssetAmount = userPositionAfter.baseAssetAmount.add(baseAssetAmountSwapped);
|
|
65
|
-
userPositionAfter.quoteAssetAmount =
|
|
66
|
-
userPositionAfter.quoteAssetAmount.sub(costBasisRealized);
|
|
67
|
-
return [userAccountAfter, userPositionAfter, marketAfter];
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
exports.calculateNewStateAfterOrder = calculateNewStateAfterOrder;
|
|
72
|
-
function calculateAmountSwapped(marketBefore, marketAfter) {
|
|
73
|
-
return {
|
|
74
|
-
quoteAssetAmountSwapped: marketBefore.amm.quoteAssetReserve
|
|
75
|
-
.sub(marketAfter.amm.quoteAssetReserve)
|
|
76
|
-
.abs()
|
|
77
|
-
.mul(marketBefore.amm.pegMultiplier)
|
|
78
|
-
.div(numericConstants_1.PEG_PRECISION)
|
|
79
|
-
.div(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO),
|
|
80
|
-
baseAssetAmountSwapped: marketBefore.amm.baseAssetReserve.sub(marketAfter.amm.baseAssetReserve),
|
|
81
|
-
};
|
|
82
|
-
}
|
|
83
|
-
function calculateBaseAssetAmountMarketCanExecute(market, order, oraclePriceData) {
|
|
84
|
-
if ((0, types_1.isVariant)(order.orderType, 'limit')) {
|
|
85
|
-
return calculateAmountToTradeForLimit(market, order, oraclePriceData);
|
|
86
|
-
}
|
|
87
|
-
else if ((0, types_1.isVariant)(order.orderType, 'triggerLimit')) {
|
|
88
|
-
return calculateAmountToTradeForTriggerLimit(market, order);
|
|
89
|
-
}
|
|
90
|
-
else if ((0, types_1.isVariant)(order.orderType, 'market')) {
|
|
91
|
-
return numericConstants_1.ZERO;
|
|
92
|
-
}
|
|
93
|
-
else {
|
|
94
|
-
return calculateAmountToTradeForTriggerMarket(market, order);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
exports.calculateBaseAssetAmountMarketCanExecute = calculateBaseAssetAmountMarketCanExecute;
|
|
98
|
-
function calculateAmountToTradeForLimit(market, order, oraclePriceData) {
|
|
99
|
-
let limitPrice = order.price;
|
|
100
|
-
if (!order.oraclePriceOffset.eq(numericConstants_1.ZERO)) {
|
|
101
|
-
if (!oraclePriceData) {
|
|
102
|
-
throw Error('Cant calculate limit price for oracle offset oracle without OraclePriceData');
|
|
103
|
-
}
|
|
104
|
-
const floatingPrice = oraclePriceData.price.add(order.oraclePriceOffset);
|
|
105
|
-
if (order.postOnly) {
|
|
106
|
-
limitPrice = (0, types_1.isVariant)(order.direction, 'long')
|
|
107
|
-
? _1.BN.min(order.price, floatingPrice)
|
|
108
|
-
: _1.BN.max(order.price, floatingPrice);
|
|
109
|
-
}
|
|
110
|
-
else {
|
|
111
|
-
limitPrice = floatingPrice;
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
const [maxAmountToTrade, direction] = (0, amm_1.calculateMaxBaseAssetAmountToTrade)(market.amm, limitPrice, order.direction);
|
|
115
|
-
const baseAssetAmount = (0, _1.standardizeBaseAssetAmount)(maxAmountToTrade, market.amm.baseAssetAmountStepSize);
|
|
116
|
-
// Check that directions are the same
|
|
117
|
-
const sameDirection = isSameDirection(direction, order.direction);
|
|
118
|
-
if (!sameDirection) {
|
|
119
|
-
return numericConstants_1.ZERO;
|
|
120
|
-
}
|
|
121
|
-
return baseAssetAmount.gt(order.baseAssetAmount)
|
|
122
|
-
? order.baseAssetAmount
|
|
123
|
-
: baseAssetAmount;
|
|
124
|
-
}
|
|
125
|
-
exports.calculateAmountToTradeForLimit = calculateAmountToTradeForLimit;
|
|
126
|
-
function calculateAmountToTradeForTriggerLimit(market, order) {
|
|
127
|
-
if (!order.triggered) {
|
|
128
|
-
return numericConstants_1.ZERO;
|
|
129
|
-
}
|
|
130
|
-
return calculateAmountToTradeForLimit(market, order);
|
|
131
|
-
}
|
|
132
|
-
exports.calculateAmountToTradeForTriggerLimit = calculateAmountToTradeForTriggerLimit;
|
|
133
|
-
function isSameDirection(firstDirection, secondDirection) {
|
|
134
|
-
return (((0, types_1.isVariant)(firstDirection, 'long') && (0, types_1.isVariant)(secondDirection, 'long')) ||
|
|
135
|
-
((0, types_1.isVariant)(firstDirection, 'short') && (0, types_1.isVariant)(secondDirection, 'short')));
|
|
136
|
-
}
|
|
137
|
-
function calculateAmountToTradeForTriggerMarket(market, order) {
|
|
138
|
-
if (!order.triggered) {
|
|
139
|
-
return numericConstants_1.ZERO;
|
|
140
|
-
}
|
|
141
|
-
return order.baseAssetAmount;
|
|
142
|
-
}
|
package/src/orders.ts
DELETED
|
@@ -1,251 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
isVariant,
|
|
3
|
-
MarketAccount,
|
|
4
|
-
Order,
|
|
5
|
-
PositionDirection,
|
|
6
|
-
UserAccount,
|
|
7
|
-
UserPosition,
|
|
8
|
-
} from './types';
|
|
9
|
-
import { BN, standardizeBaseAssetAmount } from '.';
|
|
10
|
-
import { calculateNewMarketAfterTrade } from './math/market';
|
|
11
|
-
import {
|
|
12
|
-
AMM_TO_QUOTE_PRECISION_RATIO,
|
|
13
|
-
PEG_PRECISION,
|
|
14
|
-
ZERO,
|
|
15
|
-
} from './constants/numericConstants';
|
|
16
|
-
import { calculateMaxBaseAssetAmountToTrade } from './math/amm';
|
|
17
|
-
import {
|
|
18
|
-
findDirectionToClose,
|
|
19
|
-
positionCurrentDirection,
|
|
20
|
-
} from './math/position';
|
|
21
|
-
import { OraclePriceData } from '.';
|
|
22
|
-
|
|
23
|
-
export function calculateNewStateAfterOrder(
|
|
24
|
-
userAccount: UserAccount,
|
|
25
|
-
userPosition: UserPosition,
|
|
26
|
-
market: MarketAccount,
|
|
27
|
-
order: Order
|
|
28
|
-
): [UserAccount, UserPosition, MarketAccount] | null {
|
|
29
|
-
if (isVariant(order.status, 'init')) {
|
|
30
|
-
return null;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
const baseAssetAmountToTrade = calculateBaseAssetAmountMarketCanExecute(
|
|
34
|
-
market,
|
|
35
|
-
order
|
|
36
|
-
);
|
|
37
|
-
if (baseAssetAmountToTrade.lt(market.amm.baseAssetAmountStepSize)) {
|
|
38
|
-
return null;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
const userAccountAfter = Object.assign({}, userAccount);
|
|
42
|
-
const userPositionAfter = Object.assign({}, userPosition);
|
|
43
|
-
|
|
44
|
-
const currentPositionDirection = positionCurrentDirection(userPosition);
|
|
45
|
-
const increasePosition =
|
|
46
|
-
userPosition.baseAssetAmount.eq(ZERO) ||
|
|
47
|
-
isSameDirection(order.direction, currentPositionDirection);
|
|
48
|
-
|
|
49
|
-
if (increasePosition) {
|
|
50
|
-
const marketAfter = calculateNewMarketAfterTrade(
|
|
51
|
-
baseAssetAmountToTrade,
|
|
52
|
-
order.direction,
|
|
53
|
-
market
|
|
54
|
-
);
|
|
55
|
-
|
|
56
|
-
const { quoteAssetAmountSwapped, baseAssetAmountSwapped } =
|
|
57
|
-
calculateAmountSwapped(market, marketAfter);
|
|
58
|
-
|
|
59
|
-
userPositionAfter.baseAssetAmount = userPositionAfter.baseAssetAmount.add(
|
|
60
|
-
baseAssetAmountSwapped
|
|
61
|
-
);
|
|
62
|
-
userPositionAfter.quoteAssetAmount = userPositionAfter.quoteAssetAmount.add(
|
|
63
|
-
quoteAssetAmountSwapped
|
|
64
|
-
);
|
|
65
|
-
|
|
66
|
-
return [userAccountAfter, userPositionAfter, marketAfter];
|
|
67
|
-
} else {
|
|
68
|
-
const reversePosition = baseAssetAmountToTrade.gt(
|
|
69
|
-
userPosition.baseAssetAmount.abs()
|
|
70
|
-
);
|
|
71
|
-
|
|
72
|
-
if (reversePosition) {
|
|
73
|
-
const intermediateMarket = calculateNewMarketAfterTrade(
|
|
74
|
-
userPosition.baseAssetAmount,
|
|
75
|
-
findDirectionToClose(userPosition),
|
|
76
|
-
market
|
|
77
|
-
);
|
|
78
|
-
|
|
79
|
-
const { quoteAssetAmountSwapped: baseAssetValue } =
|
|
80
|
-
calculateAmountSwapped(market, intermediateMarket);
|
|
81
|
-
|
|
82
|
-
let pnl;
|
|
83
|
-
if (isVariant(currentPositionDirection, 'long')) {
|
|
84
|
-
pnl = baseAssetValue.sub(userPosition.quoteAssetAmount);
|
|
85
|
-
} else {
|
|
86
|
-
pnl = userPosition.quoteAssetAmount.sub(baseAssetValue);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
userAccountAfter.collateral = userAccountAfter.collateral.add(pnl);
|
|
90
|
-
|
|
91
|
-
const baseAssetAmountLeft = baseAssetAmountToTrade.sub(
|
|
92
|
-
userPosition.baseAssetAmount.abs()
|
|
93
|
-
);
|
|
94
|
-
|
|
95
|
-
const marketAfter = calculateNewMarketAfterTrade(
|
|
96
|
-
baseAssetAmountLeft,
|
|
97
|
-
order.direction,
|
|
98
|
-
intermediateMarket
|
|
99
|
-
);
|
|
100
|
-
|
|
101
|
-
const { quoteAssetAmountSwapped, baseAssetAmountSwapped } =
|
|
102
|
-
calculateAmountSwapped(intermediateMarket, marketAfter);
|
|
103
|
-
|
|
104
|
-
userPositionAfter.quoteAssetAmount = quoteAssetAmountSwapped;
|
|
105
|
-
userPositionAfter.baseAssetAmount = baseAssetAmountSwapped;
|
|
106
|
-
|
|
107
|
-
return [userAccountAfter, userPositionAfter, marketAfter];
|
|
108
|
-
} else {
|
|
109
|
-
const marketAfter = calculateNewMarketAfterTrade(
|
|
110
|
-
baseAssetAmountToTrade,
|
|
111
|
-
order.direction,
|
|
112
|
-
market
|
|
113
|
-
);
|
|
114
|
-
|
|
115
|
-
const {
|
|
116
|
-
quoteAssetAmountSwapped: baseAssetValue,
|
|
117
|
-
baseAssetAmountSwapped,
|
|
118
|
-
} = calculateAmountSwapped(market, marketAfter);
|
|
119
|
-
|
|
120
|
-
const costBasisRealized = userPosition.quoteAssetAmount
|
|
121
|
-
.mul(baseAssetAmountSwapped.abs())
|
|
122
|
-
.div(userPosition.baseAssetAmount.abs());
|
|
123
|
-
|
|
124
|
-
let pnl;
|
|
125
|
-
if (isVariant(currentPositionDirection, 'long')) {
|
|
126
|
-
pnl = baseAssetValue.sub(costBasisRealized);
|
|
127
|
-
} else {
|
|
128
|
-
pnl = costBasisRealized.sub(baseAssetValue);
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
userAccountAfter.collateral = userAccountAfter.collateral.add(pnl);
|
|
132
|
-
|
|
133
|
-
userPositionAfter.baseAssetAmount = userPositionAfter.baseAssetAmount.add(
|
|
134
|
-
baseAssetAmountSwapped
|
|
135
|
-
);
|
|
136
|
-
userPositionAfter.quoteAssetAmount =
|
|
137
|
-
userPositionAfter.quoteAssetAmount.sub(costBasisRealized);
|
|
138
|
-
|
|
139
|
-
return [userAccountAfter, userPositionAfter, marketAfter];
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
function calculateAmountSwapped(
|
|
145
|
-
marketBefore: MarketAccount,
|
|
146
|
-
marketAfter: MarketAccount
|
|
147
|
-
): { quoteAssetAmountSwapped: BN; baseAssetAmountSwapped: BN } {
|
|
148
|
-
return {
|
|
149
|
-
quoteAssetAmountSwapped: marketBefore.amm.quoteAssetReserve
|
|
150
|
-
.sub(marketAfter.amm.quoteAssetReserve)
|
|
151
|
-
.abs()
|
|
152
|
-
.mul(marketBefore.amm.pegMultiplier)
|
|
153
|
-
.div(PEG_PRECISION)
|
|
154
|
-
.div(AMM_TO_QUOTE_PRECISION_RATIO),
|
|
155
|
-
baseAssetAmountSwapped: marketBefore.amm.baseAssetReserve.sub(
|
|
156
|
-
marketAfter.amm.baseAssetReserve
|
|
157
|
-
),
|
|
158
|
-
};
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
export function calculateBaseAssetAmountMarketCanExecute(
|
|
162
|
-
market: MarketAccount,
|
|
163
|
-
order: Order,
|
|
164
|
-
oraclePriceData?: OraclePriceData
|
|
165
|
-
): BN {
|
|
166
|
-
if (isVariant(order.orderType, 'limit')) {
|
|
167
|
-
return calculateAmountToTradeForLimit(market, order, oraclePriceData);
|
|
168
|
-
} else if (isVariant(order.orderType, 'triggerLimit')) {
|
|
169
|
-
return calculateAmountToTradeForTriggerLimit(market, order);
|
|
170
|
-
} else if (isVariant(order.orderType, 'market')) {
|
|
171
|
-
return ZERO;
|
|
172
|
-
} else {
|
|
173
|
-
return calculateAmountToTradeForTriggerMarket(market, order);
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
export function calculateAmountToTradeForLimit(
|
|
178
|
-
market: MarketAccount,
|
|
179
|
-
order: Order,
|
|
180
|
-
oraclePriceData?: OraclePriceData
|
|
181
|
-
): BN {
|
|
182
|
-
let limitPrice = order.price;
|
|
183
|
-
if (!order.oraclePriceOffset.eq(ZERO)) {
|
|
184
|
-
if (!oraclePriceData) {
|
|
185
|
-
throw Error(
|
|
186
|
-
'Cant calculate limit price for oracle offset oracle without OraclePriceData'
|
|
187
|
-
);
|
|
188
|
-
}
|
|
189
|
-
const floatingPrice = oraclePriceData.price.add(order.oraclePriceOffset);
|
|
190
|
-
if (order.postOnly) {
|
|
191
|
-
limitPrice = isVariant(order.direction, 'long')
|
|
192
|
-
? BN.min(order.price, floatingPrice)
|
|
193
|
-
: BN.max(order.price, floatingPrice);
|
|
194
|
-
} else {
|
|
195
|
-
limitPrice = floatingPrice;
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
const [maxAmountToTrade, direction] = calculateMaxBaseAssetAmountToTrade(
|
|
200
|
-
market.amm,
|
|
201
|
-
limitPrice,
|
|
202
|
-
order.direction
|
|
203
|
-
);
|
|
204
|
-
|
|
205
|
-
const baseAssetAmount = standardizeBaseAssetAmount(
|
|
206
|
-
maxAmountToTrade,
|
|
207
|
-
market.amm.baseAssetAmountStepSize
|
|
208
|
-
);
|
|
209
|
-
|
|
210
|
-
// Check that directions are the same
|
|
211
|
-
const sameDirection = isSameDirection(direction, order.direction);
|
|
212
|
-
if (!sameDirection) {
|
|
213
|
-
return ZERO;
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
return baseAssetAmount.gt(order.baseAssetAmount)
|
|
217
|
-
? order.baseAssetAmount
|
|
218
|
-
: baseAssetAmount;
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
export function calculateAmountToTradeForTriggerLimit(
|
|
222
|
-
market: MarketAccount,
|
|
223
|
-
order: Order
|
|
224
|
-
): BN {
|
|
225
|
-
if (!order.triggered) {
|
|
226
|
-
return ZERO;
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
return calculateAmountToTradeForLimit(market, order);
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
function isSameDirection(
|
|
233
|
-
firstDirection: PositionDirection,
|
|
234
|
-
secondDirection: PositionDirection
|
|
235
|
-
): boolean {
|
|
236
|
-
return (
|
|
237
|
-
(isVariant(firstDirection, 'long') && isVariant(secondDirection, 'long')) ||
|
|
238
|
-
(isVariant(firstDirection, 'short') && isVariant(secondDirection, 'short'))
|
|
239
|
-
);
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
function calculateAmountToTradeForTriggerMarket(
|
|
243
|
-
market: MarketAccount,
|
|
244
|
-
order: Order
|
|
245
|
-
): BN {
|
|
246
|
-
if (!order.triggered) {
|
|
247
|
-
return ZERO;
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
return order.baseAssetAmount;
|
|
251
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"computeUnits.js","sourceRoot":"","sources":["computeUnits.ts"],"names":[],"mappings":";;;AAEO,KAAK,UAAU,0BAA0B,CAC/C,SAAoB,EACpB,UAAsB,EACtB,WAAmB,EACnB,aAAuB,WAAW;IAElC,MAAM,EAAE,GAAG,MAAM,UAAU,CAAC,cAAc,CAAC,WAAW,EAAE,EAAE,UAAU,EAAE,CAAC,CAAC;IACxE,MAAM,YAAY,GAAG,EAAE,CAAC;IACxB,MAAM,KAAK,GAAG,IAAI,MAAM,CACvB,WAAW,SAAS,CAAC,QAAQ,EAAE,gDAAgD,CAC/E,CAAC;IACF,EAAE,CAAC,IAAI,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC,UAAU,EAAE,EAAE;QAC1C,MAAM,KAAK,GAAG,UAAU,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;QACtC,IAAI,KAAK,IAAI,KAAK,CAAC,CAAC,CAAC,EAAE;YACtB,YAAY,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;SAC5B;IACF,CAAC,CAAC,CAAC;IACH,OAAO,YAAY,CAAC;AACrB,CAAC;AAlBD,gEAkBC"}
|