1- import { useCallback , useState } from "react" ;
2- import { FormProvider } from "react-hook-form" ;
3- import type { Hex } from "viem" ;
4- import { AbiItemFormWithPreview } from "../../abi-form/abi-item-form-with-preview.js" ;
51import {
62 Accordion ,
73 AccordionContent ,
84 AccordionItem ,
95 AccordionTrigger ,
106} from "../../shadcn/accordion.js" ;
11- import { Button } from "../../shadcn/button.js" ;
12- import {
13- ConnectWalletAlert ,
14- DefaultResultDisplay ,
15- MsgSenderInput ,
16- } from "../shared/components.js" ;
17- import { useMsgSenderForm } from "../shared/form-utils.js" ;
18- import type { BaseExecutionProps , ExecutionParams } from "../shared/types.js" ;
19- import { useRawExecution } from "../shared/use-raw-execution.js" ;
7+ import { ExecutionForm } from "../shared/components/execution-form.js" ;
8+ import type { BaseExecutionProps , ExecutionParams } from "../types.js" ;
209
2110interface RawOperationsProps extends BaseExecutionProps {
2211 onQuery : ( params : ExecutionParams ) => Promise < `0x${string } `> ;
2312 onWrite : ( params : ExecutionParams ) => Promise < `0x${string } `> ;
13+ onSimulate ?: ( params : ExecutionParams ) => Promise < `0x${string } `> ;
2414}
2515
2616export function RawOperations ( {
@@ -32,6 +22,7 @@ export function RawOperations({
3222 isConnected,
3323 onQuery,
3424 onWrite,
25+ onSimulate,
3526 addressRenderer,
3627 onHashClick,
3728} : RawOperationsProps ) {
@@ -46,7 +37,7 @@ export function RawOperations({
4637 addresses = { addresses }
4738 requiresConnection = { requiresConnection }
4839 isConnected = { isConnected }
49- onExecute = { onQuery }
40+ onQuery = { onQuery }
5041 addressRenderer = { addressRenderer }
5142 onHashClick = { onHashClick }
5243 />
@@ -58,7 +49,8 @@ export function RawOperations({
5849 addresses = { addresses }
5950 requiresConnection = { requiresConnection }
6051 isConnected = { isConnected }
61- onExecute = { onWrite }
52+ onWrite = { onWrite }
53+ onSimulate = { onSimulate }
6254 addressRenderer = { addressRenderer }
6355 onHashClick = { onHashClick }
6456 />
@@ -69,7 +61,9 @@ export function RawOperations({
6961
7062interface RawOperationItemProps extends BaseExecutionProps {
7163 type : "call" | "transaction" ;
72- onExecute : ( params : ExecutionParams ) => Promise < `0x${string } `> ;
64+ onQuery ?: ( params : ExecutionParams ) => Promise < `0x${string } `> ;
65+ onWrite ?: ( params : ExecutionParams ) => Promise < `0x${string } `> ;
66+ onSimulate ?: ( params : ExecutionParams ) => Promise < `0x${string } `> ;
7367}
7468
7569function RawOperationItem ( {
@@ -80,41 +74,18 @@ function RawOperationItem({
8074 addresses,
8175 requiresConnection,
8276 isConnected,
83- onExecute,
77+ onQuery,
78+ onWrite,
79+ onSimulate,
8480 addressRenderer,
8581 onHashClick,
8682} : RawOperationItemProps ) {
87- const [ callData , setCallData ] = useState < string > ( "" ) ;
88- const [ value , setValue ] = useState < bigint | undefined > ( ) ;
89- const { form, msgSender } = useMsgSenderForm ( sender ) ;
90-
91- const isWrite = type === "transaction" ;
92- const {
93- result,
94- isExecuting,
95- execute : executeRaw ,
96- } = useRawExecution ( {
97- isWrite,
98- onExecute,
99- } ) ;
10083 const title = type === "call" ? "Raw Call" : "Raw Transaction" ;
10184 const description =
10285 type === "call"
10386 ? "Execute eth_call with arbitrary calldata"
10487 : "Send transaction with arbitrary calldata" ;
10588
106- const handleCallDataChange = useCallback (
107- ( { data, value : newValue } : { data ?: Hex ; value ?: bigint } ) => {
108- setCallData ( data || "" ) ;
109- setValue ( newValue ) ;
110- } ,
111- [ ] ,
112- ) ;
113-
114- const handleExecute = ( ) => {
115- executeRaw ( { callData, value, msgSender } ) ;
116- } ;
117-
11889 return (
11990 < AccordionItem
12091 value = { type }
@@ -127,53 +98,23 @@ function RawOperationItem({
12798 </ div >
12899 </ AccordionTrigger >
129100 < AccordionContent className = "px-3 pb-3" >
130- < FormProvider { ...form } >
131- < div className = "mt-4 space-y-6" >
132- { isWrite && < MsgSenderInput /> }
133-
134- < AbiItemFormWithPreview
135- addresses = { addresses }
136- onChange = { handleCallDataChange }
137- abiFunction = { type === "call" ? "rawCall" : "raw" }
138- address = { address }
139- sender = { sender || address }
140- chainId = { chainId }
141- ArgProps = {
142- addressRenderer
143- ? {
144- addressRenderer,
145- }
146- : undefined
147- }
148- />
149-
150- { isWrite && requiresConnection && ! isConnected && (
151- < ConnectWalletAlert />
152- ) }
153-
154- < div className = "flex flex-row items-center justify-center gap-2" >
155- < Button
156- onClick = { handleExecute }
157- disabled = { ! callData || isExecuting || ( isWrite && ! isConnected ) }
158- className = "w-fit"
159- >
160- { isExecuting
161- ? "Executing..."
162- : type === "call"
163- ? "Call"
164- : "Send Transaction" }
165- </ Button >
166- </ div >
167-
168- { result && (
169- < DefaultResultDisplay
170- key = { `${ result . type } -${ result . data } ` }
171- result = { result }
172- onHashClick = { onHashClick }
173- />
174- ) }
175- </ div >
176- </ FormProvider >
101+ < ExecutionForm
102+ abiFunction = { type === "call" ? "rawCall" : "raw" }
103+ address = { address }
104+ chainId = { chainId }
105+ sender = { sender }
106+ addresses = { addresses }
107+ requiresConnection = { requiresConnection }
108+ isConnected = { isConnected }
109+ addressRenderer = { addressRenderer }
110+ onHashClick = { onHashClick }
111+ executionParams = { {
112+ onQuery,
113+ onWrite,
114+ onSimulate,
115+ } }
116+ className = "mt-4 space-y-4"
117+ />
177118 </ AccordionContent >
178119 </ AccordionItem >
179120 ) ;
0 commit comments