5

Metamasks injected web3 gives an error when using web3.eth.sendTransaction()

web3.eth.sendTransaction({from:web3.eth.accounts[0], to:'', data: // hash to interact with contraact, gas:85000});

enter image description here

What would be a way to use Metamask to have a website prompt a user to sign a transaction ?

7

You're missing the last argument to sendTransaction, the callback function(error, transactionHash) { ... }

The MetaMask FAQ states:

All Async - Think of MetaMask as a light client

The user does not have the full blockchain on their machine and so data lookups can be a little slow. For this reason, we are unable to support most synchronous methods. The exception to this is:

eth_accounts (web3.eth.accounts)

eth_coinbase (web3.eth.coinbase)

Usually a method call can be made async by simply adding a callback as the last argument to a synchronous method.

See ethereum wiki on "using callbacks"

Not only is this a technical limitation, it's also a user experience issue. When you use synchronous calls, you block the user's interface, and so it's a generally bad practice anyways. Think of this API restriction as a gift to your users.

So all calls to web3.js should have a callback as their last argument: the only calls where that's not needed are for web3.eth.accounts and web3.eth.coinbase. The generic form of the callback is function(error, result).

  • Useful about those two commands. I'm looking in to using deasync – Eamorr Jun 3 '17 at 0:58
  • There is no clear guide to how to successfully get this to work anywhere. Code of a properly formatted button that works with MetaMask would be useful. It's poorly documented. – TrumpPaiPence Jan 13 '18 at 11:28
  • @TrumpPaiPence please try looking for and contacting MetaMask support. – eth Jan 16 '18 at 4:58
0
var receiver = "0x541209bd9C60cDb11A5076b785ba1BD44cd15768";  
var sender = web3.eth.accounts[0];
web3.eth.sendTransaction({to:receiver,
                        from:sender, 
                       value:web3.toWei("0.5", "ether")}
                        ,function (err, res){});

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.