|
| 1 | +import { createContext, type FC, type PropsWithChildren, useContext } from 'react' |
| 2 | + |
| 3 | +import toast from 'react-hot-toast' |
| 4 | +import { |
| 5 | + Hash, |
| 6 | + type ReplacementReturnType, |
| 7 | + type SignMessageErrorType, |
| 8 | + type TransactionExecutionError, |
| 9 | +} from 'viem' |
| 10 | + |
| 11 | +import { ExplorerLink } from '@/src/components/sharedComponents/ExplorerLink' |
| 12 | +import { useWeb3Status } from '@/src/hooks/useWeb3Status' |
| 13 | +import { ToastNotification } from '@/src/lib/toast/ToastNotification' |
| 14 | + |
| 15 | +type WatchSignatureArgs = { |
| 16 | + successMessage?: string |
| 17 | + message: JSX.Element | string |
| 18 | + signaturePromise: Promise<Hash> |
| 19 | + onToastId?: (toastId: string) => void |
| 20 | + showSuccessToast?: boolean |
| 21 | +} |
| 22 | + |
| 23 | +type WatchHashArgs = { |
| 24 | + message?: string |
| 25 | + successMessage?: string |
| 26 | + errorMessage?: string |
| 27 | + hash: Hash |
| 28 | + toastId?: string |
| 29 | +} |
| 30 | + |
| 31 | +type WatchTxArgs = { txPromise: Promise<Hash>; methodId?: string } |
| 32 | + |
| 33 | +type TransactionContextValue = { |
| 34 | + watchSignature: (args: WatchSignatureArgs) => void |
| 35 | + watchHash: (args: WatchHashArgs) => void |
| 36 | + watchTx: (args: WatchTxArgs) => void |
| 37 | +} |
| 38 | + |
| 39 | +const TransactionContext = createContext<TransactionContextValue | undefined>(undefined) |
| 40 | + |
| 41 | +export const TransactionNotificationProvider: FC<PropsWithChildren> = ({ children }) => { |
| 42 | + const { readOnlyClient } = useWeb3Status() |
| 43 | + const chain = readOnlyClient?.chain |
| 44 | + |
| 45 | + async function watchSignature({ |
| 46 | + message, |
| 47 | + onToastId, |
| 48 | + showSuccessToast = true, |
| 49 | + signaturePromise, |
| 50 | + successMessage = 'Signature received!', |
| 51 | + }: WatchSignatureArgs) { |
| 52 | + const toastId = toast.loading(() => <ToastNotification message={message} />) |
| 53 | + onToastId?.(toastId) |
| 54 | + |
| 55 | + try { |
| 56 | + await signaturePromise |
| 57 | + if (showSuccessToast) { |
| 58 | + toast.success(<ToastNotification message={successMessage} />, { |
| 59 | + id: toastId, |
| 60 | + }) |
| 61 | + } |
| 62 | + } catch (e) { |
| 63 | + const error = e as TransactionExecutionError | SignMessageErrorType |
| 64 | + let message = error.message || 'An error occurred' |
| 65 | + if ('shortMessage' in error) { |
| 66 | + message = error.shortMessage |
| 67 | + } |
| 68 | + toast.error(<ToastNotification message={message} />, { id: toastId }) |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + async function watchHash({ |
| 73 | + errorMessage = 'Transaction was reverted!', |
| 74 | + hash, |
| 75 | + message = 'Transaction sent', |
| 76 | + successMessage = 'Transaction has been mined!', |
| 77 | + toastId, |
| 78 | + }: WatchHashArgs) { |
| 79 | + if (!chain) { |
| 80 | + console.error('Chain is not defined') |
| 81 | + return |
| 82 | + } |
| 83 | + |
| 84 | + if (!readOnlyClient) { |
| 85 | + console.error('ReadOnlyClient is not defined') |
| 86 | + return |
| 87 | + } |
| 88 | + |
| 89 | + toast.loading(() => <ToastNotification message={message} />, { |
| 90 | + id: toastId, |
| 91 | + }) |
| 92 | + |
| 93 | + try { |
| 94 | + let replacedTx = null as ReplacementReturnType | null |
| 95 | + const receipt = await readOnlyClient.waitForTransactionReceipt({ |
| 96 | + hash, |
| 97 | + onReplaced: (replacedTxData) => (replacedTx = replacedTxData), |
| 98 | + }) |
| 99 | + |
| 100 | + if (replacedTx !== null) { |
| 101 | + if (['replaced', 'cancelled'].includes(replacedTx.reason)) { |
| 102 | + toast.error( |
| 103 | + <div> |
| 104 | + <div>Transaction has been {replacedTx.reason}!</div> |
| 105 | + <ExplorerLink chain={chain} hashOrAddress={replacedTx.transaction.hash} /> |
| 106 | + </div>, |
| 107 | + { id: toastId }, |
| 108 | + ) |
| 109 | + } else { |
| 110 | + toast.success( |
| 111 | + <div> |
| 112 | + <div>{successMessage}</div> |
| 113 | + <ExplorerLink chain={chain} hashOrAddress={replacedTx.transaction.hash} /> |
| 114 | + </div>, |
| 115 | + { id: toastId }, |
| 116 | + ) |
| 117 | + } |
| 118 | + return |
| 119 | + } |
| 120 | + |
| 121 | + if (receipt.status === 'success') { |
| 122 | + toast.success( |
| 123 | + <div> |
| 124 | + <div>{successMessage}</div> |
| 125 | + <ExplorerLink chain={chain} hashOrAddress={hash} /> |
| 126 | + </div>, |
| 127 | + { id: toastId }, |
| 128 | + ) |
| 129 | + } else { |
| 130 | + toast.error( |
| 131 | + <div> |
| 132 | + <div>{errorMessage}</div> |
| 133 | + <ExplorerLink chain={chain} hashOrAddress={hash} /> |
| 134 | + </div>, |
| 135 | + { id: toastId }, |
| 136 | + ) |
| 137 | + } |
| 138 | + } catch (error) { |
| 139 | + console.error('Error watching hash', error) |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + async function watchTx({ methodId, txPromise }: WatchTxArgs) { |
| 144 | + const transactionMessage = methodId ? `Transaction for calling ${methodId}` : 'Transaction' |
| 145 | + |
| 146 | + let toastId: string = '' |
| 147 | + await watchSignature({ |
| 148 | + message: `Signature requested: ${transactionMessage}`, |
| 149 | + signaturePromise: txPromise, |
| 150 | + showSuccessToast: false, |
| 151 | + onToastId: (id) => (toastId = id), |
| 152 | + }) |
| 153 | + |
| 154 | + const hash = await txPromise |
| 155 | + await watchHash({ |
| 156 | + hash, |
| 157 | + toastId, |
| 158 | + message: `${transactionMessage} is pending to be mined ...`, |
| 159 | + successMessage: `${transactionMessage} has been mined!`, |
| 160 | + errorMessage: `${transactionMessage} has reverted!`, |
| 161 | + }) |
| 162 | + } |
| 163 | + |
| 164 | + return ( |
| 165 | + <TransactionContext.Provider value={{ watchTx, watchHash, watchSignature }}> |
| 166 | + {children} |
| 167 | + </TransactionContext.Provider> |
| 168 | + ) |
| 169 | +} |
| 170 | + |
| 171 | +// eslint-disable-next-line react-refresh/only-export-components |
| 172 | +export function useTransactionNotification() { |
| 173 | + const context = useContext(TransactionContext) |
| 174 | + if (context === undefined) { |
| 175 | + throw new Error( |
| 176 | + 'useTransactionNotification must be used within a TransactionNotificationProvider', |
| 177 | + ) |
| 178 | + } |
| 179 | + return context |
| 180 | +} |
0 commit comments