Skip to content

Commit

Permalink
test implementation 2
Browse files Browse the repository at this point in the history
  • Loading branch information
Nagaprasadvr committed Apr 24, 2024
1 parent ffbb4fc commit 58763bf
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { Convergence } from '../../../Convergence';
import { protocolCache } from '../../protocolModule/cache';
import { collateralMintCache } from '../cache';
import {
addComputeBudgetIxsIfNeeded,
getComputeUnitsToBeConsumed,
getEstimatedPriorityFeeInMicorLamps,
} from '@/utils/helpers';
Expand Down Expand Up @@ -181,32 +182,6 @@ export const fundCollateralBuilder = async (
signers: [user],
key: 'fundCollateral',
});
const computeUnitsConsumed = await getComputeUnitsToBeConsumed(
txBuilder,
convergence.connection
);
if (!computeUnitsConsumed) {
return txBuilder;
}
if (convergence.transactionPriority === 'dynamic') {
const estimatedTxFeeWithComputeUnits =
await getEstimatedPriorityFeeInMicorLamps(
txBuilder,
convergence.connection
);

if (!estimatedTxFeeWithComputeUnits) {
return txBuilder;
}
txBuilder.addDynamicComputeBudgetIxs(
estimatedTxFeeWithComputeUnits.microLamports,
estimatedTxFeeWithComputeUnits.unitsConsumed
);
} else {
txBuilder.addStaticComputeBudgetIxs(
convergence,
computeUnitsConsumed.unitsConsumed
);
}
await addComputeBudgetIxsIfNeeded(txBuilder, convergence);
return txBuilder;
};
34 changes: 34 additions & 0 deletions packages/js/src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
VersionedTransaction,
} from '@solana/web3.js';
import { TransactionBuilder } from './TransactionBuilder';
import { Convergence } from '@/Convergence';

type EstimatedPriorityFee = {
unitsConsumed: number;
Expand Down Expand Up @@ -99,3 +100,36 @@ export const getComputeUnitsToBeConsumed = async (
return null;
}
};

export const addComputeBudgetIxsIfNeeded = async (
txBuilder: TransactionBuilder,
convergence: Convergence
) => {
const computeUnitsConsumed = await getComputeUnitsToBeConsumed(
txBuilder,
convergence.connection
);
if (!computeUnitsConsumed) {
return txBuilder;
}
if (convergence.transactionPriority === 'dynamic') {
const estimatedTxFeeWithComputeUnits =
await getEstimatedPriorityFeeInMicorLamps(
txBuilder,
convergence.connection
);

if (!estimatedTxFeeWithComputeUnits) {
return txBuilder;
}
txBuilder.addDynamicComputeBudgetIxs(
estimatedTxFeeWithComputeUnits.microLamports,
estimatedTxFeeWithComputeUnits.unitsConsumed
);
} else {
txBuilder.addStaticComputeBudgetIxs(
convergence,
computeUnitsConsumed.unitsConsumed
);
}
};

0 comments on commit 58763bf

Please sign in to comment.