Deploy a Solidity Smart Contract with Hardhat to a Local Ethereum Network

Share this video with your friends

Social Share Links

Send Tweet

Now you need a way to keep a local network alive, deploy the smart contract there and then interact with it. You can think on this like a local web server. When you develop an API you have a local server running it so the client application can talk to it so you can develop and test, this is the same idea.

Head over to your terminal and run tthe following command.

$ npm run hardhat node

This will ramp up a local ethreum network using hardhat and will show in the console a set of accounts and private keys that can be used for testing pruposes.

These are 20 test accounts and addresses created for your. Each account is also rich! are loaded with 10000 ETH. (fake eth btw).

Right now this is just an empty blockchain. There are no blocks nor smart contracts on it. But you want to create a new block and put the smart contract that you just created there, for that you need to create a little script that will use ethers.js power to compile and deploy.

Instructor: [0:00] Your smart contract is ready to be used, but first, you need to deploy it somewhere. The first step will be to run a local Ethereum network to deploy and run the smart contract to be able to interact with it. You can think about it as a local web server. [0:22] When you develop an API, you have a local server running so the client application can communicate with it. This local network works based on the same idea. Head over to your terminal and run npm run hardhat node. This command will ramp up a local Ethereum network using Hardhat framework and will output a list of private keys to use for testing purposes.

[0:54] These are 20 addresses that hold a big amount of fake coins. With this running, you can deploy your smart contract. For that, let's create a script to handle the deployment. Open your editor and create a file scripts/deploy.js. This is a JavaScript file that harnesses the power of the Hardhat framework and the ethers.js library. Let's create a main asynchronous function that will take care of the process.

[1:32] First, get the deployer address, get the contract reference, call the deploy function, and wait to get the contract deployed. Finally, create a run function, and call it. Now, go back to a terminal window and run npm run hardhat-deploy. Make sure to run this command from the root of your project without stopping the local network.

[2:08] After the script is done, the contract will be deployed to the network. This deployment uses the first test account from the local network as the owner of the contract.

[2:22] The console will show you some information where the most important piece is the address of your contract that can be seen here. You can think of this address like your API endpoint. Keep this address safe, since you'll use it later for the website.

[2:41] In this lesson, you learned and how to deploy the smart contract to this network.