-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_rpc.rs
265 lines (199 loc) · 7.63 KB
/
json_rpc.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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
use std::{error::Error, str::FromStr};
use ethers::{
core::types::Block,
providers::{Http, Provider},
types::U256,
};
#[allow(dead_code)]
async fn get_block_number(provider: &Provider<Http>) -> Result<String, Box<dyn Error>> {
let block_number: String = provider.request("eth_blockNumber", ()).await?;
Ok(block_number)
}
#[allow(dead_code)]
async fn get_balance(provider: &Provider<Http>, address: &str) -> Result<String, Box<dyn Error>> {
let account_balance: String = provider
.request("eth_getBalance", (address, "latest"))
.await?;
Ok(account_balance)
}
#[allow(dead_code)]
async fn get_nonce(provider: &Provider<Http>, address: &str) -> Result<String, Box<dyn Error>> {
let account_nonce: String = provider
.request("eth_getTransactionCount", (address, "latest"))
.await?;
Ok(account_nonce)
}
#[allow(dead_code)]
async fn get_total_transactions(
provider: &Provider<Http>,
block_number: &str,
) -> Result<i128, Box<dyn Error>> {
let block: Block<String> = provider
.request("eth_getBlockByNumber", (block_number, false))
.await?;
Ok(block.transactions.len() as i128)
}
#[allow(dead_code)]
async fn get_total_balance(
provider: &Provider<Http>,
addresses: Vec<&str>,
) -> Result<U256, Box<dyn Error>> {
let params: Vec<(String, &str)> = addresses
.iter()
.map(|address| (String::from(*address), "latest"))
.collect();
let mut cl: Vec<String> = Vec::new();
for param in params {
let balance = provider.request("eth_getBalance", param).await?;
cl.push(balance)
}
let sum = cl
.into_iter()
.map(|account| U256::from_str(&account).unwrap())
.fold(U256::from(0), |acc, curr| acc.saturating_add(curr));
Ok(sum)
}
// Note: These tests must be run one by one or sequentially as running them in parallel can result in unexpected behaviour
#[cfg(test)]
mod tests {
mod get_block_number {
use std::{error::Error, str::FromStr};
use ethers::{providers::Middleware, types::U64};
use crate::week_3::json_rpc::get_block_number;
#[ignore]
#[tokio::test]
async fn should_get_the_latest_block_number() -> Result<(), Box<dyn Error>> {
// Arrange
let provider = crate::utils::get_provider();
let current_block_number = provider.get_block_number().await?;
let expected_block_number = current_block_number + U64::from(1);
crate::utils::mine_block(&provider).await?;
// Act
let res = get_block_number(&provider).await;
// Assert
assert!(res.is_ok());
assert_eq!(U64::from_str(&res?)?, expected_block_number);
Ok(())
}
}
mod get_balance {
use std::error::Error;
use crate::week_3::json_rpc::get_balance;
#[ignore]
#[tokio::test]
async fn should_get_the_address_balance() -> Result<(), Box<dyn Error>> {
// Arrange
let provider = crate::utils::get_provider();
let address = "0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266";
// 10000 * (10 ^ 18) ETH
let expected_balance: String = "0x21e19e0c9bab2400000".to_string();
// Act
let res = get_balance(&provider, address).await;
// Assert
assert!(res.is_ok());
assert_eq!(res?, expected_balance);
Ok(())
}
}
mod get_nonce {
use std::{error::Error, ops::Add, str::FromStr};
use ethers::{abi::Address, providers::Middleware, types::U256};
use crate::week_3::json_rpc::get_nonce;
#[ignore]
#[tokio::test]
async fn should_get_the_account_nonce() -> Result<(), Box<dyn Error>> {
// Arrange
let client_with_signer = crate::utils::get_provider_with_signer(None, None);
let provider = client_with_signer.inner();
let address = "0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266";
let current_account_nonce = client_with_signer
.get_transaction_count(address.parse::<Address>()?, None)
.await?;
let expected_nonce = current_account_nonce.add(1);
crate::utils::send_ether(&client_with_signer, 1, None).await?;
crate::utils::mine_block(provider).await?;
// Act
let res = get_nonce(provider, address).await;
// Assert
assert!(res.is_ok());
assert_eq!(U256::from_str(&res?)?, expected_nonce);
Ok(())
}
}
mod get_total_transactions {
use std::error::Error;
use ethers::{
prelude::SignerMiddleware,
providers::{Http, Middleware, Provider},
signers::Wallet,
};
use k256::ecdsa::SigningKey;
use crate::week_3::json_rpc::get_total_transactions;
async fn test_get_total_transactions(
client: &SignerMiddleware<Provider<Http>, Wallet<SigningKey>>,
num_transactions: i128,
) -> Result<(), Box<dyn Error>> {
// Arrange
let provider = client.inner();
for _ in 0..num_transactions {
crate::utils::send_ether(client, 1, None).await?;
}
crate::utils::mine_block(provider).await?;
let current_block_number = provider.get_block_number().await?;
// Act
let res = get_total_transactions(provider, ¤t_block_number.to_string()).await;
// Assert
assert!(res.is_ok());
assert_eq!(res.unwrap(), num_transactions);
Ok(())
}
#[ignore]
#[tokio::test]
async fn should_get_the_expected_number_of_total_transactions() -> Result<(), Box<dyn Error>>
{
// Arrange
let client_with_signer = crate::utils::get_provider_with_signer(None, None);
let transactions_num = vec![0, 1, 3, 5, 11];
for transaction_num in transactions_num {
test_get_total_transactions(&client_with_signer, transaction_num).await?
}
Ok(())
}
}
mod get_total_balance {
use std::error::Error;
use ethers::providers::Middleware;
use ethers::types::U256;
use crate::week_3::json_rpc::get_total_balance;
#[ignore]
#[tokio::test]
async fn should_get_the_total_balance_of_the_addresses() -> Result<(), Box<dyn Error>> {
// Arrange
let client_with_signer = crate::utils::get_provider_with_signer(None, None);
let provider = client_with_signer.inner();
let addresses = vec![
"0x5409ed021d9299bf6814279a6a1411a7e866a631",
"0xebbe46f475db84e70313592eb4f94df73043c118",
"0xd4d38fc5fd03a9beba9e9a41573ef8de75c2784c",
"0xec4a61ce697253baa1088b2ea9112b9483098e64",
"0xfbf1d566853edc65cdeda8e22975ca1ebfc4ed38",
];
let expected_total = 15 * (10 ^ 18);
for (idx, address) in addresses.iter().enumerate() {
crate::utils::send_ether(
&client_with_signer,
((idx as i128) + 1) * (10 ^ 18),
Some(address),
)
.await?;
}
crate::utils::mine_block(provider).await?;
// Act
let res = get_total_balance(provider, addresses).await;
// Assert
assert!(res.is_ok());
assert_eq!(res.unwrap(), U256::from(expected_total));
Ok(())
}
}
}