Smart contracts basic guide

What are EVM Smart Contracts?

Smart contracts are self-executing programs that run on the Ethereum Virtual Machine (EVM). They automatically enforce agreements and execute transactions when predetermined conditions are met, without requiring intermediaries.

How EVM Works

The Ethereum Virtual Machine (EVM) is a decentralized computing environment that:

  • Executes smart contract bytecode across thousands oaf nodes

  • Maintains consensus through blockchain technology

  • Processes transactions in a deterministic manner

  • Charges "gas" fees for computational resources

  • Provides immutable code execution once deployed

Execution Flow:

  1. Smart contract written in Solidity

  2. Compiled to EVM bytecode

  3. Deployed to blockchain with unique address

  4. Users interact by sending transactions

  5. EVM executes code and updates blockchain state

Limitations

Technical Limitations

  • Gas Limits: Complex operations can exceed block gas limits

  • Immutability: Deployed contracts cannot be modified (unless designed with upgradability)

  • Storage Costs: On-chain storage is expensive

  • Deterministic Only: Cannot access external APIs directly

Practical Limitations

  • Scalability: Limited transactions per second

  • High Fees: Gas costs can be prohibitive during network congestion

  • Security Risks: Bugs can lead to permanent fund loss

  • User Experience: Requires wallet setup and gas fee understanding

Becoming a Solidity Developer

Learning Path

  1. Prerequisites

    • Basic programming knowledge (JavaScript/Python helpful)

    • Understanding of blockchain concepts

    • Familiarity with command line

  2. Core Skills

    • Solidity Language: Syntax, data types, functions

    • Development Tools: Hardhat, Truffle, Remix IDE

    • Testing: Unit tests, integration tests

    • Security: Common vulnerabilities and best practices


OpenZeppelin Contracts

OpenZeppelin provides battle-tested, secure smart contract libraries that implement common standards and patterns.

Key Features

  • Security: Audited by security experts

  • Standards: ERC20, ERC721, ERC1155 implementations

  • Modularity: Reusable components

  • Upgradability: Proxy patterns for contract upgrades

  • ERC20: Fungible tokens

  • ERC721: Non-fungible tokens (NFTs)

  • Ownable: Access control

  • ReentrancyGuard: Prevents reentrancy attacks

  • Pausable: Emergency stop functionality

ERC vs DEX Standards

Understanding the Difference

It's important to clarify that DEX standards are not extensions or replacements of ERC standards. They are completely separate concepts:

ERC Standards: Token interface specifications on EthereumDEX Standards: Token standards on DEX Chain (a different blockchain)

  • ERC Standards: Token interface specifications on Ethereum

  • DEX Standards: Token standards on DEX Chain (a different blockchain)

ERC Standards (Ethereum Request for Comments)

ERC standards define token interfaces on the Ethereum blockchain:

Standard

Type

Description

Key Functions

ERC20

Fungible Tokens

Standard interface for identical, interchangeable tokens

transfer(), approve(), balanceOf()

ERC721

Non-Fungible Tokens

Standard for unique, non-interchangeable tokens

ownerOf(), transferFrom(), tokenURI()

ERC1155

Multi-Token

Supports both fungible and non-fungible tokens in one contract

balanceOf(), safeBatchTransferFrom()

DEX Standards (DEX Chain)

DEX standards are token specifications for the DEX Chain blockchain (a separate blockchain ecosystem):

Standard

Ethereum Equivalent

Compatibility

Description

DEX20

ERC20

✅ Fully Compatible

Fungible tokens on DEX Chain with same interface as ERC20

DEX721

ERC721

✅ Fully Compatible

Non-fungible tokens on DEX Chain with same interface as ERC721

DEX1155

ERC1155

✅ Fully Compatible

Multi-token standard on DEX Chain with same interface as ERC1155

Simple Smart Contract Example

Here's a basic token contract using OpenZeppelin:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";

contract MyToken is ERC20, Ownable {
    uint256 public constant MAX_SUPPLY = 1000000 * 10**18; // 1 million tokens

    constructor(
        string memory name,
        string memory symbol,
        address initialOwner
    ) ERC20(name, symbol) Ownable(initialOwner) {
        // Mint initial supply to owner
        _mint(initialOwner, 100000 * 10**18); // 100k tokens
    }

    // Only owner can mint new tokens
    function mint(address to, uint256 amount) public onlyOwner {
        require(totalSupply() + amount <= MAX_SUPPLY, "Exceeds max supply");
        _mint(to, amount);
    }

    // Burn tokens from caller's balance
    function burn(uint256 amount) public {
        _burn(msg.sender, amount);
    }

    // Get token balance of an address
    function getBalance(address account) public view returns (uint256) {
        return balanceOf(account);
    }
}

Contract Explanation

  • Inheritance: Extends ERC20(which is compatible with DEX20) and Ownable (access control)

  • Constructor: Sets token name, symbol, and initial owner

  • Mint Function: Owner-only function to create new tokens

  • Burn Function: Allows users to destroy their tokens

  • Max Supply: Prevents unlimited token creation

Next Steps

  1. Practice: Build simple contracts and gradually increase complexity

  2. Security: Learn about common vulnerabilities (reentrancy, overflow, etc.)

  3. Testing: Write comprehensive tests for your contracts

  4. Community: Join solidity developer communities and forums

Remember: Smart contract development requires careful attention to security, as deployed contracts handle real value and cannot be easily modified.

Last updated