Skip to content

Commit 5d01397

Browse files
committed
De-stringify Receipt and Log
Fix #2
1 parent a778fd7 commit 5d01397

2 files changed

Lines changed: 34 additions & 38 deletions

File tree

src/rpc/mod.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,42 +31,42 @@ pub enum Either<T, U> {
3131
#[derive(Serialize, Deserialize, Debug, Clone)]
3232
#[serde(untagged)]
3333
pub enum RPCTopicFilter {
34-
Single(String),
35-
Or(Vec<String>)
34+
Single(Hex<H256>),
35+
Or(Vec<Hex<H256>>)
3636
}
3737

3838
#[derive(Serialize, Deserialize, Debug, Clone)]
3939
#[serde(rename_all = "camelCase")]
4040
pub struct RPCLogFilter {
4141
pub from_block: Option<String>,
4242
pub to_block: Option<String>,
43-
pub address: Option<String>,
43+
pub address: Option<Hex<Address>>,
4444
pub topics: Option<Vec<Option<RPCTopicFilter>>>,
4545
}
4646

4747
#[derive(Serialize, Deserialize, Debug, Clone)]
4848
#[serde(rename_all = "camelCase")]
4949
pub struct RPCLog {
5050
pub removed: bool,
51-
pub log_index: String,
52-
pub transaction_index: String,
53-
pub transaction_hash: String,
54-
pub block_hash: String,
55-
pub block_number: String,
56-
pub data: String,
57-
pub topics: Vec<String>,
51+
pub log_index: Hex<usize>,
52+
pub transaction_index: Hex<usize>,
53+
pub transaction_hash: Hex<H256>,
54+
pub block_hash: Hex<H256>,
55+
pub block_number: Hex<U256>,
56+
pub data: Bytes,
57+
pub topics: Vec<Hex<H256>>,
5858
}
5959

6060
#[derive(Serialize, Deserialize, Debug, Clone)]
6161
#[serde(rename_all = "camelCase")]
6262
pub struct RPCReceipt {
63-
pub transaction_hash: String,
64-
pub transaction_index: String,
65-
pub block_hash: String,
66-
pub block_number: String,
67-
pub cumulative_gas_used: String,
68-
pub gas_used: String,
69-
pub contract_address: Option<String>,
63+
pub transaction_hash: Hex<H256>,
64+
pub transaction_index: Hex<usize>,
65+
pub block_hash: Hex<H256>,
66+
pub block_number: Hex<U256>,
67+
pub cumulative_gas_used: Hex<Gas>,
68+
pub gas_used: Hex<Gas>,
69+
pub contract_address: Option<Hex<Address>>,
7070
pub logs: Vec<RPCLog>,
7171
pub root: Hex<H256>,
7272
pub status: usize,

src/rpc/util.rs

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,13 @@ pub fn to_rpc_log(receipt: &Receipt, index: usize, transaction: &Transaction, bl
5555

5656
RPCLog {
5757
removed: false,
58-
log_index: format!("0x{:x}", index),
59-
transaction_index: format!("0x{:x}", transaction_index),
60-
transaction_hash: format!("0x{:x}", transaction_hash),
61-
block_hash: format!("0x{:x}", block.header.header_hash()),
62-
block_number: format!("0x{:x}", block.header.number),
63-
data: to_hex(&receipt.logs[index].data),
64-
topics: receipt.logs[index].topics.iter().map(|t| format!("0x{:x}", t)).collect(),
58+
log_index: Hex(index),
59+
transaction_index: Hex(transaction_index),
60+
transaction_hash: Hex(transaction_hash),
61+
block_hash: Hex(block.header.header_hash()),
62+
block_number: Hex(block.header.number),
63+
data: Bytes(receipt.logs[index].data.clone()),
64+
topics: receipt.logs[index].topics.iter().map(|t| Hex(*t)).collect(),
6565
}
6666
}
6767

@@ -103,13 +103,13 @@ pub fn to_rpc_receipt(state: &MinerState, receipt: Receipt, transaction: &Transa
103103
};
104104

105105
Ok(RPCReceipt {
106-
transaction_hash: format!("0x{:x}", transaction_hash),
107-
transaction_index: format!("0x{:x}", transaction_index),
108-
block_hash: format!("0x{:x}", block.header.header_hash()),
109-
block_number: format!("0x{:x}", block.header.number),
110-
cumulative_gas_used: format!("0x{:x}", cumulative_gas_used),
111-
gas_used: format!("0x{:x}", receipt.used_gas),
112-
contract_address: contract_address.map(|v| format!("0x{:x}", v)),
106+
transaction_hash: Hex(transaction_hash),
107+
transaction_index: Hex(transaction_index),
108+
block_hash: Hex(block.header.header_hash()),
109+
block_number: Hex(block.header.number),
110+
cumulative_gas_used: Hex(cumulative_gas_used),
111+
gas_used: Hex(receipt.used_gas),
112+
contract_address: contract_address.map(|v| Hex(v)),
113113
logs: {
114114
let mut ret = Vec::new();
115115
for i in 0..receipt.logs.len() {
@@ -306,14 +306,10 @@ pub fn from_topic_filter(filter: Option<RPCTopicFilter>) -> Result<TopicFilter,
306306
Ok(match filter {
307307
None => TopicFilter::All,
308308
Some(RPCTopicFilter::Single(s)) => TopicFilter::Or(vec![
309-
H256::from_str(&s)?
309+
s.0
310310
]),
311311
Some(RPCTopicFilter::Or(ss)) => {
312-
let mut ret = Vec::new();
313-
for s in ss {
314-
ret.push(H256::from_str(&s)?)
315-
}
316-
TopicFilter::Or(ret)
312+
TopicFilter::Or(ss.into_iter().map(|v| v.0).collect())
317313
},
318314
})
319315
}
@@ -323,7 +319,7 @@ pub fn from_log_filter(state: &MinerState, filter: RPCLogFilter) -> Result<LogFi
323319
from_block: from_block_number(state, filter.from_block)?,
324320
to_block: from_block_number(state, filter.to_block)?,
325321
address: match filter.address {
326-
Some(val) => Some(Address::from_str(&val)?),
322+
Some(val) => Some(val.0),
327323
None => None,
328324
},
329325
topics: match filter.topics {

0 commit comments

Comments
 (0)