@drift-labs/sdk 0.2.0-temp.1 → 2.0.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 (274) hide show
  1. package/README.md +27 -29
  2. package/lib/accounts/bulkAccountLoader.d.ts +2 -0
  3. package/lib/accounts/bulkAccountLoader.js +36 -29
  4. package/lib/accounts/bulkUserStatsSubscription.d.ts +7 -0
  5. package/lib/accounts/bulkUserStatsSubscription.js +21 -0
  6. package/lib/accounts/bulkUserSubscription.d.ts +2 -2
  7. package/lib/accounts/bulkUserSubscription.js +0 -1
  8. package/lib/accounts/fetch.d.ts +2 -1
  9. package/lib/accounts/fetch.js +9 -1
  10. package/lib/accounts/{pollingClearingHouseAccountSubscriber.d.ts → pollingDriftClientAccountSubscriber.d.ts} +20 -25
  11. package/lib/accounts/{pollingClearingHouseAccountSubscriber.js → pollingDriftClientAccountSubscriber.js} +45 -49
  12. package/lib/accounts/{pollingOracleSubscriber.d.ts → pollingOracleAccountSubscriber.d.ts} +2 -2
  13. package/lib/accounts/{pollingOracleSubscriber.js → pollingOracleAccountSubscriber.js} +3 -3
  14. package/lib/accounts/pollingUserAccountSubscriber.d.ts +1 -1
  15. package/lib/accounts/pollingUserAccountSubscriber.js +5 -11
  16. package/lib/accounts/pollingUserStatsAccountSubscriber.d.ts +27 -0
  17. package/lib/accounts/pollingUserStatsAccountSubscriber.js +107 -0
  18. package/lib/accounts/types.d.ts +26 -15
  19. package/lib/accounts/webSocketDriftClientAccountSubscriber.d.ts +49 -0
  20. package/lib/accounts/{webSocketClearingHouseAccountSubscriber.js → webSocketDriftClientAccountSubscriber.js} +47 -45
  21. package/lib/accounts/webSocketUserStatsAccountSubsriber.d.ts +20 -0
  22. package/lib/accounts/webSocketUserStatsAccountSubsriber.js +47 -0
  23. package/lib/addresses/marketAddresses.d.ts +1 -3
  24. package/lib/addresses/marketAddresses.js +1 -1
  25. package/lib/addresses/pda.d.ts +15 -9
  26. package/lib/addresses/pda.js +73 -35
  27. package/lib/adminClient.d.ts +65 -0
  28. package/lib/adminClient.js +637 -0
  29. package/lib/config.d.ts +10 -10
  30. package/lib/config.js +26 -22
  31. package/lib/constants/numericConstants.d.ts +29 -12
  32. package/lib/constants/numericConstants.js +40 -21
  33. package/lib/constants/perpMarkets.d.ts +18 -0
  34. package/lib/constants/{markets.js → perpMarkets.js} +20 -9
  35. package/lib/constants/spotMarkets.d.ts +19 -0
  36. package/lib/constants/spotMarkets.js +63 -0
  37. package/lib/dlob/DLOB.d.ts +82 -0
  38. package/lib/dlob/DLOB.js +694 -0
  39. package/lib/dlob/DLOBNode.d.ts +54 -0
  40. package/lib/dlob/DLOBNode.js +77 -0
  41. package/lib/dlob/NodeList.d.ts +27 -0
  42. package/lib/dlob/NodeList.js +144 -0
  43. package/lib/driftClient.d.ts +234 -0
  44. package/lib/driftClient.js +2108 -0
  45. package/lib/{clearingHouseConfig.d.ts → driftClientConfig.d.ts} +9 -9
  46. package/lib/{clearingHouseConfig.js → driftClientConfig.js} +0 -0
  47. package/lib/events/eventList.js +3 -0
  48. package/lib/events/eventSubscriber.d.ts +5 -2
  49. package/lib/events/eventSubscriber.js +25 -11
  50. package/lib/events/fetchLogs.d.ts +13 -2
  51. package/lib/events/fetchLogs.js +45 -14
  52. package/lib/events/pollingLogProvider.d.ts +2 -1
  53. package/lib/events/pollingLogProvider.js +7 -3
  54. package/lib/events/sort.js +8 -11
  55. package/lib/events/types.d.ts +11 -3
  56. package/lib/events/types.js +8 -0
  57. package/lib/events/webSocketLogProvider.js +1 -1
  58. package/lib/examples/makeTradeExample.js +30 -18
  59. package/lib/factory/bigNum.d.ts +8 -4
  60. package/lib/factory/bigNum.js +109 -19
  61. package/lib/idl/drift.json +8392 -0
  62. package/lib/index.d.ts +29 -12
  63. package/lib/index.js +29 -12
  64. package/lib/math/amm.d.ts +9 -6
  65. package/lib/math/amm.js +91 -38
  66. package/lib/math/conversion.js +1 -1
  67. package/lib/math/exchangeStatus.d.ts +4 -0
  68. package/lib/math/exchangeStatus.js +18 -0
  69. package/lib/math/funding.d.ts +6 -6
  70. package/lib/math/funding.js +23 -21
  71. package/lib/math/insurance.d.ts +4 -0
  72. package/lib/math/insurance.js +27 -0
  73. package/lib/math/margin.d.ts +11 -0
  74. package/lib/math/margin.js +82 -0
  75. package/lib/math/market.d.ts +14 -9
  76. package/lib/math/market.js +70 -10
  77. package/lib/math/oracles.d.ts +4 -0
  78. package/lib/math/oracles.js +36 -8
  79. package/lib/math/orders.d.ts +16 -6
  80. package/lib/math/orders.js +97 -17
  81. package/lib/math/position.d.ts +27 -13
  82. package/lib/math/position.js +91 -37
  83. package/lib/math/repeg.js +17 -8
  84. package/lib/math/spotBalance.d.ts +22 -0
  85. package/lib/math/spotBalance.js +192 -0
  86. package/lib/math/spotMarket.d.ts +4 -0
  87. package/lib/math/spotMarket.js +8 -0
  88. package/lib/math/spotPosition.d.ts +6 -0
  89. package/lib/math/spotPosition.js +23 -0
  90. package/lib/math/trade.d.ts +10 -10
  91. package/lib/math/trade.js +27 -31
  92. package/lib/oracles/pythClient.js +1 -1
  93. package/lib/oracles/quoteAssetOracleClient.js +1 -1
  94. package/lib/oracles/switchboardClient.js +1 -1
  95. package/lib/orderParams.d.ts +4 -4
  96. package/lib/orderParams.js +12 -4
  97. package/lib/serum/serumFulfillmentConfigMap.d.ts +10 -0
  98. package/lib/serum/serumFulfillmentConfigMap.js +17 -0
  99. package/lib/serum/serumSubscriber.d.ts +27 -0
  100. package/lib/serum/serumSubscriber.js +56 -0
  101. package/lib/serum/types.d.ts +11 -0
  102. package/{src/oracles → lib/serum}/types.js +0 -0
  103. package/lib/tokenFaucet.d.ts +1 -0
  104. package/lib/tokenFaucet.js +23 -12
  105. package/lib/tx/retryTxSender.d.ts +1 -1
  106. package/lib/tx/retryTxSender.js +13 -4
  107. package/lib/tx/types.d.ts +1 -1
  108. package/lib/types.d.ts +631 -222
  109. package/lib/types.js +137 -24
  110. package/lib/user.d.ts +228 -0
  111. package/lib/user.js +959 -0
  112. package/lib/userConfig.d.ts +14 -0
  113. package/lib/{clearingHouseUserConfig.js → userConfig.js} +0 -0
  114. package/lib/userMap/userMap.d.ts +41 -0
  115. package/lib/userMap/userMap.js +85 -0
  116. package/lib/userMap/userStatsMap.d.ts +19 -0
  117. package/lib/userMap/userStatsMap.js +68 -0
  118. package/lib/userName.d.ts +1 -0
  119. package/lib/userName.js +3 -2
  120. package/lib/userStats.d.ts +18 -0
  121. package/lib/userStats.js +49 -0
  122. package/lib/userStatsConfig.d.ts +14 -0
  123. package/{src/clearingHouseConfig.js → lib/userStatsConfig.js} +0 -0
  124. package/lib/util/getTokenAddress.d.ts +2 -0
  125. package/lib/util/getTokenAddress.js +9 -0
  126. package/package.json +12 -5
  127. package/src/accounts/bulkAccountLoader.ts +44 -34
  128. package/src/accounts/bulkUserStatsSubscription.ts +33 -0
  129. package/src/accounts/bulkUserSubscription.ts +2 -3
  130. package/src/accounts/fetch.ts +27 -2
  131. package/src/accounts/{pollingClearingHouseAccountSubscriber.ts → pollingDriftClientAccountSubscriber.ts} +65 -75
  132. package/src/accounts/{pollingOracleSubscriber.ts → pollingOracleAccountSubscriber.ts} +2 -2
  133. package/src/accounts/pollingUserAccountSubscriber.ts +5 -12
  134. package/src/accounts/pollingUserStatsAccountSubscriber.ts +166 -0
  135. package/src/accounts/types.ts +35 -15
  136. package/src/accounts/{webSocketClearingHouseAccountSubscriber.ts → webSocketDriftClientAccountSubscriber.ts} +78 -73
  137. package/src/accounts/webSocketUserStatsAccountSubsriber.ts +80 -0
  138. package/src/addresses/marketAddresses.ts +3 -4
  139. package/src/addresses/pda.ts +105 -33
  140. package/src/adminClient.ts +1207 -0
  141. package/src/config.ts +41 -34
  142. package/src/constants/numericConstants.ts +59 -26
  143. package/src/constants/{markets.ts → perpMarkets.ts} +22 -11
  144. package/src/constants/spotMarkets.ts +83 -0
  145. package/src/dlob/DLOB.ts +1120 -0
  146. package/src/dlob/DLOBNode.ts +155 -0
  147. package/src/dlob/NodeList.ts +195 -0
  148. package/src/driftClient.ts +3594 -0
  149. package/src/{clearingHouseConfig.ts → driftClientConfig.ts} +9 -8
  150. package/src/events/eventList.ts +3 -0
  151. package/src/events/eventSubscriber.ts +36 -14
  152. package/src/events/fetchLogs.ts +60 -15
  153. package/src/events/pollingLogProvider.ts +11 -3
  154. package/src/events/sort.ts +11 -15
  155. package/src/events/types.ts +27 -2
  156. package/src/events/webSocketLogProvider.ts +1 -1
  157. package/src/examples/makeTradeExample.js +152 -75
  158. package/src/examples/makeTradeExample.ts +44 -28
  159. package/src/factory/bigNum.ts +150 -22
  160. package/src/idl/drift.json +8392 -0
  161. package/src/idl/pyth.json +98 -2
  162. package/src/index.ts +29 -12
  163. package/src/math/amm.ts +161 -48
  164. package/src/math/conversion.ts +2 -2
  165. package/src/math/exchangeStatus.ts +31 -0
  166. package/src/math/funding.ts +41 -31
  167. package/src/math/insurance.ts +35 -0
  168. package/src/math/margin.ts +133 -0
  169. package/src/math/market.ts +143 -14
  170. package/src/math/oracles.ts +63 -9
  171. package/src/math/orders.ts +168 -26
  172. package/src/math/position.ts +133 -59
  173. package/src/math/repeg.ts +19 -9
  174. package/src/math/spotBalance.ts +319 -0
  175. package/src/math/spotMarket.ts +9 -0
  176. package/src/math/spotPosition.ts +47 -0
  177. package/src/math/trade.ts +33 -37
  178. package/src/oracles/pythClient.ts +2 -2
  179. package/src/oracles/quoteAssetOracleClient.ts +2 -2
  180. package/src/oracles/switchboardClient.ts +2 -2
  181. package/src/orderParams.ts +16 -8
  182. package/src/serum/serumFulfillmentConfigMap.ts +26 -0
  183. package/src/serum/serumSubscriber.ts +99 -0
  184. package/src/serum/types.ts +13 -0
  185. package/src/tokenFaucet.ts +38 -15
  186. package/src/tx/retryTxSender.ts +16 -5
  187. package/src/tx/types.ts +2 -1
  188. package/src/types.ts +594 -195
  189. package/src/user.ts +1599 -0
  190. package/src/{clearingHouseUserConfig.ts → userConfig.ts} +5 -5
  191. package/src/userMap/userMap.ts +124 -0
  192. package/src/userMap/userStatsMap.ts +108 -0
  193. package/src/userName.ts +2 -1
  194. package/src/userStats.ts +75 -0
  195. package/src/userStatsConfig.ts +18 -0
  196. package/src/util/getTokenAddress.ts +18 -0
  197. package/tests/bn/test.ts +46 -11
  198. package/tests/dlob/helpers.ts +619 -0
  199. package/tests/dlob/test.ts +4586 -0
  200. package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +0 -49
  201. package/lib/admin.d.ts +0 -44
  202. package/lib/admin.js +0 -438
  203. package/lib/clearingHouse.d.ts +0 -146
  204. package/lib/clearingHouse.js +0 -1154
  205. package/lib/clearingHouseUser.d.ts +0 -187
  206. package/lib/clearingHouseUser.js +0 -634
  207. package/lib/clearingHouseUserConfig.d.ts +0 -14
  208. package/lib/constants/banks.d.ts +0 -16
  209. package/lib/constants/banks.js +0 -41
  210. package/lib/constants/markets.d.ts +0 -19
  211. package/lib/idl/clearing_house.json +0 -4464
  212. package/lib/math/bankBalance.d.ts +0 -9
  213. package/lib/math/bankBalance.js +0 -75
  214. package/lib/math/state.d.ts +0 -8
  215. package/lib/math/state.js +0 -15
  216. package/lib/orders.d.ts +0 -8
  217. package/lib/orders.js +0 -134
  218. package/src/accounts/bulkAccountLoader.js +0 -197
  219. package/src/accounts/bulkUserSubscription.js +0 -33
  220. package/src/accounts/pollingClearingHouseAccountSubscriber.js +0 -311
  221. package/src/accounts/pollingOracleSubscriber.js +0 -93
  222. package/src/accounts/pollingTokenAccountSubscriber.js +0 -90
  223. package/src/accounts/pollingUserAccountSubscriber.js +0 -132
  224. package/src/accounts/types.js +0 -10
  225. package/src/accounts/utils.js +0 -7
  226. package/src/accounts/webSocketAccountSubscriber.js +0 -93
  227. package/src/accounts/webSocketClearingHouseAccountSubscriber.js +0 -233
  228. package/src/accounts/webSocketUserAccountSubscriber.js +0 -62
  229. package/src/addresses/marketAddresses.js +0 -26
  230. package/src/admin.js +0 -517
  231. package/src/admin.ts +0 -730
  232. package/src/clearingHouse.ts +0 -1828
  233. package/src/clearingHouseUser.ts +0 -978
  234. package/src/clearingHouseUserConfig.js +0 -2
  235. package/src/config.js +0 -67
  236. package/src/constants/banks.js +0 -42
  237. package/src/constants/banks.ts +0 -50
  238. package/src/constants/markets.js +0 -42
  239. package/src/constants/numericConstants.js +0 -41
  240. package/src/events/eventSubscriber.js +0 -139
  241. package/src/events/fetchLogs.js +0 -50
  242. package/src/events/pollingLogProvider.js +0 -64
  243. package/src/events/sort.js +0 -44
  244. package/src/events/txEventCache.js +0 -71
  245. package/src/events/types.js +0 -20
  246. package/src/events/webSocketLogProvider.js +0 -41
  247. package/src/factory/bigNum.js +0 -390
  248. package/src/factory/oracleClient.js +0 -20
  249. package/src/idl/clearing_house.json +0 -4464
  250. package/src/index.js +0 -69
  251. package/src/math/amm.js +0 -369
  252. package/src/math/auction.js +0 -42
  253. package/src/math/bankBalance.ts +0 -112
  254. package/src/math/conversion.js +0 -11
  255. package/src/math/funding.js +0 -248
  256. package/src/math/oracles.js +0 -26
  257. package/src/math/repeg.js +0 -128
  258. package/src/math/state.js +0 -15
  259. package/src/math/state.ts +0 -14
  260. package/src/math/trade.js +0 -253
  261. package/src/math/utils.js +0 -26
  262. package/src/math/utils.js.map +0 -1
  263. package/src/mockUSDCFaucet.js +0 -280
  264. package/src/oracles/oracleClientCache.js +0 -19
  265. package/src/oracles/pythClient.js +0 -46
  266. package/src/oracles/quoteAssetOracleClient.js +0 -32
  267. package/src/oracles/switchboardClient.js +0 -69
  268. package/src/orderParams.js +0 -20
  269. package/src/orders.ts +0 -245
  270. package/src/slot/SlotSubscriber.js +0 -39
  271. package/src/tokenFaucet.js +0 -189
  272. package/src/types.js +0 -125
  273. package/src/userName.js +0 -20
  274. package/src/wallet.js +0 -35
