1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 * Copyright 2020 Nuclei Studio OÜ
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

//! Low level primitives for the governance OS runtime and node.

#![cfg_attr(not(feature = "std"), no_std)]

use codec::{Decode, Encode};
use governance_os_support::impl_enum_default;
#[cfg(feature = "std")]
use serde::{Deserialize, Serialize};
use sp_runtime::{
    generic,
    traits::{BlakeTwo256, IdentifyAccount, Verify},
    MultiSignature, OpaqueExtrinsic, RuntimeDebug,
};

/// How we represent currencies in the runtime.
#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, Copy, PartialOrd, Ord)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
pub enum CurrencyId {
    Native,
    Custom(u32),
}
impl_enum_default!(CurrencyId, Native);

/// An index to a block.
pub type BlockNumber = u32;

/// Alias to 512-bit hash when used in the context of a transaction signature on the chain.
pub type Signature = MultiSignature;

/// Some way of identifying an account on the chain. We intentionally make it equivalent
/// to the public key of our transaction signing scheme.
pub type AccountId = <<Signature as Verify>::Signer as IdentifyAccount>::AccountId;

/// Balance of an account.
pub type Balance = u128;

/// Type used for expressing timestamp.
pub type Moment = u64;

/// Index of a transaction in the chain.
pub type Index = u32;

/// A hash of some data used by the chain.
pub type Hash = sp_core::H256;

/// Header type.
pub type Header = generic::Header<BlockNumber, BlakeTwo256>;
/// Block type.
pub type Block = generic::Block<Header, OpaqueExtrinsic>;

/// The different roles supported by the runtime.
#[derive(Encode, Decode, RuntimeDebug, Clone, PartialEq, Eq, Ord, PartialOrd)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
pub enum Role {
    ApplyAsOrganization(AccountId),
    CreateCurrencies,
    CreateOrganizations,
    ManageCurrency(CurrencyId),
    ManageRoles,
    Root,
    TransferCurrency(CurrencyId),
}
// `Default` is used for benchmarks. We have to make sure the default role is not
// root though.
impl_enum_default!(Role, CreateCurrencies);
impl governance_os_pallet_tokens::RoleBuilder for Role {
    type CurrencyId = CurrencyId;
    type Role = Role;

    fn transfer_currency(id: CurrencyId) -> Role {
        Role::TransferCurrency(id)
    }

    fn manage_currency(id: CurrencyId) -> Role {
        Role::ManageCurrency(id)
    }

    fn create_currencies() -> Role {
        Role::CreateCurrencies
    }
}
impl governance_os_pallet_bylaws::RoleBuilder for Role {
    type Role = Role;

    fn manage_roles() -> Role {
        Role::ManageRoles
    }

    fn root() -> Role {
        Role::Root
    }
}
impl governance_os_pallet_organizations::RoleBuilder for Role {
    type OrganizationId = AccountId;
    type Role = Role;

    fn create_organizations() -> Role {
        Role::CreateOrganizations
    }

    fn apply_as_organization(org_id: &AccountId) -> Role {
        Role::ApplyAsOrganization(org_id.clone())
    }
}