@drift-labs/sdk 0.2.0-master.3 → 0.2.0-master.30
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.
- package/README.md +13 -13
- package/lib/accounts/bulkUserStatsSubscription.d.ts +7 -0
- package/lib/accounts/bulkUserStatsSubscription.js +21 -0
- package/lib/accounts/bulkUserSubscription.js +0 -1
- package/lib/accounts/fetch.d.ts +2 -1
- package/lib/accounts/fetch.js +9 -1
- package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +16 -16
- package/lib/accounts/pollingClearingHouseAccountSubscriber.js +31 -28
- package/lib/accounts/pollingUserStatsAccountSubscriber.d.ts +27 -0
- package/lib/accounts/pollingUserStatsAccountSubscriber.js +113 -0
- package/lib/accounts/types.d.ts +22 -11
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +17 -17
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +37 -35
- package/lib/accounts/webSocketUserStatsAccountSubsriber.d.ts +20 -0
- package/lib/accounts/webSocketUserStatsAccountSubsriber.js +47 -0
- package/lib/addresses/marketAddresses.d.ts +1 -3
- package/lib/addresses/pda.d.ts +10 -4
- package/lib/addresses/pda.js +51 -14
- package/lib/admin.d.ts +34 -22
- package/lib/admin.js +182 -73
- package/lib/clearingHouse.d.ts +120 -42
- package/lib/clearingHouse.js +1505 -254
- package/lib/clearingHouseConfig.d.ts +4 -4
- package/lib/clearingHouseUser.d.ts +50 -38
- package/lib/clearingHouseUser.js +410 -190
- package/lib/clearingHouseUserStats.d.ts +18 -0
- package/lib/clearingHouseUserStats.js +49 -0
- package/lib/clearingHouseUserStatsConfig.d.ts +14 -0
- package/lib/clearingHouseUserStatsConfig.js +2 -0
- package/lib/config.d.ts +7 -9
- package/lib/config.js +21 -21
- package/lib/constants/numericConstants.d.ts +18 -12
- package/lib/constants/numericConstants.js +28 -21
- package/lib/constants/perpMarkets.d.ts +18 -0
- package/lib/constants/{markets.js → perpMarkets.js} +7 -7
- package/lib/constants/spotMarkets.d.ts +19 -0
- package/lib/constants/spotMarkets.js +53 -0
- package/lib/dlob/DLOB.d.ts +73 -0
- package/lib/dlob/DLOB.js +553 -0
- package/lib/dlob/DLOBNode.d.ts +52 -0
- package/lib/dlob/DLOBNode.js +82 -0
- package/lib/dlob/NodeList.d.ts +26 -0
- package/lib/dlob/NodeList.js +138 -0
- package/lib/events/eventList.js +3 -0
- package/lib/events/eventSubscriber.d.ts +4 -2
- package/lib/events/eventSubscriber.js +16 -9
- package/lib/events/fetchLogs.d.ts +10 -1
- package/lib/events/fetchLogs.js +27 -7
- package/lib/events/pollingLogProvider.d.ts +2 -1
- package/lib/events/pollingLogProvider.js +6 -2
- package/lib/events/sort.js +8 -11
- package/lib/events/types.d.ts +7 -2
- package/lib/events/types.js +5 -0
- package/lib/examples/makeTradeExample.js +20 -8
- package/lib/factory/bigNum.d.ts +1 -0
- package/lib/factory/bigNum.js +34 -10
- package/lib/idl/clearing_house.json +4313 -1433
- package/lib/idl/{mock_usdc_faucet.json → token_faucet.json} +46 -23
- package/lib/index.d.ts +21 -6
- package/lib/index.js +25 -6
- package/lib/math/amm.d.ts +8 -5
- package/lib/math/amm.js +68 -46
- package/lib/math/auction.js +4 -1
- package/lib/math/conversion.js +1 -1
- package/lib/math/funding.d.ts +6 -6
- package/lib/math/funding.js +11 -10
- package/lib/math/insurance.d.ts +4 -0
- package/lib/math/insurance.js +27 -0
- package/lib/math/margin.d.ts +11 -0
- package/lib/math/margin.js +77 -0
- package/lib/math/market.d.ts +14 -9
- package/lib/math/market.js +69 -7
- package/lib/math/oracles.d.ts +4 -0
- package/lib/math/oracles.js +36 -8
- package/lib/math/orders.d.ts +6 -2
- package/lib/math/orders.js +78 -3
- package/lib/math/position.d.ts +21 -13
- package/lib/math/position.js +76 -36
- package/lib/math/repeg.js +14 -5
- package/lib/math/spotBalance.d.ts +22 -0
- package/lib/math/spotBalance.js +193 -0
- package/lib/math/spotMarket.d.ts +4 -0
- package/lib/math/spotMarket.js +8 -0
- package/lib/math/spotPosition.d.ts +6 -0
- package/lib/math/spotPosition.js +23 -0
- package/lib/math/trade.d.ts +10 -10
- package/lib/math/trade.js +22 -29
- package/lib/oracles/pythClient.js +1 -1
- package/lib/oracles/quoteAssetOracleClient.js +1 -1
- package/lib/oracles/switchboardClient.js +1 -1
- package/lib/orderParams.d.ts +14 -5
- package/lib/orderParams.js +12 -92
- package/lib/serum/serumSubscriber.d.ts +23 -0
- package/lib/serum/serumSubscriber.js +41 -0
- package/lib/serum/types.d.ts +11 -0
- package/lib/serum/types.js +2 -0
- package/lib/slot/SlotSubscriber.d.ts +7 -0
- package/lib/slot/SlotSubscriber.js +3 -0
- package/lib/{mockUSDCFaucet.d.ts → tokenFaucet.d.ts} +8 -5
- package/lib/{mockUSDCFaucet.js → tokenFaucet.js} +63 -51
- package/lib/tx/retryTxSender.d.ts +1 -1
- package/lib/tx/retryTxSender.js +13 -4
- package/lib/tx/types.d.ts +1 -1
- package/lib/tx/utils.js +1 -1
- package/lib/types.d.ts +474 -123
- package/lib/types.js +99 -5
- package/lib/userMap/userMap.d.ts +25 -0
- package/lib/userMap/userMap.js +73 -0
- package/lib/userMap/userStatsMap.d.ts +19 -0
- package/lib/userMap/userStatsMap.js +68 -0
- package/lib/util/computeUnits.js +1 -1
- package/lib/util/getTokenAddress.d.ts +2 -0
- package/lib/util/getTokenAddress.js +9 -0
- package/package.json +9 -5
- package/src/accounts/bulkUserStatsSubscription.ts +33 -0
- package/src/accounts/bulkUserSubscription.ts +0 -1
- package/src/accounts/fetch.ts +27 -2
- package/src/accounts/pollingClearingHouseAccountSubscriber.ts +46 -42
- package/src/accounts/pollingUserStatsAccountSubscriber.ts +172 -0
- package/src/accounts/types.ts +31 -11
- package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +64 -59
- package/src/accounts/webSocketUserStatsAccountSubsriber.ts +80 -0
- package/src/addresses/marketAddresses.ts +1 -2
- package/src/addresses/pda.ts +88 -14
- package/src/admin.ts +333 -128
- package/src/assert/assert.js +9 -0
- package/src/clearingHouse.ts +2464 -458
- package/src/clearingHouseConfig.ts +4 -3
- package/src/clearingHouseUser.ts +747 -291
- package/src/clearingHouseUserStats.ts +75 -0
- package/src/clearingHouseUserStatsConfig.ts +18 -0
- package/src/config.ts +30 -31
- package/src/constants/numericConstants.ts +41 -25
- package/src/constants/{markets.ts → perpMarkets.ts} +10 -10
- package/src/constants/spotMarkets.ts +72 -0
- package/src/dlob/DLOB.ts +868 -0
- package/src/dlob/DLOBNode.ts +162 -0
- package/src/dlob/NodeList.ts +185 -0
- package/src/events/eventList.js +77 -0
- package/src/events/eventList.ts +3 -0
- package/src/events/eventSubscriber.ts +20 -12
- package/src/events/fetchLogs.ts +35 -8
- package/src/events/pollingLogProvider.ts +10 -2
- package/src/events/sort.ts +11 -15
- package/src/events/types.ts +16 -1
- package/src/examples/makeTradeExample.js +157 -0
- package/src/examples/makeTradeExample.ts +32 -14
- package/src/factory/bigNum.ts +42 -13
- package/src/idl/clearing_house.json +4313 -1433
- package/src/idl/{mock_usdc_faucet.json → token_faucet.json} +46 -23
- package/src/index.ts +21 -6
- package/src/math/amm.ts +136 -66
- package/src/math/auction.ts +5 -1
- package/src/math/conversion.ts +2 -2
- package/src/math/funding.ts +20 -18
- package/src/math/insurance.ts +35 -0
- package/src/math/margin.ts +127 -0
- package/src/math/market.ts +138 -12
- package/src/math/oracles.ts +63 -9
- package/src/math/orders.ts +138 -4
- package/src/math/position.ts +119 -58
- package/src/math/repeg.ts +16 -6
- package/src/math/spotBalance.ts +316 -0
- package/src/math/spotMarket.ts +9 -0
- package/src/math/spotPosition.ts +47 -0
- package/src/math/trade.ts +43 -49
- package/src/oracles/pythClient.ts +2 -2
- package/src/oracles/quoteAssetOracleClient.ts +2 -2
- package/src/oracles/switchboardClient.ts +2 -2
- package/src/orderParams.ts +24 -137
- package/src/serum/serumSubscriber.ts +80 -0
- package/src/serum/types.ts +13 -0
- package/src/slot/SlotSubscriber.ts +11 -1
- package/src/token/index.js +38 -0
- package/src/{mockUSDCFaucet.ts → tokenFaucet.ts} +82 -70
- package/src/tx/retryTxSender.ts +16 -5
- package/src/tx/types.js +2 -0
- package/src/tx/types.ts +2 -1
- package/src/tx/utils.js +17 -0
- package/src/tx/utils.ts +1 -1
- package/src/types.ts +477 -125
- package/src/userMap/userMap.ts +100 -0
- package/src/userMap/userStatsMap.ts +110 -0
- package/src/util/computeUnits.js +21 -11
- package/src/util/computeUnits.ts +1 -1
- package/src/util/getTokenAddress.js +9 -0
- package/src/util/getTokenAddress.ts +18 -0
- package/src/util/promiseTimeout.js +14 -0
- package/src/util/tps.js +27 -0
- package/tests/bn/test.ts +12 -3
- package/tests/dlob/helpers.ts +374 -0
- package/tests/dlob/test.ts +2865 -0
- package/lib/constants/banks.d.ts +0 -16
- package/lib/constants/banks.js +0 -34
- package/lib/constants/markets.d.ts +0 -19
- package/lib/math/bankBalance.d.ts +0 -9
- package/lib/math/bankBalance.js +0 -75
- package/lib/math/state.d.ts +0 -8
- package/lib/math/state.js +0 -15
- package/lib/orders.d.ts +0 -8
- package/lib/orders.js +0 -134
- package/src/constants/banks.ts +0 -43
- package/src/math/bankBalance.ts +0 -112
- package/src/math/state.ts +0 -14
- package/src/math/utils.js +0 -27
- package/src/math/utils.js.map +0 -1
- package/src/orders.ts +0 -244
- 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
|
+
}
|
package/src/util/computeUnits.js
CHANGED
|
@@ -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
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
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
|
package/src/util/computeUnits.ts
CHANGED
|
@@ -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
|
|
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;
|
package/src/util/tps.js
ADDED
|
@@ -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,8 +70,7 @@ describe('BigNum Tests', () => {
|
|
|
70
70
|
|
|
71
71
|
expect(val.print()).to.equal('1234.56789');
|
|
72
72
|
|
|
73
|
-
expect(val.toFixed(3)).to.equal('1234.
|
|
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');
|
|
@@ -102,7 +101,7 @@ describe('BigNum Tests', () => {
|
|
|
102
101
|
|
|
103
102
|
expect(val4.toString()).to.equal('250000000000');
|
|
104
103
|
expect(val4.print()).to.equal('0.0250000000000');
|
|
105
|
-
expect(val4.toFixed(3)).to.equal('0.025');
|
|
104
|
+
expect(val4.toNum().toFixed(3)).to.equal('0.025');
|
|
106
105
|
expect(val4.toPrecision(4)).to.equal('0.025');
|
|
107
106
|
|
|
108
107
|
// Case 5
|
|
@@ -125,6 +124,16 @@ describe('BigNum Tests', () => {
|
|
|
125
124
|
expect(BigNum.fromPrint('1234567').toMillified(5)).to.equal('1.2345M');
|
|
126
125
|
expect(BigNum.fromPrint('12345678').toMillified(5)).to.equal('12.345M');
|
|
127
126
|
expect(BigNum.fromPrint('123456789').toMillified(5)).to.equal('123.45M');
|
|
127
|
+
|
|
128
|
+
expect(BigNum.from(-95, 2).print()).to.equal('-0.95');
|
|
129
|
+
|
|
130
|
+
// Case 6 strange numbers
|
|
131
|
+
expect(BigNum.from('-100', 2).print()).to.equal('-1.00');
|
|
132
|
+
expect(BigNum.from('-8402189', 13).print()).to.equal('-0.0000008402189');
|
|
133
|
+
expect(BigNum.from('-10000000000000', 13).print()).to.equal(
|
|
134
|
+
'-1.0000000000000'
|
|
135
|
+
);
|
|
136
|
+
expect(BigNum.from('-100', 6).print()).to.equal('-0.000100');
|
|
128
137
|
});
|
|
129
138
|
|
|
130
139
|
it('can initialise from string values correctly', () => {
|