@drift-labs/sdk 0.1.23-master.3 → 0.1.25-master.0

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 (61) hide show
  1. package/lib/accounts/bulkAccountLoader.d.ts +2 -0
  2. package/lib/accounts/bulkAccountLoader.js +26 -11
  3. package/lib/addresses.js +5 -1
  4. package/lib/admin.d.ts +2 -2
  5. package/lib/admin.js +11 -5
  6. package/lib/clearingHouse.js +7 -1
  7. package/lib/clearingHouseUser.d.ts +12 -17
  8. package/lib/clearingHouseUser.js +114 -217
  9. package/lib/constants/markets.d.ts +4 -4
  10. package/lib/constants/markets.js +22 -0
  11. package/lib/constants/numericConstants.d.ts +2 -2
  12. package/lib/constants/numericConstants.js +3 -3
  13. package/lib/factory/oracleClient.d.ts +5 -0
  14. package/lib/factory/oracleClient.js +16 -0
  15. package/lib/idl/clearing_house.json +53 -5
  16. package/lib/idl/switchboard_v2.json +4663 -0
  17. package/lib/index.d.ts +4 -1
  18. package/lib/index.js +9 -2
  19. package/lib/math/funding.d.ts +6 -6
  20. package/lib/math/funding.js +4 -16
  21. package/lib/math/orders.d.ts +1 -0
  22. package/lib/math/orders.js +19 -1
  23. package/lib/mockUSDCFaucet.js +5 -1
  24. package/lib/oracles/pythClient.d.ts +14 -0
  25. package/lib/oracles/pythClient.js +53 -0
  26. package/lib/oracles/switchboardClient.d.ts +13 -0
  27. package/lib/oracles/switchboardClient.js +76 -0
  28. package/lib/oracles/types.d.ts +15 -0
  29. package/lib/oracles/types.js +2 -0
  30. package/lib/orderParams.d.ts +1 -1
  31. package/lib/orderParams.js +2 -2
  32. package/lib/orders.js +1 -1
  33. package/lib/types.d.ts +5 -1
  34. package/package.json +2 -1
  35. package/src/accounts/bulkAccountLoader.ts +37 -13
  36. package/src/accounts/types.js +10 -0
  37. package/src/accounts/utils.js +7 -0
  38. package/src/accounts/webSocketAccountSubscriber.js +76 -0
  39. package/src/addresses.js +83 -0
  40. package/src/admin.ts +15 -4
  41. package/src/clearingHouse.ts +2 -0
  42. package/src/clearingHouseUser.ts +161 -330
  43. package/src/constants/markets.ts +26 -3
  44. package/src/constants/numericConstants.ts +2 -2
  45. package/src/factory/oracleClient.ts +22 -0
  46. package/src/idl/clearing_house.json +53 -5
  47. package/src/idl/switchboard_v2.json +4663 -0
  48. package/src/index.ts +4 -1
  49. package/src/math/funding.ts +9 -25
  50. package/src/math/orders.ts +28 -0
  51. package/src/mockUSDCFaucet.js +171 -0
  52. package/src/oracles/pythClient.ts +49 -0
  53. package/src/oracles/switchboardClient.ts +87 -0
  54. package/src/oracles/types.ts +15 -0
  55. package/src/orderParams.ts +3 -2
  56. package/src/orders.ts +1 -1
  57. package/src/types.js +60 -0
  58. package/src/types.ts +6 -1
  59. package/lib/pythClient.d.ts +0 -7
  60. package/lib/pythClient.js +0 -25
  61. package/src/pythClient.ts +0 -15
@@ -1,15 +1,16 @@
1
- import { BN } from '../';
1
+ import { BN, OracleSource } from '../';
2
2
 
