Skip to content

Fix/qr modal and chain source #78

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

Merged
merged 3 commits into from
Sep 11, 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
6 changes: 2 additions & 4 deletions web/src/components/ConnectWallet/AccountDisplay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import styled, { css } from "styled-components";
import Identicon from "react-identicons";
import { isAddress } from "viem";
import { normalize } from "viem/ens";
import { useAccount, useChainId, useEnsAvatar, useEnsName } from "wagmi";
import { useAccount, useEnsAvatar, useEnsName } from "wagmi";

import { getChain } from "consts/chains";
import { shortenAddress } from "utils/shortenAddress";

import { landscapeStyle } from "styles/landscapeStyle";
Expand Down Expand Up @@ -144,8 +143,7 @@ export const AddressOrName: React.FC<IAddressOrName> = ({ address: propAddress }
};

export const ChainDisplay: React.FC = () => {
const chainId = useChainId();
const chain = getChain(chainId);
const { chain } = useAccount();
return <label>{chain?.name}</label>;
};

Expand Down
8 changes: 3 additions & 5 deletions web/src/components/ConnectWallet/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useCallback } from "react";
import { useAccount, useChainId, useSwitchChain } from "wagmi";
import { useAccount, useSwitchChain } from "wagmi";
import { useWeb3Modal, useWeb3ModalState } from "@web3modal/wagmi/react";
import { Button } from "@kleros/ui-components-library";
import { SUPPORTED_CHAINS, DEFAULT_CHAIN } from "consts/chains";
Expand Down Expand Up @@ -29,7 +29,6 @@ export const SwitchChainButton: React.FC<{ className?: string }> = ({ className
);
};


