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

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 (143) hide show
  1. package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +1 -0
  2. package/lib/accounts/pollingClearingHouseAccountSubscriber.js +3 -0
  3. package/lib/accounts/types.d.ts +2 -0
  4. package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +1 -0
  5. package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +3 -0
  6. package/lib/admin.d.ts +5 -2
  7. package/lib/admin.js +30 -4
  8. package/lib/clearingHouse.d.ts +3 -0
  9. package/lib/clearingHouse.js +19 -1
  10. package/lib/clearingHouseUser.d.ts +11 -9
  11. package/lib/clearingHouseUser.js +184 -108
  12. package/lib/config.js +1 -1
  13. package/lib/dlob/DLOB.d.ts +73 -0
  14. package/lib/dlob/DLOB.js +557 -0
  15. package/lib/dlob/DLOBNode.d.ts +52 -0
  16. package/lib/dlob/DLOBNode.js +82 -0
  17. package/lib/dlob/NodeList.d.ts +26 -0
  18. package/lib/dlob/NodeList.js +138 -0
  19. package/lib/idl/clearing_house.json +123 -203
  20. package/lib/index.d.ts +5 -0
  21. package/lib/index.js +5 -0
  22. package/lib/math/market.d.ts +3 -2
  23. package/lib/math/market.js +17 -12
  24. package/lib/math/orders.d.ts +3 -3
  25. package/lib/math/orders.js +31 -16
  26. package/lib/math/position.d.ts +3 -3
  27. package/lib/math/position.js +23 -16
  28. package/lib/math/repeg.js +8 -0
  29. package/lib/math/spotBalance.d.ts +3 -0
  30. package/lib/math/spotBalance.js +18 -1
  31. package/lib/math/spotPosition.d.ts +5 -1
  32. package/lib/math/spotPosition.js +16 -1
  33. package/lib/types.d.ts +25 -24
  34. package/lib/types.js +9 -3
  35. package/lib/userMap/userMap.d.ts +25 -0
  36. package/lib/userMap/userMap.js +73 -0
  37. package/lib/userMap/userStatsMap.d.ts +19 -0
  38. package/lib/userMap/userStatsMap.js +68 -0
  39. package/package.json +6 -3
  40. package/src/accounts/pollingClearingHouseAccountSubscriber.ts +4 -0
  41. package/src/accounts/types.ts +1 -0
  42. package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +6 -0
  43. package/src/admin.ts +50 -8
  44. package/src/clearingHouse.ts +30 -1
  45. package/src/clearingHouseUser.ts +366 -167
  46. package/src/config.ts +1 -1
  47. package/src/dlob/DLOB.ts +884 -0
  48. package/src/dlob/DLOBNode.ts +163 -0
  49. package/src/dlob/NodeList.ts +185 -0
  50. package/src/idl/clearing_house.json +123 -203
  51. package/src/index.ts +5 -0
  52. package/src/math/market.ts +22 -13
  53. package/src/math/orders.ts +29 -21
  54. package/src/math/position.ts +36 -22
  55. package/src/math/repeg.ts +9 -0
  56. package/src/math/spotBalance.ts +26 -0
  57. package/src/math/spotPosition.ts +42 -1
  58. package/src/types.ts +26 -24
  59. package/src/userMap/userMap.ts +100 -0
  60. package/src/userMap/userStatsMap.ts +110 -0
  61. package/tests/bn/test.ts +2 -3
  62. package/tests/dlob/helpers.ts +322 -0
  63. package/tests/dlob/test.ts +2865 -0
  64. package/my-script/.env +0 -7
  65. package/my-script/getUserStats.ts +0 -106
  66. package/my-script/multiConnections.ts +0 -119
  67. package/my-script/test-regex.ts +0 -11
  68. package/my-script/utils.ts +0 -52
  69. package/src/accounts/bulkAccountLoader.js +0 -249
  70. package/src/accounts/bulkUserStatsSubscription.js +0 -75
  71. package/src/accounts/bulkUserSubscription.js +0 -75
  72. package/src/accounts/fetch.js +0 -92
  73. package/src/accounts/pollingClearingHouseAccountSubscriber.js +0 -465
  74. package/src/accounts/pollingOracleSubscriber.js +0 -156
  75. package/src/accounts/pollingTokenAccountSubscriber.js +0 -141
  76. package/src/accounts/pollingUserAccountSubscriber.js +0 -208
  77. package/src/accounts/pollingUserStatsAccountSubscriber.js +0 -208
  78. package/src/accounts/types.js +0 -28
  79. package/src/accounts/utils.js +0 -7
  80. package/src/accounts/webSocketAccountSubscriber.js +0 -138
  81. package/src/accounts/webSocketClearingHouseAccountSubscriber.js +0 -433
  82. package/src/accounts/webSocketUserAccountSubscriber.js +0 -113
  83. package/src/accounts/webSocketUserStatsAccountSubsriber.js +0 -113
  84. package/src/addresses/pda.js +0 -186
  85. package/src/admin.js +0 -1284
  86. package/src/assert/assert.js +0 -9
  87. package/src/clearingHouse.js +0 -3433
  88. package/src/clearingHouseConfig.js +0 -2
  89. package/src/clearingHouseUser.js +0 -874
  90. package/src/clearingHouseUserConfig.js +0 -2
  91. package/src/clearingHouseUserStats.js +0 -115
  92. package/src/clearingHouseUserStatsConfig.js +0 -2
  93. package/src/config.js +0 -80
  94. package/src/constants/numericConstants.js +0 -48
  95. package/src/constants/perpMarkets.js +0 -42
  96. package/src/constants/spotMarkets.js +0 -51
  97. package/src/events/eventList.js +0 -120
  98. package/src/events/eventSubscriber.js +0 -202
  99. package/src/events/fetchLogs.js +0 -117
  100. package/src/events/pollingLogProvider.js +0 -113
  101. package/src/events/sort.js +0 -41
  102. package/src/events/txEventCache.js +0 -74
  103. package/src/events/types.js +0 -25
  104. package/src/events/webSocketLogProvider.js +0 -76
  105. package/src/factory/bigNum.js +0 -393
  106. package/src/factory/oracleClient.js +0 -20
  107. package/src/index.js +0 -75
  108. package/src/math/amm.js +0 -422
  109. package/src/math/auction.js +0 -42
  110. package/src/math/conversion.js +0 -12
  111. package/src/math/funding.js +0 -296
  112. package/src/math/insurance.js +0 -27
  113. package/src/math/margin.js +0 -77
  114. package/src/math/market.js +0 -105
  115. package/src/math/oracles.js +0 -56
  116. package/src/math/orders.js +0 -153
  117. package/src/math/position.js +0 -172
  118. package/src/math/repeg.js +0 -128
  119. package/src/math/spotBalance.js +0 -176
  120. package/src/math/spotMarket.js +0 -8
  121. package/src/math/spotPosition.js +0 -8
  122. package/src/math/trade.js +0 -260
  123. package/src/math/utils.js +0 -27
  124. package/src/oracles/oracleClientCache.js +0 -20
  125. package/src/oracles/pythClient.js +0 -81
  126. package/src/oracles/quoteAssetOracleClient.js +0 -63
  127. package/src/oracles/switchboardClient.js +0 -101
  128. package/src/oracles/types.js +0 -2
  129. package/src/orderParams.js +0 -28
  130. package/src/serum/serumSubscriber.js +0 -102
  131. package/src/serum/types.js +0 -2
  132. package/src/slot/SlotSubscriber.js +0 -86
  133. package/src/token/index.js +0 -38
  134. package/src/tokenFaucet.js +0 -323
  135. package/src/tx/retryTxSender.js +0 -280
  136. package/src/tx/types.js +0 -2
  137. package/src/tx/utils.js +0 -18
  138. package/src/types.js +0 -216
  139. package/src/userName.js +0 -20
  140. package/src/util/computeUnits.js +0 -62
  141. package/src/util/promiseTimeout.js +0 -14
  142. package/src/util/tps.js +0 -61
  143. package/src/wallet.js +0 -72
