Skip to content
This repository was archived by the owner on Nov 10, 2023. It is now read-only.

Commit 1657e5f

Browse files
author
Aaron Cook
authored
fix: show estimation warnings on Safes that require safeTxGas (#3825)
* fix: show error message for older Safes * fix: `waitFor` test result
1 parent 2e6ed72 commit 1657e5f

File tree

3 files changed

+21
-40
lines changed

3 files changed

+21
-40
lines changed

src/logic/hooks/__tests__/useEstimateSafeTxGas.test.ts

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { waitFor } from '@testing-library/react'
12
import { renderHook, act } from '@testing-library/react-hooks'
23
import { useEstimateSafeTxGas } from 'src/logic/hooks/useEstimateSafeTxGas'
34
import * as gas from 'src/logic/safe/transactions/gas'
@@ -20,26 +21,6 @@ const actResolve = async (callback: () => unknown): Promise<void> => {
2021
}
2122

2223
describe('useEstimateSafeTxGas', () => {
23-
it(`should return 0 if it is not a tx creation`, async () => {
24-
const spy = jest.spyOn(gas, 'estimateSafeTxGas')
25-
26-
await actResolve(() => {
27-
const { result } = renderHook(() =>
28-
useEstimateSafeTxGas({
29-
txAmount: '',
30-
txData: '0x',
31-
txRecipient: '',
32-
isCreation: false,
33-
isRejectTx: false,
34-
}),
35-
)
36-
37-
expect(result.current).toBe('0')
38-
})
39-
40-
expect(spy).toHaveBeenCalledTimes(0)
41-
})
42-
4324
it(`should return 0 if it is a reject tx`, async () => {
4425
const spy = jest.spyOn(gas, 'estimateSafeTxGas')
4526

@@ -49,11 +30,12 @@ describe('useEstimateSafeTxGas', () => {
4930
txAmount: '',
5031
txData: '0x',
5132
txRecipient: '',
52-
isCreation: false,
5333
isRejectTx: true,
5434
}),
5535
)
56-
expect(result.current).toBe('0')
36+
37+
expect(result.current.result).toBe('0')
38+
expect(result.current.error).toBe(undefined)
5739
})
5840

5941
expect(spy).toHaveBeenCalledTimes(0)
@@ -68,12 +50,12 @@ describe('useEstimateSafeTxGas', () => {
6850
txAmount: '',
6951
txData: '',
7052
txRecipient: '',
71-
isCreation: true,
7253
isRejectTx: false,
7354
}),
7455
)
7556

76-
expect(result.current).toBe('0')
57+
expect(result.current.result).toBe('0')
58+
expect(result.current.error).toBe(undefined)
7759
})
7860

7961
expect(spy).toHaveBeenCalledTimes(0)
@@ -88,7 +70,6 @@ describe('useEstimateSafeTxGas', () => {
8870
txAmount: '',
8971
txData: '0x',
9072
txRecipient: '',
91-
isCreation: true,
9273
isRejectTx: false,
9374
}),
9475
)
@@ -97,22 +78,25 @@ describe('useEstimateSafeTxGas', () => {
9778
expect(spy).toHaveBeenCalledTimes(1)
9879
})
9980

100-
it(`returns 0 if estimateSafeTxGas throws`, async () => {
81+
it(`returns 0 and the error if estimateSafeTxGas throws`, async () => {
10182
const spy = jest.spyOn(gas, 'estimateSafeTxGas').mockImplementation(() => {
102-
throw new Error()
83+
throw new Error('Estimation failed')
10384
})
10485

105-
await actResolve(() => {
86+
await actResolve(async () => {
10687
const { result } = renderHook(() =>
10788
useEstimateSafeTxGas({
10889
txAmount: '',
10990
txData: '0x',
11091
txRecipient: '',
111-
isCreation: true,
11292
isRejectTx: false,
11393
}),
11494
)
115-
expect(result.current).toBe('0')
95+
96+
await waitFor(() => {
97+
expect(result.current.result).toBe('0')
98+
expect(result.current.error?.message).toBe('Estimation failed')
99+
})
116100
})
117101

118102
expect(spy).toHaveBeenCalledTimes(1)

src/logic/hooks/useEstimateSafeTxGas.tsx

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { currentSafe } from 'src/logic/safe/store/selectors'
77
import useAsync from 'src/logic/hooks/useAsync'
88

99
type UseEstimateSafeTxGasProps = {
10-
isCreation: boolean
1110
isRejectTx: boolean
1211
txData: string
1312
txRecipient: string
@@ -16,18 +15,17 @@ type UseEstimateSafeTxGasProps = {
1615
}
1716

1817
export const useEstimateSafeTxGas = ({
19-
isCreation,
2018
isRejectTx,
2119
txData,
2220
txRecipient,
2321
txAmount,
2422
operation,
25-
}: UseEstimateSafeTxGasProps): string => {
23+
}: UseEstimateSafeTxGasProps): { result: string; error: Error | undefined } => {
2624
const defaultEstimation = '0'
2725
const { address: safeAddress, currentVersion: safeVersion } = useSelector(currentSafe)
2826

2927
const requestSafeTxGas = useCallback((): Promise<string> => {
30-
if (!isCreation || isRejectTx || !txData) return Promise.resolve(defaultEstimation)
28+
if (isRejectTx || !txData) return Promise.resolve(defaultEstimation)
3129

3230
return estimateSafeTxGas(
3331
{
@@ -39,9 +37,9 @@ export const useEstimateSafeTxGas = ({
3937
},
4038
safeVersion,
4139
)
42-
}, [isCreation, isRejectTx, operation, safeAddress, safeVersion, txAmount, txData, txRecipient])
40+
}, [isRejectTx, operation, safeAddress, safeVersion, txAmount, txData, txRecipient])
4341

44-
const { result } = useAsync<string>(requestSafeTxGas)
42+
const { result, error } = useAsync<string>(requestSafeTxGas)
4543

46-
return result || defaultEstimation
44+
return { result: result || defaultEstimation, error }
4745
}

src/routes/safe/components/Transactions/helpers/TxModalWrapper/index.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,7 @@ export const TxModalWrapper = ({
117117

118118
const approvalAndExecution = isApproveAndExecute(Number(threshold), confirmationsLen, txType, preApprovingOwner)
119119

120-
const safeTxGasEstimation = useEstimateSafeTxGas({
121-
isCreation,
120+
const { result: safeTxGasEstimation, error: safeTxGasError } = useEstimateSafeTxGas({
122121
isRejectTx,
123122
txData,
124123
txRecipient: txTo || safeAddress,
@@ -235,7 +234,7 @@ export const TxModalWrapper = ({
235234
isExecution={doExecute}
236235
isRejection={isRejectTx}
237236
safeNonce={txParameters.safeNonce}
238-
txEstimationExecutionStatus={txEstimationExecutionStatus}
237+
txEstimationExecutionStatus={safeTxGasError ? EstimationStatus.FAILURE : txEstimationExecutionStatus}
239238
/>
240239
)}
241240

0 commit comments

Comments
 (0)