@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
@@ -46,7 +46,7 @@ import {
46
46
  calculateLiabilityWeight,
47
47
  } from './math/bankBalance';
48
48
  import {
49
- calculateMarginBaseAssetValue,
49
+ calculateBaseAssetValueWithOracle,
50
50
  calculateWorstCaseBaseAssetAmount,
51
51
  } from './math/margin';
52
52
  import { OraclePriceData } from './oracles/types';
@@ -568,7 +568,7 @@ export class ClearingHouseUser {
568
568
  const market = this.clearingHouse.getMarketAccount(
569
569
  marketPosition.marketIndex
570
570
  );
571
- const posVal = calculateMarginBaseAssetValue(
571
+ const posVal = calculateBaseAssetValueWithOracle(
572
572
  market,
573
573
  marketPosition,
574
574
  this.getOracleDataForMarket(market.marketIndex)
@@ -593,7 +593,11 @@ export class ClearingHouseUser {
593
593
  const market = this.clearingHouse.getMarketAccount(
594
594
  userPosition.marketIndex
595
595
  );
596
- return calculateMarginBaseAssetValue(market, userPosition, oraclePriceData);
596
+ return calculateBaseAssetValueWithOracle(
597
+ market,
598
+ userPosition,
599
+ oraclePriceData
600
+ );
597
601
  }
598
602
 
599
603
  public getPositionSide(
@@ -644,7 +648,7 @@ export class ClearingHouseUser {
644
648
  oraclePriceData
645
649
  );
646
650
  } else {
647
- baseAssetValue = calculateMarginBaseAssetValue(
651
+ baseAssetValue = calculateBaseAssetValueWithOracle(
648
652
  market,
649
653
  position,
650
654
  oraclePriceData
@@ -818,7 +822,7 @@ export class ClearingHouseUser {
818
822
  proposedMarketPosition.marketIndex
819
823
  );
820
824
 
821
- const proposedMarketPositionValue = calculateMarginBaseAssetValue(
825
+ const proposedMarketPositionValue = calculateBaseAssetValueWithOracle(
822
826
  market,
823
827
  proposedMarketPosition,
824
828
  this.getOracleDataForMarket(market.marketIndex)
@@ -835,7 +839,7 @@ export class ClearingHouseUser {
835
839
  const market = this.clearingHouse.getMarketAccount(
836
840
  position.marketIndex
837
841
  );
838
- const positionValue = calculateMarginBaseAssetValue(
842
+ const positionValue = calculateBaseAssetValueWithOracle(
839
843
  market,
840
844
  position,
841
845
  this.getOracleDataForMarket(market.marketIndex)
package/src/config.ts CHANGED
@@ -28,7 +28,7 @@ export const configs: { [key in DriftEnv]: DriftConfig } = {
28
28
  devnet: {
29
29
  ENV: 'devnet',
30
30
  PYTH_ORACLE_MAPPING_ADDRESS: 'BmA9Z6FjioHJPpjT39QazZyhDRUdZy2ezwx4GiDdE2u2',
31
- CLEARING_HOUSE_PROGRAM_ID: '65sz7dRiWDRPZjiRxcTxPM7AE6VK4Nag9HEK6oBJXhJn',
31
+ CLEARING_HOUSE_PROGRAM_ID: 'AXmmKr4MHipFp4SDr6CTUBzJtD1rZPHTzPDVPJ6L6r64',
32
32
  USDC_MINT_ADDRESS: '8zGuJQqwhZafTah7Uc7Z4tXRnguqkn5KLFAP8oV6PHe2',
33
33
  MARKETS: DevnetMarkets,
34
34
  BANKS: DevnetBanks,
@@ -1,5 +1,11 @@
1
1
  import { PublicKey } from '@solana/web3.js';
2
2
  import { BN, DriftEnv, OracleSource } from '../';
3
+ import {
4
+ BANK_BALANCE_PRECISION,
5
+ BANK_BALANCE_PRECISION_EXP,
6
+ LAMPORTS_EXP,
7
+ LAMPORTS_PRECISION,
8
+ } from './numericConstants';
3
9
 
4
10
  export type BankConfig = {
5
11
  symbol: string;
@@ -7,6 +13,8 @@ export type BankConfig = {
7
13
  oracle: PublicKey;
8
14
  mint: PublicKey;
9
15
  oracleSource: OracleSource;
16
+ precision: BN;
17
+ precisionExp: BN;
10
18
  };
11
19
 
12
20
  export const WRAPPED_SOL_MINT = new PublicKey(
@@ -20,6 +28,8 @@ export const DevnetBanks: BankConfig[] = [
20
28
  oracle: PublicKey.default,
21
29
  oracleSource: OracleSource.QUOTE_ASSET,
22
30
  mint: new PublicKey('8zGuJQqwhZafTah7Uc7Z4tXRnguqkn5KLFAP8oV6PHe2'),
31
+ precision: BANK_BALANCE_PRECISION,
32
+ precisionExp: BANK_BALANCE_PRECISION_EXP,
23
33
  },
24
34
  {
25
35
  symbol: 'SOL',
@@ -27,6 +37,8 @@ export const DevnetBanks: BankConfig[] = [
27
37
  oracle: new PublicKey('J83w4HKfqxwcq3BEMMkPFSppX3gqekLyLJBexebFVkix'),
28
38
  oracleSource: OracleSource.PYTH,
29
39
  mint: new PublicKey(WRAPPED_SOL_MINT),
40
+ precision: LAMPORTS_PRECISION,
41
+ precisionExp: LAMPORTS_EXP,
30
42
  },
31
43
  {
32
44
  symbol: 'BTC',
@@ -34,6 +46,8 @@ export const DevnetBanks: BankConfig[] = [
34
46
  oracle: new PublicKey('HovQMDrbAgAYPCmHVSrezcSmkMtXSSUsLDFANExrZh2J'),
35
47
  oracleSource: OracleSource.PYTH,
36
48
  mint: new PublicKey('3BZPwbcqB5kKScF3TEXxwNfx5ipV13kbRVDvfVp5c6fv'),
49
+ precision: BANK_BALANCE_PRECISION,
50
+ precisionExp: BANK_BALANCE_PRECISION_EXP,
37
51
  },
38
52
  ];
39
53
 
@@ -44,6 +58,8 @@ export const MainnetBanks: BankConfig[] = [
44
58
  oracle: PublicKey.default,
45
59
  oracleSource: OracleSource.QUOTE_ASSET,
46
60
  mint: new PublicKey('EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v'),
61
+ precision: BANK_BALANCE_PRECISION,
62
+ precisionExp: BANK_BALANCE_PRECISION_EXP,
47
63
  },
48
64
  ];
49
65
 
@@ -1,3 +1,4 @@
1
+ import { LAMPORTS_PER_SOL } from '@solana/web3.js';
1
2
  import { BN } from '../';
2
3
 
3
4
  export const ZERO = new BN(0);
@@ -66,3 +67,6 @@ export const BID_ASK_SPREAD_PRECISION = new BN(1000000);
66
67
  export const ONE_YEAR = new BN(31536000);
67
68
 
68
69
  export const QUOTE_ASSET_BANK_INDEX = new BN(0);
70
+
71
+ export const LAMPORTS_PRECISION = new BN(LAMPORTS_PER_SOL);
72
+ export const LAMPORTS_EXP = new BN(Math.log10(LAMPORTS_PER_SOL));
@@ -6,8 +6,8 @@ import {
6
6
  SortFn,
7
7
  Event,
8
8
  } from './types';
9
- import { OrderRecord } from '../types';
10
- import { PublicKey, ZERO } from '../index';
9
+ import { OrderActionRecord } from '../types';
10
+ import { ZERO } from '../index';
11
11
 
12
12
  function clientSortAscFn(): 'less than' {
13
13
  return 'less than';
@@ -24,21 +24,17 @@ function defaultBlockchainSortFn(
24
24
  return currentEvent.slot <= newEvent.slot ? 'less than' : 'greater than';
25
25
  }
26
26
 
27
- function orderRecordSortFn(
28
- currentEvent: Event<OrderRecord>,
29
- newEvent: Event<OrderRecord>
27
+ function orderActionRecordSortFn(
28
+ currentEvent: Event<OrderActionRecord>,
29
+ newEvent: Event<OrderActionRecord>
30
30
  ): 'less than' | 'greater than' {
31
- const currentEventMarketIndex = !currentEvent.maker.equals(PublicKey.default)
32
- ? currentEvent.makerOrder.marketIndex
33
- : currentEvent.takerOrder.marketIndex;
34
- const newEventMarketIndex = !newEvent.maker.equals(PublicKey.default)
35
- ? newEvent.makerOrder.marketIndex
36
- : newEvent.takerOrder.marketIndex;
31
+ const currentEventMarketIndex = currentEvent.marketIndex;
32
+ const newEventMarketIndex = newEvent.marketIndex;
37
33
  if (!currentEventMarketIndex.eq(newEventMarketIndex)) {
38
34
  return currentEvent.ts.lte(newEvent.ts) ? 'less than' : 'greater than';
39
35
  }
40
36
 
41
- if (currentEvent.fillRecordId.gt(ZERO) && newEvent.fillRecordId.gt(ZERO)) {
37
+ if (currentEvent.fillRecordId?.gt(ZERO) && newEvent.fillRecordId?.gt(ZERO)) {
42
38
  return currentEvent.fillRecordId.lte(newEvent.fillRecordId)
43
39
  ? 'less than'
44
40
  : 'greater than';
@@ -57,8 +53,8 @@ export function getSortFn(
57
53
  }
58
54
 
59
55
  switch (eventType) {
60
- case 'OrderRecord':
61
- return orderRecordSortFn;
56
+ case 'OrderActionRecord':
57
+ return orderActionRecordSortFn;
62
58
  default:
63
59
  return defaultBlockchainSortFn;
64
60
  }
@@ -5,6 +5,7 @@ import {
5
5
  FundingRateRecord,
6
6
  LiquidationRecord,
7
7
  NewUserRecord,
8
+ OrderActionRecord,
8
9
  OrderRecord,
9
10
  SettlePnlRecord,
10
11
  } from '../index';
@@ -28,6 +29,7 @@ export const DefaultEventSubscriptionOptions: EventSubscriptionOptions = {
28
29
  'FundingPaymentRecord',
29
30
  'LiquidationRecord',
30
31
  'OrderRecord',
32
+ 'OrderActionRecord',
31
33
  'FundingRateRecord',
32
34
  'NewUserRecord',
33
35
  'SettlePnlRecord',
@@ -63,6 +65,7 @@ export type EventMap = {
63
65
  LiquidationRecord: Event<LiquidationRecord>;
64
66
  FundingRateRecord: Event<FundingRateRecord>;
65
67
  OrderRecord: Event<OrderRecord>;
68
+ OrderActionRecord: Event<OrderActionRecord>;
66
69
  SettlePnlRecord: Event<SettlePnlRecord>;
67
70
  NewUserRecord: Event<NewUserRecord>;
68
71
  };