diff --git a/packages/shared/lib/core/wallet/utils/getOutputParameters.ts b/packages/shared/lib/core/wallet/utils/getOutputParameters.ts index 4450facefe5..9a10ff1122f 100644 --- a/packages/shared/lib/core/wallet/utils/getOutputParameters.ts +++ b/packages/shared/lib/core/wallet/utils/getOutputParameters.ts @@ -62,7 +62,7 @@ async function buildOutputParametersForLayer2(transactionDetails: NewTransaction const senderAddress = layer2Parameters.senderAddress const recipientAddress = layer2Parameters?.networkAddress - let amount = getAmountFromTransactionDetails(transactionDetails) + const amount = getAmountFromTransactionDetails(transactionDetails) const assets = getAssetFromTransactionDetails(transactionDetails) const tag = transactionDetails?.tag ? Converter.utf8ToHex(transactionDetails?.tag) : undefined const metadata = getLayer2MetadataForTransfer(transactionDetails) @@ -87,42 +87,49 @@ async function buildOutputParametersForLayer2(transactionDetails: NewTransaction }, } - const outputForEstimate = (await prepareOutput( - selectedAccount.index, - outputParams, - getDefaultTransactionOptions() - )) as unknown as BasicOutput | NftOutput - const serializedOutput = await outputHexBytes(outputForEstimate) - const gasEstimatePayload = await getEstimatedGasForTransferFromTransactionDetails(serializedOutput) + async function getEstimateData() { + const outputForEstimate = (await prepareOutput( + selectedAccount.index, + outputParams, + getDefaultTransactionOptions() + )) as unknown as BasicOutput | NftOutput + const serializedOutput = await outputHexBytes(outputForEstimate) + const gasEstimatePayload = await getEstimatedGasForTransferFromTransactionDetails(serializedOutput) + return { + outputForEstimate, + gasEstimatePayload, + } + } + + let estimatedData = await getEstimateData() + + if (estimatedData.gasEstimatePayload.gasBurned) { + // The "+1" is due to an optimization in WASP nodes. + const metadata = getLayer2MetadataForTransfer( + transactionDetails, + (estimatedData.gasEstimatePayload.gasBurned as number) + 1 + ) + if (!outputParams.features) { + outputParams.features = {} + } + outputParams.features.metadata = metadata + estimatedData = await getEstimateData() + } // Now that we have the gasFeeCharged, update the amount & the tx details - if (gasEstimatePayload.gasFeeCharged) { + if (estimatedData.gasEstimatePayload.gasFeeCharged) { newTransactionDetails.update((state) => { if (state?.layer2Parameters) { - state.layer2Parameters.gasBudget = BigInteger(gasEstimatePayload.gasFeeCharged as number) + state.layer2Parameters.gasBudget = BigInteger(estimatedData.gasEstimatePayload.gasFeeCharged as number) } return state }) - amount = (parseInt(outputForEstimate.amount, 10) + gasEstimatePayload.gasFeeCharged).toString() + outputParams.amount = ( + parseInt(estimatedData.outputForEstimate.amount, 10) + estimatedData.gasEstimatePayload.gasFeeCharged + ).toString() } - return { - recipientAddress, - amount, - ...(assets && { assets }), - features: { - ...(tag && { tag }), - ...(metadata && { metadata }), - ...(layer2Parameters && { sender: senderAddress }), - }, - unlocks: { - ...(expirationUnixTime && { expirationUnixTime }), - ...(timelockUnixTime && { timelockUnixTime }), - }, - storageDeposit: { - returnStrategy: ReturnStrategy.Gift, - }, - } + return outputParams } function getAmountFromTransactionDetails(transactionDetails: NewTransactionDetails): string {