@@ -1,2 +0,0 @@
1
- "use strict";
2
- exports.__esModule = true;
@@ -1,115 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __generator = (this && this.__generator) || function (thisArg, body) {
12
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
- function verb(n) { return function (v) { return step([n, v]); }; }
15
- function step(op) {
16
- if (f) throw new TypeError("Generator is already executing.");
17
- while (_) try {
18
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
- if (y = 0, t) op = [op[0] & 2, t.value];
20
- switch (op[0]) {
21
- case 0: case 1: t = op; break;
22
- case 4: _.label++; return { value: op[1], done: false };
23
- case 5: _.label++; y = op[1]; op = [0]; continue;
24
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
- default:
26
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
- if (t[2]) _.ops.pop();
31
- _.trys.pop(); continue;
32
- }
33
- op = body.call(thisArg, _);
34
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
- }
37
- };
38
- exports.__esModule = true;
39
- exports.ClearingHouseUserStats = void 0;
40
- var web3_js_1 = require("@solana/web3.js");
41
- var pollingUserStatsAccountSubscriber_1 = require("./accounts/pollingUserStatsAccountSubscriber");
42
- var webSocketUserStatsAccountSubsriber_1 = require("./accounts/webSocketUserStatsAccountSubsriber");
43
- var pda_1 = require("./addresses/pda");
44
- var ClearingHouseUserStats = /** @class */ (function () {
45
- function ClearingHouseUserStats(config) {
46
- var _a;
47
- this.clearingHouse = config.clearingHouse;
48
- this.userStatsAccountPublicKey = config.userStatsAccountPublicKey;
49
- if (((_a = config.accountSubscription) === null || _a === void 0 ? void 0 : _a.type) === 'polling') {
50
- this.accountSubscriber = new pollingUserStatsAccountSubscriber_1.PollingUserStatsAccountSubscriber(config.clearingHouse.program, config.userStatsAccountPublicKey, config.accountSubscription.accountLoader);
51
- }
52
- else {
53
- this.accountSubscriber = new webSocketUserStatsAccountSubsriber_1.WebSocketUserStatsAccountSubscriber(config.clearingHouse.program, config.userStatsAccountPublicKey);
54
- }
55
- }
56
- ClearingHouseUserStats.prototype.subscribe = function () {
57
- return __awaiter(this, void 0, void 0, function () {
58
- var _a;
59
- return __generator(this, function (_b) {
60
- switch (_b.label) {
61
- case 0:
62
- _a = this;
63
- return [4 /*yield*/, this.accountSubscriber.subscribe()];
64
- case 1:
65
- _a.isSubscribed = _b.sent();
66
- return [2 /*return*/, this.isSubscribed];
67
- }
68
- });
69
- });
70
- };
71
- ClearingHouseUserStats.prototype.fetchAccounts = function () {
72
- return __awaiter(this, void 0, void 0, function () {
73
- return __generator(this, function (_a) {
74
- switch (_a.label) {
75
- case 0: return [4 /*yield*/, this.accountSubscriber.fetch()];
76
- case 1:
77
- _a.sent();
78
- return [2 /*return*/];
79
- }
80
- });
81
- });
82
- };
83
- ClearingHouseUserStats.prototype.unsubscribe = function () {
84
- return __awaiter(this, void 0, void 0, function () {
85
- return __generator(this, function (_a) {
86
- switch (_a.label) {
87
- case 0: return [4 /*yield*/, this.accountSubscriber.unsubscribe()];
88
- case 1:
89
- _a.sent();
90
- this.isSubscribed = false;
91
- return [2 /*return*/];
92
- }
93
- });
94
- });
95
- };
96
- ClearingHouseUserStats.prototype.getAccountAndSlot = function () {
97
- return this.accountSubscriber.getUserStatsAccountAndSlot();
98
- };
99
- ClearingHouseUserStats.prototype.getAccount = function () {
100
- return this.accountSubscriber.getUserStatsAccountAndSlot().data;
101
- };
102
- ClearingHouseUserStats.prototype.getReferrerInfo = function () {
103
- if (this.getAccount().referrer.equals(web3_js_1.PublicKey["default"])) {
104
- return undefined;
105
- }
106
- else {
107
- return {
108
- referrer: (0, pda_1.getUserAccountPublicKeySync)(this.clearingHouse.program.programId, this.getAccount().referrer, 0),
109
- referrerStats: (0, pda_1.getUserStatsAccountPublicKey)(this.clearingHouse.program.programId, this.getAccount().referrer)
110
- };
111
- }
112
- };
113
- return ClearingHouseUserStats;
114
- }());
115
- exports.ClearingHouseUserStats = ClearingHouseUserStats;
@@ -1,2 +0,0 @@
1
- "use strict";
2
- exports.__esModule = true;
package/src/config.js DELETED
@@ -1,80 +0,0 @@
1
- "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
- exports.__esModule = true;
14
- exports.getMarketsAndOraclesForSubscription = exports.initialize = exports.getConfig = exports.configs = void 0;
15
- var perpMarkets_1 = require("./constants/perpMarkets");
16
- var spotMarkets_1 = require("./constants/spotMarkets");
17
- exports.configs = {
18
- devnet: {
19
- ENV: 'devnet',
20
- PYTH_ORACLE_MAPPING_ADDRESS: 'BmA9Z6FjioHJPpjT39QazZyhDRUdZy2ezwx4GiDdE2u2',
21
- CLEARING_HOUSE_PROGRAM_ID: '3v1iEjbSSLSSYyt1pmx4UB5rqJGurmz71RibXF7X6UF3',
22
- USDC_MINT_ADDRESS: '8zGuJQqwhZafTah7Uc7Z4tXRnguqkn5KLFAP8oV6PHe2',
23
- PERP_MARKETS: perpMarkets_1.DevnetPerpMarkets,
24
- SPOT_MARKETS: spotMarkets_1.DevnetSpotMarkets
25
- },
26
- 'mainnet-beta': {
27
- ENV: 'mainnet-beta',
28
- PYTH_ORACLE_MAPPING_ADDRESS: 'AHtgzX45WTKfkPG53L6WYhGEXwQkN1BVknET3sVsLL8J',
29
- CLEARING_HOUSE_PROGRAM_ID: 'dammHkt7jmytvbS3nHTxQNEcP59aE57nxwV21YdqEDN',
30
- USDC_MINT_ADDRESS: 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v',
31
- PERP_MARKETS: perpMarkets_1.MainnetMarkets,
32
- SPOT_MARKETS: spotMarkets_1.MainnetSpotMarkets
33
- }
34
- };
35
- var currentConfig = exports.configs.devnet;
36
- var getConfig = function () { return currentConfig; };
37
- exports.getConfig = getConfig;
38
- /**
39
- * Allows customization of the SDK's environment and endpoints. You can pass individual settings to override the settings with your own presets.
40
- *
41
- * Defaults to master environment if you don't use this function.
42
- * @param props
43
- * @returns
44
- */
45
- var initialize = function (props) {
46
- var _a, _b;
47
- //@ts-ignore
48
- if (props.env === 'master')
49
- return __assign(__assign({}, exports.configs['devnet']), ((_a = props.overrideEnv) !== null && _a !== void 0 ? _a : {}));
50
- currentConfig = __assign(__assign({}, exports.configs[props.env]), ((_b = props.overrideEnv) !== null && _b !== void 0 ? _b : {}));
51
- return currentConfig;
52
- };
53
- exports.initialize = initialize;
54
- function getMarketsAndOraclesForSubscription(env) {
55
- var perpMarketIndexes = [];
56
- var spotMarketIndexes = [];
57
- var oracleInfos = new Map();
58
- for (var _i = 0, _a = perpMarkets_1.PerpMarkets[env]; _i < _a.length; _i++) {
59
- var market = _a[_i];
60
- perpMarketIndexes.push(market.marketIndex);
61
- oracleInfos.set(market.oracle.toString(), {
62
- publicKey: market.oracle,
63
- source: market.oracleSource
64
- });
65
- }
66
- for (var _b = 0, _c = spotMarkets_1.SpotMarkets[env]; _b < _c.length; _b++) {
67
- var spotMarket = _c[_b];
68
- spotMarketIndexes.push(spotMarket.marketIndex);
69
- oracleInfos.set(spotMarket.oracle.toString(), {
70
- publicKey: spotMarket.oracle,
71
- source: spotMarket.oracleSource
72
- });
73
- }
74
- return {
75
- perpMarketIndexes: perpMarketIndexes,
76
- spotMarketIndexes: spotMarketIndexes,
77
- oracleInfos: Array.from(oracleInfos.values())
78
- };
79
- }
80
- exports.getMarketsAndOraclesForSubscription = getMarketsAndOraclesForSubscription;
@@ -1,48 +0,0 @@
1
- "use strict";
2
- exports.__esModule = true;
3
- exports.LAMPORTS_EXP = exports.LAMPORTS_PRECISION = exports.QUOTE_SPOT_MARKET_INDEX = exports.ONE_YEAR = exports.BID_ASK_SPREAD_PRECISION = exports.MARGIN_PRECISION = exports.AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO = exports.PRICE_TO_QUOTE_PRECISION = exports.PRICE_DIV_PEG = exports.AMM_TO_QUOTE_PRECISION_RATIO = exports.BASE_PRECISION_EXP = exports.BASE_PRECISION = exports.AMM_RESERVE_PRECISION = exports.PEG_PRECISION = exports.FUNDING_PAYMENT_PRECISION = exports.MARK_PRICE_PRECISION = exports.QUOTE_PRECISION = exports.LIQUIDATION_FEE_PRECISION = exports.SPOT_MARKET_IMF_PRECISION = exports.SPOT_MARKET_IMF_PRECISION_EXP = exports.SPOT_MARKET_BALANCE_PRECISION = exports.SPOT_MARKET_BALANCE_PRECISION_EXP = exports.SPOT_MARKET_WEIGHT_PRECISION = exports.SPOT_MARKET_RATE_PRECISION = exports.SPOT_MARKET_UTILIZATION_PRECISION = exports.SPOT_MARKET_CUMULATIVE_INTEREST_PRECISION = exports.SPOT_MARKET_CUMULATIVE_INTEREST_PRECISION_EXP = exports.SPOT_MARKET_INTEREST_PRECISION = exports.SPOT_MARKET_INTEREST_PRECISION_EXP = exports.AMM_RESERVE_PRECISION_EXP = exports.PEG_PRECISION_EXP = exports.FUNDING_RATE_PRECISION_EXP = exports.MARK_PRICE_PRECISION_EXP = exports.FUNDING_PAYMENT_PRECISION_EXP = exports.QUOTE_PRECISION_EXP = exports.MAX_LEVERAGE = exports.TEN_MILLION = exports.BN_MAX = exports.TEN_THOUSAND = exports.TEN = exports.TWO = exports.ONE = exports.ZERO = void 0;
4
- var web3_js_1 = require("@solana/web3.js");
5
- var __1 = require("../");
6
- exports.ZERO = new __1.BN(0);
7
- exports.ONE = new __1.BN(1);
8
- exports.TWO = new __1.BN(2);
9
- exports.TEN = new __1.BN(10);
10
- exports.TEN_THOUSAND = new __1.BN(10000);
11
- exports.BN_MAX = new __1.BN(Number.MAX_SAFE_INTEGER);
12
- exports.TEN_MILLION = exports.TEN_THOUSAND.mul(exports.TEN_THOUSAND);
13
- exports.MAX_LEVERAGE = new __1.BN(5);
14
- exports.QUOTE_PRECISION_EXP = new __1.BN(6);
15
- exports.FUNDING_PAYMENT_PRECISION_EXP = new __1.BN(4);
16
- exports.MARK_PRICE_PRECISION_EXP = new __1.BN(10);
17
- exports.FUNDING_RATE_PRECISION_EXP = exports.MARK_PRICE_PRECISION_EXP.mul(exports.FUNDING_PAYMENT_PRECISION_EXP);
18
- exports.PEG_PRECISION_EXP = new __1.BN(3);
19
- exports.AMM_RESERVE_PRECISION_EXP = new __1.BN(13);
20
- exports.SPOT_MARKET_INTEREST_PRECISION_EXP = new __1.BN(6);
21
- exports.SPOT_MARKET_INTEREST_PRECISION = new __1.BN(10).pow(exports.SPOT_MARKET_INTEREST_PRECISION_EXP);
22
- exports.SPOT_MARKET_CUMULATIVE_INTEREST_PRECISION_EXP = new __1.BN(10);
23
- exports.SPOT_MARKET_CUMULATIVE_INTEREST_PRECISION = new __1.BN(10).pow(exports.SPOT_MARKET_CUMULATIVE_INTEREST_PRECISION_EXP);
24
- exports.SPOT_MARKET_UTILIZATION_PRECISION = new __1.BN(1000000);
25
- exports.SPOT_MARKET_RATE_PRECISION = new __1.BN(1000000);
26
- exports.SPOT_MARKET_WEIGHT_PRECISION = new __1.BN(10000);
27
- exports.SPOT_MARKET_BALANCE_PRECISION_EXP = new __1.BN(6);
28
- exports.SPOT_MARKET_BALANCE_PRECISION = new __1.BN(10).pow(exports.SPOT_MARKET_BALANCE_PRECISION_EXP);
29
- exports.SPOT_MARKET_IMF_PRECISION_EXP = new __1.BN(6);
30
- exports.SPOT_MARKET_IMF_PRECISION = new __1.BN(10).pow(exports.SPOT_MARKET_IMF_PRECISION_EXP);
31
- exports.LIQUIDATION_FEE_PRECISION = new __1.BN(1000000);
32
- exports.QUOTE_PRECISION = new __1.BN(10).pow(exports.QUOTE_PRECISION_EXP);
33
- exports.MARK_PRICE_PRECISION = new __1.BN(10).pow(exports.MARK_PRICE_PRECISION_EXP);
34
- exports.FUNDING_PAYMENT_PRECISION = new __1.BN(10).pow(exports.FUNDING_PAYMENT_PRECISION_EXP);
35
- exports.PEG_PRECISION = new __1.BN(10).pow(exports.PEG_PRECISION_EXP);
36
- exports.AMM_RESERVE_PRECISION = new __1.BN(10).pow(exports.AMM_RESERVE_PRECISION_EXP);
37
- exports.BASE_PRECISION = exports.AMM_RESERVE_PRECISION;
38
- exports.BASE_PRECISION_EXP = exports.AMM_RESERVE_PRECISION_EXP;
39
- exports.AMM_TO_QUOTE_PRECISION_RATIO = exports.AMM_RESERVE_PRECISION.div(exports.QUOTE_PRECISION); // 10^7
40
- exports.PRICE_DIV_PEG = exports.MARK_PRICE_PRECISION.div(exports.PEG_PRECISION); //10^7
41
- exports.PRICE_TO_QUOTE_PRECISION = exports.MARK_PRICE_PRECISION.div(exports.QUOTE_PRECISION);
42
- exports.AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO = exports.AMM_RESERVE_PRECISION.mul(exports.PEG_PRECISION).div(exports.QUOTE_PRECISION); // 10^10
43
- exports.MARGIN_PRECISION = exports.TEN_THOUSAND;
44
- exports.BID_ASK_SPREAD_PRECISION = new __1.BN(1000000);
45
- exports.ONE_YEAR = new __1.BN(31536000);
46
- exports.QUOTE_SPOT_MARKET_INDEX = new __1.BN(0);
47
- exports.LAMPORTS_PRECISION = new __1.BN(web3_js_1.LAMPORTS_PER_SOL);
48
- exports.LAMPORTS_EXP = new __1.BN(Math.log10(web3_js_1.LAMPORTS_PER_SOL));
@@ -1,42 +0,0 @@
1
- "use strict";
2
- exports.__esModule = true;
3
- exports.PerpMarkets = exports.MainnetMarkets = exports.DevnetPerpMarkets = void 0;
4
- var __1 = require("../");
5
- var web3_js_1 = require("@solana/web3.js");
6
- exports.DevnetPerpMarkets = [
7
- {
8
- fullName: 'Solana',
9
- category: ['L1', 'Infra'],
10
- symbol: 'SOL-PERP',
11
- baseAssetSymbol: 'SOL',
12
- marketIndex: new __1.BN(0),
13
- oracle: new web3_js_1.PublicKey('J83w4HKfqxwcq3BEMMkPFSppX3gqekLyLJBexebFVkix'),
14
- launchTs: 1655751353000,
15
- oracleSource: __1.OracleSource.PYTH
16
- },
17
- {
18
- fullName: 'Bitcoin',
19
- category: ['L1', 'Payment'],
20
- symbol: 'BTC-PERP',
21
- baseAssetSymbol: 'BTC',
22
- marketIndex: new __1.BN(1),
23
- oracle: new web3_js_1.PublicKey('HovQMDrbAgAYPCmHVSrezcSmkMtXSSUsLDFANExrZh2J'),
24
- launchTs: 1655751353000,
25
- oracleSource: __1.OracleSource.PYTH
26
- },
27
- {
28
- fullName: 'Ethereum',
29
- category: ['L1', 'Infra'],
30
- symbol: 'ETH-PERP',
31
- baseAssetSymbol: 'ETH',
32
- marketIndex: new __1.BN(2),
33
- oracle: new web3_js_1.PublicKey('EdVCmQ9FSPcVe5YySXDPCRmc8aDQLKJ9xvYBMZPie1Vw'),
34
- launchTs: 1637691133472,
35
- oracleSource: __1.OracleSource.PYTH
36
- },
37
- ];
38
- exports.MainnetMarkets = [];
39
- exports.PerpMarkets = {
40
- devnet: exports.DevnetPerpMarkets,
41
- 'mainnet-beta': []
42
- };
@@ -1,51 +0,0 @@
1
- "use strict";
2
- exports.__esModule = true;
3
- exports.SpotMarkets = exports.MainnetSpotMarkets = exports.DevnetSpotMarkets = exports.WRAPPED_SOL_MINT = void 0;
4
- var web3_js_1 = require("@solana/web3.js");
5
- var __1 = require("../");
6
- var numericConstants_1 = require("./numericConstants");
7
- exports.WRAPPED_SOL_MINT = new web3_js_1.PublicKey('So11111111111111111111111111111111111111112');
8
- exports.DevnetSpotMarkets = [
9
- {
10
- symbol: 'USDC',
11
- marketIndex: new __1.BN(0),
12
- oracle: web3_js_1.PublicKey["default"],
13
- oracleSource: __1.OracleSource.QUOTE_ASSET,
14
- mint: new web3_js_1.PublicKey('8zGuJQqwhZafTah7Uc7Z4tXRnguqkn5KLFAP8oV6PHe2'),
15
- precision: numericConstants_1.SPOT_MARKET_BALANCE_PRECISION,
16
- precisionExp: numericConstants_1.SPOT_MARKET_BALANCE_PRECISION_EXP
17
- },
18
- {
19
- symbol: 'SOL',
20
- marketIndex: new __1.BN(1),
21
- oracle: new web3_js_1.PublicKey('J83w4HKfqxwcq3BEMMkPFSppX3gqekLyLJBexebFVkix'),
22
- oracleSource: __1.OracleSource.PYTH,
23
- mint: new web3_js_1.PublicKey(exports.WRAPPED_SOL_MINT),
24
- precision: numericConstants_1.LAMPORTS_PRECISION,
25
- precisionExp: numericConstants_1.LAMPORTS_EXP
26
- },
27
- {
28
- symbol: 'BTC',
29
- marketIndex: new __1.BN(2),
30
- oracle: new web3_js_1.PublicKey('HovQMDrbAgAYPCmHVSrezcSmkMtXSSUsLDFANExrZh2J'),
31
- oracleSource: __1.OracleSource.PYTH,
32
- mint: new web3_js_1.PublicKey('3BZPwbcqB5kKScF3TEXxwNfx5ipV13kbRVDvfVp5c6fv'),
33
- precision: numericConstants_1.SPOT_MARKET_BALANCE_PRECISION,
34
- precisionExp: numericConstants_1.SPOT_MARKET_BALANCE_PRECISION_EXP
35
- },
36
- ];
37
- exports.MainnetSpotMarkets = [
38
- {
39
- symbol: 'USDC',
40
- marketIndex: new __1.BN(0),
41
- oracle: web3_js_1.PublicKey["default"],
42
- oracleSource: __1.OracleSource.QUOTE_ASSET,
43
- mint: new web3_js_1.PublicKey('EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v'),
44
- precision: numericConstants_1.SPOT_MARKET_BALANCE_PRECISION,
45
- precisionExp: numericConstants_1.SPOT_MARKET_BALANCE_PRECISION_EXP
46
- },
47
- ];
48
- exports.SpotMarkets = {
49
- devnet: exports.DevnetSpotMarkets,
50
- 'mainnet-beta': exports.MainnetSpotMarkets
51
- };
@@ -1,120 +0,0 @@
1
- "use strict";
2
- var __generator = (this && this.__generator) || function (thisArg, body) {
3
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
4
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
5
- function verb(n) { return function (v) { return step([n, v]); }; }
6
- function step(op) {
7
- if (f) throw new TypeError("Generator is already executing.");
8
- while (_) try {
9
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
10
- if (y = 0, t) op = [op[0] & 2, t.value];
11
- switch (op[0]) {
12
- case 0: case 1: t = op; break;
13
- case 4: _.label++; return { value: op[1], done: false };
14
- case 5: _.label++; y = op[1]; op = [0]; continue;
15
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
16
- default:
17
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
18
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
19
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
20
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
21
- if (t[2]) _.ops.pop();
22
- _.trys.pop(); continue;
23
- }
24
- op = body.call(thisArg, _);
25
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
26
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
27
- }
28
- };
29
- exports.__esModule = true;
30
- exports.EventList = void 0;
31
- var Node = /** @class */ (function () {
32
- function Node(event, next, prev) {
33
- this.event = event;
34
- this.next = next;
35
- this.prev = prev;
36
- }
37
- return Node;
38
- }());
39
- var EventList = /** @class */ (function () {
40
- function EventList(eventType, maxSize, sortFn, orderDirection) {
41
- this.eventType = eventType;
42
- this.maxSize = maxSize;
43
- this.sortFn = sortFn;
44
- this.orderDirection = orderDirection;
45
- this.size = 0;
46
- }
47
- EventList.prototype.insert = function (event) {
48
- this.size++;
49
- var newNode = new Node(event);
50
- if (this.head === undefined) {
51
- this.head = this.tail = newNode;
52
- return;
53
- }
54
- if (this.sortFn(this.head.event, newNode.event) ===
55
- (this.orderDirection === 'asc' ? 'less than' : 'greater than')) {
56
- this.head.prev = newNode;
57
- newNode.next = this.head;
58
- this.head = newNode;
59
- }
60
- else {
61
- var currentNode = this.head;
62
- while (currentNode.next !== undefined &&
63
- this.sortFn(currentNode.next.event, newNode.event) !==
64
- (this.orderDirection === 'asc' ? 'less than' : 'greater than')) {
65
- currentNode = currentNode.next;
66
- }
67
- newNode.next = currentNode.next;
68
- if (currentNode.next !== undefined) {
69
- newNode.next.prev = newNode;
70
- }
71
- else {
72
- this.tail = newNode;
73
- }
74
- currentNode.next = newNode;
75
- newNode.prev = currentNode;
76
- }
77
- if (this.size > this.maxSize) {
78
- this.detach();
79
- }
80
- };
81
- EventList.prototype.detach = function () {
82
- var node = this.tail;
83
- if (node.prev !== undefined) {
84
- node.prev.next = node.next;
85
- }
86
- else {
87
- this.head = node.next;
88
- }
89
- if (node.next !== undefined) {
90
- node.next.prev = node.prev;
91
- }
92
- else {
93
- this.tail = node.prev;
94
- }
95
- this.size--;
96
- };
97
- EventList.prototype.toArray = function () {
98
- return Array.from(this);
99
- };
100
- EventList.prototype[Symbol.iterator] = function () {
101
- var node;
102
- return __generator(this, function (_a) {
103
- switch (_a.label) {
104
- case 0:
105
- node = this.head;
106
- _a.label = 1;
107
- case 1:
108
- if (!node) return [3 /*break*/, 3];
109
- return [4 /*yield*/, node.event];
110
- case 2:
111
- _a.sent();
112
- node = node.next;
113
- return [3 /*break*/, 1];
114
- case 3: return [2 /*return*/];
115
- }
116
- });
117
- };
118
- return EventList;
119
- }());
120
- exports.EventList = EventList;
@@ -1,202 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __generator = (this && this.__generator) || function (thisArg, body) {
12
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
- function verb(n) { return function (v) { return step([n, v]); }; }
15
- function step(op) {
16
- if (f) throw new TypeError("Generator is already executing.");
17
- while (_) try {
18
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
- if (y = 0, t) op = [op[0] & 2, t.value];
20
- switch (op[0]) {
21
- case 0: case 1: t = op; break;
22
- case 4: _.label++; return { value: op[1], done: false };
23
- case 5: _.label++; y = op[1]; op = [0]; continue;
24
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
- default:
26
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
- if (t[2]) _.ops.pop();
31
- _.trys.pop(); continue;
32
- }
33
- op = body.call(thisArg, _);
34
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
- }
37
- };
38
- exports.__esModule = true;
39
- exports.EventSubscriber = void 0;
40
- var types_1 = require("./types");
41
- var txEventCache_1 = require("./txEventCache");
42
- var eventList_1 = require("./eventList");
43
- var pollingLogProvider_1 = require("./pollingLogProvider");
44
- var fetchLogs_1 = require("./fetchLogs");
45
- var webSocketLogProvider_1 = require("./webSocketLogProvider");
46
- var events_1 = require("events");
47
- var sort_1 = require("./sort");
48
- var EventSubscriber = /** @class */ (function () {
49
- function EventSubscriber(connection, program, options) {
50
- if (options === void 0) { options = types_1.DefaultEventSubscriptionOptions; }
51
- this.connection = connection;
52
- this.program = program;
53
- this.options = options;
54
- this.awaitTxPromises = new Map();
55
- this.awaitTxResolver = new Map();
56
- this.options = Object.assign({}, types_1.DefaultEventSubscriptionOptions, options);
57
- this.txEventCache = new txEventCache_1.TxEventCache(this.options.maxTx);
58
- this.eventListMap = new Map();
59
- for (var _i = 0, _a = this.options.eventTypes; _i < _a.length; _i++) {
60
- var eventType = _a[_i];
61
- this.eventListMap.set(eventType, new eventList_1.EventList(eventType, this.options.maxEventsPerType, (0, sort_1.getSortFn)(this.options.orderBy, this.options.orderDir, eventType), this.options.orderDir));
62
- }
63
- this.eventEmitter = new events_1.EventEmitter();
64
- if (this.options.logProviderConfig.type === 'websocket') {
65
- this.logProvider = new webSocketLogProvider_1.WebSocketLogProvider(this.connection, this.program.programId, this.options.commitment);
66
- }
67
- else {
68
- this.logProvider = new pollingLogProvider_1.PollingLogProvider(this.connection, this.program.programId, options.commitment, this.options.logProviderConfig.frequency);
69
- }
70
- }
71
- EventSubscriber.prototype.subscribe = function () {
72
- return __awaiter(this, void 0, void 0, function () {
73
- var _this = this;
74
- return __generator(this, function (_a) {
75
- try {
76
- if (this.logProvider.isSubscribed()) {
77
- return [2 /*return*/, true];
78
- }
79
- this.logProvider.subscribe(function (txSig, slot, logs) {
80
- _this.handleTxLogs(txSig, slot, logs);
81
- }, true);
82
- return [2 /*return*/, true];
83
- }
84
- catch (e) {
85
- console.error('Error fetching previous txs in event subscriber');
86
- console.error(e);
87
- return [2 /*return*/, false];
88
- }
89
- return [2 /*return*/];
90
- });
91
- });
92
- };
93
- EventSubscriber.prototype.handleTxLogs = function (txSig, slot, logs) {
94
- if (this.txEventCache.has(txSig)) {
95
- return;
96
- }
97
- var wrappedEvents = this.parseEventsFromLogs(txSig, slot, logs);
98
- for (var _i = 0, wrappedEvents_1 = wrappedEvents; _i < wrappedEvents_1.length; _i++) {
99
- var wrappedEvent = wrappedEvents_1[_i];
100
- this.eventListMap.get(wrappedEvent.eventType).insert(wrappedEvent);
101
- this.eventEmitter.emit('newEvent', wrappedEvent);
102
- }
103
- if (this.awaitTxPromises.has(txSig)) {
104
- this.awaitTxPromises["delete"](txSig);
105
- this.awaitTxResolver.get(txSig)();
106
- this.awaitTxResolver["delete"](txSig);
107
- }
108
- if (slot > this.lastSeenSlot) {
109
- this.lastSeenTxSig = txSig;
110
- }
111
- this.txEventCache.add(txSig, wrappedEvents);
112
- };
113
- EventSubscriber.prototype.fetchPreviousTx = function (fetchMax) {
114
- return __awaiter(this, void 0, void 0, function () {
115
- var txFetched, beforeTx, untilTx, response, _i, _a, _b, txSig, slot, logs;
116
- return __generator(this, function (_c) {
117
- switch (_c.label) {
118
- case 0:
119
- if (!this.options.untilTx && !fetchMax) {
120
- return [2 /*return*/];
121
- }
122
- txFetched = 0;
123
- beforeTx = undefined;
124
- untilTx = this.options.untilTx;
125
- _c.label = 1;
126
- case 1:
127
- if (!(txFetched < this.options.maxTx)) return [3 /*break*/, 3];
128
- return [4 /*yield*/, (0, fetchLogs_1.fetchLogs)(this.connection, this.program.programId, this.options.commitment === 'finalized' ? 'finalized' : 'confirmed', beforeTx, untilTx)];
129
- case 2:
130
- response = _c.sent();
131
- if (response === undefined) {
132
- return [3 /*break*/, 3];
133
- }
134
- txFetched += response.transactionLogs.length;
135
- beforeTx = response.earliestTx;
136
- for (_i = 0, _a = response.transactionLogs; _i < _a.length; _i++) {
137
- _b = _a[_i], txSig = _b.txSig, slot = _b.slot, logs = _b.logs;
138
- this.handleTxLogs(txSig, slot, logs);
139
- }
140
- return [3 /*break*/, 1];
141
- case 3: return [2 /*return*/];
142
- }
143
- });
144
- });
145
- };
146
- EventSubscriber.prototype.unsubscribe = function () {
147
- return __awaiter(this, void 0, void 0, function () {
148
- return __generator(this, function (_a) {
149
- switch (_a.label) {
150
- case 0: return [4 /*yield*/, this.logProvider.unsubscribe()];
151
- case 1: return [2 /*return*/, _a.sent()];
152
- }
153
- });
154
- });
155
- };
156
- EventSubscriber.prototype.parseEventsFromLogs = function (txSig, slot, logs) {
157
- var _this = this;
158
- var records = [];
159
- // @ts-ignore
160
- this.program._events._eventParser.parseLogs(logs, function (event) {
161
- var expectRecordType = _this.eventListMap.has(event.name);
162
- if (expectRecordType) {
163
- event.data.txSig = txSig;
164
- event.data.slot = slot;
165
- event.data.eventType = event.name;
166
- records.push(event.data);
167
- }
168
- });
169
- return records;
170
- };
171
- EventSubscriber.prototype.awaitTx = function (txSig) {
172
- var _this = this;
173
- if (this.awaitTxPromises.has(txSig)) {
174
- return this.awaitTxPromises.get(txSig);
175
- }
176
- if (this.txEventCache.has(txSig)) {
177
- return Promise.resolve();
178
- }
179
- var promise = new Promise(function (resolve) {
180
- _this.awaitTxResolver.set(txSig, resolve);
181
- });
182
- this.awaitTxPromises.set(txSig, promise);
183
- return promise;
184
- };
185
- EventSubscriber.prototype.getEventList = function (eventType) {
186
- return this.eventListMap.get(eventType);
187
- };
188
- /**
189
- * This requires the EventList be cast to an array, which requires reallocation of memory.
190
- * Would bias to using getEventList over getEvents
191
- *
192
- * @param eventType
193
- */
194
- EventSubscriber.prototype.getEventsArray = function (eventType) {
195
- return this.eventListMap.get(eventType).toArray();
196
- };
197
- EventSubscriber.prototype.getEventsByTx = function (txSig) {
198
- return this.txEventCache.get(txSig);
199
- };
200
- return EventSubscriber;
201
- }());
202
- exports.EventSubscriber = EventSubscriber;