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
use codec::Codec;
use jsonrpsee::{
core::{Error as RpcError, RpcResult},
proc_macros::rpc,
types::error::CallError,
};
use sp_api::ProvideRuntimeApi;
use sp_blockchain::HeaderBackend;
use sp_runtime::generic::BlockId;
use sp_runtime::traits::Block as BlockT;
use sp_runtime::DispatchError;
use std::sync::Arc;
pub use oracle_proxy_runtime_api::OracleProxyAPI as OracleProxyRuntimeApi;
use oracle_proxy_runtime_api::RateInfo;
#[rpc(server, client)]
pub trait OracleProxyApi<BlockHash, Symbol, Rate, ResolveTime> {
#[method(name = "oracleProxy_quote")]
fn quote(
&self,
symbol: Symbol,
at: Option<BlockHash>,
) -> RpcResult<Result<Option<Rate>, DispatchError>>;
#[method(name = "oracleProxy_listEnabledSymbols")]
fn list_enabled_symbols(
&self,
at: Option<BlockHash>,
) -> RpcResult<Result<Vec<(Symbol, ResolveTime)>, DispatchError>>;
}
pub struct OracleProxyClient<C, B> {
client: Arc<C>,
_marker: std::marker::PhantomData<B>,
}
impl<C, B> OracleProxyClient<C, B> {
pub fn new(client: Arc<C>) -> Self {
Self {
client,
_marker: Default::default(),
}
}
}
impl<C, Block, Symbol, ResolveTime>
OracleProxyApiServer<<Block as BlockT>::Hash, Symbol, RateInfo, ResolveTime>
for OracleProxyClient<C, Block>
where
Block: BlockT,
C: Send + Sync + 'static,
C: ProvideRuntimeApi<Block> + HeaderBackend<Block>,
C::Api: OracleProxyRuntimeApi<Block, Symbol, ResolveTime>,
Symbol: Codec,
RateInfo: Codec,
ResolveTime: Codec,
{
fn quote(
&self,
symbol: Symbol,
at: Option<<Block as BlockT>::Hash>,
) -> RpcResult<Result<Option<RateInfo>, DispatchError>> {
let api = self.client.runtime_api();
let at = BlockId::hash(at.unwrap_or(
self.client.info().best_hash,
));
api.quote(&at, symbol)
.map_err(|e| RpcError::Call(CallError::Failed(e.into())))
}
fn list_enabled_symbols(
&self,
at: Option<<Block as BlockT>::Hash>,
) -> RpcResult<Result<Vec<(Symbol, ResolveTime)>, DispatchError>> {
let api = self.client.runtime_api();
let at = BlockId::hash(at.unwrap_or(
self.client.info().best_hash,
));
api.list_enabled_symbols(&at)
.map_err(|e| RpcError::Call(CallError::Failed(e.into())))
}
}