forked from yanglin5689446/flow-script-runner
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #80 from blocto/feat/evm-batch-tx-panel
Feat/evm batch tx editor
- Loading branch information
Showing
5 changed files
with
228 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import React, { useEffect, useState, Dispatch, SetStateAction } from "react"; | ||
import { | ||
Box, | ||
Text, | ||
Grid, | ||
Flex, | ||
IconButton, | ||
Radio, | ||
RadioGroup, | ||
} from "@chakra-ui/react"; | ||
import { AddIcon } from "@chakra-ui/icons"; | ||
import { ReactJSXElement } from "@emotion/react/types/jsx-namespace"; | ||
import type { EthereumTypes } from "@blocto/sdk"; | ||
import EvmTxForm from "./EvmTxForm"; | ||
|
||
interface EvmBatchTxEditorProps { | ||
setRequestObject: Dispatch< | ||
SetStateAction<EthereumTypes.EIP1193RequestPayload | undefined> | ||
>; | ||
account: string | null; | ||
} | ||
|
||
const RevertOptionMap: Record<string, any> = { | ||
true: true, | ||
false: false, | ||
unset: undefined, | ||
}; | ||
const emptyTx = {}; | ||
|
||
const EvmBatchTxEditor = ({ | ||
setRequestObject, | ||
account, | ||
}: EvmBatchTxEditorProps): ReactJSXElement => { | ||
const [revert, setRevert] = useState<string>("true"); | ||
const [txs, setTxs] = useState<any[]>([emptyTx]); | ||
|
||
useEffect(() => { | ||
if (account) { | ||
setRequestObject({ | ||
method: "wallet_sendMultiCallTransaction", | ||
params: [ | ||
txs, | ||
...(RevertOptionMap[revert] !== undefined | ||
? [RevertOptionMap[revert]] | ||
: []), | ||
], | ||
}); | ||
} | ||
}, [account, setRequestObject, revert, txs]); | ||
|
||
return ( | ||
<> | ||
<Grid templateRows="repeat(4, min-content)" gap="10px"> | ||
<Box fontWeight="bold">Revert</Box> | ||
<RadioGroup | ||
value={revert} | ||
onChange={(e) => { | ||
setRevert(e); | ||
}} | ||
> | ||
<Flex gap="15px"> | ||
{Object.keys(RevertOptionMap).map((key) => ( | ||
<Radio key={key} value={key}> | ||
{key} | ||
</Radio> | ||
))} | ||
</Flex> | ||
</RadioGroup> | ||
|
||
<Flex> | ||
<Box fontWeight="bold">Transaction</Box> | ||
<IconButton | ||
ml={2} | ||
aria-label="Add Transaction" | ||
isRound | ||
icon={<AddIcon />} | ||
size="xs" | ||
colorScheme="blue" | ||
onClick={() => { | ||
setTxs((prev) => [...prev, emptyTx]); | ||
}} | ||
/> | ||
</Flex> | ||
<Flex flexDir="column" mt={2} pl={4}> | ||
{txs.map((value, i) => ( | ||
<Box key={i}> | ||
<Text fontWeight="bold" mb={2}> | ||
Transaction {i + 1} | ||
</Text> | ||
<Flex my="5px" alignItems="center"> | ||
<EvmTxForm | ||
key={i} | ||
setTransactionObject={(tx) => { | ||
setTxs((prev) => { | ||
const newTxs = [...prev]; | ||
newTxs[i] = tx; | ||
return newTxs; | ||
}); | ||
}} | ||
account={account} | ||
/> | ||
</Flex> | ||
</Box> | ||
))} | ||
</Flex> | ||
</Grid> | ||
</> | ||
); | ||
}; | ||
|
||
export default EvmBatchTxEditor; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
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"; | ||
import { web3 } from "../../services/evm"; | ||
|
||
interface EvmTxFormProps { | ||
setTransactionObject: Dispatch< | ||
EthereumTypes.EIP1193RequestPayload["params"] | undefined | ||
>; | ||
account: string | null; | ||
} | ||
|
||
const EvmTxForm = ({ | ||
setTransactionObject, | ||
account, | ||
}: EvmTxFormProps): ReactJSXElement => { | ||
const [fromString, setFrom] = useState<string>(account || ""); | ||
const [toString, setTo] = useState<string>(""); | ||
const [valueString, setValue] = useState<string>(""); | ||
const [dataString, setData] = useState<string>(""); | ||
|
||
useEffect(() => { | ||
if (account) { | ||
const sendObj: { | ||
from: string; | ||
to?: string; | ||
value?: string; | ||
data?: string; | ||
} = { | ||
from: fromString, | ||
}; | ||
if (toString) { | ||
sendObj.to = toString; | ||
} | ||
if (valueString) { | ||
sendObj.value = web3.utils.toHex(valueString); | ||
} | ||
if (dataString) { | ||
sendObj.data = dataString; | ||
} | ||
setTransactionObject([sendObj]); | ||
} | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [account, fromString, toString, dataString, valueString]); | ||
useEffect(() => { | ||
setFrom(account || ""); | ||
}, [account]); | ||
|
||
return ( | ||
<Grid | ||
templateColumns="min-content 1fr" | ||
alignItems="center" | ||
gap={6} | ||
width="100%" | ||
> | ||
<Box mx="10px">From:</Box> | ||
<Textarea | ||
rows={1} | ||
value={fromString} | ||
onChange={(e) => { | ||
setFrom(e.target.value); | ||
}} | ||
/> | ||
<Box mx="10px">To:</Box> | ||
<Textarea | ||
rows={1} | ||
value={toString} | ||
onChange={(e) => { | ||
setTo(e.target.value); | ||
}} | ||
/> | ||
<Box mx="10px">Value:</Box> | ||
<Textarea | ||
rows={1} | ||
value={valueString} | ||
onChange={(e) => { | ||
setValue(e.target.value); | ||
}} | ||
/> | ||
<Box mx="10px">Data:</Box> | ||
<Textarea | ||
rows={3} | ||
value={dataString} | ||
onChange={(e) => { | ||
setData(e.target.value); | ||
}} | ||
/> | ||
</Grid> | ||
); | ||
}; | ||
|
||
export default EvmTxForm; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters