1use core::any::Any;
2
3pub mod api;
4pub mod macros;
5
6pub use frame_metadata::RuntimeMetadataPrefixed;
7pub use frame_support::weights::Weight;
8use frame_support::{
9 sp_runtime::traits::Dispatchable,
10 traits::fungible::Inspect,
11};
12use frame_system::{
13 EventRecord,
14 pallet_prelude::{
15 BlockNumberFor,
16 OriginFor,
17 },
18};
19use ink_primitives::U256;
20pub use macros::{
21 BlockBuilder,
22 DefaultSandbox,
23};
24use pallet_revive::{
25 ContractResult,
26 ExecReturnValue,
27 InstantiateReturnValue,
28};
29use sp_core::Get;
30pub use {
32 frame_support::sp_runtime::testing::H256,
33 frame_support::{
34 self,
35 sp_runtime::{
36 AccountId32,
37 DispatchError,
38 },
39 },
40 frame_system,
41 pallet_balances,
42 pallet_revive,
43 pallet_timestamp,
44 paste,
45 sp_core::crypto::Ss58Codec,
46 sp_externalities::{
47 self,
48 Extension,
49 },
50 sp_io::TestExternalities,
51};
52
53#[derive(Clone, Debug)]
55pub struct Snapshot {
56 pub storage: RawStorage,
58 pub storage_root: StorageRoot,
60}
61
62pub type RawStorage = Vec<(Vec<u8>, (Vec<u8>, i32))>;
63pub type StorageRoot = H256;
64
65type BalanceOf<R> =
67 <<R as pallet_revive::Config>::Currency as Inspect<AccountIdFor<R>>>::Balance;
68
69pub type AccountIdFor<R> = <R as frame_system::Config>::AccountId;
71
72pub type RuntimeCall<R> = <R as frame_system::Config>::RuntimeCall;
74
75pub type EventRecordOf<Runtime> = EventRecord<
77 <Runtime as frame_system::Config>::RuntimeEvent,
78 <Runtime as frame_system::Config>::Hash,
79>;
80
81pub type ContractInstantiateResultFor<Runtime> =
83 ContractResult<OriginFor<Runtime>, BalanceOf<Runtime>>;
84
85pub type ContractResultFor<Runtime> = ContractResult<Runtime, BalanceOf<Runtime>>;
86
87pub type ContractResultInstantiate<Runtime> =
88 ContractResult<InstantiateReturnValue, BalanceOf<Runtime>>;
89
90pub type ContractExecResultFor<Runtime> =
92 ContractResult<ExecReturnValue, BalanceOf<Runtime>>;
93
94pub type MapAccountResultFor = Result<(), DispatchError>;
96
97pub type RuntimeOf<S> = <S as Sandbox>::Runtime;
99
100pub type RuntimeEventOf<S> = <RuntimeOf<S> as frame_system::Config>::RuntimeEvent;
102
103pub trait Sandbox {
105 type Runtime: frame_system::Config;
107
108 fn execute_with<T>(&mut self, execute: impl FnOnce() -> T) -> T;
110
111 fn dry_run<T>(&mut self, action: impl FnOnce(&mut Self) -> T) -> T;
113
114 fn register_extension<E: Any + Extension>(&mut self, ext: E);
116
117 fn initialize_block(
119 _height: BlockNumberFor<Self::Runtime>,
120 _parent_hash: <Self::Runtime as frame_system::Config>::Hash,
121 ) {
122 }
123
124 fn finalize_block(
126 _height: BlockNumberFor<Self::Runtime>,
127 ) -> <Self::Runtime as frame_system::Config>::Hash {
128 Default::default()
129 }
130
131 fn default_actor() -> AccountIdFor<Self::Runtime>;
133
134 fn default_gas_limit() -> Weight {
135 Weight::from_parts(100_000_000_000_000, 6 * 1024 * 1024)
136 }
137
138 fn get_metadata() -> RuntimeMetadataPrefixed;
140
141 fn convert_account_to_origin(
143 account: AccountIdFor<Self::Runtime>,
144 ) -> <<Self::Runtime as frame_system::Config>::RuntimeCall as Dispatchable>::RuntimeOrigin;
145
146 fn take_snapshot(&mut self) -> Snapshot;
148
149 fn restore_snapshot(&mut self, snapshot: Snapshot);
151}
152
153pub fn balance_to_evm_value<R>(value: BalanceOf<R>) -> U256
163where
164 R: pallet_revive::Config,
165 BalanceOf<R>: Into<U256>,
166 U256: From<u32>,
167{
168 let native_to_eth_ratio: U256 =
169 <R as pallet_revive::Config>::NativeToEthRatio::get().into();
170 let evm_value: U256 = value.into();
171 native_to_eth_ratio.saturating_mul(evm_value)
172}