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

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 (53) hide show
  1. package/lib/accounts/types.d.ts +0 -1
  2. package/lib/admin.d.ts +3 -3
  3. package/lib/admin.js +5 -5
  4. package/lib/clearingHouse.d.ts +5 -0
  5. package/lib/clearingHouse.js +46 -13
  6. package/lib/clearingHouseConfig.d.ts +1 -0
  7. package/lib/clearingHouseUser.js +3 -3
  8. package/lib/config.js +1 -1
  9. package/lib/idl/clearing_house.json +122 -94
  10. package/lib/math/insurance.js +1 -1
  11. package/lib/types.d.ts +21 -38
  12. package/package.json +1 -1
  13. package/src/addresses/marketAddresses.js +26 -0
  14. package/src/admin.ts +14 -20
  15. package/src/assert/assert.js +9 -0
  16. package/src/clearingHouse.ts +74 -8
  17. package/src/clearingHouseConfig.ts +1 -0
  18. package/src/clearingHouseUser.ts +4 -4
  19. package/src/config.ts +1 -1
  20. package/src/constants/banks.js +42 -0
  21. package/src/constants/markets.js +42 -0
  22. package/src/events/eventList.js +77 -0
  23. package/src/events/txEventCache.js +71 -0
  24. package/src/examples/makeTradeExample.js +157 -0
  25. package/src/factory/bigNum.js +390 -0
  26. package/src/factory/oracleClient.js +20 -0
  27. package/src/idl/clearing_house.json +122 -94
  28. package/src/math/auction.js +42 -0
  29. package/src/math/conversion.js +11 -0
  30. package/src/math/funding.js +248 -0
  31. package/src/math/insurance.ts +2 -2
  32. package/src/math/repeg.js +128 -0
  33. package/src/math/trade.js +253 -0
  34. package/src/math/utils.js +26 -0
  35. package/src/math/utils.js.map +1 -0
  36. package/src/oracles/oracleClientCache.js +19 -0
  37. package/src/oracles/pythClient.js +46 -0
  38. package/src/oracles/quoteAssetOracleClient.js +32 -0
  39. package/src/oracles/switchboardClient.js +69 -0
  40. package/src/oracles/types.js +2 -0
  41. package/src/token/index.js +38 -0
  42. package/src/tx/types.js +2 -0
  43. package/src/tx/utils.js +17 -0
  44. package/src/types.ts +23 -39
  45. package/src/userName.js +20 -0
  46. package/src/util/computeUnits.js +27 -0
  47. package/src/util/getTokenAddress.js +9 -0
  48. package/src/util/promiseTimeout.js +14 -0
  49. package/src/util/tps.js +27 -0
  50. package/src/wallet.js +35 -0
  51. package/lib/math/state.d.ts +0 -8
  52. package/lib/math/state.js +0 -15
  53. package/src/math/state.ts +0 -14
@@ -1,6 +1,5 @@
1
1
  /// <reference types="node" />
2
2
  /// <reference types="bn.js" />
3
- /// <reference types="node" />
4
3
  import { SpotMarketAccount, PerpMarketAccount, OracleSource, StateAccount, UserAccount, UserStatsAccount } from '../types';
5
4
  import StrictEventEmitter from 'strict-event-emitter-types';
6
5
  import { EventEmitter } from 'events';
package/lib/admin.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  /// <reference types="bn.js" />
2
2
  import { PublicKey, TransactionSignature } from '@solana/web3.js';
3
- import { FeeStructure, OracleGuardRails, OracleSource, OrderFillerRewardStructure } from './types';
3
+ import { FeeStructure, OracleGuardRails, OracleSource } from './types';
4
4
  import { BN } from '@project-serum/anchor';
5
5
  import { ClearingHouse } from './clearingHouse';
