DEV Community

SDLC Corp
SDLC Corp

Posted on

How can I integrate Web3.js to enable wallet connectivity for a cryptocurrency exchange?

Here’s how to connect a wallet (e.g., MetaMask) using Web3.js:

import Web3 from 'web3';

async function connectWallet() {
  if (window.ethereum) {
    const web3 = new Web3(window.ethereum);
    try {
      await window.ethereum.request({ method: 'eth_requestAccounts' });
      const accounts = await web3.eth.getAccounts();
      console.log('Connected account:', accounts[0]);
    } catch (error) {
      console.error('Wallet connection failed:', error);
    }
  } else {
    console.log('MetaMask not detected.');
  }
}

connectWallet();

Enter fullscreen mode Exit fullscreen mode

Build secure, scalable, and feature-rich platforms tailored to your business needs. From blockchain integration to real-time trading, get end-to-end solutions for your crypto exchange project. Let's create the future of digital trading together with Cryptocurrency Exchange Development Services!"

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay