Created
August 3, 2020 13:21
-
-
Save tomusdrw/22a543df14310801eaa96cf90fc5eb88 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[tokio::test] | |
fn should_read_state() { | |
// given | |
// This starts a special "test node" in the background. We communicate with the node over RPC. | |
let mut test = test::deterministic(PolkadotRuntime.into()); | |
type Balances = pallet_balances::Module<Runtime>; | |
// This uses ManualSeal consensus (might require conditional compilation of Runtime though :() | |
test.produce_blocks(1); | |
// This is something we want to hide away / simplify | |
let alice = sr25519::Pair::from_string("//Alice".into(), None).unwrap(); | |
let bob = sr25519::Pair::from_string("//Bob".into(), None).unwrap(); | |
let signer = PairSigner::new(alice.clone()); | |
// This runs within current-state context. Allows you to query storage items directly or call Module functions. | |
let alice_balance = test.with_state(|| Balances::free_balance(MultiSigner::from(alice.public()).into_account()))); | |
// RPC client with pallet-specific extensions. Creates and sends a transaction. | |
let client = test.rpc_client(); | |
client | |
.transfer(&signer, &Address::from(MultiSigner::from(bob.public()).into_account()), 8900000000000000) | |
.await | |
.expect("failed to transfer funds"); | |
// Produce blocks (assuming the transaction is included) | |
test.produce_blocks(1); | |
// Read the state again to check that the balances has changed. | |
let new_alice_balance = test.with_state(|| Balances::free_balance(MultiSigner::from(alice.public()).into_account())); | |
// account for fees | |
assert!((alice_balance - new_alice_balance) > 8900000000000000); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment