@drift-labs/sdk 0.2.0-master.3 → 0.2.0-master.31

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 (208) hide show
  1. package/README.md +13 -13
  2. package/lib/accounts/bulkUserStatsSubscription.d.ts +7 -0
  3. package/lib/accounts/bulkUserStatsSubscription.js +21 -0
  4. package/lib/accounts/bulkUserSubscription.js +0 -1
  5. package/lib/accounts/fetch.d.ts +2 -1
  6. package/lib/accounts/fetch.js +9 -1
  7. package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +16 -16
  8. package/lib/accounts/pollingClearingHouseAccountSubscriber.js +31 -28
  9. package/lib/accounts/pollingUserStatsAccountSubscriber.d.ts +27 -0
  10. package/lib/accounts/pollingUserStatsAccountSubscriber.js +113 -0
  11. package/lib/accounts/types.d.ts +22 -11
  12. package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +17 -17
  13. package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +37 -35
  14. package/lib/accounts/webSocketUserStatsAccountSubsriber.d.ts +20 -0
  15. package/lib/accounts/webSocketUserStatsAccountSubsriber.js +47 -0
  16. package/lib/addresses/marketAddresses.d.ts +1 -3
  17. package/lib/addresses/pda.d.ts +10 -4
  18. package/lib/addresses/pda.js +51 -14
  19. package/lib/admin.d.ts +42 -26
  20. package/lib/admin.js +228 -81
  21. package/lib/clearingHouse.d.ts +130 -49
  22. package/lib/clearingHouse.js +1256 -367
  23. package/lib/clearingHouseConfig.d.ts +4 -4
  24. package/lib/clearingHouseUser.d.ts +51 -39
  25. package/lib/clearingHouseUser.js +408 -191
  26. package/lib/clearingHouseUserStats.d.ts +18 -0
  27. package/lib/clearingHouseUserStats.js +49 -0
  28. package/lib/clearingHouseUserStatsConfig.d.ts +14 -0
  29. package/lib/clearingHouseUserStatsConfig.js +2 -0
  30. package/lib/config.d.ts +7 -9
  31. package/lib/config.js +21 -21
  32. package/lib/constants/numericConstants.d.ts +25 -12
  33. package/lib/constants/numericConstants.js +35 -21
  34. package/lib/constants/perpMarkets.d.ts +18 -0
  35. package/lib/constants/{markets.js → perpMarkets.js} +7 -7
  36. package/lib/constants/spotMarkets.d.ts +19 -0
  37. package/lib/constants/spotMarkets.js +53 -0
  38. package/lib/dlob/DLOB.d.ts +74 -0
  39. package/lib/dlob/DLOB.js +595 -0
  40. package/lib/dlob/DLOBNode.d.ts +54 -0
  41. package/lib/dlob/DLOBNode.js +85 -0
  42. package/lib/dlob/NodeList.d.ts +26 -0
  43. package/lib/dlob/NodeList.js +139 -0
  44. package/lib/events/eventList.js +3 -0
  45. package/lib/events/eventSubscriber.d.ts +4 -2
  46. package/lib/events/eventSubscriber.js +16 -9
  47. package/lib/events/fetchLogs.d.ts +10 -1
  48. package/lib/events/fetchLogs.js +27 -7
  49. package/lib/events/pollingLogProvider.d.ts +2 -1
  50. package/lib/events/pollingLogProvider.js +6 -2
  51. package/lib/events/sort.js +8 -11
  52. package/lib/events/types.d.ts +8 -2
  53. package/lib/events/types.js +6 -0
  54. package/lib/examples/makeTradeExample.js +20 -8
  55. package/lib/factory/bigNum.d.ts +2 -0
  56. package/lib/factory/bigNum.js +48 -10
  57. package/lib/idl/clearing_house.json +4889 -1529
  58. package/lib/idl/{mock_usdc_faucet.json → token_faucet.json} +46 -23
  59. package/lib/index.d.ts +21 -6
  60. package/lib/index.js +25 -6
  61. package/lib/math/amm.d.ts +9 -6
  62. package/lib/math/amm.js +91 -66
  63. package/lib/math/auction.js +4 -1
  64. package/lib/math/conversion.js +1 -1
  65. package/lib/math/funding.d.ts +6 -6
  66. package/lib/math/funding.js +11 -10
  67. package/lib/math/insurance.d.ts +4 -0
  68. package/lib/math/insurance.js +27 -0
  69. package/lib/math/margin.d.ts +11 -0
  70. package/lib/math/margin.js +77 -0
  71. package/lib/math/market.d.ts +14 -9
  72. package/lib/math/market.js +72 -10
  73. package/lib/math/oracles.d.ts +4 -0
  74. package/lib/math/oracles.js +36 -8
  75. package/lib/math/orders.d.ts +6 -2
  76. package/lib/math/orders.js +78 -3
  77. package/lib/math/position.d.ts +21 -13
  78. package/lib/math/position.js +76 -36
  79. package/lib/math/repeg.js +14 -5
  80. package/lib/math/spotBalance.d.ts +22 -0
  81. package/lib/math/spotBalance.js +192 -0
  82. package/lib/math/spotMarket.d.ts +4 -0
  83. package/lib/math/spotMarket.js +8 -0
  84. package/lib/math/spotPosition.d.ts +6 -0
  85. package/lib/math/spotPosition.js +23 -0
  86. package/lib/math/trade.d.ts +10 -10
  87. package/lib/math/trade.js +32 -39
  88. package/lib/oracles/pythClient.js +1 -1
  89. package/lib/oracles/quoteAssetOracleClient.js +1 -1
  90. package/lib/oracles/switchboardClient.js +1 -1
  91. package/lib/orderParams.d.ts +14 -5
  92. package/lib/orderParams.js +12 -92
  93. package/lib/serum/serumSubscriber.d.ts +23 -0
  94. package/lib/serum/serumSubscriber.js +41 -0
  95. package/lib/serum/types.d.ts +11 -0
  96. package/lib/serum/types.js +2 -0
  97. package/lib/slot/SlotSubscriber.d.ts +7 -0
  98. package/lib/slot/SlotSubscriber.js +3 -0
  99. package/lib/{mockUSDCFaucet.d.ts → tokenFaucet.d.ts} +8 -5
  100. package/lib/{mockUSDCFaucet.js → tokenFaucet.js} +63 -51
  101. package/lib/tx/retryTxSender.d.ts +1 -1
  102. package/lib/tx/retryTxSender.js +13 -4
  103. package/lib/tx/types.d.ts +1 -1
  104. package/lib/tx/utils.js +1 -1
  105. package/lib/types.d.ts +566 -138
  106. package/lib/types.js +129 -5
  107. package/lib/userMap/userMap.d.ts +25 -0
  108. package/lib/userMap/userMap.js +73 -0
  109. package/lib/userMap/userStatsMap.d.ts +19 -0
  110. package/lib/userMap/userStatsMap.js +68 -0
  111. package/lib/util/computeUnits.js +1 -1
  112. package/lib/util/getTokenAddress.d.ts +2 -0
  113. package/lib/util/getTokenAddress.js +9 -0
  114. package/package.json +12 -6
  115. package/src/accounts/bulkUserStatsSubscription.ts +33 -0
  116. package/src/accounts/bulkUserSubscription.ts +0 -1
  117. package/src/accounts/fetch.ts +27 -2
  118. package/src/accounts/pollingClearingHouseAccountSubscriber.ts +46 -42
  119. package/src/accounts/pollingUserStatsAccountSubscriber.ts +172 -0
  120. package/src/accounts/types.ts +31 -11
  121. package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +64 -59
  122. package/src/accounts/webSocketUserStatsAccountSubsriber.ts +80 -0
  123. package/src/addresses/marketAddresses.ts +1 -2
  124. package/src/addresses/pda.ts +88 -14
  125. package/src/admin.ts +452 -147
  126. package/src/assert/assert.js +9 -0
  127. package/src/clearingHouse.ts +2066 -467
  128. package/src/clearingHouseConfig.ts +4 -3
  129. package/src/clearingHouseUser.ts +747 -294
  130. package/src/clearingHouseUserStats.ts +75 -0
  131. package/src/clearingHouseUserStatsConfig.ts +18 -0
  132. package/src/config.ts +30 -31
  133. package/src/constants/numericConstants.ts +48 -25
  134. package/src/constants/{markets.ts → perpMarkets.ts} +10 -10
  135. package/src/constants/spotMarkets.ts +73 -0
  136. package/src/dlob/DLOB.ts +953 -0
  137. package/src/dlob/DLOBNode.ts +167 -0
  138. package/src/dlob/NodeList.ts +189 -0
  139. package/src/events/eventList.js +77 -0
  140. package/src/events/eventList.ts +3 -0
  141. package/src/events/eventSubscriber.ts +20 -12
  142. package/src/events/fetchLogs.ts +35 -8
  143. package/src/events/pollingLogProvider.ts +10 -2
  144. package/src/events/sort.ts +11 -15
  145. package/src/events/types.ts +19 -1
  146. package/src/examples/makeTradeExample.js +157 -0
  147. package/src/examples/makeTradeExample.ts +32 -14
  148. package/src/factory/bigNum.ts +65 -13
  149. package/src/idl/clearing_house.json +4889 -1529
  150. package/src/idl/{mock_usdc_faucet.json → token_faucet.json} +46 -23
  151. package/src/index.ts +21 -6
  152. package/src/math/amm.ts +171 -88
  153. package/src/math/auction.ts +5 -1
  154. package/src/math/conversion.ts +2 -2
  155. package/src/math/funding.ts +20 -18
  156. package/src/math/insurance.ts +35 -0
  157. package/src/math/margin.ts +127 -0
  158. package/src/math/market.ts +145 -14
  159. package/src/math/oracles.ts +63 -9
  160. package/src/math/orders.ts +138 -4
  161. package/src/math/position.ts +119 -58
  162. package/src/math/repeg.ts +16 -6
  163. package/src/math/spotBalance.ts +319 -0
  164. package/src/math/spotMarket.ts +9 -0
  165. package/src/math/spotPosition.ts +47 -0
  166. package/src/math/trade.ts +53 -59
  167. package/src/oracles/pythClient.ts +2 -2
  168. package/src/oracles/quoteAssetOracleClient.ts +2 -2
  169. package/src/oracles/switchboardClient.ts +2 -2
  170. package/src/orderParams.ts +24 -137
  171. package/src/serum/serumSubscriber.ts +80 -0
  172. package/src/serum/types.ts +13 -0
  173. package/src/slot/SlotSubscriber.ts +11 -1
  174. package/src/token/index.js +38 -0
  175. package/src/{mockUSDCFaucet.ts → tokenFaucet.ts} +82 -70
  176. package/src/tx/retryTxSender.ts +16 -5
  177. package/src/tx/types.js +2 -0
  178. package/src/tx/types.ts +2 -1
  179. package/src/tx/utils.js +17 -0
  180. package/src/tx/utils.ts +1 -1
  181. package/src/types.ts +533 -140
  182. package/src/userMap/userMap.ts +100 -0
  183. package/src/userMap/userStatsMap.ts +110 -0
  184. package/src/util/computeUnits.js +21 -11
  185. package/src/util/computeUnits.ts +1 -1
  186. package/src/util/getTokenAddress.js +9 -0
  187. package/src/util/getTokenAddress.ts +18 -0
  188. package/src/util/promiseTimeout.js +14 -0
  189. package/src/util/tps.js +27 -0
  190. package/tests/bn/test.ts +24 -10
  191. package/tests/dlob/helpers.ts +397 -0
  192. package/tests/dlob/test.ts +3688 -0
  193. package/lib/constants/banks.d.ts +0 -16
  194. package/lib/constants/banks.js +0 -34
  195. package/lib/constants/markets.d.ts +0 -19
  196. package/lib/math/bankBalance.d.ts +0 -9
  197. package/lib/math/bankBalance.js +0 -75
  198. package/lib/math/state.d.ts +0 -8
  199. package/lib/math/state.js +0 -15
  200. package/lib/orders.d.ts +0 -8
  201. package/lib/orders.js +0 -134
  202. package/src/constants/banks.ts +0 -43
  203. package/src/math/bankBalance.ts +0 -112
  204. package/src/math/state.ts +0 -14
  205. package/src/math/utils.js +0 -27
  206. package/src/math/utils.js.map +0 -1
  207. package/src/orders.ts +0 -244
  208. package/src/util/computeUnits.js.map +0 -1
