asteriskConnect MetaMask via ethers.js

You can also connect MetaMask programmatically using ethers.

Sample Code

import { ethers } from 'ethers';

export const LUNTRA_NETWORK_PARAMS = {
  chainId: '0x34F820', // 3470144 in hexadecimal
  chainName: 'Luntra Network',
  rpcUrls: ['https://rpc.luntrainfrastructure.com/http'],
  nativeCurrency: {
    name: 'Ether',
    symbol: 'ETH',
    decimals: 18,
  },
  blockExplorerUrls: ['https://explorer.luntrainfrastructure.com/'],
};

export async function connectMetaMask() {
  if (typeof window.ethereum === 'undefined') {
    alert('MetaMask not installed!');
    return;
  }

  const provider = new ethers.providers.Web3Provider(window.ethereum);

  try {
    await window.ethereum.request({
      method: 'wallet_addEthereumChain',
      params: [LUNTRA_NETWORK_PARAMS],
    });

    await provider.send('eth_requestAccounts', []);
    const signer = provider.getSigner();
    const address = await signer.getAddress();

    console.log('Connected address:', address);
    return { provider, signer, address };
  } catch (err) {
    console.error('MetaMask connection failed:', err);
  }
}

Using ethers.js to interact with your smart contract

Sample code

Counter contract ABI:

Contract Interaction Example:

Last updated