Skip to content

Commit

Permalink
Call runtime (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
pmikolajczyk41 committed Aug 28, 2023
1 parent c62cc7a commit 4cff120
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
54 changes: 50 additions & 4 deletions drink/src/chain_api.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
//! Basic chain API.

use frame_support::dispatch::Dispatchable;
use frame_support::sp_runtime::DispatchResultWithInfo;
use frame_support::{sp_runtime::AccountId32, traits::tokens::currency::Currency};

use crate::{DrinkResult, Error, Runtime, Sandbox};

/// The runtime call type for a particular runtime.
pub type RuntimeCall<R> = <R as frame_system::Config>::RuntimeCall;

/// Interface for basic chain operations.
pub trait ChainApi {
pub trait ChainApi<R: Runtime> {
/// Return the current height of the chain.
fn current_height(&mut self) -> u64;

Expand Down Expand Up @@ -46,9 +51,21 @@ pub trait ChainApi {
///
/// * `action` - The action to run.
fn dry_run<T>(&mut self, action: impl FnOnce(&mut Self) -> T) -> T;

/// Execute a runtime call (dispatchable).
///
/// # Arguments
///
/// * `call` - The runtime call to execute.
/// * `origin` - The origin of the call.
fn runtime_call(
&mut self,
call: RuntimeCall<R>,
origin: <RuntimeCall<R> as Dispatchable>::RuntimeOrigin,
) -> DispatchResultWithInfo<<RuntimeCall<R> as Dispatchable>::PostInfo>;
}

impl<R: Runtime> ChainApi for Sandbox<R> {
impl<R: Runtime> ChainApi<R> for Sandbox<R> {
fn current_height(&mut self) -> u64 {
self.externalities
.execute_with(|| frame_system::Pallet::<R>::block_number())
Expand Down Expand Up @@ -90,12 +107,20 @@ impl<R: Runtime> ChainApi for Sandbox<R> {

result
}

fn runtime_call(
&mut self,
call: RuntimeCall<R>,
origin: <RuntimeCall<R> as Dispatchable>::RuntimeOrigin,
) -> DispatchResultWithInfo<<RuntimeCall<R> as Dispatchable>::PostInfo> {
self.externalities.execute_with(|| call.dispatch(origin))
}
}

#[cfg(test)]
mod tests {
use crate::chain_api::ChainApi;
use crate::{runtime::MinimalRuntime, Sandbox, DEFAULT_ACTOR};
use crate::chain_api::{ChainApi, RuntimeCall};
use crate::{runtime::MinimalRuntime, AccountId32, Sandbox, DEFAULT_ACTOR};

#[test]
fn dry_run_works() {
Expand All @@ -109,4 +134,25 @@ mod tests {

assert_eq!(sandbox.balance(&DEFAULT_ACTOR), initial_balance);
}

#[test]
fn runtime_call_works() {
let mut sandbox = Sandbox::<MinimalRuntime>::new().expect("Failed to create sandbox");

const RECIPIENT: AccountId32 = AccountId32::new([2u8; 32]);
assert_ne!(DEFAULT_ACTOR, RECIPIENT);
let initial_balance = sandbox.balance(&RECIPIENT);

let call = RuntimeCall::<MinimalRuntime>::Balances(
pallet_balances::Call::<MinimalRuntime>::transfer {
dest: RECIPIENT,
value: 100,
},
);
let result = sandbox.runtime_call(call, Some(DEFAULT_ACTOR).into());
assert!(result.is_ok());

let expected_balance = initial_balance + 100;
assert_eq!(sandbox.balance(&RECIPIENT), expected_balance);
}
}
2 changes: 1 addition & 1 deletion drink/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ impl<R: Runtime> Session<R> {
}

/// Returns a reference for basic chain API.
pub fn chain_api(&mut self) -> &mut impl ChainApi {
pub fn chain_api(&mut self) -> &mut impl ChainApi<R> {
&mut self.sandbox
}

Expand Down

0 comments on commit 4cff120

Please sign in to comment.