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 d34f3be
Show file tree
Hide file tree
Showing 10 changed files with 157 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.

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.27" }
sp-blockchain = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.27" }
sp-runtime = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.27" }

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;
}
}
67 changes: 67 additions & 0 deletions meta/meta-defichain/rpc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use codec::Codec;
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_rpc::number::NumberOrHex;
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, a, b).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()
}
File renamed without changes.

0 comments on commit d34f3be

Please sign in to comment.