Skip to content

Commit c1d8864

Browse files
authored
added message for rejected transactions (#559)
1 parent 1a8dfc3 commit c1d8864

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed

packages/checkout/src/views/Checkout/PaymentMethodSelect/PayWithCrypto/index.tsx

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
compareAddress,
33
ContractVerificationStatus,
44
formatDisplay,
5+
isTxRejected,
56
sendTransactions,
67
TRANSACTION_CONFIRMATIONS_DEFAULT,
78
useAnalyticsContext
@@ -37,6 +38,8 @@ interface PayWithCryptoTabProps {
3738
isSwitchingChainRef: RefObject<boolean>
3839
}
3940

41+
type ErrorCause = 'generic' | 'user-rejection'
42+
4043
export const PayWithCryptoTab = ({ skipOnCloseCallback, isSwitchingChainRef }: PayWithCryptoTabProps) => {
4144
const connectedChainId = useChainId()
4245
const { switchChain } = useSwitchChain()
@@ -46,7 +49,7 @@ export const PayWithCryptoTab = ({ skipOnCloseCallback, isSwitchingChainRef }: P
4649
const { openTransactionStatusModal } = useTransactionStatusModal()
4750
const { selectPaymentSettings = {} as SelectPaymentSettings, closeSelectPaymentModal } = useSelectPaymentModal()
4851
const { analytics } = useAnalyticsContext()
49-
const [isError, setIsError] = useState<boolean>(false)
52+
const [error, setError] = useState<null | ErrorCause>(null)
5053
const { navigation, setNavigation } = useNavigationCheckout()
5154
const {
5255
initializeTransactionCounter,
@@ -246,7 +249,7 @@ export const PayWithCryptoTab = ({ skipOnCloseCallback, isSwitchingChainRef }: P
246249
}
247250

248251
setIsPurchasing(true)
249-
setIsError(false)
252+
setError(null)
250253

251254
try {
252255
if (connectedChainId != chainId) {
@@ -368,7 +371,8 @@ export const PayWithCryptoTab = ({ skipOnCloseCallback, isSwitchingChainRef }: P
368371
} catch (e) {
369372
console.error('Failed to purchase...', e)
370373
onError(e as Error)
371-
setIsError(true)
374+
const isRejected = isTxRejected(e as Error)
375+
setError(isRejected ? 'user-rejection' : 'generic')
372376
}
373377

374378
resetTransactionCounter()
@@ -393,7 +397,7 @@ export const PayWithCryptoTab = ({ skipOnCloseCallback, isSwitchingChainRef }: P
393397
}
394398

395399
setIsPurchasing(true)
396-
setIsError(false)
400+
setError(null)
397401

398402
try {
399403
if (connectedChainId != chainId) {
@@ -540,7 +544,8 @@ export const PayWithCryptoTab = ({ skipOnCloseCallback, isSwitchingChainRef }: P
540544
} catch (e) {
541545
console.error('Failed to purchase...', e)
542546
onError(e as Error)
543-
setIsError(true)
547+
const isRejected = isTxRejected(e as Error)
548+
setError(isRejected ? 'user-rejection' : 'generic')
544549
}
545550

546551
setIsPurchasing(false)
@@ -772,15 +777,22 @@ export const PayWithCryptoTab = ({ skipOnCloseCallback, isSwitchingChainRef }: P
772777
return 'Confirm payment'
773778
}
774779

780+
const getErrorText = () => {
781+
if (error == 'user-rejection') {
782+
return 'The transaction was rejected.'
783+
}
784+
return 'An error occurred. Please try again.'
785+
}
786+
775787
return (
776788
<div className="flex flex-col gap-4">
777789
<PriceSection />
778790

779791
<div className="flex flex-col justify-start items-center w-full gap-1">
780-
{isError && (
792+
{!!error && (
781793
<div className="flex flex-col justify-start items-center w-full">
782794
<Text variant="xsmall" color="negative">
783-
An error occurred. Please try again.
795+
{getErrorText()}
784796
</Text>
785797
</div>
786798
)}

packages/connect/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export { getModalPositionCss } from './utils/styling.js'
6363
export { getNetwork, getNetworkBackgroundColor, getNetworkColor } from './utils/networks.js'
6464
export { publicClientToProvider, walletClientToSigner } from './utils/adapters.js'
6565
export { signEthAuthProof, validateEthProof } from './utils/ethAuth.js'
66-
export { sendTransactions, waitForTransactionReceipt } from './utils/transactions.js'
66+
export { isTxRejected, sendTransactions, waitForTransactionReceipt } from './utils/transactions.js'
6767

6868
// Contexts
6969
export { ConnectConfigContextProvider, useConnectConfigContext } from './contexts/ConnectConfig.js'

packages/connect/src/utils/transactions.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,3 +265,19 @@ export const waitForTransactionReceipt = async ({
265265

266266
return receipt
267267
}
268+
269+
interface ErrorWithCode extends Error {
270+
code?: number
271+
}
272+
273+
export const isTxRejected = (error: Error): boolean => {
274+
const errorWithCode = error as ErrorWithCode
275+
276+
// error 4001 is documented in EIP-1193
277+
// https://eips.ethereum.org/EIPS/eip-1193#provider-errors
278+
if (errorWithCode?.code == 4001) {
279+
return true
280+
}
281+
282+
return false
283+
}

0 commit comments

Comments
 (0)