forked from yanglin5689446/flow-script-runner
-
Notifications
You must be signed in to change notification settings - Fork 0
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/evm batch tx editor #80
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
revert flag default 幫我放 true: https://www.notion.so/portto/Multi-Call-Transaction-Tech-Spec-7d181bbf64f74e6690fe3743169f1a29?pvs=4#995fca2076b740f196f9a7510a1d7e4e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
另外幫我做成 optional 的可以嗎 因為這個 param 其實可以不帶
wallet 理論上還是要用 revert = false 的方式幫處理掉~想讓 QA 也可以測一下 dapp 沒帶的情況 謝謝!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
resolved enhance: optional revert and improve empty tx express
RevertOptionMap
處理最後送進 sdk 的 revert 值There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我是要說 default 放 false lol 講錯了搜哩XDD
貼的那個 spec 就是寫 default false