-
Notifications
You must be signed in to change notification settings - Fork 473
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Show error on tx and safe creation if wallet balance is insuffi…
…cient (#2986) * feat: Show error on tx and safe creation if wallet balance is insufficient * fix: Write tests for useWalletCanPay * fix: Write another test for ExecuteForm
- Loading branch information
1 parent
dad0369
commit fbf7ac9
Showing
6 changed files
with
142 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import useWalletCanPay from '@/hooks/useWalletCanPay' | ||
import * as walletBalance from '@/hooks/wallets/useWalletBalance' | ||
import { renderHook } from '@/tests/test-utils' | ||
import { BigNumber } from 'ethers' | ||
|
||
describe('useWalletCanPay', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks() | ||
}) | ||
|
||
it('should return true if gasLimit is missing', () => { | ||
const { result } = renderHook(() => | ||
useWalletCanPay({ maxFeePerGas: BigNumber.from(1), maxPriorityFeePerGas: BigNumber.from(1) }), | ||
) | ||
|
||
expect(result.current).toEqual(true) | ||
}) | ||
|
||
it('should return true if maxFeePerGas is missing', () => { | ||
const { result } = renderHook(() => | ||
useWalletCanPay({ gasLimit: BigNumber.from(21000), maxPriorityFeePerGas: BigNumber.from(1) }), | ||
) | ||
|
||
expect(result.current).toEqual(true) | ||
}) | ||
|
||
it('should return true if wallet balance is missing', () => { | ||
jest.spyOn(walletBalance, 'default').mockReturnValue([undefined, undefined, false]) | ||
|
||
const { result } = renderHook(() => | ||
useWalletCanPay({ gasLimit: BigNumber.from(21000), maxFeePerGas: BigNumber.from(1) }), | ||
) | ||
|
||
expect(result.current).toEqual(true) | ||
}) | ||
|
||
it('should return false if wallet balance is smaller than gas costs', () => { | ||
jest.spyOn(walletBalance, 'default').mockReturnValue([BigNumber.from(20999), undefined, false]) | ||
|
||
const { result } = renderHook(() => | ||
useWalletCanPay({ gasLimit: BigNumber.from(21000), maxFeePerGas: BigNumber.from(1) }), | ||
) | ||
|
||
expect(result.current).toEqual(false) | ||
}) | ||
|
||
it('should return true if wallet balance is larger or equal than gas costs', () => { | ||
jest.spyOn(walletBalance, 'default').mockReturnValue([BigNumber.from(21000), undefined, false]) | ||
|
||
const { result } = renderHook(() => | ||
useWalletCanPay({ gasLimit: BigNumber.from(21000), maxFeePerGas: BigNumber.from(1) }), | ||
) | ||
|
||
expect(result.current).toEqual(true) | ||
}) | ||
|
||
it('should return true if wallet balance is larger or equal than gas costs', () => { | ||
jest.spyOn(walletBalance, 'default').mockReturnValue([BigNumber.from(21001), undefined, false]) | ||
|
||
const { result } = renderHook(() => | ||
useWalletCanPay({ gasLimit: BigNumber.from(21000), maxFeePerGas: BigNumber.from(1) }), | ||
) | ||
|
||
expect(result.current).toEqual(true) | ||
}) | ||
|
||
it('should take maxPriorityFeePerGas into account', () => { | ||
jest.spyOn(walletBalance, 'default').mockReturnValue([BigNumber.from(42000), undefined, false]) | ||
|
||
const { result } = renderHook(() => | ||
useWalletCanPay({ | ||
gasLimit: BigNumber.from(21000), | ||
maxFeePerGas: BigNumber.from(1), | ||
maxPriorityFeePerGas: BigNumber.from(1), | ||
}), | ||
) | ||
|
||
expect(result.current).toEqual(true) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import useWalletBalance from '@/hooks/wallets/useWalletBalance' | ||
import { BigNumber } from 'ethers' | ||
|
||
const useWalletCanPay = ({ | ||
gasLimit, | ||
maxFeePerGas, | ||
maxPriorityFeePerGas, | ||
}: { | ||
gasLimit?: BigNumber | ||
maxFeePerGas?: BigNumber | null | ||
maxPriorityFeePerGas?: BigNumber | null | ||
}) => { | ||
const [walletBalance] = useWalletBalance() | ||
|
||
// Take an optimistic approach and assume the wallet can pay | ||
// if gasLimit, maxFeePerGas or their walletBalance are missing | ||
if (!gasLimit || !maxFeePerGas || !walletBalance) return true | ||
|
||
const totalFee = maxFeePerGas.add(maxPriorityFeePerGas || BigNumber.from(0)).mul(gasLimit) | ||
|
||
return walletBalance.gte(totalFee) | ||
} | ||
|
||
export default useWalletCanPay |