How to write an Ethereum smart contract using Solidity | QuickNode (2024)

6 min read

Overview

This article is intended for developers new to Ethereum development. In this article, we will talk about Solidity and smart contracts, What they are and what role they actually play in the ethereum development with the end goal of writing a smart contract using Solidity

What is Ethereum?

Before getting started with smart contracts or Solidity let us first get an overview of what Ethereum is: Ethereum is a decentralized open-source blockchain with support for a Turing-complete programming language, Solidity. What we normally call computer programs are called smart contracts in Ethereum. Launched in 2015, Ethereum has been gaining significant popularity the last 5 years. It is a permissionless, public blockchain, which means anyone can use the blockchain, though for every write operation you need to pay ETH (Gas).

What is a Smart Contract?

The smart contract term was coined by Nick szabo in 1997. Smart contracts are nothing but programs that exist on the blockchain. These "smart contracts" can be used by making outside method calls or calls from other smart contracts. These smart contracts execute in the EVM (Ethereum virtual machine). Smart contracts can reduce malicious exceptions, fraudulent losses, and a need for trusted intermediator, when properly written and audited.

Ethereum supports Turing complete smart contracts, which means you can perform almost any type of operation you want. (Remember, you need to pay ETH for every write operation).

What is Solidity?

As we saw that smart contracts are nothing but programs and you need a programming language to write these programs. Ethereum core contributors invented a programming language called Solidity to write smart contracts (aka computer programs that run on the blockchain). Solidity is a high-level, object-oriented language inspired by JavaScript, C++, and Python - it has syntax very similar to JavaScript. There are other blockchains and Ethereum forks that support Solidity - such as Tron. Solidity is not the only language you can use to write smart contracts though. There are other languages that can be used to write smart contracts, like Vyper, the most popular and adopted smart contract language after solidity.

Your first Smartcontract

Now, let's write a simple smart contract. Our contract will allow us to store an unsigned integer and retrieve it.

// SPDX-License-Identifier: MIT
pragma solidity >=0.4.0 <0.7.0;
contract SimpleStorage {
uint storedData;
function set(uint x) public {
storedData = x;
}
function get() public view returns (uint) {
return storedData;
}
}

The code snippet above is a smart contract written in Solidity language. Let’s take a moment to understand what the code we wrote in our smart contract is doing line by line.

Line 1: Specifying SPDX license type, which is an addition after Solidity ^0.6.8; whenever the source code of a smart contract is made available to the public, these licenses can help resolve/avoid copyright issues. If you do not wish to specify any license type, you can use a special value UNLICENSED or simply skip the whole comment (it won’t result in an error, just a warning).

Line 2: On the first line we are declaring which Solidity compiler we want to use. For instance, we are targeting any version between ≥ 0.4.0 and <0.7.0 .

Line 3: We are declaring our contract here and naming it as Simplestorage. It is normal practice to use the same filename as the contract name. For example - this contract will be saved in the file name SimpleStorage.sol (.sol is the file extension for solidity smart contracts).

Line 4: We are declaring a uint (Unsigned Integer) variable named storedData, this variable will be used to store data.

Line 5-7: Next, we will add a set function, using which we will change the value of our variable storeData. Here set function is accepting a parameter x whose value, we are storing into storeData. In addition, the function is marked as public which means that the function can be called by anyone.

  • Line 8-10*: We will add a get function to retrieve the value of storeData variable. This function is marked as view which tells Solidity compiler that this is a read-only function.

Other than that the get function also has returns (uint), which means that the function will return a uint.

Deploying the Smartcontract

After writing a smart contract it needs to be deployed on the ethereum network, we will deploy our smart contract using Remix. There are other ways to deploy smart contracts, but to make it beginner-friendly, we will use Remix. Remix is an online web Ethereum IDE. It’s simple and supports many functionalities. So open Remix using this link. Remix has plugins, we need to activate two plugins for compiling and deploying our smart contract which you can see in the image below.

How to write an Ethereum smart contract using Solidity | QuickNode (1)

Here, you can see we have activated both plugins

Next, create a file on Remix with the name SimpleStorage.sol and copy/paste our smart contract code.

// SPDX-License-Identifier: MIT
pragma solidity >=0.4.0 <0.7.0;
contract SimpleStorage {
uint storedData;
function set(uint x) public {
storedData = x;
}
function get() public view returns (uint) {
return storedData;
}
}

Now, lets compile our smart contract using the Remix plugin.

How to write an Ethereum smart contract using Solidity | QuickNode (2)

Click on “Compile SimpleStorage.sol” to compile the contract

Finally, we can now deploy our SimpleStorage smart contract. We will deploy our smart contract on Ropsten testnet. Blockchains have multiple public networks. One is the main public network, which we usually call mainnet. Others are for testing purposes, which we usually call testnets.

