contract_extrinsics/pallet_contracts_primitives.rs
1// Copyright (C) Use Ink (UK) Ltd.
2// This file is part of cargo-contract.
3//
4// cargo-contract is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// cargo-contract is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with cargo-contract. If not, see <http://www.gnu.org/licenses/>.
16
17use pallet_contracts_uapi::ReturnFlags;
18use scale::{
19 Decode,
20 Encode,
21 MaxEncodedLen,
22};
23use scale_info::TypeInfo;
24use sp_runtime::{
25 DispatchError,
26 RuntimeDebug,
27};
28use sp_weights::Weight;
29
30// A copy of primitive types defined within `pallet_contracts`, required for RPC calls.
31
32/// Result type of a `bare_call` or `bare_instantiate` call as well as
33/// `ContractsApi::call` and `ContractsApi::instantiate`.
34///
35/// It contains the execution result together with some auxiliary information.
36///
37/// #Note
38///
39/// It has been extended to include `events` at the end of the struct while not bumping
40/// the `ContractsApi` version. Therefore when SCALE decoding a `ContractResult` its
41/// trailing data should be ignored to avoid any potential compatibility issues.
42#[derive(Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug, TypeInfo)]
43pub struct ContractResult<R, Balance> {
44 /// How much weight was consumed during execution.
45 pub gas_consumed: Weight,
46 /// How much weight is required as gas limit in order to execute this call.
47 ///
48 /// This value should be used to determine the weight limit for on-chain execution.
49 ///
50 /// # Note
51 ///
52 /// This can only different from [`Self::gas_consumed`] when weight pre charging
53 /// is used. Currently, only `seal_call_runtime` makes use of pre charging.
54 /// Additionally, any `seal_call` or `seal_instantiate` makes use of pre-charging
55 /// when a non-zero `gas_limit` argument is supplied.
56 pub gas_required: Weight,
57 /// How much balance was paid by the origin into the contract's deposit account in
58 /// order to pay for storage.
59 ///
60 /// The storage deposit is never actually charged from the origin in case of
61 /// [`Self::result`] is `Err`. This is because on error all storage changes are
62 /// rolled back including the payment of the deposit.
63 pub storage_deposit: StorageDeposit<Balance>,
64 /// An optional debug message. This message is only filled when explicitly requested
65 /// by the code that calls into the contract. Otherwise it is empty.
66 ///
67 /// The contained bytes are valid UTF-8. This is not declared as `String` because
68 /// this type is not allowed within the runtime.
69 ///
70 /// Clients should not make any assumptions about the format of the buffer.
71 /// They should just display it as-is. It is **not** only a collection of log lines
72 /// provided by a contract but a formatted buffer with different sections.
73 ///
74 /// # Note
75 ///
76 /// The debug message is never generated during on-chain execution. It is reserved
77 /// for RPC calls.
78 pub debug_message: Vec<u8>,
79 /// The execution result of the wasm code.
80 pub result: R,
81}
82
83/// Result type of a `bare_call` call as well as `ContractsApi::call`.
84pub type ContractExecResult<Balance> =
85 ContractResult<Result<ExecReturnValue, DispatchError>, Balance>;
86
87/// Result type of a `bare_instantiate` call as well as `ContractsApi::instantiate`.
88pub type ContractInstantiateResult<AccountId, Balance> =
89 ContractResult<Result<InstantiateReturnValue<AccountId>, DispatchError>, Balance>;
90
91/// Result type of a `bare_code_upload` call.
92pub type CodeUploadResult<CodeHash, Balance> =
93 Result<CodeUploadReturnValue<CodeHash, Balance>, DispatchError>;
94
95/// Result type of a `get_storage` call.
96pub type GetStorageResult = Result<Option<Vec<u8>>, ContractAccessError>;
97
98/// The possible errors that can happen querying the storage of a contract.
99#[derive(
100 Copy, Clone, Eq, PartialEq, Encode, Decode, MaxEncodedLen, RuntimeDebug, TypeInfo,
101)]
102pub enum ContractAccessError {
103 /// The given address doesn't point to a contract.
104 DoesntExist,
105 /// Storage key cannot be decoded from the provided input data.
106 KeyDecodingFailed,
107 /// Storage is migrating. Try again later.
108 MigrationInProgress,
109}
110
111/// Output of a contract call or instantiation which ran to completion.
112#[derive(Clone, PartialEq, Eq, Encode, Decode, RuntimeDebug, TypeInfo)]
113pub struct ExecReturnValue {
114 /// Flags passed along by `seal_return`. Empty when `seal_return` was never called.
115 pub flags: ReturnFlags,
116 /// Buffer passed along by `seal_return`. Empty when `seal_return` was never called.
117 pub data: Vec<u8>,
118}
119
120impl ExecReturnValue {
121 /// The contract did revert all storage changes.
122 pub fn did_revert(&self) -> bool {
123 self.flags.contains(ReturnFlags::REVERT)
124 }
125}
126
127/// The result of a successful contract instantiation.
128#[derive(Clone, PartialEq, Eq, Encode, Decode, RuntimeDebug, TypeInfo)]
129pub struct InstantiateReturnValue<AccountId> {
130 /// The output of the called constructor.
131 pub result: ExecReturnValue,
132 /// The account id of the new contract.
133 pub account_id: AccountId,
134}
135
136/// The result of successfully uploading a contract.
137#[derive(Clone, PartialEq, Eq, Encode, Decode, MaxEncodedLen, RuntimeDebug, TypeInfo)]
138pub struct CodeUploadReturnValue<CodeHash, Balance> {
139 /// The key under which the new code is stored.
140 pub code_hash: CodeHash,
141 /// The deposit that was reserved at the caller. Is zero when the code already
142 /// existed.
143 pub deposit: Balance,
144}
145
146/// Reference to an existing code hash or a new wasm module.
147#[derive(Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug, TypeInfo)]
148pub enum Code<Hash> {
149 /// A wasm module as raw bytes.
150 Upload(Vec<u8>),
151 /// The code hash of an on-chain wasm blob.
152 Existing(Hash),
153}
154
155/// The amount of balance that was either charged or refunded in order to pay for storage.
156#[derive(
157 Clone,
158 Eq,
159 PartialEq,
160 Ord,
161 PartialOrd,
162 Encode,
163 Decode,
164 MaxEncodedLen,
165 RuntimeDebug,
166 TypeInfo,
167 serde::Serialize,
168)]
169pub enum StorageDeposit<Balance> {
170 /// The transaction reduced storage consumption.
171 ///
172 /// This means that the specified amount of balance was transferred from the involved
173 /// deposit accounts to the origin.
174 Refund(Balance),
175 /// The transaction increased storage consumption.
176 ///
177 /// This means that the specified amount of balance was transferred from the origin
178 /// to the involved deposit accounts.
179 Charge(Balance),
180}