@drift-labs/sdk 2.31.1-beta.2 → 2.31.1-beta.20

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 (51) hide show
  1. package/VERSION +1 -1
  2. package/lib/accounts/mockUserAccountSubscriber.d.ts +23 -0
  3. package/lib/accounts/mockUserAccountSubscriber.js +31 -0
  4. package/lib/constants/perpMarkets.js +20 -0
  5. package/lib/dlob/orderBookLevels.js +2 -2
  6. package/lib/driftClient.d.ts +57 -4
  7. package/lib/driftClient.js +244 -205
  8. package/lib/driftClientConfig.d.ts +2 -1
  9. package/lib/idl/drift.json +31 -1
  10. package/lib/index.d.ts +2 -0
  11. package/lib/index.js +2 -0
  12. package/lib/marinade/index.d.ts +11 -0
  13. package/lib/marinade/index.js +36 -0
  14. package/lib/marinade/types.d.ts +1963 -0
  15. package/lib/marinade/types.js +1965 -0
  16. package/lib/math/spotBalance.d.ts +9 -2
  17. package/lib/math/spotBalance.js +54 -6
  18. package/lib/math/superStake.d.ts +21 -0
  19. package/lib/math/superStake.js +100 -0
  20. package/lib/math/tiers.d.ts +4 -0
  21. package/lib/math/tiers.js +52 -0
  22. package/lib/tx/retryTxSender.d.ts +12 -3
  23. package/lib/tx/retryTxSender.js +22 -22
  24. package/lib/tx/types.d.ts +2 -2
  25. package/lib/user.d.ts +10 -1
  26. package/lib/user.js +39 -8
  27. package/lib/userConfig.d.ts +4 -0
  28. package/lib/userStats.js +4 -1
  29. package/lib/userStatsConfig.d.ts +2 -0
  30. package/package.json +1 -1
  31. package/src/accounts/mockUserAccountSubscriber.ts +53 -0
  32. package/src/config.ts +2 -2
  33. package/src/constants/perpMarkets.ts +20 -0
  34. package/src/dlob/orderBookLevels.ts +3 -2
  35. package/src/driftClient.ts +440 -224
  36. package/src/driftClientConfig.ts +2 -1
  37. package/src/idl/drift.json +31 -1
  38. package/src/index.ts +2 -0
  39. package/src/marinade/idl/idl.json +1962 -0
  40. package/src/marinade/index.ts +64 -0
  41. package/src/marinade/types.ts +3925 -0
  42. package/src/math/spotBalance.ts +83 -5
  43. package/src/math/superStake.ts +133 -0
  44. package/src/math/tiers.ts +44 -0
  45. package/src/tx/retryTxSender.ts +39 -35
  46. package/src/tx/types.ts +2 -2
  47. package/src/user.ts +63 -12
  48. package/src/userConfig.ts +5 -0
  49. package/src/userStats.ts +4 -0
  50. package/src/userStatsConfig.ts +3 -0
  51. package/tests/spot/test.ts +156 -0