We could use pretty much any Ethereum client, such as Geth or OpenEthereum (fka Parity), for our purposes today. Since that is a bit too involved for fetching logs, we'll just create a free QuickNode account here and easily set up an Ethereum endpoint. We’ll need a Ropsten endpoint to get data from the chain as we’ve deployed our contract on the Ropsten testnet. After you've created your free Ethereum endpoint, copy your HTTP Provider endpoint:

How to write an Ethereum smart contract using Solidity | QuickNode (3)

We’ll deploy our contract on the Ropsten testnet. To get started, you will need the Metamask browser extension to create an ETH wallet and some test ETH, which you can get by going to the Ropsten faucet. You'll need to select Ropsten Test Network on your Metamask wallet and copy-paste the wallet address into the text field in the faucet, then click Send me test Ether.

How to write an Ethereum smart contract using Solidity | QuickNode (4)

Now, let's add our node to Metamask:

Step 1: Open Metamask, click on the network menu on top, and select custom RPC.

How to write an Ethereum smart contract using Solidity | QuickNode (5)

Step 2: Enter the network name (It can be any name of your choice, QuickNode in this example), Paste your QuickNode endpoint URL in the second field that says New RPC URL, Enter the Chain ID, which is also known as the network id (3 here as we are using an Ethereum Ropsten endpoint), and click on save.

How to write an Ethereum smart contract using Solidity | QuickNode (6)

Now, go back to remix, go to the third tab on the left menu, and select Injected Web3 under the ENVIRONMENT option.

How to write an Ethereum smart contract using Solidity | QuickNode (7)

Click on deploy, and now a Metamask window must open up to confirm the transaction.

How to write an Ethereum smart contract using Solidity | QuickNode (8)

After you click on confirm, it might take some time for the transaction to get approved. Once confirmed, you must see a transaction confirmed message in the remix console and the deployed contract under the Deployed Contracts section.

How to write an Ethereum smart contract using Solidity | QuickNode (9)

Conclusion

So we have successfully created and deployed our smart contract written in Solidity to the blockchain, You can refer the Solidity Documentation for more.

Subscribe to our newsletter for more articles and guide on Ethereum. If you have any feedback, please feel free to reach out to us via Twitter and you can always chat with us if you have a question via our community server on Discord, thanks :)

How to write an Ethereum smart contract using Solidity | QuickNode (2024)

FAQs

Are smart contracts written in Solidity? ›

Your first Smart contract​

Now, let's write a simple smart contract. Our contract will allow us to store an unsigned integer and retrieve it. The code snippet above is a smart contract written in Solidity language. Let's take a moment to understand what the code we wrote in our smart contract is doing line by line.

What language are Ethereum smart contracts written in? ›

Solidity is the primary language used to develop smart contracts for Ethereum as well as other private blockchains, such as the enterprise-oriented Hyperledger Fabric blockchain. SWIFT deployed a proof of concept using Solidity running on Hyperledger Fabric.

How to write ERC20 smart contract? ›

Creating Ethereum ERC20 Smart Contract
  1. Step 1: Decide what you want your Token to be.
  2. Step 2: Code the Contract.
  3. Step 3: Test the Token on the TestNet.
  4. Watch the Custom Token.
  5. Verify the Source Code.

Is rust better than Solidity? ›

Rust is better suited for projects where performance and security are top priorities. Scalability Needs: For projects requiring high scalability, Rust's efficiency and performance are advantageous. Solidity is suitable for projects within the Ethereum network, but may face limitations in scalability outside of it.

Is Solidity easy to learn? ›

Solidity isn't a walk in the park by any means. In fact, it's probably one of the most challenging programming languages to implement and master out there. Yes, learning it is easy and many programmers can learn it in days, if not weeks.

How hard is it to write a smart contract? ›

If you have no experience, it may take you a few months to get comfortable with coding a simple smart contract. Developers with more experience might need just days or weeks. Once created, your smart contract can take anywhere from a handful of seconds to minutes to complete.

Are smart contracts hard to learn? ›

Learning how to design, develop and test smart contracts is not radically different from learning any other type of programming, Guyer said. It helps to have a basic understanding of the execution environment in which your code will run.

What is the simplest smart contract? ›

The simplest example of a smart contract is a transaction between a consumer and a business, where a sale is made.

Is Solidity worth learning in 2024? ›

If you feel certain about dedicating a few years of your life to learning Solidity, give it a try. It's quite probable that you will have a reliable constant career for years to come.

How long does it take to learn Solidity? ›

If you've already had previous experience with coding in languages such as JavaScript, C++, and/or Python, you'll most likely find it easy to learn Solidity within weeks to several months of intensive study. However, if you have no experience in programming, it might take you from 6 months to a year.

