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
//! Ethereum types' definitions.
use serde::{Deserialize, Serialize, Serializer};

use crate::types::*;

/// Hash types representing 32-bytes hashes.
pub type Hash = H256;

/// The block number type.
pub enum BlockNumber {
    /// 32-byte number
    Number(U256),
    /// Earliest block
    Earliest,
    /// Latest block
    Latest,
    /// Pending block
    Pending,
}

impl Serialize for BlockNumber {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        match *self {
            BlockNumber::Number(ref number) => number.serialize(serializer),
            BlockNumber::Earliest => "earliest".serialize(serializer),
            BlockNumber::Latest => "latest".serialize(serializer),
            BlockNumber::Pending => "pending".serialize(serializer),
        }
    }
}

/// The transactions in a block.
#[derive(Debug, Deserialize)]
#[serde(untagged)]
pub enum BlockTransactions {
    /// A vector of hashes
    Hashes(Vec<Hash>),
    /// A vector of [`Transaction`](struct.Transaction.html) objects
    Full(Vec<Transaction>),
}

/// An Ethereum transaction.
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Transaction {
    /// Hash of the block where this transaction was in
    pub block_hash: Option<Hash>,
    /// Block number where this transaction was in
    pub block_number: Option<U256>,
    /// Address of sender
    pub from: Address,
    /// Gas
    pub gas: U256,
    /// Gas price
    pub gas_price: U256,
    /// Transaction hash
    pub hash: Hash,
    /// Data
    pub input: Bytes,
    /// Number of transactions made by the sender prior to this one
    pub nonce: U256,
    /// Address of receiver
    pub to: Option<Address>,
    /// Transaction's index position in the block
    pub transaction_index: Option<U256>,
    /// Value transferred in Wei
    pub value: U256,
    /// ECDSA recovery id
    pub v: U256,
    /// ECDSA signature r
    pub r: U256,
    /// ECDSA signature s
    pub s: U256,
}

/// An Ethereum Block.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Block {
    /// Block number
    pub number: Option<U256>,
    /// Hash of the block
    pub hash: Option<Hash>,
    /// Hash of the parent block
    pub parent_hash: Hash,
    /// Hash of the generated proof-of-work
    pub nonce: Option<U64>,
    /// SHA3 of the uncles data in the block
    pub sha3_uncles: Hash,
    /// Bloom filter for the logs of the block
    pub logs_bloom: Option<Bloom>,
    /// Root of the transaction trie of the block
    pub transactions_root: Hash,
    /// Root of the final state trie of the block
    pub state_root: Hash,
    /// Root of the receipts trie of the block
    pub receipts_root: Hash,
    /// Address of the beneficiary to whom the mining rewards were given
    pub author: Option<Address>,
    /// Alias of `author`
    pub miner: Address,
    /// Difficulty for this block
    pub difficulty: U256,
    /// Total difficulty of the chain until this block
    pub total_difficulty: Option<U256>,
    /// 'extra data' field of this block
    pub extra_data: Bytes,
    /// Size of this block in bytes
    pub size: Option<U256>,
    /// Maximum gas allowed in this block
    pub gas_limit: U256,
    /// Total used gas by all transactions in this block
    pub gas_used: U256,
    /// unix timestamp for when the block was collated
    pub timestamp: U256,
    /// Seal fields
    pub seal_fields: Option<Vec<Bytes>>,
    /// Transactions
    pub transactions: BlockTransactions,
    /// Vector of uncle hashes
    pub uncles: Vec<Hash>,
}

