Metamask wallet call

Could you pleeeeeeeease add the functionality to call the users metamask extension and retrieve the wallet address. My website right now relies on the user to input their address, but the problem is that the user could input an address that is not theirs.

Couldn’t you grab this with a Javascript function?

Am I correct in my understanding that the metamask extension you are talking about refers to a crypto wallet?

@robert Yes, i am referring to the users crypto wallet address

Yes, you might be able to, I am just not sure how to go about prompting web3 to make the metamask wallet extension pop up, so i can connect to the site.

I believe its something along the lines of:
const Web3 = require(“web3”);
const ethEnabled = async () => {
if (window.ethereum) {
await window.ethereum.send(‘eth_requestAccounts’);
window.web3 = new Web3(window.ethereum);
return true;
}
return false;
}

@robert

More documentation:
I tried to insert the js, then call it in the module. But I can’t seem to get it working.

python code in main module

import anvil.js
from anvil.js.window import getaddress



class Form1(Form1Template):
  
  
  
  @staticmethod
  def getaddy():
      status = getaddress()
      print(status)
      return status
      

js code in native library:

<script>
  
  function getaddress(){
  	const Web3 = require("web3");
	const ethEnabled = async () => {
  		if (window.ethereum) {
    	await window.ethereum.send('eth_requestAccounts');
    	window.web3 = new Web3(window.ethereum);
        var accounts = await web3.eth.getAccounts();
          return accounts 
   	 	return true;
  	}
  	return false;
	}
    }    
</script>
  
    

Try calling your getaddy function on form_show. That is when JS will actually be called.

We’re unlikely to add a python api for metamask anytime soon.
The best approach is to use anvil.js as you’ve started doing.

More context would be useful in helping out here.
e.g.
Where did you get your code snippet from?
What error are you getting when you execute the snippet?

As a quick tip - anytime you see the word require in a javascript code snippet - then it won’t work in the browser.

When looking for a js library a good place to go is the github/documentation page.
There is a cdn link in the github page for web3

Which means we can add a script in Native libaries

<script src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"></script>

The docs say that this adds Web3 to window so you can then remove the line const Web3 = require("web3"); from your script.

Javascript is messy sometimes.

1 Like

Only on days with a ‘y’ in their name.

3 Likes

Great Stuff, thanks for sharing the content