-
Notifications
You must be signed in to change notification settings - Fork 247
Expand file tree
/
Copy pathrpc_mock.rs
More file actions
147 lines (128 loc) · 4.31 KB
/
rpc_mock.rs
File metadata and controls
147 lines (128 loc) · 4.31 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
use base64::{self, engine::general_purpose::STANDARD, Engine};
use serde_json::{json, Value};
use solana_client::{nonblocking::rpc_client::RpcClient, rpc_request::RpcRequest};
use solana_sdk::{account::Account, pubkey::Pubkey};
use std::{collections::HashMap, sync::Arc};
use crate::tests::account_mock::MintAccountMockBuilder;
pub const DEFAULT_LOCAL_RPC_URL: &str = "http://localhost:8899";
/// Builder for creating mock RPC clients with different responses
pub struct RpcMockBuilder {
mocks: HashMap<RpcRequest, Value>,
}
impl Default for RpcMockBuilder {
fn default() -> Self {
Self::new()
}
}
impl RpcMockBuilder {
pub fn new() -> Self {
Self { mocks: HashMap::new() }
}
pub fn with_account_info(mut self, account: &Account) -> Self {
let encoded_data = STANDARD.encode(&account.data);
self.mocks.insert(
RpcRequest::GetAccountInfo,
json!({
"context": { "slot": 1 },
"value": {
"data": [encoded_data, "base64"],
"executable": account.executable,
"lamports": account.lamports,
"owner": account.owner.to_string(),
"rentEpoch": account.rent_epoch
}
}),
);
self
}
pub fn with_account_not_found(mut self) -> Self {
self.mocks.insert(
RpcRequest::GetAccountInfo,
json!({
"context": { "slot": 1 },
"value": null
}),
);
self
}
pub fn with_fee_estimate(mut self, fee: u64) -> Self {
self.mocks.insert(
RpcRequest::GetFeeForMessage,
json!({
"context": { "slot": 1 },
"value": fee
}),
);
self
}
pub fn with_mint_account(self, decimals: u8) -> Self {
let mint_account = MintAccountMockBuilder::new()
.with_decimals(decimals)
.with_supply(1_000_000_000_000)
.build();
self.with_account_info(&mint_account)
}
pub fn with_custom_mock(mut self, request: RpcRequest, response: Value) -> Self {
self.mocks.insert(request, response);
self
}
pub fn with_custom_mocks(mut self, extra_mocks: HashMap<RpcRequest, Value>) -> Self {
self.mocks.extend(extra_mocks);
self
}
pub fn with_blockhash(mut self) -> Self {
self.mocks.insert(
RpcRequest::GetLatestBlockhash,
json!({ "context": { "slot": 1 }, "value": { "blockhash": Pubkey::new_unique().to_string(), "lastValidBlockHeight": 1000 } }),
);
self
}
pub fn with_epoch_info_mock(mut self) -> Self {
self.mocks.insert(
RpcRequest::GetEpochInfo,
json!({
"context": { "slot": 1 },
"value": {
"epoch": 100,
"slotIndex": 1,
"slotsInEpoch": 432000
}
}),
);
self
}
pub fn with_send_transaction(mut self) -> Self {
self.mocks.insert(
RpcRequest::SendTransaction,
json!("5j7s8Wmt6yZb8kWBBdyKVEE8Pk8z2yBV2bX4Ct4nnEzHrNmHbG8LNHKtPj8F3mJq1vE8Zk2sZf2RjNjVxNz8QdJZ")
);
self.mocks.insert(
RpcRequest::GetSignatureStatuses,
json!({
"context": { "slot": 1 },
"value": [
{
"slot": 1,
"confirmations": 0,
"err": null,
"status": { "Ok": null },
"confirmation_status": "finalized"
}
]
}),
);
self
}
pub fn build(self) -> Arc<RpcClient> {
Arc::new(RpcClient::new_mock_with_mocks(DEFAULT_LOCAL_RPC_URL.to_string(), self.mocks))
}
}
pub fn create_mock_rpc_client_with_account(account: &Account) -> Arc<RpcClient> {
RpcMockBuilder::new().with_account_info(account).build()
}
pub fn create_mock_rpc_client_account_not_found() -> Arc<RpcClient> {
RpcMockBuilder::new().with_account_not_found().build()
}
pub fn create_mock_rpc_client_with_mint(mint_decimals: u8) -> Arc<RpcClient> {
RpcMockBuilder::new().with_mint_account(mint_decimals).build()
}