|
| 1 | +import React, { useCallback, useEffect, useState } from 'react'; |
| 2 | +import { BigNumber, UnsignedTransaction, ethers } from 'ethers'; |
| 3 | +import { decodeFunctionData } from 'viem'; |
| 4 | +import { Address, useTransaction } from 'wagmi'; |
| 5 | +import { ActionResponse, HeaderResponse, SingleLineResponse } from '@/components/cactiComponents'; |
| 6 | +import SkeletonWrap from '@/components/shared/SkeletonWrap'; |
| 7 | +import useAbi from '@/hooks/useAbi'; |
| 8 | +import TransactionReplayInput from './TransactionReplayInput'; |
| 9 | + |
| 10 | +interface TransactionReplayProps { |
| 11 | + txHash: Address; |
| 12 | +} |
| 13 | + |
| 14 | +const TransactionReplay = ({ txHash }: TransactionReplayProps) => { |
| 15 | + const { data, isLoading } = useTransaction({ hash: txHash }); |
| 16 | + const { data: abi } = useAbi(data?.to as Address | undefined); |
| 17 | + const [sendParams, setSendParams] = useState<UnsignedTransaction>(); |
| 18 | + const [isError, setIsError] = useState(false); |
| 19 | + |
| 20 | + const explorerUrl = `https://etherscan.io/tx/${txHash}`; |
| 21 | + |
| 22 | + // State to hold editable fields, stored as strings for simplicity |
| 23 | + const [decoded, setDecoded] = useState<{ |
| 24 | + to?: string; |
| 25 | + value: string; |
| 26 | + functionName?: string; |
| 27 | + args?: { |
| 28 | + name: string; // name of the argument |
| 29 | + value: string; // value of the argument |
| 30 | + type: string; // type of the argument |
| 31 | + }[]; |
| 32 | + }>(); |
| 33 | + |
| 34 | + // handle decoding the transaction data |
| 35 | + const handleDecode = useCallback(() => { |
| 36 | + if (!data) return console.log('no data'); |
| 37 | + if (!abi) { |
| 38 | + console.log('no abi, is possibly an eth/native currency transfer'); |
| 39 | + return setDecoded({ |
| 40 | + to: data.to, |
| 41 | + value: data.value.toString(), |
| 42 | + functionName: 'transfer ETH', |
| 43 | + }); |
| 44 | + } |
| 45 | + |
| 46 | + // Decode the function data |
| 47 | + let args: string[] = []; |
| 48 | + let functionName: string; |
| 49 | + |
| 50 | + try { |
| 51 | + const decoded = decodeFunctionData({ abi, data: data.data as Address }); |
| 52 | + args = decoded.args as string[]; |
| 53 | + functionName = decoded.functionName; |
| 54 | + } catch (e) { |
| 55 | + setIsError(true); |
| 56 | + return console.error('error decoding function data', e); |
| 57 | + } |
| 58 | + |
| 59 | + // Get the types of the arguments for the function |
| 60 | + const getArgsTypes = ({ |
| 61 | + abi, |
| 62 | + functionName, |
| 63 | + argsLength, |
| 64 | + }: { |
| 65 | + abi: any[]; |
| 66 | + functionName: string; |
| 67 | + argsLength: number; |
| 68 | + }) => { |
| 69 | + return abi.find((item) => item.name === functionName && item.inputs.length === argsLength) |
| 70 | + ?.inputs as { |
| 71 | + name: string; |
| 72 | + type: string; |
| 73 | + }[]; |
| 74 | + }; |
| 75 | + |
| 76 | + const _args = args as string[]; |
| 77 | + const types = getArgsTypes({ abi, functionName, argsLength: _args.length }); |
| 78 | + |
| 79 | + setDecoded({ |
| 80 | + to: data.to, |
| 81 | + value: data.value.toString(), |
| 82 | + functionName, |
| 83 | + args: _args.map((_, i) => ({ |
| 84 | + name: types[i].name, |
| 85 | + value: _args[i], |
| 86 | + type: types[i].type, |
| 87 | + })), |
| 88 | + }); |
| 89 | + }, [abi, data]); |
| 90 | + |
| 91 | + useEffect(() => { |
| 92 | + handleDecode(); |
| 93 | + }, [handleDecode]); |
| 94 | + |
| 95 | + const handleReset = useCallback( |
| 96 | + (e: React.MouseEvent<HTMLButtonElement>) => { |
| 97 | + e.preventDefault(); |
| 98 | + handleDecode(); |
| 99 | + }, |
| 100 | + [handleDecode] |
| 101 | + ); |
| 102 | + |
| 103 | + const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => { |
| 104 | + const { name, value } = e.target; |
| 105 | + |
| 106 | + // handle changing the value param |
| 107 | + if (name === 'value') { |
| 108 | + setDecoded((d) => d && { ...d, value }); |
| 109 | + return; |
| 110 | + } |
| 111 | + // handle changing the to param |
| 112 | + if (name === 'to') { |
| 113 | + setDecoded((d) => d && { ...d, to: value }); |
| 114 | + return; |
| 115 | + } |
| 116 | + |
| 117 | + // handle changing the args |
| 118 | + setDecoded((d) => { |
| 119 | + if (!d?.args) return; |
| 120 | + |
| 121 | + const newArgs = [...d.args]; |
| 122 | + const argIndex = newArgs.findIndex((arg) => arg.name === name); |
| 123 | + if (argIndex !== -1) newArgs[argIndex] = { ...newArgs[argIndex], value: value }; |
| 124 | + |
| 125 | + return { ...d, args: newArgs }; |
| 126 | + }); |
| 127 | + }; |
| 128 | + |
| 129 | + const getSendParams = useCallback((): UnsignedTransaction | undefined => { |
| 130 | + if (!decoded) { |
| 131 | + console.error('Decoded data is missing'); |
| 132 | + return; |
| 133 | + } |
| 134 | + |
| 135 | + // Initialize a transaction object |
| 136 | + let transaction: Partial<UnsignedTransaction> = { |
| 137 | + to: decoded.to, |
| 138 | + value: BigNumber.from(decoded.value), |
| 139 | + }; |
| 140 | + |
| 141 | + // If it's a simple transfer |
| 142 | + if (decoded.functionName === 'transfer ETH') { |
| 143 | + transaction.data = '0x'; |
| 144 | + } else { |
| 145 | + if (!decoded.functionName || !decoded.args) { |
| 146 | + console.error('Missing function name or args'); |
| 147 | + return; |
| 148 | + } |
| 149 | + // For contract interactions |
| 150 | + // First, convert decoded arguments to their proper types |
| 151 | + const convertedArgs = |
| 152 | + decoded.args?.map((arg) => { |
| 153 | + // Conversion logic here, based on the ABI or arg.type |
| 154 | + // For simplicity, let's assume everything's a string |
| 155 | + return arg.value; |
| 156 | + }) || []; |
| 157 | + |
| 158 | + // Create the function signature |
| 159 | + const functionTypes = decoded.args.map((arg) => arg.type).join(','); |
| 160 | + const functionSignature = `${decoded.functionName}(${functionTypes})`; |
| 161 | + |
| 162 | + // Create the encoded data field for contract interaction |
| 163 | + const iface = new ethers.utils.Interface(abi); |
| 164 | + try { |
| 165 | + // Encode the function data |
| 166 | + const data = iface.encodeFunctionData(functionSignature, convertedArgs); |
| 167 | + transaction.data = data; |
| 168 | + // Now, you can populate the transaction object and pass it to ActionResponse |
| 169 | + } catch (e) { |
| 170 | + console.error('Error encoding function data', e); |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + setSendParams(transaction); |
| 175 | + }, [abi, decoded]); |
| 176 | + |
| 177 | + useEffect(() => { |
| 178 | + getSendParams(); |
| 179 | + }, [getSendParams]); |
| 180 | + |
| 181 | + return ( |
| 182 | + <> |
| 183 | + {isError && <SingleLineResponse>error handling this transaction</SingleLineResponse>} |
| 184 | + {!data && !isLoading && ( |
| 185 | + <SingleLineResponse>no data found for this transaction</SingleLineResponse> |
| 186 | + )} |
| 187 | + {!decoded ? ( |
| 188 | + isError ? null : ( |
| 189 | + <SkeletonWrap /> |
| 190 | + ) |
| 191 | + ) : ( |
| 192 | + <> |
| 193 | + <HeaderResponse text={`${decoded.functionName}`} altUrl={explorerUrl} /> |
| 194 | + <SingleLineResponse className="flex items-center justify-center p-2"> |
| 195 | + <form className="w-full p-2"> |
| 196 | + {decoded?.args?.map((arg, i) => ( |
| 197 | + <div className="mb-4" key={i}> |
| 198 | + <label htmlFor={arg.name} className="block font-medium text-gray-400"> |
| 199 | + {arg.name} |
| 200 | + </label> |
| 201 | + <TransactionReplayInput |
| 202 | + name={arg.name} |
| 203 | + value={arg.value} |
| 204 | + onChange={handleInputChange} |
| 205 | + /> |
| 206 | + </div> |
| 207 | + ))} |
| 208 | + {BigNumber.from(decoded.value)?.gt(ethers.constants.Zero) && decoded.to && ( |
| 209 | + <> |
| 210 | + <div className="mb-4"> |
| 211 | + <label htmlFor={'to'} className="block font-medium text-gray-400"> |
| 212 | + {'to'} |
| 213 | + </label> |
| 214 | + <TransactionReplayInput |
| 215 | + name={'to'} |
| 216 | + value={decoded.to} |
| 217 | + onChange={handleInputChange} |
| 218 | + /> |
| 219 | + </div> |
| 220 | + <div className="mb-4"> |
| 221 | + <label htmlFor={'value'} className="block font-medium text-gray-400"> |
| 222 | + {'value'} |
| 223 | + </label> |
| 224 | + <TransactionReplayInput |
| 225 | + name={'value'} |
| 226 | + value={decoded.value} |
| 227 | + onChange={handleInputChange} |
| 228 | + /> |
| 229 | + </div> |
| 230 | + </> |
| 231 | + )} |
| 232 | + <button className="rounded-md bg-green-primary p-1.5" onClick={handleReset}> |
| 233 | + Reset |
| 234 | + </button> |
| 235 | + </form> |
| 236 | + </SingleLineResponse> |
| 237 | + <ActionResponse txParams={undefined} sendParams={sendParams} approvalParams={undefined} /> |
| 238 | + </> |
| 239 | + )} |
| 240 | + </> |
| 241 | + ); |
| 242 | +}; |
| 243 | + |
| 244 | +export default TransactionReplay; |
0 commit comments