0

I'm trying to implement a really basic and simple metamask payment in reactjs. This is what i done, handleClick is triggered by a button:

handleClick = () => { 
        let web3 = new Web3(); 
        window.web3 = new Web3(web3.currentProvider);
        web3.eth.sendTransaction({
            to: '0xE08aa75AAE695c4622Cd430FbeBF4B97689d4Ee3',
            from: '0xf59F88E6eA4A937e228E4aaf378e96EDfb646B14',
            value: web3.utils.toWei('1', 'ether'), 
        })      
  };

I didn't insert any check if metamask is there because i want to try the most simple thing possibile to better understand web3js and reactjs. I don't get any error but nothing happens

1

I guess the problem there is that you are overriding the injected web3 instance.

The variable web3 should be injected by MetaMask so with your second line of code you override it, instead you should create a new variable and set the provider using injected web3.

Try this:

handleClick = () => { 
        let web3js = new Web3(window.web3.currentProvider); 
        web3js.eth.sendTransaction({
            to: '0xE08aa75AAE695c4622Cd430FbeBF4B97689d4Ee3',
            from: '0xf59F88E6eA4A937e228E4aaf378e96EDfb646B14',
            value: web3js.utils.toWei('1', 'ether'), 
        })      
  };
  • Thank for your answer, but if i don't add let web3 = new Web3(); i get a 'web3' is not defined error. (web3js imported with import Web3 from 'web3'; – milonite May 14 '18 at 10:31
  • Ah sorry - can you try to do this: let web3js = new Web3(window.web3.currentProvider) – Kai Kälberer May 14 '18 at 10:34
  • many thanks, now works! this is my final code: let web3 = new Web3(); let web3js = new Web3(window.web3.currentProvider) web3js.eth.sendTransaction({ to: '0xE08aa75AAE695c4622Cd430FbeBF4B97689d4Ee3', from: '0xf59F88E6eA4A937e228E4aaf378e96EDfb646B14', value: web3.utils.toWei('1', 'ether'), – milonite May 14 '18 at 10:38
  • I also updated my answer, you can get rid of first let web3 part :) - code should work exactly as in my answer (hopefully :)) – Kai Kälberer May 14 '18 at 10:40
  • nope without let let web3 = new Web3(); it gives me 'web3' is not defined error anyway – milonite May 14 '18 at 10:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.