#![cfg(test)]

use core::str::FromStr;

use runner::Runner as StabilityRunner;

use ethereum::{TransactionAction, TransactionSignature};
use frame_support::{
	construct_runtime,
	pallet_prelude::{StorageValue, ValueQuery},
	parameter_types,
	traits::{Everything, GenesisBuild, StorageInstance},
	weights::Weight,
};
use pallet_evm::{EnsureAddressNever, EnsureAddressRoot};
use rlp::RlpStream;
use sp_core::{keccak_256, H160, H256, U256};
use sp_runtime::{
	traits::{BlakeTwo256, ConstU32, IdentifyAccount, IdentityLookup, Verify},
	MultiSignature,
};
use std::collections::BTreeMap;

pub type Signature = MultiSignature;

pub type AccountId = <<Signature as Verify>::Signer as IdentifyAccount>::AccountId;
pub type Balance = u128;
pub type BlockNumber = u32;
type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Runtime>;
type Block = frame_system::mocking::MockBlock<Runtime>;

pub struct MockUserFeeTokenController;
impl pallet_user_fee_selector::UserFeeTokenController for MockUserFeeTokenController {
	type Error = ();

	fn balance_of(_token: H160) -> sp_core::U256 {
		return U256::from(1000000);
	}

	fn get_user_fee_token(_account: H160) -> H160 {
		return ERC20SlotZero::get();
	}

	fn set_user_fee_token(_account: H160, _token: H160) -> Result<(), Self::Error> {
		Ok(())
	}
}

parameter_types! {
	pub const BlockHashCount: u32 = 250;
	pub const SS58Prefix: u8 = 42;
}

impl frame_system::Config for Runtime {
	type BaseCallFilter = Everything;
	type DbWeight = ();
	type RuntimeOrigin = RuntimeOrigin;
	type Index = u64;
	type BlockNumber = BlockNumber;
	type RuntimeCall = RuntimeCall;
	type Hash = H256;
	type Hashing = BlakeTwo256;
	type AccountId = AccountId;
	type Lookup = IdentityLookup<AccountId>;
	type Header = sp_runtime::generic::Header<BlockNumber, BlakeTwo256>;
	type RuntimeEvent = RuntimeEvent;
	type BlockHashCount = BlockHashCount;
	type Version = ();
	type PalletInfo = PalletInfo;
	type AccountData = pallet_balances::AccountData<Balance>;
	type OnNewAccount = ();
	type OnKilledAccount = ();
	type SystemWeightInfo = ();
	type BlockWeights = ();
	type BlockLength = ();
	type SS58Prefix = SS58Prefix;
	type OnSetCode = ();
	type MaxConsumers = frame_support::traits::ConstU32<16>;
}

parameter_types! {
	pub const ExistentialDeposit: u128 = 1;
}

impl pallet_balances::Config for Runtime {
	type MaxReserves = ();
	type ReserveIdentifier = ();
	type MaxLocks = ();
	type Balance = Balance;
	type RuntimeEvent = RuntimeEvent;
	type DustRemoval = ();
	type ExistentialDeposit = ExistentialDeposit;
	type AccountStore = System;
	type WeightInfo = ();
	type MaxHolds = ();
	type HoldIdentifier = ();
	type FreezeIdentifier = ();
	type MaxFreezes = ();
}

parameter_types! {
	pub const MinimumPeriod: u64 = 5;
}

impl pallet_timestamp::Config for Runtime {
	type Moment = u64;
	type OnTimestampSet = ();
	type MinimumPeriod = MinimumPeriod;
	type WeightInfo = ();
}

pub struct MockDNTFeeController;
impl runner::OnChargeDecentralizedNativeTokenFee for MockDNTFeeController {
	type Error = ();

	fn get_transaction_fee_token(_from: H160) -> H160 {
		Default::default()
	}

	fn get_transaction_conversion_rate(
		_user: H160,
		_validator: H160,
		_token: H160,
	) -> (U256, U256) {
		(1.into(), 1.into())
	}

	fn get_fee_vault() -> H160 {
		Default::default()
	}

	fn withdraw_fee(
		_from: H160,
		_token: H160,
		_conversion_rate: (U256, U256),
		_amount: U256,
	) -> Result<(), Self::Error> {
		Ok(())
	}

	fn correct_fee(
		_from: H160,
		_token: H160,
		_conversion_rate: (U256, U256),
		_paid_amount: U256,
		_actual_amount: U256,
	) -> Result<(), Self::Error> {
		Ok(())
	}

