0
0
BlockchainConceptBeginner · 4 min read

What is DAO in Solidity: Definition and Example

A DAO (Decentralized Autonomous Organization) is a smart contract on the blockchain that runs rules automatically without a central authority. It allows members to vote and make decisions transparently and securely using Solidity code.
⚙️

How It Works

A DAO works like a digital club where all members have a say in decisions. Instead of one boss, the rules are written in code and run automatically on the blockchain. This means no one can cheat or change the rules without everyone's agreement.

Imagine a group of friends pooling money to buy something. They write rules about how to spend the money and vote on each purchase. The DAO enforces these rules without needing a leader, making everything fair and transparent.

💻

Example

This simple Solidity example shows a DAO where members can propose spending funds and vote to approve or reject proposals.

solidity
pragma solidity ^0.8.0;

contract SimpleDAO {
    address public owner;
    mapping(address => bool) public members;
    uint public memberCount;

    struct Proposal {
        string description;
        uint amount;
        uint votesFor;
        uint votesAgainst;
        bool executed;
        mapping(address => bool) voted;
    }

    Proposal[] public proposals;

    modifier onlyMember() {
        require(members[msg.sender], "Not a member");
        _;
    }

    constructor() {
        owner = msg.sender;
        members[msg.sender] = true;
        memberCount = 1;
    }

    function addMember(address _member) public {
        require(msg.sender == owner, "Only owner can add members");
        require(!members[_member], "Already a member");
        members[_member] = true;
        memberCount++;
    }

    function createProposal(string memory _desc, uint _amount) public onlyMember {
        proposals.push();
        Proposal storage p = proposals[proposals.length - 1];
        p.description = _desc;
        p.amount = _amount;
        p.executed = false;
    }

    function vote(uint _proposalId, bool _support) public onlyMember {
        Proposal storage p = proposals[_proposalId];
        require(!p.voted[msg.sender], "Already voted");
        p.voted[msg.sender] = true;
        if (_support) {
            p.votesFor++;
        } else {
            p.votesAgainst++;
        }
    }

    function executeProposal(uint _proposalId) public onlyMember {
        Proposal storage p = proposals[_proposalId];
        require(!p.executed, "Already executed");
        require(p.votesFor > p.votesAgainst, "Not enough votes");
        p.executed = true;
        // In real DAO, here would be code to transfer funds
    }

    // Function to receive Ether
    receive() external payable {}
}
Output
No direct output; contract deploys and allows proposals, voting, and execution.
🎯

When to Use

Use a DAO when you want a group to manage funds or decisions without trusting a single person. It is great for communities, clubs, or projects where transparency and fairness are important.

Common uses include managing investment funds, charity donations, or collective ownership of assets. DAOs help avoid conflicts by automating rules and voting.

Key Points

  • A DAO is a smart contract that runs rules automatically on the blockchain.
  • Members vote on proposals to make decisions together.
  • It removes the need for a central leader or authority.
  • DAOs increase transparency and trust in group decisions.

Key Takeaways

A DAO is a blockchain-based organization controlled by code and member votes.
It automates decision-making without a central authority.
DAOs are useful for transparent and fair group management.
Solidity smart contracts implement DAO rules and voting.
DAOs help avoid conflicts by enforcing agreed rules automatically.