diff --git a/packages/marginfi-client-v2/src/services/transaction/helpers/tx-formatting.ts b/packages/marginfi-client-v2/src/services/transaction/helpers/tx-formatting.ts index cf3181291..eb4f949b3 100644 --- a/packages/marginfi-client-v2/src/services/transaction/helpers/tx-formatting.ts +++ b/packages/marginfi-client-v2/src/services/transaction/helpers/tx-formatting.ts @@ -17,7 +17,6 @@ import { getComputeBudgetUnits, microLamportsToUi, uiToMicroLamports, - MARGINFI_PROGRAM, addTransactionMetadata, TransactionArenaKeyMap, } from "@mrgnlabs/mrgn-common"; @@ -52,17 +51,33 @@ function getFlashloanIndex(transactions: SolanaTransaction[]): number | null { return null; } -export function formatTransactions( - transactionsArg: SolanaTransaction[], - broadcastType: TransactionBroadcastType, +type FeeSettings = { priorityFeeMicro: number, bundleTipUi: number, feePayer: PublicKey, - blockhash: string, maxCapUi?: number, - addArenaTxTag?: boolean +} + +/** + * Formats a list of Solana transactions into versioned transactions, applying + * necessary settings such as fees and blockhash. Optionally adds transaction tags. + * + * @param {SolanaTransaction[]} transactionsArg - The array of Solana transactions to format. + * @param {TransactionBroadcastType} broadcastType - The type of transaction broadcast to use. + * @param {string} blockhash - The recent blockhash to set for the transactions. + * @param {FeeSettings} feeSettings - The settings for transaction fees, including priority fee and bundle tip. + * @param {boolean} [addTransactionTags] - Optional flag to add transaction tags. + * @returns {VersionedTransaction[]} - The array of formatted versioned transactions. + */ +export function formatTransactions( + transactionsArg: SolanaTransaction[], + broadcastType: TransactionBroadcastType, + blockhash: string, + feeSettings: FeeSettings, + addTransactionTags?: boolean ): VersionedTransaction[] { let formattedTransactions: VersionedTransaction[] = []; + const { priorityFeeMicro, bundleTipUi, feePayer, maxCapUi } = feeSettings; const flashloanIndex = getFlashloanIndex(transactionsArg); transactionsArg.forEach((tx) => { @@ -72,7 +87,7 @@ export function formatTransactions( } }); - let transactions = addArenaTxTag ? addArenaTxTags(transactionsArg) : transactionsArg; + let transactions = addTransactionTags ? addTransactionTxTags(transactionsArg) : transactionsArg; const txSizes: number[] = transactions.map((tx) => getTxSize(tx)); const dummyPriorityFeeIx = makePriorityFeeMicroIx(1); @@ -169,14 +184,12 @@ export function formatTransactions( return formattedTransactions; } -function addArenaTxTags(transactions: SolanaTransaction[]): SolanaTransaction[] { +function addTransactionTxTags(transactions: SolanaTransaction[]): SolanaTransaction[] { const txWithTags: SolanaTransaction[] = []; - for (const [index, tx] of transactions.entries()) { + for (const [_, tx] of transactions.entries()) { let solanaTx: SolanaTransaction = tx; const arenaKey = TransactionArenaKeyMap[tx.type]; - console.log("arenaKey", arenaKey); - console.log("tx.type", tx.type); if (arenaKey) { if (isV0Tx(solanaTx)) { diff --git a/packages/marginfi-client-v2/src/services/transaction/transaction.service.ts b/packages/marginfi-client-v2/src/services/transaction/transaction.service.ts index 36628d7e7..215989756 100644 --- a/packages/marginfi-client-v2/src/services/transaction/transaction.service.ts +++ b/packages/marginfi-client-v2/src/services/transaction/transaction.service.ts @@ -242,11 +242,14 @@ export async function processTransactions({ versionedTransactions = formatTransactions( updatedTransactions, broadcastType, - processOpts.priorityFeeMicro ?? 0, - processOpts.bundleTipUi ?? 0, - wallet.publicKey, blockhash, - maxCapUi, + { + priorityFeeMicro: processOpts.priorityFeeMicro ?? 0, + bundleTipUi: processOpts.bundleTipUi ?? 0, + feePayer: wallet.publicKey, + + maxCapUi, + }, processOpts.addArenaTxTag ); diff --git a/packages/mrgn-common/src/modules/transactions/transaction.types.ts b/packages/mrgn-common/src/modules/transactions/transaction.types.ts index 2c117d72f..20cc4f9f5 100644 --- a/packages/mrgn-common/src/modules/transactions/transaction.types.ts +++ b/packages/mrgn-common/src/modules/transactions/transaction.types.ts @@ -119,18 +119,15 @@ export const TransactionArenaKeyMap: Partial> // Add more mappings if needed }; -export type ExtendedTransaction = Transaction & { +export type ExtendedTransactionProperties = { type: TransactionType; signers?: Array; addressLookupTables?: AddressLookupTableAccount[]; unitsConsumed?: number; }; -export type ExtendedV0Transaction = VersionedTransaction & { - type: TransactionType; - signers?: Array; - addressLookupTables?: AddressLookupTableAccount[]; - unitsConsumed?: number; -}; +export type ExtendedTransaction = Transaction & ExtendedTransactionProperties + +export type ExtendedV0Transaction = VersionedTransaction & ExtendedTransactionProperties export type SolanaTransaction = ExtendedTransaction | ExtendedV0Transaction; diff --git a/packages/mrgn-common/src/modules/transactions/transaction.utils.ts b/packages/mrgn-common/src/modules/transactions/transaction.utils.ts index de5a1e57a..fba5ebceb 100644 --- a/packages/mrgn-common/src/modules/transactions/transaction.utils.ts +++ b/packages/mrgn-common/src/modules/transactions/transaction.utils.ts @@ -11,7 +11,7 @@ import { TransactionMessage, VersionedTransaction, } from "@solana/web3.js"; -import { SolanaTransaction, TransactionType } from "./transaction.types"; +import { ExtendedTransactionProperties, SolanaTransaction, TransactionType } from "./transaction.types"; /** * Determines if a given transaction is a VersionedTransaction. @@ -246,22 +246,14 @@ export function replaceV0TxBlockhash(transaction: VersionedTransaction, blockhas * @param options - An object containing optional metadata: * - signers: An array of Signer objects that are associated with the transaction. * - addressLookupTables: An array of AddressLookupTableAccount objects for address resolution. + * - unitsConsumed: A number representing the compute units consumed by the transaction. + * - type: The type of the transaction, as defined by TransactionType. * @returns A SolanaTransaction object that includes the original transaction and the additional metadata. */ export function addTransactionMetadata( transaction: T, - options: { - signers?: Array; - addressLookupTables?: AddressLookupTableAccount[]; - type: TransactionType; - unitsConsumed?: number; - } -): T & { - signers?: Array; - addressLookupTables?: AddressLookupTableAccount[]; - type: TransactionType; - unitsConsumed?: number; -} { + options: ExtendedTransactionProperties +): T & ExtendedTransactionProperties { return Object.assign(transaction, options); }