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