Skip to content

Commit

Permalink
fix(txts): Better getComputeUnits (#125)
Browse files Browse the repository at this point in the history
* Add Signers Param to getComputeUnits

* Update README.md

* Remove replaceRecentBlockhash Conflict
  • Loading branch information
0xIchigo authored Aug 15, 2024
1 parent 048eb6c commit 061be6b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -717,10 +717,10 @@ const { smartTransaction: transaction, lastValidBlockHeight } = await helius.rpc
```

### getComputeUnits()
This method simulates a transaction to get the total compute units consumed. It takes in an array of instructions, a fee payer, and an array of lookup tables. It returns the compute units consumed, or null if unsuccessful:
This method simulates a transaction to get the total compute units consumed. It takes in an array of instructions, a fee payer, an array of lookup tables, and an array of signers. It returns the compute units consumed, or null if unsuccessful:

```ts
const units = helius.rpc.getComputeUnits(instructions, payerKey, []);
const units = helius.rpc.getComputeUnits(instructions, payerKey, [], []);
```

### pollTransactionConfirmation()
Expand Down
14 changes: 10 additions & 4 deletions src/RpcClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -456,12 +456,14 @@ export class RpcClient {
* @param {TransactionInstruction[]} instructions - The transaction instructions
* @param {PublicKey} payer - The public key of the payer
* @param {AddressLookupTableAccount[]} lookupTables - The address lookup tables
* @param {Signer[]} signers - Optional signers for the transaction
* @returns {Promise<number | null>} - The compute units consumed, or null if unsuccessful
*/
async getComputeUnits(
instructions: TransactionInstruction[],
payer: PublicKey,
lookupTables: AddressLookupTableAccount[]
lookupTables: AddressLookupTableAccount[],
signers?: Signer[]
): Promise<number | null> {
const testInstructions = [
ComputeBudgetProgram.setComputeUnitLimit({ units: 1_400_000 }),
Expand All @@ -476,11 +478,14 @@ export class RpcClient {
}).compileToV0Message(lookupTables)
);

if (signers) {
testTransaction.sign(signers);
}

const rpcResponse = await this.connection.simulateTransaction(
testTransaction,
{
replaceRecentBlockhash: true,
sigVerify: false,
sigVerify: !!signers,
}
);

Expand Down Expand Up @@ -636,7 +641,8 @@ export class RpcClient {
const units = await this.getComputeUnits(
instructions,
payerKey,
isVersioned ? lookupTables : []
isVersioned ? lookupTables : [],
signers
);

if (!units) {
Expand Down

0 comments on commit 061be6b

Please sign in to comment.