Skip to content

Instantly share code, notes, and snippets.

@tomusdrw
Created August 3, 2020 13:21
Show Gist options
  • Save tomusdrw/22a543df14310801eaa96cf90fc5eb88 to your computer and use it in GitHub Desktop.
Save tomusdrw/22a543df14310801eaa96cf90fc5eb88 to your computer and use it in GitHub Desktop.
#[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