package/src/math/utils.js DELETED
@@ -1,26 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.squareRootBN = void 0;
4
- const __1 = require("../");
5
- const squareRootBN = (n, closeness = new __1.BN(1)) => {
6
- // Assuming the sqrt of n as n only
7
- let x = n;
8
- // The closed guess will be stored in the root
9
- let root;
10
- // To count the number of iterations
11
- let count = 0;
12
- const TWO = new __1.BN(2);
13
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
14
- while (count < Number.MAX_SAFE_INTEGER) {
15
- count++;
16
- // Calculate more closed x
17
- root = x.add(n.div(x)).div(TWO);
18
- // Check for closeness
19
- if (x.sub(root).abs().lte(closeness))
20
- break;
21
- // Update root
22
- x = root;
23
- }
24
- return root;
25
- };
26
- exports.squareRootBN = squareRootBN;
@@ -1 +0,0 @@
1
- {"version":3,"file":"utils.js","sourceRoot":"","sources":["utils.ts"],"names":[],"mappings":";;;AAAA,2BAAyB;AAElB,MAAM,YAAY,GAAG,CAAC,CAAC,EAAE,SAAS,GAAG,IAAI,MAAE,CAAC,CAAC,CAAC,EAAE,EAAE;IACxD,mCAAmC;IACnC,IAAI,CAAC,GAAG,CAAC,CAAC;IAEV,8CAA8C;IAC9C,IAAI,IAAI,CAAC;IAET,oCAAoC;IACpC,IAAI,KAAK,GAAG,CAAC,CAAC;IACd,MAAM,GAAG,GAAG,IAAI,MAAE,CAAC,CAAC,CAAC,CAAC;IAEtB,6DAA6D;IAC7D,OAAO,KAAK,GAAG,MAAM,CAAC,gBAAgB,EAAE;QACvC,KAAK,EAAE,CAAC;QAER,0BAA0B;QAC1B,IAAI,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QAEhC,sBAAsB;QACtB,IAAI,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,SAAS,CAAC;YAAE,MAAM;QAE5C,cAAc;QACd,CAAC,GAAG,IAAI,CAAC;KACT;IAED,OAAO,IAAI,CAAC;AACb,CAAC,CAAC;AA1BW,QAAA,YAAY,gBA0BvB"}
@@ -1,280 +0,0 @@
1
- 'use strict';
2
- var __createBinding =
3
- (this && this.__createBinding) ||
4
- (Object.create
5
- ? function (o, m, k, k2) {
6
- if (k2 === undefined) k2 = k;
7
- Object.defineProperty(o, k2, {
8
- enumerable: true,
9
- get: function () {
10
- return m[k];
11
- },
12
- });
13
- }
14
- : function (o, m, k, k2) {
15
- if (k2 === undefined) k2 = k;
16
- o[k2] = m[k];
17
- });
18
- var __setModuleDefault =
19
- (this && this.__setModuleDefault) ||
20
- (Object.create
21
- ? function (o, v) {
22
- Object.defineProperty(o, 'default', { enumerable: true, value: v });
23
- }
24
- : function (o, v) {
25
- o['default'] = v;
26
- });
27
- var __importStar =
28
- (this && this.__importStar) ||
29
- function (mod) {
30
- if (mod && mod.__esModule) return mod;
31
- var result = {};
32
- if (mod != null)
33
- for (var k in mod)
34
- if (k !== 'default' && Object.prototype.hasOwnProperty.call(mod, k))
35
- __createBinding(result, mod, k);
36
- __setModuleDefault(result, mod);
37
- return result;
38
- };
39
- var __awaiter =
40
- (this && this.__awaiter) ||
41
- function (thisArg, _arguments, P, generator) {
42
- function adopt(value) {
43
- return value instanceof P
44
- ? value
45
- : new P(function (resolve) {
46
- resolve(value);
47
- });
48
- }
49
- return new (P || (P = Promise))(function (resolve, reject) {
50
- function fulfilled(value) {
51
- try {
52
- step(generator.next(value));
53
- } catch (e) {
54
- reject(e);
55
- }
56
- }
57
- function rejected(value) {
58
- try {
59
- step(generator['throw'](value));
60
- } catch (e) {
61
- reject(e);
62
- }
63
- }
64
- function step(result) {
65
- result.done
66
- ? resolve(result.value)
67
- : adopt(result.value).then(fulfilled, rejected);
68
- }
69
- step((generator = generator.apply(thisArg, _arguments || [])).next());
70
- });
71
- };
72
- var __importDefault =
73
- (this && this.__importDefault) ||
74
- function (mod) {
75
- return mod && mod.__esModule ? mod : { default: mod };
76
- };
77
- Object.defineProperty(exports, '__esModule', { value: true });
78
- exports.MockUSDCFaucet = void 0;
79
- const anchor = __importStar(require('@project-serum/anchor'));
80
- const anchor_1 = require('@project-serum/anchor');
81
- const spl_token_1 = require('@solana/spl-token');
82
- const web3_js_1 = require('@solana/web3.js');
83
- const mock_usdc_faucet_json_1 = __importDefault(
84
- require('./idl/token_faucet.json')
85
- );
86
- class MockUSDCFaucet {
87
- constructor(connection, wallet, programId, opts) {
88
- this.connection = connection;
89
- this.wallet = wallet;
90
- this.opts = opts || anchor_1.AnchorProvider.defaultOptions();
91
- const provider = new anchor_1.AnchorProvider(connection, wallet, this.opts);
92
- this.provider = provider;
93
- this.program = new anchor_1.Program(
94
- mock_usdc_faucet_json_1.default,
95
- programId,
96
- provider
97
- );
98
- }
99
- getMockUSDCFaucetStatePublicKeyAndNonce() {
100
- return __awaiter(this, void 0, void 0, function* () {
101
- return anchor.web3.PublicKey.findProgramAddress(
102
- [Buffer.from(anchor.utils.bytes.utf8.encode('mock_usdc_faucet'))],
103
- this.program.programId
104
- );
105
- });
106
- }
107
- getMockUSDCFaucetStatePublicKey() {
108
- return __awaiter(this, void 0, void 0, function* () {
109
- if (this.mockUSDCFaucetStatePublicKey) {
110
- return this.mockUSDCFaucetStatePublicKey;
111
- }
112
- this.mockUSDCFaucetStatePublicKey =
113
- (yield this.getMockUSDCFaucetStatePublicKeyAndNonce())[0];
114
- return this.mockUSDCFaucetStatePublicKey;
115
- });
116
- }
117
- initialize() {
118
- return __awaiter(this, void 0, void 0, function* () {
119
- const stateAccountRPCResponse =
120
- yield this.connection.getParsedAccountInfo(
121
- yield this.getMockUSDCFaucetStatePublicKey()
122
- );
123
- if (stateAccountRPCResponse.value !== null) {
124
- throw new Error('Faucet already initialized');
125
- }
126
- const fakeUSDCMint = anchor.web3.Keypair.generate();
127
- const createUSDCMintAccountIx = web3_js_1.SystemProgram.createAccount({
128
- fromPubkey: this.wallet.publicKey,
129
- newAccountPubkey: fakeUSDCMint.publicKey,
130
- lamports: yield spl_token_1.Token.getMinBalanceRentForExemptMint(
131
- this.connection
132
- ),
133
- space: spl_token_1.MintLayout.span,
134
- programId: spl_token_1.TOKEN_PROGRAM_ID,
135
- });
136
- const [mintAuthority, _mintAuthorityNonce] =
137
- yield web3_js_1.PublicKey.findProgramAddress(
138
- [fakeUSDCMint.publicKey.toBuffer()],
139
- this.program.programId
140
- );
141
- const initUSDCMintIx = spl_token_1.Token.createInitMintInstruction(
142
- spl_token_1.TOKEN_PROGRAM_ID,
143
- fakeUSDCMint.publicKey,
144
- 6,
145
- mintAuthority,
146
- null
147
- );
148
- const [mockUSDCFaucetStatePublicKey, mockUSDCFaucetStateNonce] =
149
- yield this.getMockUSDCFaucetStatePublicKeyAndNonce();
150
- return yield this.program.rpc.initialize(mockUSDCFaucetStateNonce, {
151
- accounts: {
152
- mockUsdcFaucetState: mockUSDCFaucetStatePublicKey,
153
- admin: this.wallet.publicKey,
154
- mintAccount: fakeUSDCMint.publicKey,
155
- rent: web3_js_1.SYSVAR_RENT_PUBKEY,
156
- systemProgram: anchor.web3.SystemProgram.programId,
157
- },
158
- instructions: [createUSDCMintAccountIx, initUSDCMintIx],
159
- signers: [fakeUSDCMint],
160
- });
161
- });
162
- }
163
- fetchState() {
164
- return __awaiter(this, void 0, void 0, function* () {
165
- return yield this.program.account.mockUsdcFaucetState.fetch(
166
- yield this.getMockUSDCFaucetStatePublicKey()
167
- );
168
- });
169
- }
170
- mintToUser(userTokenAccount, amount) {
171
- return __awaiter(this, void 0, void 0, function* () {
172
- const state = yield this.fetchState();
173
- return yield this.program.rpc.mintToUser(amount, {
174
- accounts: {
175
- mockUsdcFaucetState: yield this.getMockUSDCFaucetStatePublicKey(),
176
- mintAccount: state.mint,
177
- userTokenAccount,
178
- mintAuthority: state.mintAuthority,
179
- tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
180
- },
181
- });
182
- });
183
- }
184
- createAssociatedTokenAccountAndMintTo(userPublicKey, amount) {
185
- return __awaiter(this, void 0, void 0, function* () {
186
- const [associatedTokenPublicKey, createAssociatedAccountIx, mintToTx] =
187
- yield this.createAssociatedTokenAccountAndMintToInstructions(
188
- userPublicKey,
189
- amount
190
- );
191
- const tx = new web3_js_1.Transaction()
192
- .add(createAssociatedAccountIx)
193
- .add(mintToTx);
194
- const txSig = yield this.program.provider.sendAndConfirm(
195
- tx,
196
- [],
197
- this.opts
198
- );
199
- return [associatedTokenPublicKey, txSig];
200
- });
201
- }
202
- createAssociatedTokenAccountAndMintToInstructions(userPublicKey, amount) {
203
- return __awaiter(this, void 0, void 0, function* () {
204
- const state = yield this.fetchState();
205
- const associateTokenPublicKey =
206
- yield this.getAssosciatedMockUSDMintAddress({
207
- userPubKey: userPublicKey,
208
- });
209
- const createAssociatedAccountIx =
210
- spl_token_1.Token.createAssociatedTokenAccountInstruction(
211
- spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID,
212
- spl_token_1.TOKEN_PROGRAM_ID,
213
- state.mint,
214
- associateTokenPublicKey,
215
- userPublicKey,
216
- this.wallet.publicKey
217
- );
218
- const mintToIx = yield this.program.instruction.mintToUser(amount, {
219
- accounts: {
220
- mockUsdcFaucetState: yield this.getMockUSDCFaucetStatePublicKey(),
221
- mintAccount: state.mint,
222
- userTokenAccount: associateTokenPublicKey,
223
- mintAuthority: state.mintAuthority,
224
- tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
225
- },
226
- });
227
- return [associateTokenPublicKey, createAssociatedAccountIx, mintToIx];
228
- });
229
- }
230
- getAssosciatedMockUSDMintAddress(props) {
231
- return __awaiter(this, void 0, void 0, function* () {
232
- const state = yield this.fetchState();
233
- return spl_token_1.Token.getAssociatedTokenAddress(
234
- spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID,
235
- spl_token_1.TOKEN_PROGRAM_ID,
236
- state.mint,
237
- props.userPubKey
238
- );
239
- });
240
- }
241
- getTokenAccountInfo(props) {
242
- return __awaiter(this, void 0, void 0, function* () {
243
- const assosciatedKey = yield this.getAssosciatedMockUSDMintAddress(props);
244
- const state = yield this.fetchState();
245
- const token = new spl_token_1.Token(
246
- this.connection,
247
- state.mint,
248
- spl_token_1.TOKEN_PROGRAM_ID,
249
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
250
- // @ts-ignore
251
- this.provider.payer
252
- );
253
- return yield token.getAccountInfo(assosciatedKey);
254
- });
255
- }
256
- subscribeToTokenAccount(props) {
257
- return __awaiter(this, void 0, void 0, function* () {
258
- try {
259
- const tokenAccountKey = yield this.getAssosciatedMockUSDMintAddress(
260
- props
261
- );
262
- props.callback(yield this.getTokenAccountInfo(props));
263
- // Couldn't find a way to do it using anchor framework subscription, someone on serum discord recommended this way
264
- this.connection.onAccountChange(
265
- tokenAccountKey,
266
- (
267
- _accountInfo /* accountInfo is a buffer which we don't know how to deserialize */
268
- ) =>
269
- __awaiter(this, void 0, void 0, function* () {
270
- props.callback(yield this.getTokenAccountInfo(props));
271
- })
272
- );
273
- return true;
274
- } catch (e) {
275
- return false;
276
- }
277
- });
278
- }
279
- }
280
- exports.MockUSDCFaucet = MockUSDCFaucet;
@@ -1,19 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.OracleClientCache = void 0;
4
- const oracleClient_1 = require("../factory/oracleClient");
5
- class OracleClientCache {
6
- constructor() {
7
- this.cache = new Map();
8
- }
9
- get(oracleSource, connection) {
10
- const key = Object.keys(oracleSource)[0];
11
- if (this.cache.has(key)) {
12
- return this.cache.get(key);
13
- }
14
- const client = oracleClient_1.getOracleClient(oracleSource, connection);
15
- this.cache.set(key, client);
16
- return client;
17
- }
18
- }
19
- exports.OracleClientCache = OracleClientCache;
@@ -1,46 +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
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.convertPythPrice = exports.PythClient = void 0;
13
- const client_1 = require("@pythnetwork/client");
14
- const anchor_1 = require("@project-serum/anchor");
15
- const numericConstants_1 = require("../constants/numericConstants");
16
- class PythClient {
17
- constructor(connection) {
18
- this.connection = connection;
19
- }
20
- getOraclePriceData(pricePublicKey) {
21
- return __awaiter(this, void 0, void 0, function* () {
22
- const accountInfo = yield this.connection.getAccountInfo(pricePublicKey);
23
- return this.getOraclePriceDataFromBuffer(accountInfo.data);
24
- });
25
- }
26
- getOraclePriceDataFromBuffer(buffer) {
27
- const priceData = client_1.parsePriceData(buffer);
28
- return {
29
- price: convertPythPrice(priceData.aggregate.price, priceData.exponent),
30
- slot: new anchor_1.BN(priceData.lastSlot.toString()),
31
- confidence: convertPythPrice(priceData.confidence, priceData.exponent),
32
- twap: convertPythPrice(priceData.twap.value, priceData.exponent),
33
- twapConfidence: convertPythPrice(priceData.twac.value, priceData.exponent),
34
- hasSufficientNumberOfDataPoints: true,
35
- };
36
- }
37
- }
38
- exports.PythClient = PythClient;
39
- function convertPythPrice(price, exponent) {
40
- exponent = Math.abs(exponent);
41
- const pythPrecision = numericConstants_1.TEN.pow(new anchor_1.BN(exponent).abs());
42
- return new anchor_1.BN(price * Math.pow(10, exponent))
43
- .mul(numericConstants_1.MARK_PRICE_PRECISION)
44
- .div(pythPrecision);
45
- }
46
- exports.convertPythPrice = convertPythPrice;
@@ -1,32 +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
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.QuoteAssetOracleClient = exports.QUOTE_ORACLE_PRICE_DATA = void 0;
13
- const anchor_1 = require("@project-serum/anchor");
14
- const numericConstants_1 = require("../constants/numericConstants");
15
- exports.QUOTE_ORACLE_PRICE_DATA = {
16
- price: numericConstants_1.MARK_PRICE_PRECISION,
17
- slot: new anchor_1.BN(0),
18
- confidence: new anchor_1.BN(1),
19
- hasSufficientNumberOfDataPoints: true,
20
- };
21
- class QuoteAssetOracleClient {
22
- constructor() { }
23
- getOraclePriceData(_pricePublicKey) {
24
- return __awaiter(this, void 0, void 0, function* () {
25
- return Promise.resolve(exports.QUOTE_ORACLE_PRICE_DATA);
26
- });
27
- }
28
- getOraclePriceDataFromBuffer(_buffer) {
29
- return exports.QUOTE_ORACLE_PRICE_DATA;
30
- }
31
- }
32
- exports.QuoteAssetOracleClient = QuoteAssetOracleClient;
@@ -1,69 +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 __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.SwitchboardClient = void 0;
16
- const web3_js_1 = require("@solana/web3.js");
17
- const anchor_1 = require("@project-serum/anchor");
18
- const numericConstants_1 = require("../constants/numericConstants");
19
- const wallet_1 = require("../wallet");
20
- const switchboard_v2_json_1 = __importDefault(require("../idl/switchboard_v2.json"));
21
- let program;
22
- class SwitchboardClient {
23
- constructor(connection) {
24
- this.connection = connection;
25
- }
26
- getOraclePriceData(pricePublicKey) {
27
- return __awaiter(this, void 0, void 0, function* () {
28
- const accountInfo = yield this.connection.getAccountInfo(pricePublicKey);
29
- return this.getOraclePriceDataFromBuffer(accountInfo.data);
30
- });
31
- }
32
- getOraclePriceDataFromBuffer(buffer) {
33
- const program = this.getProgram();
34
- const aggregatorAccountData = program.account.aggregatorAccountData.coder.accounts.decode('AggregatorAccountData', buffer);
35
- const price = convertSwitchboardDecimal(aggregatorAccountData.latestConfirmedRound.result);
36
- const confidence = convertSwitchboardDecimal(aggregatorAccountData.latestConfirmedRound
37
- .stdDeviation);
38
- const hasSufficientNumberOfDataPoints = aggregatorAccountData.latestConfirmedRound.numSuccess >=
39
- aggregatorAccountData.minOracleResults;
40
- const slot = aggregatorAccountData.latestConfirmedRound.roundOpenSlot;
41
- return {
42
- price,
43
- slot,
44
- confidence,
45
- hasSufficientNumberOfDataPoints,
46
- };
47
- }
48
- getProgram() {
49
- if (program) {
50
- return program;
51
- }
52
- program = getSwitchboardProgram(this.connection);
53
- return program;
54
- }
55
- }
56
- exports.SwitchboardClient = SwitchboardClient;
57
- function getSwitchboardProgram(connection) {
58
- const DEFAULT_KEYPAIR = web3_js_1.Keypair.fromSeed(new Uint8Array(32).fill(1));
59
- const programId = web3_js_1.PublicKey.default;
60
- const wallet = new wallet_1.Wallet(DEFAULT_KEYPAIR);
61
- const provider = new anchor_1.AnchorProvider(connection, wallet, {});
62
- return new anchor_1.Program(switchboard_v2_json_1.default, programId, provider);
63
- }
64
- function convertSwitchboardDecimal(switchboardDecimal) {
65
- const switchboardPrecision = numericConstants_1.TEN.pow(new anchor_1.BN(switchboardDecimal.scale));
66
- return switchboardDecimal.mantissa
67
- .mul(numericConstants_1.MARK_PRICE_PRECISION)
68
- .div(switchboardPrecision);
69
- }
@@ -1,20 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getMarketOrderParams = exports.getTriggerLimitOrderParams = exports.getTriggerMarketOrderParams = exports.getLimitOrderParams = void 0;
4
- const types_1 = require("./types");
5
- function getLimitOrderParams(params) {
6
- return Object.assign({}, params, { orderType: types_1.OrderType.LIMIT });
7
- }
8
- exports.getLimitOrderParams = getLimitOrderParams;
9
- function getTriggerMarketOrderParams(params) {
10
- return Object.assign({}, params, { orderType: types_1.OrderType.TRIGGER_MARKET });
11
- }
12
- exports.getTriggerMarketOrderParams = getTriggerMarketOrderParams;
13
- function getTriggerLimitOrderParams(params) {
14
- return Object.assign({}, params, { orderType: types_1.OrderType.TRIGGER_LIMIT });
15
- }
16
- exports.getTriggerLimitOrderParams = getTriggerLimitOrderParams;
17
- function getMarketOrderParams(params) {
18
- return Object.assign({}, params, { orderType: types_1.OrderType.MARKET });
19
- }
20
- exports.getMarketOrderParams = getMarketOrderParams;