3
- type Market = {
3
+ export type MarketConfig = {
4
4
  symbol: string;
5
5
  baseAssetSymbol: string;
6
6
  marketIndex: BN;
7
7
  devnetPythOracle: string;
8
8
  mainnetPythOracle: string;
9
9
  launchTs: number;
10
+ oracleSource: OracleSource;
10
11
  };
11
12
 
12
- export const Markets: Market[] = [
13
+ export const Markets: MarketConfig[] = [
13
14
  {
14
15
  symbol: 'SOL-PERP',
15
16
  baseAssetSymbol: 'SOL',
@@ -17,6 +18,7 @@ export const Markets: Market[] = [
17
18
  devnetPythOracle: 'J83w4HKfqxwcq3BEMMkPFSppX3gqekLyLJBexebFVkix',
18
19
  mainnetPythOracle: 'H6ARHf6YXhGYeQfUzQNGk6rDNnLBQKrenN712K4AQJEG',
19
20
  launchTs: 1635209696886,
21
+ oracleSource: OracleSource.PYTH,
20
22
  },
21
23
  {
22
24
  symbol: 'BTC-PERP',
@@ -25,6 +27,7 @@ export const Markets: Market[] = [
25
27
  devnetPythOracle: 'HovQMDrbAgAYPCmHVSrezcSmkMtXSSUsLDFANExrZh2J',
26
28
  mainnetPythOracle: 'GVXRSBjFk6e6J3NbVPXohDJetcTjaeeuykUpbQF8UoMU',
27
29
  launchTs: 1637691088868,
30
+ oracleSource: OracleSource.PYTH,
28
31
  },
29
32
  {
30
33
  symbol: 'ETH-PERP',
@@ -33,6 +36,7 @@ export const Markets: Market[] = [
33
36
  devnetPythOracle: 'EdVCmQ9FSPcVe5YySXDPCRmc8aDQLKJ9xvYBMZPie1Vw',
34
37
  mainnetPythOracle: 'JBu1AL4obBcCMqKBBxhpWCNUt136ijcuMZLFvTP7iWdB',
35
38
  launchTs: 1637691133472,
39
+ oracleSource: OracleSource.PYTH,
36
40
  },
37
41
  {
38
42
  symbol: 'LUNA-PERP',
@@ -41,6 +45,7 @@ export const Markets: Market[] = [
41
45
  devnetPythOracle: '8PugCXTAHLM9kfLSQWe2njE5pzAgUdpPk3Nx5zSm7BD3',
42
46
  mainnetPythOracle: '5bmWuR1dgP4avtGYMNKLuxumZTVKGgoN2BCMXWDNL9nY',
43
47
  launchTs: 1638821738525,
48
+ oracleSource: OracleSource.PYTH,
44
49
  },
45
50
  {
46
51
  symbol: 'AVAX-PERP',
@@ -49,6 +54,7 @@ export const Markets: Market[] = [
49
54
  devnetPythOracle: 'FVb5h1VmHPfVb1RfqZckchq18GxRv4iKt8T4eVTQAqdz',
50
55
  mainnetPythOracle: 'Ax9ujW5B9oqcv59N8m6f1BpTBq2rGeGaBcpKjC5UYsXU',
51
56
  launchTs: 1639092501080,
57
+ oracleSource: OracleSource.PYTH,
52
58
  },
53
59
  {
54
60
  symbol: 'BNB-PERP',
@@ -57,6 +63,7 @@ export const Markets: Market[] = [
57
63
  devnetPythOracle: 'GwzBgrXb4PG59zjce24SF2b9JXbLEjJJTBkmytuEZj1b',
58
64
  mainnetPythOracle: '4CkQJBxhU8EZ2UjhigbtdaPbpTe6mqf811fipYBFbSYN',
59
65
  launchTs: 1639523193012,
66
+ oracleSource: OracleSource.PYTH,
60
67
  },
61
68
  {
62
69
  symbol: 'MATIC-PERP',
@@ -65,6 +72,7 @@ export const Markets: Market[] = [
65
72
  devnetPythOracle: 'FBirwuDFuRAu4iSGc7RGxN5koHB7EJM1wbCmyPuQoGur',
66
73
  mainnetPythOracle: '7KVswB9vkCgeM3SHP7aGDijvdRAHK8P5wi9JXViCrtYh',
67
74
  launchTs: 1641488603564,
75
+ oracleSource: OracleSource.PYTH,
68
76
  },
69
77
  {
70
78
  symbol: 'ATOM-PERP',
@@ -73,6 +81,7 @@ export const Markets: Market[] = [
73
81
  devnetPythOracle: '7YAze8qFUMkBnyLVdKT4TFUUFui99EwS5gfRArMcrvFk',
74
82
  mainnetPythOracle: 'CrCpTerNqtZvqLcKqz1k13oVeXV9WkMD2zA9hBKXrsbN',
75
83
  launchTs: 1641920238195,
84
+ oracleSource: OracleSource.PYTH,
76
85
  },
77
86
  {
78
87
  symbol: 'DOT-PERP',
@@ -81,6 +90,7 @@ export const Markets: Market[] = [
81
90
  devnetPythOracle: '4dqq5VBpN4EwYb7wyywjjfknvMKu7m78j9mKZRXTj462',
82
91
  mainnetPythOracle: 'EcV1X1gY2yb4KXxjVQtTHTbioum2gvmPnFk4zYAt7zne',
83
92
  launchTs: 1642629253786,
93
+ oracleSource: OracleSource.PYTH,
84
94
  },
85
95
  {
86
96
  symbol: 'ADA-PERP',
@@ -89,6 +99,7 @@ export const Markets: Market[] = [
89
99
  devnetPythOracle: '8oGTURNmSQkrBS1AQ5NjB2p8qY34UVmMA9ojrw8vnHus',
90
100
  mainnetPythOracle: '3pyn4svBbxJ9Wnn3RVeafyLWfzie6yC5eTig2S62v9SC',
91
101
  launchTs: 1643084413000,
102
+ oracleSource: OracleSource.PYTH,
92
103
  },
93
104
  {
94
105
  symbol: 'ALGO-PERP',
@@ -97,6 +108,7 @@ export const Markets: Market[] = [
97
108
  devnetPythOracle: 'c1A946dY5NHuVda77C8XXtXytyR3wK1SCP3eA9VRfC3',
98
109
  mainnetPythOracle: 'HqFyq1wh1xKvL7KDqqT7NJeSPdAqsDqnmBisUC2XdXAX',
99
110
  launchTs: 1643686767000,
111
+ oracleSource: OracleSource.PYTH,
100
112
  },
101
113
  {
102
114
  symbol: 'FTT-PERP',
@@ -105,6 +117,7 @@ export const Markets: Market[] = [
105
117
  devnetPythOracle: '6vivTRs5ZPeeXbjo7dfburfaYDWoXjBtdtuYgQRuGfu',
106
118
  mainnetPythOracle: '8JPJJkmDScpcNmBRKGZuPuG2GYAveQgP3t5gFuMymwvF',
107
119
  launchTs: 1644382122000,
120
+ oracleSource: OracleSource.PYTH,
108
121
  },
109
122
  {
110
123
  symbol: 'LTC-PERP',
@@ -113,6 +126,16 @@ export const Markets: Market[] = [
113
126
  devnetPythOracle: 'BLArYBCUYhdWiY8PCUTpvFE21iaJq85dvxLk9bYMobcU',
114
127
  mainnetPythOracle: '8RMnV1eD55iqUFJLMguPkYBkq8DCtx81XcmAja93LvRR',
115
128
  launchTs: 1645027429000,
129
+ oracleSource: OracleSource.PYTH,
130
+ },
131
+ {
132
+ symbol: 'XRP-PERP',
133
+ baseAssetSymbol: 'XRP',
134
+ marketIndex: new BN(13),
135
+ devnetPythOracle: 'WMW5xc3HypXwTnPesyUT49uLsyHwNURsWAEk39onKuk',
136
+ mainnetPythOracle: 'WMW5xc3HypXwTnPesyUT49uLsyHwNURsWAEk39onKuk',
137
+ launchTs: 1647543166000,
138
+ oracleSource: OracleSource.SWITCHBOARD,
116
139
  },
117
140
  // {
118
141
  // symbol: 'mSOL-PERP',
@@ -3,12 +3,11 @@ import { BN } from '../';
3
3
  export const ZERO = new BN(0);
4
4
  export const ONE = new BN(1);
5
5
  export const TWO = new BN(2);
6
+ export const TEN = new BN(10);
6
7
  export const TEN_THOUSAND = new BN(10000);
7
8
  export const BN_MAX = new BN(Number.MAX_SAFE_INTEGER);
8
9
 
9
10
  export const MAX_LEVERAGE = new BN(5);
10
- export const FULL_LIQUIDATION_RATIO = new BN(500);
11
- export const PARTIAL_LIQUIDATION_RATIO = new BN(625);
12
11
 
13
12
  export const QUOTE_PRECISION = new BN(10 ** 6);
14
13
  export const MARK_PRICE_PRECISION = new BN(10 ** 10);
@@ -23,3 +22,4 @@ export const PRICE_TO_QUOTE_PRECISION =
23
22
  MARK_PRICE_PRECISION.div(QUOTE_PRECISION);
24
23
  export const AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO =
25
24
  AMM_RESERVE_PRECISION.mul(PEG_PRECISION).div(QUOTE_PRECISION); // 10^10
25
+ export const MARGIN_PRECISION = TEN_THOUSAND;
@@ -0,0 +1,22 @@
1
+ import { isVariant, OracleSource } from '../types';
2
+ import { Connection } from '@solana/web3.js';
3
+ import { DriftEnv } from '../config';
4
+ import { OracleClient } from '../oracles/types';
5
+ import { PythClient } from '../oracles/pythClient';
6
+ import { SwitchboardClient } from '../oracles/switchboardClient';
7
+
8
+ export function getOracleClient(
9
+ oracleSource: OracleSource,
10
+ connection: Connection,
11
+ env: DriftEnv
12
+ ): OracleClient {
13
+ if (isVariant(oracleSource, 'pyth')) {
14
+ return new PythClient(connection);
15
+ }
16
+
17
+ if (isVariant(oracleSource, 'switchboard')) {
18
+ return new SwitchboardClient(connection, env);
19
+ }
20
+
21
+ throw new Error(`Unknown oracle source ${oracleSource}`);
22
+ }
@@ -211,6 +211,24 @@
211
211
  {
212
212
  "name": "ammPegMultiplier",
213
213
  "type": "u128"
214
+ },
215
+ {
216
+ "name": "oracleSource",
217
+ "type": {
218
+ "defined": "OracleSource"
219
+ }
220
+ },
221
+ {
222
+ "name": "marginRatioInitial",
223
+ "type": "u32"
224
+ },
225
+ {
226
+ "name": "marginRatioPartial",
227
+ "type": "u32"
228
+ },
229
+ {
230
+ "name": "marginRatioMaintenance",
231
+ "type": "u32"
214
232
  }
215
233
  ]
216
234
  },
@@ -1526,22 +1544,31 @@
1526
1544
  },
1527
1545
  {
1528
1546
  "name": "state",
1547
+ "isMut": false,
1548
+ "isSigner": false
1549
+ },
1550
+ {
1551
+ "name": "markets",
1529
1552
  "isMut": true,
1530
1553
  "isSigner": false
1531
1554
  }
1532
1555
  ],
1533
1556
  "args": [
1557
+ {
1558
+ "name": "marketIndex",
1559
+ "type": "u64"
1560
+ },
1534
1561
  {
1535
1562
  "name": "marginRatioInitial",
1536
- "type": "u128"
1563
+ "type": "u32"
1537
1564
  },
1538
1565
  {
1539
1566
  "name": "marginRatioPartial",
1540
- "type": "u128"
1567
+ "type": "u32"
1541
1568
  },
1542
1569
  {
1543
1570
  "name": "marginRatioMaintenance",
1544
- "type": "u128"
1571
+ "type": "u32"
1545
1572
  }
1546
1573
  ]
1547
1574
  },
@@ -3028,9 +3055,21 @@
3028
3055
  "defined": "AMM"
3029
3056
  }
3030
3057
  },
3058
+ {
3059
+ "name": "marginRatioInitial",
3060
+ "type": "u32"
3061
+ },
3062
+ {
3063
+ "name": "marginRatioPartial",
3064
+ "type": "u32"
3065
+ },
3066
+ {
3067
+ "name": "marginRatioMaintenance",
3068
+ "type": "u32"
3069
+ },
3031
3070
  {
3032
3071
  "name": "padding0",
3033
- "type": "u128"
3072
+ "type": "u32"
3034
3073
  },
3035
3074
  {
3036
3075
  "name": "padding1",
@@ -3222,12 +3261,16 @@
3222
3261
  "name": "fillerReward",
3223
3262
  "type": "u128"
3224
3263
  },
3264
+ {
3265
+ "name": "quoteAssetAmountSurplus",
3266
+ "type": "u128"
3267
+ },
3225
3268
  {
3226
3269
  "name": "padding",
3227
3270
  "type": {
3228
3271
  "array": [
3229
3272
  "u64",
3230
- 10
3273
+ 8
3231
3274
  ]
3232
3275
  }
3233
3276
  }
@@ -4132,6 +4175,11 @@
4132
4175
  "code": 6056,
4133
4176
  "name": "NoPositionsLiquidatable",
4134
4177
  "msg": "No positions liquidatable"
4178
+ },
4179
+ {
4180
+ "code": 6057,
4181
+ "name": "InvalidMarginRatio",
4182
+ "msg": "Invalid Margin Ratio"
4135
4183
  }
4136
4184
  ]
4137
4185
  }