Skip to content

Commit

Permalink
fix(client): client now throws error when metamask is not installed
Browse files Browse the repository at this point in the history
  • Loading branch information
OwshenNetwork committed Jun 18, 2024
1 parent 3e9269f commit f716098
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
33 changes: 29 additions & 4 deletions client/src/components/WalletConnection/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,47 @@ const Web3ModalComponent = () => {
window.ethereum.on("accountsChanged", (accounts) => {
dispatch(setUserDetails({ address: accounts[0] }));
});
}else{
toast.error("Please make sure you have installed Metamask on your browser!")
} else {
toast.error(
"Please make sure you have installed Metamask on your browser!"
);
}
}, [dispatch]);

const connectWallet = async () => {
if (!window.ethereum) {
toast.error(
"Please make sure you have installed Metamask on your browser!"
);
return;
}

try {
const web3Modal = new Web3Modal();
const web3Modal = new Web3Modal({
cacheProvider: true,
providerOptions: {
injected: {
id: "WEB3_CONNECT_MODAL_ID",
enable: () => {
if (typeof window.ethereum !== "undefined") {
try {
return window.ethereum.enable();
} catch (error) {
console.error("User denied account access");
}
}
},
},
},
});

const _provider = await web3Modal.connect();
const web3 = new Web3(_provider);

const accounts = await web3.eth.getAccounts();
dispatch(setUserDetails({ address: accounts[0] }));
} catch (error) {
console.error("Error while connecting to your wallet:", error);
// Handle the error appropriately, e.g., show a message to the user
return toast.error(
"No wallet detected. Please connect your wallet to proceed."
);
Expand Down
12 changes: 7 additions & 5 deletions client/src/utils/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,13 @@ export const coreEndpoint =

export const chainIdOfWallet = async () => {
try {
const web3Modal = new Web3Modal();
const provider = await web3Modal.connect();
const web3 = new Web3(provider);
const chainId = await web3.eth.getChainId(); // Get the chainId
return Number(chainId);
if (window.ethereum) {
const web3Modal = new Web3Modal();
const provider = await web3Modal.connect();
const web3 = new Web3(provider);
const chainId = await web3.eth.getChainId(); // Get the chainId
return Number(chainId);
}
} catch (error) {
console.error("Error getting chain ID:", error);
}
Expand Down
5 changes: 4 additions & 1 deletion src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ impl NodeManager {
}

pub fn remove_peer(&mut self, peer: Peer) {
self.peers.retain(|p| p.addr != peer.addr);
// Keep at least one peer!
if self.peers.len() > 1 {
self.peers.retain(|p| p.addr != peer.addr);
}
}

fn update_peer(&mut self, peer: Peer) {
Expand Down

0 comments on commit f716098

Please sign in to comment.