ink_primitives/types.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 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 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 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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
// Copyright (C) Use Ink (UK) Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use crate::arithmetic::AtLeast32BitUnsigned;
use core::array::TryFromSliceError;
use derive_more::From;
use primitive_types::{
H160,
U256,
};
use scale::{
Decode,
Encode,
MaxEncodedLen,
};
#[cfg(feature = "std")]
use {
scale_decode::DecodeAsType,
scale_encode::EncodeAsType,
scale_info::TypeInfo,
};
/// The default environment `AccountId` type.
///
/// # Note
///
/// This is a mirror of the `AccountId` type used in the default configuration
/// of PALLET contracts.
#[derive(
Debug,
Copy,
Clone,
PartialEq,
Eq,
Ord,
PartialOrd,
Hash,
Decode,
Encode,
MaxEncodedLen,
From,
)]
#[cfg_attr(feature = "std", derive(TypeInfo, DecodeAsType, EncodeAsType))]
pub struct AccountId(pub [u8; 32]);
impl AsRef<[u8; 32]> for AccountId {
#[inline]
fn as_ref(&self) -> &[u8; 32] {
&self.0
}
}
impl AsMut<[u8; 32]> for AccountId {
#[inline]
fn as_mut(&mut self) -> &mut [u8; 32] {
&mut self.0
}
}
impl AsRef<[u8]> for AccountId {
#[inline]
fn as_ref(&self) -> &[u8] {
&self.0[..]
}
}
impl AsMut<[u8]> for AccountId {
#[inline]
fn as_mut(&mut self) -> &mut [u8] {
&mut self.0[..]
}
}
impl<'a> TryFrom<&'a [u8]> for AccountId {
type Error = TryFromSliceError;
fn try_from(bytes: &'a [u8]) -> Result<Self, TryFromSliceError> {
let address = <[u8; 32]>::try_from(bytes)?;
Ok(Self(address))
}
}
/// The default environment `Hash` type.
///
/// # Note
///
/// This is a mirror of the `Hash` type used in the default configuration
/// of PALLET contracts.
#[derive(
Debug,
Copy,
Clone,
PartialEq,
Eq,
Ord,
PartialOrd,
Hash,
Decode,
Encode,
MaxEncodedLen,
From,
Default,
)]
#[cfg_attr(feature = "std", derive(TypeInfo, DecodeAsType, EncodeAsType))]
pub struct Hash([u8; 32]);
impl<'a> TryFrom<&'a [u8]> for Hash {
type Error = TryFromSliceError;
fn try_from(bytes: &'a [u8]) -> Result<Self, TryFromSliceError> {
let hash = <[u8; 32]>::try_from(bytes)?;
Ok(Self(hash))
}
}
impl AsRef<[u8]> for Hash {
fn as_ref(&self) -> &[u8] {
&self.0[..]
}
}
impl AsMut<[u8]> for Hash {
fn as_mut(&mut self) -> &mut [u8] {
&mut self.0[..]
}
}
impl From<Hash> for [u8; 32] {
fn from(hash: Hash) -> Self {
hash.0
}
}
/// The equivalent of `Zero` for hashes.
///
/// A hash that consists only of 0 bits is clear.
pub trait Clear {
/// The clear hash.
const CLEAR_HASH: Self;
/// Returns `true` if the hash is clear.
fn is_clear(&self) -> bool;
}
impl Clear for [u8; 32] {
const CLEAR_HASH: Self = [0x00; 32];
fn is_clear(&self) -> bool {
self == &Self::CLEAR_HASH
}
}
impl Clear for Hash {
const CLEAR_HASH: Self = Self(<[u8; 32] as Clear>::CLEAR_HASH);
fn is_clear(&self) -> bool {
<[u8; 32] as Clear>::is_clear(&self.0)
}
}
// impl Clear for H256 {
// const CLEAR_HASH: Self = H256::CLEAR_HASH;
//
// fn is_clear(&self) -> bool {
// self.as_bytes().iter().all(|&byte| byte == 0x00)
// }
// }
#[derive(Debug, Clone, Eq, PartialEq, Encode, Decode)]
#[cfg_attr(
feature = "std",
derive(
scale_info::TypeInfo,
EncodeAsType,
serde::Serialize,
serde::Deserialize
)
)]
pub enum DepositLimit<Balance> {
/// Allows bypassing all balance transfer checks.
Unchecked,
/// Specifies a maximum allowable balance for a deposit.
Balance(Balance),
}
impl<T> From<T> for DepositLimit<T> {
fn from(value: T) -> Self {
Self::Balance(value)
}
}
/// Allows to instantiate a type from its little-endian bytes representation.
pub trait FromLittleEndian {
/// The little-endian bytes representation.
type Bytes: Default + AsRef<[u8]> + AsMut<[u8]>;
/// Create a new instance from the little-endian bytes representation.
fn from_le_bytes(bytes: Self::Bytes) -> Self;
}
impl FromLittleEndian for u8 {
type Bytes = [u8; 1];
#[inline]
fn from_le_bytes(bytes: Self::Bytes) -> Self {
u8::from_le_bytes(bytes)
}
}
impl FromLittleEndian for u16 {
type Bytes = [u8; 2];
#[inline]
fn from_le_bytes(bytes: Self::Bytes) -> Self {
u16::from_le_bytes(bytes)
}
}
impl FromLittleEndian for u32 {
type Bytes = [u8; 4];
#[inline]
fn from_le_bytes(bytes: Self::Bytes) -> Self {
u32::from_le_bytes(bytes)
}
}
impl FromLittleEndian for u64 {
type Bytes = [u8; 8];
#[inline]
fn from_le_bytes(bytes: Self::Bytes) -> Self {
u64::from_le_bytes(bytes)
}
}
impl FromLittleEndian for u128 {
type Bytes = [u8; 16];
#[inline]
fn from_le_bytes(bytes: Self::Bytes) -> Self {
u128::from_le_bytes(bytes)
}
}
impl FromLittleEndian for U256 {
type Bytes = [u8; 32];
#[inline]
fn from_le_bytes(bytes: Self::Bytes) -> Self {
U256::from_little_endian(&bytes)
//U256::from_le_bytes(bytes)
}
}
/// todo remove
/// A trait to enforce that a type should be an [`Environment::AccountId`].
///
/// If you have an [`Environment`] which uses an [`Environment::AccountId`] type other
/// than the ink! provided [`AccountId`](https://docs.rs/ink_primitives/latest/ink_primitives/struct.AccountId.html)
/// you will need to implement this trait for your [`Environment::AccountId`] concrete
/// type.
pub trait AccountIdGuard {}
/// The ink! provided [`AccountId`](https://docs.rs/ink_primitives/latest/ink_primitives/struct.AccountId.html)
/// used in the [`DefaultEnvironment`].
impl AccountIdGuard for AccountId {}
impl AccountIdGuard for H160 {}
cfg_if::cfg_if! {
if #[cfg(feature = "std")] {
pub trait CodecAsType: scale_decode::DecodeAsType + scale_encode::EncodeAsType {}
impl<T: scale_decode::DecodeAsType + scale_encode::EncodeAsType> CodecAsType for T {}
} else {
pub trait CodecAsType {}
impl<T> CodecAsType for T {}
}
}
/// The environmental types usable by contracts defined with ink!.
pub trait Environment: Clone {
/// The maximum number of supported event topics provided by the runtime.
///
/// The value must match the maximum number of supported event topics of the used
/// runtime.
const MAX_EVENT_TOPICS: usize;
/// The account id type.
type AccountId: 'static
+ scale::Codec
+ scale::MaxEncodedLen
+ CodecAsType
+ Clone
+ PartialEq
+ Eq
+ Ord
+ AsRef<[u8]>
+ AsMut<[u8]>;
/// The type of balances.
type Balance: 'static
+ scale::Codec
+ CodecAsType
+ Copy
+ Clone
+ PartialEq
+ Eq
+ AtLeast32BitUnsigned
+ FromLittleEndian;
/// The type of hash.
type Hash: 'static
+ scale::Codec
+ scale::MaxEncodedLen
+ CodecAsType
+ Copy
+ Clone
+ Clear
+ PartialEq
+ Eq
+ Ord
+ AsRef<[u8]>
+ AsMut<[u8]>;
/// The type of a timestamp.
type Timestamp: 'static
+ scale::Codec
+ CodecAsType
+ Copy
+ Clone
+ PartialEq
+ Eq
+ AtLeast32BitUnsigned
+ FromLittleEndian;
/// The type of block number.
type BlockNumber: 'static
+ scale::Codec
+ CodecAsType
+ Copy
+ Clone
+ PartialEq
+ Eq
+ AtLeast32BitUnsigned
+ FromLittleEndian;
/// The chain extension for the environment.
///
/// This is a type that is defined through the `#[ink::chain_extension]` procedural
/// macro. For more information about usage and definition click
/// [this][chain_extension] link.
///
/// [chain_extension]: https://use-ink.github.io/ink/ink/attr.chain_extension.html
type ChainExtension;
/// TODO comment
type EventRecord: 'static + scale::Codec;
}
/// Placeholder for chains that have no defined chain extension.
#[cfg_attr(feature = "std", derive(TypeInfo))]
pub enum NoChainExtension {}
/// The fundamental types of the default configuration.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "std", derive(TypeInfo))]
pub enum DefaultEnvironment {}
impl Environment for DefaultEnvironment {
const MAX_EVENT_TOPICS: usize = 4;
type AccountId = AccountId;
type Balance = Balance;
type Hash = Hash;
type Timestamp = Timestamp;
type BlockNumber = BlockNumber;
type ChainExtension = NoChainExtension;
type EventRecord = EventRecord;
}
/// The default balance type.
pub type Balance = u128;
/// The default timestamp type.
pub type Timestamp = u64;
/// The default gas type.
pub type Gas = u64;
/// The default block number type.
pub type BlockNumber = u32;
// todo replace with ()
#[derive(Encode, Decode, MaxEncodedLen, Debug)]
pub struct RuntimeEvent();
/// The default event record type.
pub type EventRecord = EventRecordFoo<RuntimeEvent, Hash>;
#[derive(Encode, Decode, Debug)]
#[cfg_attr(feature = "std", derive(TypeInfo))]
pub struct EventRecordFoo<E, H> {
/// The phase of the block it happened in.
pub phase: Phase,
/// The event itself.
pub event: E,
/// The list of the topics this event has.
pub topics: ink_prelude::vec::Vec<H>,
}
/// A phase of a block's execution.
#[derive(Debug, Encode, Decode, MaxEncodedLen)]
#[cfg_attr(feature = "std", derive(PartialEq, Eq, Clone, TypeInfo))]
pub enum Phase {
/// Applying an extrinsic.
ApplyExtrinsic(u32),
/// Finalizing the block.
Finalization,
/// Initializing the block.
Initialization,
}
/// The type of origins supported by `pallet-revive`.
#[derive(Clone, ::scale::Encode, ::scale::Decode, PartialEq)]
#[cfg_attr(feature = "std", derive(::scale_info::TypeInfo))]
pub enum Origin<E: Environment> {
Root,
Signed(E::AccountId),
}