Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add sendTx feat to UserOp template #67

Merged
merged 3 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/components/EvmEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,12 @@ const EvmEditor = (): ReactJSXElement => {
</TabPanel>
<TabPanel>
<EvmSendEditor
setRequestObject={setRequestObject}
setRequestObject={([params] = []) =>
setRequestObject({
method: "eth_sendTransaction",
params,
})
}
Copy link
Member

@mordochi mordochi Sep 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any concern that we update the method like this?

Cuz we can still keep it setRequestObject={setRequestObject} and setTransactionToCallData should be

const setTransactionToCallData = useCallback(
    ({ params: [tx] = [] }) => {
        const { to, value = "0x", data = "0x" } = tx;
        ...
    },
    [otherParam, setRequestObject]
);

This way we don't have to update src/components/EvmEditor.tsx and src/components/EvmEditors/EvmSendEditor.tsx

account={account}
/>
</TabPanel>
Expand Down
18 changes: 4 additions & 14 deletions src/components/EvmEditors/EvmSendEditor.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState, Dispatch, SetStateAction } from "react";
import React, { useEffect, useState, Dispatch } from "react";
import { Box, Textarea, Grid } from "@chakra-ui/react";
import { ReactJSXElement } from "@emotion/react/types/jsx-namespace";
import type { EthereumTypes } from "@blocto/sdk";
Expand All @@ -9,7 +9,7 @@ const EvmSendEditor = ({
account,
}: {
setRequestObject: Dispatch<
SetStateAction<EthereumTypes.EIP1193RequestPayload | undefined>
EthereumTypes.EIP1193RequestPayload["params"] | undefined
>;
account: string | null;
}): ReactJSXElement => {
Expand All @@ -36,19 +36,9 @@ const EvmSendEditor = ({
if (dataString) {
sendObj.data = dataString;
}
setRequestObject({
method: "eth_sendTransaction",
params: [sendObj],
});
setRequestObject([sendObj]);
}
}, [
account,
fromString,
toString,
dataString,
valueString,
setRequestObject,
]);
}, [account, fromString, toString, dataString, valueString]);
useEffect(() => {
setFrom(account || "");
}, [account]);
Expand Down
69 changes: 58 additions & 11 deletions src/components/EvmEditors/EvmUserOpEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,22 @@ import { EthereumTypes } from "@blocto/sdk";
import ParamEditor from "./ParamEditor";
import * as UserOperationTemplate from "../../scripts/evm/UserOperation";
import type { IUserOperationTemplate } from "../../scripts/evm/UserOperation";
import EvmSendEditor from "./EvmSendEditor";
import { AbiItem, numberToHex, isAddress, isHexStrict } from "web3-utils";
import Web3EthAbi from "web3-eth-abi";

const MenuGroups = [{ title: "Request", templates: UserOperationTemplate }];
const ABI: AbiItem = {
inputs: [
{ internalType: "address", name: "dest", type: "address" },
{ internalType: "uint256", name: "value", type: "uint256" },
{ internalType: "bytes", name: "func", type: "bytes" },
],
name: "execute",
outputs: [],
stateMutability: "nonpayable",
type: "function",
};

const EvmUserOpEditor = ({
setRequestObject,
Expand All @@ -34,10 +48,13 @@ const EvmUserOpEditor = ({
>;
account: string | null;
}): ReactJSXElement => {
const [templateId, setTemplateId] = useState<string>("");
const [callData, setCallData] = useState<string>("");
const [otherParam, setOtherParam] = useState<string[][]>([]);

const importTemplate = useCallback((template: IUserOperationTemplate) => {
setCallData(template.userOpObj.callData || "");
setTemplateId(template.id);
setCallData(template.userOpObj.callData);
setOtherParam(
Object.entries(template.userOpObj)
.filter(([key]) => key !== "callData")
Expand All @@ -54,6 +71,27 @@ const EvmUserOpEditor = ({
}
}, [account, callData, otherParam, setRequestObject]);

const setTransactionToCallData = useCallback(
([params]) => {
const { to, value = "0x", data = "0x" } = params;

if (!isAddress(to) || !isHexStrict(data)) return;

const callData = Web3EthAbi.encodeFunctionCall(ABI, [
to,
numberToHex(value),
data,
]);

setCallData(callData);
setRequestObject({
method: "eth_sendUserOperation",
params: [{ callData, ...Object.fromEntries(otherParam) }],
});
},
[otherParam, setRequestObject]
);

return (
<Flex flexDirection="column">
<Menu>
Expand All @@ -80,16 +118,25 @@ const EvmUserOpEditor = ({
</MenuList>
</Menu>

<Flex my="20px" alignItems="center">
<Box mx="10px">callData:</Box>
<Textarea
rows={2}
value={callData}
onChange={(e) => {
setCallData(e.target.value);
}}
/>
</Flex>
{templateId === "sendTransaction" ? (
<Box my="20px">
<EvmSendEditor
setRequestObject={setTransactionToCallData}
account={account}
/>
</Box>
) : (
<Flex my="20px" alignItems="center">
<Box mx="10px">callData:</Box>
<Textarea
rows={2}
value={callData}
onChange={(e) => {
setCallData(e.target.value);
}}
/>
</Flex>
)}

<ParamEditor
title="Other Param"
mordochi marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
13 changes: 13 additions & 0 deletions src/scripts/evm/UserOperation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ interface IUserOperation {
paymasterAndData?: string;
}
export interface IUserOperationTemplate {
id: string;
name: string;
userOpObj: IUserOperation;
}

export const sendTokens: IUserOperationTemplate = {
id: "sendTokens",
name: "Send native token",
userOpObj: {
callData:
Expand All @@ -21,6 +23,7 @@ export const sendTokens: IUserOperationTemplate = {
};

export const transferErc20: IUserOperationTemplate = {
id: "transferErc20",
name: "Transfer erc20 token",
userOpObj: {
callData:
Expand All @@ -29,6 +32,7 @@ export const transferErc20: IUserOperationTemplate = {
};

export const setValueWithCustomPaymaster: IUserOperationTemplate = {
id: "setValueWithCustomPaymaster",
name: "Set value with custom paymaster",
userOpObj: {
callData:
Expand All @@ -39,9 +43,18 @@ export const setValueWithCustomPaymaster: IUserOperationTemplate = {
};

export const getNft: IUserOperationTemplate = {
id: "getNft",
name: "Mint NFT",
userOpObj: {
callData:
"b61d27f600000000000000000000000000005ea00ac477b1030ce78506496e8c2de24bf5000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000084161ac21f000000000000000000000000fd8ec18d48ac1f46b600e231da07d1da8209ceef0000000000000000000000000000a26b00c1f0df003000390027140000faa7190000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000",
},
};

export const sendTransaction: IUserOperationTemplate = {
id: "sendTransaction",
name: "Send transaction",
userOpObj: {
callData: "",
},
};
Loading