UV_Labs
Published in

UV_Labs

Creating and Deploying your first Smart Contract

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 Alchemy and 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 Alchemy API URL and MetaMask Private key inside hardhat.config.js.

  • For the API URL, Click on create app

Note that we will be using Ropsten network and Environment would be Development.

  • Copy the API URL with the help of the video provided below.
NOTE: Do not copy the API Key shown in the video, since it has been destroyed by the Author for security reasons.

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

We are group of Nerds, trying out different stuff on blockchain and most importantly having fun

Recommended from Medium

Yet Another Productivity Hack…

Query Processing and Optimization in Oracle

The real benefit of 100% code coverage

Migrate or copy DynamoDB table via script

Tips to Improve quality of your code review requests

Nowa: a free platform to build Flutter apps without writing code

12 Arduino Projects for Beginners — With Code

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Pari Tomar

Pari Tomar

More from Medium

Fork Uniswap v2 Smart-Contracts & UI on Remix

Deploying a Dapp to Mainnet

Interaction between Smart Contracts

Attacks on Smart Contract — Chapter 1