Building a full-stack Decentralised Application on the Ethereum blockchain
Part Four: Deploying the smart contract to a live Ethereum network
This blog series is broken down into five parts:
Deploying our smart contract locally is a great way to test our smart contract, see it working within a predictable environment and deploy quickly and easily. But deploying onto a live Ethereum network is what is needed to actually get your smart contract out in the world and be able to test, interact and utilise your smart contract within a realistic environment.
We’re going to launch our smart contract on the Rinkeby network which is a live Ethereum test network. To do this, we’re going to need to create an Ethereum wallet for ourselves and get some Eth to use on the Rinkeby network (this is free, don’t worry). We’re then going to sign up to Alchemy and use their Ethereum API to easily deploy our smart contract. We’ll also need to make a few changes to our Hardhat config file to specify the network we want to deploy to, and the private key of our wallet to fund the deployment.
Creating a MetaMask wallet
MetaMask is an Ethereum wallet that operates within your browser, it’s very simple to use and has great utility when interacting with Web3 websites such as decentralised exchanges and the Ethereum Naming Service (ENS). Head over to the MetaMask website, install the Google Chrome extension (you’ll have to use Chrome for MetaMask) and follow the steps to set up a wallet for yourself. I’d also recommend you do some reading at this stage to understand how an Ethereum wallet works and how to access your wallet’s address and private key as we’ll need those soon. It’s also important to understand the difference between the main Ethereum chain and the test networks such as Rinkeby. Here’s a good guide.
Getting some Eth from a faucet
Now we have a wallet set up, we’ll need to get ourselves some Eth. A faucet is a website that gives out free cryptocurrency, that’s where we’ll head to now. Go to the Chainlink Rinkeby faucet, enter your address, follow the rest of the prompts and submit your request for some free Eth. Once the request has been submitted successfully it will take about 30-60 seconds for the balance to pop up in your MetaMask wallet. You will need to make sure that your MetaMask wallet is showing your balance on the Rinkeby network and not the Ethereum mainnet which is what it will be showing by default. To switch networks, simply click on the network name at the top of your wallet and a dropdown list of the available networks will appear. You may also need to click the ‘show/hide test networks’ toggle to show the Rinkeby Network option here. Your wallet should at this stage look something like this:
Signing up to Alchemy
The next step to deploying our smart contract is to create a free account on Alchemy to gain access to their Ethereum API which we will use to easily deploy our smart contract to a live network. Head to their site, start the account creation process and when asked what ecosystem you’ll be working on, select Ethereum. You’ll also be prompted to initialise a project as part of the account creation process so feel free to provide any team name and app name you like, but select ‘Rinkeby’ as the network.
Updating our Hardhat config file
Now that we have our MetaMask wallet set up with some Eth and an Alchemy account, we need to update our Hardhat config file. First, we have to add our Alchemy API key so that we can make a request to the Alchemy API to deploy our smart contract and, secondly, we’ll need to provide our private key to our Ethereum wallet so that as part of the deployment we can pay the necessary gas fees.
You’ll be able to grab the Alchemy link you require with your API key already embedded into it from the dashboard page of the app you created by clicking the ‘VIEW KEY’ button in the top right of the screen.
Your MetaMask wallet private key can be obtained by clicking the 3 dots in the top right corner of your wallet, then selecting ‘Account details’. From there click ‘Export private key’, enter your password and voila! There it is.
Notes on private keys
It’s important for me to state here - in case you’ve not looked into how crypto wallets work too much - your private key is what gives access and control to your wallet. DO NOT share this with anyone or post it online. What I’ve done whilst building this blog post is create an entirely separate wallet account for development purposes that is separate from my personal funds. This ensures that if the private key from my development account was leaked, the only thing an attacker would gain access to is some worthless Eth on test networks and not my personal mainnet funds that sit on my other account. It’s fortunate that I did take this step as an issue with my .gitignore file caused my hardhat.config.js file to be uploaded to my public GitHUb repo with my private key in it for an hour or so. I’m sure someone has built a bot that trawles GitHub for wallet private keys by now and that account may now be compromised.
Set up your hardhat.config.js file so it looks like mine below, by inserting your MetaMask private key and the URL from Alchemy containing your API key.
require("@nomiclabs/hardhat-waffle"); require("solidity-coverage"); // Replace this private key with your Ropsten account private key // To export your private key from Metamask, open Metamask and // go to Account Details > Export Private Key // Be aware of NEVER putting real Ether into testing accounts const METAMASK_PRIVATE_KEY = "YOUR_METAMASK_WALLET_PRIVATE_KEY_HERE"; /** * @type import('hardhat/config').HardhatUserConfig */ module.exports = { solidity: "0.8.7", networks: { rinkeby: { url: `https://eth-rinkeby.alchemyapi.io/v2/YOUR_API_KEY_WILL_BE_HERE`, accounts: [`${METAMASK_PRIVATE_KEY}`] } } };
Deploying our smart contract and seeing it live in the world
Now that we’ve added the extra information required for deployment to our hardhat config file, we can run our deploy script again, but this time specifying that we want to deploy to the Rinkeby network, like this:
npx hardhat run scripts/deployInbox.js --network rinkeby
We can also add this command to our package.json file if we like, as the script “deploy:rinkeby” to make our life easier.
After running this command you should see an output similar to the following:
The inbox smart contract deployed to: 0x8d8671021Ea191Bf2523fEb915dd5fBC3f08b88a
Copy the contract address from the output and open https://rinkeby.etherscan.io/ in your browser. Etherscan is a blockchain explorer and shows us all blocks, transactions, tokens and smart contracts on the Ethereum network. This particular version of etherscan is focused on the Rinkeby test network. From there copy the contract address into the search box. You should then see a screen resembling the following:
This screen show us our smart contract on the Rinkeby network, and it gives us a lot of information about the smart contract, including:
Who created the smart contract (their wallet address)
When it was created (Age & the transaction hash from the block that created it)
The transaction fee that was paid to create the smart contract
How much Eth is sitting in the wallet attached to the smart contract
And BOOM! We have built, tested and deployed our very own smart contract onto a live Ethereum network. I hope this blog has been helpful to you and I encourage you to keep learning and building as this really is a very exciting space to be in right now. Keep an eye out for my next blog series where I’ll be building a web application to interact with a smart contract.