	fn pay_fees(
		_token: H160,
		_conversion_rate: (U256, U256),
		_actual_amount: U256,
		_validator: H160,
		_to: Option<H160>,
	) -> Result<(U256, U256), Self::Error> {
		Ok((Default::default(), Default::default()))
	}
}

parameter_types! {
	pub BlockGasLimit: U256 = U256::max_value();
	pub const WeightPerGas: Weight = Weight::from_parts(1, 0);
	pub ERC20SlotZero: H160 = H160::from_str("0x22D598E0a9a1b474CdC7c6fBeA0B4F83E12046a9").unwrap();
	pub ZeroSlot : H256 = H256::from_low_u64_be(0);
}

parameter_types! {
	pub const ChainId: u64 = 20180428;
	pub const GasLimitPovSizeRatio: u64 = 15;
}

impl pallet_evm::Config for Runtime {
	type FeeCalculator = ();
	type GasWeightMapping = pallet_evm::FixedGasWeightMapping<Self>;
	type WeightPerGas = WeightPerGas;
	type CallOrigin = EnsureAddressRoot<AccountId>;
	type WithdrawOrigin = EnsureAddressNever<AccountId>;
	type AddressMapping = pallet_evm::HashedAddressMapping<BlakeTwo256>;
	type Currency = Balances;
	type RuntimeEvent = RuntimeEvent;
	type Runner = StabilityRunner<Self, MockDNTFeeController, MockUserFeeTokenController>;
	type PrecompilesType = ();
	type PrecompilesValue = ();
	type ChainId = ChainId;
	type OnChargeTransaction = ();
	type BlockGasLimit = BlockGasLimit;
	type BlockHashMapping = pallet_evm::SubstrateBlockHashMapping<Self>;
	type FindAuthor = ();
	type OnCreate = ();
	type GasLimitPovSizeRatio = GasLimitPovSizeRatio;
	type Timestamp = Timestamp;
	type WeightInfo = pallet_evm::weights::SubstrateWeight<Self>;
}

parameter_types! {
	pub const PostBlockAndTxnHashes: pallet_ethereum::PostLogContent = pallet_ethereum::PostLogContent::BlockAndTxnHashes;
}

impl pallet_ethereum::Config for Runtime {
	type RuntimeEvent = RuntimeEvent;

	type StateRoot = ();
	type PostLogContent = PostBlockAndTxnHashes;
	type ExtraDataLength = ConstU32<30>;
}

pub struct MockPrefix;
impl StorageInstance for MockPrefix {
	fn pallet_prefix() -> &'static str {
		"MockPrefix"
	}

	const STORAGE_PREFIX: &'static str = "MockPrefix";
}

// Bool value is true for deposit and false for withdraw
pub type StorageCalledArguments =
	StorageValue<MockPrefix, Vec<(bool, H160, H160, U256)>, ValueQuery>;

pub struct MockERC20Manager;
impl pallet_erc20_manager::ERC20Manager for MockERC20Manager {
	type Error = ();

	fn balance_of(_token: H160, _payer: H160) -> U256 {
		U256::max_value()
	}

	fn withdraw_amount(token: H160, from: H160, amount: U256) -> Result<U256, Self::Error> {
		StorageCalledArguments::mutate(|args| {
			args.push((false, token, from, amount));
		});
		Ok(amount.into())
	}

	fn deposit_amount(token: H160, from: H160, amount: U256) -> Result<U256, Self::Error> {
		StorageCalledArguments::mutate(|args| {
			args.push((true, token, from, amount));
		});
		Ok(amount.into())
	}
}

pub struct LegacyUnsignedTransaction {
	pub nonce: U256,
	pub gas_price: U256,
	pub gas_limit: U256,
	pub action: TransactionAction,
	pub value: U256,
	pub input: Vec<u8>,
}

impl LegacyUnsignedTransaction {
	fn signing_rlp_append(&self, s: &mut RlpStream) {
		s.begin_list(9);
		s.append(&self.nonce);
		s.append(&self.gas_price);
		s.append(&self.gas_limit);
		s.append(&self.action);
		s.append(&self.value);
		s.append(&self.input);
		s.append(&ChainId::get());
		s.append(&0u8);
		s.append(&0u8);
	}

	fn signing_hash(&self) -> H256 {
		let mut stream = RlpStream::new();
		self.signing_rlp_append(&mut stream);
		H256::from(keccak_256(&stream.out()))
	}

