Stuck on ETH Donation button

Hi there, i’m getting stuck into implementing a simple ETH donation button to my website. I just want to give people the opportunity to donate a small amount of ETH to me as a nice gesture for my courses because I think ETH is booming. There are some codes shared in public, but due to the deprecation of web3 in Metamask, the code needs some rewriting. This is the old code below:


var tipButton = document.querySelector(’.tip-button’)

tipButton.addEventListener(‘click’, function() {

if (typeof web3 === ‘undefined’) {

return renderMessage(‘You need to install MetaMask to use this feature.’)

}

var user_address = web3.eth.accounts[0]

web3.eth.sendTransaction({

to: YOUR_ADDRESS,

from: user_address,

value: web3.toWei(‘1’, ‘ether’),

}, function (err, transactionHash) {

if (err) return renderMessage('Oh no!: ’ + 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!’)

})

})


Can anyone set this up into the current standard, and migrate it? I think it can be done very easily, but I keep getting stuck… Would love to see if anyone could help me out, thanks in advance! :slight_smile:

Hey @moeshrande, welcome to the MetaMask community! :fox_face:

You are correct in that Web3.js has been deprecated. This provider migration guide may help you:

2 Likes

Hi nakedwinnie, thanks for your quick reply!! Yeah I did see that provider migration guide, but I keep getting stuck… So I was hoping for someone to rewrite the code above…

Another small question: The “YOUR_ADDRESS” code above, is the wallet address where the donation should be sent to right? Not a smart contract address right?
Thanks a lot! :slight_smile:

Correct, if you are setting this up to receive crypto donations, then it can be the wallet address, such as your MetaMask wallet. However, it can also be a smart contract address, such as a multisig like https://gnosis-safe.io/, where multiple signatures are required to confirm transactions.

What you decide to be the “YOUR_ADDRESS” is ultimately up to you.

2 Likes

Thank you for the help! :slight_smile:

Could anyone else help me out with the code above? Re-writing it into the right metamask way with the migration? It would help me a lot, been struggling for 2,5 weeks now… Thanks in advance guys :slight_smile:

This topic was automatically closed after 30 days. New replies are no longer allowed.