npx) version 8.5.5
npmproject (default settings are fine):
package.jsonfile for you that looks like this:
Enterto accept all the default settings and install the sample project's dependencies (
contracts- folder where your smart contracts live
scripts- folder where your hardhat javscript scripts live
withdrawscript to cash out our tips
hardhat.config.js- configuration file with settings for solidity version and deployment
constructorsaves the address of the wallet that was responsible for deploying inside an
ownervariable as a
payableaddress. This is useful for later when we want to withdraw any tips collected by the contract.
buyCoffeefunction is the most important function on the contract. It accepts two strings, a
_name, and a
_message, and it also accepts ether due to the
payablemodifier. It uses the
_messageinputs to create a
Memostruct that is stored on the blockchain.
buyCoffeefunction, they must submit some ether due to the
require(msg.value > 0)statement. The ether is then held on the contract
balanceuntil it is withdrawn.
memosarray holds all of the
Memostructs generated from coffee purchases.
NewMemolog events are emitted every time a coffee is purchased. This allows us to listen for new coffee purchases from our frontend website.
withdrawTipsis a function that anyone can call, but will only ever send money to the original deployer of the contract.
address(this).balancefetches the ether stored on the contract
owner.send(...)is the syntax for creating a send transaction with ether
require(...)statement that wraps everything is there to ensure that if there are any issues, the transaction is reverted and nothing is lost
scriptsfolder, there should be a sample script already populated
sample-script.js. Let's rename that file to
buy-coffee.jsand paste in the following code:
main()function. The commented code shows the flow of the script:
9999.99877086625ETH. This is because it started with 10k ETH as one of the pre-populated hardhat addresses, but it had to spend a tiny amount to deploy to the local blockchain.
== bought coffee ==, Address 1 purchases one coffee. Two other wallets that are not shown ALSO purchase coffees. In total, 3 coffees were purchased for a total tip amount of
3.0ETH. You can see that Address 2 (which represents the contract address), is holding on to
withdrawTips()function is called in
== withdrawTips ==, the contract goes back down to
0ETH, and the original deployer, aka Address 0, has now earned some money and is sitting on
scripts/deploy.jsthat will be super simple, just for deploying our contract to any network we choose later (we'll choose Goerli later if you haven't noticed).
deploy.jsfile should look like this:
deploy.jsscript coded and saved, if you run the following command:
networkthat the Hardhat tool uses is a local development network, right on your computer. It's fast and deterministic, and great for some quick sanity checking.
hardhat.config.jsonfile comes in.
hardhat.config.jsfile, you will see some sample deploy code. Delete that and paste this version in:
dotenvat the top of the configuration file, our entire Hardhat project will have access to those dependencies.
dotenvyet, that's an important tool we'll talk about that in a bit.
process.env.PRIVATE_KEYare how we can access environment variables to use in our config file while not exposing the secret values.
modules.exports, we are using solidity compiler version
0.8.4. Different compiler versions support different features and syntax sets, so it's important to match this version with the
pragmadeclaration at the top of our
pragma solidity ^0.8.0;. In this case, even though the numbers don't match exactly, that's okay because the karat
^symbol means that any version that's greater than or equal to
modules.exports, we define a
networkssetting that contains one test network configuration for
dotenvmodule. As its name implies,
dotenvhelps us connect a
.envfile to the rest of our project. Let's set it up.
.envfile with the variables that we need:
.gitignorethat ensures you don't accidentally push the file to version control. Make sure that
.envis listed in your
GOERLI_API_KEY- from your same Alchemy Ethereum Goerli app, you can get the last portion of the URL, and that will be your API KEY
dotenvinstalled and your
.envfile populated, we are ALMOST ready to deploy to the Goerli testnet!
Error HH8, then I highly recommend searching Google and Stack Overflow or Ethereum Stackexchange for solutions. It's common to run into those issues when something in your
.env, or your
dotenvmodule isn't set up correctly.
withdrawTips()function to pull money out from our contract balance and send it over to the owner's wallet:
--network goerliflag this time, and that's because our script hard-codes the network configuration directly inside the logic:
BuyMeACoffee.solsmart contract earlier.
Albertand replace it with your name / anon profile / ENS domain, or whatever it is you'd like for people to call you.
cmd + For
ctrl + Fto look for all instances of
npm run devto start a local server to test out your changes. The website should load in a few seconds:
Connect your wallet.
Connect your wallet, a MetaMask window will pop up asking if you want to confirm the connection by signing a message. This message signing does not require any gas fees or costs.