-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathogmios_mock.rs
122 lines (107 loc) · 3.16 KB
/
ogmios_mock.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
use ogmios_client::{
query_ledger_state::{
OgmiosTip, ProtocolParametersResponse, QueryLedgerState, QueryUtxoByUtxoId,
},
query_network::{QueryNetwork, ShelleyGenesisConfigurationResponse},
transactions::{OgmiosEvaluateTransactionResponse, SubmitTransactionResponse, Transactions},
types::OgmiosUtxo,
OgmiosClientError,
};
#[derive(Clone, Default, Debug)]
pub struct MockOgmiosClient {
shelley_config: ShelleyGenesisConfigurationResponse,
utxos: Vec<OgmiosUtxo>,
protocol_parameters: ProtocolParametersResponse,
evaluate_result: Option<Vec<OgmiosEvaluateTransactionResponse>>,
submit_result: Option<SubmitTransactionResponse>,
}
impl MockOgmiosClient {
pub fn new() -> Self {
Self::default()
}
pub fn with_utxos(self, utxos: Vec<OgmiosUtxo>) -> Self {
let mut current_utxos = self.utxos;
current_utxos.extend(utxos);
Self { utxos: current_utxos, ..self }
}
pub fn with_protocol_parameters(self, protocol_parameters: ProtocolParametersResponse) -> Self {
Self { protocol_parameters, ..self }
}
pub fn with_evaluate_result(
self,
evaluate_result: Vec<OgmiosEvaluateTransactionResponse>,
) -> Self {
Self { evaluate_result: Some(evaluate_result), ..self }
}
pub fn with_submit_result(self, submit_result: SubmitTransactionResponse) -> Self {
Self { submit_result: Some(submit_result), ..self }
}
pub fn with_shelley_config(self, shelley_config: ShelleyGenesisConfigurationResponse) -> Self {
Self { shelley_config, ..self }
}
}
impl QueryNetwork for MockOgmiosClient {
async fn shelley_genesis_configuration(
&self,
) -> Result<ShelleyGenesisConfigurationResponse, ogmios_client::OgmiosClientError> {
Ok(self.shelley_config.clone())
}
}
impl Transactions for MockOgmiosClient {
async fn evaluate_transaction(
&self,
_tx_bytes: &[u8],
) -> Result<
Vec<ogmios_client::transactions::OgmiosEvaluateTransactionResponse>,
ogmios_client::OgmiosClientError,
> {
Ok(self.evaluate_result.clone().unwrap())
}
async fn submit_transaction(
&self,
_tx_bytes: &[u8],
) -> Result<
ogmios_client::transactions::SubmitTransactionResponse,
ogmios_client::OgmiosClientError,
> {
Ok(self.submit_result.clone().unwrap())
}
}
impl QueryLedgerState for MockOgmiosClient {
async fn get_tip(&self) -> Result<OgmiosTip, OgmiosClientError> {
unimplemented!()
}
async fn era_summaries(
&self,
) -> Result<Vec<ogmios_client::query_ledger_state::EraSummary>, ogmios_client::OgmiosClientError>
{
unimplemented!()
}
async fn query_utxos(
&self,
addresses: &[String],
) -> Result<Vec<ogmios_client::types::OgmiosUtxo>, ogmios_client::OgmiosClientError> {
Ok(self
.utxos
.iter()
.filter(|utxo| addresses.contains(&utxo.address))
.cloned()
.collect())
}
async fn query_protocol_parameters(
&self,
) -> Result<
ogmios_client::query_ledger_state::ProtocolParametersResponse,
ogmios_client::OgmiosClientError,
> {
Ok(self.protocol_parameters.clone())
}
}
impl QueryUtxoByUtxoId for MockOgmiosClient {
async fn query_utxo_by_id(
&self,
queried_utxo: sidechain_domain::UtxoId,
) -> Result<Option<OgmiosUtxo>, ogmios_client::OgmiosClientError> {
Ok(self.utxos.iter().find(|utxo| utxo.utxo_id() == queried_utxo).cloned())
}
}