Skip to content

Commit c3b62e7

Browse files
fix: do not allow sending if failed to request estimategas-onledger (#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
1 parent 25b9993 commit c3b62e7

File tree

7 files changed

+42
-32
lines changed

7 files changed

+42
-32
lines changed

packages/desktop/components/popups/send/SendNftForm.svelte

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,8 @@
8787
8888
const outputParams = await getOutputParameters(details)
8989
preparedOutput = await prepareOutput($selectedAccount.index, outputParams, getDefaultTransactionOptions())
90-
91-
return Promise.resolve()
9290
} catch (err) {
9391
handleError(err)
94-
return Promise.reject()
9592
} finally {
9693
isPreparingOutput = false
9794
}

packages/desktop/components/popups/send/SendTokenForm.svelte

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,8 @@
7777
7878
const outputParams = await getOutputParameters(details)
7979
preparedOutput = await prepareOutput($selectedAccount.index, outputParams, getDefaultTransactionOptions())
80-
81-
return Promise.resolve()
8280
} catch (err) {
8381
handleError(err)
84-
return Promise.reject()
8582
} finally {
8683
isPreparingOutput = false
8784
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from './layer2-asset-allowance.interface'
2+
export * from './layer2-gas-estimate-payload.interface'
23
export * from './layer2-parameters.interface'
34
export * from './layer2-smart-contract-call-data.interface'
45
export * from './layer2-transfer-allowance-metadata.interface'
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface ILayer2GasEstimatePayload {
2+
gasBurned?: number
3+
gasFeeCharged?: number
4+
}

packages/shared/lib/core/layer-2/utils/getEstimatedGasForTransferFromTransactionDetails.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
import BigInteger from 'big-integer'
1+
import { localize } from '@core/i18n'
22
import { getActiveProfile } from '@core/profile'
3-
4-
interface GasEstimatePayload {
5-
gasBurned?: number
6-
gasFeeCharged?: number
7-
}
3+
import BigInteger from 'big-integer'
4+
import { ILayer2GasEstimatePayload } from '../interfaces'
85

96
export async function getEstimatedGasForTransferFromTransactionDetails(
107
serializedOutputHex: string
11-
): Promise<GasEstimatePayload> {
8+
): Promise<ILayer2GasEstimatePayload> {
129
const profile = getActiveProfile()
1310
const chainMetadata = profile.network?.chains?.[0] ?? null
1411

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

35-
return { gasBurned, gasFeeCharged }
32+
if (gasBurned && gasFeeCharged) {
33+
return { gasBurned, gasFeeCharged }
34+
}
3635
}
36+
37+
throw new Error(localize('error.layer2.estimatedGas'))
3738
}
3839

3940
return {}

packages/shared/lib/core/wallet/utils/getOutputParameters.ts

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { getSelectedAccount, prepareOutput } from '@core/account'
2+
import { ILayer2GasEstimatePayload } from '@core/layer-2/interfaces'
23
import {
34
getEstimatedGasForTransferFromTransactionDetails,
45
getLayer2MetadataForTransfer,
@@ -51,7 +52,9 @@ function buildOutputParameters(transactionDetails: NewTransactionDetails): Outpu
5152
}
5253
}
5354

54-
async function buildOutputParametersForLayer2(transactionDetails: NewTransactionDetails): Promise<OutputParams> {
55+
async function buildOutputParametersForLayer2(
56+
transactionDetails: NewTransactionDetails
57+
): Promise<OutputParams | undefined> {
5558
const { expirationDate, timelockDate, layer2Parameters } = transactionDetails ?? {}
5659
const selectedAccount = getSelectedAccount()
5760

@@ -87,7 +90,10 @@ async function buildOutputParametersForLayer2(transactionDetails: NewTransaction
8790
},
8891
}
8992

90-
async function getEstimateData() {
93+
async function getEstimateData(): Promise<{
94+
outputForEstimate: BasicOutput | NftOutput
95+
gasEstimatePayload: ILayer2GasEstimatePayload
96+
}> {
9197
const outputForEstimate = (await prepareOutput(
9298
selectedAccount.index,
9399
outputParams,
@@ -103,7 +109,7 @@ async function buildOutputParametersForLayer2(transactionDetails: NewTransaction
103109

104110
let estimatedData = await getEstimateData()
105111

106-
if (estimatedData.gasEstimatePayload.gasBurned) {
112+
if (estimatedData?.gasEstimatePayload?.gasBurned) {
107113
// The "+1" is due to an optimization in WASP nodes.
108114
const metadata = getLayer2MetadataForTransfer(
109115
transactionDetails,
@@ -113,23 +119,26 @@ async function buildOutputParametersForLayer2(transactionDetails: NewTransaction
113119
outputParams.features = {}
114120
}
115121
outputParams.features.metadata = metadata
122+
116123
estimatedData = await getEstimateData()
117-
}
118124

119-
// Now that we have the gasFeeCharged, update the amount & the tx details
120-
if (estimatedData.gasEstimatePayload.gasFeeCharged) {
121-
newTransactionDetails.update((state) => {
122-
if (state?.layer2Parameters) {
123-
state.layer2Parameters.gasBudget = BigInteger(estimatedData.gasEstimatePayload.gasFeeCharged as number)
124-
}
125-
return state
126-
})
127-
outputParams.amount = (
128-
parseInt(estimatedData.outputForEstimate.amount, 10) + estimatedData.gasEstimatePayload.gasFeeCharged
129-
).toString()
125+
if (estimatedData?.gasEstimatePayload?.gasFeeCharged) {
126+
// Now that we have the gasFeeCharged, update the amount & the tx details
127+
newTransactionDetails.update((state) => {
128+
if (state?.layer2Parameters) {
129+
state.layer2Parameters.gasBudget = BigInteger(
130+
estimatedData.gasEstimatePayload.gasFeeCharged as number
131+
)
132+
}
133+
return state
134+
})
135+
outputParams.amount = (
136+
parseInt(estimatedData.outputForEstimate.amount, 10) + estimatedData.gasEstimatePayload.gasFeeCharged
137+
).toString()
138+
139+
return outputParams
140+
}
130141
}
131-
132-
return outputParams
133142
}
134143

135144
function getAmountFromTransactionDetails(transactionDetails: NewTransactionDetails): string {

packages/shared/locales/en.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1965,7 +1965,8 @@
19651965
"reservedTagKeyword": "Unable to use reserved tag keyword"
19661966
},
19671967
"layer2": {
1968-
"layer1Recipient": "A layer 2 transaction cannot be sent to a layer 1 account."
1968+
"layer1Recipient": "A layer 2 transaction cannot be sent to a layer 1 account.",
1969+
"estimatedGas": "Failed to estimate gas."
19691970
},
19701971
"node": {
19711972
"invalid": "Please enter a valid URL.",

0 commit comments

Comments
 (0)