diff --git a/packages/js/src/plugins/collateralModule/operations/fundCollateral.ts b/packages/js/src/plugins/collateralModule/operations/fundCollateral.ts index f0ed5c220..e713c88cf 100644 --- a/packages/js/src/plugins/collateralModule/operations/fundCollateral.ts +++ b/packages/js/src/plugins/collateralModule/operations/fundCollateral.ts @@ -19,6 +19,7 @@ import { Convergence } from '../../../Convergence'; import { protocolCache } from '../../protocolModule/cache'; import { collateralMintCache } from '../cache'; import { + addComputeBudgetIxsIfNeeded, getComputeUnitsToBeConsumed, getEstimatedPriorityFeeInMicorLamps, } from '@/utils/helpers'; @@ -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; }; diff --git a/packages/js/src/utils/helpers.ts b/packages/js/src/utils/helpers.ts index ee997c3ae..7d74c2c13 100644 --- a/packages/js/src/utils/helpers.ts +++ b/packages/js/src/utils/helpers.ts @@ -5,6 +5,7 @@ import { VersionedTransaction, } from '@solana/web3.js'; import { TransactionBuilder } from './TransactionBuilder'; +import { Convergence } from '@/Convergence'; type EstimatedPriorityFee = { unitsConsumed: number; @@ -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 + ); + } +};