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

fix: do not allow sending if failed to request estimategas-onledger #7696

Merged
merged 7 commits into from
Nov 20, 2023
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
3 changes: 0 additions & 3 deletions packages/desktop/components/popups/send/SendNftForm.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,8 @@

const outputParams = await getOutputParameters(details)
preparedOutput = await prepareOutput($selectedAccount.index, outputParams, getDefaultTransactionOptions())

return Promise.resolve()
} catch (err) {
handleError(err)
return Promise.reject()
} finally {
isPreparingOutput = false
}
Expand Down
3 changes: 0 additions & 3 deletions packages/desktop/components/popups/send/SendTokenForm.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,8 @@

const outputParams = await getOutputParameters(details)
preparedOutput = await prepareOutput($selectedAccount.index, outputParams, getDefaultTransactionOptions())

return Promise.resolve()
} catch (err) {
handleError(err)
return Promise.reject()
} finally {
isPreparingOutput = false
}
Expand Down
1 change: 1 addition & 0 deletions packages/shared/lib/core/layer-2/interfaces/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './layer2-asset-allowance.interface'
export * from './layer2-gas-estimate-payload.interface'
export * from './layer2-parameters.interface'
export * from './layer2-smart-contract-call-data.interface'
export * from './layer2-transfer-allowance-metadata.interface'
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface ILayer2GasEstimatePayload {
gasBurned?: number
gasFeeCharged?: number
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import BigInteger from 'big-integer'
import { localize } from '@core/i18n'
import { getActiveProfile } from '@core/profile'

interface GasEstimatePayload {
gasBurned?: number
gasFeeCharged?: number
}
import BigInteger from 'big-integer'
import { ILayer2GasEstimatePayload } from '../interfaces'

export async function getEstimatedGasForTransferFromTransactionDetails(
serializedOutputHex: string
): Promise<GasEstimatePayload> {
): Promise<ILayer2GasEstimatePayload> {
const profile = getActiveProfile()
const chainMetadata = profile.network?.chains?.[0] ?? null

Expand All @@ -32,8 +29,12 @@ export async function getEstimatedGasForTransferFromTransactionDetails(
const gasBurned = BigInteger(data.gasBurned as string).toJSNumber()
const gasFeeCharged = BigInteger(data.gasFeeCharged as string).toJSNumber()

return { gasBurned, gasFeeCharged }
if (gasBurned && gasFeeCharged) {
return { gasBurned, gasFeeCharged }
}
}

throw new Error(localize('error.layer2.estimatedGas'))
}
begonaalvarezd marked this conversation as resolved.
Show resolved Hide resolved

return {}
Expand Down
43 changes: 26 additions & 17 deletions packages/shared/lib/core/wallet/utils/getOutputParameters.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getSelectedAccount, prepareOutput } from '@core/account'
import { ILayer2GasEstimatePayload } from '@core/layer-2/interfaces'
import {
getEstimatedGasForTransferFromTransactionDetails,
getLayer2MetadataForTransfer,
Expand Down Expand Up @@ -51,7 +52,9 @@ function buildOutputParameters(transactionDetails: NewTransactionDetails): Outpu
}
}

async function buildOutputParametersForLayer2(transactionDetails: NewTransactionDetails): Promise<OutputParams> {
async function buildOutputParametersForLayer2(
transactionDetails: NewTransactionDetails
): Promise<OutputParams | undefined> {
const { expirationDate, timelockDate, layer2Parameters } = transactionDetails ?? {}
const selectedAccount = getSelectedAccount()

Expand Down Expand Up @@ -87,7 +90,10 @@ async function buildOutputParametersForLayer2(transactionDetails: NewTransaction
},
}

async function getEstimateData() {
async function getEstimateData(): Promise<{
outputForEstimate: BasicOutput | NftOutput
gasEstimatePayload: ILayer2GasEstimatePayload
}> {
const outputForEstimate = (await prepareOutput(
selectedAccount.index,
outputParams,
Expand All @@ -103,7 +109,7 @@ async function buildOutputParametersForLayer2(transactionDetails: NewTransaction

let estimatedData = await getEstimateData()

if (estimatedData.gasEstimatePayload.gasBurned) {
if (estimatedData?.gasEstimatePayload?.gasBurned) {
begonaalvarezd marked this conversation as resolved.
Show resolved Hide resolved
// The "+1" is due to an optimization in WASP nodes.
const metadata = getLayer2MetadataForTransfer(
transactionDetails,
Expand All @@ -113,23 +119,26 @@ async function buildOutputParametersForLayer2(transactionDetails: NewTransaction
outputParams.features = {}
}
outputParams.features.metadata = metadata

estimatedData = await getEstimateData()
}

// Now that we have the gasFeeCharged, update the amount & the tx details
if (estimatedData.gasEstimatePayload.gasFeeCharged) {
newTransactionDetails.update((state) => {
if (state?.layer2Parameters) {
state.layer2Parameters.gasBudget = BigInteger(estimatedData.gasEstimatePayload.gasFeeCharged as number)
}
return state
})
outputParams.amount = (
parseInt(estimatedData.outputForEstimate.amount, 10) + estimatedData.gasEstimatePayload.gasFeeCharged
).toString()
if (estimatedData?.gasEstimatePayload?.gasFeeCharged) {
// Now that we have the gasFeeCharged, update the amount & the tx details
newTransactionDetails.update((state) => {
if (state?.layer2Parameters) {
state.layer2Parameters.gasBudget = BigInteger(
estimatedData.gasEstimatePayload.gasFeeCharged as number
)
}
return state
})
outputParams.amount = (
parseInt(estimatedData.outputForEstimate.amount, 10) + estimatedData.gasEstimatePayload.gasFeeCharged
).toString()

return outputParams
}
}

return outputParams
begonaalvarezd marked this conversation as resolved.
Show resolved Hide resolved
}

function getAmountFromTransactionDetails(transactionDetails: NewTransactionDetails): string {
Expand Down
3 changes: 2 additions & 1 deletion packages/shared/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1965,7 +1965,8 @@
"reservedTagKeyword": "Unable to use reserved tag keyword"
},
"layer2": {
"layer1Recipient": "A layer 2 transaction cannot be sent to a layer 1 account."
"layer1Recipient": "A layer 2 transaction cannot be sent to a layer 1 account.",
"estimatedGas": "Failed to estimate gas."
},
"node": {
"invalid": "Please enter a valid URL.",
Expand Down
Loading