
Smart Contracts and Web3: A Complete Guide to Building DEXs and CEXs for Iranian and International Businesses
What smart contracts are, how DEXs and CEXs actually work, which one to build, and how security, sanctions, and local-market constraints differ between Iran and the rest of the world.
Quick answer
A smart contract is a program that runs on a blockchain and enforces pre-written rules without an intermediary. A centralized exchange (CEX) works like a bank: it holds user funds and matches orders on its own servers. A decentralized exchange (DEX) does the same job with smart contracts while users keep custody of their assets. Building a CEX is mostly a problem of infrastructure security, liquidity, and regulation; building a DEX is mostly a problem of code security and liquidity bootstrapping. Neither fits everyone, and the right choice depends on your audience, target market, and the team's ability to operate it long term.
What exactly is a smart contract, and what is it not?
A smart contract is a small program deployed to a blockchain such as Ethereum. Anyone can call its functions, and every node on the network verifies the result. A simple example: a contract that says, if the buyer pays the agreed amount, transfer the token to them. That logic runs without a company, a bank, or a lawyer in the middle.
A few misconceptions are common. Smart does not mean artificial intelligence; the contract is just code that does exactly what it was written to do, right or wrong. Contract also does not mean a legal contract, and without a legal framework it is not automatically enforceable in court. Most importantly, once a contract is deployed it is hard or impossible to change. A bug in the code is a bug in whatever money that code controls.
That is why engineering quality, testing, and security review matter more in Web3 than in most software. On a normal website you fix a bug tomorrow; on a deployed contract, user funds can be gone the same night.
Which chain and which language? Choosing the technical foundation
Most projects start on EVM-compatible chains because the tooling, libraries, and developer pool are largest there. The main language is Solidity, and the usual tools for testing and deployment are Hardhat and Foundry. Libraries such as OpenZeppelin ship reviewed implementations of standards like ERC-20, and it is almost always better to use those than to write them from scratch.
Choosing a network comes down to transaction cost, speed, and where your users are. Ethereum mainnet offers strong security and credibility but fees rise when the network is busy. Layer-2 networks such as Arbitrum, Optimism, and Base cost less while inheriting Ethereum's security. TRON is widely used for USDT transfers, and Solana uses a different architecture and language (Rust), which splits the development path entirely.
- Fungible token standard: ERC-20; unique tokens (NFTs): ERC-721 and ERC-1155
- Deploy to a testnet first, and move to mainnet only after testing and review
- Decide whether the contract is upgradeable or fixed; each has its own risks and benefits
How does a centralized exchange (CEX) work?
On a CEX the user signs up, completes identity verification, deposits assets into the exchange's wallets, and effectively lets the exchange hold them. Buy and sell orders go into a matching engine on the exchange's servers, and trades are recorded in the exchange's database, not on the blockchain. The blockchain only gets involved at deposit and withdrawal time.
That architecture delivers speed and a polished experience: limit and market orders, leverage, customer support, and fiat gateways. The price is trust. Users must trust the exchange's security and honesty, and market history is full of exchange hacks and collapses; the FTX failure in 2022 is the best-known example.
Building a serious CEX is far more than writing a matching engine. You need hot and cold wallet management, an accurate ledger, KYC and AML, fraud monitoring, support, and above all liquidity and the right licenses.
- Strengths: high speed, advanced order types, human support, fiat gateway options
- Risks: custody of user funds, hacks, regulation and licensing, user trust
- Main cost: security infrastructure, operations, and compliance teams, not just development
How does a decentralized exchange (DEX) work?
On a DEX the user connects their own wallet (such as MetaMask) and the assets never move into an exchange account. Trades happen directly against a smart contract. The most popular model is the AMM, or automated market maker: instead of an order book there is a liquidity pool holding two tokens, and the price is computed from the pool's balances. In the simplest form (Uniswap v2), the product of the two balances stays constant: x × y = k.
Liquidity is supplied by users called liquidity providers, who earn a share of trading fees. The model has its own risks: impermanent loss, slippage in shallow pools, and attacks such as front-running and MEV, where bots profit from transaction ordering.
One important point: copying a well-known DEX's code is technically easy, but the hard business problem is attracting liquidity and users. Without liquidity, prices are poor and users do not return, so plan liquidity acquisition together with the technical design from day one.
- Strengths: users keep custody, code and transactions are transparent, no signup required
- Risks: contract bugs, price-oracle manipulation, a harder experience for newcomers
- Main cost: code security, independent audits, and liquidity acquisition
DEX or CEX? How to choose
It comes down to one question: who holds the user's assets, and what responsibility are you willing to take on? If your audience is everyday, less technical users who want support and a local-currency gateway, the centralized model is more natural, along with all its obligations and risks. If your audience already uses wallets and DeFi and you want a market for a specific token or asset, a DEX or a simple liquidity pool is usually the shorter path.
Many projects do not need a full exchange at all. If the goal is a token, loyalty rewards, an initial sale, a staking pool, or an NFT marketplace, one or a few smart contracts plus a good frontend are enough, at much lower cost and risk.
- Everyday users + local-currency gateway + support → CEX or partnering with an existing exchange
- Wallet-native users + a specific token market → DEX or a liquidity pool
- Just a token, staking, or NFTs → a few smart contracts and a dApp, no exchange needed
Security: where Web3 projects fail
Most major attacks on Web3 projects come from a handful of known patterns, and most are preventable. Security is not a final step; it has to be part of the design from day one.
- Reentrancy: the contract sends funds to another contract before updating its own state. The checks-effects-interactions pattern and a reentrancy guard control it
- Weak access control: sensitive functions callable by anyone. Keep permissions minimal and explicit
- Price oracle manipulation: never base decisions solely on the spot price of a small pool
- Admin and private keys: use multisig and timelocks so one leaked key cannot take down the whole project
- Testing and audits: unit tests, fuzzing, internal review, then an independent audit before mainnet, and finally a bug bounty
- Since Solidity 0.8, arithmetic overflow reverts by default, but logic bugs remain entirely your responsibility
Iran and international markets: what actually differs in practice
Technically a smart contract is the same everywhere; what differs is the surrounding environment. In international markets the main concern is compliance with each target country's rules: licensing for digital-asset services, KYC/AML requirements, and geographic restrictions. These vary widely between countries, and you should consult legal counsel in the specific market before you build.
For businesses operating in Iran or serving Iranian users, a few more factors apply. Sanctions and payment restrictions limit access to some international services (including certain exchanges, payment providers, and cloud services) and shape infrastructure choices. Domestic crypto regulation may also change frequently. We do not provide legal advice, and we recommend checking the current rules with official sources and a qualified lawyer before deciding anything; no project should be built on circumventing the law.
Product expectations differ too: Iranian users usually need a Persian, right-to-left interface, Persian-language support, and a simple deposit and withdrawal experience, and the domestic market is highly sensitive to currency conversion and price stability. International markets expect English documentation, public contract audits, and more transparency. That is why projects targeting both markets are usually built bilingual from the start, with a modular architecture and international security standards.
A suggested path for starting a project
A Web3 project should not begin with one heavy, sudden step. A sensible path is incremental, and at each stage you can continue or stop.
- 1. Define the problem: who uses it, what assets move, and do you genuinely need a blockchain?
- 2. Architecture and threat model: chain choice, standards, roles, and risk points
- 3. Build the contracts with reviewed libraries and scenario-driven tests (Hardhat or Foundry)
- 4. Deploy to a testnet, build the dApp frontend, and connect wallets
- 5. Independent audit, fix findings, and release gradually with capped limits
- 6. Monitoring, support, and an update plan after launch
The takeaway
A smart contract is a powerful tool, but only when it solves the right problem. Before building a DEX or CEX, ask yourself whether you truly need an exchange or whether a token and a few simple contracts are enough. After that, treat security, liquidity, and compliance as seriously as the code itself.
If you plan a Web3 project, the Sosho Studio team can help with architecture, smart contract development and testing, and building the dApp frontend. Start from the contact section by describing your goal and target market.
Frequently asked questions
What is the main difference between a DEX and a CEX?
On a CEX the exchange holds user assets and matches orders on its own servers. On a DEX trades execute against a smart contract and users keep their assets in their own wallets.
Is it easier to build a DEX or a CEX?
Technically a basic DEX has less code, but contract security and liquidity acquisition are the main challenges. A CEX needs larger security infrastructure, operations, and regulatory compliance. Neither is simple, and the cost and risk of both depend on the project's scale.
Does every Web3 project need a security audit?
If the contract holds or moves real user assets, an independent audit before release is strongly recommended. It is not needed for experimental testnet prototypes, but testing and internal review are always necessary.
Can a smart contract be changed after deployment?
Not by default. With an upgradeable (proxy) pattern the logic can be changed, but it brings its own risks and requires users to trust whoever controls upgrades; this is usually controlled with a multisig and a timelock.
Is building a crypto exchange legal in Iran and for global markets?
It depends on each market's laws and licensing situation, and it changes often. We do not give legal advice; before building, check the current rules and restrictions (including sanctions) with official sources and a qualified lawyer in that market.