@drift-labs/sdk 0.1.24 → 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 (63) hide show
  1. package/lib/accounts/bulkAccountLoader.d.ts +1 -0
  2. package/lib/accounts/bulkAccountLoader.js +16 -2
  3. package/lib/accounts/pollingClearingHouseAccountSubscriber.js +3 -3
  4. package/lib/accounts/pollingTokenAccountSubscriber.js +2 -2
  5. package/lib/accounts/pollingUserAccountSubscriber.js +4 -4
  6. package/lib/accounts/webSocketAccountSubscriber.js +2 -2
  7. package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +1 -1
  8. package/lib/accounts/webSocketUserAccountSubscriber.js +2 -2
  9. package/lib/addresses.js +5 -1
  10. package/lib/admin.d.ts +1 -1
  11. package/lib/admin.js +15 -10
  12. package/lib/clearingHouse.js +26 -20
  13. package/lib/clearingHouseUser.js +18 -18
  14. package/lib/constants/markets.d.ts +4 -4
  15. package/lib/constants/markets.js +22 -0
  16. package/lib/constants/numericConstants.d.ts +1 -0
  17. package/lib/constants/numericConstants.js +2 -1
  18. package/lib/examples/makeTradeExample.js +6 -6
  19. package/lib/factory/oracleClient.d.ts +5 -0
  20. package/lib/factory/oracleClient.js +16 -0
  21. package/lib/idl/clearing_house.json +11 -1
  22. package/lib/idl/switchboard_v2.json +4663 -0
  23. package/lib/index.d.ts +4 -1
  24. package/lib/index.js +9 -2
  25. package/lib/math/amm.js +12 -12
  26. package/lib/math/conversion.js +1 -1
  27. package/lib/math/funding.d.ts +6 -6
  28. package/lib/math/funding.js +5 -17
  29. package/lib/math/market.js +2 -2
  30. package/lib/math/orders.js +9 -9
  31. package/lib/math/position.js +1 -1
  32. package/lib/math/trade.js +18 -18
  33. package/lib/mockUSDCFaucet.js +5 -1
  34. package/lib/oracles/pythClient.d.ts +14 -0
  35. package/lib/oracles/pythClient.js +53 -0
  36. package/lib/oracles/switchboardClient.d.ts +13 -0
  37. package/lib/oracles/switchboardClient.js +76 -0
  38. package/lib/oracles/types.d.ts +15 -0
  39. package/lib/oracles/types.js +2 -0
  40. package/lib/orderParams.d.ts +1 -1
  41. package/lib/orderParams.js +2 -2
  42. package/lib/orders.js +19 -19
  43. package/lib/tx/retryTxSender.js +1 -1
  44. package/lib/types.d.ts +1 -0
  45. package/package.json +2 -1
  46. package/src/accounts/bulkAccountLoader.ts +19 -0
  47. package/src/admin.ts +2 -0
  48. package/src/clearingHouse.ts +2 -0
  49. package/src/constants/markets.ts +26 -3
  50. package/src/constants/numericConstants.ts +1 -0
  51. package/src/factory/oracleClient.ts +22 -0
  52. package/src/idl/clearing_house.json +11 -1
  53. package/src/idl/switchboard_v2.json +4663 -0
  54. package/src/index.ts +4 -1
  55. package/src/math/funding.ts +9 -25
  56. package/src/oracles/pythClient.ts +49 -0
  57. package/src/oracles/switchboardClient.ts +87 -0
  58. package/src/oracles/types.ts +15 -0
  59. package/src/orderParams.ts +3 -2
  60. package/src/types.ts +1 -0
  61. package/lib/pythClient.d.ts +0 -7
  62. package/lib/pythClient.js +0 -25
  63. package/src/pythClient.ts +0 -15
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getMarketOrderParams = exports.getTriggerLimitOrderParams = exports.getTriggerMarketOrderParams = exports.getLimitOrderParams = void 0;
4
4
  const types_1 = require("./types");
5
5
  const numericConstants_1 = require("./constants/numericConstants");
