Solidity Introduction
Solidity is a programming language for writing smart contracts. A smart contract is a program that lives on a blockchain, and a blockchain is a shared record that many computers keep together.
The smallest contract
This contract holds one number. The number is set on the same line that declares it.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;
contract FirstSteps {
uint256 public luckyNumber = 7;
}Public grows a getter
The word public adds a free reading function with the same name as the variable. Calling score() reads the stored value.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;
contract ScoreCard {
uint256 public score = 100;
}The starting value is the set value
A number set at declaration is the value the contract starts with. Nothing needs to run first for the getter to read it.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;
contract SavingsJar {
uint256 public coins = 25;
}Example
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;
contract NumberBoard {
uint256 public boardNumber = 42;
}Key points
- Blockchain: a shared record that many computers keep and agree on.
- Smart contract: a program that lives on a blockchain and runs there.
- Deploy: to put a program onto a blockchain so others can use it.
- Contract: the Solidity unit that holds data and the code that works with it.
- State variable: a value stored inside a contract.
- Public: a public state variable can be read by anyone.
In this example, the contract NumberBoard declares one public state variable named boardNumber. The value 42 is set on the declaration line itself. Because the variable is public, anyone can read it through the boardNumber() getter.
Note
A deployed contract lives on the blockchain itself, not on one person's computer.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Remix IDE
Exercise
What does deploying a smart contract mean?