@@ -0,0 +1,100 @@
1
+ import {
2
+ ClearingHouseUser,
3
+ ClearingHouse,
4
+ UserAccount,
5
+ bulkPollingUserSubscribe,
6
+ OrderRecord,
7
+ ClearingHouseUserAccountSubscriptionConfig,
8
+ } from '..';
9
+ import { ProgramAccount } from '@project-serum/anchor';
10
+
11
+ import { PublicKey } from '@solana/web3.js';
12
+
13
+ export class UserMap {
14
+ private userMap = new Map<string, ClearingHouseUser>();
15
+ private clearingHouse: ClearingHouse;
16
+ private accountSubscription: ClearingHouseUserAccountSubscriptionConfig;
17
+
18
+ constructor(
19
+ clearingHouse: ClearingHouse,
20
+ accountSubscription: ClearingHouseUserAccountSubscriptionConfig
21
+ ) {
22
+ this.clearingHouse = clearingHouse;
23
+ this.accountSubscription = accountSubscription;
24
+ }
25
+
26
+ public async fetchAllUsers() {
27
+ const userArray: ClearingHouseUser[] = [];
28
+
29
+ const programUserAccounts =
30
+ (await this.clearingHouse.program.account.user.all()) as ProgramAccount<UserAccount>[];
31
+ for (const programUserAccount of programUserAccounts) {
32
+ if (this.userMap.has(programUserAccount.publicKey.toString())) {
33
+ continue;
34
+ }
35
+
36
+ const user = new ClearingHouseUser({
37
+ clearingHouse: this.clearingHouse,
38
+ userAccountPublicKey: programUserAccount.publicKey,
39
+ accountSubscription: this.accountSubscription,
40
+ });
41
+ userArray.push(user);
42
+ }
43
+
44
+ if (this.accountSubscription.type === 'polling') {
45
+ await bulkPollingUserSubscribe(
46
+ userArray,
47
+ this.accountSubscription.accountLoader
48
+ );
49
+ }
50
+
51
+ for (const user of userArray) {
52
+ this.userMap.set(user.getUserAccountPublicKey().toString(), user);
53
+ }
54
+ }
55
+
56
+ public async addPubkey(userAccountPublicKey: PublicKey) {
57
+ const user = new ClearingHouseUser({
58
+ clearingHouse: this.clearingHouse,
59
+ userAccountPublicKey,
60
+ accountSubscription: this.accountSubscription,
61
+ });
62
+ await user.subscribe();
63
+ this.userMap.set(userAccountPublicKey.toString(), user);
64
+ }
65
+
66
+ public has(key: string): boolean {
67
+ return this.userMap.has(key);
68
+ }
69
+
70
+ /**
71
+ * gets the ClearingHouseUser for a particular userAccountPublicKey, if no ClearingHouseUser exists, undefined is returned
72
+ * @param key userAccountPublicKey to get ClearngHouseUserFor
73
+ * @returns user ClearingHouseUser | undefined
74
+ */
75
+ public get(key: string): ClearingHouseUser | undefined {
76
+ return this.userMap.get(key);
77
+ }
78
+
79
+ /**
80
+ * gets the ClearingHouseUser for a particular userAccountPublicKey, if no ClearingHouseUser exists, new one is created
81
+ * @param key userAccountPublicKey to get ClearngHouseUserFor
82
+ * @returns ClearingHouseUser
83
+ */
84
+ public async mustGet(key: string): Promise<ClearingHouseUser> {
85
+ if (!this.has(key)) {
86
+ await this.addPubkey(new PublicKey(key));
87
+ }
88
+ const user = this.userMap.get(key);
89
+ await user.fetchAccounts();
90
+ return user;
91
+ }
92
+
93
+ public async updateWithOrderRecord(record: OrderRecord) {
94
+ await this.addPubkey(record.user);
95
+ }
96
+
97
+ public values(): IterableIterator<ClearingHouseUser> {
98
+ return this.userMap.values();
99
+ }
100
+ }
@@ -0,0 +1,110 @@
1
+ import {
2
+ ClearingHouse,
3
+ getUserStatsAccountPublicKey,
4
+ OrderRecord,
5
+ UserStatsAccount,
6
+ ClearingHouseUserStats,
7
+ ClearingHouseUserStatsAccountSubscriptionConfig,
8
+ bulkPollingUserStatsSubscribe,
9
+ } from '..';
10
+ import { ProgramAccount } from '@project-serum/anchor';
11
+ import { PublicKey } from '@solana/web3.js';
12
+
13
+ import { UserMap } from './userMap';
14
+
15
+ export class UserStatsMap {
16
+ /**
17
+ * map from authority pubkey to ClearingHouseUserStats
18
+ */
19
+ private userStatsMap = new Map<string, ClearingHouseUserStats>();
20
+ private clearingHouse: ClearingHouse;
21
+ private accountSubscription: ClearingHouseUserStatsAccountSubscriptionConfig;
22
+
23
+ constructor(
24
+ clearingHouse: ClearingHouse,
25
+ accountSubscription: ClearingHouseUserStatsAccountSubscriptionConfig
26
+ ) {
27
+ this.clearingHouse = clearingHouse;
28
+ this.accountSubscription = accountSubscription;
29
+ }
30
+
31
+ public async fetchAllUserStats() {
32
+ const userStatArray: ClearingHouseUserStats[] = [];
33
+
34
+ const programUserAccounts =
35
+ (await this.clearingHouse.program.account.userStats.all()) as ProgramAccount<UserStatsAccount>[];
36
+
37
+ for (const programUserAccount of programUserAccounts) {
38
+ const userStat: UserStatsAccount = programUserAccount.account;
39
+ if (this.userStatsMap.has(userStat.authority.toString())) {
40
+ continue;
41
+ }
42
+
43
+ const chUserStat = new ClearingHouseUserStats({
44
+ clearingHouse: this.clearingHouse,
45
+ userStatsAccountPublicKey: getUserStatsAccountPublicKey(
46
+ this.clearingHouse.program.programId,
47
+ userStat.authority
48
+ ),
49
+ accountSubscription: this.accountSubscription,
50
+ });
51
+ userStatArray.push(chUserStat);
52
+ }
53
+
54
+ if (this.accountSubscription.type === 'polling') {
55
+ await bulkPollingUserStatsSubscribe(
56
+ userStatArray,
57
+ this.accountSubscription.accountLoader
58
+ );
59
+ }
60
+
61
+ for (const userStat of userStatArray) {
62
+ this.userStatsMap.set(
63
+ userStat.getAccount().authority.toString(),
64
+ userStat
65
+ );
66
+ }
67
+ }
68
+
69
+ public async addUserStat(authority: PublicKey) {
70
+ const userStat = new ClearingHouseUserStats({
71
+ clearingHouse: this.clearingHouse,
72
+ userStatsAccountPublicKey: getUserStatsAccountPublicKey(
73
+ this.clearingHouse.program.programId,
74
+ authority
75
+ ),
76
+ accountSubscription: this.accountSubscription,
77
+ });
78
+ await userStat.subscribe();
79
+
80
+ this.userStatsMap.set(authority.toString(), userStat);
81
+ }
82
+
83
+ public async updateWithOrderRecord(record: OrderRecord, userMap: UserMap) {
84
+ if (!this.has(record.user.toString())) {
85
+ const takerUserAccount = await userMap.mustGet(record.user.toString());
86
+ this.addUserStat(takerUserAccount.getUserAccount().authority);
87
+ }
88
+ }
89
+
90
+ public has(authorityPublicKey: string): boolean {
91
+ return this.userStatsMap.has(authorityPublicKey);
92
+ }
93
+
94
+ public get(authorityPublicKey: string): ClearingHouseUserStats {
95
+ return this.userStatsMap.get(authorityPublicKey);
96
+ }
97
+
98
+ public async mustGet(
99
+ authorityPublicKey: string
100
+ ): Promise<ClearingHouseUserStats> {
101
+ if (!this.has(authorityPublicKey)) {
102
+ await this.addUserStat(new PublicKey(authorityPublicKey));
103
+ }
104
+ return this.get(authorityPublicKey);
105
+ }
106
+
107
+ public values(): IterableIterator<ClearingHouseUserStats> {
108
+ return this.userStatsMap.values();
109
+ }
110
+ }
@@ -1,17 +1,27 @@
1
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
+ };
2
11
  Object.defineProperty(exports, "__esModule", { value: true });
