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
mod recover;

use cfx_types::{Address, AddressSpaceUtil, Bloom, Space, U256};
use primitives::{
    transaction::eth_transaction::Eip155Transaction, Action, LogEntry, Receipt,
    SignedTransaction, TransactionStatus,
};

pub use recover::{build_bloom_and_recover_phantom, recover_phantom};

#[derive(Clone, Debug, Default)]
pub struct PhantomTransaction {
    pub from: Address,
    pub nonce: U256,
    pub action: Action,
    pub value: U256,
    pub data: Vec<u8>,

    pub log_bloom: Bloom,
    pub logs: Vec<LogEntry>,
    pub outcome_status: TransactionStatus,
}

impl PhantomTransaction {
    fn simple_transfer(
        from: Address, to: Address, nonce: U256, value: U256, data: Vec<u8>,
    ) -> PhantomTransaction {
        PhantomTransaction {
            from,
            nonce,
            action: Action::Call(to),
            value,
            data,
            outcome_status: TransactionStatus::Success,
            ..Default::default()
        }
    }
}

impl PhantomTransaction {
    pub fn into_eip155(self, chain_id: u32) -> SignedTransaction {
        let tx = Eip155Transaction {
            action: self.action,
            chain_id: Some(chain_id),
            data: self.data,
            gas_price: 0.into(),
            gas: 0.into(),
            nonce: self.nonce,
            value: self.value,
        };

        tx.fake_sign_phantom(self.from.with_space(Space::Ethereum))
    }

    pub fn into_receipt(self, accumulated_gas_used: U256) -> Receipt {
        Receipt {
            accumulated_gas_used,
            gas_fee: 0.into(),
            gas_sponsor_paid: false,
            log_bloom: self.log_bloom,
            logs: self.logs,
            outcome_status: self.outcome_status,
            storage_collateralized: vec![],
            storage_released: vec![],
            storage_sponsor_paid: false,
            burnt_gas_fee: None,
        }
    }
}