forked from diadata-org/oracle-pallet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
131 lines (118 loc) · 2.98 KB
/
lib.rs
File metadata and controls
131 lines (118 loc) · 2.98 KB
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
121
122
123
124
125
126
127
128
129
130
131
use dia_oracle_runtime_api::{CoinInfo, PriceInfo};
use jsonrpsee::{
core::RpcResult,
proc_macros::rpc,
types::error::{CallError, ErrorObject},
};
use sp_api::ProvideRuntimeApi;
use sp_blockchain::HeaderBackend;
use sp_core::Bytes;
use sp_runtime::traits::Block as BlockT;
pub use dia_oracle_runtime_api::DiaOracleApi as DiaOracleRuntimeApi;
use std::sync::Arc;
#[rpc(client, server)]
pub trait DiaOracleApi<BlockHash> {
#[method(name = "dia_getCoinInfo")]
fn get_coin_info(
&self,
blockchain: Bytes,
symbol: Bytes,
at: Option<BlockHash>,
) -> RpcResult<CoinInfo>;
#[method(name = "dia_getValue")]
fn get_value(
&self,
blockchain: Bytes,
symbol: Bytes,
at: Option<BlockHash>,
) -> RpcResult<PriceInfo>;
}
/// A struct that implements the [`DiaOracleApi`].
pub struct DiaOracleRpc<C, P> {
client: Arc<C>,
_marker: std::marker::PhantomData<P>,
}
impl<C, P> DiaOracleRpc<C, P> {
/// Create new `TransactionPayment` 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> DiaOracleApiServer<<Block as BlockT>::Hash> for DiaOracleRpc<C, Block>
where
Block: BlockT,
C: 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block>,
C::Api: DiaOracleRuntimeApi<Block>,
{
fn get_coin_info(
&self,
blockchain: Bytes,
symbol: Bytes,
at: Option<<Block as BlockT>::Hash>,
) -> RpcResult<CoinInfo> {
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 r = api
.get_coin_info(at, blockchain.to_vec(), symbol.to_vec())
.map_err(|e| {
CallError::Custom(ErrorObject::owned(
Error::RuntimeError.into(),
"Unable to query get_coin_info.",
Some(format!("{:?}", e)),
))
})?
.map_err(|e| {
CallError::Custom(ErrorObject::owned(
Error::RuntimeError.into(),
"Unable to query get_coin_info.",
Some(format!("{:?}", e)),
))
})?;
Ok(r)
}
fn get_value(
&self,
blockchain: Bytes,
symbol: Bytes,
at: Option<<Block as BlockT>::Hash>,
) -> RpcResult<PriceInfo> {
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 r = api
.get_value(at, blockchain.to_vec(), symbol.to_vec())
.map_err(|e| {
CallError::Custom(ErrorObject::owned(
Error::RuntimeError.into(),
"Unable to query get_value.",
Some(format!("{:?}", e)),
))
})?
.map_err(|e| {
CallError::Custom(ErrorObject::owned(
Error::RuntimeError.into(),
"Unable to query get_value.",
Some(format!("{:?}", e)),
))
})?;
Ok(r)
}
}