From 6b9b33b4c680fb495887f66bffc22e436f7b5c62 Mon Sep 17 00:00:00 2001 From: "Chiaki.C" Date: Tue, 26 Dec 2023 15:51:51 +0800 Subject: [PATCH] feat: add contract editor in userOp editor --- src/components/EvmEditor.tsx | 1 + .../EvmEditors/EvmContractEditor.tsx | 76 +++++++++++-------- src/components/EvmEditors/EvmUserOpEditor.tsx | 21 ++++- src/scripts/evm/UserOperation.ts | 10 ++- 4 files changed, 75 insertions(+), 33 deletions(-) diff --git a/src/components/EvmEditor.tsx b/src/components/EvmEditor.tsx index ece7c86..bb64b7d 100644 --- a/src/components/EvmEditor.tsx +++ b/src/components/EvmEditor.tsx @@ -249,6 +249,7 @@ const EvmEditor = (): ReactJSXElement => { diff --git a/src/components/EvmEditors/EvmContractEditor.tsx b/src/components/EvmEditors/EvmContractEditor.tsx index 603a0e9..fdaae69 100644 --- a/src/components/EvmEditors/EvmContractEditor.tsx +++ b/src/components/EvmEditors/EvmContractEditor.tsx @@ -32,15 +32,19 @@ const EvmContractEditor = ({ setDecodeType, account, chainId, + writeOnly, }: { setRequestObject: Dispatch< SetStateAction >; - setDecodeType: Dispatch>; + setDecodeType?: Dispatch>; account: string | null; chainId: string | null; + writeOnly?: boolean; }): ReactJSXElement => { - const [requestMethod, setRequestMethod] = useState("eth_call"); + const [requestMethod, setRequestMethod] = useState( + "eth_sendTransaction" + ); const [contractAddress, setContractAddress] = useState(""); const [contractAbi, setContractAbi] = useState(""); const [methodName, setMethodName] = useState(""); @@ -90,10 +94,12 @@ const EvmContractEditor = ({ "latest", ], }); - setDecodeType( - JSON.parse(contractAbi).find((m: any) => m.name === methodName) - .outputs - ); + if (setDecodeType) { + setDecodeType( + JSON.parse(contractAbi).find((m: any) => m.name === methodName) + .outputs + ); + } } else { setRequestObject({ method: requestMethod, @@ -132,35 +138,45 @@ const EvmContractEditor = ({ {MenuGroups.map((menuGroup) => ( - {Object.entries(menuGroup.templates).map(([name, template]) => ( - { - importTemplate(template); - }} - > - {template.description} - - ))} + {Object.entries(menuGroup.templates) + .filter( + // if writeOnly is true, only show write templates + (item) => + !writeOnly || item[1].method === "eth_sendTransaction" + ) + .map(([name, template]) => ( + { + importTemplate(template); + }} + > + {template.description} + + ))} ))} - Method - { - setRequestMethod(e); - }} - > - - Read - Write - - + {!writeOnly && ( + <> + Method + { + setRequestMethod(e); + }} + > + + Read + Write + + + + )} Contract info >; account: string | null; + chainId: string | null; }): ReactJSXElement => { const [templateId, setTemplateId] = useState(""); const [callData, setCallData] = useState(""); @@ -72,8 +75,8 @@ const EvmUserOpEditor = ({ }, [account, callData, otherParam, setRequestObject]); const setTransactionToCallData = useCallback( - ([params]) => { - const { to, value = "0x", data = "0x" } = params; + ({ params }) => { + const { to, value = "0x", data = "0x" } = params[0]; if (!isAddress(to) || !isHexStrict(data)) return; @@ -121,8 +124,22 @@ const EvmUserOpEditor = ({ {templateId === "sendTransaction" ? ( + setTransactionToCallData({ + method: "eth_sendTransaction", + params, + }) + } + account={account} + /> + + ) : templateId === "sendContractTransaction" ? ( + + ) : ( diff --git a/src/scripts/evm/UserOperation.ts b/src/scripts/evm/UserOperation.ts index fcc7034..6e7a168 100644 --- a/src/scripts/evm/UserOperation.ts +++ b/src/scripts/evm/UserOperation.ts @@ -51,10 +51,18 @@ export const getNft: IUserOperationTemplate = { }, }; -export const sendTransaction: IUserOperationTemplate = { +export const sendRawTransaction: IUserOperationTemplate = { id: "sendTransaction", name: "Send transaction", userOpObj: { callData: "", }, }; + +export const sendContractTransaction: IUserOperationTemplate = { + id: "sendContractTransaction", + name: "Send Contract transaction", + userOpObj: { + callData: "", + }, +};