forked from isabella232/fuel-canary-watchtower
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextended_provider.rs
104 lines (95 loc) · 3.04 KB
/
extended_provider.rs
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
use cynic::QueryBuilder;
use fuel_core_client::client::{
pagination::{PaginatedResult, PaginationRequest},
schema::{
block::{BlockByHeightArgs, Consensus, Header},
schema,
tx::OpaqueTransaction,
BlockId, ConnectionArgs, PageInfo,
},
FuelClient,
};
use fuels::accounts::fuel_crypto;
#[derive(cynic::QueryFragment, Debug)]
#[cynic(
schema_path = "./assets/schema.sdl",
graphql_type = "Query",
variables = "ConnectionArgs"
)]
pub struct FullBlocksQuery {
#[arguments(after: $after, before: $before, first: $first, last: $last)]
pub blocks: FullBlockConnection,
}
#[derive(cynic::QueryFragment, Debug)]
#[cynic(schema_path = "./assets/schema.sdl", graphql_type = "BlockConnection")]
pub struct FullBlockConnection {
pub edges: Vec<FullBlockEdge>,
pub page_info: PageInfo,
}
#[derive(cynic::QueryFragment, Debug)]
#[cynic(schema_path = "./assets/schema.sdl", graphql_type = "BlockEdge")]
pub struct FullBlockEdge {
pub cursor: String,
pub node: FullBlock,
}
#[derive(cynic::QueryFragment, Debug)]
#[cynic(
schema_path = "./assets/schema.sdl",
graphql_type = "Query",
variables = "BlockByHeightArgs"
)]
pub struct FullBlockByHeightQuery {
#[arguments(height: $height)]
pub block: Option<FullBlock>,
}
#[derive(cynic::QueryFragment, Debug)]
#[cynic(schema_path = "./assets/schema.sdl", graphql_type = "Block")]
pub struct FullBlock {
pub id: BlockId,
pub header: Header,
pub consensus: Consensus,
pub transactions: Vec<OpaqueTransaction>,
}
impl FullBlock {
/// Returns the block producer public key, if any.
pub fn block_producer(&self) -> Option<fuel_crypto::PublicKey> {
let message = self.header.id.clone().into_message();
match &self.consensus {
Consensus::Genesis(_) => Some(Default::default()),
Consensus::PoAConsensus(poa) => {
let signature = poa.signature.clone().into_signature();
let producer_pub_key = signature.recover(&message);
producer_pub_key.ok()
}
Consensus::Unknown => None,
}
}
}
impl From<FullBlockConnection> for PaginatedResult<FullBlock, String> {
fn from(conn: FullBlockConnection) -> Self {
PaginatedResult {
cursor: conn.page_info.end_cursor,
has_next_page: conn.page_info.has_next_page,
has_previous_page: conn.page_info.has_previous_page,
results: conn.edges.into_iter().map(|e| e.node).collect(),
}
}
}
#[async_trait::async_trait]
pub trait ClientExt {
async fn full_blocks(
&self,
request: PaginationRequest<String>,
) -> std::io::Result<PaginatedResult<FullBlock, String>>;
}
#[async_trait::async_trait]
impl ClientExt for FuelClient {
async fn full_blocks(
&self,
request: PaginationRequest<String>,
) -> std::io::Result<PaginatedResult<FullBlock, String>> {
let query = FullBlocksQuery::build(request.into());
let blocks = self.query(query).await?.blocks.into();
Ok(blocks)
}
}