Creating and Deploying your first Smart Contract

Pari Tomar
BuildBear Labs
Published in
6 min readMar 28, 2022

--

There are a lot of people out there who are exploring web3 and blockchain. A lot of people have reached out to us asking for a quick tutorial for a Hello World Smart Contract. This article is for our friends in such Beginner basket and if you are a Beginner too; this is MUST READ for you too.

By the end of this article, we will understand how can we code our own smart contract using Solidity, deploy our smart contract on the Ropsten testnet, and interact with it.

As mentioned above, this is a beginners guide. This will be our Part 1 to a small series of Smart Contract Work. We will be doing another part “Part 2” on coding a NFT Smart Contract. If you like our work, please clap and share for us to know that our content is relevant to you :)

Tools we will be using:

  • Node Provider: Node Provider is a tool that makes it very easy to get your smart contract from your local development into the blockchain.

There are a number of node providers out there like Alchemy, Infura, and Quicknode.

For our smart contract, we are going to use Alchemy, as it also provides a free tier that allows us to make a certain number of requests each day and is easy to use.

  • MetaMask: MetaMask is a virtual wallet that allows us to interact with our blockchain. It consists of the main network and various test networks like Ropsten, Rinkeby, and Kovan.

We are able to test our smart contract using the MetaMask and can deploy it to our blockchain.

  • Hardhat: Hardhat is a free development tool used for the purpose of testing and deploying our smart contract.

A popular alternative to Hardhat is Truffle. If you’re familiar with truffle, try checking out this video.

Make sure before going any further, you should create an account on MetaMask, which we will be going to use later in your project.

1. Initialize the project

To begin writing our first smart contract, open your terminal shell, make a directory and then initialize it using the npm init command.

You should be able to see a new file created “package.json”

Next, we are going to install Hardhat as a dev-dependency inside our hello-world directory. [to learn more about Hardhat, click here]

npm install --save-dev hardhat

Once Hardhat is installed, we will initialize it by using the command.

npx hardhat

Hardhat provides us with various options, either we could start from scratch, writing own smart contract and scripts or we could use a sample project to start with.

Create an empty hardhat.config.js file which we will be customizing on our own.

  • Create contracts and scripts folder, which will contain our smart contract and deploy script.
mkdir contracts
mkdir scripts

Our folder structure might look like:

2. Write our Smart Contract

Navigate to the ‘contracts’ folder and create a file helloworld.sol.

Below is a sample Hello World smart contract from the Ethereum Foundation that we will be using for this tutorial.

// SPDX-License-Identifier: MIT

pragma solidity >=0.8.0;
// Defines a contract named `HelloWorld`.
contract HelloWorld {
event UpdatedMessages(string oldStr, string newStr);

string public message;
constructor(string memory initMessage) {
message = initMessage;
}
function update(string memory newMessage) public {
string memory oldMsg = message;
message = newMessage;
emit UpdatedMessages(oldMsg, newMessage);
}
}

This smart contract stores a message when created and the message can be updated using the update function.

If you’re new to solidity, try learning through their documentation.

3. Customize the hardhat.config.js file

We will integrate our BuildBear API URL and MetaMask Private key inside hardhat.config.js.

  • For the API URL, Click on create an endpoint

Then click on the submit button which you can find at the end of the page, as we are going to use the default node.

  • Copy the API URL with the help of the video provided below.

Follow these instructions to get funds from the faucet in order to perform transactions.

Then Follow these instructions to export your private key

We are going to use .env file to store the API URL and MetaMask Private Key.

Make sure you install dotenv in your project locally.

npm install dotenv --save

Create a .env file and store your API URL and Private Key in it.

  • Install Ether.js library which makes it easier to interact which the Ethereum blockchain.
npm install --save-dev @nomiclabs/hardhat-ethers "ethers@^5.0.0"

Inside the hardhat.config.js file, we will be using all these plugins and keys.

The updated file might look like:

4. Deploy our smart contract

To check whether our smart contract works just fine, we will be compiling it using the terminal shell and running the compile command*.*

npx hardhat compile
  • Since now our smart contract and its configuration are good to go, we will be writing a deploy script which is mandatory in order to deploy our smart contract.

Create a file inside scripts > deploy.js

deploy.js:

async function main() {

const HelloWorld = await ethers.getContractFactory("helloWorld");


const helloWorld = await HelloWorld.deploy("Hello World!");
console.log("Contract deployed to address:", helloWorld.address);
}

main()
.then(() => process.exit(0))
.catch(error => {
console.error(error);
process.exit(1);
});

Here, we are making an instance of our hello world smart contract and using the deploy() function to deploy it on the blockchain.

Finally, we are going to deploy our smart contract!

In order to do so, run the following command in your CLI.

npx run scripts/deploy.js --network ropsten

We have provided a flag network in order to use the ropsten network, if we do not provide the network, the smart contract will be deployed on the hardhat network, by default.

Congratulations! You have deployed your first smart contract.

You may see the results like:

To learn more about your deployed smart contract, open Ropsten Etherscan.

Here, you can see the information regarding your transaction.

You can check the Timestamp of the contract deployment, the gas price used, and much more information.

In order to see under the hood, go through the Alchemy Dashboard, where you might see a bunch of JSON-RPC calls made by our smart contract.

That’s all for part-1 of this tutorial. In further parts, we will see how can we mint our own ‘NFT’. If you like our work, please clap and share for us to know that our content is relevant to you :)

And feel free to share your first experience with us on writing your first smart contract.

Authors (open to feedback): Amateur-Dev and Pari Tomar

Reference used: https://youtu.be/g73EGNKatDw

--

--

Pari Tomar
BuildBear Labs

Researches, talks, shares know-how on building a career in blockchain space