calls.rs 4.61 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// 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/>.

17 18 19 20 21 22 23
use crate::{
    env::{
        self,
        CallError,
        Env,
        EnvTypes,
    },
24 25 26 27
    memory::vec::{
        self,
        Vec,
    },
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
};
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),
        }
    }
}

104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
impl<E, R> CallBuilder<E, R>
where
    E: EnvTypes,
{
    /// Sets the maximumly allowed gas costs for the call.
    pub fn gas_cost(self, gas_cost: u64) -> Self {
        let mut this = self;
        this.gas_cost = gas_cost;
        this
    }

    /// Sets the value transferred upon the execution of the call.
    pub fn value(self, value: E::Balance) -> Self {
        let mut this = self;
        this.value = value;
        this
    }

    /// Pushes an argument to the inputs of the call.
    pub fn push_arg<A>(self, arg: &A) -> Self
    where
        A: scale::Encode,
    {
        let mut this = self;
        this.raw_input = this.raw_input.push_arg(arg);
        this
    }
}

133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
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(),
        )
    }
}