DAO Smart Contract

Decentralized Autonomous Organizations (DAOs) are an innovative way to organize communities where decisions are made collectively without centralized authority. In this tutorial, we’ll explore a simple DAO implemented in Solidity. By the end, you’ll understand how to deploy and interact with this contract.

Example Use Case: DAO Implementation in Gaming

DAOs can be particularly impactful in gaming environments. Imagine a massive multiplayer online game (MMO) with a shared in-game economy. A DAO can be used to manage a treasury funded by player contributions, allowing players to propose and vote on game updates, community events, or rewards.

For example:

  1. In-Game Treasury Management: Players deposit some of their in-game earnings into a DAO treasury. Proposals for using these funds—such as hosting tournaments or funding new content—are created and voted on.

  2. Player-Driven Governance: Gamers vote on new features like maps, characters, or weapons, giving them a direct say in the game's evolution.

  3. Community Rewards: DAOs could allocate funds to reward top-performing players or teams, enhancing engagement and competition.

This decentralized approach ensures that game updates align with player interests, creating a more engaging and community-driven gaming experience.

Prerequisites

Before starting, ensure you have:

  1. This guide is not an introduction to Solidity Programming; you are expected to understand Basic Solidity Programming.

  2. To complete this guide, you will need MetaMask installed and the Somnia Network added to the list of Networks. If you have yet to install MetaMask, please follow this guide to Connect Your Wallet.

  3. You can deploy the Smart Contracts using our Hardhat or Foundry guides.

Overview of the DAO Contract

The provided DAO contract allows users to:

  1. Deposit funds to gain voting power.

  2. Create proposals.

  3. Vote on proposals.

  4. Execute proposals if they pass.

The key features of the contract include:

  • Proposal Struct: Stores details of proposals.

  • Voting Mechanism: Allows weighted voting based on deposited funds.

  • Execution Logic: Ensures proposals are executed only if approved.

Setting Up the Development Environment

Follow the Hardhat or Foundry guides.

Create the Smart Contract

Create a new file named DAO.sol in the contracts folder and copy the provided contract code.

DAO.sol

Let’s break down the contract into its main components:

Mappings

Mappings are used to store structured data efficiently:

  1. proposals

  • Stores all proposals created in the DAO.

  • It represents the Proposal struct containing details like description, deadline, votes, and proposer.

  1. votingPower

  • Tracks the voting power of each address.

  • Voting power increases when users deposit funds into the DAO.

  1. hasVoted

  • Tracks whether a specific address has voted on a specific proposal.

  • Prevents double voting.

Functions

The contract includes several key functions:

  1. Constructor

  • Sets the deployer as the owner of the contract.

  1. deposit

  • Allows users to deposit STT Tokens to gain voting power.

  • Increases their votingPower by the amount deposited.

  1. createProposal

  • Allows users with voting power to create new proposals.

  • Adds the proposal to the proposals mapping.

  1. vote

  • Allows users to cast a vote on a proposal.

  • Updates the yesVotes or noVotes in the Proposal struct based on the user's choice.

  • Prevents double voting by using the hasVoted mapping.

  1. executeProposal

  • Executes a proposal if it passes (more yes votes than no votes).

  • Transfers a fixed amount of ETH to the proposer as an example of execution logic.

  • Ensures proposals cannot be executed multiple times.

Key Variables

  1. totalProposals

  • Tracks the total number of proposals created.

  1. votingDuration

  • Sets the default duration for voting on proposals.

  1. owner

  • Stores the address of the contract owner.

  • Used for functions that require administrative control.

Understanding these components shows how the DAO enables decentralized governance while maintaining transparency and fairness.

Deploy the Smart Contract to Somnia

Follow the Hardhat or Foundry guides. First, compile the Smart Contract to Bytecode by running the Hardhat or Foundry compile instructions.

This is an example deployment script using Hardhat. Create a file in the /ignition/module folder and name it deploy.js

Before running the deploy command, add Somnia Network to the hardhat.config.js file:

Ensure that the deploying address has enough STT Tokens. You can get STT Tokens from the Faucet.

Run the deployment script:

Congratulations. 🎉 You have successfully deployed the DAO Smart Contract. 🎉

Interacting with the Contract

Use the Hardhat console or scripts to interact with the contract.

1. Deposit Funds

Call the deposit function to gain voting power:

2. Create a Proposal

Create a new proposal by calling createProposal:

3. Vote on a Proposal

Vote on a proposal by specifying its ID and your support (true for yes, false for no):

4. Execute a Proposal

After the voting deadline, execute the proposal if it has majority votes:

Testing the Contract

Writing Tests

Create a test file DAO.test.js in the test folder.

Run the tests:

Enhance the DAO

You can expand this DAO contract by:

  1. Adding Governance Tokens: Reward participants with tokens for voting or executing proposals. Follow the ERC20 Token Guide here.

  2. Implementing Quorums: Require a minimum number of votes for proposals to pass.

  3. Flexible Voting Power: Allow dynamic voting power allocation.

Conclusion

This tutorial provided a foundational understanding of building and deploying a simple DAO on Somnia. Experiment with enhancements to create more complex governance structures. DAOs are a powerful tool for decentralized decision-making, and the possibilities for innovation are limitless!

Last updated