@drift-labs/sdk 0.2.0-master.4 → 0.2.0-master.41
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/README.md +27 -27
- package/lib/accounts/bulkAccountLoader.d.ts +2 -0
- package/lib/accounts/bulkAccountLoader.js +36 -29
- package/lib/accounts/bulkUserStatsSubscription.d.ts +7 -0
- package/lib/accounts/bulkUserStatsSubscription.js +21 -0
- package/lib/accounts/bulkUserSubscription.d.ts +2 -2
- 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/{pollingClearingHouseAccountSubscriber.d.ts → pollingDriftClientAccountSubscriber.d.ts} +20 -25
- package/lib/accounts/{pollingClearingHouseAccountSubscriber.js → pollingDriftClientAccountSubscriber.js} +45 -49
- package/lib/accounts/{pollingOracleSubscriber.d.ts → pollingOracleAccountSubscriber.d.ts} +2 -2
- package/lib/accounts/{pollingOracleSubscriber.js → pollingOracleAccountSubscriber.js} +3 -3
- package/lib/accounts/pollingUserAccountSubscriber.d.ts +1 -1
- package/lib/accounts/pollingUserAccountSubscriber.js +5 -11
- package/lib/accounts/pollingUserStatsAccountSubscriber.d.ts +27 -0
- package/lib/accounts/pollingUserStatsAccountSubscriber.js +107 -0
- package/lib/accounts/types.d.ts +26 -15
- package/lib/accounts/webSocketDriftClientAccountSubscriber.d.ts +49 -0
- package/lib/accounts/{webSocketClearingHouseAccountSubscriber.js → webSocketDriftClientAccountSubscriber.js} +47 -45
- package/lib/accounts/webSocketUserStatsAccountSubsriber.d.ts +20 -0
- package/lib/accounts/webSocketUserStatsAccountSubsriber.js +47 -0
- package/lib/addresses/marketAddresses.d.ts +1 -3
- package/lib/addresses/marketAddresses.js +1 -1
- package/lib/addresses/pda.d.ts +15 -9
- package/lib/addresses/pda.js +73 -35
- package/lib/adminClient.d.ts +65 -0
- package/lib/adminClient.js +637 -0
- package/lib/config.d.ts +9 -9
- package/lib/config.js +25 -21
- package/lib/constants/numericConstants.d.ts +30 -12
- package/lib/constants/numericConstants.js +41 -21
- package/lib/constants/perpMarkets.d.ts +18 -0
- package/lib/constants/{markets.js → perpMarkets.js} +7 -7
- package/lib/constants/spotMarkets.d.ts +19 -0
- package/lib/constants/spotMarkets.js +53 -0
- package/lib/dlob/DLOB.d.ts +82 -0
- package/lib/dlob/DLOB.js +694 -0
- package/lib/dlob/DLOBNode.d.ts +54 -0
- package/lib/dlob/DLOBNode.js +77 -0
- package/lib/dlob/NodeList.d.ts +27 -0
- package/lib/dlob/NodeList.js +144 -0
- package/lib/driftClient.d.ts +234 -0
- package/lib/driftClient.js +2108 -0
- package/lib/{clearingHouseConfig.d.ts → driftClientConfig.d.ts} +9 -9
- package/lib/{clearingHouseConfig.js → driftClientConfig.js} +0 -0
- package/lib/events/eventList.js +3 -0
- package/lib/events/eventSubscriber.d.ts +5 -2
- package/lib/events/eventSubscriber.js +25 -11
- package/lib/events/fetchLogs.d.ts +13 -2
- package/lib/events/fetchLogs.js +40 -12
- package/lib/events/pollingLogProvider.d.ts +2 -1
- package/lib/events/pollingLogProvider.js +7 -3
- package/lib/events/sort.js +8 -11
- package/lib/events/types.d.ts +11 -3
- package/lib/events/types.js +8 -0
- package/lib/events/webSocketLogProvider.js +1 -1
- package/lib/examples/makeTradeExample.js +30 -18
- package/lib/factory/bigNum.d.ts +8 -4
- package/lib/factory/bigNum.js +109 -19
- package/lib/idl/drift.json +8386 -0
- package/lib/idl/{mock_usdc_faucet.json → token_faucet.json} +46 -23
- package/lib/index.d.ts +30 -13
- package/lib/index.js +30 -13
- package/lib/math/amm.d.ts +9 -6
- package/lib/math/amm.js +91 -38
- package/lib/math/conversion.js +1 -1
- package/lib/math/exchangeStatus.d.ts +4 -0
- package/lib/math/exchangeStatus.js +18 -0
- package/lib/math/funding.d.ts +6 -6
- package/lib/math/funding.js +23 -21
- package/lib/math/insurance.d.ts +4 -0
- package/lib/math/insurance.js +27 -0
- package/lib/math/margin.d.ts +11 -0
- package/lib/math/margin.js +82 -0
- package/lib/math/market.d.ts +14 -9
- package/lib/math/market.js +70 -10
- package/lib/math/oracles.d.ts +4 -0
- package/lib/math/oracles.js +36 -8
- package/lib/math/orders.d.ts +16 -6
- package/lib/math/orders.js +97 -17
- package/lib/math/position.d.ts +27 -13
- package/lib/math/position.js +92 -36
- package/lib/math/repeg.js +17 -8
- package/lib/math/spotBalance.d.ts +22 -0
- package/lib/math/spotBalance.js +192 -0
- package/lib/math/spotMarket.d.ts +4 -0
- package/lib/math/spotMarket.js +8 -0
- package/lib/math/spotPosition.d.ts +6 -0
- package/lib/math/spotPosition.js +23 -0
- package/lib/math/trade.d.ts +10 -10
- package/lib/math/trade.js +27 -31
- package/lib/oracles/pythClient.js +1 -1
- package/lib/oracles/quoteAssetOracleClient.js +1 -1
- package/lib/oracles/switchboardClient.js +1 -1
- package/lib/orderParams.d.ts +4 -4
- package/lib/orderParams.js +12 -4
- package/lib/serum/serumFulfillmentConfigMap.d.ts +10 -0
- package/lib/serum/serumFulfillmentConfigMap.js +17 -0
- package/lib/serum/serumSubscriber.d.ts +27 -0
- package/lib/serum/serumSubscriber.js +56 -0
- package/lib/serum/types.d.ts +11 -0
- package/lib/{clearingHouseUserConfig.js → serum/types.js} +0 -0
- 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.d.ts +1 -1
- package/lib/tx/retryTxSender.js +13 -4
- package/lib/tx/types.d.ts +1 -1
- package/lib/tx/utils.js +1 -1
- package/lib/types.d.ts +688 -216
- package/lib/types.js +153 -24
- package/lib/user.d.ts +228 -0
- package/lib/user.js +959 -0
- package/lib/userConfig.d.ts +14 -0
- package/lib/userConfig.js +2 -0
- package/lib/userMap/userMap.d.ts +41 -0
- package/lib/userMap/userMap.js +85 -0
- package/lib/userMap/userStatsMap.d.ts +19 -0
- package/lib/userMap/userStatsMap.js +68 -0
- package/lib/userName.d.ts +1 -0
- package/lib/userName.js +3 -2
- package/lib/userStats.d.ts +18 -0
- package/lib/userStats.js +49 -0
- package/lib/userStatsConfig.d.ts +14 -0
- package/lib/userStatsConfig.js +2 -0
- 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 +11 -4
- package/src/accounts/bulkAccountLoader.ts +44 -34
- package/src/accounts/bulkUserStatsSubscription.ts +33 -0
- package/src/accounts/bulkUserSubscription.ts +2 -3
- package/src/accounts/fetch.ts +27 -2
- package/src/accounts/{pollingClearingHouseAccountSubscriber.ts → pollingDriftClientAccountSubscriber.ts} +65 -75
- package/src/accounts/{pollingOracleSubscriber.ts → pollingOracleAccountSubscriber.ts} +2 -2
- package/src/accounts/pollingUserAccountSubscriber.ts +5 -12
- package/src/accounts/pollingUserStatsAccountSubscriber.ts +166 -0
- package/src/accounts/types.ts +35 -15
- package/src/accounts/{webSocketClearingHouseAccountSubscriber.ts → webSocketDriftClientAccountSubscriber.ts} +78 -73
- package/src/accounts/webSocketUserStatsAccountSubsriber.ts +80 -0
- package/src/addresses/marketAddresses.ts +3 -4
- package/src/addresses/pda.ts +105 -33
- package/src/adminClient.ts +1207 -0
- package/src/assert/assert.js +9 -0
- package/src/config.ts +37 -31
- package/src/constants/numericConstants.ts +58 -24
- package/src/constants/{markets.ts → perpMarkets.ts} +10 -10
- package/src/constants/spotMarkets.ts +73 -0
- package/src/dlob/DLOB.ts +1120 -0
- package/src/dlob/DLOBNode.ts +155 -0
- package/src/dlob/NodeList.ts +195 -0
- package/src/driftClient.ts +3594 -0
- package/src/{clearingHouseConfig.ts → driftClientConfig.ts} +9 -8
- package/src/events/eventList.js +77 -0
- package/src/events/eventList.ts +3 -0
- package/src/events/eventSubscriber.ts +36 -14
- package/src/events/fetchLogs.ts +55 -13
- package/src/events/pollingLogProvider.ts +11 -3
- package/src/events/sort.ts +11 -15
- package/src/events/types.ts +27 -2
- package/src/events/webSocketLogProvider.ts +1 -1
- package/src/examples/makeTradeExample.js +157 -0
- package/src/examples/makeTradeExample.ts +44 -28
- package/src/factory/bigNum.ts +150 -22
- package/src/idl/drift.json +8386 -0
- package/src/idl/pyth.json +98 -2
- package/src/idl/{mock_usdc_faucet.json → token_faucet.json} +46 -23
- package/src/index.ts +30 -13
- package/src/math/amm.ts +161 -48
- package/src/math/conversion.ts +2 -2
- package/src/math/exchangeStatus.ts +31 -0
- package/src/math/funding.ts +41 -31
- package/src/math/insurance.ts +35 -0
- package/src/math/margin.ts +133 -0
- package/src/math/market.ts +143 -14
- package/src/math/oracles.ts +63 -9
- package/src/math/orders.ts +168 -26
- package/src/math/position.ts +136 -58
- package/src/math/repeg.ts +19 -9
- package/src/math/spotBalance.ts +319 -0
- package/src/math/spotMarket.ts +9 -0
- package/src/math/spotPosition.ts +47 -0
- package/src/math/trade.ts +33 -37
- package/src/oracles/pythClient.ts +2 -2
- package/src/oracles/quoteAssetOracleClient.ts +2 -2
- package/src/oracles/switchboardClient.ts +2 -2
- package/src/orderParams.ts +16 -8
- package/src/serum/serumFulfillmentConfigMap.ts +26 -0
- package/src/serum/serumSubscriber.ts +99 -0
- package/src/serum/types.ts +13 -0
- package/src/slot/SlotSubscriber.ts +11 -1
- package/src/token/index.js +38 -0
- package/src/{mockUSDCFaucet.ts → tokenFaucet.ts} +82 -70
- package/src/tx/retryTxSender.ts +16 -5
- package/src/tx/types.js +2 -0
- package/src/tx/types.ts +2 -1
- package/src/tx/utils.js +17 -0
- package/src/tx/utils.ts +1 -1
- package/src/types.ts +650 -189
- package/src/user.ts +1599 -0
- package/src/{clearingHouseUserConfig.ts → userConfig.ts} +5 -5
- package/src/userMap/userMap.ts +124 -0
- package/src/userMap/userStatsMap.ts +108 -0
- package/src/userName.ts +2 -1
- package/src/userStats.ts +75 -0
- package/src/userStatsConfig.ts +18 -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/tests/bn/test.ts +46 -11
- package/tests/dlob/helpers.ts +620 -0
- package/tests/dlob/test.ts +4586 -0
- package/yarn-error.log +3160 -0
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +0 -49
- package/lib/admin.d.ts +0 -44
- package/lib/admin.js +0 -433
- package/lib/clearingHouse.d.ts +0 -133
- package/lib/clearingHouse.js +0 -931
- package/lib/clearingHouseUser.d.ts +0 -187
- package/lib/clearingHouseUser.js +0 -643
- package/lib/clearingHouseUserConfig.d.ts +0 -14
- package/lib/constants/banks.d.ts +0 -16
- package/lib/constants/banks.js +0 -34
- package/lib/constants/markets.d.ts +0 -19
- package/lib/idl/clearing_house.json +0 -3998
- package/lib/math/bankBalance.d.ts +0 -9
- package/lib/math/bankBalance.js +0 -75
- package/lib/math/state.d.ts +0 -8
- package/lib/math/state.js +0 -15
- package/lib/orders.d.ts +0 -8
- package/lib/orders.js +0 -134
- package/src/admin.ts +0 -722
- package/src/clearingHouse.ts +0 -1451
- package/src/clearingHouseUser.ts +0 -989
- package/src/constants/banks.ts +0 -43
- package/src/idl/clearing_house.json +0 -3998
- package/src/math/bankBalance.ts +0 -112
- package/src/math/state.ts +0 -14
- package/src/math/utils.js +0 -27
- package/src/math/utils.js.map +0 -1
- package/src/orders.ts +0 -244
- package/src/util/computeUnits.js.map +0 -1
package/lib/dlob/DLOB.js
ADDED
|
@@ -0,0 +1,694 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DLOB = void 0;
|
|
4
|
+
const NodeList_1 = require("./NodeList");
|
|
5
|
+
const __1 = require("..");
|
|
6
|
+
const exchangeStatus_1 = require("../math/exchangeStatus");
|
|
7
|
+
class DLOB {
|
|
8
|
+
/**
|
|
9
|
+
*
|
|
10
|
+
* @param perpMarkets The perp markets to maintain a DLOB for
|
|
11
|
+
* @param spotMarkets The spot markets to maintain a DLOB for
|
|
12
|
+
* @param userMap map of all users
|
|
13
|
+
* @param silent set to true to prevent logging on inserts and removals
|
|
14
|
+
*/
|
|
15
|
+
constructor(perpMarkets, spotMarkets, stateAccount, userMap, silent) {
|
|
16
|
+
this.openOrders = new Map();
|
|
17
|
+
this.orderLists = new Map();
|
|
18
|
+
this.marketIndexToAccount = new Map();
|
|
19
|
+
this.silent = false;
|
|
20
|
+
this.initialized = false;
|
|
21
|
+
this.stateAccount = stateAccount;
|
|
22
|
+
this.userMap = userMap;
|
|
23
|
+
this.silent = silent;
|
|
24
|
+
this.openOrders.set('perp', new Set());
|
|
25
|
+
this.openOrders.set('spot', new Set());
|
|
26
|
+
this.orderLists.set('perp', new Map());
|
|
27
|
+
this.orderLists.set('spot', new Map());
|
|
28
|
+
this.marketIndexToAccount.set('perp', new Map());
|
|
29
|
+
this.marketIndexToAccount.set('spot', new Map());
|
|
30
|
+
for (const market of perpMarkets) {
|
|
31
|
+
const marketIndex = market.marketIndex;
|
|
32
|
+
this.marketIndexToAccount.get('perp').set(marketIndex, market);
|
|
33
|
+
this.orderLists.get('perp').set(marketIndex, {
|
|
34
|
+
limit: {
|
|
35
|
+
ask: new NodeList_1.NodeList('limit', 'asc'),
|
|
36
|
+
bid: new NodeList_1.NodeList('limit', 'desc'),
|
|
37
|
+
},
|
|
38
|
+
floatingLimit: {
|
|
39
|
+
ask: new NodeList_1.NodeList('floatingLimit', 'asc'),
|
|
40
|
+
bid: new NodeList_1.NodeList('floatingLimit', 'desc'),
|
|
41
|
+
},
|
|
42
|
+
market: {
|
|
43
|
+
ask: new NodeList_1.NodeList('market', 'asc'),
|
|
44
|
+
bid: new NodeList_1.NodeList('market', 'asc'), // always sort ascending for market orders
|
|
45
|
+
},
|
|
46
|
+
trigger: {
|
|
47
|
+
above: new NodeList_1.NodeList('trigger', 'asc'),
|
|
48
|
+
below: new NodeList_1.NodeList('trigger', 'desc'),
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
for (const market of spotMarkets) {
|
|
53
|
+
const marketIndex = market.marketIndex;
|
|
54
|
+
this.marketIndexToAccount.get('spot').set(marketIndex, market);
|
|
55
|
+
this.orderLists.get('spot').set(marketIndex, {
|
|
56
|
+
limit: {
|
|
57
|
+
ask: new NodeList_1.NodeList('limit', 'asc'),
|
|
58
|
+
bid: new NodeList_1.NodeList('limit', 'desc'),
|
|
59
|
+
},
|
|
60
|
+
floatingLimit: {
|
|
61
|
+
ask: new NodeList_1.NodeList('floatingLimit', 'asc'),
|
|
62
|
+
bid: new NodeList_1.NodeList('floatingLimit', 'desc'),
|
|
63
|
+
},
|
|
64
|
+
market: {
|
|
65
|
+
ask: new NodeList_1.NodeList('market', 'asc'),
|
|
66
|
+
bid: new NodeList_1.NodeList('market', 'asc'), // always sort ascending for market orders
|
|
67
|
+
},
|
|
68
|
+
trigger: {
|
|
69
|
+
above: new NodeList_1.NodeList('trigger', 'asc'),
|
|
70
|
+
below: new NodeList_1.NodeList('trigger', 'desc'),
|
|
71
|
+
},
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
clear() {
|
|
76
|
+
for (const marketType of this.openOrders.keys()) {
|
|
77
|
+
this.openOrders.get(marketType).clear();
|
|
78
|
+
}
|
|
79
|
+
this.openOrders.clear();
|
|
80
|
+
for (const marketType of this.orderLists.keys()) {
|
|
81
|
+
for (const marketIndex of this.orderLists.get(marketType).keys()) {
|
|
82
|
+
const marketNodeLists = this.orderLists
|
|
83
|
+
.get(marketType)
|
|
84
|
+
.get(marketIndex);
|
|
85
|
+
for (const side of Object.keys(marketNodeLists)) {
|
|
86
|
+
for (const orderType of Object.keys(marketNodeLists[side])) {
|
|
87
|
+
marketNodeLists[side][orderType].clear();
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
this.orderLists.clear();
|
|
93
|
+
for (const marketType of this.marketIndexToAccount.keys()) {
|
|
94
|
+
this.marketIndexToAccount.get(marketType).clear();
|
|
95
|
+
}
|
|
96
|
+
this.marketIndexToAccount.clear();
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* initializes a new DLOB instance
|
|
100
|
+
*
|
|
101
|
+
* @returns a promise that resolves when the DLOB is initialized
|
|
102
|
+
*/
|
|
103
|
+
async init() {
|
|
104
|
+
if (this.initialized) {
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
// initialize the dlob with the user map (prevents hitting getProgramAccounts)
|
|
108
|
+
for (const user of this.userMap.values()) {
|
|
109
|
+
const userAccount = user.getUserAccount();
|
|
110
|
+
const userAccountPubkey = user.getUserAccountPublicKey();
|
|
111
|
+
for (const order of userAccount.orders) {
|
|
112
|
+
this.insertOrder(order, userAccountPubkey);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
this.initialized = true;
|
|
116
|
+
return true;
|
|
117
|
+
}
|
|
118
|
+
insertOrder(order, userAccount, onInsert) {
|
|
119
|
+
var _a;
|
|
120
|
+
if ((0, __1.isVariant)(order.status, 'init')) {
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
const marketType = (0, __1.getVariant)(order.marketType);
|
|
124
|
+
if ((0, __1.isVariant)(order.status, 'open')) {
|
|
125
|
+
this.openOrders
|
|
126
|
+
.get(marketType)
|
|
127
|
+
.add((0, NodeList_1.getOrderSignature)(order.orderId, userAccount));
|
|
128
|
+
}
|
|
129
|
+
(_a = this.getListForOrder(order)) === null || _a === void 0 ? void 0 : _a.insert(order, marketType, this.marketIndexToAccount.get(marketType).get(order.marketIndex), userAccount);
|
|
130
|
+
if (onInsert) {
|
|
131
|
+
onInsert();
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
trigger(order, userAccount, onTrigger) {
|
|
135
|
+
var _a;
|
|
136
|
+
if ((0, __1.isVariant)(order.status, 'init')) {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
const marketType = (0, __1.getVariant)(order.marketType);
|
|
140
|
+
const triggerList = this.orderLists.get(marketType).get(order.marketIndex)
|
|
141
|
+
.trigger[(0, __1.isVariant)(order.triggerCondition, 'above') ? 'above' : 'below'];
|
|
142
|
+
triggerList.remove(order, userAccount);
|
|
143
|
+
(_a = this.getListForOrder(order)) === null || _a === void 0 ? void 0 : _a.insert(order, marketType, this.marketIndexToAccount.get(marketType).get(order.marketIndex), userAccount);
|
|
144
|
+
if (onTrigger) {
|
|
145
|
+
onTrigger();
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
getListForOrder(order) {
|
|
149
|
+
const isInactiveTriggerOrder = (0, __1.mustBeTriggered)(order) && !(0, __1.isTriggered)(order);
|
|
150
|
+
let type;
|
|
151
|
+
if (isInactiveTriggerOrder) {
|
|
152
|
+
type = 'trigger';
|
|
153
|
+
}
|
|
154
|
+
else if ((0, __1.isOneOfVariant)(order.orderType, ['market', 'triggerMarket'])) {
|
|
155
|
+
type = 'market';
|
|
156
|
+
}
|
|
157
|
+
else if (order.oraclePriceOffset !== 0) {
|
|
158
|
+
type = 'floatingLimit';
|
|
159
|
+
}
|
|
160
|
+
else {
|
|
161
|
+
type = 'limit';
|
|
162
|
+
}
|
|
163
|
+
let subType;
|
|
164
|
+
if (isInactiveTriggerOrder) {
|
|
165
|
+
subType = (0, __1.isVariant)(order.triggerCondition, 'above') ? 'above' : 'below';
|
|
166
|
+
}
|
|
167
|
+
else {
|
|
168
|
+
subType = (0, __1.isVariant)(order.direction, 'long') ? 'bid' : 'ask';
|
|
169
|
+
}
|
|
170
|
+
const marketType = (0, __1.getVariant)(order.marketType);
|
|
171
|
+
if (!this.orderLists.has(marketType)) {
|
|
172
|
+
return undefined;
|
|
173
|
+
}
|
|
174
|
+
return this.orderLists.get(marketType).get(order.marketIndex)[type][subType];
|
|
175
|
+
}
|
|
176
|
+
findNodesToFill(marketIndex, fallbackBid, fallbackAsk, slot, ts, marketType, oraclePriceData) {
|
|
177
|
+
const marketAccount = this.marketIndexToAccount
|
|
178
|
+
.get((0, __1.getVariant)(marketType))
|
|
179
|
+
.get(marketIndex);
|
|
180
|
+
if ((0, exchangeStatus_1.fillPaused)(this.stateAccount, marketAccount)) {
|
|
181
|
+
return [];
|
|
182
|
+
}
|
|
183
|
+
// Find all the crossing nodes
|
|
184
|
+
const crossingNodesToFill = this.findCrossingNodesToFill(marketIndex, slot, marketType, oraclePriceData);
|
|
185
|
+
let fallbackCrossingNodesToFill = new Array();
|
|
186
|
+
if (!(0, exchangeStatus_1.ammPaused)(this.stateAccount, marketAccount)) {
|
|
187
|
+
fallbackCrossingNodesToFill = this.findFallbackCrossingNodesToFill(marketIndex, fallbackBid, fallbackAsk, slot, marketType, oraclePriceData);
|
|
188
|
+
}
|
|
189
|
+
// get expired market nodes
|
|
190
|
+
const expiredNodesToFill = this.findExpiredNodesToFill(marketIndex, ts, marketType);
|
|
191
|
+
return crossingNodesToFill.concat(fallbackCrossingNodesToFill, expiredNodesToFill);
|
|
192
|
+
}
|
|
193
|
+
findCrossingNodesToFill(marketIndex, slot, marketType, oraclePriceData) {
|
|
194
|
+
const nodesToFill = new Array();
|
|
195
|
+
const askGenerator = this.getAsks(marketIndex, undefined, // dont include vask
|
|
196
|
+
slot, marketType, oraclePriceData);
|
|
197
|
+
const bidGenerator = this.getBids(marketIndex, undefined, // dont include vbid
|
|
198
|
+
slot, marketType, oraclePriceData);
|
|
199
|
+
let nextAsk = askGenerator.next();
|
|
200
|
+
let nextBid = bidGenerator.next();
|
|
201
|
+
// First try to find orders that cross
|
|
202
|
+
while (!nextAsk.done && !nextBid.done) {
|
|
203
|
+
const { crossingNodes, exhaustedSide } = this.findCrossingOrders(nextAsk.value, nextBid.value, oraclePriceData, slot);
|
|
204
|
+
if (exhaustedSide === 'bid') {
|
|
205
|
+
nextBid = bidGenerator.next();
|
|
206
|
+
}
|
|
207
|
+
else if (exhaustedSide === 'ask') {
|
|
208
|
+
nextAsk = askGenerator.next();
|
|
209
|
+
}
|
|
210
|
+
else if (exhaustedSide === 'both') {
|
|
211
|
+
nextBid = bidGenerator.next();
|
|
212
|
+
nextAsk = askGenerator.next();
|
|
213
|
+
}
|
|
214
|
+
else if (exhaustedSide === 'nocross') {
|
|
215
|
+
break;
|
|
216
|
+
}
|
|
217
|
+
else {
|
|
218
|
+
console.error(`invalid exhaustedSide: ${exhaustedSide}`);
|
|
219
|
+
break;
|
|
220
|
+
}
|
|
221
|
+
for (const crossingNode of crossingNodes) {
|
|
222
|
+
nodesToFill.push(crossingNode);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
return nodesToFill;
|
|
226
|
+
}
|
|
227
|
+
findFallbackCrossingNodesToFill(marketIndex, fallbackBid, fallbackAsk, slot, marketType, oraclePriceData) {
|
|
228
|
+
var _a, _b;
|
|
229
|
+
const nodesToFill = new Array();
|
|
230
|
+
const askGenerator = this.getAsks(marketIndex, undefined, // dont include vask
|
|
231
|
+
slot, marketType, oraclePriceData);
|
|
232
|
+
const bidGenerator = this.getBids(marketIndex, undefined, // dont include vbid
|
|
233
|
+
slot, marketType, oraclePriceData);
|
|
234
|
+
let nextAsk = askGenerator.next();
|
|
235
|
+
let nextBid = bidGenerator.next();
|
|
236
|
+
// check for asks that cross fallbackBid
|
|
237
|
+
while (!nextAsk.done && fallbackBid !== undefined) {
|
|
238
|
+
const askNode = nextAsk.value;
|
|
239
|
+
if ((0, __1.isVariant)(marketType, 'spot') && ((_a = askNode.order) === null || _a === void 0 ? void 0 : _a.postOnly)) {
|
|
240
|
+
nextAsk = askGenerator.next();
|
|
241
|
+
continue;
|
|
242
|
+
}
|
|
243
|
+
const askLimitPrice = (0, __1.getOptionalLimitPrice)(askNode.order, oraclePriceData, slot);
|
|
244
|
+
// order crosses if there is no limit price or it crosses fallback price
|
|
245
|
+
const crosses = askLimitPrice === undefined || askLimitPrice.lte(fallbackBid);
|
|
246
|
+
// fallback is available if auction is complete or it's a spot order
|
|
247
|
+
const fallbackAvailable = (0, __1.isVariant)(marketType, 'spot') || (0, __1.isAuctionComplete)(askNode.order, slot);
|
|
248
|
+
if (crosses && fallbackAvailable) {
|
|
249
|
+
nodesToFill.push({
|
|
250
|
+
node: askNode,
|
|
251
|
+
makerNode: undefined, // filled by fallback
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
nextAsk = askGenerator.next();
|
|
255
|
+
}
|
|
256
|
+
// check for bids that cross fallbackAsk
|
|
257
|
+
while (!nextBid.done && fallbackAsk !== undefined) {
|
|
258
|
+
const bidNode = nextBid.value;
|
|
259
|
+
if ((0, __1.isVariant)(marketType, 'spot') && ((_b = bidNode.order) === null || _b === void 0 ? void 0 : _b.postOnly)) {
|
|
260
|
+
nextBid = bidGenerator.next();
|
|
261
|
+
continue;
|
|
262
|
+
}
|
|
263
|
+
const bidLimitPrice = (0, __1.getOptionalLimitPrice)(bidNode.order, oraclePriceData, slot);
|
|
264
|
+
// order crosses if there is no limit price or it crosses fallback price
|
|
265
|
+
const crosses = bidLimitPrice === undefined || bidLimitPrice.gte(fallbackAsk);
|
|
266
|
+
// fallback is available if auction is complete or it's a spot order
|
|
267
|
+
const fallbackAvailable = (0, __1.isVariant)(marketType, 'spot') || (0, __1.isAuctionComplete)(bidNode.order, slot);
|
|
268
|
+
if (crosses && fallbackAvailable) {
|
|
269
|
+
nodesToFill.push({
|
|
270
|
+
node: bidNode,
|
|
271
|
+
makerNode: undefined, // filled by fallback
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
nextBid = bidGenerator.next();
|
|
275
|
+
}
|
|
276
|
+
return nodesToFill;
|
|
277
|
+
}
|
|
278
|
+
findExpiredNodesToFill(marketIndex, ts, marketType) {
|
|
279
|
+
const nodesToFill = new Array();
|
|
280
|
+
const marketTypeStr = (0, __1.getVariant)(marketType);
|
|
281
|
+
const nodeLists = this.orderLists.get(marketTypeStr).get(marketIndex);
|
|
282
|
+
// All bids/asks that can expire
|
|
283
|
+
const bidGenerators = [
|
|
284
|
+
nodeLists.limit.bid.getGenerator(),
|
|
285
|
+
nodeLists.floatingLimit.bid.getGenerator(),
|
|
286
|
+
nodeLists.market.bid.getGenerator(),
|
|
287
|
+
];
|
|
288
|
+
const askGenerators = [
|
|
289
|
+
nodeLists.limit.ask.getGenerator(),
|
|
290
|
+
nodeLists.floatingLimit.ask.getGenerator(),
|
|
291
|
+
nodeLists.market.ask.getGenerator(),
|
|
292
|
+
];
|
|
293
|
+
for (const bidGenerator of bidGenerators) {
|
|
294
|
+
for (const bid of bidGenerator) {
|
|
295
|
+
if ((0, __1.isOrderExpired)(bid.order, ts)) {
|
|
296
|
+
nodesToFill.push({
|
|
297
|
+
node: bid,
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
for (const askGenerator of askGenerators) {
|
|
303
|
+
for (const ask of askGenerator) {
|
|
304
|
+
if ((0, __1.isOrderExpired)(ask.order, ts)) {
|
|
305
|
+
nodesToFill.push({
|
|
306
|
+
node: ask,
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
return nodesToFill;
|
|
312
|
+
}
|
|
313
|
+
findJitAuctionNodesToFill(marketIndex, slot, marketType) {
|
|
314
|
+
const nodesToFill = new Array();
|
|
315
|
+
// Then see if there are orders still in JIT auction
|
|
316
|
+
for (const marketBid of this.getMarketBids(marketIndex, marketType)) {
|
|
317
|
+
if (!(0, __1.isAuctionComplete)(marketBid.order, slot)) {
|
|
318
|
+
nodesToFill.push({
|
|
319
|
+
node: marketBid,
|
|
320
|
+
});
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
for (const marketAsk of this.getMarketAsks(marketIndex, marketType)) {
|
|
324
|
+
if (!(0, __1.isAuctionComplete)(marketAsk.order, slot)) {
|
|
325
|
+
nodesToFill.push({
|
|
326
|
+
node: marketAsk,
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
return nodesToFill;
|
|
331
|
+
}
|
|
332
|
+
getMarketBids(marketIndex, marketType) {
|
|
333
|
+
const marketTypeStr = (0, __1.getVariant)(marketType);
|
|
334
|
+
return this.orderLists
|
|
335
|
+
.get(marketTypeStr)
|
|
336
|
+
.get(marketIndex)
|
|
337
|
+
.market.bid.getGenerator();
|
|
338
|
+
}
|
|
339
|
+
getMarketAsks(marketIndex, marketType) {
|
|
340
|
+
const marketTypeStr = (0, __1.getVariant)(marketType);
|
|
341
|
+
return this.orderLists
|
|
342
|
+
.get(marketTypeStr)
|
|
343
|
+
.get(marketIndex)
|
|
344
|
+
.market.ask.getGenerator();
|
|
345
|
+
}
|
|
346
|
+
*getAsks(marketIndex, fallbackAsk, slot, marketType, oraclePriceData) {
|
|
347
|
+
if ((0, __1.isVariant)(marketType, 'spot') && !oraclePriceData) {
|
|
348
|
+
throw new Error('Must provide OraclePriceData to get spot asks');
|
|
349
|
+
}
|
|
350
|
+
const marketTypeStr = (0, __1.getVariant)(marketType);
|
|
351
|
+
const nodeLists = this.orderLists.get(marketTypeStr).get(marketIndex);
|
|
352
|
+
const generatorList = [
|
|
353
|
+
nodeLists.limit.ask.getGenerator(),
|
|
354
|
+
nodeLists.floatingLimit.ask.getGenerator(),
|
|
355
|
+
nodeLists.market.ask.getGenerator(),
|
|
356
|
+
];
|
|
357
|
+
if (marketTypeStr === 'perp' && fallbackAsk) {
|
|
358
|
+
generatorList.push((0, NodeList_1.getVammNodeGenerator)(fallbackAsk));
|
|
359
|
+
}
|
|
360
|
+
if (generatorList.length === 0) {
|
|
361
|
+
throw new Error('No ask generators found');
|
|
362
|
+
}
|
|
363
|
+
const askGenerators = generatorList.map((generator) => {
|
|
364
|
+
return {
|
|
365
|
+
next: generator.next(),
|
|
366
|
+
generator,
|
|
367
|
+
};
|
|
368
|
+
});
|
|
369
|
+
let asksExhausted = false;
|
|
370
|
+
while (!asksExhausted) {
|
|
371
|
+
const bestGenerator = askGenerators.reduce((bestGenerator, currentGenerator) => {
|
|
372
|
+
if (currentGenerator.next.done) {
|
|
373
|
+
return bestGenerator;
|
|
374
|
+
}
|
|
375
|
+
if (bestGenerator.next.done) {
|
|
376
|
+
return currentGenerator;
|
|
377
|
+
}
|
|
378
|
+
const bestAskPrice = bestGenerator.next.value.getPrice(oraclePriceData, slot);
|
|
379
|
+
const currentAskPrice = currentGenerator.next.value.getPrice(oraclePriceData, slot);
|
|
380
|
+
return bestAskPrice.lt(currentAskPrice)
|
|
381
|
+
? bestGenerator
|
|
382
|
+
: currentGenerator;
|
|
383
|
+
});
|
|
384
|
+
if (!bestGenerator.next.done) {
|
|
385
|
+
// skip this node if it's already completely filled
|
|
386
|
+
if (bestGenerator.next.value.isBaseFilled()) {
|
|
387
|
+
bestGenerator.next = bestGenerator.generator.next();
|
|
388
|
+
continue;
|
|
389
|
+
}
|
|
390
|
+
// skip order if user is being liquidated/bankrupt
|
|
391
|
+
if (bestGenerator.next.value.userAccount !== undefined) {
|
|
392
|
+
const user = this.userMap.get(bestGenerator.next.value.userAccount.toString());
|
|
393
|
+
if (user === null || user === void 0 ? void 0 : user.isBeingLiquidated()) {
|
|
394
|
+
bestGenerator.next = bestGenerator.generator.next();
|
|
395
|
+
continue;
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
yield bestGenerator.next.value;
|
|
399
|
+
bestGenerator.next = bestGenerator.generator.next();
|
|
400
|
+
}
|
|
401
|
+
else {
|
|
402
|
+
asksExhausted = true;
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
*getBids(marketIndex, fallbackBid, slot, marketType, oraclePriceData) {
|
|
407
|
+
if ((0, __1.isVariant)(marketType, 'spot') && !oraclePriceData) {
|
|
408
|
+
throw new Error('Must provide OraclePriceData to get spot bids');
|
|
409
|
+
}
|
|
410
|
+
const marketTypeStr = (0, __1.getVariant)(marketType);
|
|
411
|
+
const nodeLists = this.orderLists.get(marketTypeStr).get(marketIndex);
|
|
412
|
+
const generatorList = [
|
|
413
|
+
nodeLists.limit.bid.getGenerator(),
|
|
414
|
+
nodeLists.floatingLimit.bid.getGenerator(),
|
|
415
|
+
nodeLists.market.bid.getGenerator(),
|
|
416
|
+
];
|
|
417
|
+
if (marketTypeStr === 'perp' && fallbackBid) {
|
|
418
|
+
generatorList.push((0, NodeList_1.getVammNodeGenerator)(fallbackBid));
|
|
419
|
+
}
|
|
420
|
+
if (generatorList.length === 0) {
|
|
421
|
+
throw new Error('No bid generators found');
|
|
422
|
+
}
|
|
423
|
+
const bidGenerators = generatorList.map((generator) => {
|
|
424
|
+
return {
|
|
425
|
+
next: generator.next(),
|
|
426
|
+
generator,
|
|
427
|
+
};
|
|
428
|
+
});
|
|
429
|
+
let bidsExhausted = false; // there will always be the fallbackBid
|
|
430
|
+
while (!bidsExhausted) {
|
|
431
|
+
const bestGenerator = bidGenerators.reduce((bestGenerator, currentGenerator) => {
|
|
432
|
+
if (currentGenerator.next.done) {
|
|
433
|
+
return bestGenerator;
|
|
434
|
+
}
|
|
435
|
+
if (bestGenerator.next.done) {
|
|
436
|
+
return currentGenerator;
|
|
437
|
+
}
|
|
438
|
+
const bestBidPrice = bestGenerator.next.value.getPrice(oraclePriceData, slot);
|
|
439
|
+
const currentBidPrice = currentGenerator.next.value.getPrice(oraclePriceData, slot);
|
|
440
|
+
return bestBidPrice.gt(currentBidPrice)
|
|
441
|
+
? bestGenerator
|
|
442
|
+
: currentGenerator;
|
|
443
|
+
});
|
|
444
|
+
if (!bestGenerator.next.done) {
|
|
445
|
+
// skip this node if it's already completely filled
|
|
446
|
+
if (bestGenerator.next.value.isBaseFilled()) {
|
|
447
|
+
bestGenerator.next = bestGenerator.generator.next();
|
|
448
|
+
continue;
|
|
449
|
+
}
|
|
450
|
+
// skip order if user is being liquidated/bankrupt
|
|
451
|
+
if (bestGenerator.next.value.userAccount !== undefined) {
|
|
452
|
+
const user = this.userMap.get(bestGenerator.next.value.userAccount.toString());
|
|
453
|
+
if (user === null || user === void 0 ? void 0 : user.isBeingLiquidated()) {
|
|
454
|
+
bestGenerator.next = bestGenerator.generator.next();
|
|
455
|
+
continue;
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
yield bestGenerator.next.value;
|
|
459
|
+
bestGenerator.next = bestGenerator.generator.next();
|
|
460
|
+
}
|
|
461
|
+
else {
|
|
462
|
+
bidsExhausted = true;
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
findCrossingOrders(askNode, bidNode, oraclePriceData, slot) {
|
|
467
|
+
const bidPrice = bidNode.getPrice(oraclePriceData, slot);
|
|
468
|
+
const askPrice = askNode.getPrice(oraclePriceData, slot);
|
|
469
|
+
// orders don't cross - we're done walkin gup the book
|
|
470
|
+
if (bidPrice.lt(askPrice)) {
|
|
471
|
+
return {
|
|
472
|
+
crossingNodes: [],
|
|
473
|
+
exhaustedSide: 'nocross',
|
|
474
|
+
};
|
|
475
|
+
}
|
|
476
|
+
const bidOrder = bidNode.order;
|
|
477
|
+
const askOrder = askNode.order;
|
|
478
|
+
const bidUserAuthority = this.userMap.getUserAuthority(bidNode.userAccount.toString());
|
|
479
|
+
const askUserAuthority = this.userMap.getUserAuthority(askNode.userAccount.toString());
|
|
480
|
+
// Can't match orders from the same authority
|
|
481
|
+
const sameAuthority = bidUserAuthority.equals(askUserAuthority);
|
|
482
|
+
if (sameAuthority || (bidOrder.postOnly && askOrder.postOnly)) {
|
|
483
|
+
// don't have a principle way to pick which one to exhaust,
|
|
484
|
+
// exhaust each one 50% of the time so we can try each one against other orders
|
|
485
|
+
const exhaustedSide = Math.random() < 0.5 ? 'bid' : 'ask';
|
|
486
|
+
return {
|
|
487
|
+
crossingNodes: [],
|
|
488
|
+
exhaustedSide,
|
|
489
|
+
};
|
|
490
|
+
}
|
|
491
|
+
const { takerNode, makerNode, makerSide } = this.determineMakerAndTaker(askNode, bidNode);
|
|
492
|
+
// If maker is market order and auction is complete, cant be maker
|
|
493
|
+
if ((0, __1.isMarketOrder)(makerNode.order) &&
|
|
494
|
+
(0, __1.isAuctionComplete)(makerNode.order, slot)) {
|
|
495
|
+
return {
|
|
496
|
+
crossingNodes: [],
|
|
497
|
+
exhaustedSide: makerSide,
|
|
498
|
+
};
|
|
499
|
+
}
|
|
500
|
+
const bidBaseRemaining = bidOrder.baseAssetAmount.sub(bidOrder.baseAssetAmountFilled);
|
|
501
|
+
const askBaseRemaining = askOrder.baseAssetAmount.sub(askOrder.baseAssetAmountFilled);
|
|
502
|
+
let exhaustedSide;
|
|
503
|
+
if (bidBaseRemaining.eq(askBaseRemaining)) {
|
|
504
|
+
exhaustedSide = 'both';
|
|
505
|
+
}
|
|
506
|
+
else if (bidBaseRemaining.gt(askBaseRemaining)) {
|
|
507
|
+
exhaustedSide = 'ask';
|
|
508
|
+
}
|
|
509
|
+
else {
|
|
510
|
+
exhaustedSide = 'bid';
|
|
511
|
+
}
|
|
512
|
+
// update the orders on DLOB as if they were fill - so we don't try to match them on the next iteration
|
|
513
|
+
// NOTE: if something goes wrong during the actual fill (transaction fails, i.e. due to orders already being filled)
|
|
514
|
+
// then we risk having a mismatch between this local DLOB and the actual DLOB state on the blockchain. This isn't
|
|
515
|
+
// a problem in the current implementation because we construct a new DLOB from the blockchain state every time, rather
|
|
516
|
+
// than updating the existing DLOB based on events.
|
|
517
|
+
if (exhaustedSide === 'ask') {
|
|
518
|
+
// bid partially filled
|
|
519
|
+
const newBidOrder = { ...bidOrder };
|
|
520
|
+
newBidOrder.baseAssetAmountFilled =
|
|
521
|
+
bidOrder.baseAssetAmountFilled.add(askBaseRemaining);
|
|
522
|
+
this.getListForOrder(newBidOrder).update(newBidOrder, bidNode.userAccount);
|
|
523
|
+
// ask completely filled
|
|
524
|
+
const newAskOrder = { ...askOrder };
|
|
525
|
+
newAskOrder.baseAssetAmountFilled = askOrder.baseAssetAmount;
|
|
526
|
+
this.getListForOrder(newAskOrder).update(newAskOrder, askNode.userAccount);
|
|
527
|
+
}
|
|
528
|
+
else if (exhaustedSide === 'bid') {
|
|
529
|
+
// ask partially filled
|
|
530
|
+
const newAskOrder = { ...askOrder };
|
|
531
|
+
newAskOrder.baseAssetAmountFilled =
|
|
532
|
+
askOrder.baseAssetAmountFilled.add(bidBaseRemaining);
|
|
533
|
+
this.getListForOrder(newAskOrder).update(newAskOrder, askNode.userAccount);
|
|
534
|
+
// bid completely filled
|
|
535
|
+
const newBidOrder = { ...bidOrder };
|
|
536
|
+
newBidOrder.baseAssetAmountFilled = bidOrder.baseAssetAmount;
|
|
537
|
+
this.getListForOrder(newBidOrder).update(newBidOrder, bidNode.userAccount);
|
|
538
|
+
}
|
|
539
|
+
else {
|
|
540
|
+
// both completely filled
|
|
541
|
+
const newBidOrder = { ...bidOrder };
|
|
542
|
+
newBidOrder.baseAssetAmountFilled = bidOrder.baseAssetAmount;
|
|
543
|
+
this.getListForOrder(newBidOrder).update(newBidOrder, bidNode.userAccount);
|
|
544
|
+
const newAskOrder = { ...askOrder };
|
|
545
|
+
newAskOrder.baseAssetAmountFilled = askOrder.baseAssetAmount;
|
|
546
|
+
this.getListForOrder(newAskOrder).update(newAskOrder, askNode.userAccount);
|
|
547
|
+
}
|
|
548
|
+
return {
|
|
549
|
+
crossingNodes: [
|
|
550
|
+
{
|
|
551
|
+
node: takerNode,
|
|
552
|
+
makerNode: makerNode,
|
|
553
|
+
},
|
|
554
|
+
],
|
|
555
|
+
exhaustedSide,
|
|
556
|
+
};
|
|
557
|
+
}
|
|
558
|
+
determineMakerAndTaker(askNode, bidNode) {
|
|
559
|
+
if (bidNode.order.postOnly) {
|
|
560
|
+
return {
|
|
561
|
+
takerNode: askNode,
|
|
562
|
+
makerNode: bidNode,
|
|
563
|
+
makerSide: 'bid',
|
|
564
|
+
};
|
|
565
|
+
}
|
|
566
|
+
else if (askNode.order.postOnly) {
|
|
567
|
+
return {
|
|
568
|
+
takerNode: bidNode,
|
|
569
|
+
makerNode: askNode,
|
|
570
|
+
makerSide: 'ask',
|
|
571
|
+
};
|
|
572
|
+
}
|
|
573
|
+
else if ((0, __1.isMarketOrder)(bidNode.order) && (0, __1.isLimitOrder)(askNode.order)) {
|
|
574
|
+
return {
|
|
575
|
+
takerNode: bidNode,
|
|
576
|
+
makerNode: askNode,
|
|
577
|
+
makerSide: 'ask',
|
|
578
|
+
};
|
|
579
|
+
}
|
|
580
|
+
else if ((0, __1.isMarketOrder)(askNode.order) && (0, __1.isLimitOrder)(bidNode.order)) {
|
|
581
|
+
return {
|
|
582
|
+
takerNode: askNode,
|
|
583
|
+
makerNode: bidNode,
|
|
584
|
+
makerSide: 'bid',
|
|
585
|
+
};
|
|
586
|
+
}
|
|
587
|
+
else if (askNode.order.slot.lt(bidNode.order.slot)) {
|
|
588
|
+
return {
|
|
589
|
+
takerNode: bidNode,
|
|
590
|
+
makerNode: askNode,
|
|
591
|
+
makerSide: 'ask',
|
|
592
|
+
};
|
|
593
|
+
}
|
|
594
|
+
else {
|
|
595
|
+
return {
|
|
596
|
+
takerNode: askNode,
|
|
597
|
+
makerNode: bidNode,
|
|
598
|
+
makerSide: 'bid',
|
|
599
|
+
};
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
getBestAsk(marketIndex, fallbackAsk, slot, marketType, oraclePriceData) {
|
|
603
|
+
return this.getAsks(marketIndex, fallbackAsk, slot, marketType, oraclePriceData)
|
|
604
|
+
.next()
|
|
605
|
+
.value.getPrice(oraclePriceData, slot);
|
|
606
|
+
}
|
|
607
|
+
getBestBid(marketIndex, fallbackBid, slot, marketType, oraclePriceData) {
|
|
608
|
+
return this.getBids(marketIndex, fallbackBid, slot, marketType, oraclePriceData)
|
|
609
|
+
.next()
|
|
610
|
+
.value.getPrice(oraclePriceData, slot);
|
|
611
|
+
}
|
|
612
|
+
findNodesToTrigger(marketIndex, slot, oraclePrice, marketType) {
|
|
613
|
+
if ((0, exchangeStatus_1.exchangePaused)(this.stateAccount)) {
|
|
614
|
+
return [];
|
|
615
|
+
}
|
|
616
|
+
const nodesToTrigger = [];
|
|
617
|
+
const marketTypeStr = (0, __1.getVariant)(marketType);
|
|
618
|
+
for (const node of this.orderLists
|
|
619
|
+
.get(marketTypeStr)
|
|
620
|
+
.get(marketIndex)
|
|
621
|
+
.trigger.above.getGenerator()) {
|
|
622
|
+
if (oraclePrice.gt(node.order.triggerPrice)) {
|
|
623
|
+
if ((0, __1.isAuctionComplete)(node.order, slot)) {
|
|
624
|
+
nodesToTrigger.push({
|
|
625
|
+
node: node,
|
|
626
|
+
});
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
else {
|
|
630
|
+
break;
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
for (const node of this.orderLists
|
|
634
|
+
.get(marketTypeStr)
|
|
635
|
+
.get(marketIndex)
|
|
636
|
+
.trigger.below.getGenerator()) {
|
|
637
|
+
if (oraclePrice.lt(node.order.triggerPrice)) {
|
|
638
|
+
if ((0, __1.isAuctionComplete)(node.order, slot)) {
|
|
639
|
+
nodesToTrigger.push({
|
|
640
|
+
node: node,
|
|
641
|
+
});
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
else {
|
|
645
|
+
break;
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
return nodesToTrigger;
|
|
649
|
+
}
|
|
650
|
+
printTopOfOrderLists(sdkConfig, driftClient, slotSubscriber, marketIndex, marketType) {
|
|
651
|
+
if ((0, __1.isVariant)(marketType, 'perp')) {
|
|
652
|
+
const market = driftClient.getPerpMarketAccount(marketIndex);
|
|
653
|
+
const slot = slotSubscriber.getSlot();
|
|
654
|
+
const oraclePriceData = driftClient.getOracleDataForPerpMarket(marketIndex);
|
|
655
|
+
const fallbackAsk = (0, __1.calculateAskPrice)(market, oraclePriceData);
|
|
656
|
+
const fallbackBid = (0, __1.calculateBidPrice)(market, oraclePriceData);
|
|
657
|
+
const bestAsk = this.getBestAsk(marketIndex, fallbackAsk, slot, marketType, oraclePriceData);
|
|
658
|
+
const bestBid = this.getBestBid(marketIndex, fallbackBid, slot, marketType, oraclePriceData);
|
|
659
|
+
const mid = bestAsk.add(bestBid).div(new __1.BN(2));
|
|
660
|
+
const bidSpread = ((0, __1.convertToNumber)(bestBid, __1.PRICE_PRECISION) /
|
|
661
|
+
(0, __1.convertToNumber)(oraclePriceData.price, __1.PRICE_PRECISION) -
|
|
662
|
+
1) *
|
|
663
|
+
100.0;
|
|
664
|
+
const askSpread = ((0, __1.convertToNumber)(bestAsk, __1.PRICE_PRECISION) /
|
|
665
|
+
(0, __1.convertToNumber)(oraclePriceData.price, __1.PRICE_PRECISION) -
|
|
666
|
+
1) *
|
|
667
|
+
100.0;
|
|
668
|
+
console.log(`Market ${sdkConfig.MARKETS[marketIndex].symbol} Orders`);
|
|
669
|
+
console.log(` Ask`, (0, __1.convertToNumber)(bestAsk, __1.PRICE_PRECISION).toFixed(3), `(${askSpread.toFixed(4)}%)`);
|
|
670
|
+
console.log(` Mid`, (0, __1.convertToNumber)(mid, __1.PRICE_PRECISION).toFixed(3));
|
|
671
|
+
console.log(` Bid`, (0, __1.convertToNumber)(bestBid, __1.PRICE_PRECISION).toFixed(3), `(${bidSpread.toFixed(4)}%)`);
|
|
672
|
+
}
|
|
673
|
+
else if ((0, __1.isVariant)(marketType, 'spot')) {
|
|
674
|
+
const slot = slotSubscriber.getSlot();
|
|
675
|
+
const oraclePriceData = driftClient.getOracleDataForPerpMarket(marketIndex);
|
|
676
|
+
const bestAsk = this.getBestAsk(marketIndex, undefined, slot, marketType, oraclePriceData);
|
|
677
|
+
const bestBid = this.getBestBid(marketIndex, undefined, slot, marketType, oraclePriceData);
|
|
678
|
+
const mid = bestAsk.add(bestBid).div(new __1.BN(2));
|
|
679
|
+
const bidSpread = ((0, __1.convertToNumber)(bestBid, __1.PRICE_PRECISION) /
|
|
680
|
+
(0, __1.convertToNumber)(oraclePriceData.price, __1.PRICE_PRECISION) -
|
|
681
|
+
1) *
|
|
682
|
+
100.0;
|
|
683
|
+
const askSpread = ((0, __1.convertToNumber)(bestAsk, __1.PRICE_PRECISION) /
|
|
684
|
+
(0, __1.convertToNumber)(oraclePriceData.price, __1.PRICE_PRECISION) -
|
|
685
|
+
1) *
|
|
686
|
+
100.0;
|
|
687
|
+
console.log(`Market ${sdkConfig.MARKETS[marketIndex].symbol} Orders`);
|
|
688
|
+
console.log(` Ask`, (0, __1.convertToNumber)(bestAsk, __1.PRICE_PRECISION).toFixed(3), `(${askSpread.toFixed(4)}%)`);
|
|
689
|
+
console.log(` Mid`, (0, __1.convertToNumber)(mid, __1.PRICE_PRECISION).toFixed(3));
|
|
690
|
+
console.log(` Bid`, (0, __1.convertToNumber)(bestBid, __1.PRICE_PRECISION).toFixed(3), `(${bidSpread.toFixed(4)}%)`);
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
exports.DLOB = DLOB;
|