web3 tutorial [05/10] - alchemy: deploying our smart contract

by parttimelarry

Deploying to a Testnet

So far we have only compiled and tested our smart contract locally. The next step is to deploy it to an Ethereum testnet. There are several testnets available to developers: Rinkeby, Kovan, Ropsten, and Goerli. These networks allow us to test our smart contract in “production-like” staging environment using test ether before we go to mainnet.

What is Alchemy?

While we could technically run our own Ethereum node, we would need to invest time and money into reliability, alerting, resource monitoring, and disaster recovery. Ain’t nobody got time for that.

We want to focus our time and attention on building our dapp. This is why we will be using Alchemy, a blockchain developer platform that provides this node infrastructure for us.

Create an Alchemy App

To get started, visit the Alchemy website and sign up for an account. Once registered, click the Create App button on your dashboard. You should see a dialog that looks like the one below.

Give your application a name and description. Under Network, select Goerli (a test network) and click Create App. This will take you to a dashboard where you can monitor your app.

Create Alchemy App

Copy Your API Key

On the application dashboard, click the View Key button and copy your key to the clipboard. We will add this key to our hardhat.config.js.

Copy API Key

Hardhat Deployment Configuration

Open your hardhat.config.js file and add the goerli test network using the format below. For the “url”, paste in the API Key URL you copied above.

module.exports = {
  solidity: "0.8.4",
  networks: {
    goerli: {
      url: "https://eth-goerli.alchemyapi.io/v2/yourapiurl",
      accounts: ["private key here"],
    },
  },
};

Under accounts, you will need to provide a private key. To export your private key from Metamask:

  1. Click the 3 dots on the top right
  2. Click Account Details
  3. Click the Export Private Key button
  4. Enter your Metamask password and Confirm.

Export Private Key

Put your private key in quotes where it says “private key here”. DO NOT commit this file to git or show it to anyone. We are only using the private key here to deploy our smart contract to the blockchain.

Alchemy does not support private keys, this key is used to sign transaction.

https://docs.alchemy.com/alchemy/resources/faq

Create a Deploy Script

When we initialized our hardhat project, it created a default deployment script. Let’s edit this to deploy our Calend3 smart contract. Open scripts/deploy.js in your text editor and change greeter to Contract / Calend3 as shown below. Feel free to use a more specific variable name than Contract/contract.

// We require the Hardhat Runtime Environment explicitly here. This is optional
// but useful for running the script in a standalone fashion through `node <script>`.
//
// When running the script with `npx hardhat run <script>` you'll find the Hardhat
// Runtime Environment's members available in the global scope.
const hre = require("hardhat");

async function main() {
  // Hardhat always runs the compile task when running scripts with its command
  // line interface.
  //
  // If this script is run directly using `node` you may want to call compile
  // manually to make sure everything is compiled
  // await hre.run('compile');

  // We get the contract to deploy
  const Contract = await hre.ethers.getContractFactory("Calend3");
  const contract = await Contract.deploy();

  await contract.deployed();

  console.log("Calend3 contract deployed to:", contract.address);
}

// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main()
  .then(() => process.exit(0))
  .catch((error) => {
    console.error(error);
    process.exit(1);
  });

Run the Deploy Script

To deploy your smart contract to your local blockchain, use hardhat to run the script without a network parameter:

npx hardhat run scripts/deploy.js

You should see a message that the contract was deployed to a specific address:

Calend3 contract deployed to: 0x5FbDB2315678afecb367f032d93F642f64180aa3

This will allow us to test our web application locally against our local blockchain.

We are going to go ahead and deploy our smart contract to the Goerli testnet. To do this, use the parameter –network and pass in the name of the test network:

npx hardhat run scripts/deploy.js --network goerli

This will take a few seconds since it is deploying the smart contract to an actual remote network. Once it is finished, you should see your smart contract’s address on the Goerli network:

Calend3 contract deployed to: 0x2e0f141E3C228D240735A4EBD8Fe07c0E3a6156B

Now that it is deployed, we can see it on the Goerli Testnet Etherscan. Here is a link to mine. Type your contract address into Goerli Etherscan. You should see a Contract Creation transaction like the one below:

Goerli Etherscan

Amazing! We just deployed a smart contract to an Ethereum testnet. In the next section, we will begin building the user interface for our dapp using React. Let’s go!