@@ -0,0 +1,53 @@
1
+ import { DataAndSlot, UserAccountEvents, UserAccountSubscriber } from './types';
2
+ import { PublicKey } from '@solana/web3.js';
3
+ import StrictEventEmitter from 'strict-event-emitter-types';
4
+ import { EventEmitter } from 'events';
5
+ import { UserAccount } from '../types';
6
+
7
+ export class MockUserAccountSubscriber implements UserAccountSubscriber {
8
+ isSubscribed: boolean;
9
+ eventEmitter: StrictEventEmitter<EventEmitter, UserAccountEvents>;
10
+ userAccountPublicKey: PublicKey;
11
+
12
+ callbackId?: string;
13
+ errorCallbackId?: string;
14
+
15
+ user: DataAndSlot<UserAccount>;
16
+
17
+ public constructor(
18
+ userAccountPublicKey: PublicKey,
19
+ data: UserAccount,
20
+ slot: number
21
+ ) {
22
+ this.isSubscribed = true;
23
+ this.eventEmitter = new EventEmitter();
24
+ this.userAccountPublicKey = userAccountPublicKey;
25
+ this.user = { data, slot };
26
+ }
27
+
28
+ async subscribe(_userAccount?: UserAccount): Promise<boolean> {
29
+ return true;
30
+ }
31
+
32
+ async addToAccountLoader(): Promise<void> {}
33
+
34
+ async fetch(): Promise<void> {}
35
+
36
+ doesAccountExist(): boolean {
37
+ return this.user !== undefined;
38
+ }
39
+
40
+ async unsubscribe(): Promise<void> {}
41
+
42
+ assertIsSubscribed(): void {}
43
+
44
+ public getUserAccountAndSlot(): DataAndSlot<UserAccount> {
45
+ return this.user;
46
+ }
47
+
48
+ public updateData(userAccount: UserAccount, slot: number): void {
49
+ this.user = { data: userAccount, slot };
50
+ this.eventEmitter.emit('userAccountUpdate', userAccount);
51
+ this.eventEmitter.emit('update');
52
+ }
53
+ }
package/src/config.ts CHANGED
@@ -131,7 +131,7 @@ export async function findAllMarketAndOracles(program: Program): Promise<{
131
131
  (await program.account.spotMarket.all()) as ProgramAccount<SpotMarketAccount>[];
132
132
 
133
133
  for (const perpMarketProgramAccount of perpMarketProgramAccounts) {
134
- const perpMarket = perpMarketProgramAccount.account;
134
+ const perpMarket = perpMarketProgramAccount.account as PerpMarketAccount;
135
135
  perpMarketIndexes.push(perpMarket.marketIndex);
136
136
  oracleInfos.set(perpMarket.amm.oracle.toString(), {
137
137
  publicKey: perpMarket.amm.oracle,
@@ -140,7 +140,7 @@ export async function findAllMarketAndOracles(program: Program): Promise<{
140
140
  }
141
141
 
142
142
  for (const spotMarketProgramAccount of spotMarketProgramAccounts) {
143
- const spotMarket = spotMarketProgramAccount.account;
143
+ const spotMarket = spotMarketProgramAccount.account as SpotMarketAccount;
144
144
  spotMarketIndexes.push(spotMarket.marketIndex);
145
145
  oracleInfos.set(spotMarket.oracle.toString(), {
146
146
  publicKey: spotMarket.oracle,
@@ -134,6 +134,16 @@ export const DevnetPerpMarkets: PerpMarketConfig[] = [
134
134
  launchTs: 1683125906000,
135
135
  oracleSource: OracleSource.PYTH,
136
136
  },
137
+ {
138
+ fullName: 'RNDR',
139
+ category: ['Infra'],
140
+ symbol: 'RNDR-PERP',
141
+ baseAssetSymbol: 'RNDR',
142
+ marketIndex: 12,
143
+ oracle: new PublicKey('C2QvUPBiU3fViSyqA4nZgGyYqLgYf9PRpd8B8oLoo48w'),
144
+ launchTs: 1683125906000,
145
+ oracleSource: OracleSource.PYTH,
146
+ },
137
147
  ];
138
148
 
139
149
  export const MainnetPerpMarkets: PerpMarketConfig[] = [
@@ -257,6 +267,16 @@ export const MainnetPerpMarkets: PerpMarketConfig[] = [
257
267
  launchTs: 1683125906000,
258
268
  oracleSource: OracleSource.PYTH,
259
269
  },
270
+ {
271
+ fullName: 'RNDR',
272
+ category: ['Infra'],
273
+ symbol: 'RNDR-PERP',
274
+ baseAssetSymbol: 'RNDR',
275
+ marketIndex: 12,
276
+ oracle: new PublicKey('CYGfrBJB9HgLf9iZyN4aH5HvUAi2htQ4MjPxeXMf4Egn'),
277
+ launchTs: 1683125906000,
278
+ oracleSource: OracleSource.PYTH,
279
+ },
260
280
  ];
261
281
 
262
282
  export const PerpMarkets: { [key in DriftEnv]: PerpMarketConfig[] } = {
@@ -12,6 +12,7 @@ import {
12
12
  PositionDirection,
13
13
  standardizePrice,
14
14
  SwapDirection,
15
+ ZERO,
15
16
  } from '..';
16
17
  import { PublicKey } from '@solana/web3.js';
17
18
 
@@ -169,7 +170,7 @@ export function getVammL2Generator({
169
170
  pegMultiplier: updatedAmm.pegMultiplier,
170
171
  };
171
172
  const getL2Bids = function* () {
172
- while (numBids < numOrders) {
173
+ while (numBids < numOrders && baseSize.gt(ZERO)) {
173
174
  const [afterSwapQuoteReserves, afterSwapBaseReserves] =
174
175
  calculateAmmReservesAfterSwap(
175
176
  bidAmm,
@@ -208,7 +209,7 @@ export function getVammL2Generator({
208
209
  pegMultiplier: updatedAmm.pegMultiplier,
209
210
  };
210
211
  const getL2Asks = function* () {
211
- while (numAsks < numOrders) {
212
+ while (numAsks < numOrders && askSize.gt(ZERO)) {
212
213
  const [afterSwapQuoteReserves, afterSwapBaseReserves] =
213
214
  calculateAmmReservesAfterSwap(
214
215
  askAmm,