3
12
  exports.findComputeUnitConsumption = void 0;
4
- async function findComputeUnitConsumption(programId, connection, txSignature, commitment = 'confirmed') {
5
- const tx = await connection.getTransaction(txSignature, { commitment });
6
- const computeUnits = [];
7
- const regex = new RegExp(`Program ${programId.toString()} consumed ([0-9]{0,6}) of 200000 compute units`);
8
- tx.meta.logMessages.forEach((logMessage) => {
9
- const match = logMessage.match(regex);
10
- if (match && match[1]) {
11
- computeUnits.push(match[1]);
12
- }
13
+ function findComputeUnitConsumption(programId, connection, txSignature, commitment = 'confirmed') {
14
+ return __awaiter(this, void 0, void 0, function* () {
15
+ const tx = yield connection.getTransaction(txSignature, { commitment });
16
+ const computeUnits = [];
17
+ const regex = new RegExp(`Program ${programId.toString()} consumed ([0-9]{0,6}) of ([0-9]{0,7}) compute units`);
18
+ tx.meta.logMessages.forEach((logMessage) => {
19
+ const match = logMessage.match(regex);
20
+ if (match && match[1]) {
21
+ computeUnits.push(match[1]);
22
+ }
23
+ });
24
+ return computeUnits;
13
25
  });
14
- return computeUnits;
15
26
  }
16
27
  exports.findComputeUnitConsumption = findComputeUnitConsumption;
17
- //# sourceMappingURL=computeUnits.js.map
@@ -9,7 +9,7 @@ export async function findComputeUnitConsumption(
9
9
  const tx = await connection.getTransaction(txSignature, { commitment });
10
10
  const computeUnits = [];
11
11
  const regex = new RegExp(
12
- `Program ${programId.toString()} consumed ([0-9]{0,6}) of 200000 compute units`
12
+ `Program ${programId.toString()} consumed ([0-9]{0,6}) of ([0-9]{0,7}) compute units`
13
13
  );
14
14
  tx.meta.logMessages.forEach((logMessage) => {
15
15
  const match = logMessage.match(regex);
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getTokenAddress = void 0;
4
+ const spl_token_1 = require("@solana/spl-token");
5
+ const web3_js_1 = require("@solana/web3.js");
6
+ const getTokenAddress = (mintAddress, userPubKey) => {
7
+ return spl_token_1.Token.getAssociatedTokenAddress(spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID, spl_token_1.TOKEN_PROGRAM_ID, new web3_js_1.PublicKey(mintAddress), new web3_js_1.PublicKey(userPubKey));
8
+ };
9
+ exports.getTokenAddress = getTokenAddress;
@@ -0,0 +1,18 @@
1
+ import {
2
+ Token,
3
+ ASSOCIATED_TOKEN_PROGRAM_ID,
4
+ TOKEN_PROGRAM_ID,
5
+ } from '@solana/spl-token';
6
+ import { PublicKey } from '@solana/web3.js';
7
+
8
+ export const getTokenAddress = (
9
+ mintAddress: string,
10
+ userPubKey: string
11
+ ): Promise<PublicKey> => {
12
+ return Token.getAssociatedTokenAddress(
13
+ ASSOCIATED_TOKEN_PROGRAM_ID,
14
+ TOKEN_PROGRAM_ID,
15
+ new PublicKey(mintAddress),
16
+ new PublicKey(userPubKey)
17
+ );
18
+ };
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.promiseTimeout = void 0;
4
+ function promiseTimeout(promise, timeoutMs) {
5
+ let timeoutId;
6
+ const timeoutPromise = new Promise((resolve) => {
7
+ timeoutId = setTimeout(() => resolve(null), timeoutMs);
8
+ });
9
+ return Promise.race([promise, timeoutPromise]).then((result) => {
10
+ clearTimeout(timeoutId);
11
+ return result;
12
+ });
13
+ }
14
+ exports.promiseTimeout = promiseTimeout;
@@ -0,0 +1,27 @@
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.estimateTps = void 0;
13
+ function estimateTps(programId, connection, failed) {
14
+ return __awaiter(this, void 0, void 0, function* () {
15
+ let signatures = yield connection.getSignaturesForAddress(programId, undefined, 'finalized');
16
+ if (failed) {
17
+ signatures = signatures.filter((signature) => signature.err);
18
+ }
19
+ const numberOfSignatures = signatures.length;
20
+ if (numberOfSignatures === 0) {
21
+ return 0;
22
+ }
23
+ return (numberOfSignatures /
24
+ (signatures[0].blockTime - signatures[numberOfSignatures - 1].blockTime));
25
+ });
26
+ }
27
+ exports.estimateTps = estimateTps;
package/tests/bn/test.ts CHANGED
@@ -70,14 +70,15 @@ describe('BigNum Tests', () => {
70
70
 
71
71
  expect(val.print()).to.equal('1234.56789');
72
72
 
73
- expect(val.toFixed(3)).to.equal('1234.567');
74
-
73
+ expect(val.toNum().toFixed(3)).to.equal('1234.568');
75
74
  expect(val.toPrecision(1)).to.equal('1e3');
76
75
  expect(val.toPrecision(3)).to.equal('123e1');
77
76
  expect(val.toPrecision(4)).to.equal('1234');
78
77
  expect(val.toPrecision(5)).to.equal('1234.5');
79
78
  expect(val.toPrecision(11)).to.equal('1234.5678900');
80
79
 
80
+ expect(BigNum.from('1234').toPrecision(5)).to.equal('1234.0');
81
+
81
82
  // Case 2
82
83
  const val2 = BigNum.from(1, 5);
83
84
 
@@ -100,9 +101,9 @@ describe('BigNum Tests', () => {
100
101
  .shift(AMM_RESERVE_PRECISION_EXP)
101
102
  .div(BigNum.from(entryPriceNum * 10 ** 8));
102
103
 
103
- expect(val4.toString()).to.equal('250000000000');
104
- expect(val4.print()).to.equal('0.0250000000000');
105
- expect(val4.toFixed(3)).to.equal('0.025');
104
+ expect(val4.toString()).to.equal('25000000');
105
+ expect(val4.print()).to.equal('0.025000000');
106
+ expect(val4.toNum().toFixed(3)).to.equal('0.025');
106
107
  expect(val4.toPrecision(4)).to.equal('0.025');
107
108
 
108
109
  // Case 5
@@ -119,12 +120,22 @@ describe('BigNum Tests', () => {
119
120
  expect(BigNum.fromPrint('1').toMillified(5)).to.equal('1.0000');
120
121
  expect(BigNum.fromPrint('12').toMillified(5)).to.equal('12.000');
121
122
  expect(BigNum.fromPrint('123').toMillified(5)).to.equal('123.00');
122
- expect(BigNum.fromPrint('1234').toMillified(5)).to.equal('1.2340K');
123
- expect(BigNum.fromPrint('12345').toMillified(5)).to.equal('12.345K');
123
+ expect(BigNum.fromPrint('1234').toMillified(5)).to.equal('1234.0');
124
+ expect(BigNum.fromPrint('12345').toMillified(5)).to.equal('12345');
124
125
  expect(BigNum.fromPrint('123456').toMillified(5)).to.equal('123.45K');
125
126
  expect(BigNum.fromPrint('1234567').toMillified(5)).to.equal('1.2345M');
126
127
  expect(BigNum.fromPrint('12345678').toMillified(5)).to.equal('12.345M');
127
128
  expect(BigNum.fromPrint('123456789').toMillified(5)).to.equal('123.45M');
129
+
130
+ expect(BigNum.from(-95, 2).print()).to.equal('-0.95');
131
+
132
+ // Case 6 strange numbers
133
+ expect(BigNum.from('-100', 2).print()).to.equal('-1.00');
134
+ expect(BigNum.from('-8402189', 13).print()).to.equal('-0.0000008402189');
135
+ expect(BigNum.from('-10000000000000', 13).print()).to.equal(
136
+ '-1.0000000000000'
137
+ );
138
+ expect(BigNum.from('-100', 6).print()).to.equal('-0.000100');
128
139
  });
129
140
 
130
141
  it('can initialise from string values correctly', () => {
@@ -133,8 +144,8 @@ describe('BigNum Tests', () => {
133
144
  const baseAmountVal1 = '14.33';
134
145
  const val1 = BigNum.fromPrint(baseAmountVal1, BASE_PRECISION_EXP);
135
146
 
136
- expect(val1.toString()).to.equal('143300000000000');
137
- expect(val1.print()).to.equal('14.3300000000000');
147
+ expect(val1.toString()).to.equal('14330000000');
148
+ expect(val1.print()).to.equal('14.330000000');
138
149
 
139
150
  const baseAmountVal2 = '34.1';
140
151
  const val2 = BigNum.fromPrint(baseAmountVal2, BASE_PRECISION_EXP);
@@ -216,7 +227,7 @@ describe('BigNum Tests', () => {
216
227
  .shift(AMM_RESERVE_PRECISION_EXP)
217
228
  .div(BigNum.from(entryPriceNum * 10 ** 8));
218
229
 
219
- expect(val.toString()).to.equal('250000000000');
230
+ expect(val.toString()).to.equal('25000000');
220
231
  expect(val.printShort()).to.equal('0.025');
221
232
 
222
233
  const val2 = BigNum.from(10000, 4);
@@ -251,5 +262,8 @@ describe('BigNum Tests', () => {
251
262
 
252
263
  const val9 = BigNum.from('1000000000123', 6);
253
264
  expect(val9.prettyPrint()).to.equal('1,000,000.000123');
265
+
266
+ const val10 = BigNum.from('100000000000', 6);
267
+ expect(val10.prettyPrint(true)).to.equal('100,000');
254
268
  });
255
269
  });