How To Write And Deploy Your First Smart Contract

·

Smart contracts are self-executing programs stored on blockchains that facilitate, verify, and enforce agreements without intermediaries. They enable transparent, automated transactions powered by immutable code.

This guide walks you through writing, testing, and deploying an ERC-20 token contract on Ethereum using Solidity. By the end, you’ll have a functional smart contract capable of:

👉 Ready to dive into blockchain development?

Why Smart Contracts Matter

Smart contracts hold over $142 billion in digital assets, revolutionizing industries like finance, supply chain, and real estate. Their advantages include:

Prerequisites

  1. MetaMask Wallet: Install the Chrome or Firefox extension.
  2. Test ETH: Obtain Kovan testnet ETH from a faucet.
  3. Solidity Basics: Review the official docs.
  4. Remix IDE: Use this web-based editor.

Designing the Smart Contract

Our MyToken contract will:

Writing the Contract

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

interface IERC20 {
    function totalSupply() external view returns (uint256);
    function balanceOf(address account) external view returns (uint256);
    function transfer(address recipient, uint256 amount) external returns (bool);
}

contract MyToken is IERC20 {
    string public constant name = "MyToken";
    string public constant symbol = "MTKN";
    uint8 public constant decimals = 18;
    uint256 public totalSupply;
    mapping(address => uint256) private balances;

    constructor() {
        totalSupply = 1000000 * (10 ** decimals);
        balances[msg.sender] = totalSupply;
    }

    function totalSupply() public override view returns (uint256) {
        return totalSupply;
    }

    function balanceOf(address tokenOwner) public override view returns (uint256) {
        return balances[tokenOwner];
    }

    function transfer(address receiver, uint256 numTokens) public override returns (bool) {
        require(numTokens <= balances[msg.sender]);
        balances[msg.sender] -= numTokens;
        balances[receiver] += numTokens;
        emit Transfer(msg.sender, receiver, numTokens);
        return true;
    }
}

Key Components:

Compiling and Deploying

  1. Compile in Remix to generate ABI/bytecode.
  2. Connect MetaMask to Kovan Testnet.
  3. Deploy via "Injected Web3" and confirm the transaction.

👉 Explore advanced deployment tools like Hardhat

Interacting with the Contract

  1. Check Balances: Call balanceOf() with your address.
  2. Transfer Tokens: Use transfer() to send tokens to another account.

Extending Functionality

Consider adding:

function burn(uint256 amount) public {
    require(balances[msg.sender] >= amount);
    balances[msg.sender] -= amount;
    totalSupply -= amount;
}

Security Best Practices

Beyond ERC-20

FAQ

Q: How much does deploying a smart contract cost?
A: Fees depend on contract complexity and network congestion. Testnets like Kovan use free ETH.

Q: Can I update a deployed contract?
A: No. Smart contracts are immutable—plan upgrades via proxy patterns.

Q: What’s the difference between ETH and tokens?
A: ETH is the native cryptocurrency, while tokens (like ERC-20) are assets built atop Ethereum.

Conclusion

You’ve now deployed a functional ERC-20 token! From here, explore:

Smart contracts unlock endless possibilities for decentralized innovation. Happy coding!

👉 Start building your next blockchain project today