dao.rs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2023 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. */
  18. use darkfi_dao_contract::dao_model::DaoBulla;
  19. use darkfi_sdk::{
  20. crypto::{poseidon_hash, PublicKey, SecretKey, TokenId},
  21. incrementalmerkletree::Position,
  22. pasta::pallas,
  23. };
  24. use darkfi_serial::{SerialDecodable, SerialEncodable};
  25. #[derive(Debug, Clone, SerialEncodable, SerialDecodable)]
  26. /// Parameters representing a DAO to be initialized
  27. pub struct DaoParams {
  28. /// The minimum amount of governance tokens needed to open a proposal
  29. pub proposer_limit: u64,
  30. /// Minimal threshold of participating total tokens needed for a proposal to pass
  31. pub quorum: u64,
  32. /// The ratio of winning/total votes needed for a proposal to pass
  33. pub approval_ratio_base: u64,
  34. pub approval_ratio_quot: u64,
  35. /// DAO's governance token ID
  36. pub gov_token_id: TokenId,
  37. /// Secret key for the DAO
  38. pub secret_key: SecretKey,
  39. /// DAO bulla blind
  40. pub bulla_blind: pallas::Base,
  41. }
  42. #[derive(Debug, Clone)]
  43. /// Parameters representing an intialized DAO, optionally deployed on-chain
  44. pub struct Dao {
  45. /// Numeric identifier for the DAO
  46. pub id: u64,
  47. /// Named identifier for the DAO
  48. pub name: String,
  49. /// The minimum amount of governance tokens needed to open a proposal
  50. pub proposer_limit: u64,
  51. /// Minimal threshold of participating total tokens needed for a proposal to pass
  52. pub quorum: u64,
  53. /// The ratio of winning/total votes needed for a proposal to pass
  54. pub approval_ratio_base: u64,
  55. pub approval_ratio_quot: u64,
  56. /// DAO's governance token ID
  57. pub gov_token_id: TokenId,
  58. /// Secret key for the DAO
  59. pub secret_key: SecretKey,
  60. /// DAO bulla blind
  61. pub bulla_blind: pallas::Base,
  62. /// Leaf position of the DAO in the Merkle tree of DAOs
  63. pub leaf_position: Option<Position>,
  64. /// The transaction hash where the DAO was deployed
  65. pub tx_hash: Option<blake3::Hash>,
  66. /// The call index in the transaction where the DAO was deployed
  67. pub call_index: Option<u32>,
  68. }
  69. impl Dao {
  70. pub fn bulla(&self) -> DaoBulla {
  71. let (x, y) = PublicKey::from_secret(self.secret_key).xy();
  72. DaoBulla::from(poseidon_hash([
  73. pallas::Base::from(self.proposer_limit),
  74. pallas::Base::from(self.quorum),
  75. pallas::Base::from(self.approval_ratio_base),
  76. pallas::Base::from(self.approval_ratio_quot),
  77. self.gov_token_id.inner(),
  78. x,
  79. y,
  80. self.bulla_blind,
  81. ]))
  82. }
  83. }