How to Write Ethereum Smart Contracts in Solidity

TL;DR
Solidity is a statically typed, contract-oriented language for writing Ethereum smart contracts that run on the Ethereum Virtual Machine. Using the browser-based Remix IDE, you can write, compile, and deploy a contract without installing anything. A basic contract declares a state variable stored on the blockchain, plus get and set functions to read and write it.
Transcript
hey everybody this is gregory from daf university so i'm so excited to be on free code camp today to teach you how to become a blockchain developer i'm going to take you over the shoulder and teach you how to write ethereum smart contracts with a solidity programming language for the blockchain and you don't even have to know anything about solidit... Read More
Key Insights
- Solidity is the main programming language for writing smart contracts on the Ethereum blockchain. It is a contract-oriented, high-level language that resembles JavaScript, Python, and C++, and it runs on the Ethereum Virtual Machine.
- Solidity is statically typed rather than dynamically typed, meaning you must declare the data type of every variable and function argument. It also supports features like inheritance and libraries.
- A smart contract is code executed on the blockchain that behaves like a publicly accessible microservice. Everyone on the blockchain can see it, use it, read and write data with it, and execute the code inside it.
- State variables declared inside a contract are stored on the blockchain in storage, not locally. Setting such a value writes data to the blockchain, unlike a local variable that disappears once its function finishes.
- Remix is a browser-based IDE that lets you write, compile, deploy, and run Solidity smart contracts without downloading or installing anything on your computer.
- Function visibility is set with keywords like 'public', which allows anyone with access to the contract on the blockchain to call the function, not just code inside the contract itself.
- The 'view' modifier is added to functions that read but do not change contract data, resolving the state-mutability warning that Solidity raises when a function does not modify any values.
- A constructor is a function called automatically when the contract is created or deployed to the blockchain, and it can set a default value for state variables at deployment time.
Install to Summarize YouTube Videos and Get Transcripts
Explore YouTube Video Summarizer or Get YouTube Transcript Extractor
Questions & Answers
Q: What is Solidity used for?
Solidity is the main programming language for writing smart contracts on the Ethereum blockchain. It is a contract-oriented language, meaning smart contracts are the primary way you organize code, store data, and write programming logic. It is a high-level language for implementing these smart contracts, looks a lot like JavaScript, Python, and C++, and is used to run on the Ethereum Virtual Machine, which runs the code on the Ethereum blockchain.
Q: Is Solidity statically typed or dynamically typed?
Solidity is statically typed, as opposed to a dynamically typed language. This means you have to declare the data type of every variable you want to store and the type of every function argument, because the language needs to know the type of data being stored or passed in. For example, a variable is declared with a type like 'string', and function arguments also include their declared data type. Solidity additionally supports features such as inheritance and libraries.
Q: How do you write a Solidity smart contract without installing anything?
You can use Remix, a website that lets you write Solidity smart contracts directly in your browser. It has a text editor and lets you compile, deploy, and run contracts, so you do not have to download or install anything on your computer. In the tutorial, a new Solidity file is created in the file browser, code is written in the editor, and the contract is compiled and deployed all within the browser using Remix.
Q: What is a smart contract in Ethereum?
A smart contract is code that gets executed on the blockchain, in this case the Ethereum blockchain since Solidity is used. It behaves kind of like a microservice on the web because it is publicly accessible to everyone on the blockchain. People can see the smart contract, use it, read and write data with it, and execute any code written inside it. It looks something like a class in an object-oriented system, but it is more than a class because it is publicly accessible.
Q: Why is a contract state variable different from a local variable?
A state variable declared inside a smart contract belongs to the entire contract and is stored on the blockchain in storage. When you set this value, you are actually writing data to the blockchain. This differs from a local variable set inside a function, which is local to that function's scope and disappears instantly once the function is called. In the tutorial, the string variable 'value' is a state variable persisted on the blockchain.
Q: How do you make a function readable by anyone on the blockchain?
You set the function's visibility using the 'public' keyword. Marking a function as public tells Solidity that the function can be called by anyone who has access to the smart contract on the blockchain, not just code inside the contract. In the tutorial, both the 'get' function that returns the stored value and the 'set' function that updates it are declared public so that anyone can read or set the value on the blockchain.
Q: What does the 'view' modifier do in Solidity?
The 'view' modifier is added to a function that reads data but does not change anything inside the contract. In newer versions of Solidity, a warning appears saying the function state mutability can be restricted when a function does not modify any values. Adding the 'view' modifier to such a function, like the 'get' function that only returns the stored value, makes that warning go away and signals that the function does not alter contract state.
Q: What is a constructor in a Solidity smart contract?
A constructor is a function that is called whenever the contract is instantiated, created, or deployed to the blockchain, similar to constructor functions in other programming languages that use classes. In newer versions of Solidity, you write it with the 'constructor' keyword and can mark it public. Code inside the constructor runs at deployment, so in the tutorial it sets the state variable 'value' to a default so the contract has some initial value once deployed.
Summary & Key Takeaways
-
Gregory from Dapp University introduces this free course on becoming a blockchain developer by writing Ethereum smart contracts in Solidity. Solidity is a contract-oriented, statically typed, high-level language resembling JavaScript, Python, and C++ that runs on the Ethereum Virtual Machine and supports inheritance and libraries. No prior blockchain knowledge is required to follow along.
-
The tutorial uses Remix, a browser-based IDE, so nothing needs to be installed. You create a Solidity file, declare the compiler version with 'pragma solidity', and define a contract with curly braces. Inside, a string state variable named 'value' is declared, which is stored on the blockchain rather than kept locally in function scope.
-
The contract adds a public 'get' function marked 'view' that returns the string value, a public 'set' function that assigns a passed-in argument to the state variable, and a constructor that sets a default value at deployment. After compiling and deploying to the in-browser JavaScript virtual machine, get and set functions are tested, with each transaction recorded on the test blockchain.
Read in Other Languages (beta)
Share This Summary 📚
Summarize YouTube Videos and Get Video Transcripts with 1-Click
Try YouTube Summary with ChatGPT & Claude or YouTube Transcript Generator
Explore More Summaries from freeCodeCamp.org 📚






Summarize YouTube Videos and Get Video Transcripts with 1-Click
Try YouTube Summary with ChatGPT & Claude or YouTube Transcript Generator