Can I learn Solidity without coding experience? ›

Yes, you can learn Solidity directly by studying its documentation, online courses, and practice coding. Having prior programming experience may help, but it's not a strict requirement.

How do you create a smart contract with solidity? ›

Let's go through the smart contract code. The first line (// SPDX-License-Identifier: MIT) is a comment that specifies the license under which the code is released. The second line (pragma solidity 0.8. 0;) is a statement that specifies the version of the Solidity compiler to use.

What is the difference between smart contract and ERC20? ›

Smart contracts are a foundational technology in the blockchain space, particularly within the Ethereum ecosystem. In the context of ERC-20 tokens, they not only facilitate the creation and management of digital assets but also ensure that these tokens operate seamlessly across the myriad of decentralized applications.

Can Ethereum be used for smart contracts? ›

Ethereum provides a decentralized virtual computer — the Ethereum Virtual Machine (EVM) — on which developers can build applications consisting of multiple smart contracts. Think of the EVM as a distributed global computer where all smart contracts are executed.

Does Ethereum have a smart contract address? ›

Understanding Ethereum Contract Addresses

Unlike a typical Ethereum (or Ether) address used for sending and receiving cryptocurrency, an Ethereum contract address represents a smart contract. These contracts are collections of code and data that reside at a specific address on the Ethereum blockchain.

What is the structure of smart contract in Ethereum? ›

Types. In Solidity, the contract type is a struct that organizes a set of related functions around a single purpose. Address types are Ethereum addresses that equate to 20 bytes, and are represented in hexadecimal form beginning with the prefix 0x.

How much does it cost to put a smart contract on Ethereum? ›

Smart contract creation cost can be anywhere from $10 to $2,000 assuming Ether costs between $1,500 to $2,000. The biggest factors are 1) Ethereum price, 2) the size of the compiled contract (in bytes), 3) the current gas price on the Ethereum network.

Top Articles
Why Does the Stock Market Go Up Over the Long Term? » Claret
What Income Do You Need to Afford a $500k House?
Sdn Md 2023-2024
Caesars Rewards Loyalty Program Review [Previously Total Rewards]
Best Big Jumpshot 2K23
Quick Pickling 101
Pitt Authorized User
Alpha Kenny Buddy - Songs, Events and Music Stats | Viberate.com
Erskine Plus Portal
P2P4U Net Soccer
83600 Block Of 11Th Street East Palmdale Ca
Myunlb
Erskine Plus Portal
Classic Lotto Payout Calculator
Belly Dump Trailers For Sale On Craigslist
finaint.com
Illinois Gun Shows 2022
Overton Funeral Home Waterloo Iowa
Florida History: Jacksonville's role in the silent film industry
Schedule 360 Albertsons
Energy Healing Conference Utah
Pokemon Unbound Shiny Stone Location
Heart Ring Worth Aj
Rust Belt Revival Auctions
Loslaten met de Sedona methode
A Man Called Otto Showtimes Near Cinemark University Mall
Utexas Iot Wifi
What Individuals Need to Know When Raising Money for a Charitable Cause
Skycurve Replacement Mat
Dei Ebill
8002905511
Mini-Mental State Examination (MMSE) – Strokengine
Plasma Donation Racine Wi
Craigslist Texas Killeen
Citibank Branch Locations In Orlando Florida
After Transmigrating, The Fat Wife Made A Comeback! Chapter 2209 – Chapter 2209: Love at First Sight - Novel Cool
Hotel Denizen Mckinney
Autotrader Bmw X5
Verizon TV and Internet Packages
Lake Dunson Robertson Funeral Home Lagrange Georgia Obituary
Unlock The Secrets Of "Skip The Game" Greensboro North Carolina
42 Manufacturing jobs in Grayling
Streameast.xy2
Mcgiftcardmall.con
Ursula Creed Datasheet
Seven Rotten Tomatoes
LumiSpa iO Activating Cleanser kaufen | 19% Rabatt | NuSkin
Blackwolf Run Pro Shop
Penny Paws San Antonio Photos
Craigslist St Helens
2000 Ford F-150 for sale - Scottsdale, AZ - craigslist
Latest Posts
Article information

Author: Patricia Veum II

Last Updated:

Views: 5945

Rating: 4.3 / 5 (64 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Patricia Veum II

Birthday: 1994-12-16

Address: 2064 Little Summit, Goldieton, MS 97651-0862

Phone: +6873952696715

Job: Principal Officer

Hobby: Rafting, Cabaret, Candle making, Jigsaw puzzles, Inline skating, Magic, Graffiti

Introduction: My name is Patricia Veum II, I am a vast, combative, smiling, famous, inexpensive, zealous, sparkling person who loves writing and wants to share my knowledge and understanding with you.