2016-02-09 12:27:05 +01:00
|
|
|
// Copyright 2015, 2016 Ethcore (UK) Ltd.
|
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2016-02-29 22:21:15 +01:00
|
|
|
use util::numbers::*;
|
2016-03-27 15:12:21 +02:00
|
|
|
use ethcore::transaction::{LocalizedTransaction, Action, SignedTransaction};
|
2016-02-09 13:17:44 +01:00
|
|
|
use v1::types::{Bytes, OptionalValue};
|
2016-02-08 11:58:47 +01:00
|
|
|
|
|
|
|
#[derive(Debug, Default, Serialize)]
|
|
|
|
pub struct Transaction {
|
2016-02-09 13:17:44 +01:00
|
|
|
pub hash: H256,
|
|
|
|
pub nonce: U256,
|
2016-02-08 11:58:47 +01:00
|
|
|
#[serde(rename="blockHash")]
|
2016-02-09 13:17:44 +01:00
|
|
|
pub block_hash: OptionalValue<H256>,
|
2016-02-08 11:58:47 +01:00
|
|
|
#[serde(rename="blockNumber")]
|
2016-02-09 13:17:44 +01:00
|
|
|
pub block_number: OptionalValue<U256>,
|
2016-02-08 11:58:47 +01:00
|
|
|
#[serde(rename="transactionIndex")]
|
2016-02-09 15:17:01 +01:00
|
|
|
pub transaction_index: OptionalValue<U256>,
|
2016-02-09 13:17:44 +01:00
|
|
|
pub from: Address,
|
|
|
|
pub to: OptionalValue<Address>,
|
|
|
|
pub value: U256,
|
2016-02-08 11:58:47 +01:00
|
|
|
#[serde(rename="gasPrice")]
|
2016-02-09 13:17:44 +01:00
|
|
|
pub gas_price: U256,
|
|
|
|
pub gas: U256,
|
|
|
|
pub input: Bytes
|
2016-02-08 11:58:47 +01:00
|
|
|
}
|
|
|
|
|
2016-02-09 16:38:21 +01:00
|
|
|
impl From<LocalizedTransaction> for Transaction {
|
|
|
|
fn from(t: LocalizedTransaction) -> Transaction {
|
|
|
|
Transaction {
|
|
|
|
hash: t.hash(),
|
|
|
|
nonce: t.nonce,
|
|
|
|
block_hash: OptionalValue::Value(t.block_hash.clone()),
|
|
|
|
block_number: OptionalValue::Value(U256::from(t.block_number)),
|
|
|
|
transaction_index: OptionalValue::Value(U256::from(t.transaction_index)),
|
|
|
|
from: t.sender().unwrap(),
|
|
|
|
to: match t.action {
|
|
|
|
Action::Create => OptionalValue::Null,
|
|
|
|
Action::Call(ref address) => OptionalValue::Value(address.clone())
|
|
|
|
},
|
|
|
|
value: t.value,
|
|
|
|
gas_price: t.gas_price,
|
|
|
|
gas: t.gas,
|
|
|
|
input: Bytes::new(t.data.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-27 15:12:21 +02:00
|
|
|
impl From<SignedTransaction> for Transaction {
|
|
|
|
fn from(t: SignedTransaction) -> Transaction {
|
|
|
|
Transaction {
|
|
|
|
hash: t.hash(),
|
|
|
|
nonce: t.nonce,
|
|
|
|
block_hash: OptionalValue::Null,
|
|
|
|
block_number: OptionalValue::Null,
|
|
|
|
transaction_index: OptionalValue::Null,
|
|
|
|
from: t.sender().unwrap(),
|
|
|
|
to: match t.action {
|
|
|
|
Action::Create => OptionalValue::Null,
|
|
|
|
Action::Call(ref address) => OptionalValue::Value(address.clone())
|
|
|
|
},
|
|
|
|
value: t.value,
|
|
|
|
gas_price: t.gas_price,
|
|
|
|
gas: t.gas,
|
|
|
|
input: Bytes::new(t.data.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-08 11:58:47 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use serde_json;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_transaction_serialize() {
|
|
|
|
let t = Transaction::default();
|
|
|
|
let serialized = serde_json::to_string(&t).unwrap();
|
2016-03-19 11:44:36 +01:00
|
|
|
assert_eq!(serialized, r#"{"hash":"0x0000000000000000000000000000000000000000000000000000000000000000","nonce":"0x00","blockHash":null,"blockNumber":null,"transactionIndex":null,"from":"0x0000000000000000000000000000000000000000","to":null,"value":"0x00","gasPrice":"0x00","gas":"0x00","input":"0x"}"#);
|
2016-02-08 11:58:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|