	pub fn sign(&self, key: &H256) -> ethereum::LegacyTransaction {
		self.sign_with_chain_id(key, ChainId::get())
	}

	pub fn sign_with_chain_id(&self, key: &H256, chain_id: u64) -> ethereum::LegacyTransaction {
		let hash = self.signing_hash();
		let msg = libsecp256k1::Message::parse(hash.as_fixed_bytes());
		let s = libsecp256k1::sign(
			&msg,
			&libsecp256k1::SecretKey::parse_slice(&key[..]).unwrap(),
		);
		let sig = s.0.serialize();

		let sig = TransactionSignature::new(
			s.1.serialize() as u64 % 2 + chain_id * 2 + 35,
			H256::from_slice(&sig[0..32]),
			H256::from_slice(&sig[32..64]),
		)
		.unwrap();

		ethereum::LegacyTransaction {
			nonce: self.nonce,
			gas_price: self.gas_price,
			gas_limit: self.gas_limit,
			action: self.action,
			value: self.value,
			input: self.input.clone(),
			signature: sig,
		}
	}
}

fn legacy_erc20_creation_unsigned_transaction(nonce: U256) -> LegacyUnsignedTransaction {
	LegacyUnsignedTransaction {
		nonce,
		gas_price: U256::from(0),
		gas_limit: U256::from(0x100000),
		action: ethereum::TransactionAction::Call(Sponsor::get()),
		value: U256::zero(),
		input: vec![],
	}
}

pub fn legacy_erc20_creation_transaction(
	nonce: U256,
	private_key: &H256,
) -> ethereum::LegacyTransaction {
	legacy_erc20_creation_unsigned_transaction(nonce).sign(private_key)
}

impl crate::Config for Runtime {
	type RuntimeCall = RuntimeCall;
}

// Configure a mock runtime to test the pallet.
construct_runtime!(
	pub enum Runtime where
		Block = Block,
		NodeBlock = Block,
		UncheckedExtrinsic = UncheckedExtrinsic,
	{
		System: frame_system::{Pallet, Call, Config, Storage, Event<T>},
		Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>},
		Evm: pallet_evm::{Pallet, Call, Storage, Event<T>},
		Timestamp: pallet_timestamp::{Pallet, Call, Storage, Inherent},
		Ethereum: pallet_ethereum,
		MetaTransactions: crate
	}
);

