Creating a smart contract is less mysterious than it sounds. A smart contract is a small program that lives on a blockchain and runs exactly as written whenever someone calls it. This guide walks you through creating one from scratch: planning it, writing it in Solidity, testing it, and deploying it to a test network, using free tools and no prior blockchain experience.
If you first want the concepts, how smart contracts work, where they’re used, and their risks, read Smart Contracts Explained.
Key Takeaways
- Most smart contracts are written in Solidity and run on Ethereum or EVM-compatible chains such as BNB Smart Chain, Polygon, Arbitrum, and Base.
- Beginners can write, compile, and deploy a first contract entirely in the browser with Remix and a MetaMask wallet. Hardhat and Foundry are the standard tools for larger projects.
- Always deploy to a testnet such as Sepolia first. Testnet ETH is free, mainnet mistakes are permanent.
- Deployed code can’t simply be edited, so security, access control, and testing have to be planned before launch, not after.
What You Need Before You Start
- A browser and Remix IDE: Remix is a free, browser-based editor for writing, compiling, and deploying Solidity. Nothing to install.
- A wallet: MetaMask or another EVM wallet to sign the deployment transaction. Our guide to crypto wallets explains the options.
- Testnet ETH: free Sepolia ETH from a public faucet, to pay gas on the test network.
- Basic programming knowledge: Solidity’s syntax looks like JavaScript or C++. If you’ve written either, you’ll recognise most of it.
How to Create a Smart Contract: Step by Step
Step 1: Define what the contract should do
Write the rules down in plain language before writing any code. Answer three questions:
- What data does the contract store (balances, owners, a message, deadlines)?
- Who is allowed to do what (anyone, only the owner, only approved addresses)?
- What happens in each situation, including the edge cases (zero amounts, repeated calls, a deadline that has passed)?
Smart contracts work best for clear “if this, then that” logic. If an agreement relies on judgment or negotiation, keep that part off-chain.
Step 2: Choose a blockchain
For most projects the choice is between Ethereum and the many EVM-compatible networks, which run the same Solidity code:
| Option | Language | Good for |
|---|---|---|
| Ethereum mainnet | Solidity / Vyper | Maximum security and liquidity; highest fees |
| Ethereum Layer 2s (Arbitrum, Optimism, Base) | Solidity | Same tools as Ethereum with much lower fees |
| BNB Smart Chain, Polygon, Avalanche | Solidity | Low fees, large user bases |
| Solana | Rust | High-throughput apps; different tooling and programming model |
This guide uses Solidity and Ethereum’s Sepolia testnet, so everything you learn carries over to any EVM chain. See our overview of smart contract platforms for a wider comparison.
Step 3: Set up your tools
- Remix (browser): best for learning and small contracts. It compiles, deploys, and lets you call functions from one screen.
- Hardhat (JavaScript/TypeScript) or Foundry (Solidity-based testing, very fast): the standard frameworks for real projects, automated tests, and scripted deployments.
- OpenZeppelin Contracts: audited, reusable building blocks for tokens, ownership, and access control. Use them instead of writing these parts yourself.
Older guides often recommend Truffle and Ganache. ConsenSys discontinued both in 2023, so new projects should use Hardhat or Foundry instead.
Step 4: Write the contract
In Remix, create a new file called Greeting.sol and paste in this contract. It stores a message that anyone can read but only the deployer can change, and it records every change as an event:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract Greeting {
string private greeting;
address public owner;
event GreetingChanged(string newGreeting);
constructor(string memory initialGreeting) {
greeting = initialGreeting;
owner = msg.sender;
}
function getGreeting() public view returns (string memory) {
return greeting;
}
function setGreeting(string memory newGreeting) public {
require(msg.sender == owner, "Only the owner can change the greeting");
greeting = newGreeting;
emit GreetingChanged(newGreeting);
}
}
What each part does:
- pragma pins the compiler version, so the contract always compiles the same way.
- State variables (
greeting,owner) are stored permanently on the blockchain. - The constructor runs once at deployment and records who deployed the contract.
getGreetingis a view function: reading it is free and needs no transaction.setGreetingchanges state, so it costs gas, and therequireline rejects anyone who isn’t the owner. This is the simplest form of access control.
Step 5: Compile and test
- Open the Solidity Compiler tab in Remix, pick a 0.8.x compiler version, and click Compile. Fix any errors it reports.
- In the Deploy tab, choose the “Remix VM” environment. This is a simulated blockchain in your browser with pre-funded test accounts.
- Enter an initial greeting, deploy, then call
getGreetingandsetGreeting. Switch to a different test account and confirm thatsetGreetingfails for it.
For anything beyond a demo, write automated tests in Hardhat or Foundry that cover normal use, wrong callers, and edge cases. Tests are cheap; bugs on mainnet are not.
Step 6: Deploy to a testnet
- Switch MetaMask to the Sepolia test network and claim free Sepolia ETH from a faucet.
- In Remix’s Deploy tab, choose “Injected Provider – MetaMask” as the environment.
- Click Deploy and confirm the transaction in MetaMask. After a few seconds, Remix shows your contract’s address.
- Look the address up on the Sepolia block explorer to see the deployment, and call the functions again from Remix to check they work on a real network.
Step 7: Verify and go live
Verifying your source code on Etherscan (or the explorer for your chain) publishes the code behind the address, so users can read exactly what the contract does. Only move to mainnet once the contract is tested and, if it will hold real money, reviewed by someone else. Deploying to mainnet works the same way as the testnet, but uses real ETH for gas, and the deployment is permanent.
Security Checklist Before Launch
Smart contract bugs have cost users billions, and deployed code can’t simply be patched. Before launching anything that holds value:
- Use audited libraries such as OpenZeppelin for tokens, ownership, and roles instead of custom code.
- Lock down access control: every function that changes important state should check who is calling it.
- Guard against reentrancy: update balances before sending funds, or use a reentrancy guard.
- Validate inputs and handle edge cases explicitly.
- Plan for upgrades and emergencies: decide up front whether you need an upgradeable proxy or a pause function, because you can’t add them after deployment.
- Get an independent review: automated scanners catch common issues, and a professional smart contract audit is standard for anything holding significant funds.
- Monitor after launch: set up alerts for unusual transactions so you can react quickly.
How Much Does It Cost to Create a Smart Contract?
Writing and testing a contract with free tools costs nothing, and testnet deployments are free. Deploying to a live network costs gas, which depends on the contract’s size and the network: a small contract can cost cents on a Layer 2 and considerably more on Ethereum mainnet when fees are high. For production projects the larger costs are usually development time and a security audit, which can run from a few thousand to tens of thousands of dollars depending on complexity.
Final Thoughts
Creating your first smart contract takes an afternoon: define the rules, write them in Solidity, test in Remix, and deploy to a testnet. The harder part is everything that makes a contract safe to hold real money: careful design, thorough tests, and an outside review. Start small, keep your contracts simple, and lean on audited libraries wherever you can.
If you’d like to stay up to date on smart contracts and all things Web3, make sure to subscribe to our newsletter.
Frequently Asked Questions
Can I create a smart contract without coding?
Yes, for standard use cases. No-code token and NFT generators can deploy common contract types for you. For anything custom you’ll need to write code, or hire a developer, and you should still understand what the generated contract does.
Which programming language is used for smart contracts?
Solidity is the most widely used, for Ethereum and all EVM-compatible chains. Vyper is a Python-like alternative on Ethereum, and Solana programs are mostly written in Rust.
Can a smart contract be changed after deployment?
Not directly; deployed code is permanent. Developers who need flexibility use an upgradeable proxy pattern, where a fixed proxy points to replaceable logic, or deploy a new version and migrate users. Both have to be planned before launch.
Are there hidden costs beyond gas fees?
Yes. Security audits, ongoing monitoring, and the development time for complex contracts usually cost far more than the gas to deploy.
Is a smart contract legally binding?
It depends on the country and the agreement. The code always executes as written, but whether courts treat it as an enforceable contract is still evolving, so important agreements often pair the smart contract with a traditional legal document.