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
// This file is part of Astar.

// Copyright (C) Stake Technologies Pte.Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later

// Astar 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.

// Astar 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 Astar. If not, see <http://www.gnu.org/licenses/>.

use parity_scale_codec::{Decode, Encode};
use scale_info::TypeInfo;

use ethereum::{
    AccessListItem, EIP1559Transaction, TransactionAction, TransactionV2 as Transaction,
};
use ethereum_types::{H160, H256, U256};
use frame_support::{pallet_prelude::*, traits::ConstU32, BoundedVec};
use sp_std::prelude::*;

/// Max Ethereum tx input size: 65_536 bytes
pub const MAX_ETHEREUM_TX_INPUT_SIZE: u32 = 2u32.pow(16);

pub type EthereumTxInput = BoundedVec<u8, ConstU32<MAX_ETHEREUM_TX_INPUT_SIZE>>;

/// The checked Ethereum transaction. Only contracts `call` is support(no `create`).
#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo)]
pub struct CheckedEthereumTx {
    /// Gas limit.
    pub gas_limit: U256,
    /// Contract address to call.
    pub target: H160,
    /// Amount to transfer.
    pub value: U256,
    /// Input of a contract call.
    pub input: EthereumTxInput,
    /// Optional access list, specified in EIP-2930.
    pub maybe_access_list: Option<Vec<(H160, Vec<H256>)>>,
}

impl CheckedEthereumTx {
    pub fn into_ethereum_tx(&self, nonce: U256, chain_id: u64) -> Transaction {
        let access_list = if let Some(ref list) = self.maybe_access_list {
            list.iter()
                .map(|(address, storage_keys)| AccessListItem {
                    address: *address,
                    storage_keys: storage_keys.clone(),
                })
                .collect()
        } else {
            Vec::new()
        };

        Transaction::EIP1559(EIP1559Transaction {
            chain_id,
            nonce,
            max_fee_per_gas: U256::zero(),
            max_priority_fee_per_gas: U256::zero(),
            gas_limit: self.gas_limit,
            value: self.value,
            action: TransactionAction::Call(self.target),
            input: self.input.to_vec(),
            access_list,
            odd_y_parity: true,
            r: dummy_rs(),
            s: dummy_rs(),
        })
    }
}

/// Dummy signature for all transactions.
fn dummy_rs() -> H256 {
    H256::from_low_u64_be(1u64)
}