DEV Community

Cover image for Deploying a Smart Contract With Hardhat and Alchemy on an Ethereum Network

Posted on


Deploying a Smart Contract With Hardhat and Alchemy on an Ethereum Network

So, you've written your smart contract and maybe even played around with it a little on remix. But now you want to share it with all your friends and possibly even the world so that they can marvel at the wonder you've just created.

I personally write my contracts on Remix IDM, compile it, deploy on the javascript machine, play around with it a little, and test out the functions.

But Remix could be unreliable sometimes and you might want a more elaborate way of writing your contracts, including test and deployment scripts. For that, I personally like to use Hardhat and Alchemy. They are very easy to setup and use, but could be a little bit of a hassle if you have not used them before.

Because of that, i'll be sharing the documented steps that I have been using to get my dApps out there through Hardhat and Alchemy.

Alchemy Helps us broadcast our transaction on the blockchain when we deploy our contract. So, it is kind of like telling all the other nodes "hey everyone! A new contract has just been deployed and it's over here!!"

To get started with Alchemy, just head on over to their website and create an account with them.

After you have done that and have gotten logged in to your dashboard, click on Create App button. It's near the top to your right. Alchemy dashboard

A pop up will come up and you can go ahead and enter the name of the app you want to create, and add some description of the application. This is for you, by the way. To help you remember what the application is about.

For the environment, I usually leave it as development. You can select the chain you'd be working on. I've personally worked with ethereum and polygon. The last dropdown is the network selection. You can either choose the main net of your network(where real money is paid and used, by the way) or choose from a test net(where it's free and fake money is used).

choosing your environment on alchemy

Next, copy the Key to your app and save it somewhere, we're going to need it later. You want to copy the HTTP option for this.

Alchemy API keys

Now that we're done with Alchemy, open up your terminal and create a new directory with your project name, then cd into the directory.

In your project directory, run the following command;

npm init -y
Enter fullscreen mode Exit fullscreen mode

You'll need npm installed for that to work, by the way. You can learn about installing npm here

Then run

npm install --save-dev @nomiclabs/hardhat-waffle ethereum-waffle chai @nomiclabs/hardhat-ethers ethers
Enter fullscreen mode Exit fullscreen mode

To make sure that everything is running just fine, go ahead and run

npx hardhat compile
npx hardhat test
Enter fullscreen mode Exit fullscreen mode

It should show you that your test has passed

When you check the contracts, test, and scripts folder you'd find some test files already there. You can go on and delete those. But don't delete the whole folders please, just the files inside of them.

Create a new file in your contracts folder and name it with the name of the contract you want to create. This is where you will write your contract code in solidity. So, you can just go ahead and do that. Alternatively, you could also copy your code from somewhere and paste it here. I usually first try out my contract code in Remix before transferring it here.

When you are done writing your contract, the next thing you'll have to do is write a 'deploy' script in your scripts folder. You can name the file 'deploy.js' This is what we are going to use to deploy your contract and get it on the blockchain for the world to see 🏄‍♂️

Here's an example of a simple contract script i've written

smart contract file

And the deployment script here 👇

deployment script

The last thing you'd need to do before deploying your contract is going to your 'hardhat.config.js' file and change it to this


module.exports = {
  solidity: "0.8.0",
  networks: {
    rinkeby: {
      url: "YOUR_ALCHEMY_API_URL",
      accounts: ["YOUR_PRIVATE_ACCOUNT_KEY"]
Enter fullscreen mode Exit fullscreen mode

Edit the file and add your alchemy url for the app. Also you'll need to add your private account key.

Finally, deploy your application to the blockchain by running this on your terminal 👇

npx hardhat run scripts/deploy.js --network {your blockchain network}
Enter fullscreen mode Exit fullscreen mode

Note: you need to replace "{your blockchain network}" with the actual name of your network. So, if i'm deploying to rinkeby for example, it will look like this.

npx hardhat run scripts/deploy.js --network rinkeby
Enter fullscreen mode Exit fullscreen mode

That's it!!! you've successfully deployed your app 🎉 🎉 🎊

Top comments (0)

An Animated Guide to Node.js Event Loop

>> Check out this classic DEV post <<