Skip to content

Commit

Permalink
Initial version for treasury creation along with organization
Browse files Browse the repository at this point in the history
  • Loading branch information
vayesy committed May 5, 2022
1 parent 152f06d commit 0343694
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 21 deletions.
6 changes: 4 additions & 2 deletions control/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ scale-info = { version = "1.0", default-features = false, features = ["derive"]
sp-core = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.13" }
sp-runtime = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.13" }
sp-std = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.13" }
sp-storage = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.13" }
sp-storage = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.13" }
sp-io = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.13" }

frame-support = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.13" }
frame-system = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.13" }
Expand All @@ -41,6 +42,7 @@ gamedao-traits = { package = "gamedao-traits", path = "../traits", default-featu
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.13" }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.13" }
sp-io = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.13" }
sp-keystore = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.13" }
frame-support-test = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.13" }
pallet-balances = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.13" }
orml-tokens = { path = "../../orml/tokens", default-features = false }
Expand Down Expand Up @@ -73,4 +75,4 @@ std = [

"gamedao-traits/std",

]
]
35 changes: 19 additions & 16 deletions control/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ mod tests;
// #[cfg(feature = "runtime-benchmarks")]
// mod benchmarking;

use codec::HasCompact;
use codec::{HasCompact, Decode};
use frame_support::{
codec::{Encode},
dispatch::DispatchResult,
Expand All @@ -30,15 +30,15 @@ use frame_support::{
use gamedao_traits::ControlTrait;
use orml_traits::{MultiCurrency, MultiReservableCurrency};
use scale_info::TypeInfo;
use sp_runtime::traits::AtLeast32BitUnsigned;
use sp_runtime::traits::{AtLeast32BitUnsigned, TrailingZeroInput};
use sp_std::{fmt::Debug, vec::Vec};

pub use pallet::*;

#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::pallet_prelude::*;
use frame_support::{pallet_prelude::*, transactional};
use frame_system::pallet_prelude::*;

#[pallet::pallet]
Expand Down Expand Up @@ -222,6 +222,10 @@ pub mod pallet {
UnknownError,
/// Guru Meditation
GuruMeditation,
/// Failed to create treasury for organization
TreasuryCreationIssue,
/// Treasury account already exists
TreasuryExists
}

#[pallet::call]
Expand Down Expand Up @@ -255,13 +259,11 @@ pub mod pallet {
///
/// - `creator`: creator
/// - `controller`: current controller
/// - `treasury`: treasury
/// - `name`: Org name
/// - `cid`: IPFS
/// - `org_type`: individual | legal Org | dao
/// - `access`: anyDAO can join | only member can add | only
/// - `fee_model`: only TX by OS | fees are reserved | fees are moved to
/// treasury
/// - `fee_model`: only TX by OS | fees are reserved | fees are moved to treasury
/// - `fee`: fee
/// - `gov_asset`: control assets to empower actors
/// - `pay_asset`:
Expand All @@ -271,12 +273,10 @@ pub mod pallet {
///
/// Weight:
#[pallet::weight(5_000_000)]
#[transactional]
pub fn create_org(
origin: OriginFor<T>,
controller: T::AccountId,
// TODO: treasury shouldn't be provided here. Ink contract as an option. How to dynamically init the
// pallet?
treasury: T::AccountId,
name: Vec<u8>,
cid: Vec<u8>,
org_type: OrgType,
Expand All @@ -296,14 +296,10 @@ pub mod pallet {
let free_balance = T::Currency::free_balance(T::PaymentTokenId::get(), &sender);
ensure!(free_balance > creation_fee, Error::<T>::BalanceTooLow);

let free_balance_treasury = T::Currency::free_balance(T::PaymentTokenId::get(), &treasury);
ensure!(free_balance_treasury > creation_fee, Error::<T>::BalanceTooLow);

// controller and treasury must not be equal
ensure!(&controller != &treasury, Error::<T>::DuplicateAddress);

let nonce = Self::get_and_increment_nonce();
let (org_id, _) = T::Randomness::random(&nonce.encode());
let treasury = Self::generate_treasury_account_id(org_id)?;
ensure!(!<frame_system::Pallet<T>>::account_exists(&treasury), Error::<T>::TreasuryExists);
let current_block = <frame_system::Pallet<T>>::block_number();

let org = Org {
Expand Down Expand Up @@ -474,7 +470,7 @@ impl<T: Config> Pallet<T> {
OrgAccess::<T>::insert(org_id.clone(), access.clone());
OrgCreator::<T>::insert(org_id.clone(), creator.clone());
OrgController::<T>::insert(org_id.clone(), controller.clone());
OrgTreasury::<T>::insert(org_id.clone(), treasury.clone());
OrgTreasury::<T>::insert(org_id.clone(), treasury);
OrgState::<T>::insert(org_id.clone(), ControlState::Active);
OrgsControlled::<T>::mutate(&controller, |controlled| controlled.push(org_id.clone()));

Expand Down Expand Up @@ -700,6 +696,13 @@ impl<T: Config> Pallet<T> {
}
}

fn generate_treasury_account_id(org_id: T::Hash) -> Result<T::AccountId, Error<T>> {
let encoded = &org_id.encode();
let mut trailing = TrailingZeroInput::new(&encoded);
T::AccountId::decode(&mut trailing).
map_err(|_| <Error<T>>::TreasuryCreationIssue)
}

fn get_and_increment_nonce() -> u128 {
let nonce = Nonce::<T>::get();
Nonce::<T>::put(nonce.wrapping_add(1));
Expand Down
11 changes: 8 additions & 3 deletions control/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
#![cfg(test)]

use super::*;
use std::sync::Arc;
use frame_support::{assert_noop, assert_ok};
use sp_keystore::{testing::KeyStore, KeystoreExt};
use super::*;
use mock::*;

#[test]
fn control_create_campaign_success() {
new_test_ext().execute_with(|| {
let mut ext = new_test_ext();
ext.register_extension(KeystoreExt(Arc::new(KeyStore::new())));
ext.execute_with(|| {
System::set_block_number(3);
// create a DAO with account #5.
assert_ok!(Control::create_org(
Origin::signed(ALICE),
BOB,
TREASURY,
vec![12, 56],
vec![11, 111],
Default::default(),
Expand All @@ -25,6 +29,7 @@ fn control_create_campaign_success() {

// check that there are now 1 Control in storage
assert_eq!(Nonce::<Test>::get(), 1);
assert_eq!(System::events().len(), 2);

// // check that account #5 is creator
// let creator_hash = <OrgByHash<Test>>::get(0);
Expand Down

0 comments on commit 0343694

Please sign in to comment.