const ConnectButton: React.FC<{ className?: string }> = ({ className }) => {
const { open } = useWeb3Modal();
const { open: isOpen } = useWeb3ModalState();
Expand All @@ -45,10 +44,9 @@ const ConnectButton: React.FC<{ className?: string }> = ({ className }) => {
};

const ConnectWallet: React.FC<{ className?: string }> = ({ className }) => {
const chainId = useChainId();
const { isConnected } = useAccount();
const { isConnected, chain } = useAccount();
if (isConnected) {
if (chainId !== DEFAULT_CHAIN) {
if (chain?.id !== DEFAULT_CHAIN) {
return <SwitchChainButton {...{ className }} />;
} else return <AccountDisplay />;
} else return <ConnectButton {...{ className }} />;
Expand Down
7 changes: 3 additions & 4 deletions web/src/components/EnsureAuth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useMemo, useState } from "react";

import * as jwt from "jose";
import { SiweMessage } from "siwe";
import { useAccount, useChainId, useSignMessage } from "wagmi";
import { useAccount, useSignMessage } from "wagmi";

import { Button } from "@kleros/ui-components-library";

Expand Down Expand Up @@ -37,8 +37,7 @@ export const EnsureAuth: React.FC<IEnsureAuth> = ({ children, message, buttonTex
const [isLoading, setIsLoading] = useState(false);

const [authToken, setAuthToken] = useSessionStorage<string | null>("auth-token", localToken);
const { address } = useAccount();
const chainId = useChainId();
const { address, chain } = useAccount();

const { signMessageAsync } = useSignMessage();

Expand All @@ -58,7 +57,7 @@ export const EnsureAuth: React.FC<IEnsureAuth> = ({ children, message, buttonTex
setIsLoading(true);
if (!address) return;

const message = await createSiweMessage(address, "Sign In to Kleros with Ethereum.", chainId);
const message = await createSiweMessage(address, "Sign In to Kleros with Ethereum.", chain.id);

const signature = await signMessageAsync({ message });

Expand Down
7 changes: 0 additions & 7 deletions web/src/consts/chains.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { extractChain } from "viem";
import { Chain, arbitrumSepolia, gnosisChiado } from "wagmi/chains";

export const DEFAULT_CHAIN = arbitrumSepolia.id;
Expand All @@ -15,9 +14,3 @@ export const QUERY_CHAINS: Record<number, Chain> = {
export const ALL_CHAINS = [...Object.values(SUPPORTED_CHAINS), ...Object.values(QUERY_CHAINS)];

export const QUERY_CHAIN_IDS = Object.keys(QUERY_CHAINS);

export const getChain = (chainId: number): Chain | null =>
extractChain({
chains: ALL_CHAINS,
id: chainId,
});
7 changes: 2 additions & 5 deletions web/src/context/Web3Provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ const getTransports = () => {
const alchemyTransport = (chain: Chain) =>
fallback([http(alchemyURL("https", chain.id)), webSocket(alchemyURL("wss", chain.id))]);
const chiadoTransport = () =>
fallback([
http("https://rpc.chiadochain.net"),
webSocket("wss://rpc.chiadochain.net/wss"),
]);
fallback([http("https://rpc.chiadochain.net"), webSocket("wss://rpc.chiadochain.net/wss")]);

return {
[arbitrumSepolia.id]: alchemyTransport(arbitrumSepolia),
Expand All @@ -42,7 +39,7 @@ const projectId = import.meta.env.WALLETCONNECT_PROJECT_ID ?? "";
const wagmiConfig = createConfig({
chains,
transports,
connectors: [walletConnect({ projectId })],
connectors: [walletConnect({ projectId, showQrModal: false })],
});

createWeb3Modal({
Expand Down
8 changes: 3 additions & 5 deletions web/src/hooks/useNativeTokenSymbol.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { useChainId } from "wagmi";
import { getChain } from "consts/chains";
import { useAccount } from "wagmi";

export const useNativeTokenSymbol = () => {
const chainId = useChainId();
const chain = getChain(chainId);
const { chain } = useAccount();
return chain?.nativeCurrency.symbol;
};
};
12 changes: 6 additions & 6 deletions web/src/hooks/useTokenMetadata.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { useState, useEffect } from "react";
import { Alchemy } from "alchemy-sdk";
import { useChainId } from "wagmi";
import { useAccount } from "wagmi";
import { alchemyConfig } from "utils/alchemyConfig";

export const useTokenMetadata = (tokenAddress: string) => {
const chainId = useChainId();
const { chain } = useAccount();
const [tokenMetadata, setTokenMetadata] = useState<any>(null);

useEffect(() => {
const fetchTokenMetadata = async () => {
if (!tokenAddress || tokenAddress === "native") return;
const alchemy = new Alchemy(alchemyConfig(chainId));
if (!tokenAddress || tokenAddress === "native" || !chain) return;
const alchemy = new Alchemy(alchemyConfig(chain.id));
try {
const metadata = await alchemy.core.getTokenMetadata(tokenAddress);
setTokenMetadata(metadata);
Expand All @@ -21,7 +21,7 @@ export const useTokenMetadata = (tokenAddress: string) => {
};

fetchTokenMetadata();
}, [tokenAddress, chainId]);
}, [tokenAddress, chain]);

return { tokenMetadata };
};
};
29 changes: 14 additions & 15 deletions web/src/layout/Header/DesktopHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import styled, { css } from "styled-components";
import { landscapeStyle } from "styles/landscapeStyle";
import { useToggle } from "react-use";
import { Link } from "react-router-dom";
import { useAccount, useChainId } from "wagmi";
import { useAccount } from "wagmi";
import { useLockOverlayScroll } from "hooks/useLockOverlayScroll";
import { DEFAULT_CHAIN } from "consts/chains";
import KlerosSolutionsIcon from "svgs/menu-icons/kleros-solutions.svg";
Expand Down Expand Up @@ -75,7 +75,8 @@ const ConnectWalletContainer = styled.div<{ isConnected: boolean; isDefaultChain
}

${({ isConnected, isDefaultChain }) =>
isConnected && isDefaultChain &&
isConnected &&
isDefaultChain &&
css`
cursor: pointer;
& > * {
Expand All @@ -88,24 +89,19 @@ const DesktopHeader = () => {
const [isDappListOpen, toggleIsDappListOpen] = useToggle(false);
const [isHelpOpen, toggleIsHelpOpen] = useToggle(false);
const [isSettingsOpen, toggleIsSettingsOpen] = useToggle(false);

const chainId = useChainId();
const { isConnected } = useAccount();

const isDefaultChain = chainId === DEFAULT_CHAIN;


const { isConnected, chain } = useAccount();

const isDefaultChain = chain?.id === DEFAULT_CHAIN;

useLockOverlayScroll(isDappListOpen || isHelpOpen || isSettingsOpen);

return (
<>
<Container>
<LeftSide>
<LightButtonContainer>
<LightButton
text=""
onClick={toggleIsDappListOpen}
Icon={StyledKlerosSolutionsIcon}
/>
<LightButton text="" onClick={toggleIsDappListOpen} Icon={StyledKlerosSolutionsIcon} />
</LightButtonContainer>
<StyledLink to={"/"}>
<EscrowLogo />
Expand All @@ -117,7 +113,10 @@ const DesktopHeader = () => {
</MiddleSide>

<RightSide>
<ConnectWalletContainer {...{ isConnected, isDefaultChain }} onClick={isConnected && isDefaultChain ? toggleIsSettingsOpen : undefined}>
<ConnectWalletContainer
{...{ isConnected, isDefaultChain }}
onClick={isConnected && isDefaultChain ? toggleIsSettingsOpen : undefined}
>
<ConnectWallet />
</ConnectWalletContainer>
<Menu {...{ toggleIsHelpOpen, toggleIsSettingsOpen }} />
Expand All @@ -134,4 +133,4 @@ const DesktopHeader = () => {
);
};

export default DesktopHeader;
export default DesktopHeader;
7 changes: 3 additions & 4 deletions web/src/pages/MyTransactions/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from "react";
import styled from "styled-components";
import { Route, Routes } from "react-router-dom";
import { useAccount, useChainId } from "wagmi";
import { useAccount } from "wagmi";
import { DEFAULT_CHAIN } from "consts/chains";
import ConnectWallet from "components/ConnectWallet";
import TransactionsFetcher from "./TransactionsFetcher";
Expand All @@ -27,9 +27,8 @@ export const ConnectWalletContainer = styled.div`
`;

const Dashboard: React.FC = () => {
const { isConnected } = useAccount();
const chainId = useChainId();
const isOnSupportedChain = chainId === DEFAULT_CHAIN;
const { isConnected, chain } = useAccount();
const isOnSupportedChain = chain?.id === DEFAULT_CHAIN;

return (
<Container>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
useSimulateEscrowUniversalCreateErc20Transaction,
escrowUniversalAddress,
} from "hooks/contracts/generated";
import { useChainId } from "wagmi";
import { erc20Abi } from "viem";
import { useNewTransactionContext } from "context/NewTransactionContext";
import {
Expand Down Expand Up @@ -47,8 +46,7 @@ const DepositPaymentButton: React.FC = () => {
const refetchQuery = useQueryRefetch();
const [isSending, setIsSending] = useState(false);
const [isApproved, setIsApproved] = useState(false);
const { address } = useAccount();
const chainId = useChainId();
const { address, chain } = useAccount();
const ensResult = useEnsAddress({ name: sellerAddress, chainId: 1 });
const deadlineTimestamp = useMemo(() => BigInt(Math.floor(new Date(deadline).getTime() / 1000)), [deadline]);
const isNativeTransaction = sendingToken?.address === "native";
Expand All @@ -62,11 +60,11 @@ const DepositPaymentButton: React.FC = () => {
}, [sellerAddress, ensResult.data]);

const { data: allowance, refetch: refetchAllowance } = useReadContract({
query: { enabled: !isNativeTransaction },
query: { enabled: !isNativeTransaction && chain?.id },
address: sendingToken?.address,
abi: erc20Abi,
functionName: "allowance",
args: [address, escrowUniversalAddress?.[chainId]],
args: [address, escrowUniversalAddress?.[chain?.id]],
});

useEffect(() => {
Expand Down Expand Up @@ -101,11 +99,11 @@ const DepositPaymentButton: React.FC = () => {
useWriteEscrowUniversalCreateErc20Transaction(createERC20TransactionConfig);

const { data: approveConfig } = useSimulateContract({
query: { enabled: !isNativeTransaction },
query: { enabled: !isNativeTransaction && chain?.id },
address: sendingToken?.address,
abi: erc20Abi,
functionName: "approve",
args: [escrowUniversalAddress?.[chainId], transactionValue],
args: [escrowUniversalAddress?.[chain?.id], transactionValue],
});

const { writeContractAsync: approve } = useWriteContract(approveConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useState, useEffect, useRef, useMemo } from "react";
import styled from "styled-components";
import { useClickAway } from "react-use";
import { Alchemy } from "alchemy-sdk";
import { useAccount, useChainId } from "wagmi";
import { useAccount } from "wagmi";
import { useNewTransactionContext } from "context/NewTransactionContext";
import { initializeTokens } from "utils/initializeTokens";
import { alchemyConfig } from "utils/alchemyConfig";
Expand All @@ -18,24 +18,23 @@ const Container = styled.div`
`;

const TokenSelector: React.FC = () => {
const { address } = useAccount();
const chainId = useChainId();
const { address, chain } = useAccount();
const { sendingToken, setSendingToken } = useNewTransactionContext();
const [tokens, setTokens] = useLocalStorage("tokens", []);
const [isOpen, setIsOpen] = useState(false);
const containerRef = useRef(null);
const [loading, setLoading] = useState(true);

const alchemyInstance = useMemo(() => new Alchemy(alchemyConfig(chainId)), [chainId]);
const alchemyInstance = useMemo(() => chain && new Alchemy(alchemyConfig(chain?.id)), [chain]);

useClickAway(containerRef, () => setIsOpen(false));

useEffect(() => {
if (address && chainId) {
if (address && alchemyInstance && chain) {
localStorage.removeItem("tokens");
initializeTokens(address, setTokens, setLoading, chainId, alchemyInstance);
initializeTokens(address, setTokens, setLoading, chain.id, alchemyInstance);
}
}, [address, chainId, alchemyInstance]);
}, [address, chain, alchemyInstance]);

useEffect(() => {
if (tokens?.length > 0) {
Expand Down
7 changes: 3 additions & 4 deletions web/src/pages/NewTransaction/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import Notifications from "./Terms/Notifications";
import Payment from "./Terms/Payment";
import Timeline from "./Timeline";
import { responsiveSize } from "styles/responsiveSize";
import { useAccount, useChainId } from "wagmi";
import { useAccount } from "wagmi";
import ConnectWallet from "components/ConnectWallet";
import { ConnectWalletContainer } from "../MyTransactions";
import { DEFAULT_CHAIN } from "consts/chains";
Expand All @@ -39,11 +39,10 @@ const MiddleContentContainer = styled.div`
const NewTransaction: React.FC = () => {
const location = useLocation();
const { width } = useWindowSize();
const { isConnected } = useAccount();
const chainId = useChainId();
const { isConnected, chain } = useAccount();
const isPreviewPage = location.pathname.includes("/preview");
const isMobileView = width <= BREAKPOINT_LANDSCAPE;
const isOnSupportedChain = chainId === DEFAULT_CHAIN;
const isOnSupportedChain = chain?.id === DEFAULT_CHAIN;

return (
<>
Expand Down