Skip to content

Commit

Permalink
[examples] complete basic implementation of dispatching Balances Call
Browse files Browse the repository at this point in the history
  • Loading branch information
ascjones committed Jun 19, 2019
1 parent b8fdacf commit 9f13807
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 23 deletions.
9 changes: 4 additions & 5 deletions core/src/env/api.rs
Expand Up @@ -75,10 +75,9 @@ where
}

/// TODO: [AJ] docs
pub unsafe fn dispatch_call<T, E>(call: <E as EnvTypes>::Call)
where
T: parity_codec::Encode,
E: Env,
pub unsafe fn dispatch_call<T>(call: <T as EnvTypes>::Call)
where
T: Env,
{
E::dispatch_call(&call.encode()[..])
T::dispatch_call(&call.encode()[..])
}
1 change: 1 addition & 0 deletions core/src/env/mod.rs
Expand Up @@ -41,6 +41,7 @@ pub use api::*;
pub use traits::*;

pub use self::srml::DefaultSrmlTypes;
pub use self::srml::calls;

/// The storage environment implementation that is currently being used.
///
Expand Down
3 changes: 1 addition & 2 deletions core/src/env/srml/mod.rs
Expand Up @@ -18,10 +18,9 @@
mod srml_only;

mod types;
mod calls;
pub mod calls;

pub use self::types::DefaultSrmlTypes;
pub use self::calls::Balances;

#[cfg(not(feature = "test-env"))]
pub use self::srml_only::{
Expand Down
9 changes: 7 additions & 2 deletions core/src/env/srml/types.rs
Expand Up @@ -40,8 +40,7 @@ impl EnvTypes for DefaultSrmlTypes {
}

/// The default SRML address index type.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Encode, Decode)]
pub struct AccountIndex(u32);
pub type AccountIndex = u32;

/// The default SRML address type.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Encode, Decode)]
Expand Down Expand Up @@ -95,3 +94,9 @@ pub enum Call {
Balances(super::calls::Balances<DefaultSrmlTypes>),
}

impl From<super::calls::Balances<DefaultSrmlTypes>> for Call {
fn from(balances_call: super::calls::Balances<DefaultSrmlTypes>) -> Call {
Call::Balances(balances_call)
}
}

5 changes: 3 additions & 2 deletions core/src/env/test_env.rs
Expand Up @@ -470,12 +470,13 @@ impl<T> TestEnv<T> where T: EnvTypes {
})
}

pub fn dispatched_calls() -> impl IntoIterator<Item = Vec<u8>> {
pub fn dispatched_calls() -> impl IntoIterator<Item = T::Call> {
TEST_ENV_DATA.with(|test_env| {
test_env
.borrow()
.dispatched_calls()
.map(|call| call.to_vec())
.map(|call| Decode::decode(&mut &call[..])
.expect("Valid encoded Call"))
.collect::<Vec<_>>()
})
}
Expand Down
23 changes: 11 additions & 12 deletions examples/lang/calls/src/lib.rs
Expand Up @@ -17,9 +17,7 @@
#![cfg_attr(not(any(test, feature = "test-env")), no_std)]

use ink_core::{
env::{self, DefaultSrmlTypes},
memory::format,
storage,
env::DefaultSrmlTypes,
};
use ink_lang::contract;

Expand All @@ -36,21 +34,22 @@ contract! {

impl Calls {
/// Dispatches a `transfer` call to the Balances srml module
// pub(external) fn dispatch_transfer(&mut self) {
// env::dispatch_call() // TODO: [AJ] Dispatch Balance::transfer Call
// }
pub(external) fn balance_transfer(&mut self, dest: u32, value: Balance) {
let transfer_call = ink_core::env::calls::Balances::<DefaultSrmlTypes>::transfer(dest, value);
unsafe { ink_core::env::dispatch_call::<ink_core::env::ContractEnv<DefaultSrmlTypes>>(transfer_call.into()) };
}
}
}

#[cfg(test)]
mod tests {
use super::Flipper;
use super::*;

#[test]
fn it_works() {
let mut flipper = Flipper::deploy_mock();
assert_eq!(flipper.get(), false);
flipper.flip();
assert_eq!(flipper.get(), true);
fn dispatches_balances_call() {
let mut calls = Calls::deploy_mock();
assert_eq!(env::dispatched_calls().into_iter().count(), 0);
calls.balance_transfer(1, 10000);
assert_eq!(env::dispatched_calls().into_iter().count(), 1);
}
}

0 comments on commit 9f13807

Please sign in to comment.