Skip to content
This repository has been archived by the owner on Aug 24, 2023. It is now read-only.

Commit

Permalink
meta-consensus -> meta-defichain
Browse files Browse the repository at this point in the history
  • Loading branch information
canonbrother committed Nov 1, 2022
1 parent cd55e72 commit e85c5af
Show file tree
Hide file tree
Showing 11 changed files with 205 additions and 14 deletions.
29 changes: 28 additions & 1 deletion Cargo.lock

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

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
[workspace]
members = [
"meta/meta-consensus",
"meta/meta-defichain",
"meta/meta-defichain/rpc",
"meta/meta-defichain/rpc/runtime-api",
"meta/meta-runtime",
"meta/meta-node",
]
10 changes: 0 additions & 10 deletions meta/meta-consensus/Cargo.toml

This file was deleted.

2 changes: 0 additions & 2 deletions meta/meta-consensus/src/lib.rs

This file was deleted.

Empty file removed meta/meta-consensus/src/tests.rs
Empty file.
26 changes: 26 additions & 0 deletions meta/meta-defichain/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[package]
name = "meta-defichain"
version = "0.1.0"
edition = "2021"

[dependencies]
codec = { default-features = false, version = "3.1.5", features = ["derive", "max-encoded-len"], package = "parity-scale-codec" }
scale-info = { default-features = false, version = "2.1.2", features = ["derive"] }
frame-support = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.27" }
frame-system = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.27" }
sp-api = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.27" }
sp-runtime = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.27" }

serde = { version = "1.0.144", features = ["derive"], optional = true }

[features]
default = ["std"]
std = [
"codec/std",
"scale-info/std",
"serde",
"frame-support/std",
"frame-system/std",
"sp-api/std",
"sp-runtime/std",
]
13 changes: 13 additions & 0 deletions meta/meta-defichain/rpc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "meta-defichain-rpc"
version = "0.1.0"
edition = "2021"

[dependencies]
jsonrpsee = { version = "0.14.0", features = ["server", "macros"] }

sp-api = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.25" }
sp-blockchain = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.25" }
sp-runtime = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.25" }

meta-defichain-rpc-runtime-api = { path = "./runtime-api", package = "meta-defichain-rpc-runtime-api"}
13 changes: 13 additions & 0 deletions meta/meta-defichain/rpc/runtime-api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "meta-defichain-rpc-runtime-api"
version = "0.1.0"
edition = "2021"

[dependencies]
sp-api = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.27" }

[features]
default = ["std"]
std = [
"sp-api/std",
]
7 changes: 7 additions & 0 deletions meta/meta-defichain/rpc/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#![cfg_attr(not(feature = "std"), no_std)]

sp_api::decl_runtime_apis! {
pub trait DefichainApi {
fn get_7() -> u64;
}
}
65 changes: 65 additions & 0 deletions meta/meta-defichain/rpc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use jsonrpsee::{
core::{async_trait, Error as JsonRpseeError, RpcResult},
proc_macros::rpc,
types::error::{CallError, ErrorObject},
};
use sp_api::ProvideRuntimeApi;
use sp_blockchain::HeaderBackend;
use sp_runtime::{
generic::BlockId,
traits::{Block as BlockT, Header as HeaderT},
};
use std::{marker::PhantomData, sync::Arc};

pub use meta_defichain_rpc_runtime_api::DefichainApi as DefichainRuntimeApi;

const RUNTIME_ERROR: i32 = 1;

#[rpc(client, server)]
pub trait DefichainApi<BlockHash> {
#[method(name = "defichain_get7")]
fn get_7(&self, at: Option<BlockHash>) -> RpcResult<u64>;
}

/// A struct that implements the `DefichainApi`
pub struct Defichain<Client, Block> {
client: Arc<Client>,
_marker: PhantomData<Block>,
}

impl<Client, Block> Defichain<Client, Block> {
/// Create new `Defichain` instance with the given reference to the client
pub fn new(client: Arc<Client>) -> Self {
Self {
client,
_marker: Default::default(),
}
}
}

#[async_trait]
impl<Client, Block> DefichainApiServer<<Block as BlockT>::Hash> for Defichain<Client, Block>
where
Block: BlockT,
Client: Send + Sync + 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block>,
Client::Api: DefichainRuntimeApi<Block>,
{
fn get_7(&self, at: Option<<Block as BlockT>::Hash>) -> RpcResult<u64> {
let api = self.client.runtime_api();
// to calling the runtime at specific block
let at = BlockId::hash(at.unwrap_or_else(||
// If the block hash is not supplied assume the best block.
self.client.info().best_hash));
api.get_7(&at).map_err(runtime_error_into_rpc_err)
}
}

/// Converts a runtime trap into an RPC error.
fn runtime_error_into_rpc_err(err: impl std::fmt::Debug) -> JsonRpseeError {
CallError::Custom(ErrorObject::owned(
RUNTIME_ERROR,
"Runtime error",
Some(format!("{:?}", err)),
))
.into()
}
50 changes: 50 additions & 0 deletions meta/meta-defichain/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//! The Substrate runtime. This can be compiled with `#[no_std]`, ready for Wasm.
#![cfg_attr(not(feature = "std"), no_std)]

pub use pallet::*;

#[frame_support::pallet]
pub mod pallet {
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
use sp_runtime::traits::AtLeast32BitUnsigned;

/// Configure the pallet by specifying the parameters and types on which it depends.
#[pallet::config]
pub trait Config: frame_system::Config {
/// Because this pallet emits events, it depends on the runtime's definition of an event.
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;

// The type used to store balances.
type Balance: MaxEncodedLen + Member + Parameter + AtLeast32BitUnsigned + Default + Copy;
}

#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
pub struct Pallet<T>(_);

/// Storage item for balances to accounts mapping.
#[pallet::storage]
#[pallet::getter(fn get_balance)]
pub(super) type BalanceToAccount<T: Config> =
StorageMap<_, Blake2_128Concat, T::AccountId, T::Balance, ValueQuery>;

/// Token mint can emit two Event types.
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
/// New token supply was minted.
MintedNewSupply(T::AccountId),
/// Tokens were successfully transferred between accounts. [from, to, value]
Transferred(T::AccountId, T::AccountId, T::Balance),
}

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
}

impl<T: Config> Pallet<T> {
pub fn get_7() -> u64 {
7
}
}

0 comments on commit e85c5af

Please sign in to comment.