Skip to content

Smart Contracts: Automated Agreements on the Blockchain

Haikel Fazzani Haikel Fazzani
2025-02-30

1. What Are Smart Contracts?

Imagine a special kind of computer program that lives on a blockchain (like a super secure, public record book). This program automatically runs an agreement when certain conditions are met. That’s a smart contract! No need for lawyers or banks to make sure things happen; the code does it all.

Smart Contracts

Think of it like a Vending Machine:

You put money into a vending machine, pick your snack, and if you’ve put in enough money, the machine automatically gives you the snack. A smart contract works similarly: if certain conditions are met (like a payment being received), it automatically performs an action (like sending you something digital).


2. Why Are They So Cool?


3. How Do Smart Contracts Work?

Smart contracts typically run on blockchains like Ethereum. Think of it as their home.

They are usually written in special programming languages. The most popular one for Ethereum is Solidity, which looks a bit like JavaScript.

Here’s the simple flow:

  1. Write the Code: You write the smart contract rules using a language like Solidity.
  2. Put it on the Blockchain: You upload your contract’s code to the blockchain. This makes it live and accessible.
  3. It Waits: The contract just sits there, waiting for its conditions to be met.
  4. It Executes: When those conditions are finally met (someone sends money, a deadline passes, etc.), the contract automatically runs its programmed actions.
  5. Record Keeping: Any changes the contract makes (like updating a balance) are recorded forever on the blockchain.

4. Let’s Look at Some Super Simple Code!

Don’t worry if you’ve never coded before; we’ll break these down. These examples use Solidity.

Example 1: Storing a Number

This contract is like a digital sticky note that can only hold one number at a time. You can write a number to it, and read the number back later.

// This tells the compiler which Solidity version to use
pragma solidity ^0.8.0;

// This defines our smart contract called "SimpleStorage"
contract SimpleStorage {
    // This variable will store a number on the blockchain forever!
    uint256 public storedNumber;

    // This function lets anyone save a new number
    function setNumber(uint256 _newNumber) public {
        storedNumber = _newNumber; // Update the stored number
    }

    // This function lets anyone see the currently stored number
    function getNumber() public view returns (uint256) {
        return storedNumber; // Give back the stored number
    }
}

What’s happening here?

Example 2: Making Your Own Digital Money (a Basic Token)

This is a super simplified version of how digital currencies like Bitcoin or Ethereum are made. We’ll use a ready-made template to keep it easy.

pragma solidity ^0.8.0;
// We're importing a standard template for tokens from OpenZeppelin, a trusted source.
// Think of it like using a cookie cutter for making cookies.
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

// Now we create our own token! We'll call it "MyAwesomeToken"
contract MyAwesomeToken is ERC20 {
    // This special function runs only ONCE when the contract is first put on the blockchain
    constructor() ERC20("MyAwesomeToken", "MAT") {
        // Here, we're giving the person who launched this contract 1 million tokens.
        // The 10**18 makes sure we account for tiny decimal parts of the token.
        _mint(msg.sender, 1000000 * 10**18);
    }
}

What’s happening here?

Example 3: Simple Fundraiser

This contract lets people contribute money towards a goal, and the organizer can take the money if the goal is met by a deadline.

pragma solidity ^0.8.0;

contract SimpleFundraiser {
    address public organizer; // The person who started the fundraiser
    uint256 public goalAmount; // The target amount of money we want to raise (in Wei - tiny ETH units)
    uint256 public deadline; // When the fundraiser ends (a specific time)
    mapping(address => uint256) public contributions; // Keeps track of how much each person contributed

    // This runs when you first create the fundraiser contract
    constructor(uint256 _goal, uint256 _durationInSeconds) {
        organizer = msg.sender; // The person launching this is the organizer
        goalAmount = _goal;
        deadline = block.timestamp + _durationInSeconds; // Calculate the end time
    }

    // This function lets people send money to the fundraiser
    function contribute() public payable {
        // Make sure the fundraiser isn't over yet
        require(block.timestamp <= deadline, "Sorry, the fundraiser is over!");
        // Record how much this person contributed
        contributions[msg.sender] += msg.value;
    }

    // This function lets the organizer get the money
    function withdrawFunds() public {
        // Only the organizer can call this function
        require(msg.sender == organizer, "Only the fundraiser organizer can withdraw.");
        // Make sure the deadline has passed
        require(block.timestamp > deadline, "Please wait until the fundraiser ends.");
        // Make sure we met our goal!
        require(address(this).balance >= goalAmount, "Goal not met yet!");

        uint256 amountCollected = address(this).balance; // How much money we actually have
        // Send all the collected money to the organizer
        payable(organizer).transfer(amountCollected);
    }
}

What’s happening here?

(Note: This example is simplified and doesn’t include automatic refunds if the goal isn’t met – that would need more advanced code.)


5. Real-World Uses of Smart Contracts

Smart contracts are the backbone of many exciting things:


6. Things to Keep in Mind

While amazing, smart contracts have challenges:


7. The Future is Bright!

Smart contracts are constantly evolving. We’ll see them connect across different blockchains, integrate with artificial intelligence, and become even more reliable.


8. Ready to Try It?

Smart contracts are truly changing how agreements work in the digital world. If you’re curious, you can start experimenting with tools like Remix IDE (an online tool for writing and testing Solidity contracts) to play with these examples yourself!


smart contracts blockchain Solidity DeFi Ethereum code examples tutorial decentralized applications dApps

More Insights.