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

Fix: evm editor improve #87

Merged
merged 2 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
96 changes: 24 additions & 72 deletions src/components/EvmChainSelect.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
import React, { useEffect, useState } from "react";
import {
Button,
Menu,
MenuButton,
MenuList,
MenuItem,
MenuGroup,
MenuDivider,
} from "@chakra-ui/react";
import { ChevronDownIcon } from "@chakra-ui/icons";
import { Select } from "@chakra-ui/react";
import { ReactJSXElement } from "@emotion/react/types/jsx-namespace";
import { supportedChains, bloctoSDK, useEthereum } from "../services/evm";

Expand All @@ -21,70 +12,31 @@ const supportedTestnetChains = supportedChains.filter(

const EvmChainSelect: React.FC = ({}): ReactJSXElement => {
const { chainId: currentChainId } = useEthereum();
const [chainName, setChainName] = useState(
supportedChains.find(({ chainId }) => chainId === currentChainId)?.name ||
"Ethereum Goerli"
);
useEffect(() => {
const chainName = supportedChains.find(
({ chainId }) => chainId === currentChainId
)?.name;
if (chainName) {
setChainName(chainName);
}
}, [currentChainId]);

return (
<Menu>
<MenuButton as={Button} rightIcon={<ChevronDownIcon />} width="200px">
{chainName}
</MenuButton>
<MenuList>
<MenuGroup title="Testnet">
{supportedTestnetChains.map(({ name, chainId }) => (
<MenuItem
key={chainId}
pl={5}
color="gray.700"
onClick={() => {
bloctoSDK.ethereum
.request({
method: "wallet_switchEthereumChain",
params: [{ chainId }],
})
.then(() => {
setChainName(name);
});
}}
>
{name}
</MenuItem>
))}
</MenuGroup>
<MenuDivider />
<MenuGroup title="Mainnet">
{supportedMainnetChains.map(({ name, chainId }) => (
<MenuItem
key={chainId}
pl={5}
color="gray.700"
onClick={() => {
bloctoSDK.ethereum
.request({
method: "wallet_switchEthereumChain",
params: [{ chainId }],
})
.then(() => {
setChainName(name);
});
}}
>
{name}
</MenuItem>
))}
</MenuGroup>
</MenuList>
</Menu>
<Select
width="200px"
value={currentChainId || ""}
onChange={(e) => {
bloctoSDK.ethereum.request({
method: "wallet_switchEthereumChain",
params: [{ chainId: e.target.value }],
});
}}
>
<option disabled>Testnet</option>
{supportedTestnetChains.map(({ name, chainId }) => (
<option key={chainId} value={chainId}>
{name}
</option>
))}
<option disabled>Mainnet</option>
{supportedMainnetChains.map(({ name, chainId }) => (
<option key={chainId} value={chainId}>
{name}
</option>
))}
</Select>
);
};

Expand Down
6 changes: 5 additions & 1 deletion src/components/EvmEditors/EvmTxForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ const EvmTxForm = ({
sendObj.to = toString;
}
if (valueString) {
sendObj.value = web3.utils.toHex(valueString);
try {
sendObj.value = web3.utils.toHex(valueString);
} catch (e) {
setValue("");
}
}
if (dataString) {
sendObj.data = dataString;
Expand Down