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 9, 2022
1 parent 152f06d commit 6298da7
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 34 deletions.
4 changes: 2 additions & 2 deletions control/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ 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" }

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 Down Expand Up @@ -73,4 +73,4 @@ std = [

"gamedao-traits/std",

]
]
48 changes: 24 additions & 24 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 @@ -76,7 +76,6 @@ pub mod pallet {
type Randomness: Randomness<Self::Hash, Self::BlockNumber>;

// type GameDAOAdminOrigin: EnsureOrigin<Self::Origin>;
type GameDAOTreasury: Get<Self::AccountId>;

#[pallet::constant]
type MaxDAOsPerAccount: Get<u32>;
Expand All @@ -87,11 +86,9 @@ pub mod pallet {

#[pallet::constant]
type ProtocolTokenId: Get<Self::CurrencyId>;
#[pallet::constant]
type PaymentTokenId: Get<Self::CurrencyId>;

#[pallet::constant]
type CreationFee: Get<Self::Balance>;
type InitialDeposit: Get<Self::Balance>;
}

/// Get an Org by its hash
Expand Down Expand Up @@ -222,6 +219,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 +256,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 +270,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 @@ -292,18 +289,14 @@ pub mod pallet {
) -> DispatchResult {
let sender = ensure_signed(origin)?;

let creation_fee = T::CreationFee::get();
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 initial_deposit = T::InitialDeposit::get();
let free_balance = T::Currency::free_balance(T::ProtocolTokenId::get(), &sender);
ensure!(free_balance > initial_deposit, Error::<T>::BalanceTooLow);

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 @@ -495,8 +488,8 @@ impl<T: Config> Pallet<T> {
T::Currency::transfer(
T::ProtocolTokenId::get(),
&creator,
&T::GameDAOTreasury::get(),
T::CreationFee::get(),
&treasury,
T::InitialDeposit::get(),
)?;

Ok(())
Expand Down Expand Up @@ -700,6 +693,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
8 changes: 2 additions & 6 deletions control/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,7 @@ frame_support::parameter_types! {
pub const MaxMembersPerDAO: u32 = 2;
pub const MaxCreationsPerBlock: u32 = 2;
pub const ProtocolTokenId: u32 = PROTOCOL_TOKEN_ID;
pub const PaymentTokenId: CurrencyId = PAYMENT_TOKEN_ID;
pub const CreationFee: Balance = 1 * DOLLARS;
pub const GameDAOTreasury: AccountId = TREASURY;
pub const InitialDeposit: Balance = 1 * DOLLARS;
}
impl pallet_control::Config for Test {
type Balance = Balance;
Expand All @@ -145,16 +143,14 @@ impl pallet_control::Config for Test {
type Randomness = TestRandomness<Self>;

// type GameDAOAdminOrigin: EnsureOrigin<Self::Origin>;
type GameDAOTreasury = GameDAOTreasury;

type ForceOrigin = frame_system::EnsureRoot<Self::AccountId>;

type MaxDAOsPerAccount = MaxDAOsPerAccount;
type MaxMembersPerDAO = MaxMembersPerDAO;
type MaxCreationsPerBlock = MaxCreationsPerBlock;
type ProtocolTokenId = ProtocolTokenId;
type PaymentTokenId = PaymentTokenId;
type CreationFee = CreationFee;
type InitialDeposit = InitialDeposit;
}

pub fn new_test_ext() -> sp_io::TestExternalities {
Expand Down
15 changes: 13 additions & 2 deletions control/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#![cfg(test)]

use super::*;
use frame_support::{assert_noop, assert_ok};
use super::*;
use mock::*;

#[test]
fn control_create_campaign_success() {
new_test_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 +25,17 @@ fn control_create_campaign_success() {

// check that there are now 1 Control in storage
assert_eq!(Nonce::<Test>::get(), 1);
let created_org_id = OrgByNonce::<Test>::get(0).unwrap();
System::assert_has_event(
mock::Event::Control(
crate::Event::OrgCreated {
sender_id: ALICE,
org_id: created_org_id,
created_at: System::block_number(),
realm_index: 0
}
)
);

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

0 comments on commit 6298da7

Please sign in to comment.