Skip to content

Commit

Permalink
fix: do not allow sending if failed to request estimategas-onledger (
Browse files Browse the repository at this point in the history
…#7696)

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

* fix: logic

* fix: remove unnecessary try/catch

* feat: remove redundant promise resolve & reject
  • Loading branch information
begonaalvarezd authored Nov 20, 2023
1 parent 25b9993 commit c3b62e7
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 32 deletions.
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'))
}

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) {
// 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
}

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

0 comments on commit c3b62e7

Please sign in to comment.