@drift-labs/sdk 0.2.0-master.29 → 0.2.0-master.30

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 (106) hide show
  1. package/README.md +13 -13
  2. package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +10 -11
  3. package/lib/accounts/pollingClearingHouseAccountSubscriber.js +4 -4
  4. package/lib/accounts/types.d.ts +4 -6
  5. package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +10 -11
  6. package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +6 -8
  7. package/lib/addresses/marketAddresses.d.ts +1 -3
  8. package/lib/addresses/pda.d.ts +5 -5
  9. package/lib/addresses/pda.js +5 -5
  10. package/lib/admin.d.ts +27 -28
  11. package/lib/admin.js +12 -37
  12. package/lib/clearingHouse.d.ts +61 -62
  13. package/lib/clearingHouse.js +92 -110
  14. package/lib/clearingHouseConfig.d.ts +2 -4
  15. package/lib/clearingHouseUser.d.ts +16 -16
  16. package/lib/clearingHouseUser.js +16 -16
  17. package/lib/config.d.ts +2 -4
  18. package/lib/config.js +1 -1
  19. package/lib/constants/numericConstants.d.ts +7 -8
  20. package/lib/constants/numericConstants.js +17 -18
  21. package/lib/constants/perpMarkets.d.ts +2 -3
  22. package/lib/constants/perpMarkets.js +3 -3
  23. package/lib/constants/spotMarkets.d.ts +2 -1
  24. package/lib/constants/spotMarkets.js +6 -4
  25. package/lib/dlob/DLOB.d.ts +13 -13
  26. package/lib/dlob/DLOB.js +36 -40
  27. package/lib/dlob/DLOBNode.js +2 -2
  28. package/lib/events/sort.js +1 -1
  29. package/lib/examples/makeTradeExample.js +3 -3
  30. package/lib/idl/clearing_house.json +359 -310
  31. package/lib/math/amm.d.ts +2 -2
  32. package/lib/math/amm.js +10 -10
  33. package/lib/math/conversion.js +1 -1
  34. package/lib/math/funding.js +9 -9
  35. package/lib/math/margin.js +3 -3
  36. package/lib/math/market.d.ts +4 -4
  37. package/lib/math/market.js +7 -7
  38. package/lib/math/oracles.js +6 -8
  39. package/lib/math/position.d.ts +2 -2
  40. package/lib/math/position.js +9 -9
  41. package/lib/math/repeg.js +7 -6
  42. package/lib/math/spotBalance.js +5 -5
  43. package/lib/math/spotPosition.js +2 -2
  44. package/lib/math/trade.d.ts +6 -6
  45. package/lib/math/trade.js +15 -19
  46. package/lib/oracles/pythClient.js +1 -1
  47. package/lib/oracles/quoteAssetOracleClient.js +1 -1
  48. package/lib/oracles/switchboardClient.js +1 -1
  49. package/lib/types.d.ts +63 -51
  50. package/lib/types.js +1 -1
  51. package/package.json +2 -1
  52. package/src/accounts/pollingClearingHouseAccountSubscriber.ts +15 -15
  53. package/src/accounts/types.ts +4 -5
  54. package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +17 -25
  55. package/src/addresses/marketAddresses.ts +1 -2
  56. package/src/addresses/pda.ts +10 -10
  57. package/src/admin.ts +42 -79
  58. package/src/clearingHouse.ts +164 -212
  59. package/src/clearingHouseConfig.ts +2 -3
  60. package/src/clearingHouseUser.ts +35 -35
  61. package/src/config.ts +3 -4
  62. package/src/constants/numericConstants.ts +19 -21
  63. package/src/constants/perpMarkets.ts +5 -5
  64. package/src/constants/spotMarkets.ts +8 -5
  65. package/src/dlob/DLOB.ts +54 -70
  66. package/src/dlob/DLOBNode.ts +5 -6
  67. package/src/events/sort.ts +1 -1
  68. package/src/examples/makeTradeExample.js +2 -2
  69. package/src/examples/makeTradeExample.ts +5 -8
  70. package/src/idl/clearing_house.json +359 -310
  71. package/src/math/amm.ts +14 -11
  72. package/src/math/conversion.ts +2 -2
  73. package/src/math/funding.ts +13 -11
  74. package/src/math/margin.ts +4 -5
  75. package/src/math/market.ts +5 -5
  76. package/src/math/oracles.ts +9 -9
  77. package/src/math/position.ts +11 -19
  78. package/src/math/repeg.ts +8 -7
  79. package/src/math/spotBalance.ts +6 -6
  80. package/src/math/spotPosition.ts +2 -2
  81. package/src/math/trade.ts +17 -21
  82. package/src/oracles/pythClient.ts +2 -2
  83. package/src/oracles/quoteAssetOracleClient.ts +2 -2
  84. package/src/oracles/switchboardClient.ts +2 -2
  85. package/src/types.ts +69 -51
  86. package/tests/dlob/helpers.ts +56 -4
  87. package/src/addresses/marketAddresses.js +0 -26
  88. package/src/constants/banks.js +0 -42
  89. package/src/constants/markets.js +0 -42
  90. package/src/events/txEventCache.js +0 -71
  91. package/src/factory/bigNum.js +0 -390
  92. package/src/factory/oracleClient.js +0 -20
  93. package/src/math/auction.js +0 -42
  94. package/src/math/conversion.js +0 -11
  95. package/src/math/funding.js +0 -248
  96. package/src/math/repeg.js +0 -128
  97. package/src/math/trade.js +0 -253
  98. package/src/math/utils.js +0 -26
  99. package/src/math/utils.js.map +0 -1
  100. package/src/oracles/oracleClientCache.js +0 -19
  101. package/src/oracles/pythClient.js +0 -46
  102. package/src/oracles/quoteAssetOracleClient.js +0 -32
  103. package/src/oracles/switchboardClient.js +0 -69
  104. package/src/oracles/types.js +0 -2
  105. package/src/userName.js +0 -20
  106. package/src/wallet.js +0 -35
