Skip to content

Commit

Permalink
Merge pull request #1072 from mrgnlabs/feat/sdk-tweaks
Browse files Browse the repository at this point in the history
chore: added jsdocs and improved naming
  • Loading branch information
k0beLeenders authored Feb 18, 2025
2 parents e7f380e + 06dfd1c commit 72c514e
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
getComputeBudgetUnits,
microLamportsToUi,
uiToMicroLamports,
MARGINFI_PROGRAM,
addTransactionMetadata,
TransactionArenaKeyMap,
} from "@mrgnlabs/mrgn-common";
Expand Down Expand Up @@ -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) => {
Expand All @@ -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);
Expand Down Expand Up @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,18 +119,15 @@ export const TransactionArenaKeyMap: Partial<Record<TransactionType, PublicKey>>
// Add more mappings if needed
};

export type ExtendedTransaction = Transaction & {
export type ExtendedTransactionProperties = {
type: TransactionType;
signers?: Array<Signer>;
addressLookupTables?: AddressLookupTableAccount[];
unitsConsumed?: number;
};

export type ExtendedV0Transaction = VersionedTransaction & {
type: TransactionType;
signers?: Array<Signer>;
addressLookupTables?: AddressLookupTableAccount[];
unitsConsumed?: number;
};
export type ExtendedTransaction = Transaction & ExtendedTransactionProperties

export type ExtendedV0Transaction = VersionedTransaction & ExtendedTransactionProperties

export type SolanaTransaction = ExtendedTransaction | ExtendedV0Transaction;
18 changes: 5 additions & 13 deletions packages/mrgn-common/src/modules/transactions/transaction.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<T extends Transaction | VersionedTransaction>(
transaction: T,
options: {
signers?: Array<Signer>;
addressLookupTables?: AddressLookupTableAccount[];
type: TransactionType;
unitsConsumed?: number;
}
): T & {
signers?: Array<Signer>;
addressLookupTables?: AddressLookupTableAccount[];
type: TransactionType;
unitsConsumed?: number;
} {
options: ExtendedTransactionProperties
): T & ExtendedTransactionProperties {
return Object.assign(transaction, options);
}

Expand Down

0 comments on commit 72c514e

Please sign in to comment.