How to Write a Smart Contract: A Practical First Build
Writing a smart contract means defining one small job, coding it in Solidity or another chain language, testing the failure paths, and deploying to a testnet first.
On this page
Short answer
To write a smart contract, define one small job in a sentence, pick the language that matches your chain, write the storage and functions with access rules, test the happy path and the failure paths on a local chain, and deploy to a testnet before mainnet. Keep the first contract small, verify the code on a block explorer, and have it reviewed before it holds real funds.
What is a smart contract?
A smart contract is a small program stored on a blockchain that runs automatically when its conditions are met, and it holds and moves value without a person in the middle. Once deployed, its code and its state are visible on the chain, and its rules run exactly as written.
The usual example is an agreement with money attached: a buyer deposits funds, the contract checks that a condition was met, and it releases the funds to the seller. If the condition is not met, the contract returns the funds or keeps them locked, depending on the rules the author wrote.
A smart contract can do a few basic jobs well:
- Hold funds. It can keep tokens or coins until a condition is met.
- Record ownership. It can track who owns a token, a ticket or a share.
- Enforce rules. It can allow or refuse an action based on who is calling and what the state is.
- Call other contracts. It can hand work to another contract, such as a price feed or a token.
Smart contracts do not understand the outside world on their own. They read data from the chain, such as balances and timestamps, and from oracles when they need prices or events from outside. For how that execution works in practice, how a smart contract works walks through the flow step by step.
Which language and chain should you pick?
You pick the language and chain by the network your users are already on and the tools your team already knows, because a contract is only useful if people can reach it and your team can maintain it.
| Language | Main chains | Best for | What to know |
|---|---|---|---|
| Solidity | Ethereum and EVM chains | First contracts, DeFi, tokens | Largest set of examples and audit tools |
| Rust | Solana | High-throughput apps | Contracts are called programs; a different model |
| Clarity | Stacks | Bitcoin-linked apps | Decidable code; no surprise behaviour |
| Move | Aptos and Sui | Asset-heavy apps | Assets are treated as resources that cannot be copied |
Solidity is the most common starting point, because it has the largest set of examples, libraries and audit tools, and it runs on Ethereum and the many chains that copy its virtual machine. Rust is the choice on Solana, where contracts are called programs and run in a different model. Start with the stack that matches your users, not the one that sounds newest.
How do you write your first smart contract?
You write your first smart contract by defining one small job, writing the smallest code that does it, and testing it on a local chain before it touches real funds.
- Define the job in one sentence. Write down what the contract must do, such as "hold deposits until a deadline, then pay out or refund". If the sentence needs an "and" three times, the job is too big.
- Set up the project. Create a project with a development framework, such as Hardhat or Foundry for Solidity, and a local chain you can reset in seconds.
- Write the storage first. List the facts the contract must remember: who deposited, how much, and when the deadline passes. Storage shapes every function that follows.
- Write the functions. Add one function per action: deposit, check the goal, pay out, refund. Each function should change one thing and check its conditions first.
- Add access rules. State who may call each function. Only the owner should change settings, and only the right person should trigger a payout.
- Test, then deploy to a testnet. Write tests for the happy path and the failure paths, run them locally, and only then deploy to a testnet with fake funds.
Keep the first contract small. A contract with one job and three functions is easier to read, test and audit than one that tries to do everything, and it is the right size to learn the habits that matter later. If you would rather have a team build it, smart contract development covers how a contract is scoped, written and reviewed.
Example (hypothetical): a crowdfunding contract
Example (hypothetical): a team wants to raise 10,000 tokens to fund a small project. The contract accepts deposits until a deadline, and it pays out only if the goal is met; if not, each backer can claim a refund.
Forty backers each deposit 250 tokens, so 40 x 250 = 10,000 tokens, exactly the goal. The contract records each deposit, and when the deadline passes it checks the total. Because the total is 10,000, the funds move to the project wallet.
If only 30 backers had joined, the total would be 30 x 250 = 7,500 tokens, below the goal, so the contract would refuse the payout and each backer could withdraw their 250 tokens. One rule, one check and one refund path cover the whole job.
A platform fee of 2% on the full goal would be 0.02 x 10,000 = 200 tokens, leaving 9,800 tokens for the project. Writing the fee into the contract from the start avoids a dispute later about what was taken and when.
How do you test a smart contract before deploying?
You test a smart contract before deploying by running it on a local chain, writing unit tests for every function, and trying the ways it can fail, not just the ways it should work.
Unit tests should cover each function on its own: a deposit that succeeds, a deposit after the deadline that must fail, a payout before the goal is met that must fail, and a refund that returns exactly what was deposited. A test that only checks the happy path tells you nothing about the cases that lose money.
Then try the hostile cases. Call functions from the wrong address, send the wrong amount, call a function twice, and try to withdraw more than was deposited. Many contract losses come from a sequence of calls that no single test tried, so test sequences, not just single calls.
Finally, run the contract on a testnet with the same steps a real user would follow, and read the transaction results by hand. An independent review catches what tests miss, which is why what a smart contract audit covers is worth reading before you deploy to mainnet.
How do you deploy a smart contract?
You deploy a smart contract by compiling it, sending the deployment transaction from a wallet that holds the chain's native token for gas, and then verifying the code on a block explorer so others can read it.
- Compile and check warnings. Fix every compiler warning, because warnings in contract code often point at real bugs.
- Deploy to a testnet first. Use the same deployment script you will use for mainnet, and check every function on the testnet copy.
- Verify the source code. Publish the source on a block explorer so anyone can check that the deployed code matches what was reviewed.
- Deploy to mainnet. Use a wallet with a small gas balance set aside, and record the contract address and the transaction hash.
- Hand over control carefully. Move admin rights to a multisig wallet, and write down who holds the keys and what they may change.
Deployment is not the end. A deployed contract cannot be changed, so teams either accept that the code is final or build in an upgrade path before launch, with clear rules about who can trigger it. Decide this before the first deployment, not after.
What are the common mistakes?
The most common mistakes when writing a smart contract are skipping tests, trusting user input, and deploying to mainnet before anyone else has read the code. All three are avoidable, and all three have lost real money.
- Only testing the happy path. A contract that works when everyone behaves is not tested; test the wrong caller, the wrong amount and the repeated call.
- Trusting input without checks. Every value that comes in from outside should be checked for size, timing and permission before it changes state.
- Updating state after sending funds. Change balances and flags before any external call, or a caller can re-enter the function and drain the contract.
- Skipping the audit. A second pair of eyes, or a formal review, catches the bug you stopped seeing in your own code.
- No pause or upgrade plan. If a bug is found after launch, the team needs a way to stop the contract or replace it.
The bug patterns behind most of these mistakes are well known. Common smart contract vulnerabilities lists the ones auditors find most often, and what to check for in each.
Key takeaways
- A smart contract is a program on a blockchain that runs its rules automatically and holds value
- Pick the language and chain by where your users are, not by what sounds newest
- Write one small job with clear access rules, then test the failure paths, not just the happy path
- Change balances before any external call, so a caller cannot re-enter the function
- Deploy to a testnet, verify the source code, and get a review before the contract holds real funds
Frequently asked questions
What language should I use to write my first smart contract?
Solidity is the usual choice for a first contract, because it has the most examples, libraries and audit tools, and it runs on Ethereum and the many chains that copy its virtual machine. If your users are on Solana, use Rust instead; the chain your users are on matters more than the language.
Do I need to know a lot of programming to write a smart contract?
You need basic programming skill and a clear grasp of the job the contract must do. The code itself is small, but the hard part is thinking through every way it can be called, including the wrong ways, and writing tests for those cases.
Can I change a smart contract after deploying it?
No, not by default: deployed code is final. Teams either accept that, or they build in an upgrade path before launch, with clear rules about who can trigger an upgrade. Decide this before the first deployment, and put the admin keys in a multisig wallet.
How much does it cost to deploy a smart contract?
Deployment costs a gas fee that varies by chain, contract size and network demand, so check the chain's own fee estimator or a block explorer; see vendor site. The larger costs are usually the build, the testing and the audit, not the deployment transaction itself.
What is the difference between a smart contract and a dApp?
A smart contract is the on-chain program that holds the rules and the funds. A dApp is the whole application: the contract plus the website or app that users interact with, plus any servers that serve it. Most dApps have one or more contracts at their core.
Do I need an audit for a small contract?
Yes, before it holds real funds. Even a small contract can lose everything it holds if it has one bug, and an audit is the cheapest way to find the bugs you stopped seeing in your own code. At minimum, have another developer read it line by line and test the hostile cases.