Skip to content

Commit

Permalink
department-funding
Browse files Browse the repository at this point in the history
  • Loading branch information
amiyatulu committed May 25, 2024
1 parent 966b376 commit 6048d4c
Show file tree
Hide file tree
Showing 16 changed files with 1,450 additions and 2 deletions.
24 changes: 24 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ pallet-schelling-game-shared = {path = "pallets/schelling-game-shared", default-
pallet-profile-validation = {path = "pallets/profile-validation", default-features = false}
pallet-project-tips = {path = "pallets/project-tips", default-features = false}
pallet-positive-externality = { path = "pallets/positive-externality", default-features = false }
pallet-department-funding = { path = "pallets/department-funding", default-features = false }

## Traits
trait-sortition-sum-game = {path = "traits/trait-sortition-sum-game", default-features=false}
Expand All @@ -88,12 +89,15 @@ trait-schelling-game-shared = {path = "traits/trait-schelling-game-shared", defa
profile-validation-runtime-api = { path = "pallets/profile-validation/profile-validation-runtime-api", default-features = false}
project-tips-runtime-api = { path = "pallets/project-tips/project-tips-runtime-api", default-features = false }
positive-externality-runtime-api = { path = "pallets/positive-externality/positive-externality-runtime-api", default-features = false}
department-funding-runtime-api = { path = "pallets/department-funding/department-funding-runtime-api", default-features = false}


## Rpc
profile-validation-rpc = { path = "pallets/profile-validation/profile-validation-rpc", default-features = false}
project-tips-rpc = { path = "pallets/project-tips/project-tips-rpc", default-features = false }
positive-externality-rpc = { path = "pallets/positive-externality/positive-externality-rpc", default-features = false}
department-funding-rpc = { path = "pallets/department-funding/department-funding-rpc", default-features = false}




Expand Down
54 changes: 54 additions & 0 deletions pallets/department-funding/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
[package]
name = "pallet-department-funding"
version = "4.0.0-dev"
description = "FRAME pallet template for defining custom runtime logic."
authors = ["Substrate DevHub <https://github.com/substrate-developer-hub>"]
homepage = "https://substrate.io"
edition = "2021"
license = "MIT-0"
publish = false
repository = "https://github.com/substrate-developer-hub/substrate-node-template/"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
parity-scale-codec = { workspace = true }
scale-info = { workspace = true }
frame-benchmarking = { workspace = true , optional = true}
frame-support = { workspace = true }
frame-system = { workspace = true }
sp-std = { workspace = true}
pallet-timestamp = { workspace = true }
pallet-balances = { workspace = true }
pallet-support = { workspace = true }
pallet-shared-storage = { workspace = true }
trait-shared-storage = { workspace = true }
pallet-schelling-game-shared = { workspace = true }
trait-schelling-game-shared = { workspace = true }
pallet-sortition-sum-game = { workspace = true }

[dev-dependencies]
sp-core = { workspace = true }
sp-io = { workspace = true }
sp-runtime = { workspace = true }
frame-support-test = { workspace = true }

[features]
default = ["std"]
std = [
"parity-scale-codec/std",
"frame-benchmarking?/std",
"frame-support/std",
"frame-system/std",
"scale-info/std",
"pallet-timestamp/std",
"pallet-balances/std",
"pallet-support/std",
"pallet-shared-storage/std",
"pallet-schelling-game-shared/std",
"pallet-sortition-sum-game/std",
"frame-support-test/std",
]
runtime-benchmarks = ["frame-benchmarking/runtime-benchmarks"]
try-runtime = ["frame-support/try-runtime"]
1 change: 1 addition & 0 deletions pallets/department-funding/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
License: MIT-0
14 changes: 14 additions & 0 deletions pallets/department-funding/department-funding-rpc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "department-funding-rpc"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
jsonrpsee = { workspace = true }
sc-rpc = { workspace = true}
sp-api = { workspace = true }
sp-blockchain = {workspace = true }
sp-runtime = { workspace = true }
department-funding-runtime-api = { workspace = true}
228 changes: 228 additions & 0 deletions pallets/department-funding/department-funding-rpc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
use department_funding_runtime_api::DepartmentFundingApi as DepartmentFundingRuntimeApi;
use jsonrpsee::{
core::{Error as JsonRpseeError, RpcResult},
proc_macros::rpc,
types::error::{CallError, ErrorCode, ErrorObject},
};
use sp_api::codec::Codec;
use sp_api::ProvideRuntimeApi;
use sp_blockchain::HeaderBackend;
use sp_runtime::traits::Block as BlockT;
use std::sync::Arc;

type DepartmentRequiredFundId = u64;

#[rpc(client, server)]
pub trait DepartmentFundingApi<BlockHash, AccountId> {
#[method(name = "departmentfunding_evidenceperiodendblock")]
fn get_evidence_period_end_block(
&self,
department_required_fund_id: DepartmentRequiredFundId,
at: Option<BlockHash>,
) -> RpcResult<Option<u32>>;
#[method(name = "departmentfunding_stakingperiodendblock")]
fn get_staking_period_end_block(
&self,
department_required_fund_id: DepartmentRequiredFundId,
at: Option<BlockHash>,
) -> RpcResult<Option<u32>>;
#[method(name = "departmentfunding_drawingperiodend")]
fn get_drawing_period_end(
&self,
department_required_fund_id: DepartmentRequiredFundId,
at: Option<BlockHash>,
) -> RpcResult<(u64, u64, bool)>;
#[method(name = "departmentfunding_commitendblock")]
fn get_commit_period_end_block(
&self,
department_required_fund_id: DepartmentRequiredFundId,
at: Option<BlockHash>,
) -> RpcResult<Option<u32>>;
#[method(name = "departmentfunding_voteendblock")]
fn get_vote_period_end_block(
&self,
department_required_fund_id: DepartmentRequiredFundId,
at: Option<BlockHash>,
) -> RpcResult<Option<u32>>;
#[method(name = "departmentfunding_selectedjuror")]
fn selected_as_juror(
&self,
department_required_fund_id: DepartmentRequiredFundId,
who: AccountId,
at: Option<BlockHash>,
) -> RpcResult<bool>;
}

/// A struct that implements the `SumStorageApi`.
pub struct DepartmentFunding<C, M> {
// If you have more generics, no need to SumStorage<C, M, N, P, ...>
// just use a tuple like SumStorage<C, (M, N, P, ...)>
client: Arc<C>,
_marker: std::marker::PhantomData<M>,
}

impl<C, M> DepartmentFunding<C, M> {
/// Create new `SumStorage` instance with the given reference to the client.
pub fn new(client: Arc<C>) -> Self {
Self { client, _marker: Default::default() }
}
}

/// Error type of this RPC api.
pub enum Error {
/// The transaction was not decodable.
DecodeError,
/// The call to runtime failed.
RuntimeError,
}

impl From<Error> for i32 {
fn from(e: Error) -> i32 {
match e {
Error::RuntimeError => 1,
Error::DecodeError => 2,
}
}
}

impl<C, Block, AccountId> DepartmentFundingApiServer<<Block as BlockT>::Hash, AccountId>
for DepartmentFunding<C, Block>
where
Block: BlockT,
AccountId: Codec,
C: Send + Sync + 'static,
C: ProvideRuntimeApi<Block>,
C: HeaderBackend<Block>,
C::Api: DepartmentFundingRuntimeApi<Block, AccountId>,
{
fn get_evidence_period_end_block(
&self,
department_required_fund_id: DepartmentRequiredFundId,
at: Option<Block::Hash>,
) -> RpcResult<Option<u32>> {
let api = self.client.runtime_api();
let at = at.unwrap_or_else(||
// If the block hash is not supplied assume the best block.
self.client.info().best_hash);

let runtime_api_result = api.get_evidence_period_end_block(at, department_required_fund_id);
fn map_err(error: impl ToString, desc: &'static str) -> CallError {
CallError::Custom(ErrorObject::owned(
Error::RuntimeError.into(),
desc,
Some(error.to_string()),
))
}
let res = runtime_api_result.map_err(|e| map_err(e, "Unable to query dispatch info."))?;
Ok(res)
}
fn get_staking_period_end_block(
&self,
department_required_fund_id: DepartmentRequiredFundId,
at: Option<Block::Hash>,
) -> RpcResult<Option<u32>> {
let api = self.client.runtime_api();
let at = at.unwrap_or_else(||
// If the block hash is not supplied assume the best block.
self.client.info().best_hash);

let runtime_api_result = api.get_staking_period_end_block(at, department_required_fund_id);
fn map_err(error: impl ToString, desc: &'static str) -> CallError {
CallError::Custom(ErrorObject::owned(
Error::RuntimeError.into(),
desc,
Some(error.to_string()),
))
}
let res = runtime_api_result.map_err(|e| map_err(e, "Unable to query dispatch info."))?;
Ok(res)
}
fn get_drawing_period_end(
&self,
department_required_fund_id: DepartmentRequiredFundId,
at: Option<Block::Hash>,
) -> RpcResult<(u64, u64, bool)> {
let api = self.client.runtime_api();
let at = at.unwrap_or_else(||
// If the block hash is not supplied assume the best block.
self.client.info().best_hash);

let runtime_api_result = api.get_drawing_period_end(at, department_required_fund_id);
fn map_err(error: impl ToString, desc: &'static str) -> CallError {
CallError::Custom(ErrorObject::owned(
Error::RuntimeError.into(),
desc,
Some(error.to_string()),
))
}
let res = runtime_api_result.map_err(|e| map_err(e, "Unable to query dispatch info."))?;
Ok(res)
}

fn get_commit_period_end_block(
&self,
department_required_fund_id: DepartmentRequiredFundId,
at: Option<Block::Hash>,
) -> RpcResult<Option<u32>> {
let api = self.client.runtime_api();
let at = at.unwrap_or_else(||
// If the block hash is not supplied assume the best block.
self.client.info().best_hash);

let runtime_api_result = api.get_commit_period_end_block(at, department_required_fund_id);
fn map_err(error: impl ToString, desc: &'static str) -> CallError {
CallError::Custom(ErrorObject::owned(
Error::RuntimeError.into(),
desc,
Some(error.to_string()),
))
}
let res = runtime_api_result.map_err(|e| map_err(e, "Unable to query dispatch info."))?;
Ok(res)
}

fn get_vote_period_end_block(
&self,
department_required_fund_id: DepartmentRequiredFundId,
at: Option<Block::Hash>,
) -> RpcResult<Option<u32>> {
let api = self.client.runtime_api();
let at = at.unwrap_or_else(||
// If the block hash is not supplied assume the best block.
self.client.info().best_hash);

let runtime_api_result = api.get_vote_period_end_block(at, department_required_fund_id);
fn map_err(error: impl ToString, desc: &'static str) -> CallError {
CallError::Custom(ErrorObject::owned(
Error::RuntimeError.into(),
desc,
Some(error.to_string()),
))
}
let res = runtime_api_result.map_err(|e| map_err(e, "Unable to query dispatch info."))?;
Ok(res)
}

fn selected_as_juror(
&self,
department_required_fund_id: DepartmentRequiredFundId,
who: AccountId,
at: Option<Block::Hash>,
) -> RpcResult<bool> {
let api = self.client.runtime_api();
let at = at.unwrap_or_else(||
// If the block hash is not supplied assume the best block.
self.client.info().best_hash);

let runtime_api_result = api.selected_as_juror(at, department_required_fund_id, who);
fn map_err(error: impl ToString, desc: &'static str) -> CallError {
CallError::Custom(ErrorObject::owned(
Error::RuntimeError.into(),
desc,
Some(error.to_string()),
))
}
let res = runtime_api_result.map_err(|e| map_err(e, "Unable to query dispatch info."))?;
Ok(res)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "department-funding-runtime-api"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
sp-api = { workspace = true }
frame-support = { workspace = true }

[features]
default = ["std"]
std = [
"sp-api/std",
"frame-support/std",
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#![cfg_attr(not(feature = "std"), no_std)]

// use frame_support::sp_std::{vec::Vec};
// or
use frame_support::sp_std::prelude::*;
use sp_api::codec::Codec;

type DepartmentRequiredFundId = u64;

sp_api::decl_runtime_apis! {
pub trait DepartmentFundingApi<AccountId> where AccountId: Codec {

fn get_evidence_period_end_block(department_required_fund_id: DepartmentRequiredFundId) -> Option<u32>;
fn get_staking_period_end_block(department_required_fund_id: DepartmentRequiredFundId) -> Option<u32>;
fn get_drawing_period_end(department_required_fund_id: DepartmentRequiredFundId) -> (u64, u64, bool);
fn get_commit_period_end_block(department_required_fund_id: DepartmentRequiredFundId) -> Option<u32>;
fn get_vote_period_end_block(department_required_fund_id: DepartmentRequiredFundId) -> Option<u32>;
fn selected_as_juror(department_required_fund_id: DepartmentRequiredFundId, who: AccountId) -> bool;
}
}
Loading

0 comments on commit 6048d4c

Please sign in to comment.