-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathtransaction.ts
112 lines (99 loc) · 3.42 KB
/
transaction.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import {
AddressLookupTableAccount,
Commitment,
ComputeBudgetProgram,
Connection,
PublicKey,
TransactionInstruction,
TransactionMessage,
VersionedTransaction,
Signer, Transaction
} from "@solana/web3.js";
import { getErrorFromRPCResponse } from "./logs";
export const confirmTransaction = async (
connection: Connection,
signature: string,
commitment: Commitment = "finalized",
): Promise<string> => {
const block = await connection.getLatestBlockhash();
const rpcResponse = await connection.confirmTransaction(
{
signature,
...block,
},
commitment,
);
getErrorFromRPCResponse(rpcResponse);
return signature;
};
// Was getSimulationUnits
// Credit https://twitter.com/stegabob, originally from
// https://x.com/stegaBOB/status/1766662289392889920
export const getSimulationComputeUnits = async (
connection: Connection,
instructions: Array<TransactionInstruction>,
payer: PublicKey,
lookupTables: Array<AddressLookupTableAccount> | [],
): Promise<number | null> => {
const testInstructions = [
// Set an arbitrarily high number in simulation
// so we can be sure the transaction will succeed
// and get the real compute units used
ComputeBudgetProgram.setComputeUnitLimit({ units: 1_400_000 }),
...instructions,
];
const testTransaction = new VersionedTransaction(
new TransactionMessage({
instructions: testInstructions,
payerKey: payer,
// RecentBlockhash can by any public key during simulation
// since 'replaceRecentBlockhash' is set to 'true' below
recentBlockhash: PublicKey.default.toString(),
}).compileToV0Message(lookupTables),
);
const rpcResponse = await connection.simulateTransaction(testTransaction, {
replaceRecentBlockhash: true,
sigVerify: false,
});
getErrorFromRPCResponse(rpcResponse);
return rpcResponse.value.unitsConsumed || null;
};
export const getLegacySimulationComputeUnits = async (
connection: Connection,
instructions: Array<TransactionInstruction>,
signers: Array<PublicKey> | Array<Signer>,
feePayer?: PublicKey | Signer
): Promise<number | null> => {
const testInstructions = [
// Set an arbitrarily high number in simulation
// so we can be sure the transaction will succeed
// and get the real compute units used
ComputeBudgetProgram.setComputeUnitLimit({units: 1_400_000}),
...instructions,
];
signers = (signers as Signer[]).map(signer => signer?.publicKey ? signer.publicKey : signer) as Array<PublicKey>
feePayer = ((feePayer as Signer)?.publicKey ? (feePayer as Signer).publicKey : feePayer || signers[0]) as PublicKey
const transaction = new Transaction();
transaction.instructions = testInstructions;
transaction.feePayer = feePayer;
transaction.recentBlockhash = '11111111111111111111111111111111';
// deprecated:
// transaction.setSigners(...signers.map(signer => signer.publicKey))
transaction.signatures = signers.map(publicKey => ({
signature: null,
publicKey
}))
const args = [
transaction.serialize({ verifySignatures: false }).toString('base64'),
{
encoding: 'base64',
replaceRecentBlockhash: true
}
];
// deprecated:
// const rpcResponse = await connection.simulateTransaction(transaction, signers);
// @ts-ignore
const rpcResponse = (await connection._rpcRequest('simulateTransaction', args)).result
getErrorFromRPCResponse(rpcResponse);
return rpcResponse.value.unitsConsumed || null;
};