Masterstroke Academy
Back to All Blogs
Blockchain Development

How to Write Your First Smart Contract Using Solidity

Learn Solidity basics, smart contract coding, and deployment to build your first blockchain application.

MST Editorial TeamMST Editorial Team
How to Write Your First Smart Contract Using Solidity

Quick answer: To write your first smart contract, open Remix IDE in your browser, create a file ending in .sol , write a contract with a state variable and a function, compile it with a matching Solidity version, and deploy it to a test environment. Then call its functions and read the result. A beginner can finish this in under an hour with no software installed.

Key takeaways:

  • You only need a browser, a code editor tab and a test wallet. Nothing to install.
  • Your first contract can be 20 lines: store a value, change it, and log the change.
  • Always start on a test network, never with real funds.
  • Learn two safety habits from day one: access control and testing.

Most Solidity tutorials either stop at "Hello World" or jump straight into complex token code. This guide sits in between. You will write a small but real contract, understand every line, deploy it, break it on purpose, and fix it. That is how the language really sticks.

Quick definitions

  • Smart contract: a program on a blockchain that runs exactly as written when called.
  • Solidity: the main programming language for smart contracts on EVM-compatible blockchains.
  • EVM: the Ethereum Virtual Machine, the runtime that executes contract code on many networks.
  • Gas: the fee paid to run a transaction or deploy a contract.
  • Testnet: a public practice network that uses valueless test tokens.
  • Remix IDE: a free browser-based editor for writing, compiling and deploying Solidity.

What is a smart contract, and what is Solidity?

A smart contract is a program stored on a blockchain that runs exactly as written when someone calls it. Once deployed, nobody can quietly change its code, which is why it can hold rules for payments, ownership, voting or credentials without a middleman.

Solidity is the most widely used programming language for writing smart contracts on EVM-compatible blockchains. EVM stands for Ethereum Virtual Machine, the runtime that executes the code. Because many networks are EVM-compatible, including MST Blockchain, the Solidity you learn once works across them.

Watch the Video: Write Your First Smart Contract Using Solidity

What do you need before you start?

  • A laptop and a modern browser (Chrome, Edge or Firefox)
  • Basic comfort with any programming language. Variables, functions and if-statements are enough
  • A browser wallet for the deployment step, set up for test use only
  • About 45 to 60 minutes

If you have never coded, spend a week on JavaScript or Python basics first. The blockchain developer roadmap shows the full order. New terms are explained in the glossary.

How do you write your first smart contract? The 7 steps

Step 1: Open Remix IDE

  • Search for Remix IDE and open the official site in your browser.
  • You get a file explorer, an editor and a compiler panel. No setup or login is needed.
  • Remix is the quickest way to start because everything runs in the browser, which suits students on shared or college laptops.

Step 2: Create a new .sol file

  • In the file explorer, create a new file named HelloBlockchain.sol.
  • The .sol extension tells the editor it is Solidity code.

Step 3: Write the contract

Paste this complete contract into the file:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract HelloBlockchain {
    string public message;
    address public owner;
    event MessageUpdated(address indexed by, string newMessage);
    constructor(string memory initialMessage) {
        message = initialMessage;
        owner = msg.sender;
    }
    function updateMessage(string calldata newMessage) external {
        require(msg.sender == owner, "Only owner can update");
        message = newMessage;
        emit MessageUpdated(msg.sender, newMessage);
    }
}

This contract stores a message, remembers who deployed it, lets only that owner change the message, and records every change as an event.

Step 4: Compile the contract

  • Open the Solidity compiler tab.
  • Select a compiler version that matches the pragma line. Version 0.8.20 or higher works for this code.
  • Click Compile. A green check means the code is valid.

Step 5: Deploy in the Remix VM first

  • Open the Deploy tab and choose the Remix VM environment. This is a simulated blockchain inside your browser. It is free and instant.
  • Type a starting message such as "Hello Pune" in the constructor box.
  • Click Deploy. Your contract appears under Deployed Contracts.

Step 6: Call the functions

  • Click the message button to read the stored text. Reading is free.
  • Type a new message into updateMessage and send it. This is a transaction.
  • Check the logs to see the MessageUpdated event.
  • Switch to a second account in Remix and try updateMessage again. It fails with "Only owner can update". That is access control working.

Step 7: Deploy to a testnet

  • Add the testnet network details to your wallet from the MST Blockchain and fund the account with test tokens.
  • In Remix, change the environment to your injected wallet and deploy the same contract.
  • Confirm the transaction in your wallet, then look it up on the MST testnet explorer. You will see your contract address, the transaction hash and the gas used.
  • Keep real funds out of it. A test wallet should hold test tokens only. If you want to understand how to hold keys safely, read What Is a Non-Custodial Wallet? on BridgeKey.

What does each line of the contract do?

Line What it does
// SPDX-License-Identifier: MIT Declares the license. Without it the compiler prints a warning
pragma solidity ^0.8.20; Sets the allowed compiler version range
contract HelloBlockchain { ... } Defines the contract, similar to a class
string public message; State variable stored on-chain. "public" creates a free read function automatically
address public owner; Stores the deployer's wallet address
event MessageUpdated(...) A log entry that apps and explorers can read
constructor(...) Runs once at deployment. Sets the first message and the owner
msg.sender The address that called the function
require(condition, "text") Stops the transaction and reverts all changes if the condition is false
emit MessageUpdated(...) Writes the event to the transaction log

What are the most common beginner errors and how do you fix them?

