Let’s run through a simple “hello world” example to create and run a simple smart contract on Ethereum. It is amazing how much a simple example can teach you.
You will need the following setup done. At each step, I would recommend you spend some time to review the documentation on the respective product pages to get a little bit deeper with them (deep enough that you know the intent of the tool, install it and know where it fits in the hello world),
Install Metamask – Similar to the real world where one may have a physical wallet to store assets (cash or credit cards), we need a digital wallet to transact on the blockchain. Metamask is a popular Ethereum digital wallet that serves that purpose for the Ethereum blockchain. It allows you to buy & sell assets on the blockchain, and store tokens (eth coins) and other assets (NFTs). Your wallet will have a unique ID that you can use to refer to it when transacting on the blockchain. Metamask installs as a browser extension. I used the Brave browser, but Chrome should work. If you use Coinbase or similar applications, you are trusting the broker/exchange to store all your assets (and keys). When using a personal digital wallet such as Metamask, you are in full control. That also means you better have backups of the wallet in a secure location. If you lose the wallet you are not getting back your assets — it is gone. Here is a good read on Metamassk from Hitesh Sant – https://geekflare.com/finance/beginners-guide-to-metamask/
The Network – Since Ethereum is a protocol there can be multiple implementations of it. The Mainnet network is where real-world transactions live. But for testing, we will use the test network called Ropsten. You will need some test eth tokens (crypto) to test. The ether tokens in these test networks do not have real-world value. You can request test tokens (eth) by using a Faucet tool such as https://faucet.dimensions.network/
Use Metamask to create an Ethereum account against the test network. You can create multiple accounts and use them for different purposes. For example Acct1 against Ropsten to do testing, another one against the Maninet to buy NFTs on OpenSea, another one against Mainnet for holding onto crypto assets, etc. For our purposes, you will need the account address to load test tokens via a faucet. Give a working test faucet the account address and it will deposit some test tokens in a few minutes.
Programming Smart Contracts – Solidity is a statically typed, high-level object-oriented programming language used to code the smart contracts that will run on the Ethereum blockchain. You can install Solidity support on many development IDEs. I chose to install a plugin to VS Code. Our hello world is really simple in that a new message gets written to the blockchain every time it is run (after the necessary consensus is achieved). For this getting started code, you do not need to get deeper into the language.
Dev Tool with Hardhat – Hardhat provides a development environment to compile, debug, test and deploy your software locally. Similar to tools like Maven, Gradle or Gulp it automates many common development tasks. In addition, Hardhat provides a local Ethereum network designed for development use. When you are ready to deploy to the Ropsten test network you can do that using the CLI commands.
Connecting to Ethereum – Next, we need to decide how we connect to the Ropsten Ethereum chain. We could run a full node setup, but most of us cannot do this. We will instead use Alchemy a blockchain developer platform. Alchemy allows us to communicate with the Ethereum chain without having to run our own nodes. Set up an account on Alchemy and get hold of the API key.
Put the API key in a .env file (npm install dotenv –save). Go ahead and create a .env file that will look like this…
API_URL = "https://eth-ropsten.alchemyapi.io/v2/your-api-key"
PRIVATE_KEY = "your-metamask-private-key"
Do not check this file into your source code repo. The best way is to add the file to .gitignore
To run the app
# compile the code
npx hardhat compile
@ test the code
npx hardhat test
To deploy the Smart Contract to the Ropsten test Ethereum network
npx hardhat run scripts/deploy.js --network ropsten
npx hardhat run scripts/interact.js
The super simple hello world is at Github at https://github.com/thomasma/ethblkchainhello