The next article relies on Adam’s CharmingData channel and GitHub repository. ♥️
On this tutorial, we’ll create, take a look at, and deploy a wise contract referred to as SimpleStorage
. This contract will permit your customers to create their very own sentence and retailer it on the blockchain. It’s going to additionally permit customers to retrieve the final sentence saved and show it on the web page.

Think about the next Solidity contract:
// SPDX-License-Identifier: MIT pragma solidity ^0.8.18; contract SimpleStorage { // Outline a string referred to as storeTheSentence. This can be a state variable, which might be completely saved within the blockchain. string storeTheSentence; constructor(string reminiscence initialSentence) { // Assign the worth of initialSentence to the storeTheSentence variable storeTheSentence = initialSentence; } // Declares a operate referred to as getSentence // Solidity funcions visibility: public, personal, inner and exterior // Solidity view capabilities: https://www.geeksforgeeks.org/solidity-view-and-pure-functions/ operate getSentence() public view returns (string reminiscence) { return storeTheSentence; } // Declare a operate referred to as setSentence // The operate takes 1 enter parameter, a string named newSentence, with the calldata knowledge location within the Ethereum Digital Machine operate setSentence(string calldata newSentence) exterior { // Assign the worth of newSentence to the storeTheSentence variable storeTheSentence = newSentence; } }
This can be a easy sensible contract written in Solidity, the preferred programming language for implementing sensible contracts on the Ethereum blockchain. The contract’s identify is SimpleStorage
, and it primarily focuses on storing and retrieving a string (a sequence of characters).
Right here’s a short breakdown of the contract:
- State variable (
storeTheSentence
): This can be a string variable that can retailer a sentence. This variable’s worth is saved on the blockchain, making it a everlasting, unerasable report. - Constructor operate: When the contract is first deployed to the Ethereum blockchain, the constructor operate is executed as soon as. This operate accepts one argument, a string (
initialSentence
), and assigns its worth to the state variablestoreTheSentence
. getSentence()
operate: This operate is apublic view
operate, which suggests anybody can name it to learn the worth ofstoreTheSentence
with out altering any knowledge or state within the contract. It doesn’t price any fuel to name this operate as a result of it doesn’t change the state of the blockchain.setSentence()
operate: This operate is said asexterior
, that means it might probably solely be referred to as from outdoors the contract. It accepts one string argument (newSentence
) and assigns its worth tostoreTheSentence
, successfully updating the saved sentence. As this operate modifies the state of the contract, it requires a transaction to be despatched and can price fuel to execute.
In essence, this contract permits anybody to retailer a sentence on the Ethereum blockchain and retrieve it. Because of the everlasting and public nature of blockchain storage, the saved sentence can be seen to anybody and can’t be erased or modified as soon as set (besides by calling the setSentence
operate once more).
Let’s learn to set it up in a step-by-step method subsequent. 👇
Backend
- Open the terminal (command line), create two new folders referred to as
simple-storage
andbackend
. Go into thebackend
folder.
mkdir simple-storage cd simple-storage mkdir backend cd backend
- Arrange a Hardhat venture. Set up the required libraries, by typing within the terminal:
npm init --yes npm set up --save-dev --save-exact hardhat@2.14.0 npm set up dotenv
- This library is likely to be needed to put in as effectively, particularly should you’re a Home windows person:
npm set up --save-dev @nomicfoundation/hardhat-toolbox@2
- Ensure you are nonetheless within the
backend
listing. Now, create a pattern contract venture by typing within the terminal:
npx hardhat
- Choose
Create a Javascript Mission
. Don’t change something within the Hardhat venture root, simply click on enter. Sure, add a.gitignore
.
- Open the
backend
folder and take away any current contracts contained in thecontracts
folder. Insert yourSimpleStorage
contract. - Now, contained in the
scripts
folder, substitute the contect of thedeploy.js
file with the deploy.js code from this venture. - Create a
.env
file contained in thebackend
folder. This might be used to retailer your pockets key and your Quicknode endpoint. A QuickNode API endpoint offers you fast entry to a community of nodes.
- Add these two traces contained in the
.env
file, and replace the content material contained in the citation marks:
QUICKNODE_HTTP_URL="your-quicknode-http-provider-goes-here-inside-the-quotation-marks" PRIVATE_KEY="your-wallet-private-key-goes-here-inside-the-quotation-marks"
- Open the
hardhat.config.js
file contained in thebackend
folder and substitute its content material with the hardhat.config.js code from this venture. - Compile your Contract by going again to your terminal (guarantee you might be within the
backend
listing). And sort:
npx hardhat compile
- Each time you modify your contract (
SimpleStorage
), you’ll have to repeat the above compile step.
- Along with your contract compiled, now we are able to deploy it to the sepolia testnet. In your terminal sort:
npx hardhat run scripts/deploy.js --network sepolia
Save the contract handle that was printed out. Go to https://sepolia.etherscan.io/ and insert the contract handle into the Explorer enter discipline. Take a while to discover the transaction particulars of your contract.
- Save this handle someplace. Don’t lose it!
Testing
Each Contract ought to be examined earlier than deployment. On this case we deployed first becuause it’s freaking thrilling to see our contract deployed. However we want exams to verify our contract has no bugs and is safe.
- Open the
take a look at
folder and take away any current recordsdata inside it. Create a brand new file referred to astesting.js
, and insert the testing.js code from this venture. - Return to the terminal, be sure you’re within the
backend
listing, and kind:
npx hardhat take a look at
Frontend
On this part of the tutorial we’ll create a dApp (decentralized app) that can connect with your contract and its capabilities, permitting you to work together with it. In different phrases, we’re constructing an interface for the contract.
- Open the terminal (command line) and go into the
simple-storage
listing. Create a pattern Subsequent app by typing:
npx create-next-app@13 frontend
- Ensure that to decide on these settings.