package/README.md CHANGED
@@ -54,13 +54,13 @@ a BigNum: 10,500,000, with precision 10^6, is equal to 10.5 because 10,500,000 /
54
54
 
55
55
  The Drift SDK uses some common precisions, which are available as constants to import from the SDK.
56
56
 
57
- | Precision Name | Value |
58
- | ------------------------- | ----- |
59
- | QUOTE_PRECISION | 10^6 |
60
- | MARK_PRICE_PRECISION | 10^10 |
61
- | AMM_RESERVE_PRECISION | 10^13 |
62
- | FUNDING_PAYMENT_PRECISION | 10^4 |
63
- | PEG_PRECISION | 10^3 |
57
+ | Precision Name | Value |
58
+ | --------------------- | ----- |
59
+ | FUNDING_RATE_BUFFER | 10^3 |
60
+ | QUOTE_PRECISION | 10^6 |
61
+ | PEG_PRECISION | 10^6 |
62
+ | PRICE_PRECISION | 10^6 |
63
+ | AMM_RESERVE_PRECISION | 10^9 |
64
64
 
65
65
  **Important Note for BigNum division**
66
66
 
@@ -88,7 +88,7 @@ import { BN, Provider } from '@project-serum/anchor';
88
88
  import { Token, TOKEN_PROGRAM_ID } from '@solana/spl-token';
89
89
  import { Connection, Keypair, PublicKey } from '@solana/web3.js';
90
90
  import {
91
- calculateMarkPrice,
91
+ calculateReservePrice,
92
92
  ClearingHouse,
93
93
  ClearingHouseUser,
94
94
  initialize,
@@ -96,9 +96,9 @@ import {
96
96
  PositionDirection,
97
97
  convertToNumber,
98
98
  calculateTradeSlippage,
99
- MARK_PRICE_PRECISION,
99
+ PRICE_PRECISION,
100
100
  QUOTE_PRECISION,
101
- Wallet
101
+ Wallet,
102
102
  } from '@drift-labs/sdk';
103
103
 
104
104
  export const getTokenAddress = (
@@ -177,11 +177,11 @@ const main = async () => {
177
177
  (market) => market.baseAssetSymbol === 'SOL'
178
178
  );
179
179
 
180
- const currentMarketPrice = calculateMarkPrice(
180
+ const currentMarketPrice = calculateReservePrice(
181
181
  clearingHouse.getMarket(solMarketInfo.marketIndex)
182
182
  );
183
183
 
184
- const formattedPrice = convertToNumber(currentMarketPrice, MARK_PRICE_PRECISION);
184
+ const formattedPrice = convertToNumber(currentMarketPrice, PRICE_PRECISION);
185
185
 
186
186
  console.log(`Current Market Price is $${formattedPrice}`);
187
187
 
@@ -194,7 +194,7 @@ const main = async () => {
194
194
  new BN(5000).mul(QUOTE_PRECISION),
195
195
  solMarketAccount
196
196
  )[0],
197
- MARK_PRICE_PRECISION
197
+ PRICE_PRECISION
198
198
  );
199
199
 
200
200
  console.log(
@@ -1,7 +1,6 @@
1
- /// <reference types="bn.js" />
2
1
  /// <reference types="node" />
3
2
  import { DataAndSlot, AccountToPoll, ClearingHouseAccountEvents, ClearingHouseAccountSubscriber, OraclesToPoll } from './types';
4
- import { BN, Program } from '@project-serum/anchor';
3
+ import { Program } from '@project-serum/anchor';
5
4
  import StrictEventEmitter from 'strict-event-emitter-types';
6
5
  import { EventEmitter } from 'events';
7
6
  import { SpotMarketAccount, PerpMarketAccount, StateAccount, UserAccount } from '../types';
@@ -12,8 +11,8 @@ import { OracleClientCache } from '../oracles/oracleClientCache';
12
11
  export declare class PollingClearingHouseAccountSubscriber implements ClearingHouseAccountSubscriber {
13
12
  isSubscribed: boolean;
14
13
  program: Program;
15
- perpMarketIndexes: BN[];
16
- spotMarketIndexes: BN[];
14
+ perpMarketIndexes: number[];
15
+ spotMarketIndexes: number[];
17
16
  oracleInfos: OracleInfo[];
18
17
  oracleClientCache: OracleClientCache;
19
18
  eventEmitter: StrictEventEmitter<EventEmitter, ClearingHouseAccountEvents>;
@@ -29,13 +28,13 @@ export declare class PollingClearingHouseAccountSubscriber implements ClearingHo
29
28
  private isSubscribing;
30
29
  private subscriptionPromise;
31
30
  private subscriptionPromiseResolver;
32
- constructor(program: Program, accountLoader: BulkAccountLoader, perpMarketIndexes: BN[], spotMarketIndexes: BN[], oracleInfos: OracleInfo[]);
31
+ constructor(program: Program, accountLoader: BulkAccountLoader, perpMarketIndexes: number[], spotMarketIndexes: number[], oracleInfos: OracleInfo[]);
33
32
  subscribe(): Promise<boolean>;
34
33
  updateAccountsToPoll(): Promise<void>;
35
34
  updateMarketAccountsToPoll(): Promise<boolean>;
36
- addMarketAccountToPoll(marketIndex: BN): Promise<boolean>;
35
+ addMarketAccountToPoll(marketIndex: number): Promise<boolean>;
37
36
  updateSpotMarketAccountsToPoll(): Promise<boolean>;
38
- addSpotMarketAccountToPoll(marketIndex: BN): Promise<boolean>;
37
+ addSpotMarketAccountToPoll(marketIndex: number): Promise<boolean>;
39
38
  updateOraclesToPoll(): boolean;
40
39
  addOracleToPoll(oracleInfo: OracleInfo): boolean;
41
40
  getClearingHouseAccounts(): Promise<ClearingHouseAccounts>;
@@ -45,14 +44,14 @@ export declare class PollingClearingHouseAccountSubscriber implements ClearingHo
45
44
  fetch(): Promise<void>;
46
45
  didSubscriptionSucceed(): boolean;
47
46
  unsubscribe(): Promise<void>;
48
- addSpotMarket(marketIndex: BN): Promise<boolean>;
49
- addPerpMarket(marketIndex: BN): Promise<boolean>;
47
+ addSpotMarket(marketIndex: number): Promise<boolean>;
48
+ addPerpMarket(marketIndex: number): Promise<boolean>;
50
49
  addOracle(oracleInfo: OracleInfo): Promise<boolean>;
51
50
  assertIsSubscribed(): void;
52
51
  getStateAccountAndSlot(): DataAndSlot<StateAccount>;
53
- getMarketAccountAndSlot(marketIndex: BN): DataAndSlot<PerpMarketAccount> | undefined;
52
+ getMarketAccountAndSlot(marketIndex: number): DataAndSlot<PerpMarketAccount> | undefined;
54
53
  getMarketAccountsAndSlots(): DataAndSlot<PerpMarketAccount>[];
55
- getSpotMarketAccountAndSlot(marketIndex: BN): DataAndSlot<SpotMarketAccount> | undefined;
54
+ getSpotMarketAccountAndSlot(marketIndex: number): DataAndSlot<SpotMarketAccount> | undefined;
56
55
  getSpotMarketAccountsAndSlots(): DataAndSlot<SpotMarketAccount>[];
57
56
  getOraclePriceDataAndSlot(oraclePublicKey: PublicKey): DataAndSlot<OraclePriceData> | undefined;
58
57
  }
@@ -79,7 +79,7 @@ class PollingClearingHouseAccountSubscriber {
79
79
  key: 'perpMarket',
80
80
  publicKey: marketPublicKey,
81
81
  eventType: 'perpMarketAccountUpdate',
82
- mapKey: marketIndex.toNumber(),
82
+ mapKey: marketIndex,
83
83
  });
84
84
  return true;
85
85
  }
@@ -95,7 +95,7 @@ class PollingClearingHouseAccountSubscriber {
95
95
  key: 'spotMarket',
96
96
  publicKey: marketPublicKey,
97
97
  eventType: 'spotMarketAccountUpdate',
98
- mapKey: marketIndex.toNumber(),
98
+ mapKey: marketIndex,
99
99
  });
