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

chore: added jsdocs and improved naming #1072

Merged
merged 1 commit into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
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
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 @@ -241,11 +241,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;
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