Build · Lesson 11 of 14
What just happened?
About 4 minutes
You've deployed a contract. Now let's see what each part did.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
contract MyFirstContract {
string public message = "Hello, Blockskool!";
function setMessage(string calldata newMessage) external {
message = newMessage;
}
}Line by line
// SPDX-License-Identifier: MITsays which licence the code uses. MIT means anyone can reuse it.pragma solidity ^0.8.24;says which Solidity versions can compile it: 0.8.24 or newer, but below 0.9.contract MyFirstContract { ... }wraps everything in one contract, a bit like a class in other languages.string public message = "Hello, Blockskool!";creates a piece of text stored on the blockchain. Because it'spublic, Solidity automatically adds a function to read it. That's themessagefunction you clicked Call on.function setMessage(string calldata newMessage) externalcreates a function anyone can call from outside the contract, with one piece of text as input.message = newMessage;replaces the stored text with the new one.
Reading vs writing
- Reading data (
message) is free and instant. Remix marks it with a blue dot and a Call button. - Writing data (
setMessage) is a transaction. Every computer in the network has to agree on the change, so it costs gas. Remix marks it with an orange dot and a Transact button. On the Remix VM the gas is fake.
Before you move on
Tick these off as you go.