Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Gauntlet support for custom unit price and limit (#425)
* add support for custom unit price and limit * fix signers
- Loading branch information
Showing
3 changed files
with
43 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,17 @@ | ||
import { Transaction, TransactionInstruction, TransactionCtorFields } from '@solana/web3.js' | ||
import { Transaction, TransactionInstruction, TransactionBlockhashCtor, ComputeBudgetProgram } from '@solana/web3.js' | ||
|
||
export const makeTx = (rawTx: TransactionInstruction[], opts?: TransactionCtorFields): Transaction => { | ||
return rawTx.reduce((tx, instruction) => tx.add(instruction), new Transaction(opts)) | ||
export const makeTx = ( | ||
rawTx: TransactionInstruction[], | ||
opts?: TransactionBlockhashCtor, | ||
overrides: { price?: number; units?: number } = {}, | ||
): Transaction => { | ||
if (overrides.price && overrides.units) | ||
throw new Error('Cannot set limit for units and price in the same transaction') | ||
|
||
let computeIx: TransactionInstruction | ||
if (overrides.price) computeIx = ComputeBudgetProgram.setComputeUnitPrice({ microLamports: overrides.price }) | ||
if (overrides.units) computeIx = ComputeBudgetProgram.setComputeUnitLimit({ units: overrides.units }) | ||
const initialTx = computeIx ? new Transaction(opts).add(computeIx) : new Transaction(opts) | ||
|
||
return rawTx.reduce((tx, instruction) => tx.add(instruction), initialTx) | ||
} |