traits.rs 4.53 KB
Newer Older
1
// Copyright 2018-2019 Parity Technologies (UK) Ltd.
2
// This file is part of ink!.
3
//
4
// ink! is free software: you can redistribute it and/or modify
5
6
7
8
// 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.
//
9
// ink! is distributed in the hope that it will be useful,
10
11
12
13
14
// 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
15
// along with ink!.  If not, see <http://www.gnu.org/licenses/>.
16

17
18
19
20
21
22
use crate::{
    memory::vec::Vec,
    storage::Key,
};
use parity_codec::Codec;

Andrew Jones's avatar
Andrew Jones committed
23
24
#[cfg(not(feature = "test-env"))]
/// The environmental types usable by contracts defined with ink!.
25
26
pub trait EnvTypes {
    /// The type of an address.
Andrew Jones's avatar
Andrew Jones committed
27
    type AccountId: Codec + Clone + PartialEq + Eq;
28
    /// The type of balances.
Andrew Jones's avatar
Andrew Jones committed
29
    type Balance: Codec + Clone + PartialEq + Eq;
30
    /// The type of hash.
Andrew Jones's avatar
Andrew Jones committed
31
    type Hash: Codec + Clone + PartialEq + Eq;
32
    /// The type of timestamps.
Andrew Jones's avatar
Andrew Jones committed
33
    type Moment: Codec + Clone + PartialEq + Eq;
34
35
    /// The type of block number.
    type BlockNumber: Codec + Clone + PartialEq + Eq;
Andrew Jones's avatar
Andrew Jones committed
36
37
38
39
40
41
42
43
44
45
46
47
48
}

#[cfg(feature = "test-env")]
/// The environmental types usable by contracts defined with ink!.
pub trait EnvTypes {
    /// The type of an address.
    type AccountId: Codec + Clone + PartialEq + Eq + core::fmt::Debug;
    /// The type of balances.
    type Balance: Codec + Clone + PartialEq + Eq + core::fmt::Debug;
    /// The type of hash.
    type Hash: Codec + Clone + PartialEq + Eq + core::fmt::Debug;
    /// The type of timestamps.
    type Moment: Codec + Clone + PartialEq + Eq + core::fmt::Debug;
49
50
    /// The type of block number.
    type BlockNumber: Codec + Clone + PartialEq + Eq + core::fmt::Debug;
51
52
53
54
55
}

/// Types implementing this can act as contract storage.
pub trait EnvStorage {
    /// Stores the given value under the given key.
56
57
58
    ///
    /// # Safety
    ///
59
    /// This operation is unsafe because it does not check for key integrity.
60
    /// Users can compare this operation with a raw pointer dereferencing in Rust.
61
62
63
    unsafe fn store(key: Key, value: &[u8]);

    /// Clears the value stored under the given key.
64
65
66
    ///
    /// # Safety
    ///
67
    /// This operation is unsafe because it does not check for key integrity.
68
    /// Users can compare this operation with a raw pointer dereferencing in Rust.
69
70
71
    unsafe fn clear(key: Key);

    /// Loads data stored under the given key.
72
73
74
    ///
    /// # Safety
    ///
75
    /// This operation is unsafe because it does not check for key integrity.
76
    /// Users can compare this operation with a raw pointer dereferencing in Rust.
77
78
79
    unsafe fn load(key: Key) -> Option<Vec<u8>>;
}

80
/// The environment API usable by contracts defined with pDSL.
Andrew Jones's avatar
Andrew Jones committed
81
pub trait Env: EnvTypes {
82
83
84
85
86
87
    /// Returns the chain address of the contract.
    fn address() -> <Self as EnvTypes>::AccountId;

    /// Returns the chain balance of the contract.
    fn balance() -> <Self as EnvTypes>::Balance;

88
    /// Returns the chain address of the caller.
89
    fn caller() -> <Self as EnvTypes>::AccountId;
90
91
92
93

    /// Loads input data for contract execution.
    fn input() -> Vec<u8>;

Shawn Tabrizi's avatar
Shawn Tabrizi committed
94
    /// Get the random seed from the latest block.
95
    fn random_seed() -> <Self as EnvTypes>::Hash;
96

97
98
99
    /// Get the timestamp of the latest block.
    fn now() -> <Self as EnvTypes>::Moment;

100
101
102
    /// Get the block number of the latest block.
    fn current_block() -> <Self as EnvTypes>::BlockNumber;

103
104
105
106
107
108
109
110
111
    /// Returns the current gas price.
    fn gas_price() -> <Self as EnvTypes>::Balance;

    /// Returns the gas left for this contract execution.
    fn gas_left() -> <Self as EnvTypes>::Balance;

    /// Returns the amount of value that has been transferred.
    fn value_transferred() -> <Self as EnvTypes>::Balance;

112
    /// Returns from the contract execution with the given value.
113
114
115
116
117
118
    ///
    /// # Safety
    ///
    /// The external callers rely on the correct type of the encoded
    /// returned value. This API is unsafe because it does not provide
    /// guarantees on its own to always encode the expected type.
119
    unsafe fn r#return(value: &[u8]) -> !;
120
121
122
123
124
125
126

    /// Prints the given content to Substrate output.
    ///
    /// # Note
    ///
    /// Usable only in development (`--dev`) chains.
    fn println(content: &str);
127
128

    /// Deposits raw event data through Contracts module.
129
    fn deposit_raw_event(topics: &[<Self as EnvTypes>::Hash], data: &[u8]);
130
}