100
100
  return true;
101
101
  }
@@ -254,13 +254,13 @@ class PollingClearingHouseAccountSubscriber {
254
254
  return this.state;
255
255
  }
256
256
  getMarketAccountAndSlot(marketIndex) {
257
- return this.perpMarket.get(marketIndex.toNumber());
257
+ return this.perpMarket.get(marketIndex);
258
258
  }
259
259
  getMarketAccountsAndSlots() {
260
260
  return Array.from(this.perpMarket.values());
261
261
  }
262
262
  getSpotMarketAccountAndSlot(marketIndex) {
263
- return this.spotMarket.get(marketIndex.toNumber());
263
+ return this.spotMarket.get(marketIndex);
264
264
  }
265
265
  getSpotMarketAccountsAndSlots() {
266
266
  return Array.from(this.spotMarket.values());
@@ -1,12 +1,10 @@
1
1
  /// <reference types="node" />
2
- /// <reference types="bn.js" />
3
2
  import { SpotMarketAccount, PerpMarketAccount, OracleSource, StateAccount, UserAccount, UserStatsAccount } from '../types';
4
3
  import StrictEventEmitter from 'strict-event-emitter-types';
5
4
  import { EventEmitter } from 'events';
6
5
  import { PublicKey } from '@solana/web3.js';
7
6
  import { AccountInfo } from '@solana/spl-token';
8
7
  import { OracleInfo, OraclePriceData } from '..';
9
- import { BN } from '@project-serum/anchor';
10
8
  export interface AccountSubscriber<T> {
11
9
  dataAndSlot?: DataAndSlot<T>;
12
10
  subscribe(onChange: (data: T) => void): Promise<void>;
@@ -31,13 +29,13 @@ export interface ClearingHouseAccountSubscriber {
31
29
  subscribe(): Promise<boolean>;
32
30
  fetch(): Promise<void>;
33
31
  unsubscribe(): Promise<void>;
34
- addPerpMarket(marketIndex: BN): Promise<boolean>;
35
- addSpotMarket(marketIndex: BN): Promise<boolean>;
32
+ addPerpMarket(marketIndex: number): Promise<boolean>;
33
+ addSpotMarket(marketIndex: number): Promise<boolean>;
36
34
  addOracle(oracleInfo: OracleInfo): Promise<boolean>;
37
35
  getStateAccountAndSlot(): DataAndSlot<StateAccount>;
38
- getMarketAccountAndSlot(marketIndex: BN): DataAndSlot<PerpMarketAccount> | undefined;
36
+ getMarketAccountAndSlot(marketIndex: number): DataAndSlot<PerpMarketAccount> | undefined;
39
37
  getMarketAccountsAndSlots(): DataAndSlot<PerpMarketAccount>[];
40
- getSpotMarketAccountAndSlot(marketIndex: BN): DataAndSlot<SpotMarketAccount> | undefined;
38
+ getSpotMarketAccountAndSlot(marketIndex: number): DataAndSlot<SpotMarketAccount> | undefined;
41
39
  getSpotMarketAccountsAndSlots(): DataAndSlot<SpotMarketAccount>[];
42
40
  getOraclePriceDataAndSlot(oraclePublicKey: PublicKey): DataAndSlot<OraclePriceData> | undefined;
43
41
  }
@@ -1,9 +1,8 @@
1
- /// <reference types="bn.js" />
2
1
  /// <reference types="node" />
3
2
  import { ClearingHouseAccountSubscriber, ClearingHouseAccountEvents, DataAndSlot } from './types';
4
3
  import { AccountSubscriber } from './types';
5
4
  import { SpotMarketAccount, PerpMarketAccount, StateAccount } from '../types';
6
- import { BN, Program } from '@project-serum/anchor';
5
+ import { Program } from '@project-serum/anchor';
7
6
  import StrictEventEmitter from 'strict-event-emitter-types';
8
7
  import { EventEmitter } from 'events';
9
8
  import { PublicKey } from '@solana/web3.js';
@@ -12,8 +11,8 @@ import { OracleClientCache } from '../oracles/oracleClientCache';
12
11
  export declare class WebSocketClearingHouseAccountSubscriber implements ClearingHouseAccountSubscriber {
13
12
  isSubscribed: boolean;
14
13
  program: Program;
15
- perpMarketIndexes: BN[];
16
- spotMarketIndexes: BN[];
14
+ perpMarketIndexes: number[];
15
+ spotMarketIndexes: number[];
17
16
  oracleInfos: OracleInfo[];
18
17
  oracleClientCache: OracleClientCache;
19
18
  eventEmitter: StrictEventEmitter<EventEmitter, ClearingHouseAccountEvents>;
@@ -24,12 +23,12 @@ export declare class WebSocketClearingHouseAccountSubscriber implements Clearing
24
23
  private isSubscribing;
25
24
  private subscriptionPromise;
26
25
  private subscriptionPromiseResolver;
27
- constructor(program: Program, perpMarketIndexes: BN[], spotMarketIndexes: BN[], oracleInfos: OracleInfo[]);
26
+ constructor(program: Program, perpMarketIndexes: number[], spotMarketIndexes: number[], oracleInfos: OracleInfo[]);
28
27
  subscribe(): Promise<boolean>;
29
28
  subscribeToMarketAccounts(): Promise<boolean>;
30
- subscribeToMarketAccount(marketIndex: BN): Promise<boolean>;
29
+ subscribeToMarketAccount(marketIndex: number): Promise<boolean>;
31
30
  subscribeToSpotMarketAccounts(): Promise<boolean>;
32
- subscribeToSpotMarketAccount(marketIndex: BN): Promise<boolean>;
31
+ subscribeToSpotMarketAccount(marketIndex: number): Promise<boolean>;
33
32
  subscribeToOracles(): Promise<boolean>;
34
33
  subscribeToOracle(oracleInfo: OracleInfo): Promise<boolean>;
35
34
  unsubscribeFromMarketAccounts(): Promise<void>;
@@ -37,14 +36,14 @@ export declare class WebSocketClearingHouseAccountSubscriber implements Clearing
37
36
  unsubscribeFromOracles(): Promise<void>;
38
37
  fetch(): Promise<void>;
39
38
  unsubscribe(): Promise<void>;
40
- addSpotMarket(marketIndex: BN): Promise<boolean>;
41
- addPerpMarket(marketIndex: BN): Promise<boolean>;
39
+ addSpotMarket(marketIndex: number): Promise<boolean>;
40
+ addPerpMarket(marketIndex: number): Promise<boolean>;
42
41
  addOracle(oracleInfo: OracleInfo): Promise<boolean>;
43
42
  assertIsSubscribed(): void;
44
43
  getStateAccountAndSlot(): DataAndSlot<StateAccount>;
45
- getMarketAccountAndSlot(marketIndex: BN): DataAndSlot<PerpMarketAccount> | undefined;
44
+ getMarketAccountAndSlot(marketIndex: number): DataAndSlot<PerpMarketAccount> | undefined;
46
45
  getMarketAccountsAndSlots(): DataAndSlot<PerpMarketAccount>[];
47
- getSpotMarketAccountAndSlot(marketIndex: BN): DataAndSlot<SpotMarketAccount> | undefined;
46
+ getSpotMarketAccountAndSlot(marketIndex: number): DataAndSlot<SpotMarketAccount> | undefined;
48
47
  getSpotMarketAccountsAndSlots(): DataAndSlot<SpotMarketAccount>[];
49
48
  getOraclePriceDataAndSlot(oraclePublicKey: PublicKey): DataAndSlot<OraclePriceData> | undefined;
50
49
  }
@@ -65,7 +65,7 @@ class WebSocketClearingHouseAccountSubscriber {
65
65
  this.eventEmitter.emit('perpMarketAccountUpdate', data);
66
66
  this.eventEmitter.emit('update');
67
67
  });
68
- this.perpMarketAccountSubscribers.set(marketIndex.toNumber(), accountSubscriber);
68
+ this.perpMarketAccountSubscribers.set(marketIndex, accountSubscriber);
69
69
  return true;
70
70
  }
