Introducing Ethereum Development - Part 1 - MetaMask and Web3
In this series, we will introduce building simple Ethereum projects by following the fate of Alice the author.
Alice is a writer who wants to go freelance and escape the drudgery of her day job. She’s fed up with middlemen taking a cut of her earnings and wants to explore new technology. She has experience in web and is super happy to discover she can use a basic web stack to create interactive contracts on Ethereum Platform and get paid directly.
After a quick recce of dev tools and software, she decides to use MetaMask to enable creation of an embeddable donation button to use on her upcoming blog posts. She’s hoping Bob the benefactor and people like him will help support her work.
web3.eth.sendTransaction() can be used to send messages and data but here we're just sending ether. It is possible to add a specific amount of Ether to cover the cost of making the transaction (gas price) and by excluding this value we allow MetaMask to make this decision for us. In the callback, we wait for a transaction receipt and which we can use to track the transaction via Etherscan
to: '0xE767aEB31dAAF66366999F72FB5De2CEEA76c277': Send this to Alices account
from: web3.eth.coinbase: Send this transaction from the current default authorised account.
toWei(): Our input field is expecting values set in Ether but transactions on the network itself are measured in a smaller unit called Wei. 1 Ether equals 1,000,000,000,000,000,000 Wei.
Return object: If the callback succeeds we get a hash of the pending transaction which we use to generate a link on the Testnet blockchain explorer. Here, we can watch the progress of our transaction as it gets mined. If our the transaction fails, we get an error object.
Here's a working example of the form.