Hello Geeksters!
If you've read my previous blogs, you'll know that I keep blabbering about Solidity and coding. I talk as if I am an expert but I am clearly not. I'm still a beginner figuring her way around Solidity. So the question comes, how did I start? What was my first code? I'll tell you the answer to that in this blog.
So a little bit basics first.
What is a smart contract?
A smart contract is a little piece of code that runs on the blockchain and runs automatically when certain conditions are met. It’s immutable, transparent, and decentralized. For example, when I send my best friend some crypto currency for a cute dress, the act of sending it triggers a smart contract to reduce the amount from my account and to add the same amount to her account after making sure my wallet has sufficient balance.
What is Solidity?
Unlike most programs, Ethereum based machines cannot be coded in Python. It needs a special language called Solidity. So Solidity is used to write these smart contracts for the Ethereum based networks.
Now we'll start with the basic hello world
program in solidity.
You will be coding in something called a Remix IDE (https://remix.ethereum.org) which is an online IDE for Solidity development.
Head over to Remix and create a new file named HelloWorld.sol
.
Paste the following code:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract HelloWorld {
string public message;
constructor() {
message = "Hello, World!";
}
function updateMessage(string calldata newMessage) public {
message = newMessage;
}
}
Let's understand what this code actually does.
// SPDX-License-Identifier: MIT
- This is a license identifier. It’s not required for functionality, but is good practice and required by most tools like Remix. MIT is a permissive open-source license.pragma solidity ^0.8.0;
- This line tells the compiler that this code is written for Solidity version 0.8.0 or higher, but not 0.9.0 or above. This ensures compatibility and avoids using older or incompatible features.contract HelloWorld {
- Starts the definition of a contract named HelloWorld. A contract in Solidity is like a class in other programming languages. It contains variables and functions that define behavior.string public message;
- Declares a state variable named message of type string. The keyword public automatically creates a getter function — anyone can call it to read the value stored in message.constructor() {
- A constructor is a special function that runs only once, at the time the contract is deployed to the blockchain. It initializes contract state — in this case, it sets the default value of the message.message = "Hello, World!";
- A variable called message holds the message 'Hello World!' in it.function updateMessage(string calldata newMessage) public {
- Defines a public function named updateMessage. It takes a string input called newMessage, marked as calldata (a temporary, read-only location for function inputs). public means anyone can call this function from outside the contract.message = newMessage;
- Inside the function: updates the state variable message to the new value provided by the user.
So now that you've coded the smart contract, let's run it.
In Remix, on the left hand bar, you'll see an option to compile the smart contract. Click on it. Once its compiled successfully, you can deploy your first every smart contract!
To deploy it, you'll see a Deploy and Run Transactions tab as well. Click on it. Click message() — see "Hello, World!". Input "Web3 is cool!" into updateMessage — click execute. Click message() again — now you'll see "Web3 is cool!".
If you've followed everything, you should now see your first ever smart contract that was deployed on a network! Congratulations! I got this feel of pride not so long back, so I hope you're proud of yourself as well. Its the tiny steps that matter. I'm still very much early in my learning phase but that is what is exciting. Keep pushing through and you've got this!
Top comments (0)
Some comments may only be visible to logged-in visitors. Sign in to view all comments. Some comments have been hidden by the post's author - find out more