From 26d3e27544424841f53e282a5e428a76bcd001ad Mon Sep 17 00:00:00 2001 From: Aaron Surty Date: Wed, 6 Oct 2021 12:32:44 -0400 Subject: [PATCH] metamask no longer injects web3 so using ethereum instead closes #8 --- index.html | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/index.html b/index.html index da228b6..0669df6 100644 --- a/index.html +++ b/index.html @@ -39,26 +39,33 @@

MetaMask Tip Button Example

var tipButton = document.querySelector('.tip-button') -tipButton.addEventListener('click', function() { +tipButton.addEventListener('click', async function() { - if (typeof web3 === 'undefined') { + if (typeof ethereum === 'undefined') { return renderMessage('
You need to install MetaMask to use this feature. https://metamask.io
') } - var user_address = web3.eth.accounts[0] + const accounts = await ethereum.request({method:'eth_requestAccounts'}) + var user_address = accounts[0] + + try { + const transactionHash = await ethereum.request({ + method: 'eth_sendTransaction', + params: [ + { + 'to': MY_ADDRESS, + 'from': user_address, + 'value': '0x11c37937e08000', + }, + ], + }) + // Handle the result + console.log(transactionHash) + } catch (error) { + console.error(error) + } - web3.eth.sendTransaction({ - to: MY_ADDRESS, - from: user_address, - value: web3.toWei('1', 'ether'), - }, function (err, transactionHash) { - if (err) return renderMessage('There was a problem!: ' + err.message) - // If you get a transactionHash, you can assume it was sent, - // or if you want to guarantee it was received, you can poll - // for that transaction to be mined first. - renderMessage('Thanks for the generosity!!') - }) }) function renderMessage (message) {