@drift-labs/sdk 0.1.18-orders.4 → 0.1.18-orders.5
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.
|
@@ -54,11 +54,14 @@ class PollingUserAccountSubscriber {
|
|
|
54
54
|
eventType: 'userPositionsData',
|
|
55
55
|
});
|
|
56
56
|
const userOrdersPublicKey = yield addresses_1.getUserOrdersAccountPublicKey(this.program.programId, userPublicKey);
|
|
57
|
-
this.
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
57
|
+
const userOrdersExist = (yield this.program.provider.connection.getParsedAccountInfo(userOrdersPublicKey)).value !== null;
|
|
58
|
+
if (userOrdersExist) {
|
|
59
|
+
this.accountsToPoll.set(userOrdersPublicKey.toString(), {
|
|
60
|
+
key: 'userOrders',
|
|
61
|
+
publicKey: userOrdersPublicKey,
|
|
62
|
+
eventType: 'userOrdersData',
|
|
63
|
+
});
|
|
64
|
+
}
|
|
62
65
|
for (const [_, accountToPoll] of this.accountsToPoll) {
|
|
63
66
|
accountToPoll.callbackId = this.accountLoader.addAccount(accountToPoll.publicKey, (buffer) => {
|
|
64
67
|
const account = this.program.account[accountToPoll.key].coder.accounts.decode(utils_1.capitalize(accountToPoll.key), buffer);
|
package/lib/orders.d.ts
CHANGED
|
@@ -3,4 +3,4 @@ import { Market, Order, UserAccount, UserPosition } from './types';
|
|
|
3
3
|
import { BN } from '.';
|
|
4
4
|
export declare function calculateNewStateAfterOrder(userAccount: UserAccount, userPosition: UserPosition, market: Market, order: Order): [UserAccount, UserPosition, Market] | null;
|
|
5
5
|
export declare function calculateAmountToTradeForLimit(market: Market, order: Order): BN;
|
|
6
|
-
export declare function
|
|
6
|
+
export declare function calculateAmountToTradeForTriggerLimit(market: Market, order: Order): BN;
|
package/lib/orders.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.calculateAmountToTradeForTriggerLimit = exports.calculateAmountToTradeForLimit = exports.calculateNewStateAfterOrder = void 0;
|
|
4
4
|
const types_1 = require("./types");
|
|
5
5
|
const market_1 = require("./math/market");
|
|
6
6
|
const numericConstants_1 = require("./constants/numericConstants");
|
|
@@ -83,15 +83,15 @@ function calculateAmountToTrade(market, order) {
|
|
|
83
83
|
if (types_1.isVariant(order.orderType, 'limit')) {
|
|
84
84
|
return calculateAmountToTradeForLimit(market, order);
|
|
85
85
|
}
|
|
86
|
-
else if (types_1.isVariant(order.orderType, '
|
|
87
|
-
return
|
|
86
|
+
else if (types_1.isVariant(order.orderType, 'triggerLimit')) {
|
|
87
|
+
return calculateAmountToTradeForTriggerLimit(market, order);
|
|
88
88
|
}
|
|
89
89
|
else if (types_1.isVariant(order.orderType, 'market')) {
|
|
90
90
|
// should never be a market order queued
|
|
91
91
|
return numericConstants_1.ZERO;
|
|
92
92
|
}
|
|
93
93
|
else {
|
|
94
|
-
return
|
|
94
|
+
return calculateAmountToTradeForTriggerMarket(market, order);
|
|
95
95
|
}
|
|
96
96
|
}
|
|
97
97
|
function calculateAmountToTradeForLimit(market, order) {
|
|
@@ -106,21 +106,21 @@ function calculateAmountToTradeForLimit(market, order) {
|
|
|
106
106
|
: maxAmountToTrade;
|
|
107
107
|
}
|
|
108
108
|
exports.calculateAmountToTradeForLimit = calculateAmountToTradeForLimit;
|
|
109
|
-
function
|
|
109
|
+
function calculateAmountToTradeForTriggerLimit(market, order) {
|
|
110
110
|
if (order.baseAssetAmountFilled.eq(numericConstants_1.ZERO)) {
|
|
111
|
-
const baseAssetAmount =
|
|
111
|
+
const baseAssetAmount = calculateAmountToTradeForTriggerMarket(market, order);
|
|
112
112
|
if (baseAssetAmount.eq(numericConstants_1.ZERO)) {
|
|
113
113
|
return numericConstants_1.ZERO;
|
|
114
114
|
}
|
|
115
115
|
}
|
|
116
116
|
return calculateAmountToTradeForLimit(market, order);
|
|
117
117
|
}
|
|
118
|
-
exports.
|
|
118
|
+
exports.calculateAmountToTradeForTriggerLimit = calculateAmountToTradeForTriggerLimit;
|
|
119
119
|
function isSameDirection(firstDirection, secondDirection) {
|
|
120
120
|
return ((types_1.isVariant(firstDirection, 'long') && types_1.isVariant(secondDirection, 'long')) ||
|
|
121
121
|
(types_1.isVariant(firstDirection, 'short') && types_1.isVariant(secondDirection, 'short')));
|
|
122
122
|
}
|
|
123
|
-
function
|
|
123
|
+
function calculateAmountToTradeForTriggerMarket(market, order) {
|
|
124
124
|
return isTriggerConditionSatisfied(market, order)
|
|
125
125
|
? order.baseAssetAmount
|
|
126
126
|
: numericConstants_1.ZERO;
|
package/package.json
CHANGED
|
@@ -89,11 +89,19 @@ export class PollingUserAccountSubscriber implements UserAccountSubscriber {
|
|
|
89
89
|
userPublicKey
|
|
90
90
|
);
|
|
91
91
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
92
|
+
const userOrdersExist =
|
|
93
|
+
(
|
|
94
|
+
await this.program.provider.connection.getParsedAccountInfo(
|
|
95
|
+
userOrdersPublicKey
|
|
96
|
+
)
|
|
97
|
+
).value !== null;
|
|
98
|
+
if (userOrdersExist) {
|
|
99
|
+
this.accountsToPoll.set(userOrdersPublicKey.toString(), {
|
|
100
|
+
key: 'userOrders',
|
|
101
|
+
publicKey: userOrdersPublicKey,
|
|
102
|
+
eventType: 'userOrdersData',
|
|
103
|
+
});
|
|
104
|
+
}
|
|
97
105
|
|
|
98
106
|
for (const [_, accountToPoll] of this.accountsToPoll) {
|
|
99
107
|
accountToPoll.callbackId = this.accountLoader.addAccount(
|
package/src/orders.ts
CHANGED
|
@@ -160,13 +160,13 @@ function calculateAmountSwapped(
|
|
|
160
160
|
function calculateAmountToTrade(market: Market, order: Order): BN {
|
|
161
161
|
if (isVariant(order.orderType, 'limit')) {
|
|
162
162
|
return calculateAmountToTradeForLimit(market, order);
|
|
163
|
-
} else if (isVariant(order.orderType, '
|
|
164
|
-
return
|
|
163
|
+
} else if (isVariant(order.orderType, 'triggerLimit')) {
|
|
164
|
+
return calculateAmountToTradeForTriggerLimit(market, order);
|
|
165
165
|
} else if (isVariant(order.orderType, 'market')) {
|
|
166
166
|
// should never be a market order queued
|
|
167
167
|
return ZERO;
|
|
168
168
|
} else {
|
|
169
|
-
return
|
|
169
|
+
return calculateAmountToTradeForTriggerMarket(market, order);
|
|
170
170
|
}
|
|
171
171
|
}
|
|
172
172
|
|
|
@@ -190,12 +190,15 @@ export function calculateAmountToTradeForLimit(
|
|
|
190
190
|
: maxAmountToTrade;
|
|
191
191
|
}
|
|
192
192
|
|
|
193
|
-
export function
|
|
193
|
+
export function calculateAmountToTradeForTriggerLimit(
|
|
194
194
|
market: Market,
|
|
195
195
|
order: Order
|
|
196
196
|
): BN {
|
|
197
197
|
if (order.baseAssetAmountFilled.eq(ZERO)) {
|
|
198
|
-
const baseAssetAmount =
|
|
198
|
+
const baseAssetAmount = calculateAmountToTradeForTriggerMarket(
|
|
199
|
+
market,
|
|
200
|
+
order
|
|
201
|
+
);
|
|
199
202
|
if (baseAssetAmount.eq(ZERO)) {
|
|
200
203
|
return ZERO;
|
|
201
204
|
}
|
|
@@ -214,7 +217,10 @@ function isSameDirection(
|
|
|
214
217
|
);
|
|
215
218
|
}
|
|
216
219
|
|
|
217
|
-
function
|
|
220
|
+
function calculateAmountToTradeForTriggerMarket(
|
|
221
|
+
market: Market,
|
|
222
|
+
order: Order
|
|
223
|
+
): BN {
|
|
218
224
|
return isTriggerConditionSatisfied(market, order)
|
|
219
225
|
? order.baseAssetAmount
|
|
220
226
|
: ZERO;
|