ink_env/
lib.rs

1// Copyright (C) Use Ink (UK) Ltd.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! The `ink_env` utilities used to interoperate with the contract executor.
16//!
17//! Mainly provides entities to work on a contract's storage
18//! as well as high-level collections on top of those.
19//! Also provides environmental utilities, such as storage allocators,
20//! FFI to interface with FRAME contracts and a primitive blockchain
21//! emulator for simple off-chain testing.
22
23#![doc(
24    html_logo_url = "https://use.ink/img/crate-docs/logo.png",
25    html_favicon_url = "https://use.ink/crate-docs/favicon.png"
26)]
27#![cfg_attr(not(feature = "std"), no_std)]
28#![cfg_attr(docsrs, feature(doc_cfg))]
29#![deny(
30    missing_docs,
31    bad_style,
32    bare_trait_objects,
33    improper_ctypes,
34    non_shorthand_field_patterns,
35    no_mangle_generic_items,
36    overflowing_literals,
37    path_statements,
38    patterns_in_fns_without_body,
39    unconditional_recursion,
40    unused_allocation,
41    unused_comparisons,
42    unused_parens,
43    while_true,
44    trivial_casts,
45    trivial_numeric_casts,
46    unused_extern_crates
47)]
48
49/// The capacity of the static buffer.
50/// Usually set to 16 kB.
51/// Can be modified by setting `INK_STATIC_BUFFER_SIZE` environmental variable.
52#[const_env::from_env("INK_STATIC_BUFFER_SIZE")]
53pub const BUFFER_SIZE: usize = 16384;
54
55#[cfg(target_arch = "riscv64")]
56#[panic_handler]
57fn panic(info: &core::panic::PanicInfo) -> ! {
58    // todo
59    //#[cfg(any(feature = "ink-debug", feature = "std"))]
60    self::return_value(
61        ReturnFlags::REVERT,
62        &ink_prelude::format!("{}", info.message()).as_bytes(),
63    );
64
65    /*
66    #[cfg(not(any(feature = "ink-debug", feature = "std")))]
67    cfg_if::cfg_if! {
68        if #[cfg(target_arch = "riscv64")] {
69            // Safety: The unimp instruction is guaranteed to trap
70            unsafe {
71                core::arch::asm!("unimp");
72                core::hint::unreachable_unchecked();
73            }
74        } else {
75            core::compile_error!("ink! only supports riscv64");
76        }
77    }
78    */
79}
80
81// This extern crate definition is required since otherwise rustc
82// is not recognizing its allocator and panic handler definitions.
83#[cfg(not(any(feature = "std", feature = "no-allocator")))]
84extern crate ink_allocator;
85
86mod api;
87mod backend;
88pub mod call;
89mod engine;
90mod error;
91#[doc(hidden)]
92pub mod event;
93pub mod hash;
94
95#[cfg(test)]
96mod tests;
97
98#[cfg(any(feature = "std", test, doc))]
99#[doc(inline)]
100pub use self::engine::off_chain::test_api as test;
101
102use self::backend::{
103    EnvBackend,
104    TypedEnvBackend,
105};
106pub use self::{
107    api::*,
108    error::{
109        Error,
110        Result,
111    },
112    event::{
113        Event,
114        TopicEncoder,
115    },
116    types::{
117        AccountIdGuard,
118        Balance,
119        BlockNumber,
120        CodecAsType,
121        DefaultEnvironment,
122        Environment,
123        FromLittleEndian,
124        Gas,
125        Timestamp,
126    },
127};
128pub use ink_primitives::{
129    contract::{
130        ContractEnv,
131        ContractReference,
132        ContractReverseReference,
133    },
134    reflect,
135    reflect::{
136        DecodeDispatch,
137        DispatchError,
138    },
139    types,
140};
141#[doc(inline)]
142pub use pallet_revive_uapi::{
143    CallFlags,
144    ReturnErrorCode,
145    ReturnFlags,
146};
147
148/// A convenience type alias to the marker type representing the "default" ABI for calls.
149///
150/// # Note
151///
152/// The "default" ABI for calls is "ink", unless the ABI is set to "sol"
153/// in the ink! project's manifest file (i.e. `Cargo.toml`).
154#[cfg(not(ink_abi = "sol"))]
155#[doc(hidden)]
156pub type DefaultAbi = ink_primitives::abi::Ink;
157
158/// A convenience type alias to the marker type representing the "default" ABI for calls.
159///
160/// # Note
161///
162/// The "default" ABI for calls is "ink", unless the ABI is set to "sol"
163/// in the ink! project's manifest file (i.e. `Cargo.toml`).
164#[cfg(ink_abi = "sol")]
165#[doc(hidden)]
166pub type DefaultAbi = ink_primitives::abi::Sol;