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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
//! Convenience utilities for tests.
use ethcontract::{Account, Address, PrivateKey};
/// Generate public address by hashing the given string.
///
/// # Safety
///
/// This function is intended for tests and should not be used in production.
///
/// # Examples
///
/// ```
/// # use ethcontract_mock::utils::address_for;
/// let address = address_for("Alice");
/// # assert_eq!(address, "0xbf0b5a4099f0bf6c8bc4252ebec548bae95602ea".parse().unwrap());
/// ```
pub fn address_for(who: &str) -> Address {
account_for(who).address()
}
/// Shortcut for [`address_for`]`("Alice")`.
///
/// # Examples
///
/// ```
/// # use ethcontract_mock::utils::address;
/// let address = address();
/// # assert_eq!(address, "0xbf0b5a4099f0bf6c8bc4252ebec548bae95602ea".parse().unwrap());
/// ```
pub fn address() -> Address {
address_for("Alice")
}
/// Generate a private key by hashing the given string.
///
/// # Safety
///
/// This function is intended for tests and should not be used in production.
///
/// # Examples
///
/// ```
/// # use ethcontract_mock::utils::account_for;
/// let account = account_for("Bob");
/// # assert_eq!(account.address(), "0x4dba461ca9342f4a6cf942abd7eacf8ae259108c".parse().unwrap());
/// ```
pub fn account_for(who: &str) -> Account {
use ethcontract::web3::signing::keccak256;
Account::Offline(
PrivateKey::from_raw(keccak256(who.as_bytes())).unwrap(),
None,
)
}
/// Shortcut for [`account_for`]`("Alice")`.
///
/// # Examples
///
/// ```
/// # use ethcontract_mock::utils::account;
/// let account = account();
/// # assert_eq!(account.address(), "0xbf0b5a4099f0bf6c8bc4252ebec548bae95602ea".parse().unwrap());
/// ```
pub fn account() -> Account {
account_for("Alice")
}
/// Deploy a mocked version of a generated contract.
///
/// # Parameters
///
/// - `mock`: a [Mock] instance.
/// - `contract` type of the contract.
///
/// # Examples
///
/// ```
/// # use ethcontract_mock::{Mock, mock_contract};
/// # ethcontract::contract!(
/// # "../examples/truffle/build/contracts/IERC20.json",
/// # contract = IERC20 as ERC20,
/// # );
/// # fn main() {
/// let mock = Mock::new(1234);
/// let (contract, instance) = mock_contract!(mock, ERC20);
/// # }
/// ```
///
/// [Mock]: crate::Mock
#[macro_export]
macro_rules! mock_contract {
($mock:ident, $contract:ident) => {{
let mock = $mock;
let contract = mock.deploy($contract::raw_contract().abi.clone());
let instance = $contract::at(&contract.web3(), contract.address());
(contract, instance)
}};
}