Track 4 · Solidity foundations · lesson 2

Your first contract

9 min


Now you change something. One thing.

Greeter.solTweak
Loading editor…
Change the greeting to exactly: Hello, chain
Run the code to see what happens.
Nudge
Only one thing on the page needs to change, and it is inside the quotes.
Show me the approach
A `string public` state variable automatically gets a getter with the same name — the test calls `greeting()`. You do not need to write a function.
Show me the code
Replace the initial value with `"Hello, chain"`.
Explain the solution
The line becomes `string public greeting = "Hello, chain";`. Because the variable is `public`, Solidity generates a `greeting()` view function for you, which is what the test calls.

If it went red, read the message under the editor before doing anything else. The compiler's own wording is on the first line; the plain-English version is underneath it.

What just happened when you pressed Run

Four separate things, in order:

  1. Compiled. Solidity became EVM bytecode — the low-level instructions the machine actually understands.
  2. Deployed. That bytecode was sent as a transaction with no recipient, which is how a contract is created. It got an address.
  3. Called. The test called greeting() at that address.
  4. Checked. The returned value was compared against what the test expects.

This is exactly the cycle you will run against a real network in track 6. Same steps, same tools, and there it costs money.

Notice that deployment reported a gas figure. Storing a string permanently is one of the more expensive things a contract can do — the longer your greeting, the more deployment costs.

That is not a quirk of this playground. It is the pricing from track 1, applied to code you just wrote.

Predict

You deploy this contract twice, unchanged. Do you get one contract or two?

Choose one answer

Check

Why does `string public greeting` need no getter written by hand?

Choose one answer
public storage is not private dataOptional

Marking a variable private stops other contracts reading it. It does not hide anything from people.

Every storage slot of every contract is readable by anyone with a node — the data is right there in the state. private is a compile-time visibility rule, not encryption.

Contracts have been drained because someone stored a secret in a private variable and assumed that meant secret. Never put anything on-chain you would not publish.

Worth remembering

  • Run = compile, deploy, call, check — the same cycle you'll use on a real network.
  • Deployment is a transaction with no recipient; the contract gets its own address.
  • The same bytecode deployed twice gives two independent contracts with separate state.
  • `private` hides data from other contracts, never from people. Nothing on-chain is secret.