Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Legacy Transaction Simulation ComputeUnits #60

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 57 additions & 2 deletions src/lib/transaction.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
import { AddressLookupTableAccount, Commitment, ComputeBudgetProgram, Connection, PublicKey, TransactionInstruction, TransactionMessage, VersionedTransaction } from "@solana/web3.js";
import {
AddressLookupTableAccount,
Commitment,
ComputeBudgetProgram,
Connection,
PublicKey,
TransactionInstruction,
TransactionMessage,
VersionedTransaction,
Signer, Transaction
} from "@solana/web3.js";
import { getErrorFromRPCResponse } from "./logs";

export const confirmTransaction = async (
Expand Down Expand Up @@ -54,4 +64,49 @@ export const getSimulationComputeUnits = async (

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;
};