71
71
  async subscribeToSpotMarketAccounts() {
@@ -81,7 +81,7 @@ class WebSocketClearingHouseAccountSubscriber {
81
81
  this.eventEmitter.emit('spotMarketAccountUpdate', data);
82
82
  this.eventEmitter.emit('update');
83
83
  });
84
- this.spotMarketAccountSubscribers.set(marketIndex.toNumber(), accountSubscriber);
84
+ this.spotMarketAccountSubscribers.set(marketIndex, accountSubscriber);
85
85
  return true;
86
86
  }
87
87
  async subscribeToOracles() {
@@ -139,13 +139,13 @@ class WebSocketClearingHouseAccountSubscriber {
139
139
  this.isSubscribed = false;
140
140
  }
141
141
  async addSpotMarket(marketIndex) {
142
- if (this.spotMarketAccountSubscribers.has(marketIndex.toNumber())) {
142
+ if (this.spotMarketAccountSubscribers.has(marketIndex)) {
143
143
  return true;
144
144
  }
145
145
  return this.subscribeToSpotMarketAccount(marketIndex);
146
146
  }
147
147
  async addPerpMarket(marketIndex) {
148
- if (this.perpMarketAccountSubscribers.has(marketIndex.toNumber())) {
148
+ if (this.perpMarketAccountSubscribers.has(marketIndex)) {
149
149
  return true;
150
150
  }
151
151
  return this.subscribeToMarketAccount(marketIndex);
@@ -170,16 +170,14 @@ class WebSocketClearingHouseAccountSubscriber {
170
170
  }
171
171
  getMarketAccountAndSlot(marketIndex) {
172
172
  this.assertIsSubscribed();
173
- return this.perpMarketAccountSubscribers.get(marketIndex.toNumber())
174
- .dataAndSlot;
173
+ return this.perpMarketAccountSubscribers.get(marketIndex).dataAndSlot;
175
174
  }
176
175
  getMarketAccountsAndSlots() {
177
176
  return Array.from(this.perpMarketAccountSubscribers.values()).map((subscriber) => subscriber.dataAndSlot);
178
177
  }
179
178
  getSpotMarketAccountAndSlot(marketIndex) {
180
179
  this.assertIsSubscribed();
181
- return this.spotMarketAccountSubscribers.get(marketIndex.toNumber())
182
- .dataAndSlot;
180
+ return this.spotMarketAccountSubscribers.get(marketIndex).dataAndSlot;
183
181
  }
184
182
  getSpotMarketAccountsAndSlots() {
185
183
  return Array.from(this.spotMarketAccountSubscribers.values()).map((subscriber) => subscriber.dataAndSlot);
@@ -1,4 +1,2 @@
1
- /// <reference types="bn.js" />
2
1
  import { PublicKey } from '@solana/web3.js';
3
- import { BN } from '@project-serum/anchor';
4
- export declare function getMarketAddress(programId: PublicKey, marketIndex: BN): Promise<PublicKey>;
2
+ export declare function getMarketAddress(programId: PublicKey, marketIndex: number): Promise<PublicKey>;
@@ -7,11 +7,11 @@ export declare function getUserAccountPublicKeyAndNonce(programId: PublicKey, au
7
7
  export declare function getUserAccountPublicKey(programId: PublicKey, authority: PublicKey, userId?: number): Promise<PublicKey>;
8
8
  export declare function getUserAccountPublicKeySync(programId: PublicKey, authority: PublicKey, userId?: number): PublicKey;
9
9
  export declare function getUserStatsAccountPublicKey(programId: PublicKey, authority: PublicKey): PublicKey;
10
- export declare function getMarketPublicKey(programId: PublicKey, marketIndex: BN): Promise<PublicKey>;
11
- export declare function getSpotMarketPublicKey(programId: PublicKey, marketIndex: BN): Promise<PublicKey>;
12
- export declare function getSpotMarketVaultPublicKey(programId: PublicKey, marketIndex: BN): Promise<PublicKey>;
13
- export declare function getInsuranceFundVaultPublicKey(programId: PublicKey, marketIndex: BN): Promise<PublicKey>;
14
- export declare function getInsuranceFundStakeAccountPublicKey(programId: PublicKey, authority: PublicKey, marketIndex: BN): PublicKey;
10
+ export declare function getMarketPublicKey(programId: PublicKey, marketIndex: number): Promise<PublicKey>;
11
+ export declare function getSpotMarketPublicKey(programId: PublicKey, marketIndex: number): Promise<PublicKey>;
12
+ export declare function getSpotMarketVaultPublicKey(programId: PublicKey, marketIndex: number): Promise<PublicKey>;
13
+ export declare function getInsuranceFundVaultPublicKey(programId: PublicKey, marketIndex: number): Promise<PublicKey>;
14
+ export declare function getInsuranceFundStakeAccountPublicKey(programId: PublicKey, authority: PublicKey, marketIndex: number): PublicKey;
15
15
  export declare function getClearingHouseSignerPublicKey(programId: PublicKey): PublicKey;
16
16
  export declare function getSerumOpenOrdersPublicKey(programId: PublicKey, market: PublicKey): PublicKey;
17
17
  export declare function getSerumSignerPublicKey(programId: PublicKey, market: PublicKey, nonce: BN): PublicKey;
@@ -63,28 +63,28 @@ exports.getUserStatsAccountPublicKey = getUserStatsAccountPublicKey;
63
63
  async function getMarketPublicKey(programId, marketIndex) {
64
64
  return (await anchor.web3.PublicKey.findProgramAddress([
65
65
  Buffer.from(anchor.utils.bytes.utf8.encode('market')),
66
- marketIndex.toArrayLike(Buffer, 'le', 8),
66
+ new anchor.BN(marketIndex).toArrayLike(Buffer, 'le', 2),
67
67
  ], programId))[0];
68
68
  }
69
69
  exports.getMarketPublicKey = getMarketPublicKey;
70
70
  async function getSpotMarketPublicKey(programId, marketIndex) {
71
71
  return (await anchor.web3.PublicKey.findProgramAddress([
72
72
  Buffer.from(anchor.utils.bytes.utf8.encode('spot_market')),
73
- marketIndex.toArrayLike(Buffer, 'le', 8),
73
+ new anchor.BN(marketIndex).toArrayLike(Buffer, 'le', 2),
74
74
  ], programId))[0];
75
75
  }
76
76
  exports.getSpotMarketPublicKey = getSpotMarketPublicKey;
77
77
  async function getSpotMarketVaultPublicKey(programId, marketIndex) {
78
78
  return (await anchor.web3.PublicKey.findProgramAddress([
79
79
  Buffer.from(anchor.utils.bytes.utf8.encode('spot_market_vault')),
80
- marketIndex.toArrayLike(Buffer, 'le', 8),
80
+ new anchor.BN(marketIndex).toArrayLike(Buffer, 'le', 2),
81
81
  ], programId))[0];
82
82
  }
83
83
  exports.getSpotMarketVaultPublicKey = getSpotMarketVaultPublicKey;
84
84
  async function getInsuranceFundVaultPublicKey(programId, marketIndex) {
85
85
  return (await anchor.web3.PublicKey.findProgramAddress([
86
86
  Buffer.from(anchor.utils.bytes.utf8.encode('insurance_fund_vault')),
87
- marketIndex.toArrayLike(Buffer, 'le', 8),
87
+ new anchor.BN(marketIndex).toArrayLike(Buffer, 'le', 2),
88
88
  ], programId))[0];
89
89
  }
90
90
  exports.getInsuranceFundVaultPublicKey = getInsuranceFundVaultPublicKey;
@@ -92,7 +92,7 @@ function getInsuranceFundStakeAccountPublicKey(programId, authority, marketIndex
92
92
  return anchor.web3.PublicKey.findProgramAddressSync([
93
93
  Buffer.from(anchor.utils.bytes.utf8.encode('insurance_fund_stake')),
94
94
  authority.toBuffer(),
95
- marketIndex.toArrayLike(Buffer, 'le', 8),
95
+ new anchor.BN(marketIndex).toArrayLike(Buffer, 'le', 2),
96
96
  ], programId)[0];
97
97
  }
98
98
  exports.getInsuranceFundStakeAccountPublicKey = getInsuranceFundStakeAccountPublicKey;
package/lib/admin.d.ts CHANGED
@@ -6,24 +6,22 @@ import { ClearingHouse } from './clearingHouse';
6
6
  export declare class Admin extends ClearingHouse {
7
7
  initialize(usdcMint: PublicKey, adminControlsPrices: boolean): Promise<[TransactionSignature]>;
8
8
  initializeSpotMarket(mint: PublicKey, optimalUtilization: BN, optimalRate: BN, maxRate: BN, oracle: PublicKey, oracleSource: OracleSource, initialAssetWeight: BN, maintenanceAssetWeight: BN, initialLiabilityWeight: BN, maintenanceLiabilityWeight: BN, imfFactor?: BN, liquidationFee?: BN): Promise<TransactionSignature>;
9
- initializeSerumFulfillmentConfig(marketIndex: BN, serumMarket: PublicKey, serumProgram: PublicKey): Promise<TransactionSignature>;
9
+ initializeSerumFulfillmentConfig(marketIndex: number, serumMarket: PublicKey, serumProgram: PublicKey): Promise<TransactionSignature>;
10
10
  initializeMarket(priceOracle: PublicKey, baseAssetReserve: BN, quoteAssetReserve: BN, periodicity: BN, pegMultiplier?: BN, oracleSource?: OracleSource, marginRatioInitial?: number, marginRatioMaintenance?: number, liquidationFee?: BN): Promise<TransactionSignature>;
11
- moveAmmPrice(marketIndex: BN, baseAssetReserve: BN, quoteAssetReserve: BN, sqrtK?: BN): Promise<TransactionSignature>;
12
- updateK(sqrtK: BN, marketIndex: BN): Promise<TransactionSignature>;
13
- updateConcentrationScale(marketIndex: BN, concentrationScale: BN): Promise<TransactionSignature>;
14
- moveAmmToPrice(perpMarketIndex: BN, targetPrice: BN): Promise<TransactionSignature>;
15
- repegAmmCurve(newPeg: BN, marketIndex: BN): Promise<TransactionSignature>;
16
- updateAmmOracleTwap(marketIndex: BN): Promise<TransactionSignature>;
17
- resetAmmOracleTwap(marketIndex: BN): Promise<TransactionSignature>;
18
- withdrawFromInsuranceVault(amount: BN, recipient: PublicKey): Promise<TransactionSignature>;
19
- withdrawFromMarketToInsuranceVault(marketIndex: BN, amount: BN, recipient: PublicKey): Promise<TransactionSignature>;
20
- withdrawFromInsuranceVaultToMarket(marketIndex: BN, amount: BN): Promise<TransactionSignature>;
11
+ moveAmmPrice(marketIndex: number, baseAssetReserve: BN, quoteAssetReserve: BN, sqrtK?: BN): Promise<TransactionSignature>;
12
+ updateK(sqrtK: BN, marketIndex: number): Promise<TransactionSignature>;
13
+ updateConcentrationScale(marketIndex: number, concentrationScale: BN): Promise<TransactionSignature>;
14
+ moveAmmToPrice(perpMarketIndex: number, targetPrice: BN): Promise<TransactionSignature>;
15
+ repegAmmCurve(newPeg: BN, marketIndex: number): Promise<TransactionSignature>;
16
+ updateAmmOracleTwap(marketIndex: number): Promise<TransactionSignature>;
17
+ resetAmmOracleTwap(marketIndex: number): Promise<TransactionSignature>;
18
+ depositIntoMarketFeePool(marketIndex: number, amount: BN, sourceVault: PublicKey): Promise<TransactionSignature>;
21
19
  updateAdmin(admin: PublicKey): Promise<TransactionSignature>;
22
- updateCurveUpdateIntensity(marketIndex: BN, curveUpdateIntensity: number): Promise<TransactionSignature>;
23
- updateMarginRatio(marketIndex: BN, marginRatioInitial: number, marginRatioMaintenance: number): Promise<TransactionSignature>;
24
- updateMarketBaseSpread(marketIndex: BN, baseSpread: number): Promise<TransactionSignature>;
25
- updateAmmJitIntensity(marketIndex: BN, ammJitIntensity: number): Promise<TransactionSignature>;
26
- updateMarketMaxSpread(marketIndex: BN, maxSpread: number): Promise<TransactionSignature>;
20
+ updateCurveUpdateIntensity(marketIndex: number, curveUpdateIntensity: number): Promise<TransactionSignature>;
21
+ updateMarginRatio(marketIndex: number, marginRatioInitial: number, marginRatioMaintenance: number): Promise<TransactionSignature>;
22
+ updateMarketBaseSpread(marketIndex: number, baseSpread: number): Promise<TransactionSignature>;
23
+ updateAmmJitIntensity(marketIndex: number, ammJitIntensity: number): Promise<TransactionSignature>;
24
+ updateMarketMaxSpread(marketIndex: number, maxSpread: number): Promise<TransactionSignature>;
27
25
  updatePartialLiquidationClosePercentage(numerator: BN, denominator: BN): Promise<TransactionSignature>;
28
26
  updatePartialLiquidationPenaltyPercentage(numerator: BN, denominator: BN): Promise<TransactionSignature>;
29
27
  updateFullLiquidationPenaltyPercentage(numerator: BN, denominator: BN): Promise<TransactionSignature>;
@@ -32,15 +30,15 @@ export declare class Admin extends ClearingHouse {
32
30
  updatePerpFeeStructure(feeStructure: FeeStructure): Promise<TransactionSignature>;
33
31
  updateSpotFeeStructure(feeStructure: FeeStructure): Promise<TransactionSignature>;
34
32
  updateOracleGuardRails(oracleGuardRails: OracleGuardRails): Promise<TransactionSignature>;
35
- updateWithdrawGuardThreshold(marketIndex: BN, withdrawGuardThreshold: BN): Promise<TransactionSignature>;
36
- updateSpotMarketIfFactor(marketIndex: BN, userIfFactor: BN, totalIfFactor: BN): Promise<TransactionSignature>;
37
- updateSpotMarketRevenueSettlePeriod(marketIndex: BN, revenueSettlePeriod: BN): Promise<TransactionSignature>;
38
- updateInsuranceWithdrawEscrowPeriod(marketIndex: BN, insuranceWithdrawEscrowPeriod: BN): Promise<TransactionSignature>;
39
- updateLpCooldownTime(marketIndex: BN, cooldownTime: BN): Promise<TransactionSignature>;
40
- updateMarketOracle(marketIndex: BN, oracle: PublicKey, oracleSource: OracleSource): Promise<TransactionSignature>;
41
- updateMarketMinimumQuoteAssetTradeSize(marketIndex: BN, minimumTradeSize: BN): Promise<TransactionSignature>;
42
- updateMarketBaseAssetAmountStepSize(marketIndex: BN, stepSize: BN): Promise<TransactionSignature>;
43
- updateMarketExpiry(perpMarketIndex: BN, expiryTs: BN): Promise<TransactionSignature>;
33
+ updateWithdrawGuardThreshold(marketIndex: number, withdrawGuardThreshold: BN): Promise<TransactionSignature>;
34
+ updateSpotMarketIfFactor(marketIndex: number, userIfFactor: BN, totalIfFactor: BN): Promise<TransactionSignature>;
35
+ updateSpotMarketRevenueSettlePeriod(marketIndex: number, revenueSettlePeriod: BN): Promise<TransactionSignature>;
36
+ updateInsuranceWithdrawEscrowPeriod(marketIndex: number, insuranceWithdrawEscrowPeriod: BN): Promise<TransactionSignature>;
37
+ updateLpCooldownTime(marketIndex: number, cooldownTime: BN): Promise<TransactionSignature>;
38
+ updateMarketOracle(marketIndex: number, oracle: PublicKey, oracleSource: OracleSource): Promise<TransactionSignature>;
39
+ updateMarketMinimumQuoteAssetTradeSize(marketIndex: number, minimumTradeSize: BN): Promise<TransactionSignature>;
40
+ updateMarketBaseAssetAmountStepSize(marketIndex: number, stepSize: BN): Promise<TransactionSignature>;
41
+ updateMarketExpiry(perpMarketIndex: number, expiryTs: BN): Promise<TransactionSignature>;
44
42
  updateWhitelistMint(whitelistMint?: PublicKey): Promise<TransactionSignature>;
45
43
  updateDiscountMint(discountMint: PublicKey): Promise<TransactionSignature>;
46
44
  updateMaxDeposit(maxDeposit: BN): Promise<TransactionSignature>;
@@ -49,7 +47,8 @@ export declare class Admin extends ClearingHouse {
49
47
  disableAdminControlsPrices(): Promise<TransactionSignature>;
50
48
  updatePerpAuctionDuration(minDuration: BN | number): Promise<TransactionSignature>;
51
49
  updateSpotAuctionDuration(defaultAuctionDuration: number): Promise<TransactionSignature>;
52
- updateMaxBaseAssetAmountRatio(marketIndex: BN, maxBaseAssetAmountRatio: number): Promise<TransactionSignature>;
53
- updateMaxSlippageRatio(marketIndex: BN, maxSlippageRatio: number): Promise<TransactionSignature>;
54
- updateMarketMaxImbalances(marketIndex: BN, unrealizedMaxImbalance: BN, maxRevenueWithdrawPerPeriod: BN, quoteMaxInsurance: BN): Promise<TransactionSignature>;
50
+ updateMaxBaseAssetAmountRatio(marketIndex: number, maxBaseAssetAmountRatio: number): Promise<TransactionSignature>;
51
+ updateMaxSlippageRatio(marketIndex: number, maxSlippageRatio: number): Promise<TransactionSignature>;
52
+ updateMarketMaxImbalances(marketIndex: number, unrealizedMaxImbalance: BN, maxRevenueWithdrawPerPeriod: BN, quoteMaxInsurance: BN): Promise<TransactionSignature>;
53
+ updateSerumVault(srmVault: PublicKey): Promise<TransactionSignature>;
55
54
  }
package/lib/admin.js CHANGED
@@ -42,14 +42,12 @@ class Admin extends clearingHouse_1.ClearingHouse {
42
42
  throw new Error('Clearing house already initialized');
43
43
  }
44
44
  const [clearingHouseStatePublicKey] = await (0, pda_1.getClearingHouseStateAccountPublicKeyAndNonce)(this.program.programId);
45
- const [insuranceVaultPublicKey] = await web3_js_1.PublicKey.findProgramAddress([Buffer.from(anchor.utils.bytes.utf8.encode('insurance_vault'))], this.program.programId);
46
45
  const initializeTx = await this.program.transaction.initialize(adminControlsPrices, {
47
46
  accounts: {
48
47
  admin: this.wallet.publicKey,
49
48
  state: clearingHouseStatePublicKey,
50
49
  quoteAssetMint: usdcMint,
51
50
  rent: web3_js_1.SYSVAR_RENT_PUBKEY,
52
- insuranceVault: insuranceVaultPublicKey,
53
51
  clearingHouseSigner: this.getSignerPublicKey(),
54
52
  systemProgram: anchor.web3.SystemProgram.programId,
55
53
  tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
@@ -207,46 +205,14 @@ class Admin extends clearingHouse_1.ClearingHouse {
207
205
  },
208
206
  });
209
207
  }
210
- async withdrawFromInsuranceVault(amount, recipient) {
211
- const state = await this.getStateAccount();
208
+ async depositIntoMarketFeePool(marketIndex, amount, sourceVault) {
212
209
  const spotMarket = this.getQuoteSpotMarketAccount();
213
- return await this.program.rpc.withdrawFromInsuranceVault(amount, {
214
- accounts: {
215
- admin: this.wallet.publicKey,
216
- state: await this.getStatePublicKey(),
217
- spotMarket: spotMarket.pubkey,
218
- insuranceVault: state.insuranceVault,
219
- clearingHouseSigner: this.getSignerPublicKey(),
220
- recipient: recipient,
221
- tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
222
- },
223
- });
224
- }
225
- async withdrawFromMarketToInsuranceVault(marketIndex, amount, recipient) {
226
- const marketPublicKey = await (0, pda_1.getMarketPublicKey)(this.program.programId, marketIndex);
227
- const spotMarket = this.getQuoteSpotMarketAccount();
228
- return await this.program.rpc.withdrawFromMarketToInsuranceVault(amount, {
229
- accounts: {
230
- admin: this.wallet.publicKey,
231
- state: await this.getStatePublicKey(),
232
- market: marketPublicKey,
233
- spotMarket: spotMarket.pubkey,
234
- spotMarketVault: spotMarket.vault,
235
- clearingHouseSigner: this.getSignerPublicKey(),
236
- recipient: recipient,
237
- tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
238
- },
239
- });
240
- }
241
- async withdrawFromInsuranceVaultToMarket(marketIndex, amount) {
242
- const state = await this.getStateAccount();
243
- const spotMarket = this.getQuoteSpotMarketAccount();
244
- return await this.program.rpc.withdrawFromInsuranceVaultToMarket(amount, {
210
+ return await this.program.rpc.depositIntoMarketFeePool(amount, {
245
211
  accounts: {
246
212
  admin: this.wallet.publicKey,
247
213
  state: await this.getStatePublicKey(),
248
214
  market: await (0, pda_1.getMarketPublicKey)(this.program.programId, marketIndex),
249
- insuranceVault: state.insuranceVault,
215
+ sourceVault,
250
216
  clearingHouseSigner: this.getSignerPublicKey(),
251
217
  quoteSpotMarket: spotMarket.pubkey,
252
218
  spotMarketVault: spotMarket.vault,
@@ -545,5 +511,14 @@ class Admin extends clearingHouse_1.ClearingHouse {
545
511
  },
546
512
  });
547
513
  }
514
+ async updateSerumVault(srmVault) {
515
+ return await this.program.rpc.updateSerumVault({
516
+ accounts: {
517
+ admin: this.wallet.publicKey,
518
+ state: await this.getStatePublicKey(),
519
+ srmVault: srmVault,
520
+ },
521
+ });
522
+ }
548
523
  }
549
524
  exports.Admin = Admin;