How do I prompt a transaction to MetaMask using web3/truffle? Like if I want a charge a user x amount of ether, and have them confirm the transaction via MetaMask.


Be sure you've set the right provider (i.e. web3 = new Web3(web3.currentProvider) to use MetaMask's injected provider), and then just make the transaction: web3.eth.sendTransaction(...).

| improve this answer | |
  • On my local net, there was no authorization used. – quantumpotato Jan 15 '18 at 22:30

Look out for this update.

window.addEventListener('load', async () => {
// Modern dapp browsers...
if (window.ethereum) {
    window.web3 = new Web3(ethereum);
    try {
        // Request account access if needed
        await ethereum.enable();
        // Acccounts now exposed
        web3.eth.sendTransaction({/* ... */});
    } catch (error) {
        // User denied account access...
// Legacy dapp browsers...
else if (window.web3) {
    window.web3 = new Web3(web3.currentProvider);
    // Acccounts always exposed
    web3.eth.sendTransaction({/* ... */});
// Non-dapp browsers...
else {
    console.log('Non-Ethereum browser detected. You should consider trying MetaMask!');

See this article for more on this update: https://medium.com/metamask/https-medium-com-metamask-breaking-change-injecting-web3-7722797916a8

| improve this answer | |

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.