Problem
I am following this tutorial on how to make a donate button on my React app. The tutorial is in HTML, however, so below is my attempt at converting it to ReactJS.
Code
App.jsx
donate = () => {
const donationAddress = "0x...";
web3.eth.sendTransaction({
to: donationAddress,
value: web3.utils.toWei("1", "ether")
}, (err, transactionId) => {
if (err) {
console.log("Donation failed", err);
alert("Donation failed.");
} else {
console.log("Donation successful", transactionId);
alert("Donation successful!");
}
})
}
render() {
return (
<div>
<p>Donate</p>
<button onClick={this.donate}>Donate 1 wei</button>
</div>
);
}
I commented out the two lines of code above from the tutorial because I had received this error: TypeError: web3__WEBPACK_IMPORTED_MODULE_3__.default.toWei is not a function
.
Output
Initially, I don't receive an error on the page until I click the button. Then I receive this error: The send transactions "from" field must be defined!
.
It highlights this line as the error: web3.eth.sendTransaction({
.
How do I fix this? Thank you!
accounts.length == 0
henceaccounts[0] === undefined
?web3.toWei
is on web3.js v0.x. On web3.js v1.x, it is functionweb3.utils.toWei
.utils
in it and that error disappeared. However, I did get a different error.Please pass numbers as strings or BN objects to avoid precision errors.