6
6
  export declare class Admin extends ClearingHouse {
@@ -29,8 +29,8 @@ export declare class Admin extends ClearingHouse {
29
29
  updateFullLiquidationPenaltyPercentage(numerator: BN, denominator: BN): Promise<TransactionSignature>;
30
30
  updatePartialLiquidationShareDenominator(denominator: BN): Promise<TransactionSignature>;
31
31
  updateFullLiquidationShareDenominator(denominator: BN): Promise<TransactionSignature>;
32
- updateOrderFillerRewardStructure(orderFillerRewardStructure: OrderFillerRewardStructure): Promise<TransactionSignature>;
33
- updateFee(fees: FeeStructure): Promise<TransactionSignature>;
32
+ updatePerpFeeStructure(feeStructure: FeeStructure): Promise<TransactionSignature>;
33
+ updateSpotFeeStructure(feeStructure: FeeStructure): Promise<TransactionSignature>;
34
34
  updateOracleGuardRails(oracleGuardRails: OracleGuardRails): Promise<TransactionSignature>;
35
35
  updateWithdrawGuardThreshold(marketIndex: BN, withdrawGuardThreshold: BN): Promise<TransactionSignature>;
36
36
  updateSpotMarketIfFactor(marketIndex: BN, userIfFactor: BN, totalIfFactor: BN): Promise<TransactionSignature>;
package/lib/admin.js CHANGED
@@ -149,7 +149,7 @@ class Admin extends clearingHouse_1.ClearingHouse {
149
149
  });
150
150
  }
151
151
  async updateConcentrationScale(marketIndex, concentrationScale) {
152
- return await this.program.rpc.updateConcentrationScale(concentrationScale, {
152
+ return await this.program.rpc.updateConcentrationCoef(concentrationScale, {
153
153
  accounts: {
154
154
  state: await this.getStatePublicKey(),
155
155
  admin: this.wallet.publicKey,
@@ -349,16 +349,16 @@ class Admin extends clearingHouse_1.ClearingHouse {
349
349
  },
350
350
  });
351
351
  }
352
- async updateOrderFillerRewardStructure(orderFillerRewardStructure) {
353
- return await this.program.rpc.updateOrderFillerRewardStructure(orderFillerRewardStructure, {
352
+ async updatePerpFeeStructure(feeStructure) {
353
+ return await this.program.rpc.updatePerpFeeStructure(feeStructure, {
354
354
  accounts: {
355
355
  admin: this.wallet.publicKey,
356
356
  state: await this.getStatePublicKey(),
357
357
  },
358
358
  });
359
359
  }
360
- async updateFee(fees) {
361
- return await this.program.rpc.updateFee(fees, {
360
+ async updateSpotFeeStructure(feeStructure) {
361
+ return await this.program.rpc.updateSpotFeeStructure(feeStructure, {
362
362
  accounts: {
363
363
  admin: this.wallet.publicKey,
364
364
  state: await this.getStatePublicKey(),
@@ -2,6 +2,7 @@
2
2
  /// <reference types="bn.js" />
3
3
  import { AnchorProvider, BN, Program } from '@project-serum/anchor';
4
4
  import { StateAccount, IWallet, PositionDirection, UserAccount, PerpMarketAccount, OrderParams, Order, SpotMarketAccount, SpotPosition, MakerInfo, TakerInfo, OptionalOrderParams, ReferrerInfo, MarketType, SerumV3FulfillmentConfigAccount } from './types';
5
+ import * as anchor from '@project-serum/anchor';
5
6
  import { Connection, PublicKey, TransactionSignature, ConfirmOptions, Transaction, TransactionInstruction, AccountMeta } from '@solana/web3.js';
6
7
  import { TokenFaucet } from './tokenFaucet';
7
8
  import { EventEmitter } from 'events';
@@ -32,6 +33,7 @@ export declare class ClearingHouse {
32
33
  _isSubscribed: boolean;
33
34
  txSender: TxSender;
34
35
  marketLastSlotCache: Map<number, number>;
36
+ authority: PublicKey;
35
37
  get isSubscribed(): boolean;
36
38
  set isSubscribed(val: boolean);
37
39
  constructor(config: ClearingHouseConfig);
@@ -71,6 +73,8 @@ export declare class ClearingHouse {
71
73
  getInitializeUserStatsIx(): Promise<TransactionInstruction>;
72
74
  updateUserName(name: string, userId?: number): Promise<TransactionSignature>;
73
75
  updateUserCustomMarginRatio(marginRatio: number, userId?: number): Promise<TransactionSignature>;
76
+ updateUserDelegate(delegate: PublicKey, userId?: number): Promise<TransactionSignature>;
77
+ getUserAccountsForDelegate(delegate: PublicKey): Promise<UserAccount[]>;
74
78
  getUser(userId?: number): ClearingHouseUser;
75
79
  getUsers(): ClearingHouseUser[];
76
80
  getUserStats(): ClearingHouseUserStats;
@@ -94,6 +98,7 @@ export declare class ClearingHouse {
94
98
  private checkIfAccountExists;
95
99
  private getSolWithdrawalIxs;
96
100
  private getWrappedSolAccountCreationIxs;
101
+ getAssociatedTokenAccountCreationIx(tokenMintAddress: PublicKey, associatedTokenAddress: PublicKey): anchor.web3.TransactionInstruction;
97
102
  /**
98
103
  * Creates the Clearing House User account for a user, and deposits some initial collateral
99
104
  * @param amount
@@ -28,6 +28,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
29
  exports.ClearingHouse = void 0;
30
30
  const anchor_1 = require("@project-serum/anchor");
31
+ const bs58_1 = __importDefault(require("bs58"));
31
32
  const spl_token_1 = require("@solana/spl-token");
32
33
  const types_1 = require("./types");
33
34
  const anchor = __importStar(require("@project-serum/anchor"));
@@ -53,7 +54,7 @@ const spotPosition_1 = require("./math/spotPosition");
53
54
  */
54
55
  class ClearingHouse {
55
56
  constructor(config) {
56
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
57
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
57
58
  this.users = new Map();
58
59
  this._isSubscribed = false;
59
60
  this.marketLastSlotCache = new Map();
@@ -62,10 +63,11 @@ class ClearingHouse {
62
63
  this.opts = config.opts || anchor_1.AnchorProvider.defaultOptions();
63
64
  this.provider = new anchor_1.AnchorProvider(config.connection, config.wallet, this.opts);
64
65
  this.program = new anchor_1.Program(clearing_house_json_1.default, config.programID, this.provider);
65
- const userIds = (_a = config.userIds) !== null && _a !== void 0 ? _a : [0];
66
- this.activeUserId = (_b = config.activeUserId) !== null && _b !== void 0 ? _b : userIds[0];
66
+ this.authority = (_a = config.authority) !== null && _a !== void 0 ? _a : this.wallet.publicKey;
67
+ const userIds = (_b = config.userIds) !== null && _b !== void 0 ? _b : [0];
68
+ this.activeUserId = (_c = config.activeUserId) !== null && _c !== void 0 ? _c : userIds[0];
67
69
  this.userAccountSubscriptionConfig =
68
- ((_c = config.accountSubscription) === null || _c === void 0 ? void 0 : _c.type) === 'polling'
70
+ ((_d = config.accountSubscription) === null || _d === void 0 ? void 0 : _d.type) === 'polling'
69
71
  ? {
70
72
  type: 'polling',
71
73
  accountLoader: config.accountSubscription.accountLoader,
@@ -77,7 +79,7 @@ class ClearingHouse {
77
79
  if (config.userStats) {
78
80
  this.userStats = new clearingHouseUserStats_1.ClearingHouseUserStats({
79
81
  clearingHouse: this,
80
- userStatsAccountPublicKey: (0, pda_1.getUserStatsAccountPublicKey)(this.program.programId, this.wallet.publicKey),
82
+ userStatsAccountPublicKey: (0, pda_1.getUserStatsAccountPublicKey)(this.program.programId, this.authority),
81
83
  accountSubscription: this.userAccountSubscriptionConfig,
82
84
  });
83
85
  }
@@ -94,14 +96,14 @@ class ClearingHouse {
94
96
  : envSpotMarketIndexes;
95
97
  oracleInfos = oracleInfos ? oracleInfos : envOralceInfos;
96
98
  }
97
- if (((_d = config.accountSubscription) === null || _d === void 0 ? void 0 : _d.type) === 'polling') {
99
+ if (((_e = config.accountSubscription) === null || _e === void 0 ? void 0 : _e.type) === 'polling') {
98
100
  this.accountSubscriber = new pollingClearingHouseAccountSubscriber_1.PollingClearingHouseAccountSubscriber(this.program, config.accountSubscription.accountLoader, perpMarketIndexes !== null && perpMarketIndexes !== void 0 ? perpMarketIndexes : [], spotMarketIndexes !== null && spotMarketIndexes !== void 0 ? spotMarketIndexes : [], oracleInfos !== null && oracleInfos !== void 0 ? oracleInfos : []);
99
101
  }
100
102
  else {
101
- this.accountSubscriber = new webSocketClearingHouseAccountSubscriber_1.WebSocketClearingHouseAccountSubscriber(this.program, (_e = config.perpMarketIndexes) !== null && _e !== void 0 ? _e : [], (_f = config.spotMarketIndexes) !== null && _f !== void 0 ? _f : [], (_g = config.oracleInfos) !== null && _g !== void 0 ? _g : []);
103
+ this.accountSubscriber = new webSocketClearingHouseAccountSubscriber_1.WebSocketClearingHouseAccountSubscriber(this.program, (_f = config.perpMarketIndexes) !== null && _f !== void 0 ? _f : [], (_g = config.spotMarketIndexes) !== null && _g !== void 0 ? _g : [], (_h = config.oracleInfos) !== null && _h !== void 0 ? _h : []);
102
104
  }
103
105
  this.eventEmitter = this.accountSubscriber.eventEmitter;
104
- this.txSender = new retryTxSender_1.RetryTxSender(this.provider, (_h = config.txSenderConfig) === null || _h === void 0 ? void 0 : _h.timeout, (_j = config.txSenderConfig) === null || _j === void 0 ? void 0 : _j.retrySleep, (_k = config.txSenderConfig) === null || _k === void 0 ? void 0 : _k.additionalConnections);
106
+ this.txSender = new retryTxSender_1.RetryTxSender(this.provider, (_j = config.txSenderConfig) === null || _j === void 0 ? void 0 : _j.timeout, (_k = config.txSenderConfig) === null || _k === void 0 ? void 0 : _k.retrySleep, (_l = config.txSenderConfig) === null || _l === void 0 ? void 0 : _l.additionalConnections);
105
107
  }
106
108
  get isSubscribed() {
107
109
  return this._isSubscribed && this.accountSubscriber.isSubscribed;
@@ -116,7 +118,7 @@ class ClearingHouse {
116
118
  }
117
119
  }
118
120
  createUser(userId, accountSubscriptionConfig) {
119
- const userAccountPublicKey = (0, pda_1.getUserAccountPublicKeySync)(this.program.programId, this.wallet.publicKey, userId);
121
+ const userAccountPublicKey = (0, pda_1.getUserAccountPublicKeySync)(this.program.programId, this.authority, userId);
120
122
  return new clearingHouseUser_1.ClearingHouseUser({
121
123
  clearingHouse: this,
122
124
  userAccountPublicKey,
@@ -309,6 +311,26 @@ class ClearingHouse {
309
311
  },
310
312
  });
311
313
  }
314
+ async updateUserDelegate(delegate, userId = 0) {
315
+ return await this.program.rpc.updateUserDelegate(userId, delegate, {
316
+ accounts: {
317
+ user: await this.getUserAccountPublicKey(),
318
+ authority: this.wallet.publicKey,
319
+ },
320
+ });
321
+ }
322
+ async getUserAccountsForDelegate(delegate) {
323
+ const programAccounts = await this.program.account.user.all([
324
+ {
325
+ memcmp: {
326
+ offset: 40,
327
+ /** data to match, as base-58 encoded string and limited to less than 129 bytes */
328
+ bytes: bs58_1.default.encode(delegate.toBuffer()),
329
+ },
330
+ },
331
+ ]);
332
+ return programAccounts.map((programAccount) => programAccount.account);
333
+ }
312
334
  getUser(userId) {
313
335
  userId = userId !== null && userId !== void 0 ? userId : this.activeUserId;
314
336
  if (!this.users.has(userId)) {
@@ -326,7 +348,7 @@ class ClearingHouse {
326
348
  if (this.userStatsAccountPublicKey) {
327
349
  return this.userStatsAccountPublicKey;
328
350
  }
329
- this.userStatsAccountPublicKey = (0, pda_1.getUserStatsAccountPublicKey)(this.program.programId, this.wallet.publicKey);
351
+ this.userStatsAccountPublicKey = (0, pda_1.getUserStatsAccountPublicKey)(this.program.programId, this.authority);
330
352
  return this.userStatsAccountPublicKey;
331
353
  }
332
354
  async getUserAccountPublicKey() {
@@ -485,7 +507,7 @@ class ClearingHouse {
485
507
  const additionalSigners = [];
486
508
  const spotMarketAccount = this.getSpotMarketAccount(marketIndex);
487
509
  const isSolMarket = spotMarketAccount.mint.equals(spotMarkets_1.WRAPPED_SOL_MINT);
488
- const authority = this.wallet.publicKey;
510
+ const authority = this.authority;
489
511
  const createWSOLTokenAccount = isSolMarket && collateralAccountPublicKey.equals(authority);
490
512
  if (createWSOLTokenAccount) {
491
513
  const { ixs, signers, pubkey } = await this.getWrappedSolAccountCreationIxs(amount);
@@ -506,7 +528,7 @@ class ClearingHouse {
506
528
  }
507
529
  async getDepositInstruction(amount, marketIndex, userTokenAccount, userId, reduceOnly = false, userInitialized = true) {
508
530
  const userAccountPublicKey = userId
509
- ? await (0, pda_1.getUserAccountPublicKey)(this.program.programId, this.wallet.publicKey, userId)
531
+ ? await (0, pda_1.getUserAccountPublicKey)(this.program.programId, this.authority, userId)
510
532
  : await this.getUserAccountPublicKey();
511
533
  let remainingAccounts = [];
512
534
  if (userInitialized) {
@@ -547,7 +569,7 @@ class ClearingHouse {
547
569
  async checkIfAccountExists(account) {
548
570
  try {
549
571
  const accountInfo = await this.connection.getAccountInfo(account);
550
- return accountInfo && true;
572
+ return accountInfo != null;
551
573
  }
552
574
  catch (e) {
553
575
  // Doesn't already exist
@@ -596,6 +618,10 @@ class ClearingHouse {
596
618
  result.signers.push(wrappedSolAccount);
597
619
  return result;
598
620
  }
621
+ getAssociatedTokenAccountCreationIx(tokenMintAddress, associatedTokenAddress) {
622
+ const createAssociatedAccountIx = spl_token_1.Token.createAssociatedTokenAccountInstruction(spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID, spl_token_1.TOKEN_PROGRAM_ID, tokenMintAddress, associatedTokenAddress, this.wallet.publicKey, this.wallet.publicKey);
623
+ return createAssociatedAccountIx;
624
+ }
599
625
  /**
600
626
  * Creates the Clearing House User account for a user, and deposits some initial collateral
601
627
  * @param amount
@@ -663,6 +689,13 @@ class ClearingHouse {
663
689
  });
664
690
  signers.forEach((signer) => additionalSigners.push(signer));
665
691
  }
692
+ else {
693
+ const accountExists = await this.checkIfAccountExists(userTokenAccount);
694
+ if (!accountExists) {
695
+ const createAssociatedTokenAccountIx = this.getAssociatedTokenAccountCreationIx(spotMarketAccount.mint, userTokenAccount);
696
+ tx.add(createAssociatedTokenAccountIx);
697
+ }
698
+ }
666
699
  const withdrawCollateral = await this.getWithdrawIx(amount, spotMarketAccount.marketIndex, userTokenAccount, reduceOnly);
667
700
  tx.add(withdrawCollateral);
668
701
  // Close the wrapped sol account at the end of the transaction
@@ -19,6 +19,7 @@ export declare type ClearingHouseConfig = {
19
19
  oracleInfos?: OracleInfo[];
20
20
  env?: DriftEnv;
21
21
  userStats?: boolean;
22
+ authority?: PublicKey;
22
23
  };
23
24
  declare type ClearingHouseSubscriptionConfig = {
24
25
  type: 'websocket';
@@ -829,10 +829,10 @@ class ClearingHouseUser {
829
829
  * @returns feeForQuote : Precision QUOTE_PRECISION
830
830
  */
831
831
  calculateFeeForQuoteAmount(quoteAmount) {
832
- const feeStructure = this.clearingHouse.getStateAccount().perpFeeStructure;
832
+ const feeTier = this.clearingHouse.getStateAccount().perpFeeStructure.feeTiers[0];
833
833
  return quoteAmount
834
- .mul(feeStructure.feeNumerator)
835
- .div(feeStructure.feeDenominator);
834
+ .mul(new _1.BN(feeTier.feeNumerator))
835
+ .div(new _1.BN(feeTier.feeDenominator));
836
836
  }
837
837
  /**
838
838
  * Get the total position value, excluding any position coming from the given target market
package/lib/config.js CHANGED
@@ -7,7 +7,7 @@ exports.configs = {
7
7
  devnet: {
8
8
  ENV: 'devnet',
9
9
  PYTH_ORACLE_MAPPING_ADDRESS: 'BmA9Z6FjioHJPpjT39QazZyhDRUdZy2ezwx4GiDdE2u2',
10
- CLEARING_HOUSE_PROGRAM_ID: 'CUowJP5Dea7pkc2mKrkX2RuZUrPXL3u419iBRXyZEWQz',
10
+ CLEARING_HOUSE_PROGRAM_ID: '6MVFno8SFkVffGuCCQzg2wi8FvF8sPRFDNHa13ZPP9cK',
11
11
  USDC_MINT_ADDRESS: '8zGuJQqwhZafTah7Uc7Z4tXRnguqkn5KLFAP8oV6PHe2',
12
12
  PERP_MARKETS: perpMarkets_1.DevnetPerpMarkets,
13
13
  SPOT_MARKETS: spotMarkets_1.DevnetSpotMarkets,
@@ -1809,6 +1809,31 @@
1809
1809
  }
1810
1810
  ]
1811
1811
  },
1812
+ {
1813
+ "name": "updateUserDelegate",
1814
+ "accounts": [
1815
+ {
1816
+ "name": "user",
1817
+ "isMut": true,
1818
+ "isSigner": false
1819
+ },
1820
+ {
1821
+ "name": "authority",
1822
+ "isMut": false,
1823
+ "isSigner": true
1824
+ }
1825
+ ],
1826
+ "args": [
1827
+ {
1828
+ "name": "userId",
1829
+ "type": "u8"
1830
+ },
1831
+ {
1832
+ "name": "delegate",
1833
+ "type": "publicKey"
1834
+ }
1835
+ ]
1836
+ },
1812
1837
  {
1813
1838
  "name": "settleFundingPayment",
1814
1839
  "accounts": [
@@ -2253,7 +2278,7 @@
2253
2278
  ]
2254
2279
  },
2255
2280
  {
2256
- "name": "updateFee",
2281
+ "name": "updatePerpFeeStructure",
2257
2282
  "accounts": [
2258
2283
  {
2259
2284
  "name": "admin",
@@ -2268,7 +2293,7 @@
2268
2293
  ],
2269
2294
  "args": [
2270
2295
  {
2271
- "name": "fees",
2296
+ "name": "feeStructure",
2272
2297
  "type": {
2273
2298
  "defined": "FeeStructure"
2274
2299
  }
@@ -2276,7 +2301,7 @@
2276
2301
  ]
2277
2302
  },
2278
2303
  {
2279
- "name": "updateOrderFillerRewardStructure",
2304
+ "name": "updateSpotFeeStructure",
2280
2305
  "accounts": [
2281
2306
  {
2282
2307
  "name": "admin",
@@ -2291,9 +2316,9 @@
2291
2316
  ],
2292
2317
  "args": [
2293
2318
  {
2294
- "name": "orderFillerRewardStructure",
2319
+ "name": "feeStructure",
2295
2320
  "type": {
2296
- "defined": "OrderFillerRewardStructure"
2321
+ "defined": "FeeStructure"
2297
2322
  }
2298
2323
  }
2299
2324
  ]
@@ -2971,6 +2996,42 @@
2971
2996
  "type": "u64"
2972
2997
  }
2973
2998
  ]
2999
+ },
3000
+ {
3001
+ "name": "updateUserQuoteAssetInsuranceStake",
3002
+ "accounts": [
3003
+ {
3004
+ "name": "state",
3005
+ "isMut": false,
3006
+ "isSigner": false
3007
+ },
3008
+ {
3009
+ "name": "spotMarket",
3010
+ "isMut": false,
3011
+ "isSigner": false
3012
+ },
3013
+ {
3014
+ "name": "insuranceFundStake",
3015
+ "isMut": true,
3016
+ "isSigner": false
3017
+ },
3018
+ {
3019
+ "name": "userStats",
3020
+ "isMut": true,
3021
+ "isSigner": false
3022
+ },
3023
+ {
3024
+ "name": "authority",
3025
+ "isMut": false,
3026
+ "isSigner": true
3027
+ },
3028
+ {
3029
+ "name": "insuranceFundVault",
3030
+ "isMut": true,
3031
+ "isSigner": false
3032
+ }
3033
+ ],
3034
+ "args": []
2974
3035
  }
2975
3036
  ],
2976
3037
  "accounts": [
@@ -3447,18 +3508,6 @@
3447
3508
  "name": "insuranceVault",
3448
3509
  "type": "publicKey"
3449
3510
  },
3450
- {
3451
- "name": "perpFeeStructure",
3452
- "type": {
3453
- "defined": "FeeStructure"
3454
- }
3455
- },
3456
- {
3457
- "name": "spotFeeStructure",
3458
- "type": {
3459
- "defined": "FeeStructure"
3460
- }
3461
- },
3462
3511
  {
3463
3512
  "name": "whitelistMint",
3464
3513
  "type": "publicKey"
@@ -3512,6 +3561,18 @@
3512
3561
  {
3513
3562
  "name": "signerNonce",
3514
3563
  "type": "u8"
3564
+ },
3565
+ {
3566
+ "name": "perpFeeStructure",
3567
+ "type": {
3568
+ "defined": "FeeStructure"
3569
+ }
3570
+ },
3571
+ {
3572
+ "name": "spotFeeStructure",
3573
+ "type": {
3574
+ "defined": "FeeStructure"
3575
+ }
3515
3576
  }
3516
3577
  ]
3517
3578
  }
@@ -3525,6 +3586,10 @@
3525
3586
  "name": "authority",
3526
3587
  "type": "publicKey"
3527
3588
  },
3589
+ {
3590
+ "name": "delegate",
3591
+ "type": "publicKey"
3592
+ },
3528
3593
  {
3529
3594
  "name": "userId",
3530
3595
  "type": "u8"
@@ -3619,6 +3684,14 @@
3619
3684
  "name": "totalReferrerReward",
3620
3685
  "type": "u64"
3621
3686
  },
3687
+ {
3688
+ "name": "currentEpochReferrerReward",
3689
+ "type": "u64"
3690
+ },
3691
+ {
3692
+ "name": "nextEpochTs",
3693
+ "type": "i64"
3694
+ },
3622
3695
  {
3623
3696
  "name": "fees",
3624
3697
  "type": {
@@ -3650,8 +3723,8 @@
3650
3723
  "type": "i64"
3651
3724
  },
3652
3725
  {
3653
- "name": "quoteAssetInsuranceFundStake",
3654
- "type": "u128"
3726
+ "name": "stakedQuoteAssetAmount",
3727
+ "type": "u64"
3655
3728
  }
3656
3729
  ]
3657
3730
  }
@@ -4342,39 +4415,26 @@
4342
4415
  "kind": "struct",
4343
4416
  "fields": [
4344
4417
  {
4345
- "name": "feeNumerator",
4346
- "type": "u128"
4347
- },
4348
- {
4349
- "name": "feeDenominator",
4350
- "type": "u128"
4351
- },
4352
- {
4353
- "name": "discountTokenTiers",
4418
+ "name": "feeTiers",
4354
4419
  "type": {
4355
- "defined": "DiscountTokenTiers"
4356
- }
4357
- },
4358
- {
4359
- "name": "referralDiscount",
4360
- "type": {
4361
- "defined": "ReferralDiscount"
4420
+ "array": [
4421
+ {
4422
+ "defined": "FeeTier"
4423
+ },
4424
+ 10
4425
+ ]
4362
4426
  }
4363
4427
  },
4364
- {
4365
- "name": "makerRebateNumerator",
4366
- "type": "u128"
4367
- },
4368
- {
4369
- "name": "makerRebateDenominator",
4370
- "type": "u128"
4371
- },
4372
4428
  {
4373
4429
  "name": "fillerRewardStructure",
4374
4430
  "type": {
4375
4431
  "defined": "OrderFillerRewardStructure"
4376
4432
  }
4377
4433
  },
4434
+ {
4435
+ "name": "referrerRewardEpochUpperBound",
4436
+ "type": "u64"
4437
+ },
4378
4438
  {
4379
4439
  "name": "flatFillerFee",
4380
4440
  "type": "u128"
@@ -4383,77 +4443,41 @@
4383
4443
  }
4384
4444
  },
4385
4445
  {
4386
- "name": "DiscountTokenTiers",
4446
+ "name": "FeeTier",
4387
4447
  "type": {
4388
4448
  "kind": "struct",
4389
4449
  "fields": [
4390
4450
  {
4391
- "name": "firstTier",
4392
- "type": {
4393
- "defined": "DiscountTokenTier"
4394
- }
4395
- },
4396
- {
4397
- "name": "secondTier",
4398
- "type": {
4399
- "defined": "DiscountTokenTier"
4400
- }
4451
+ "name": "feeNumerator",
4452
+ "type": "u32"
4401
4453
  },
4402
4454
  {
4403
- "name": "thirdTier",
4404
- "type": {
4405
- "defined": "DiscountTokenTier"
4406
- }
4455
+ "name": "feeDenominator",
4456
+ "type": "u32"
4407
4457
  },
4408
4458
  {
4409
- "name": "fourthTier",
4410
- "type": {
4411
- "defined": "DiscountTokenTier"
4412
- }
4413
- }
4414
- ]
4415
- }
4416
- },
4417
- {
4418
- "name": "DiscountTokenTier",
4419
- "type": {
4420
- "kind": "struct",
4421
- "fields": [
4422
- {
4423
- "name": "minimumBalance",
4424
- "type": "u64"
4459
+ "name": "makerRebateNumerator",
4460
+ "type": "u32"
4425
4461
  },
4426
4462
  {
4427
- "name": "discountNumerator",
4428
- "type": "u128"
4463
+ "name": "makerRebateDenominator",
4464
+ "type": "u32"
4429
4465
  },
4430
- {
4431
- "name": "discountDenominator",
4432
- "type": "u128"
4433
- }
4434
- ]
4435
- }
4436
- },
4437
- {
4438
- "name": "ReferralDiscount",
4439
- "type": {
4440
- "kind": "struct",
4441
- "fields": [
4442
4466
  {
4443
4467
  "name": "referrerRewardNumerator",
4444
- "type": "u128"
4468
+ "type": "u32"
4445
4469
  },
4446
4470
  {
4447
4471
  "name": "referrerRewardDenominator",
4448
- "type": "u128"
4472
+ "type": "u32"
4449
4473
  },
4450
4474
  {
4451
- "name": "refereeDiscountNumerator",
4452
- "type": "u128"
4475
+ "name": "refereeFeeNumerator",
4476
+ "type": "u32"
4453
4477
  },
4454
4478
  {
4455
- "name": "refereeDiscountDenominator",
4456
- "type": "u128"
4479
+ "name": "refereeFeeDenominator",
4480
+ "type": "u32"
4457
4481
  }
4458
4482
  ]
4459
4483
  }
@@ -4536,6 +4560,10 @@
4536
4560
  {
4537
4561
  "name": "openAsks",
4538
4562
  "type": "i128"
4563
+ },
4564
+ {
4565
+ "name": "cumulativeDeposits",
4566
+ "type": "i64"
4539
4567
  }
4540
4568
  ]
4541
4569
  }
@@ -17,7 +17,7 @@ exports.stakeAmountToShares = stakeAmountToShares;
17
17
  function unstakeSharesToAmount(nShares, totalIfShares, insuranceFundVaultBalance) {
18
18
  let amount;
19
19
  if (totalIfShares.gt(numericConstants_1.ZERO)) {
20
- amount = index_1.BN.max(numericConstants_1.ZERO, nShares.mul(insuranceFundVaultBalance).div(totalIfShares).sub(numericConstants_1.ONE));
20
+ amount = index_1.BN.max(numericConstants_1.ZERO, nShares.mul(insuranceFundVaultBalance).div(totalIfShares));
21
21
  }
22
22
  else {
23
23
  amount = numericConstants_1.ZERO;