Skip to content

Commit

Permalink
positive-externality
Browse files Browse the repository at this point in the history
  • Loading branch information
amiyatulu committed May 25, 2024
1 parent be55eef commit 966b376
Show file tree
Hide file tree
Showing 16 changed files with 1,498 additions and 4 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.

9 changes: 9 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ pallet-shared-storage = {path = "pallets/shared-storage", default-features = fal
pallet-schelling-game-shared = {path = "pallets/schelling-game-shared", default-features = false }
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 }

## Traits
trait-sortition-sum-game = {path = "traits/trait-sortition-sum-game", default-features=false}
Expand All @@ -86,6 +87,14 @@ trait-schelling-game-shared = {path = "traits/trait-schelling-game-shared", defa
## Api
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}


## 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}



## Additional dependancies
Expand Down
56 changes: 56 additions & 0 deletions pallets/positive-externality/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
[package]
name = "pallet-positive-externality"
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/positive-externality/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/positive-externality/positive-externality-rpc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "positive-externality-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 }
positive-externality-runtime-api = { workspace = true}
226 changes: 226 additions & 0 deletions pallets/positive-externality/positive-externality-rpc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
use jsonrpsee::{
core::{Error as JsonRpseeError, RpcResult},
proc_macros::rpc,
types::error::{CallError, ErrorCode, ErrorObject},
};
use positive_externality_runtime_api::PositiveExternalityApi as PositiveExternalityRuntimeApi;
use sp_api::codec::Codec;
use sp_api::ProvideRuntimeApi;
use sp_blockchain::HeaderBackend;
use sp_runtime::traits::Block as BlockT;
use std::sync::Arc;

#[rpc(client, server)]
pub trait PositiveExternalityApi<BlockHash, AccountId> {
#[method(name = "positiveexternality_evidenceperiodendblock")]
fn get_evidence_period_end_block(
&self,
user_to_calculate: AccountId,
at: Option<BlockHash>,
) -> RpcResult<Option<u32>>;
#[method(name = "positiveexternality_stakingperiodendblock")]
fn get_staking_period_end_block(
&self,
user_to_calculate: AccountId,
at: Option<BlockHash>,
) -> RpcResult<Option<u32>>;
#[method(name = "positiveexternality_drawingperiodend")]
fn get_drawing_period_end(
&self,
user_to_calculate: AccountId,
at: Option<BlockHash>,
) -> RpcResult<(u64, u64, bool)>;
#[method(name = "positiveexternality_commitendblock")]
fn get_commit_period_end_block(
&self,
user_to_calculate: AccountId,
at: Option<BlockHash>,
) -> RpcResult<Option<u32>>;
#[method(name = "positiveexternality_voteendblock")]
fn get_vote_period_end_block(
&self,
user_to_calculate: AccountId,
at: Option<BlockHash>,
) -> RpcResult<Option<u32>>;
#[method(name = "positiveexternality_selectedjuror")]
fn selected_as_juror(
&self,
user_to_calculate: AccountId,
who: AccountId,
at: Option<BlockHash>,
) -> RpcResult<bool>;
}

/// A struct that implements the `SumStorageApi`.
pub struct PositiveExternality<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> PositiveExternality<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> PositiveExternalityApiServer<<Block as BlockT>::Hash, AccountId>
for PositiveExternality<C, Block>
where
Block: BlockT,
AccountId: Codec,
C: Send + Sync + 'static,
C: ProvideRuntimeApi<Block>,
C: HeaderBackend<Block>,
C::Api: PositiveExternalityRuntimeApi<Block, AccountId>,
{
fn get_evidence_period_end_block(
&self,
user_to_calculate: AccountId,
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, user_to_calculate);
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,
user_to_calculate: AccountId,
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, user_to_calculate);
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,
user_to_calculate: AccountId,
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, user_to_calculate);
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,
user_to_calculate: AccountId,
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, user_to_calculate);
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,
user_to_calculate: AccountId,
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, user_to_calculate);
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,
user_to_calculate: AccountId,
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, user_to_calculate, 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 = "positive-externality-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,18 @@
#![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;

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

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

0 comments on commit 966b376

Please sign in to comment.