automa icon indicating copy to clipboard operation
automa copied to clipboard

Interact with metamask wallet extension

Open zerominh opened this issue 1 year ago • 1 comments

Is your feature request related to a problem? Please describe. How to interact with meta mask wallet extension

zerominh avatar Mar 28 '24 06:03 zerominh

Yes, Automa can interact with the MetaMask extension by using MetaMask's Ethereum Provider (window.ethereum). MetaMask injects an Ethereum provider object into the window object of web pages. This object allows web pages (and, by extension, Chrome extensions that interact with those pages) to communicate with MetaMask.

Here’s an example of how to request access to a MetaMask wallet:

// Ensure MetaMask is installed and window.ethereum is available
if (typeof window.ethereum !== 'undefined') {
  console.log('MetaMask is installed!');

  // Request account access
  window.ethereum.request({ method: 'eth_requestAccounts' })
    .then(accounts => {
      const senderAddress = accounts[0]; // The first account in MetaMask
      console.log('Connected account:', senderAddress);

      // Transaction details
      const transactionParams = {
        to: '0x2163b3423fB512F5CFa14B6aFC0c6084908711b6', // Replace with the receiver's address
        from: senderAddress, // The user's MetaMask account
        value: '0xDE0B6B3A7640000', // Amount to send in wei (1 ETH)
        gas: '0x5208', // Gas limit (21000 for a basic transfer)
        gasPrice: '0x09184e72a000', // Gas price in wei (example: 100 Gwei)
      };

      // Send the transaction
      return window.ethereum.request({
        method: 'eth_sendTransaction',
        params: [transactionParams],
      });
    })
    .then(txHash => {
      console.log('Transaction successful with hash:', txHash);
    })
    .catch(error => {
      console.error('Error occurred:', error);
    });
} else {
  console.log('MetaMask is not installed!');
}

automaNextBlock();
image

jingbof avatar Sep 04 '24 02:09 jingbof