/// An ethereum log.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Log {
    /// Address from which this log originated
    pub address: Address,
    /// Hash of the block where this log was in
    pub block_hash: Option<Hash>,
    /// Block number where this log was in
    pub block_number: Option<U256>,
    /// Non-indexed arguments of the log
    pub data: Bytes,
    /// Log's index position in the block
    pub log_index: Option<U256>,
    /// Indicates whether the log was removed due to a chain reorganization
    pub removed: bool,
    /// Vector of 0 to 4 32 Bytes DATA of indexed log arguments
    pub topics: Vec<Hash>,
    /// Hash of the transaction this log was created from
    pub transaction_hash: Option<Hash>,
    /// Transaction's index position this log was created from
    pub transaction_index: Option<U256>,
    /// Log's index position in Transaction
    pub transaction_log_index: Option<U256>,
    /// Log type
    #[serde(rename = "type")]
    pub log_type: String,
}

/// Response of [`eth_getFilterChanges`](../api/struct.Api.html#method.get_filter_changes),
/// [`eth_getFilterLogs`](../api/struct.Api.html#method.get_filter_logs) &
/// [`eth_getLogs`](../api/struct.Api.html#method.get_logs) API methods.
#[derive(Debug, Deserialize)]
#[serde(untagged)]
pub enum FilterChanges {
    /// A vector of [`Log`](struct.Log.html) objects
    Logs(Vec<Log>),
    /// A vector of [`Hash`](type.Hash.html)s
    BlockHashes(Vec<Hash>),
}

/// Transaction type used as i/p for
/// [`eth_sendTransaction`](../api/struct.Api.html#method.send_transaction) API method.
#[derive(Debug, Serialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct OutgoingTransaction {
    /// Address of sender
    pub from: Address,
    /// Address of receiver
    pub to: Address,
    /// Gas
    #[serde(skip_serializing_if = "Option::is_none")]
    pub gas: Option<U256>,
    /// Gas price
    #[serde(skip_serializing_if = "Option::is_none")]
    pub gas_price: Option<U256>,
    /// Value to transfer in Wei
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value: Option<U256>,
    /// Data
    #[serde(skip_serializing_if = "Option::is_none")]
    pub data: Option<Bytes>,
    /// Number of transactions made by the sender prior to this one
    #[serde(skip_serializing_if = "Option::is_none")]
    pub nonce: Option<U256>,
}

/// Transaction type used as i/p for [`eth_call`](../api/struct.Api.html#method.call) &
/// [`eth_estimateGas`](../api/struct.Api.html#method.estimate_gas) API methods.
#[derive(Debug, Serialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct CallTransaction {
    /// Address of sender
    #[serde(skip_serializing_if = "Option::is_none")]
    pub from: Option<Address>,
    /// Address of receiver (optional only for [`eth_estimateGas`](../api/struct.Api.html#method.estimate_gas)
    /// API method)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub to: Option<Address>,
    /// Gas
    #[serde(skip_serializing_if = "Option::is_none")]
    pub gas: Option<U256>,
    /// Gas price
    #[serde(skip_serializing_if = "Option::is_none")]
    pub gas_price: Option<U256>,
    /// Value transferred in Wei
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value: Option<U256>,
    /// Data
    #[serde(skip_serializing_if = "Option::is_none")]
    pub data: Option<Bytes>,
}

/// An Ethereum Transaction receipt.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TransactionReceipt {
    /// Hash of the transaction
    pub transaction_hash: Option<H256>,
    /// Transaction's index position in the block
    pub transaction_index: Option<U256>,
    /// Hash of the block where this transaction was in
    pub block_hash: Option<H256>,
    /// Block number where this transaction was in
    pub block_number: Option<U256>,
    /// Total amount of gas used when this transaction was executed in the block
    pub cumulative_gas_used: U256,
    /// Amount of gas used by this specific transaction alone
    pub gas_used: Option<U256>,
    /// Contract address created, if the transaction was a contract creation
    pub contract_address: Option<Address>,
    /// Vector of [`Log`](struct.Log.html) objects
    pub logs: Vec<Log>,
    /// Bloom filter for the logs of the block
    pub logs_bloom: Bloom,
    /// Indicates success or failure
    pub status: Option<bool>,
}