Skip to content
This repository was archived by the owner on May 11, 2022. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,33 @@ <h1>MetaMask Tip Button Example</h1>

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('<div>You need to install <a href=“https://metmask.io“>MetaMask </a> to use this feature. <a href=“https://metmask.io“>https://metamask.io</a></div>')
}

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) {
Expand Down