parameter_types! {
	pub MetaDeploymentTransaction: Vec<u8> = hex::decode("02f92115840133edcc80849502f90085010c388d00832dc6c08080b920b760806040523480156200001157600080fd5b506040518060400160405280600f81526020017f4d65616e696e676c6573732045555200000000000000000000000000000000008152506040518060400160405280600481526020017f4d4555520000000000000000000000000000000000000000000000000000000081525081600390805190602001906200009692919062000357565b508060049080519060200190620000af92919062000357565b505050620000d2620000c6620000fb60201b60201c565b6200010360201b60201c565b620000f5337002f050fe938943acc45f65568000000000620001c960201b60201c565b620005a5565b600033905090565b6000600560009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16905081600560006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055508173ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff167f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e060405160405180910390a35050565b620001db8282620001df60201b60201c565b5050565b600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff16141562000252576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040162000249906200045a565b60405180910390fd5b62000266600083836200034d60201b60201c565b80600260008282546200027a9190620004aa565b92505081905550806000808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825401925050819055508173ffffffffffffffffffffffffffffffffffffffff16600073ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef836040516200032d91906200047c565b60405180910390a362000349600083836200035260201b60201c565b5050565b505050565b505050565b828054620003659062000511565b90600052602060002090601f016020900481019282620003895760008555620003d5565b82601f10620003a457805160ff1916838001178555620003d5565b82800160010185558215620003d5579182015b82811115620003d4578251825591602001919060010190620003b7565b5b509050620003e49190620003e8565b5090565b5b8082111562000403576000816000905550600101620003e9565b5090565b600062000416601f8362000499565b91507f45524332303a206d696e7420746f20746865207a65726f2061646472657373006000830152602082019050919050565b620004548162000507565b82525050565b60006020820190508181036000830152620004758162000407565b9050919050565b600060208201905062000493600083018462000449565b92915050565b600082825260208201905092915050565b6000620004b78262000507565b9150620004c48362000507565b9250827fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff03821115620004fc57620004fb62000547565b5b828201905092915050565b6000819050919050565b600060028204905060018216806200052a57607f821691505b6020821081141562000541576200054062000576565b5b50919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b611b0280620005b56000396000f3fe608060405234801561001057600080fd5b506004361061010b5760003560e01c806370a08231116100a257806395d89b411161007157806395d89b41146102a6578063a457c2d7146102c4578063a9059cbb146102f4578063dd62ed3e14610324578063f2fde38b146103545761010b565b806370a0823114610232578063715018a61461026257806379cc67901461026c5780638da5cb5b146102885761010b565b8063313ce567116100de578063313ce567146101ac57806339509351146101ca57806340c10f19146101fa57806342966c68146102165761010b565b806306fdde0314610110578063095ea7b31461012e57806318160ddd1461015e57806323b872dd1461017c575b600080fd5b610118610370565b604051610125919061172b565b60405180910390f35b610148600480360381019061014391906111c5565b610402565b6040516101559190611710565b60405180910390f35b610166610425565b60405161017391906118cd565b60405180910390f35b61019660048036038101906101919190611176565b61042f565b6040516101a39190611710565b60405180910390f35b6101b461045e565b6040516101c191906118e8565b60405180910390f35b6101e460048036038101906101df91906111c5565b610467565b6040516101f19190611710565b60405180910390f35b610214600480360381019061020f91906111c5565b61049e565b005b610230600480360381019061022b9190611201565b6104ac565b005b61024c60048036038101906102479190611111565b6104c0565b60405161025991906118cd565b60405180910390f35b61026a610508565b005b610286600480360381019061028191906111c5565b61051c565b005b61029061053c565b60405161029d91906116f5565b60405180910390f35b6102ae610566565b6040516102bb919061172b565b60405180910390f35b6102de60048036038101906102d991906111c5565b6105f8565b6040516102eb9190611710565b60405180910390f35b61030e600480360381019061030991906111c5565b61066f565b60405161031b9190611710565b60405180910390f35b61033e6004803603810190610339919061113a565b610692565b60405161034b91906118cd565b60405180910390f35b61036e60048036038101906103699190611111565b610719565b005b60606003805461037f906119fd565b80601f01602080910402602001604051908101604052809291908181526020018280546103ab906119fd565b80156103f85780601f106103cd576101008083540402835291602001916103f8565b820191906000526020600020905b8154815290600101906020018083116103db57829003601f168201915b5050505050905090565b60008061040d61079d565b905061041a8185856107a5565b600191505092915050565b6000600254905090565b60008061043a61079d565b9050610447858285610970565b6104528585856109fc565b60019150509392505050565b60006012905090565b60008061047261079d565b90506104938185856104848589610692565b61048e919061191f565b6107a5565b600191505092915050565b6104a88282610c74565b5050565b6104bd6104b761079d565b82610dcb565b50565b60008060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020549050919050565b610510610f99565b61051a6000611017565b565b61052e8261052861079d565b83610970565b6105388282610dcb565b5050565b6000600560009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16905090565b606060048054610575906119fd565b80601f01602080910402602001604051908101604052809291908181526020018280546105a1906119fd565b80156105ee5780601f106105c3576101008083540402835291602001916105ee565b820191906000526020600020905b8154815290600101906020018083116105d157829003601f168201915b5050505050905090565b60008061060361079d565b905060006106118286610692565b905083811015610656576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161064d9061188d565b60405180910390fd5b61066382868684036107a5565b60019250505092915050565b60008061067a61079d565b90506106878185856109fc565b600191505092915050565b6000600160008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054905092915050565b610721610f99565b600073ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff161415610791576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016107889061178d565b60405180910390fd5b61079a81611017565b50565b600033905090565b600073ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff161415610815576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161080c9061186d565b60405180910390fd5b600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff161415610885576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161087c906117ad565b60405180910390fd5b80600160008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055508173ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff167f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b9258360405161096391906118cd565b60405180910390a3505050565b600061097c8484610692565b90507fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff81146109f657818110156109e8576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016109df906117cd565b60405180910390fd5b6109f584848484036107a5565b5b50505050565b600073ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff161415610a6c576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610a639061184d565b60405180910390fd5b600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff161415610adc576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610ad39061174d565b60405180910390fd5b610ae78383836110dd565b60008060008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054905081811015610b6d576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610b64906117ed565b60405180910390fd5b8181036000808673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002081905550816000808573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825401925050819055508273ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef84604051610c5b91906118cd565b60405180910390a3610c6e8484846110e2565b50505050565b600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff161415610ce4576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610cdb906118ad565b60405180910390fd5b610cf0600083836110dd565b8060026000828254610d02919061191f565b92505081905550806000808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825401925050819055508173ffffffffffffffffffffffffffffffffffffffff16600073ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef83604051610db391906118cd565b60405180910390a3610dc7600083836110e2565b5050565b600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff161415610e3b576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610e329061182d565b60405180910390fd5b610e47826000836110dd565b60008060008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054905081811015610ecd576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610ec49061176d565b60405180910390fd5b8181036000808573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000208190555081600260008282540392505081905550600073ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef84604051610f8091906118cd565b60405180910390a3610f94836000846110e2565b505050565b610fa161079d565b73ffffffffffffffffffffffffffffffffffffffff16610fbf61053c565b73ffffffffffffffffffffffffffffffffffffffff1614611015576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161100c9061180d565b60405180910390fd5b565b6000600560009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16905081600560006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055508173ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff167f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e060405160405180910390a35050565b505050565b505050565b6000813590506110f681611a9e565b92915050565b60008135905061110b81611ab5565b92915050565b60006020828403121561112357600080fd5b6000611131848285016110e7565b91505092915050565b6000806040838503121561114d57600080fd5b600061115b858286016110e7565b925050602061116c858286016110e7565b9150509250929050565b60008060006060848603121561118b57600080fd5b6000611199868287016110e7565b93505060206111aa868287016110e7565b92505060406111bb868287016110fc565b9150509250925092565b600080604083850312156111d857600080fd5b60006111e6858286016110e7565b92505060206111f7858286016110fc565b9150509250929050565b60006020828403121561121357600080fd5b6000611221848285016110fc565b91505092915050565b61123381611975565b82525050565b61124281611987565b82525050565b600061125382611903565b61125d818561190e565b935061126d8185602086016119ca565b61127681611a8d565b840191505092915050565b600061128e60238361190e565b91507f45524332303a207472616e7366657220746f20746865207a65726f206164647260008301527f65737300000000000000000000000000000000000000000000000000000000006020830152604082019050919050565b60006112f460228361190e565b91507f45524332303a206275726e20616d6f756e7420657863656564732062616c616e60008301527f63650000000000000000000000000000000000000000000000000000000000006020830152604082019050919050565b600061135a60268361190e565b91507f4f776e61626c653a206e6577206f776e657220697320746865207a65726f206160008301527f64647265737300000000000000000000000000000000000000000000000000006020830152604082019050919050565b60006113c060228361190e565b91507f45524332303a20617070726f766520746f20746865207a65726f20616464726560008301527f73730000000000000000000000000000000000000000000000000000000000006020830152604082019050919050565b6000611426601d8361190e565b91507f45524332303a20696e73756666696369656e7420616c6c6f77616e63650000006000830152602082019050919050565b600061146660268361190e565b91507f45524332303a207472616e7366657220616d6f756e742065786365656473206260008301527f616c616e636500000000000000000000000000000000000000000000000000006020830152604082019050919050565b60006114cc60208361190e565b91507f4f776e61626c653a2063616c6c6572206973206e6f7420746865206f776e65726000830152602082019050919050565b600061150c60218361190e565b91507f45524332303a206275726e2066726f6d20746865207a65726f2061646472657360008301527f73000000000000000000000000000000000000000000000000000000000000006020830152604082019050919050565b600061157260258361190e565b91507f45524332303a207472616e736665722066726f6d20746865207a65726f20616460008301527f64726573730000000000000000000000000000000000000000000000000000006020830152604082019050919050565b60006115d860248361190e565b91507f45524332303a20617070726f76652066726f6d20746865207a65726f2061646460008301527f72657373000000000000000000000000000000000000000000000000000000006020830152604082019050919050565b600061163e60258361190e565b91507f45524332303a2064656372656173656420616c6c6f77616e63652062656c6f7760008301527f207a65726f0000000000000000000000000000000000000000000000000000006020830152604082019050919050565b60006116a4601f8361190e565b91507f45524332303a206d696e7420746f20746865207a65726f2061646472657373006000830152602082019050919050565b6116e0816119b3565b82525050565b6116ef816119bd565b82525050565b600060208201905061170a600083018461122a565b92915050565b60006020820190506117256000830184611239565b92915050565b600060208201905081810360008301526117458184611248565b905092915050565b6000602082019050818103600083015261176681611281565b9050919050565b60006020820190508181036000830152611786816112e7565b9050919050565b600060208201905081810360008301526117a68161134d565b9050919050565b600060208201905081810360008301526117c6816113b3565b9050919050565b600060208201905081810360008301526117e681611419565b9050919050565b6000602082019050818103600083015261180681611459565b9050919050565b60006020820190508181036000830152611826816114bf565b9050919050565b60006020820190508181036000830152611846816114ff565b9050919050565b6000602082019050818103600083015261186681611565565b9050919050565b60006020820190508181036000830152611886816115cb565b9050919050565b600060208201905081810360008301526118a681611631565b9050919050565b600060208201905081810360008301526118c681611697565b9050919050565b60006020820190506118e260008301846116d7565b92915050565b60006020820190506118fd60008301846116e6565b92915050565b600081519050919050565b600082825260208201905092915050565b600061192a826119b3565b9150611935836119b3565b9250827fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0382111561196a57611969611a2f565b5b828201905092915050565b600061198082611993565b9050919050565b60008115159050919050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b6000819050919050565b600060ff82169050919050565b60005b838110156119e85780820151818401526020810190506119cd565b838111156119f7576000848401525b50505050565b60006002820490506001821680611a1557607f821691505b60208210811415611a2957611a28611a5e565b5b50919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b6000601f19601f8301169050919050565b611aa781611975565b8114611ab257600080fd5b50565b611abe816119b3565b8114611ac957600080fd5b5056fea2646970667358221220d8232e5686cdc879a90d7ddc96a6201a16df458937fe021864cb22c9c7b392dc64736f6c63430008000033c001a06973bf638baba2b3f46e78e46303e8460ae185a5bc99f7dcba6efdefdf095871a0623b4531e28bb7361ae1aa9a86a3c82502949770077340098991632ac44c595f").unwrap();
	pub MetaDeploymentSignature: Vec<u8> = hex::decode("81d1647ef7fef33fb4f54ec87c57af771994b7709b9f88d0264d3673ae9d870f10084ca26a0bfb1c787b8b47d59a8ec3cdf54976eddd6ca9e6883b5267c0e4971b").unwrap();
	pub RawTransaction0: Vec<u8> = hex::decode("02f86f840133edcc808459682f0084a0eebb0083200b2094f0a57f274781b0ed17f2ae5f1709a0c360cbe4898080c001a0fe83852a38f766aafc7cc0fdffd2728c16f6dc1f10578d75eee885e9dc4016f1a01e000f6d5d275993612014ade48a8666fd23b747a099c0a623be7bdf11e1c3be").unwrap();
	// pub RawTransaction1: Vec<u8> = hex::decode("f86b01843b9aca0082520894f0a57f274781b0ed17f2ae5f1709a0c360cbe489880de0b6b3a7640000801ca029343cf22be72d879c6392a2ff5dd7e3675b6b70ffc7b7a15529ed9bdad33668a04d5de0209d299c37af1642ea17ca43fdcf0b07d4aa44fe87e338b4fe9b2fa995").unwrap();
	pub MetaTransaction0Signature: Vec<u8> = hex::decode("3171eaa7a85b9e089690bd7a2438ac99c0cd17fca959aefc7a2109371f66b7a161a62c28795e12e794bf5551ca21828e97becf89502fef18c5d785f8c3846b8e1c").unwrap();
	// pub MetaTransaction1Signature: Vec<u8> = hex::decode("fb0482cfa593aa03aaa1d7c2e5a07269d65a4a589b14c109de8d8ca0c268efca198d1d898e7022002e34d91ced41deda8ae526856a8e23d100ab92e5dbb948c31b").unwrap();
	pub Sponsor: H160 = H160::from_str("0xaf537bd156c7e548d0bf2cd43168dabf7af2feb5").unwrap();
}

pub fn new_test_ext() -> sp_io::TestExternalities {
	let mut t = frame_system::GenesisConfig::default()
		.build_storage::<Runtime>()
		.unwrap();
	let config = pallet_evm::GenesisConfig {
		accounts: BTreeMap::new(),
	};
	<pallet_evm::GenesisConfig as GenesisBuild<Runtime>>::assimilate_storage(&config, &mut t)
		.unwrap();

	let eth_config: pallet_ethereum::GenesisConfig = Default::default();

	<pallet_ethereum::GenesisConfig as GenesisBuild<Runtime>>::assimilate_storage(
		&eth_config,
		&mut t,
	)
	.unwrap();

	t.into()
}