6
- function getLimitOrderParams(marketIndex, direction, baseAssetAmount, price, reduceOnly, discountToken = false, referrer = false, userOrderId = 0) {
6
+ function getLimitOrderParams(marketIndex, direction, baseAssetAmount, price, reduceOnly, discountToken = false, referrer = false, userOrderId = 0, postOnly = false) {
7
7
  return {
8
8
  orderType: types_1.OrderType.LIMIT,
9
9
  userOrderId,
@@ -13,7 +13,7 @@ function getLimitOrderParams(marketIndex, direction, baseAssetAmount, price, red
13
13
  baseAssetAmount,
14
14
  price,
15
15
  reduceOnly,
16
- postOnly: false,
16
+ postOnly,
17
17
  immediateOrCancel: false,
18
18
  positionLimit: numericConstants_1.ZERO,
19
19
  padding0: true,
package/lib/orders.js CHANGED
@@ -8,7 +8,7 @@ const numericConstants_1 = require("./constants/numericConstants");
8
8
  const amm_1 = require("./math/amm");
9
9
  const position_1 = require("./math/position");
10
10
  function calculateNewStateAfterOrder(userAccount, userPosition, market, order) {
11
- if (types_1.isVariant(order.status, 'init')) {
11
+ if ((0, types_1.isVariant)(order.status, 'init')) {
12
12
  return null;
13
13
  }
14
14
  const baseAssetAmountToTrade = calculateBaseAssetAmountMarketCanExecute(market, order);
@@ -17,11 +17,11 @@ function calculateNewStateAfterOrder(userAccount, userPosition, market, order) {
17
17
  }
18
18
  const userAccountAfter = Object.assign({}, userAccount);
19
19
  const userPositionAfter = Object.assign({}, userPosition);
20
- const currentPositionDirection = position_1.positionCurrentDirection(userPosition);
20
+ const currentPositionDirection = (0, position_1.positionCurrentDirection)(userPosition);
21
21
  const increasePosition = userPosition.baseAssetAmount.eq(numericConstants_1.ZERO) ||
22
22
  isSameDirection(order.direction, currentPositionDirection);
23
23
  if (increasePosition) {
24
- const marketAfter = market_1.calculateNewMarketAfterTrade(baseAssetAmountToTrade, order.direction, market);
24
+ const marketAfter = (0, market_1.calculateNewMarketAfterTrade)(baseAssetAmountToTrade, order.direction, market);
25
25
  const { quoteAssetAmountSwapped, baseAssetAmountSwapped } = calculateAmountSwapped(market, marketAfter);
26
26
  userPositionAfter.baseAssetAmount = userPositionAfter.baseAssetAmount.add(baseAssetAmountSwapped);
27
27
  userPositionAfter.quoteAssetAmount = userPositionAfter.quoteAssetAmount.add(quoteAssetAmountSwapped);
@@ -30,10 +30,10 @@ function calculateNewStateAfterOrder(userAccount, userPosition, market, order) {
30
30
  else {
31
31
  const reversePosition = baseAssetAmountToTrade.gt(userPosition.baseAssetAmount.abs());
32
32
  if (reversePosition) {
33
- const intermediateMarket = market_1.calculateNewMarketAfterTrade(userPosition.baseAssetAmount, position_1.findDirectionToClose(userPosition), market);
33
+ const intermediateMarket = (0, market_1.calculateNewMarketAfterTrade)(userPosition.baseAssetAmount, (0, position_1.findDirectionToClose)(userPosition), market);
34
34
  const { quoteAssetAmountSwapped: baseAssetValue } = calculateAmountSwapped(market, intermediateMarket);
35
35
  let pnl;
36
- if (types_1.isVariant(currentPositionDirection, 'long')) {
36
+ if ((0, types_1.isVariant)(currentPositionDirection, 'long')) {
37
37
  pnl = baseAssetValue.sub(userPosition.quoteAssetAmount);
38
38
  }
39
39
  else {
@@ -41,20 +41,20 @@ function calculateNewStateAfterOrder(userAccount, userPosition, market, order) {
41
41
  }
42
42
  userAccountAfter.collateral = userAccountAfter.collateral.add(pnl);
43
43
  const baseAssetAmountLeft = baseAssetAmountToTrade.sub(userPosition.baseAssetAmount.abs());
44
- const marketAfter = market_1.calculateNewMarketAfterTrade(baseAssetAmountLeft, order.direction, intermediateMarket);
44
+ const marketAfter = (0, market_1.calculateNewMarketAfterTrade)(baseAssetAmountLeft, order.direction, intermediateMarket);
45
45
  const { quoteAssetAmountSwapped, baseAssetAmountSwapped } = calculateAmountSwapped(intermediateMarket, marketAfter);
46
46
  userPositionAfter.quoteAssetAmount = quoteAssetAmountSwapped;
47
47
  userPositionAfter.baseAssetAmount = baseAssetAmountSwapped;
48
48
  return [userAccountAfter, userPositionAfter, marketAfter];
49
49
  }
50
50
  else {
51
- const marketAfter = market_1.calculateNewMarketAfterTrade(baseAssetAmountToTrade, order.direction, market);
51
+ const marketAfter = (0, market_1.calculateNewMarketAfterTrade)(baseAssetAmountToTrade, order.direction, market);
52
52
  const { quoteAssetAmountSwapped: baseAssetValue, baseAssetAmountSwapped, } = calculateAmountSwapped(market, marketAfter);
53
53
  const costBasisRealized = userPosition.quoteAssetAmount
54
54
  .mul(baseAssetAmountSwapped.abs())
55
55
  .div(userPosition.baseAssetAmount.abs());
56
56
  let pnl;
57
- if (types_1.isVariant(currentPositionDirection, 'long')) {
57
+ if ((0, types_1.isVariant)(currentPositionDirection, 'long')) {
58
58
  pnl = baseAssetValue.sub(costBasisRealized);
59
59
  }
60
60
  else {
@@ -81,13 +81,13 @@ function calculateAmountSwapped(marketBefore, marketAfter) {
81
81
  };
82
82
  }
83
83
  function calculateBaseAssetAmountMarketCanExecute(market, order) {
84
- if (types_1.isVariant(order.orderType, 'limit')) {
84
+ if ((0, types_1.isVariant)(order.orderType, 'limit')) {
85
85
  return calculateAmountToTradeForLimit(market, order);
86
86
  }
87
- else if (types_1.isVariant(order.orderType, 'triggerLimit')) {
87
+ else if ((0, types_1.isVariant)(order.orderType, 'triggerLimit')) {
88
88
  return calculateAmountToTradeForTriggerLimit(market, order);
89
89
  }
90
- else if (types_1.isVariant(order.orderType, 'market')) {
90
+ else if ((0, types_1.isVariant)(order.orderType, 'market')) {
91
91
  // should never be a market order queued
92
92
  return numericConstants_1.ZERO;
93
93
  }
@@ -97,7 +97,7 @@ function calculateBaseAssetAmountMarketCanExecute(market, order) {
97
97
  }
98
98
  exports.calculateBaseAssetAmountMarketCanExecute = calculateBaseAssetAmountMarketCanExecute;
99
99
  function calculateAmountToTradeForLimit(market, order) {
100
- const [maxAmountToTrade, direction] = amm_1.calculateMaxBaseAssetAmountToTrade(market.amm, order.price);
100
+ const [maxAmountToTrade, direction] = (0, amm_1.calculateMaxBaseAssetAmountToTrade)(market.amm, order.price);
101
101
  // Check that directions are the same
102
102
  const sameDirection = isSameDirection(direction, order.direction);
103
103
  if (!sameDirection) {
@@ -119,8 +119,8 @@ function calculateAmountToTradeForTriggerLimit(market, order) {
119
119
  }
120
120
  exports.calculateAmountToTradeForTriggerLimit = calculateAmountToTradeForTriggerLimit;
121
121
  function isSameDirection(firstDirection, secondDirection) {
122
- return ((types_1.isVariant(firstDirection, 'long') && types_1.isVariant(secondDirection, 'long')) ||
123
- (types_1.isVariant(firstDirection, 'short') && types_1.isVariant(secondDirection, 'short')));
122
+ return (((0, types_1.isVariant)(firstDirection, 'long') && (0, types_1.isVariant)(secondDirection, 'long')) ||
123
+ ((0, types_1.isVariant)(firstDirection, 'short') && (0, types_1.isVariant)(secondDirection, 'short')));
124
124
  }
125
125
  function calculateAmountToTradeForTriggerMarket(market, order) {
126
126
  return isTriggerConditionSatisfied(market, order)
@@ -128,8 +128,8 @@ function calculateAmountToTradeForTriggerMarket(market, order) {
128
128
  : numericConstants_1.ZERO;
129
129
  }
130
130
  function isTriggerConditionSatisfied(market, order) {
131
- const markPrice = market_1.calculateMarkPrice(market);
132
- if (types_1.isVariant(order.triggerCondition, 'above')) {
131
+ const markPrice = (0, market_1.calculateMarkPrice)(market);
132
+ if ((0, types_1.isVariant)(order.triggerCondition, 'above')) {
133
133
  return markPrice.gt(order.triggerPrice);
134
134
  }
135
135
  else {
@@ -140,13 +140,13 @@ function calculateBaseAssetAmountUserCanExecute(market, order, user) {
140
140
  const maxLeverage = user.getMaxLeverage(order.marketIndex, 'Initial');
141
141
  const freeCollateral = user.getFreeCollateral();
142
142
  let quoteAssetAmount;
143
- if (_1.isOrderRiskIncreasingInSameDirection(user, order)) {
143
+ if ((0, _1.isOrderRiskIncreasingInSameDirection)(user, order)) {
144
144
  quoteAssetAmount = freeCollateral.mul(maxLeverage).div(_1.TEN_THOUSAND);
145
145
  }
146
146
  else {
147
147
  const position = user.getUserPosition(order.marketIndex) ||
148
148
  user.getEmptyPosition(order.marketIndex);
149
- const positionValue = _1.calculateBaseAssetValue(market, position);
149
+ const positionValue = (0, _1.calculateBaseAssetValue)(market, position);
150
150
  quoteAssetAmount = freeCollateral
151
151
  .mul(maxLeverage)
152
152
  .div(_1.TEN_THOUSAND)
@@ -156,7 +156,7 @@ function calculateBaseAssetAmountUserCanExecute(market, order, user) {
156
156
  return numericConstants_1.ZERO;
157
157
  }
158
158
  const baseAssetReservesBefore = market.amm.baseAssetReserve;
159
- const [_, baseAssetReservesAfter] = _1.calculateAmmReservesAfterSwap(market.amm, 'quote', quoteAssetAmount, types_1.isVariant(order.direction, 'long')
159
+ const [_, baseAssetReservesAfter] = (0, _1.calculateAmmReservesAfterSwap)(market.amm, 'quote', quoteAssetAmount, (0, types_1.isVariant)(order.direction, 'long')
160
160
  ? types_1.SwapDirection.ADD
161
161
  : types_1.SwapDirection.REMOVE);
162
162
  return baseAssetReservesBefore.sub(baseAssetReservesAfter).abs();
@@ -93,7 +93,7 @@ class RetryTxSender {
93
93
  catch (err) {
94
94
  throw new Error('signature must be base58 encoded: ' + signature);
95
95
  }
96
- assert_1.default(decodedSignature.length === 64, 'signature has invalid length');
96
+ (0, assert_1.default)(decodedSignature.length === 64, 'signature has invalid length');
97
97
  const start = Date.now();
98
98
  const subscriptionCommitment = commitment || this.provider.opts.commitment;
99
99
  let subscriptionId;
package/lib/types.d.ts CHANGED
@@ -225,6 +225,7 @@ export declare type OrderRecord = {
225
225
  fee: BN;
226
226
  fillerReward: BN;
227
227
  tradeRecordId: BN;
228
+ quoteAssetAmountSurplus: BN;
228
229
  };
229
230
  export declare type StateAccount = {
230
231
  admin: PublicKey;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "0.1.24",
3
+ "version": "0.1.25-master.0",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -32,6 +32,7 @@
32
32
  "@pythnetwork/client": "^2.5.1",
33
33
  "@solana/spl-token": "^0.1.6",
34
34
  "@solana/web3.js": "^1.22.0",
35
+ "@switchboard-xyz/switchboard-v2": "^0.0.67",
35
36
  "strict-event-emitter-types": "^2.0.0",
36
37
  "uuid": "^8.3.2"
37
38
  },
@@ -9,6 +9,8 @@ type AccountToLoad = {
9
9
 
10
10
  const GET_MULTIPLE_ACCOUNTS_CHUNK_SIZE = 99;
11
11
 
12
+ const fiveMinutes = 5 * 60 * 1000;
13
+
12
14
  export class BulkAccountLoader {
13
15
  connection: Connection;
14
16
  commitment: Commitment;
@@ -21,6 +23,7 @@ export class BulkAccountLoader {
21
23
  loadPromise?: Promise<void>;
22
24
  loadPromiseResolver: () => void;
23
25
  loggingEnabled = false;
26
+ lastUpdate = Date.now();
24
27
 
25
28
  public constructor(
26
29
  connection: Connection,
@@ -125,6 +128,20 @@ export class BulkAccountLoader {
125
128
  } finally {
126
129
  this.loadPromiseResolver();
127
130
  this.loadPromise = undefined;
131
+
132
+ const now = Date.now();
133
+ if (now - this.lastUpdate > fiveMinutes) {
134
+ if (this.loggingEnabled) {
135
+ console.log(
136
+ "Haven't seen updated account in five minutes. Bulk account loader creating new Connection Object"
137
+ );
138
+ }
139
+ this.connection = new Connection(
140
+ // @ts-ignore
141
+ this.connection._rpcEndpoint,
142
+ this.connection.commitment
143
+ );
144
+ }
128
145
  }
129
146
  }
130
147
 
@@ -166,6 +183,7 @@ export class BulkAccountLoader {
166
183
  buffer: newBuffer,
167
184
  });
168
185
  this.handleAccountCallbacks(accountToLoad, newBuffer);
186
+ this.lastUpdate = Date.now();
169
187
  continue;
170
188
  }
171
189
 
@@ -180,6 +198,7 @@ export class BulkAccountLoader {
180
198
  buffer: newBuffer,
181
199
  });
182
200
  this.handleAccountCallbacks(accountToLoad, newBuffer);
201
+ this.lastUpdate = Date.now();
183
202
  }
184
203
  }
185
204
  }
package/src/admin.ts CHANGED
@@ -217,6 +217,7 @@ export class Admin extends ClearingHouse {
217
217
  quoteAssetReserve: BN,
218
218
  periodicity: BN,
219
219
  pegMultiplier: BN = PEG_PRECISION,
220
+ oracleSource: OracleSource = OracleSource.PYTH,
220
221
  marginRatioInitial = 2000,
221
222
  marginRatioPartial = 625,
222
223
  marginRatioMaintenance = 500
@@ -231,6 +232,7 @@ export class Admin extends ClearingHouse {
231
232
  quoteAssetReserve,
232
233
  periodicity,
233
234
  pegMultiplier,
235
+ oracleSource,
234
236
  marginRatioInitial,
235
237
  marginRatioPartial,
236
238
  marginRatioMaintenance,
@@ -258,6 +258,8 @@ export class ClearingHouse {
258
258
  this.program = newProgram;
259
259
  this.userAccountPublicKey = undefined;
260
260
  this.userAccount = undefined;
261
+ this.userOrdersAccountPublicKey = undefined;
262
+ this.userOrdersExist = undefined;
261
263
  }
262
264
 
263
265
  public async initializeUserAccount(): Promise<
@@ -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,6 +3,7 @@ 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
 
@@ -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
+ }
@@ -212,6 +212,12 @@
212
212
  "name": "ammPegMultiplier",
213
213
  "type": "u128"
214
214
  },
215
+ {
216
+ "name": "oracleSource",
217
+ "type": {
218
+ "defined": "OracleSource"
219
+ }
220
+ },
215
221
  {
216
222
  "name": "marginRatioInitial",
217
223
  "type": "u32"
@@ -3255,12 +3261,16 @@
3255
3261
  "name": "fillerReward",
3256
3262
  "type": "u128"
3257
3263
  },
3264
+ {
3265
+ "name": "quoteAssetAmountSurplus",
3266
+ "type": "u128"
3267
+ },
3258
3268
  {
3259
3269
  "name": "padding",
3260
3270
  "type": {
3261
3271
  "array": [
3262
3272
  "u64",
3263
- 10
3273
+ 8
3264
3274
  ]
3265
3275
  }
3266
3276
  }