@drift-labs/sdk 0.1.18-orders.1 → 0.1.18

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.
Files changed (108) hide show
  1. package/README.md +2 -1
  2. package/lib/accounts/defaultClearingHouseAccountSubscriber.d.ts +4 -7
  3. package/lib/accounts/defaultClearingHouseAccountSubscriber.d.ts.map +1 -0
  4. package/lib/accounts/defaultClearingHouseAccountSubscriber.js +1 -26
  5. package/lib/accounts/defaultUserAccountSubscriber.d.ts +2 -3
  6. package/lib/accounts/defaultUserAccountSubscriber.d.ts.map +1 -0
  7. package/lib/accounts/defaultUserAccountSubscriber.js +4 -13
  8. package/lib/accounts/types.d.ts +6 -32
  9. package/lib/accounts/types.d.ts.map +1 -0
  10. package/lib/accounts/webSocketAccountSubscriber.d.ts +1 -0
  11. package/lib/accounts/webSocketAccountSubscriber.d.ts.map +1 -0
  12. package/lib/addresses.d.ts +1 -4
  13. package/lib/addresses.d.ts.map +1 -0
  14. package/lib/addresses.js +1 -28
  15. package/lib/admin.d.ts +6 -10
  16. package/lib/admin.d.ts.map +1 -0
  17. package/lib/admin.js +44 -50
  18. package/lib/assert/assert.d.ts +1 -0
  19. package/lib/assert/assert.d.ts.map +1 -0
  20. package/lib/clearingHouse.d.ts +3 -22
  21. package/lib/clearingHouse.d.ts.map +1 -0
  22. package/lib/clearingHouse.js +7 -238
  23. package/lib/clearingHouseUser.d.ts +17 -10
  24. package/lib/clearingHouseUser.d.ts.map +1 -0
  25. package/lib/clearingHouseUser.js +84 -98
  26. package/lib/config.d.ts +1 -0
  27. package/lib/config.d.ts.map +1 -0
  28. package/lib/config.js +1 -1
  29. package/lib/constants/markets.d.ts +1 -0
  30. package/lib/constants/markets.d.ts.map +1 -0
  31. package/lib/constants/markets.js +16 -0
  32. package/lib/constants/numericConstants.d.ts +1 -0
  33. package/lib/constants/numericConstants.d.ts.map +1 -0
  34. package/lib/examples/makeTradeExample.d.ts +1 -0
  35. package/lib/examples/makeTradeExample.d.ts.map +1 -0
  36. package/lib/idl/clearing_house.json +233 -911
  37. package/lib/index.d.ts +1 -4
  38. package/lib/index.d.ts.map +1 -0
  39. package/lib/index.js +0 -4
  40. package/lib/math/amm.d.ts +1 -1
  41. package/lib/math/amm.d.ts.map +1 -0
  42. package/lib/math/amm.js +8 -31
  43. package/lib/math/conversion.d.ts +1 -0
  44. package/lib/math/conversion.d.ts.map +1 -0
  45. package/lib/math/funding.d.ts +1 -0
  46. package/lib/math/funding.d.ts.map +1 -0
  47. package/lib/math/funding.js +4 -1
  48. package/lib/math/insuranceFund.d.ts +1 -0
  49. package/lib/math/insuranceFund.d.ts.map +1 -0
  50. package/lib/math/market.d.ts +2 -2
  51. package/lib/math/market.d.ts.map +1 -0
  52. package/lib/math/market.js +1 -11
  53. package/lib/math/position.d.ts +2 -4
  54. package/lib/math/position.d.ts.map +1 -0
  55. package/lib/math/position.js +4 -18
  56. package/lib/math/trade.d.ts +1 -0
  57. package/lib/math/trade.d.ts.map +1 -0
  58. package/lib/math/utils.d.ts +1 -0
  59. package/lib/math/utils.d.ts.map +1 -0
  60. package/lib/mockUSDCFaucet.d.ts +1 -0
  61. package/lib/mockUSDCFaucet.d.ts.map +1 -0
  62. package/lib/pythClient.d.ts +1 -0
  63. package/lib/pythClient.d.ts.map +1 -0
  64. package/lib/tx/defaultTxSender.d.ts +1 -0
  65. package/lib/tx/defaultTxSender.d.ts.map +1 -0
  66. package/lib/tx/types.d.ts +1 -0
  67. package/lib/tx/types.d.ts.map +1 -0
  68. package/lib/tx/utils.d.ts +1 -0
  69. package/lib/tx/utils.d.ts.map +1 -0
  70. package/lib/types.d.ts +8 -140
  71. package/lib/types.d.ts.map +1 -0
  72. package/lib/types.js +1 -36
  73. package/lib/util/computeUnits.d.ts +1 -0
  74. package/lib/util/computeUnits.d.ts.map +1 -0
  75. package/lib/util/tps.d.ts +1 -0
  76. package/lib/util/tps.d.ts.map +1 -0
  77. package/lib/wallet.d.ts +1 -0
  78. package/lib/wallet.d.ts.map +1 -0
  79. package/package.json +1 -1
  80. package/src/accounts/defaultClearingHouseAccountSubscriber.ts +5 -52
  81. package/src/accounts/defaultUserAccountSubscriber.ts +6 -31
  82. package/src/accounts/types.ts +5 -42
  83. package/src/addresses.ts +0 -35
  84. package/src/admin.ts +52 -86
  85. package/src/clearingHouse.ts +7 -340
  86. package/src/clearingHouseUser.ts +102 -154
  87. package/src/config.ts +1 -1
  88. package/src/constants/markets.ts +16 -0
  89. package/src/idl/clearing_house.json +233 -911
  90. package/src/index.ts +0 -4
  91. package/src/math/amm.ts +14 -47
  92. package/src/math/funding.ts +5 -1
  93. package/src/math/market.ts +2 -28
  94. package/src/math/position.ts +3 -23
  95. package/src/types.ts +7 -124
  96. package/tsconfig.json +1 -0
  97. package/lib/accounts/defaultHistoryAccountSubscriber.d.ts +0 -28
  98. package/lib/accounts/defaultHistoryAccountSubscriber.js +0 -110
  99. package/lib/math/orders.d.ts +0 -3
  100. package/lib/math/orders.js +0 -30
  101. package/lib/orderParams.d.ts +0 -7
  102. package/lib/orderParams.js +0 -88
  103. package/lib/orders.d.ts +0 -5
  104. package/lib/orders.js +0 -136
  105. package/src/accounts/defaultHistoryAccountSubscriber.ts +0 -176
  106. package/src/math/orders.ts +0 -39
  107. package/src/orderParams.ts +0 -128
  108. 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
- }