1/* This file is part of DarkFi (https://dark.fi)
2 *
3 * Copyright (C) 2020-2025 Dyne.org foundation
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
1819//! Smart contract implementing Anonymous DAOs on DarkFi.
2021use darkfi_sdk::error::ContractError;
2223/// Functions available in the contract
24#[repr(u8)]
25#[derive(PartialEq, Debug)]
26pub enum DaoFunction {
27 Mint = 0x00,
28 Propose = 0x01,
29 Vote = 0x02,
30 Exec = 0x03,
31 AuthMoneyTransfer = 0x04,
32}
3334impl TryFrom<u8> for DaoFunction {
35type Error = ContractError;
3637fn try_from(b: u8) -> core::result::Result<Self, Self::Error> {
38match b {
390x00 => Ok(DaoFunction::Mint),
400x01 => Ok(DaoFunction::Propose),
410x02 => Ok(DaoFunction::Vote),
420x03 => Ok(DaoFunction::Exec),
430x04 => Ok(DaoFunction::AuthMoneyTransfer),
44_ => Err(ContractError::InvalidFunction),
45 }
46 }
47}
4849/// Internal contract errors
50pub mod error;
5152/// Call parameters definitions
53pub mod model;
5455#[cfg(not(feature = "no-entrypoint"))]
56/// WASM entrypoint functions
57pub mod entrypoint;
5859#[cfg(feature = "client")]
60/// Client API for interaction with this smart contract
61pub mod client;
6263// These are the different sled trees that will be created
64pub const DAO_CONTRACT_DB_INFO_TREE: &str = "dao_info";
65pub const DAO_CONTRACT_DB_DAO_BULLAS: &str = "dao_bullas";
66pub const DAO_CONTRACT_DB_DAO_MERKLE_ROOTS: &str = "dao_roots";
67pub const DAO_CONTRACT_DB_PROPOSAL_BULLAS: &str = "dao_proposals";
68pub const DAO_CONTRACT_DB_VOTE_NULLIFIERS: &str = "dao_vote_nullifiers";
6970// These are keys inside the info tree
71pub const DAO_CONTRACT_KEY_DB_VERSION: &[u8] = b"db_version";
72pub const DAO_CONTRACT_KEY_DAO_MERKLE_TREE: &[u8] = b"dao_merkle_tree";
73pub const DAO_CONTRACT_KEY_LATEST_DAO_ROOT: &[u8] = b"dao_last_root";
7475/// zkas dao mint circuit namespace
76pub const DAO_CONTRACT_ZKAS_DAO_MINT_NS: &str = "Mint";
77/// zkas dao vote input circuit namespace
78pub const DAO_CONTRACT_ZKAS_DAO_VOTE_INPUT_NS: &str = "VoteInput";
79/// zkas dao vote main circuit namespace
80pub const DAO_CONTRACT_ZKAS_DAO_VOTE_MAIN_NS: &str = "VoteMain";
81/// zkas dao propose input circuit namespace
82pub const DAO_CONTRACT_ZKAS_DAO_PROPOSE_INPUT_NS: &str = "ProposeInput";
83/// zkas dao propose main circuit namespace
84pub const DAO_CONTRACT_ZKAS_DAO_PROPOSE_MAIN_NS: &str = "ProposeMain";
85/// zkas dao exec circuit namespace
86pub const DAO_CONTRACT_ZKAS_DAO_EXEC_NS: &str = "Exec";
87/// zkas dao early exec circuit namespace
88pub const DAO_CONTRACT_ZKAS_DAO_EARLY_EXEC_NS: &str = "EarlyExec";
89/// zkas dao auth money_transfer circuit namespace
90pub const DAO_CONTRACT_ZKAS_DAO_AUTH_MONEY_TRANSFER_NS: &str = "AuthMoneyTransfer";
91/// zkas dao auth money_transfer encrypted coin circuit namespace
92pub const DAO_CONTRACT_ZKAS_DAO_AUTH_MONEY_TRANSFER_ENC_COIN_NS: &str = "AuthMoneyTransferEncCoin";
9394/// Not allowed to make proposals using snapshots with block heights older than this depth
95pub const PROPOSAL_SNAPSHOT_CUTOFF_LIMIT: u32 = 100;
9697// ANCHOR: dao-blockwindow
98const _SECS_IN_HOUR: u64 = 60 * 60;
99const _WINDOW_TIME_HR: u64 = 4;
100// Precalculating the const for better performance
101// WINDOW_TIME = WINDOW_TIME_HR * SECS_IN_HOUR
102const WINDOW_TIME: u64 = 14400;
103104/// Blockwindow from block height and target time. Used for time limit on DAO proposals.
105pub fn blockwindow(height: u32, target: u32) -> u64 {
106let timestamp_secs = (height as u64) * (target as u64);
107 timestamp_secs / WINDOW_TIME
108}
109// ANCHOR_END: dao-blockwindow