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

feat: Improved l2 estimated gas #7604

Merged
merged 6 commits into from Oct 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
63 changes: 35 additions & 28 deletions packages/shared/lib/core/wallet/utils/getOutputParameters.ts
Expand Up @@ -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)
Expand All @@ -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 <OutputParams>{
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 {
Expand Down