Skip to content
This repository has been archived by the owner on Jul 19, 2024. It is now read-only.

No code DAO #10

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions contracts/no-code-DAO/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Nguyễn Đình Trường

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
143 changes: 143 additions & 0 deletions contracts/no-code-DAO/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
# Solidity Template with Hardhat [![Hardhat][hardhat-badge]][hardhat] [![License: MIT][license-badge]]

[hardhat]: https://hardhat.org/
[hardhat-badge]: https://img.shields.io/badge/Built%20with-Hardhat-FFDB1C.svg
[license]: https://opensource.org/licenses/MIT
[license-badge]: https://img.shields.io/badge/License-MIT-blue.svg

A Hardhat-based template for developing smart contract in Solidity

- [Hardhat](https://github.com/nomiclabs/hardhat): compile, run and test smart contracts
- [TypeChain](https://github.com/ethereum-ts/TypeChain): generate TypeScript bindings for smart contracts
- [Ethers](https://github.com/ethers-io/ethers.js/): renowned Ethereum library and wallet implementation
- [Solhint](https://github.com/protofire/solhint): solidity linter
- [Eslint](https://github.com/eslint/eslint): code linter
- [Solcover](https://github.com/sc-forks/solidity-coverage): code coverage
- [Prettier Plugin Solidity](https://github.com/prettier-solidity/prettier-plugin-solidity): code formatter
- [Husky](https://github.com/typicode/husky): Git hooks

## Getting Started

Click the [`Use this template`](https://github.com/ndtr2000/solidity-hardhat-template/generate) button at the top of the page to
create a new repository with this repo as the initial state.

## Features

This template builds upon the frameworks and libraries mentioned above, so for details about their specific features,
please consult their respective documentations.

### Sensible Defaults

This template comes with sensible default configurations in the following files:

```text
├── .commitlintrc.yml
├── .editorconfig
├── .eslintignore
├── .eslintrc.yml
├── .gitignore
├── .prettierignore
├── .prettierrc.yml
├── .solcover.js
├── .solhintignore
├── .solhint.json
├── .yarnrc.yml
└── hardhat.config.ts
```

## Usage

### Pre Requisites

Before being able to run any command, you need to create a `.env` file and set a BIP-39 compatible mnemonic as an
environment variable. You can follow the example in `.env.example`. If you don't already have a mnemonic, you can use
this [website](https://iancoleman.io/bip39/) to generate one.

Then, proceed with installing dependencies:

```sh
$ yarn install
```

### Compile

Compile the smart contracts with Hardhat:

```sh
$ yarn compile
```

### TypeChain

Compile the smart contracts and generate TypeChain bindings:

```sh
$ yarn typechain
```

### Test

Run the tests with Hardhat:

```sh
$ yarn test
```

### Lint Solidity

Lint the Solidity code:

```sh
$ yarn lint:sol
```

### Lint TypeScript

Lint the TypeScript code:

```sh
$ yarn lint:ts
```

### Coverage

Generate the code coverage report:

```sh
$ yarn coverage
```

### Report Gas

See the gas usage per unit test and average gas per method call:

```sh
$ REPORT_GAS=true yarn test
```

### Clean

Delete the smart contract artifacts, the coverage reports and the Hardhat cache:

```sh
$ yarn clean
```

### Deploy

Deploy the contracts to Hardhat Network:

```sh
$ yarn deploy --greeting "Bonjour, le monde!"
```

### Fix husky not executable

```sh
chmod ug+x .husky/*
chmod ug+x .git/hooks/*
```

## License

[MIT](./LICENSE.md) © Nguyễn Đình Trường
5 changes: 5 additions & 0 deletions contracts/no-code-DAO/contracts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"governor": "0xDc64a140Aa3E981100a9becA4E685f962f0cF6C9",
"implementation": "0xCf7Ed3AccA5a467e9e704C703E8D87F634fB0Fc9",
"timelock": "0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0"
}
196 changes: 196 additions & 0 deletions contracts/no-code-DAO/contracts/GovernorFactory.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.17;

import "@openzeppelin/contracts-upgradeable/proxy/ClonesUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
import "./libraries/Bytes32ToAddressMapUpgradeable.sol";
import "./interfaces/ITimelockControllerInitilizer.sol";

contract GovernorFactory is Initializable, OwnableUpgradeable, UUPSUpgradeable {
using Bytes32ToAddressMapUpgradeable for Bytes32ToAddressMapUpgradeable.Bytes32ToAddressMap;
using ClonesUpgradeable for address;

Bytes32ToAddressMapUpgradeable.Bytes32ToAddressMap private governorPresets;
Bytes32ToAddressMapUpgradeable.Bytes32ToAddressMap private voteTokenPresets;
address public timelockController;
uint256 private totalGovernor;
mapping(uint256 => Governor) public governors;

struct Governor {
address governor;
address voteToken;
address timelock;
}

struct TimelockInitializeParams {
uint256 timelockMinDelay;
address[] timelockProposers;
address[] timelockExecutors;
address timelockAdmin;
}

// ========== Events ==========

event GovernorCreated(
uint256 id,
address governor,
address voteToken,
address timelock
);

// ========== Modifiers ==========

modifier isValidName(string calldata name) {
require(bytes(name).length <= 32, "GovernorFactory: invalid name");
_;
}

// ========== Gorvernance ==========

function initialize(address _timelockController) public initializer {
timelockController = _timelockController;
__Ownable_init();
}

///@dev required by the OZ UUPS module
function _authorizeUpgrade(address) internal override onlyOwner {}

function addGovernorPreset(
string calldata _name,
address _governorPreset
) external onlyOwner isValidName(_name) {
uint8 nameLength = uint8(bytes(_name).length);
bytes32 bytesName = bytes32(abi.encodePacked(_name));
governorPresets.set(bytesName, _governorPreset, nameLength);
}

function addVoteTokenPreset(
string calldata _name,
address _voteTokenPresets
) external onlyOwner isValidName(_name) {
uint8 nameLength = uint8(bytes(_name).length);
bytes32 bytesName = bytes32(abi.encodePacked(_name));
voteTokenPresets.set(bytesName, _voteTokenPresets, nameLength);
}

// ========== Public functions ==========

function createGovernor(
string calldata _governorPreset,
bytes calldata _governorInitializeData,
string calldata _voteTokenPreset,
bytes calldata _voteTokenInitializeData,
TimelockInitializeParams calldata _timelockParams,
bytes32 salt
) external returns (address governor, address voteToken, address timelock) {
governor = getGovernorPresetAddress(_governorPreset).cloneDeterministic(
salt
);
voteToken = getVoteTokenPresetAddress(_voteTokenPreset)
.cloneDeterministic(salt);
timelock = timelockController.cloneDeterministic(salt);

(bool success, bytes memory result) = voteToken.call(
_voteTokenInitializeData
);

require(
success,
"GovernorFactory: failed to call initialize vote token"
);

bool initialized = abi.decode(result, (bool));
require(initialized, "GovernorFactory: failed to initialize governor");

(bool success_2, bytes memory result_2) = governor.call(
_governorInitializeData
);

require(
success_2,
"GovernorFactory: failed to call initialize governor"
);
bool initialized_2 = abi.decode(result_2, (bool));
require(
initialized_2,
"GovernorFactory: failed to initialize governor"
);

ITimelockControllerInitilizer(timelock).initialize(
_timelockParams.timelockMinDelay,
_timelockParams.timelockProposers,
_timelockParams.timelockExecutors,
_timelockParams.timelockAdmin
);

uint256 governorId = totalGovernor;
governors[governorId] = Governor(governor, voteToken, timelock);

emit GovernorCreated(governorId, governor, voteToken, timelock);

totalGovernor++;
}

function initializeGovernor() external {}

// ========== View functions ==========
function predictGovernorDeterministicAddress(
string calldata _governorPreset,
bytes32 _salt
) external view returns (address) {
return
getGovernorPresetAddress(_governorPreset)
.predictDeterministicAddress(_salt);
}

function predictVoteTokenDeterministicAddress(
string calldata _voteTokenPreset,
bytes32 _salt
) external view returns (address) {
return
getVoteTokenPresetAddress(_voteTokenPreset)
.predictDeterministicAddress(_salt);
}

function predictTimelockDeterministicAddress(
bytes32 _salt
) external view returns (address) {
return timelockController.predictDeterministicAddress(_salt);
}

function getAllGovernorPresets() external view returns (string[] memory) {
bytes[] memory keysBytes = governorPresets.keysPacked();
string[] memory keys = new string[](keysBytes.length);
for (uint256 i = 0; i < keysBytes.length; i++) {
keys[i] = string(keysBytes[i]);
}
return keys;
}

function getAllVoteTokenPresets() external view returns (string[] memory) {
bytes[] memory keysBytes = voteTokenPresets.keysPacked();
string[] memory keys = new string[](keysBytes.length);
for (uint256 i = 0; i < keysBytes.length; i++) {
keys[i] = string(keysBytes[i]);
}
return keys;
}

function getGovernorPresetAddress(
string calldata _name
) public view isValidName(_name) returns (address) {
bytes32 bytesName = bytes32(abi.encodePacked(_name));
return governorPresets.get(bytesName);
}

function getVoteTokenPresetAddress(
string calldata _name
) public view isValidName(_name) returns (address) {
bytes32 bytesName = bytes32(abi.encodePacked(_name));
return voteTokenPresets.get(bytesName);
}

// ========== Private functions ==========
}
Loading