- Earlier than operating the pattern app, you’ll must take away the
.git
file that was routinely created inside the brand newfrontend
folder. To take away the file, sort this command contained in the terminal:
# Linux / macOS cd frontend rm -rf .git # Home windows cd frontend rm -r -fo .git
- Set up these packages utilizing your terminal (it’s best to nonetheless be within the
frontend
listing).
npm set up web3modal npm set up ethers@5
- Create a brand new folder referred to as
constants
inside thefrontend
listing. Contained in theconstants
folder create a brand new file referred to asindex.js
. Add the next code insideindex.js
.
export const MY_CONTRACT_ADDRESS = "MY_CONTRACT_ADDRESS"; export const abi = MY_ABI;
- Change the
MY_ABI
with the abi array that may be discovered within the following file:backend/artifacts/contracts/SimpleStorage.json
- Change the
MY_CONTRACT_ADDRESS
with the handle of the contract that you just deployed within the Backend part of this tutorial.
- Lastly, to construct the interface of the contract, open the
index.js
file underneath thepages
folder (within thefrontend
folder) and substitute its content material with the index.js code from this venture. - Add the Charming Knowledge photos utilizing the directions under. When you perceive add photos to your app, be happy to interchange with your individual photos, wherein case you would want to replace the pictures’ names contained in the
pages/index.js
file (traces 143 & 154)
- Return to your terminal; be sure your within the
frontend
listing and kind:
npm run dev
Sharing your dApp with others
We are going to use Vercel to deploy the frontend of our contract. Earlier than beginning with Vercel you have to to create a GitHub account should you don’t have one and push your code right into a GitHub repository:
- As soon as your repositoy has been created in Github, go to Vercel and create an account by connecting your GitHub account.
- As soon as your GitHub account is related, click on the button so as to add a brand new venture in Vercel.
- add your repository (you may need to import it as effectively)
- Choose
Subsequent.js
because the Framework Preset. Within the Root Listing click on Edit to pick outfrontend
. - Click on Deploy, and wait a couple of minutes. When finished, click on your area hyperlink to see your dApp.
The earlier textual content was initially written by Adam from CharmingData.