[core] add initial call builder implementation

parent ecd609d5
......@@ -17,17 +17,20 @@
use super::ContractEnvStorage;
use crate::{
env::{
CallError,
traits::{
Env,
EnvTypes,
},
CallError,
EnvStorage as _,
},
memory::vec::Vec,
storage::Key,
};
use scale::{Encode, Decode};
use scale::{
Decode,
Encode,
};
/// Stores the given value under the specified key in the contract storage.
///
......
......@@ -45,6 +45,12 @@ pub use self::srml::{
DefaultSrmlTypes,
};
#[cfg(not(feature = "test-env"))]
pub use self::srml::{
CallAbi,
CallBuilder,
};
/// The storage environment implementation that is currently being used.
///
/// This may be either
......
......@@ -27,6 +27,8 @@ pub use self::types::{
#[cfg(not(feature = "test-env"))]
pub use self::srml_only::{
sys,
CallAbi,
CallBuilder,
SrmlEnv,
SrmlEnvStorage,
};
// Copyright 2018-2019 Parity Technologies (UK) Ltd.
// This file is part of ink!.
//
// ink! is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ink! is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with ink!. If not, see <http://www.gnu.org/licenses/>.
use crate::env::{
self,
CallError,
Env,
EnvTypes,
};
use core::marker::PhantomData;
use scale::Decode;
/// Consists of the input data to a call.
/// The first four bytes are the function selector and the rest are SCALE encoded inputs.
pub struct CallAbi {
/// Already encoded function selector and inputs.
raw: Vec<u8>,
}
impl CallAbi {
/// Creates new call ABI data for the given selector.
pub fn new(selector: u32) -> Self {
let bytes = selector.to_le_bytes();
Self {
raw: vec![bytes[0], bytes[1], bytes[2], bytes[3]],
}
}
/// Pushes the given argument onto the call ABI data in encoded form.
pub fn push_arg<A>(self, arg: &A) -> Self
where
A: scale::Encode,
{
let mut this = self;
this.raw.extend(&arg.encode());
this
}
/// Returns the underlying byte representation.
pub fn to_bytes(&self) -> &[u8] {
&self.raw
}
}
/// Represents a return type.
///
/// Used as a marker type to differentiate at compile-time between invoke and evaluate.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct ReturnType<T>(PhantomData<T>);
/// Builds up a call.
pub struct CallBuilder<E, R>
where
E: EnvTypes,
{
/// The account ID of the to-be-called smart contract.
account_id: E::AccountId,
/// The maximum gas costs allowed for the call.
gas_cost: u64,
/// The transferred value for the call.
value: E::Balance,
/// The expected return type.
return_type: PhantomData<ReturnType<R>>,
/// The already encoded call ABI data.
raw_input: CallAbi,
}
impl<E, R> CallBuilder<E, ReturnType<R>>
where
E: EnvTypes,
E::Balance: Default,
{
/// Instantiates an evaluatable (returns data) remote smart contract call.
pub fn eval(account_id: E::AccountId, selector: u32) -> Self {
Self {
account_id,
gas_cost: 0,
value: E::Balance::default(),
return_type: PhantomData,
raw_input: CallAbi::new(selector),
}
}
}
impl<E> CallBuilder<E, ()>
where
E: EnvTypes,
E::Balance: Default,
{
/// Instantiates a non-evaluatable (returns no data) remote smart contract call.
pub fn invoke(account_id: E::AccountId, selector: u32) -> Self {
Self {
account_id,
gas_cost: 0,
value: E::Balance::default(),
return_type: PhantomData,
raw_input: CallAbi::new(selector),
}
}
}
impl<E, R> CallBuilder<E, ReturnType<R>>
where
E: Env,
R: Decode,
{
/// Fires the call to the remote smart contract.
/// Returns the returned data back to the caller.
pub fn fire(self) -> Result<R, CallError> {
env::call_evaluate::<E, R>(
self.account_id,
self.gas_cost,
self.value,
self.raw_input.to_bytes(),
)
}
}
impl<E> CallBuilder<E, ()>
where
E: Env,
{
/// Fires the call to the remote smart contract.
pub fn fire(self) -> Result<(), CallError> {
env::call_invoke::<E>(
self.account_id,
self.gas_cost,
self.value,
self.raw_input.to_bytes(),
)
}
}
......@@ -14,10 +14,17 @@
// You should have received a copy of the GNU General Public License
// along with ink!. If not, see <http://www.gnu.org/licenses/>.
mod calls;
mod impls;
pub mod sys;
pub use self::impls::{
SrmlEnv,
SrmlEnvStorage,
pub use self::{
calls::{
CallAbi,
CallBuilder,
},
impls::{
SrmlEnv,
SrmlEnvStorage,
},
};
......@@ -35,6 +35,7 @@ use type_metadata::Metadata;
/// # Note
///
/// This is currently just a placeholder for potential future error codes.
#[derive(Debug, Copy, Clone)]
pub struct CallError;
/// The SRML fundamental types.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment