@drift-labs/sdk 0.2.0-master.22 → 0.2.0-master.23

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 (43) hide show
  1. package/lib/addresses/pda.d.ts +3 -0
  2. package/lib/addresses/pda.js +23 -1
  3. package/lib/admin.d.ts +3 -0
  4. package/lib/admin.js +31 -0
  5. package/lib/clearingHouse.d.ts +9 -2
  6. package/lib/clearingHouse.js +132 -4
  7. package/lib/clearingHouseUser.js +5 -5
  8. package/lib/config.js +1 -1
  9. package/lib/constants/banks.d.ts +2 -0
  10. package/lib/constants/banks.js +9 -0
  11. package/lib/constants/numericConstants.d.ts +2 -0
  12. package/lib/constants/numericConstants.js +4 -1
  13. package/lib/events/sort.js +7 -10
  14. package/lib/events/types.d.ts +2 -1
  15. package/lib/events/types.js +1 -0
  16. package/lib/idl/clearing_house.json +1007 -151
  17. package/lib/index.d.ts +1 -0
  18. package/lib/index.js +1 -0
  19. package/lib/math/insurance.d.ts +4 -0
  20. package/lib/math/insurance.js +27 -0
  21. package/lib/math/margin.d.ts +1 -1
  22. package/lib/math/margin.js +5 -7
  23. package/lib/math/position.js +1 -1
  24. package/lib/types.d.ts +56 -20
  25. package/lib/types.js +0 -3
  26. package/package.json +1 -1
  27. package/src/addresses/pda.ts +47 -0
  28. package/src/admin.ts +65 -0
  29. package/src/clearingHouse.ts +217 -9
  30. package/src/clearingHouseUser.ts +10 -6
  31. package/src/config.ts +1 -1
  32. package/src/constants/banks.ts +16 -0
  33. package/src/constants/numericConstants.ts +4 -0
  34. package/src/events/sort.ts +10 -14
  35. package/src/events/types.ts +3 -0
  36. package/src/idl/clearing_house.json +1007 -151
  37. package/src/index.ts +1 -0
  38. package/src/math/insurance.ts +35 -0
  39. package/src/math/margin.ts +3 -10
  40. package/src/math/position.ts +2 -2
  41. package/src/types.ts +64 -20
  42. package/src/events/eventSubscriber.js +0 -139
  43. package/src/events/sort.js +0 -44
package/lib/index.d.ts CHANGED
@@ -40,6 +40,7 @@ export * from './math/trade';
40
40
  export * from './math/orders';
41
41
  export * from './math/repeg';
42
42
  export * from './math/margin';
43
+ export * from './math/insurance';
43
44
  export * from './orderParams';
44
45
  export * from './slot/SlotSubscriber';
45
46
  export * from './wallet';
package/lib/index.js CHANGED
@@ -63,6 +63,7 @@ __exportStar(require("./math/trade"), exports);
63
63
  __exportStar(require("./math/orders"), exports);
64
64
  __exportStar(require("./math/repeg"), exports);
65
65
  __exportStar(require("./math/margin"), exports);
66
+ __exportStar(require("./math/insurance"), exports);
66
67
  __exportStar(require("./orderParams"), exports);
67
68
  __exportStar(require("./slot/SlotSubscriber"), exports);
68
69
  __exportStar(require("./wallet"), exports);
@@ -0,0 +1,4 @@
1
+ /// <reference types="bn.js" />
2
+ import { BN } from '../index';
3
+ export declare function stakeAmountToShares(amount: BN, totalIfShares: BN, insuranceFundVaultBalance: BN): BN;
4
+ export declare function unstakeSharesToAmount(nShares: BN, totalIfShares: BN, insuranceFundVaultBalance: BN): BN;
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.unstakeSharesToAmount = exports.stakeAmountToShares = void 0;
4
+ const numericConstants_1 = require("../constants/numericConstants");
5
+ const index_1 = require("../index");
6
+ function stakeAmountToShares(amount, totalIfShares, insuranceFundVaultBalance) {
7
+ let nShares;
8
+ if (insuranceFundVaultBalance.gt(numericConstants_1.ZERO)) {
9
+ nShares = amount.mul(totalIfShares).div(insuranceFundVaultBalance);
10
+ }
11
+ else {
12
+ nShares = amount;
13
+ }
14
+ return nShares;
15
+ }
16
+ exports.stakeAmountToShares = stakeAmountToShares;
17
+ function unstakeSharesToAmount(nShares, totalIfShares, insuranceFundVaultBalance) {
18
+ let amount;
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));
21
+ }
22
+ else {
23
+ amount = numericConstants_1.ZERO;
24
+ }
25
+ return amount;
26
+ }
27
+ exports.unstakeSharesToAmount = unstakeSharesToAmount;
@@ -7,5 +7,5 @@ imfFactor: BN, liabilityWeight: BN, precision: BN): BN;
7
7
  export declare function calculateSizeDiscountAssetWeight(size: BN, // AMM_RESERVE_PRECISION
8
8
  imfFactor: BN, assetWeight: BN): BN;
9
9
  export declare function calculateOraclePriceForPerpMargin(marketPosition: UserPosition, market: MarketAccount, oraclePriceData: OraclePriceData): BN;
10
- export declare function calculateMarginBaseAssetValue(market: MarketAccount, marketPosition: UserPosition, oraclePriceData: OraclePriceData): BN;
10
+ export declare function calculateBaseAssetValueWithOracle(market: MarketAccount, marketPosition: UserPosition, oraclePriceData: OraclePriceData): BN;
11
11
  export declare function calculateWorstCaseBaseAssetAmount(marketPosition: UserPosition): BN;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.calculateWorstCaseBaseAssetAmount = exports.calculateMarginBaseAssetValue = exports.calculateOraclePriceForPerpMargin = exports.calculateSizeDiscountAssetWeight = exports.calculateSizePremiumLiabilityWeight = void 0;
3
+ exports.calculateWorstCaseBaseAssetAmount = exports.calculateBaseAssetValueWithOracle = exports.calculateOraclePriceForPerpMargin = exports.calculateSizeDiscountAssetWeight = exports.calculateSizePremiumLiabilityWeight = void 0;
4
4
  const utils_1 = require("./utils");
5
5
  const numericConstants_1 = require("../constants/numericConstants");
6
6
  const anchor_1 = require("@project-serum/anchor");
@@ -50,15 +50,13 @@ function calculateOraclePriceForPerpMargin(marketPosition, market, oraclePriceDa
50
50
  return marginPrice;
51
51
  }
52
52
  exports.calculateOraclePriceForPerpMargin = calculateOraclePriceForPerpMargin;
53
- function calculateMarginBaseAssetValue(market, marketPosition, oraclePriceData) {
54
- const marginPrice = calculateOraclePriceForPerpMargin(marketPosition, market, oraclePriceData);
55
- const baseAssetValue = marketPosition.baseAssetAmount
53
+ function calculateBaseAssetValueWithOracle(market, marketPosition, oraclePriceData) {
54
+ return marketPosition.baseAssetAmount
56
55
  .abs()
57
- .mul(marginPrice)
56
+ .mul(oraclePriceData.price)
58
57
  .div(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO.mul(numericConstants_1.MARK_PRICE_PRECISION));
59
- return baseAssetValue;
60
58
  }
61
- exports.calculateMarginBaseAssetValue = calculateMarginBaseAssetValue;
59
+ exports.calculateBaseAssetValueWithOracle = calculateBaseAssetValueWithOracle;
62
60
  function calculateWorstCaseBaseAssetAmount(marketPosition) {
63
61
  const allBids = marketPosition.baseAssetAmount.add(marketPosition.openBids);
64
62
  const allAsks = marketPosition.baseAssetAmount.add(marketPosition.openAsks);
@@ -61,7 +61,7 @@ function calculatePositionPNL(market, marketPosition, withFunding = false, oracl
61
61
  if (marketPosition.baseAssetAmount.eq(numericConstants_1.ZERO)) {
62
62
  return marketPosition.quoteAssetAmount;
63
63
  }
64
- const baseAssetValue = (0, margin_1.calculateMarginBaseAssetValue)(market, marketPosition, oraclePriceData);
64
+ const baseAssetValue = (0, margin_1.calculateBaseAssetValueWithOracle)(market, marketPosition, oraclePriceData);
65
65
  const baseAssetValueSign = marketPosition.baseAssetAmount.isNeg()
66
66
  ? new __1.BN(-1)
67
67
  : new __1.BN(1);
package/lib/types.d.ts CHANGED
@@ -104,9 +104,6 @@ export declare class OrderActionExplanation {
104
104
  static readonly NONE: {
105
105
  none: {};
106
106
  };
107
- static readonly BREACHED_MARGIN_REQUIREMENT: {
108
- breachedMarginRequirement: {};
109
- };
110
107
  static readonly ORACLE_PRICE_BREACHED_LIMIT_PRICE: {
111
108
  oraclePriceBreachedLimitPrice: {};
112
109
  };
@@ -183,10 +180,15 @@ export declare type FundingRateRecord = {
183
180
  recordId: BN;
184
181
  marketIndex: BN;
185
182
  fundingRate: BN;
183
+ fundingRateLong: BN;
184
+ fundingRateShort: BN;
186
185
  cumulativeFundingRateLong: BN;
187
186
  cumulativeFundingRateShort: BN;
188
187
  oraclePriceTwap: BN;
189
188
  markPriceTwap: BN;
189
+ periodRevenue: BN;
190
+ netBaseAssetAmount: BN;
191
+ netUnsettledLpBaseAssetAmount: BN;
190
192
  };
191
193
  export declare type FundingPaymentRecord = {
192
194
  ts: BN;
@@ -294,27 +296,40 @@ export declare type SettlePnlRecord = {
294
296
  };
295
297
  export declare type OrderRecord = {
296
298
  ts: BN;
297
- taker: PublicKey;
298
- maker: PublicKey;
299
- takerOrder: Order;
300
- makerOrder: Order;
301
- takerPnl: BN;
302
- makerPnl: BN;
299
+ user: PublicKey;
300
+ order: Order;
301
+ };
302
+ export declare type OrderActionRecord = {
303
+ ts: BN;
303
304
  action: OrderAction;
304
305
  actionExplanation: OrderActionExplanation;
305
- filler: PublicKey;
306
- fillRecordId: BN;
307
306
  marketIndex: BN;
308
- baseAssetAmountFilled: BN;
309
- quoteAssetAmountFilled: BN;
310
- makerRebate: BN;
311
- takerFee: BN;
312
- fillerReward: BN;
313
- quoteAssetAmountSurplus: BN;
307
+ filler: PublicKey | null;
308
+ fillerReward: BN | null;
309
+ fillRecordId: BN | null;
310
+ referrer: PublicKey | null;
311
+ baseAssetAmountFilled: BN | null;
312
+ quoteAssetAmountFilled: BN | null;
313
+ takerPnl: BN | null;
314
+ makerPnl: BN | null;
315
+ takerFee: BN | null;
316
+ makerRebate: BN | null;
317
+ referrerReward: BN | null;
318
+ refereeDiscount: BN | null;
319
+ quoteAssetAmountSurplus: BN | null;
320
+ taker: PublicKey | null;
321
+ takerOrderId: BN | null;
322
+ takerOrderBaseAssetAmount: BN | null;
323
+ takerOrderBaseAssetAmountFilled: BN | null;
324
+ takerOrderQuoteAssetAmountFilled: BN | null;
325
+ takerOrderFee: BN | null;
326
+ maker: PublicKey | null;
327
+ makerOrderId: BN | null;
328
+ makerOrderBaseAssetAmount: BN | null;
329
+ makerOrderBaseAssetAmountFilled: BN | null;
330
+ makerOrderQuoteAssetAmountFilled: BN | null;
331
+ makerOrderFee: BN | null;
314
332
  oraclePrice: BN;
315
- referrer: PublicKey;
316
- referrerReward: BN;
317
- refereeDiscount: BN;
318
333
  };
319
334
  export declare type StateAccount = {
320
335
  admin: PublicKey;
@@ -374,6 +389,16 @@ export declare type BankAccount = {
374
389
  vault: PublicKey;
375
390
  vaultAuthority: PublicKey;
376
391
  vaultAuthorityNonce: number;
392
+ insuranceFundVault: PublicKey;
393
+ insuranceFundVaultAuthority: PublicKey;
394
+ insuranceFundVaultAuthorityNonce: number;
395
+ insuranceWithdrawEscrowPeriod: BN;
396
+ revenuePool: PoolBalance;
397
+ totalIfShares: BN;
398
+ userIfShares: BN;
399
+ userIfFactor: BN;
400
+ totalIfFactor: BN;
401
+ liquidationIfFactor: BN;
377
402
  decimals: number;
378
403
  optimalUtilization: BN;
379
404
  optimalBorrowRate: BN;
@@ -453,6 +478,7 @@ export declare type AMM = {
453
478
  maxSpread: number;
454
479
  marketPosition: UserPosition;
455
480
  marketPositionPerLp: UserPosition;
481
+ ammJitIntensity: number;
456
482
  maxBaseAssetReserve: BN;
457
483
  minBaseAssetReserve: BN;
458
484
  };
@@ -489,6 +515,7 @@ export declare type UserStatsAccount = {
489
515
  isReferrer: boolean;
490
516
  totalReferrerReward: BN;
491
517
  authority: PublicKey;
518
+ quoteAssetInsuranceFundStake: BN;
492
519
  };
493
520
  export declare type UserAccount = {
494
521
  authority: PublicKey;
@@ -665,3 +692,12 @@ export declare type OrderFillerRewardStructure = {
665
692
  timeBasedRewardLowerBound: BN;
666
693
  };
667
694
  export declare type MarginCategory = 'Initial' | 'Maintenance';
695
+ export declare type InsuranceFundStake = {
696
+ bankIndex: BN;
697
+ authority: PublicKey;
698
+ ifShares: BN;
699
+ ifBase: BN;
700
+ lastWithdrawRequestShares: BN;
701
+ lastWithdrawRequestValue: BN;
702
+ lastWithdrawRequestTs: BN;
703
+ };
package/lib/types.js CHANGED
@@ -61,9 +61,6 @@ class OrderActionExplanation {
61
61
  }
62
62
  exports.OrderActionExplanation = OrderActionExplanation;
63
63
  OrderActionExplanation.NONE = { none: {} };
64
- OrderActionExplanation.BREACHED_MARGIN_REQUIREMENT = {
65
- breachedMarginRequirement: {},
66
- };
67
64
  OrderActionExplanation.ORACLE_PRICE_BREACHED_LIMIT_PRICE = {
68
65
  oraclePriceBreachedLimitPrice: {},
69
66
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "0.2.0-master.22",
3
+ "version": "0.2.0-master.23",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -129,3 +129,50 @@ export async function getBankVaultAuthorityPublicKey(
129
129
  )
130
130
  )[0];
131
131
  }
132
+
133
+ export async function getInsuranceFundVaultPublicKey(
134
+ programId: PublicKey,
135
+ bankIndex: BN
136
+ ): Promise<PublicKey> {
137
+ return (
138
+ await anchor.web3.PublicKey.findProgramAddress(
139
+ [
140
+ Buffer.from(anchor.utils.bytes.utf8.encode('insurance_fund_vault')),
141
+ bankIndex.toArrayLike(Buffer, 'le', 8),
142
+ ],
143
+ programId
144
+ )
145
+ )[0];
146
+ }
147
+
148
+ export async function getInsuranceFundVaultAuthorityPublicKey(
149
+ programId: PublicKey,
150
+ bankIndex: BN
151
+ ): Promise<PublicKey> {
152
+ return (
153
+ await anchor.web3.PublicKey.findProgramAddress(
154
+ [
155
+ Buffer.from(
156
+ anchor.utils.bytes.utf8.encode('insurance_fund_vault_authority')
157
+ ),
158
+ bankIndex.toArrayLike(Buffer, 'le', 8),
159
+ ],
160
+ programId
161
+ )
162
+ )[0];
163
+ }
164
+
165
+ export function getInsuranceFundStakeAccountPublicKey(
166
+ programId: PublicKey,
167
+ authority: PublicKey,
168
+ bankIndex: BN
169
+ ): PublicKey {
170
+ return anchor.web3.PublicKey.findProgramAddressSync(
171
+ [
172
+ Buffer.from(anchor.utils.bytes.utf8.encode('insurance_fund_stake')),
173
+ authority.toBuffer(),
174
+ bankIndex.toArrayLike(Buffer, 'le', 8),
175
+ ],
176
+ programId
177
+ )[0];
178
+ }
package/src/admin.ts CHANGED
@@ -17,6 +17,8 @@ import {
17
17
  getBankPublicKey,
18
18
  getBankVaultPublicKey,
19
19
  getMarketPublicKey,
20
+ getInsuranceFundVaultPublicKey,
21
+ getInsuranceFundVaultAuthorityPublicKey,
20
22
  } from './addresses/pda';
21
23
  import { TOKEN_PROGRAM_ID } from '@solana/spl-token';
22
24
  import { ClearingHouse } from './clearingHouse';
@@ -102,6 +104,17 @@ export class Admin extends ClearingHouse {
102
104
  bankIndex
103
105
  );
104
106
 
107
+ const insuranceFundVault = await getInsuranceFundVaultPublicKey(
108
+ this.program.programId,
109
+ bankIndex
110
+ );
111
+
112
+ const insuranceFundVaultAuthority =
113
+ await getInsuranceFundVaultAuthorityPublicKey(
114
+ this.program.programId,
115
+ bankIndex
116
+ );
117
+
105
118
  const initializeTx = await this.program.transaction.initializeBank(
106
119
  optimalUtilization,
107
120
  optimalRate,
@@ -120,6 +133,8 @@ export class Admin extends ClearingHouse {
120
133
  bank,
121
134
  bankVault,
122
135
  bankVaultAuthority,
136
+ insuranceFundVault,
137
+ insuranceFundVaultAuthority,
123
138
  bankMint: mint,
124
139
  oracle,
125
140
  rent: SYSVAR_RENT_PUBKEY,
@@ -452,6 +467,19 @@ export class Admin extends ClearingHouse {
452
467
  });
453
468
  }
454
469
 
470
+ public async updateAmmJitIntensity(
471
+ marketIndex: BN,
472
+ ammJitIntensity: number
473
+ ): Promise<TransactionSignature> {
474
+ return await this.program.rpc.updateAmmJitIntensity(ammJitIntensity, {
475
+ accounts: {
476
+ admin: this.wallet.publicKey,
477
+ state: await this.getStatePublicKey(),
478
+ market: await getMarketPublicKey(this.program.programId, marketIndex),
479
+ },
480
+ });
481
+ }
482
+
455
483
  public async updateMarketMaxSpread(
456
484
  marketIndex: BN,
457
485
  maxSpread: number
@@ -591,6 +619,43 @@ export class Admin extends ClearingHouse {
591
619
  );
592
620
  }
593
621
 
622
+ public async updateBankIfFactor(
623
+ bankIndex: BN,
624
+ userIfFactor: BN,
625
+ totalIfFactor: BN,
626
+ liquidationIfFactor: BN
627
+ ): Promise<TransactionSignature> {
628
+ return await this.program.rpc.updateBankIfFactor(
629
+ bankIndex,
630
+ userIfFactor,
631
+ totalIfFactor,
632
+ liquidationIfFactor,
633
+ {
634
+ accounts: {
635
+ admin: this.wallet.publicKey,
636
+ state: await this.getStatePublicKey(),
637
+ bank: await getBankPublicKey(this.program.programId, bankIndex),
638
+ },
639
+ }
640
+ );
641
+ }
642
+
643
+ public async updateBankInsuranceWithdrawEscrowPeriod(
644
+ bankIndex: BN,
645
+ insuranceWithdrawEscrowPeriod: BN
646
+ ): Promise<TransactionSignature> {
647
+ return await this.program.rpc.updateBankInsuranceWithdrawEscrowPeriod(
648
+ insuranceWithdrawEscrowPeriod,
649
+ {
650
+ accounts: {
651
+ admin: this.wallet.publicKey,
652
+ state: await this.getStatePublicKey(),
653
+ bank: await getBankPublicKey(this.program.programId, bankIndex),
654
+ },
655
+ }
656
+ );
657
+ }
658
+
594
659
  public async updateLpCooldownTime(
595
660
  marketIndex: BN,
596
661
  cooldownTime: BN
@@ -40,6 +40,7 @@ import { EventEmitter } from 'events';
40
40
  import StrictEventEmitter from 'strict-event-emitter-types';
41
41
  import {
42
42
  getClearingHouseStateAccountPublicKey,
43
+ getInsuranceFundStakeAccountPublicKey,
43
44
  getMarketPublicKey,
44
45
  getUserAccountPublicKey,
45
46
  getUserAccountPublicKeySync,
@@ -2175,6 +2176,7 @@ export class ClearingHouse {
2175
2176
  public async resolvePerpBankruptcy(
2176
2177
  userAccountPublicKey: PublicKey,
2177
2178
  userAccount: UserAccount,
2179
+ bankIndex: BN,
2178
2180
  marketIndex: BN
2179
2181
  ): Promise<TransactionSignature> {
2180
2182
  const { txSig } = await this.txSender.send(
@@ -2182,6 +2184,7 @@ export class ClearingHouse {
2182
2184
  await this.getResolvePerpBankruptcyIx(
2183
2185
  userAccountPublicKey,
2184
2186
  userAccount,
2187
+ bankIndex,
2185
2188
  marketIndex
2186
2189
  )
2187
2190
  ),
@@ -2194,24 +2197,36 @@ export class ClearingHouse {
2194
2197
  public async getResolvePerpBankruptcyIx(
2195
2198
  userAccountPublicKey: PublicKey,
2196
2199
  userAccount: UserAccount,
2200
+ bankIndex: BN,
2197
2201
  marketIndex: BN
2198
2202
  ): Promise<TransactionInstruction> {
2199
2203
  const liquidatorPublicKey = await this.getUserAccountPublicKey();
2200
2204
 
2201
2205
  const remainingAccounts = this.getRemainingAccountsWithCounterparty({
2202
2206
  writableMarketIndex: marketIndex,
2207
+ writableBankIndexes: [bankIndex],
2203
2208
  counterPartyUserAccount: userAccount,
2204
2209
  });
2205
2210
 
2206
- return await this.program.instruction.resolvePerpBankruptcy(marketIndex, {
2207
- accounts: {
2208
- state: await this.getStatePublicKey(),
2209
- authority: this.wallet.publicKey,
2210
- user: userAccountPublicKey,
2211
- liquidator: liquidatorPublicKey,
2212
- },
2213
- remainingAccounts: remainingAccounts,
2214
- });
2211
+ const bank = this.getBankAccount(bankIndex);
2212
+
2213
+ return await this.program.instruction.resolvePerpBankruptcy(
2214
+ bankIndex,
2215
+ marketIndex,
2216
+ {
2217
+ accounts: {
2218
+ state: await this.getStatePublicKey(),
2219
+ authority: this.wallet.publicKey,
2220
+ user: userAccountPublicKey,
2221
+ liquidator: liquidatorPublicKey,
2222
+ bankVault: bank.vault,
2223
+ insuranceFundVault: bank.insuranceFundVault,
2224
+ insuranceFundVaultAuthority: bank.insuranceFundVaultAuthority,
2225
+ tokenProgram: TOKEN_PROGRAM_ID,
2226
+ },
2227
+ remainingAccounts: remainingAccounts,
2228
+ }
2229
+ );
2215
2230
  }
2216
2231
 
2217
2232
  public async resolveBorrowBankruptcy(
@@ -2245,12 +2260,18 @@ export class ClearingHouse {
2245
2260
  counterPartyUserAccount: userAccount,
2246
2261
  });
2247
2262
 
2263
+ const bank = this.getBankAccount(bankIndex);
2264
+
2248
2265
  return await this.program.instruction.resolveBorrowBankruptcy(bankIndex, {
2249
2266
  accounts: {
2250
2267
  state: await this.getStatePublicKey(),
2251
2268
  authority: this.wallet.publicKey,
2252
2269
  user: userAccountPublicKey,
2253
2270
  liquidator: liquidatorPublicKey,
2271
+ bankVault: bank.vault,
2272
+ insuranceFundVault: bank.insuranceFundVault,
2273
+ insuranceFundVaultAuthority: bank.insuranceFundVaultAuthority,
2274
+ tokenProgram: TOKEN_PROGRAM_ID,
2254
2275
  },
2255
2276
  remainingAccounts: remainingAccounts,
2256
2277
  });
@@ -2480,4 +2501,191 @@ export class ClearingHouse {
2480
2501
 
2481
2502
  return oracleData;
2482
2503
  }
2504
+
2505
+ public async initializeInsuranceFundStake(
2506
+ bankIndex: BN
2507
+ ): Promise<TransactionSignature> {
2508
+ const { txSig } = await this.txSender.send(
2509
+ wrapInTx(await this.getInitializeInsuranceFundStakeIx(bankIndex)),
2510
+ [],
2511
+ this.opts
2512
+ );
2513
+ return txSig;
2514
+ }
2515
+
2516
+ public async getInitializeInsuranceFundStakeIx(
2517
+ bankIndex: BN
2518
+ ): Promise<TransactionInstruction> {
2519
+ const ifStakeAccountPublicKey = getInsuranceFundStakeAccountPublicKey(
2520
+ this.program.programId,
2521
+ this.wallet.publicKey,
2522
+ bankIndex
2523
+ );
2524
+
2525
+ return await this.program.instruction.initializeInsuranceFundStake(
2526
+ bankIndex,
2527
+ {
2528
+ accounts: {
2529
+ insuranceFundStake: ifStakeAccountPublicKey,
2530
+ bank: this.getBankAccount(bankIndex).pubkey,
2531
+ userStats: this.getUserStatsAccountPublicKey(),
2532
+ authority: this.wallet.publicKey,
2533
+ payer: this.wallet.publicKey,
2534
+ rent: anchor.web3.SYSVAR_RENT_PUBKEY,
2535
+ systemProgram: anchor.web3.SystemProgram.programId,
2536
+ state: await this.getStatePublicKey(),
2537
+ },
2538
+ }
2539
+ );
2540
+ }
2541
+
2542
+ public async addInsuranceFundStake(
2543
+ bankIndex: BN,
2544
+ amount: BN,
2545
+ collateralAccountPublicKey: PublicKey
2546
+ ): Promise<TransactionSignature> {
2547
+ const bank = this.getBankAccount(bankIndex);
2548
+ const ifStakeAccountPublicKey = getInsuranceFundStakeAccountPublicKey(
2549
+ this.program.programId,
2550
+ this.wallet.publicKey,
2551
+ bankIndex
2552
+ );
2553
+
2554
+ const remainingAccounts = this.getRemainingAccounts({
2555
+ writableBankIndex: bankIndex,
2556
+ });
2557
+
2558
+ return await this.program.rpc.addInsuranceFundStake(bankIndex, amount, {
2559
+ accounts: {
2560
+ state: await this.getStatePublicKey(),
2561
+ bank: bank.pubkey,
2562
+ insuranceFundStake: ifStakeAccountPublicKey,
2563
+ userStats: this.getUserStatsAccountPublicKey(),
2564
+ authority: this.wallet.publicKey,
2565
+ insuranceFundVault: bank.insuranceFundVault,
2566
+ userTokenAccount: collateralAccountPublicKey,
2567
+ tokenProgram: TOKEN_PROGRAM_ID,
2568
+ },
2569
+ remainingAccounts,
2570
+ });
2571
+ }
2572
+
2573
+ public async requestRemoveInsuranceFundStake(
2574
+ bankIndex: BN,
2575
+ amount: BN
2576
+ ): Promise<TransactionSignature> {
2577
+ const bank = this.getBankAccount(bankIndex);
2578
+ const ifStakeAccountPublicKey = getInsuranceFundStakeAccountPublicKey(
2579
+ this.program.programId,
2580
+ this.wallet.publicKey,
2581
+ bankIndex
2582
+ );
2583
+
2584
+ const remainingAccounts = this.getRemainingAccounts({
2585
+ writableBankIndex: bankIndex,
2586
+ });
2587
+
2588
+ return await this.program.rpc.requestRemoveInsuranceFundStake(
2589
+ bankIndex,
2590
+ amount,
2591
+ {
2592
+ accounts: {
2593
+ state: await this.getStatePublicKey(),
2594
+ bank: bank.pubkey,
2595
+ insuranceFundStake: ifStakeAccountPublicKey,
2596
+ userStats: this.getUserStatsAccountPublicKey(),
2597
+ authority: this.wallet.publicKey,
2598
+ insuranceFundVault: bank.insuranceFundVault,
2599
+ // userTokenAccount: collateralAccountPublicKey,
2600
+ // tokenProgram: TOKEN_PROGRAM_ID,
2601
+ },
2602
+ remainingAccounts,
2603
+ }
2604
+ );
2605
+ }
2606
+
2607
+ public async cancelRequestRemoveInsuranceFundStake(
2608
+ bankIndex: BN
2609
+ ): Promise<TransactionSignature> {
2610
+ const bank = this.getBankAccount(bankIndex);
2611
+ const ifStakeAccountPublicKey = getInsuranceFundStakeAccountPublicKey(
2612
+ this.program.programId,
2613
+ this.wallet.publicKey,
2614
+ bankIndex
2615
+ );
2616
+
2617
+ const remainingAccounts = this.getRemainingAccounts({
2618
+ writableBankIndex: bankIndex,
2619
+ });
2620
+
2621
+ return await this.program.rpc.cancelRequestRemoveInsuranceFundStake(
2622
+ bankIndex,
2623
+ {
2624
+ accounts: {
2625
+ state: await this.getStatePublicKey(),
2626
+ bank: bank.pubkey,
2627
+ insuranceFundStake: ifStakeAccountPublicKey,
2628
+ userStats: this.getUserStatsAccountPublicKey(),
2629
+ authority: this.wallet.publicKey,
2630
+ insuranceFundVault: bank.insuranceFundVault,
2631
+ // userTokenAccount: collateralAccountPublicKey,
2632
+ // tokenProgram: TOKEN_PROGRAM_ID,
2633
+ },
2634
+ remainingAccounts,
2635
+ }
2636
+ );
2637
+ }
2638
+
2639
+ public async removeInsuranceFundStake(
2640
+ bankIndex: BN,
2641
+ collateralAccountPublicKey: PublicKey
2642
+ ): Promise<TransactionSignature> {
2643
+ const bank = this.getBankAccount(bankIndex);
2644
+ const ifStakeAccountPublicKey = getInsuranceFundStakeAccountPublicKey(
2645
+ this.program.programId,
2646
+ this.wallet.publicKey,
2647
+ bankIndex
2648
+ );
2649
+
2650
+ const remainingAccounts = this.getRemainingAccounts({
2651
+ writableBankIndex: bankIndex,
2652
+ });
2653
+
2654
+ return await this.program.rpc.removeInsuranceFundStake(bankIndex, {
2655
+ accounts: {
2656
+ state: await this.getStatePublicKey(),
2657
+ bank: bank.pubkey,
2658
+ insuranceFundStake: ifStakeAccountPublicKey,
2659
+ userStats: this.getUserStatsAccountPublicKey(),
2660
+ authority: this.wallet.publicKey,
2661
+ insuranceFundVault: bank.insuranceFundVault,
2662
+ insuranceFundVaultAuthority: bank.insuranceFundVaultAuthority,
2663
+ userTokenAccount: collateralAccountPublicKey,
2664
+ tokenProgram: TOKEN_PROGRAM_ID,
2665
+ },
2666
+ remainingAccounts,
2667
+ });
2668
+ }
2669
+
2670
+ public async settleRevenueToInsuranceFund(
2671
+ bankIndex: BN
2672
+ ): Promise<TransactionSignature> {
2673
+ const bank = this.getBankAccount(bankIndex);
2674
+
2675
+ const remainingAccounts = this.getRemainingAccounts({
2676
+ writableBankIndex: bankIndex,
2677
+ });
2678
+
2679
+ return await this.program.rpc.settleRevenueToInsuranceFund(bankIndex, {
2680
+ accounts: {
2681
+ state: await this.getStatePublicKey(),
2682
+ bank: bank.pubkey,
2683
+ bankVault: bank.vault,
2684
+ bankVaultAuthority: bank.vaultAuthority,
2685
+ insuranceFundVault: bank.insuranceFundVault,
2686
+ tokenProgram: TOKEN_PROGRAM_ID,
2687
+ },
2688
+ remainingAccounts,
2689
+ });
2690
+ }
2483
2691
  }