Operate your community-driven project using a DAO

Build a decentralized autonomous organization for your community where people can submit and then vote on proposals.

Write a DAO smart contract

Extending the OpenZeppelin Governor contract, we create a governance contract that let the community makes decisions for the protocol.

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

import "@openzeppelin/contracts/governance/Governor.sol";
import "@openzeppelin/contracts/governance/compatibility/GovernorCompatibilityBravo.sol";
import "@openzeppelin/contracts/governance/extensions/GovernorVotes.sol";
import "@openzeppelin/contracts/governance/extensions/GovernorVotesQuorumFraction.sol";
import "./ERC20Vote.sol";

contract GovernorVote is Governor, GovernorCompatibilityBravo, GovernorVotes, GovernorVotesQuorumFraction {
    constructor(IVotes _token)
    Governor("MyGovernor")
    GovernorVotes(_token)
    GovernorVotesQuorumFraction(4)
    {}

    function votingDelay() public pure override returns (uint256) {
        return 20; // 20 blocks
    }

    function votingPeriod() public pure override returns (uint256) {
        return 100; // 100 blocks
    }

    function proposalThreshold() public pure override returns (uint256) {
        return 0;
    }

    // The functions below are overrides required by Solidity.

    function state(uint256 proposalId)
    public
    view
    override(Governor, IGovernor)
    returns (ProposalState)
    {
        return super.state(proposalId);
    }

    function propose(address[] memory targets, uint256[] memory values, bytes[] memory calldatas, string memory description)
    public
    override(Governor, GovernorCompatibilityBravo)
    returns (uint256)
    {
        return super.propose(targets, values, calldatas, description);
    }

    function _execute(uint256 proposalId, address[] memory targets, uint256[] memory values, bytes[] memory calldatas, bytes32 descriptionHash)
    internal
    override(Governor)
    {
        super._execute(proposalId, targets, values, calldatas, descriptionHash);
    }

    function _cancel(address[] memory targets, uint256[] memory values, bytes[] memory calldatas, bytes32 descriptionHash)
    internal
    override(Governor)
    returns (uint256)
    {
        return super._cancel(targets, values, calldatas, descriptionHash);
    }

    function _executor()
    internal
    view
    override(Governor)
    returns (address)
    {
        return super._executor();
    }

    function supportsInterface(bytes4 interfaceId)
    public
    view
    override(Governor, IERC165)
    returns (bool)
    {
        return super.supportsInterface(interfaceId);
    }

    // override unused functions
    function proposalEta(uint256) public pure override returns (uint256) {
        return 0;
    }

    function timelock() public pure override returns (address) {
        return address(0x0);
    }

    function queue(
        address[] memory,
        uint256[] memory,
        bytes[] memory,
        bytes32
    ) public pure override returns (uint256) {
        return 0;
    }
}

contract DAO is GovernorVote {
    constructor() GovernorVote(new MyERC20VoteToken()) {
        IERC20(address(token)).transfer(msg.sender, 1e22); // 10000 tokens
    }
}

Clone the smart contract examples

We've prepared a few different examples for you to get started.

git clone https://github.com/trustlesscomputer/smart-contract-examples.git

Compile the contracts

To compile your contracts, use the built-in hardhat compile task.

cd smart-contract-examples
npm install
npx hardhat compile

Deploy the contracts

Review config file hardhat.config.ts. The network configs should look like this.

  networks: {
    mynw: {
      url: "http://localhost:10002",
      accounts: {
        mnemonic: "<your mnemonic with funds>"
      },
      timeout: 100_000,
    },
    blockscoutVerify: {
      blockscoutURL: "http://localhost:4000", // your explorer URL
      ...
    }
  }

Run the deploy scripts using hardhat-deploy.

npx hardhat deploy --tags DAO

Make sure the accounts in hardhat.config.ts have some TC in your Layer 2 that are bridged from Bitcoin Virtual Machine Layer 1.

Interact with the contracts

Once the contracts are deployed, you can interact with them. We've prepared a few hardhat tasks to make it easy for you to interact with the contracts.

npx hardhat createProposal --target <your-address-to-receive-funds> --value <proposal-amount> --description <proposal-description>
npx hardhat castVote --id <proposal-id> --vote <voting-amount>
npx hardhat getState --id <proposal-id>

Last updated