mwc_web3/types/
signed.rs

1use crate::types::{Address, Bytes, CallRequest, H256, U256};
2use serde::{Deserialize, Serialize};
3
4/// Struct representing signed data returned from `Accounts::sign` method.
5#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
6pub struct SignedData {
7    /// The original message that was signed.
8    pub message: Vec<u8>,
9    /// The keccak256 hash of the signed data.
10    #[serde(rename = "messageHash")]
11    pub message_hash: H256,
12    /// V value in 'Electrum' notation.
13    pub v: u8,
14    /// R value.
15    pub r: H256,
16    /// S value.
17    pub s: H256,
18    /// The signature bytes.
19    pub signature: Bytes,
20}
21
22/// Transaction data for signing.
23///
24/// The `Accounts::sign_transaction` method will fill optional fields with sane
25/// defaults when they are omitted. Specifically the signing account's current
26/// transaction count will be used for the `nonce`, the estimated recommended
27/// gas price will be used for `gas_price`, and the current network ID will be
28/// used for the `chain_id`.
29///
30/// It is worth noting that the chain ID is not equivalent to the network ID.
31/// They happen to be the same much of the time but it is recommended to set
32/// this for signing transactions.
33///
34/// `TransactionParameters` implements `Default` and uses `100_000` as the
35/// default `gas` to use for the transaction. This is more than enough for
36/// simple transactions sending ETH between accounts but may not be enough when
37/// interacting with complex contracts. It is recommended when interacting
38/// with contracts to use `Eth::estimate_gas` to estimate the required gas for
39/// the transaction.
40#[derive(Clone, Debug, PartialEq)]
41pub struct TransactionParameters {
42    /// Transaction nonce (None for account transaction count)
43    pub nonce: Option<U256>,
44    /// To address
45    pub to: Option<Address>,
46    /// Supplied gas
47    pub gas: U256,
48    /// Gas price (None for estimated gas price)
49    pub gas_price: Option<U256>,
50    /// Transferred value
51    pub value: U256,
52    /// Data
53    pub data: Bytes,
54    /// The chain ID (None for network ID)
55    pub chain_id: Option<u64>,
56}
57
58/// The default fas for transactions.
59///
60/// Unfortunately there is no way to construct `U256`s with const functions for
61/// constants so we just build it from it's `u64` words. Note that there is a
62/// unit test to verify that it is constructed correctly and holds the expected
63/// value of 100_000.
64const TRANSACTION_DEFAULT_GAS: U256 = U256([100_000, 0, 0, 0]);
65
66impl Default for TransactionParameters {
67    fn default() -> Self {
68        TransactionParameters {
69            nonce: None,
70            to: None,
71            gas: TRANSACTION_DEFAULT_GAS,
72            gas_price: None,
73            value: U256::zero(),
74            data: Bytes::default(),
75            chain_id: None,
76        }
77    }
78}
79
80impl From<CallRequest> for TransactionParameters {
81    fn from(call: CallRequest) -> Self {
82        TransactionParameters {
83            nonce: None,
84            to: call.to,
85            gas: call.gas.unwrap_or(TRANSACTION_DEFAULT_GAS),
86            gas_price: call.gas_price,
87            value: call.value.unwrap_or_default(),
88            data: call.data.unwrap_or_default(),
89            chain_id: None,
90        }
91    }
92}
93
94impl Into<CallRequest> for TransactionParameters {
95    fn into(self) -> CallRequest {
96        CallRequest {
97            from: None,
98            to: self.to,
99            gas: Some(self.gas),
100            gas_price: self.gas_price,
101            value: Some(self.value),
102            data: Some(self.data),
103        }
104    }
105}
106
107/// Data for offline signed transaction
108#[derive(Clone, Debug, PartialEq)]
109pub struct SignedTransaction {
110    /// The given message hash
111    pub message_hash: H256,
112    /// V value with chain replay protection.
113    pub v: u64,
114    /// R value.
115    pub r: H256,
116    /// S value.
117    pub s: H256,
118    /// The raw signed transaction ready to be sent with `send_raw_transaction`
119    pub raw_transaction: Bytes,
120    /// The transaction hash for the RLP encoded transaction.
121    pub transaction_hash: H256,
122}
123
124#[cfg(test)]
125mod tests {
126    use super::*;
127
128    #[test]
129    fn verify_transaction_default_gas() {
130        assert_eq!(TRANSACTION_DEFAULT_GAS, U256::from(100_000));
131    }
132}