@drift-labs/sdk 0.1.18-orders.1 → 0.1.19-master.2
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 +2 -1
- package/lib/accounts/defaultClearingHouseAccountSubscriber.d.ts +4 -7
- package/lib/accounts/defaultClearingHouseAccountSubscriber.d.ts.map +1 -0
- package/lib/accounts/defaultClearingHouseAccountSubscriber.js +2 -27
- package/lib/accounts/defaultUserAccountSubscriber.d.ts +2 -3
- package/lib/accounts/defaultUserAccountSubscriber.d.ts.map +1 -0
- package/lib/accounts/defaultUserAccountSubscriber.js +5 -14
- package/lib/accounts/types.d.ts +6 -32
- package/lib/accounts/types.d.ts.map +1 -0
- package/lib/accounts/webSocketAccountSubscriber.d.ts +1 -0
- package/lib/accounts/webSocketAccountSubscriber.d.ts.map +1 -0
- package/lib/addresses.d.ts +1 -4
- package/lib/addresses.d.ts.map +1 -0
- package/lib/addresses.js +1 -28
- package/lib/admin.d.ts +7 -10
- package/lib/admin.d.ts.map +1 -0
- package/lib/admin.js +64 -53
- package/lib/assert/assert.d.ts +1 -0
- package/lib/assert/assert.d.ts.map +1 -0
- package/lib/clearingHouse.d.ts +3 -22
- package/lib/clearingHouse.d.ts.map +1 -0
- package/lib/clearingHouse.js +15 -246
- package/lib/clearingHouseUser.d.ts +17 -10
- package/lib/clearingHouseUser.d.ts.map +1 -0
- package/lib/clearingHouseUser.js +97 -111
- package/lib/config.d.ts +1 -0
- package/lib/config.d.ts.map +1 -0
- package/lib/config.js +1 -1
- package/lib/constants/markets.d.ts +1 -0
- package/lib/constants/markets.d.ts.map +1 -0
- package/lib/constants/markets.js +16 -0
- package/lib/constants/numericConstants.d.ts +1 -0
- package/lib/constants/numericConstants.d.ts.map +1 -0
- package/lib/examples/makeTradeExample.d.ts +1 -0
- package/lib/examples/makeTradeExample.d.ts.map +1 -0
- package/lib/examples/makeTradeExample.js +6 -6
- package/lib/idl/clearing_house.json +233 -911
- package/lib/index.d.ts +1 -4
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +0 -4
- package/lib/math/amm.d.ts +1 -1
- package/lib/math/amm.d.ts.map +1 -0
- package/lib/math/amm.js +15 -38
- package/lib/math/conversion.d.ts +1 -0
- package/lib/math/conversion.d.ts.map +1 -0
- package/lib/math/conversion.js +1 -1
- package/lib/math/funding.d.ts +1 -0
- package/lib/math/funding.d.ts.map +1 -0
- package/lib/math/funding.js +5 -2
- package/lib/math/insuranceFund.d.ts +1 -0
- package/lib/math/insuranceFund.d.ts.map +1 -0
- package/lib/math/market.d.ts +2 -2
- package/lib/math/market.d.ts.map +1 -0
- package/lib/math/market.js +2 -12
- package/lib/math/position.d.ts +2 -4
- package/lib/math/position.d.ts.map +1 -0
- package/lib/math/position.js +5 -19
- package/lib/math/trade.d.ts +1 -0
- package/lib/math/trade.d.ts.map +1 -0
- package/lib/math/trade.js +16 -16
- package/lib/math/utils.d.ts +1 -0
- package/lib/math/utils.d.ts.map +1 -0
- package/lib/mockUSDCFaucet.d.ts +1 -0
- package/lib/mockUSDCFaucet.d.ts.map +1 -0
- package/lib/pythClient.d.ts +1 -0
- package/lib/pythClient.d.ts.map +1 -0
- package/lib/pythClient.js +1 -1
- package/lib/tx/defaultTxSender.d.ts +1 -0
- package/lib/tx/defaultTxSender.d.ts.map +1 -0
- package/lib/tx/types.d.ts +1 -0
- package/lib/tx/types.d.ts.map +1 -0
- package/lib/tx/utils.d.ts +1 -0
- package/lib/tx/utils.d.ts.map +1 -0
- package/lib/types.d.ts +9 -140
- package/lib/types.d.ts.map +1 -0
- package/lib/types.js +1 -36
- package/lib/util/computeUnits.d.ts +1 -0
- package/lib/util/computeUnits.d.ts.map +1 -0
- package/lib/util/tps.d.ts +1 -0
- package/lib/util/tps.d.ts.map +1 -0
- package/lib/wallet.d.ts +1 -0
- package/lib/wallet.d.ts.map +1 -0
- package/package.json +1 -1
- package/src/accounts/defaultClearingHouseAccountSubscriber.ts +5 -52
- package/src/accounts/defaultUserAccountSubscriber.ts +6 -31
- package/src/accounts/types.ts +5 -42
- package/src/addresses.ts +0 -35
- package/src/admin.ts +71 -86
- package/src/clearingHouse.ts +7 -340
- package/src/clearingHouseUser.ts +102 -154
- package/src/config.ts +1 -1
- package/src/constants/markets.ts +16 -0
- package/src/idl/clearing_house.json +233 -911
- package/src/index.ts +0 -4
- package/src/math/amm.ts +14 -47
- package/src/math/funding.ts +5 -1
- package/src/math/market.ts +2 -28
- package/src/math/position.ts +3 -23
- package/src/types.ts +8 -124
- package/tsconfig.json +1 -0
- package/lib/accounts/defaultHistoryAccountSubscriber.d.ts +0 -28
- package/lib/accounts/defaultHistoryAccountSubscriber.js +0 -110
- package/lib/math/orders.d.ts +0 -3
- package/lib/math/orders.js +0 -30
- package/lib/orderParams.d.ts +0 -7
- package/lib/orderParams.js +0 -88
- package/lib/orders.d.ts +0 -5
- package/lib/orders.js +0 -136
- package/src/accounts/defaultHistoryAccountSubscriber.ts +0 -176
- package/src/math/orders.ts +0 -39
- package/src/orderParams.ts +0 -128
- package/src/orders.ts +0 -230
package/src/orders.ts
DELETED
|
@@ -1,230 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
isVariant,
|
|
3
|
-
Market,
|
|
4
|
-
Order,
|
|
5
|
-
PositionDirection,
|
|
6
|
-
UserAccount,
|
|
7
|
-
UserPosition,
|
|
8
|
-
} from './types';
|
|
9
|
-
import BN from 'bn.js';
|
|
10
|
-
import {
|
|
11
|
-
calculateMarkPrice,
|
|
12
|
-
calculateNewMarketAfterTrade,
|
|
13
|
-
} from './math/market';
|
|
14
|
-
import {
|
|
15
|
-
AMM_TO_QUOTE_PRECISION_RATIO,
|
|
16
|
-
PEG_PRECISION,
|
|
17
|
-
ZERO,
|
|
18
|
-
} from './constants/numericConstants';
|
|
19
|
-
import { calculateMaxBaseAssetAmountToTrade } from './math/amm';
|
|
20
|
-
import {
|
|
21
|
-
findDirectionToClose,
|
|
22
|
-
positionCurrentDirection,
|
|
23
|
-
} from './math/position';
|
|
24
|
-
|
|
25
|
-
export function calculateNewStateAfterOrder(
|
|
26
|
-
userAccount: UserAccount,
|
|
27
|
-
userPosition: UserPosition,
|
|
28
|
-
market: Market,
|
|
29
|
-
order: Order
|
|
30
|
-
): [UserAccount, UserPosition, Market] | null {
|
|
31
|
-
if (isVariant(order.status, 'init')) {
|
|
32
|
-
return null;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
const baseAssetAmountToTrade = calculateAmountToTrade(market, order);
|
|
36
|
-
if (baseAssetAmountToTrade.lt(market.amm.minimumBaseAssetTradeSize)) {
|
|
37
|
-
return null;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
const userAccountAfter = Object.assign({}, userAccount);
|
|
41
|
-
const userPositionAfter = Object.assign({}, userPosition);
|
|
42
|
-
|
|
43
|
-
const currentPositionDirection = positionCurrentDirection(userPosition);
|
|
44
|
-
const increasePosition =
|
|
45
|
-
userPosition.baseAssetAmount.eq(ZERO) ||
|
|
46
|
-
isSameDirection(order.direction, currentPositionDirection);
|
|
47
|
-
|
|
48
|
-
if (increasePosition) {
|
|
49
|
-
const marketAfter = calculateNewMarketAfterTrade(
|
|
50
|
-
baseAssetAmountToTrade,
|
|
51
|
-
order.direction,
|
|
52
|
-
market
|
|
53
|
-
);
|
|
54
|
-
|
|
55
|
-
const { quoteAssetAmountSwapped, baseAssetAmountSwapped } =
|
|
56
|
-
calculateAmountSwapped(market, marketAfter);
|
|
57
|
-
|
|
58
|
-
userPositionAfter.baseAssetAmount = userPositionAfter.baseAssetAmount.add(
|
|
59
|
-
baseAssetAmountSwapped
|
|
60
|
-
);
|
|
61
|
-
userPositionAfter.quoteAssetAmount = userPositionAfter.quoteAssetAmount.add(
|
|
62
|
-
quoteAssetAmountSwapped
|
|
63
|
-
);
|
|
64
|
-
|
|
65
|
-
return [userAccountAfter, userPositionAfter, marketAfter];
|
|
66
|
-
} else {
|
|
67
|
-
const reversePosition = baseAssetAmountToTrade.gt(
|
|
68
|
-
userPosition.baseAssetAmount.abs()
|
|
69
|
-
);
|
|
70
|
-
|
|
71
|
-
if (reversePosition) {
|
|
72
|
-
const intermediateMarket = calculateNewMarketAfterTrade(
|
|
73
|
-
userPosition.baseAssetAmount,
|
|
74
|
-
findDirectionToClose(userPosition),
|
|
75
|
-
market
|
|
76
|
-
);
|
|
77
|
-
|
|
78
|
-
const { quoteAssetAmountSwapped: baseAssetValue } =
|
|
79
|
-
calculateAmountSwapped(market, intermediateMarket);
|
|
80
|
-
|
|
81
|
-
let pnl;
|
|
82
|
-
if (isVariant(currentPositionDirection, 'long')) {
|
|
83
|
-
pnl = baseAssetValue.sub(userPosition.quoteAssetAmount);
|
|
84
|
-
} else {
|
|
85
|
-
pnl = userPosition.quoteAssetAmount.sub(baseAssetValue);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
userAccountAfter.collateral = userAccountAfter.collateral.add(pnl);
|
|
89
|
-
|
|
90
|
-
const baseAssetAmountLeft = baseAssetAmountToTrade.sub(
|
|
91
|
-
userPosition.baseAssetAmount.abs()
|
|
92
|
-
);
|
|
93
|
-
|
|
94
|
-
const marketAfter = calculateNewMarketAfterTrade(
|
|
95
|
-
baseAssetAmountLeft,
|
|
96
|
-
order.direction,
|
|
97
|
-
intermediateMarket
|
|
98
|
-
);
|
|
99
|
-
|
|
100
|
-
const { quoteAssetAmountSwapped, baseAssetAmountSwapped } =
|
|
101
|
-
calculateAmountSwapped(intermediateMarket, marketAfter);
|
|
102
|
-
|
|
103
|
-
userPositionAfter.quoteAssetAmount = quoteAssetAmountSwapped;
|
|
104
|
-
userPositionAfter.baseAssetAmount = baseAssetAmountSwapped;
|
|
105
|
-
|
|
106
|
-
return [userAccountAfter, userPositionAfter, marketAfter];
|
|
107
|
-
} else {
|
|
108
|
-
const marketAfter = calculateNewMarketAfterTrade(
|
|
109
|
-
baseAssetAmountToTrade,
|
|
110
|
-
order.direction,
|
|
111
|
-
market
|
|
112
|
-
);
|
|
113
|
-
|
|
114
|
-
const {
|
|
115
|
-
quoteAssetAmountSwapped: baseAssetValue,
|
|
116
|
-
baseAssetAmountSwapped,
|
|
117
|
-
} = calculateAmountSwapped(market, marketAfter);
|
|
118
|
-
|
|
119
|
-
const costBasisRealized = userPosition.quoteAssetAmount
|
|
120
|
-
.mul(baseAssetAmountSwapped.abs())
|
|
121
|
-
.div(userPosition.baseAssetAmount.abs());
|
|
122
|
-
|
|
123
|
-
let pnl;
|
|
124
|
-
if (isVariant(currentPositionDirection, 'long')) {
|
|
125
|
-
pnl = baseAssetValue.sub(costBasisRealized);
|
|
126
|
-
} else {
|
|
127
|
-
pnl = costBasisRealized.sub(baseAssetValue);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
userAccountAfter.collateral = userAccountAfter.collateral.add(pnl);
|
|
131
|
-
|
|
132
|
-
userPositionAfter.baseAssetAmount = userPositionAfter.baseAssetAmount.add(
|
|
133
|
-
baseAssetAmountSwapped
|
|
134
|
-
);
|
|
135
|
-
userPositionAfter.quoteAssetAmount =
|
|
136
|
-
userPositionAfter.quoteAssetAmount.sub(costBasisRealized);
|
|
137
|
-
|
|
138
|
-
return [userAccountAfter, userPositionAfter, marketAfter];
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
function calculateAmountSwapped(
|
|
144
|
-
marketBefore: Market,
|
|
145
|
-
marketAfter: Market
|
|
146
|
-
): { quoteAssetAmountSwapped: BN; baseAssetAmountSwapped: BN } {
|
|
147
|
-
return {
|
|
148
|
-
quoteAssetAmountSwapped: marketBefore.amm.quoteAssetReserve
|
|
149
|
-
.sub(marketAfter.amm.quoteAssetReserve)
|
|
150
|
-
.abs()
|
|
151
|
-
.mul(marketBefore.amm.pegMultiplier)
|
|
152
|
-
.div(PEG_PRECISION)
|
|
153
|
-
.div(AMM_TO_QUOTE_PRECISION_RATIO),
|
|
154
|
-
baseAssetAmountSwapped: marketBefore.amm.baseAssetReserve.sub(
|
|
155
|
-
marketAfter.amm.baseAssetReserve
|
|
156
|
-
),
|
|
157
|
-
};
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
function calculateAmountToTrade(market: Market, order: Order): BN {
|
|
161
|
-
if (isVariant(order.orderType, 'limit')) {
|
|
162
|
-
return calculateAmountToTradeForLimit(market, order);
|
|
163
|
-
} else if (isVariant(order.orderType, 'stopLimit')) {
|
|
164
|
-
return calculateAmountToTradeForStopLimit(market, order);
|
|
165
|
-
} else if (isVariant(order.orderType, 'market')) {
|
|
166
|
-
// should never be a market order queued
|
|
167
|
-
return ZERO;
|
|
168
|
-
} else {
|
|
169
|
-
return calculateAmountToTradeForStop(market, order);
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
export function calculateAmountToTradeForLimit(
|
|
174
|
-
market: Market,
|
|
175
|
-
order: Order
|
|
176
|
-
): BN {
|
|
177
|
-
const [maxAmountToTrade, direction] = calculateMaxBaseAssetAmountToTrade(
|
|
178
|
-
market.amm,
|
|
179
|
-
order.price
|
|
180
|
-
);
|
|
181
|
-
|
|
182
|
-
// Check that directions are the same
|
|
183
|
-
const sameDirection = isSameDirection(direction, order.direction);
|
|
184
|
-
if (!sameDirection) {
|
|
185
|
-
return ZERO;
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
return maxAmountToTrade.gt(order.baseAssetAmount)
|
|
189
|
-
? order.baseAssetAmount
|
|
190
|
-
: maxAmountToTrade;
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
export function calculateAmountToTradeForStopLimit(
|
|
194
|
-
market: Market,
|
|
195
|
-
order: Order
|
|
196
|
-
): BN {
|
|
197
|
-
if (order.baseAssetAmountFilled.eq(ZERO)) {
|
|
198
|
-
const baseAssetAmount = calculateAmountToTradeForStop(market, order);
|
|
199
|
-
if (baseAssetAmount.eq(ZERO)) {
|
|
200
|
-
return ZERO;
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
return calculateAmountToTradeForLimit(market, order);
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
function isSameDirection(
|
|
208
|
-
firstDirection: PositionDirection,
|
|
209
|
-
secondDirection: PositionDirection
|
|
210
|
-
): boolean {
|
|
211
|
-
return (
|
|
212
|
-
(isVariant(firstDirection, 'long') && isVariant(secondDirection, 'long')) ||
|
|
213
|
-
(isVariant(firstDirection, 'short') && isVariant(secondDirection, 'short'))
|
|
214
|
-
);
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
function calculateAmountToTradeForStop(market: Market, order: Order): BN {
|
|
218
|
-
return isTriggerConditionSatisfied(market, order)
|
|
219
|
-
? order.baseAssetAmount
|
|
220
|
-
: ZERO;
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
function isTriggerConditionSatisfied(market: Market, order: Order): boolean {
|
|
224
|
-
const markPrice = calculateMarkPrice(market);
|
|
225
|
-
if (isVariant(order.triggerCondition, 'above')) {
|
|
226
|
-
return markPrice.gt(order.triggerPrice);
|
|
227
|
-
} else {
|
|
228
|
-
return markPrice.lt(order.triggerPrice);
|
|
229
|
-
}
|
|
230
|
-
}
|