Error Likely cause Fix
Compiler version mismatch Pragma and selected compiler differ Choose a compiler that satisfies the pragma line
Transaction reverted: Only owner can update Calling from a different account Switch to the deploying account
Insufficient funds for gas Wallet has no test tokens Get test tokens, then retry
Wrong network Wallet on a different chain Switch the wallet to the testnet you intend
Parser error: expected ; Missing semicolon or bracket Check the line shown in the error
Gas estimation failed The function would revert Test the call in the Remix VM first

How different is Solidity from JavaScript?

Solidity looks familiar if you know JavaScript, but a few rules are different, and they matter in your first contract.

Topic JavaScript Solidity
Types Loosely typed Strictly typed (uint256, address, string)
Where code runs Browser or server Every node on the blockchain
Changing code Redeploy any time Deployed code cannot be edited
Cost of running Server cost Gas paid per transaction
Errors Often logged and ignored A failed require reverts the whole transaction

What should you check before every deployment?

  • Compiler version matches the pragma line
  • Every sensitive function has an access check
  • No secrets or private data are stored on-chain
  • Each function was tested with the owner, a stranger and unusual inputs
  • The wallet is on the right network and holds only test tokens
  • You have saved the contract address and transaction hash

How do you test a smart contract before deploying?

  • Remix VM: try every function with several accounts and unusual inputs.
  • Write down expected results: what should happen for the owner, for a stranger and for an empty message?
  • Automated tests: once you outgrow Remix, move to Hardhat or Foundry and write tests that run on every change.
  • Testnet first: deploy publicly on a test network and read the transaction details before any real use.

What are the security basics for a first contract?

  • Control who can call sensitive functions. The owner check in this contract is the simplest version.
  • Never store secrets on-chain. Everything on a blockchain is public, including private-looking variables.
  • Avoid tx.origin for permissions. Use msg.sender.
  • Update state before sending value. This habit prevents reentrancy bugs.
  • Use audited libraries such as OpenZeppelin instead of copying random code.
  • Remember contracts are hard to change. Test twice, deploy once.

A real-world lesson: the 2016 DAO attack exploited a reentrancy flaw and led to a split in Ethereum. Small habits in your first contract are the same habits that prevent that kind of loss. For a wallet-side view of safe key handling, see BridgeKey security.

How much does it cost to deploy a smart contract?

On a testnet it costs nothing, because you use free test tokens. On a main network you pay gas, which depends on the size of the contract and how busy the network is. Check current fees on the network's mainnet explorer before deploying. Keep contracts small and avoid unnecessary storage to reduce cost.

What should you build after your first contract?

  • A voting contract with one vote per address
  • An ERC-20 token with a fixed supply, using OpenZeppelin
  • A certificate registry that records a hash of a credential, a strong fit for India where verification matters
  • A simple crowdfunding or escrow contract that teaches handling value safely

Publish each project on GitHub with a short README. Strong projects can also support applications for ecosystem funding, such as the MST Blockchain grants.

What is it like learning Solidity in Pune, Bengaluru and across India?

Browser-based tools mean you can start on any laptop, including a college lab machine in Pune or a shared device at home. Learners in Baner, Hinjewadi, Kothrud and Pimpri-Chinchwad often study in the evening around classes or work. In Bengaluru and Hyderabad, in-person meetups and hackathons add a practical push.

Online practice plus a few offline events works well. Student communities and MST college events are a good way to meet people who are building.

How can MasterStroke Academy help you go beyond your first contract?

A single tutorial gets you started. A structured path gets you hired or funded. MasterStroke Academy teaches Solidity in a browser IDE and runs your code on MST Blockchain, an EVM-compatible Layer 1.

  • Structure: 4 phases, 21 modules, 123 submodules and 130+ hours, in the curriculum overview.
  • Beginner-friendly: no prior blockchain experience is needed.
  • Graded progress: 70% or more on each submodule to unlock the next.
  • Verifiable certificate: recorded on-chain.
  • Free to start: register here, then pick a Fellowship plan for full access.
  • Support: Monday to Saturday, 10 am to 6 pm IST.

Honest limits: MasterStroke is newer than long-established institutes and is self-paced, so it needs steady weekly effort. Fellowship prices are not shown on the course page. Read the FAQ and the comparison page before deciding.

Deploy your first contract this week

Do not just read this. Open the editor and run the code.

FAQs

1. How do I write my first smart contract in Solidity?

Open Remix IDE, create a .sol file, write your contract, compile it using Solidity compiler and deploy it through Remix VM to test its functions.

2. Do I need to install anything to write a Solidity smart contract?

No, Remix IDE works directly in your browser. You only need a compatible wallet and testnet tokens for public testnet deployment.

3. Is Solidity easy to learn for beginners?

Yes, beginners with basic programming knowledge can learn Solidity by practising variables, functions, smart contracts and blockchain concepts.

4. Which Solidity version should beginners use?

Beginners should use a recent stable Solidity 0.8.x version compatible with their project's compiler requirements.

5. What is the difference between Remix VM and a blockchain testnet?

Remix VM is a simulated blockchain for testing, while a testnet is a public blockchain network used to test real transactions.

6. How much does it cost to deploy a Solidity smart contract?

Testnet deployment is generally free, while mainnet deployment requires gas fees based on contract complexity and network demand.

7. Can I modify a smart contract after deployment?

No, deployed smart contract code is generally immutable. Developers can use upgradeable proxy patterns or deploy a new contract version.

8. Where can I learn Solidity and blockchain development in Pune?

You can learn through practical blockchain development programmes like Masterstroke Academy, which focuses on Web3 and blockchain skills.

9. What projects should I build after learning Solidity?

Start with voting contracts, ERC-20 tokens, certificate registries, crowdfunding applications and NFT marketplaces to gain practical experience.

Ready to Build on Blockchain?

Join Masterstroke Academy to master Solidity, smart contract security, DeFi protocols, and earn on-chain verifiable credentials.