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

[Eng 50] | Dao creation network switch #2645

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,6 @@ export function EstablishEssentials(props: ICreationStepProps) {
},
});

useEffect(() => {
if (chain.id !== walletChainID) {
const chainId = getChainIdFromPrefix(addressPrefix);
switchChain({ chainId });
}
}, [chain.id, walletChainID, addressPrefix, switchChain]);

return (
<>
<StepWrapper
Expand Down
20 changes: 20 additions & 0 deletions src/components/DaoCreator/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Box } from '@chakra-ui/react';
import { Formik } from 'formik';
import { useEffect } from 'react';
import { useChainId, useSwitchChain } from 'wagmi';
import { useDAOCreateSchema } from '../../hooks/schemas/DAOCreate/useDAOCreateSchema';
import { useNetworkConfigStore } from '../../providers/NetworkConfig/useNetworkConfigStore';
import {
AzoriusERC20DAO,
AzoriusERC721DAO,
Expand Down Expand Up @@ -28,6 +31,8 @@ function DaoCreator({
mode: DAOCreateMode;
}) {
const { totalParentVotingWeight } = useParentSafeVotingWeight();
const walletChainID = useChainId();
const { chain } = useNetworkConfigStore();

const { createDAOValidation } = useDAOCreateSchema({
isSubDAO: !!isSubDAO,
Expand All @@ -37,6 +42,21 @@ function DaoCreator({
const { prepareMultisigFormData, prepareAzoriusERC20FormData, prepareAzoriusERC721FormData } =
usePrepareFormData();

const { switchChain } = useSwitchChain({
mutation: {
onError: () => {
if (chain.id !== walletChainID && !isSubDAO) {
switchChain({ chainId: chain.id });
}
},
},
Comment on lines +46 to +52
Copy link
Member

Choose a reason for hiding this comment

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

@Da-Colon can you explain what this mutation is and why it's needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So my understanding is that Metamask doesn't require that popup permission to switch networks anymore. Network permission are done within Metamask and if its listed then its available.

So when switchchain is called if the network is allowed then switch should happen. BUT for some reason this doesn't happen as it should. I looked into this before and mentioned a while back that I believe this error is occurs in
the current version of Viem.

So during testing. I found that this pattern 'brute' forces past the error and switches the network. Didn't see this problem in Frame (didn't reach the error).

There are two ways to get rid of this need more than likely.

  • Update viem to latest, but requires some changes in the app as they change Address typing to string for whatever reason.
  • We could also create our own wallet client to use in the app. I attempted to set this up here. but requires a bit more thought, a little refactor and additional testing for how network config (contracts) is used in this workflow.

Copy link
Contributor Author

@Da-Colon Da-Colon Dec 20, 2024

Choose a reason for hiding this comment

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

As far as what the mutation, it taps into the internal tanstack query parameters and I chose to use the onError from there.

Copy link
Contributor

Choose a reason for hiding this comment

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

@Da-Colon Does this work with other wallet extensions? Major ones are Coinbase Wallet and TokenPocket.

Copy link
Contributor

Choose a reason for hiding this comment

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

I will do some testing with CB and TP. Please hold on until testing is done.

});
useEffect(() => {
if (chain.id !== walletChainID && !isSubDAO) {
switchChain({ chainId: chain.id });
}
}, [chain.id, walletChainID, switchChain, isSubDAO]);

return (
<Box>
<Formik<CreatorFormState>
Expand Down
Loading