Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: integration test storage-types #1762

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
f617f2d
adds storage_types contract from https://github.com/paritytech/ink-ex…
peetzweg Apr 20, 2023
b7c1be0
Merge branch 'master' into integration-test-storage_types
peetzweg Apr 20, 2023
dfc9e5a
renames folder
peetzweg Apr 20, 2023
23b1592
aligns contract name with folder name
peetzweg Apr 20, 2023
c1a2da7
resolves clippy recommendations
peetzweg Apr 21, 2023
b3a1026
correcting imports
peetzweg Apr 21, 2023
e12ab8e
fixes riscv ci pipeline by aligning Cargo.toml with other integration…
peetzweg Apr 21, 2023
c35b47a
returns Option and Result with a value in success cases
peetzweg May 9, 2023
ae1cd94
use nightly formatter
peetzweg May 9, 2023
123c4d4
removes code regarding `Mapping` type. Afaik a mapping can't be retur…
peetzweg May 9, 2023
8911c93
Merge branch 'master' into peetzweg-integration-test-storage_types
peetzweg Jul 6, 2023
43a511c
update storage_types contract to work with latest master changes
peetzweg Jul 6, 2023
251c45f
adds empty enums as well as tuples to test how they are decoded by FE…
peetzweg Nov 16, 2023
261c952
adds storage_types contract from https://github.com/paritytech/ink-ex…
peetzweg Apr 20, 2023
4ff27ce
renames folder
peetzweg Apr 20, 2023
a8c12a1
aligns contract name with folder name
peetzweg Apr 20, 2023
7b11ad3
resolves clippy recommendations
peetzweg Apr 21, 2023
adfbeba
correcting imports
peetzweg Apr 21, 2023
52fb025
fixes riscv ci pipeline by aligning Cargo.toml with other integration…
peetzweg Apr 21, 2023
ef0d43a
returns Option and Result with a value in success cases
peetzweg May 9, 2023
fcef200
use nightly formatter
peetzweg May 9, 2023
4675b1e
removes code regarding `Mapping` type. Afaik a mapping can't be retur…
peetzweg May 9, 2023
13ca08e
update storage_types contract to work with latest master changes
peetzweg Jul 6, 2023
1bfaec2
adds empty enums as well as tuples to test how they are decoded by FE…
peetzweg Nov 16, 2023
4b29f02
replace .to_string with String::from
peetzweg Nov 16, 2023
3abdad4
Merge branch 'peetzweg-integration-test-storage_types' of github.com:…
peetzweg Nov 16, 2023
76d46cc
adds a payable function
peetzweg Nov 22, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 27 additions & 0 deletions integration-tests/storage-types/Cargo.toml
@@ -0,0 +1,27 @@
[package]
name = "storage-types"
version = "1.0.0"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2021"
publish = false

[dependencies]
ink = { path = "../../crates/ink", default-features = false }

scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] }
scale-info = { version = "2.3", default-features = false, features = ["derive"], optional = true }

[dev-dependencies]
ink_e2e = { path = "../../crates/e2e" }

[lib]
path = "lib.rs"

[features]
default = ["std"]
std = [
"ink/std",
"scale/std",
"scale-info/std",
]
ink-as-dependency = []
272 changes: 272 additions & 0 deletions integration-tests/storage-types/lib.rs
@@ -0,0 +1,272 @@
//! # Integration Test for Storage Types
//!
//! This contract is made to showcase all of ink!'s storage types.
//! With this the proper decoding of the storage types can be tested.

#![cfg_attr(not(feature = "std"), no_std)]

#[ink::contract]
mod storage_types {
use ink::{
prelude::{
string::String,
vec,
vec::Vec,
},
storage::Mapping,
};
use scale::{
Decode,
Encode,
};

#[derive(Debug, Decode, Encode)]
#[cfg_attr(feature = "std", derive(::scale_info::TypeInfo))]
pub enum CustomError {
ErrorWithMessage(String),
}

#[derive(Clone, Debug, Decode, Default, Encode)]
#[cfg_attr(
feature = "std",
derive(scale_info::TypeInfo, ink::storage::traits::StorageLayout)
)]
pub enum EnumWithoutValues {
#[default]
A,
B,
C,
}

#[derive(Clone, Debug, Decode, Encode)]
#[cfg_attr(
feature = "std",
derive(scale_info::TypeInfo, ink::storage::traits::StorageLayout)
)]
pub enum EnumWithValues {
OneValue(u32),
TwoValues(u32, u32),
ThreeValues(u32, u32, u32),
}

#[derive(Clone, Debug, Decode, Encode)]
#[cfg_attr(
feature = "std",
derive(scale_info::TypeInfo, ink::storage::traits::StorageLayout)
)]
pub struct PrimitiveTypes {
bool_value: bool,
enum_without_values: EnumWithoutValues,
enum_with_values: EnumWithValues,
array_value: [u32; 3],
tuple_value: (u32, u32),
}

#[derive(Clone, Debug, Decode, Encode)]
#[cfg_attr(
feature = "std",
derive(scale_info::TypeInfo, ink::storage::traits::StorageLayout)
)]
pub struct SignedIntegers {
i128_value_max: i128,
i128_value_min: i128,
i16_value_max: i16,
i16_value_min: i16,
i32_value_max: i32,
i32_value_min: i32,
i64_value_max: i64,
i64_value_min: i64,
i8_value_max: i8,
i8_value_min: i8,
}

#[derive(Clone, Debug, Decode, Encode)]
#[cfg_attr(
feature = "std",
derive(scale_info::TypeInfo, ink::storage::traits::StorageLayout)
)]
pub struct SubstrateTypes {
account_id_value: AccountId,
balance_value_max: Balance,
balance_value_min: Balance,
hash_value: Hash,
}

#[derive(Clone, Debug, Decode, scale::Encode)]
#[cfg_attr(
feature = "std",
derive(scale_info::TypeInfo, ink::storage::traits::StorageLayout)
)]
pub struct InkPreludeTypes {
string_value: String,
vec_string_value: Vec<String>,
vec_vec_string_value: Vec<Vec<String>>,
}

#[derive(Clone, Decode, Encode)]
#[cfg_attr(
feature = "std",
derive(Debug, scale_info::TypeInfo, ink::storage::traits::StorageLayout)
)]
pub struct UnsignedIntegers {
u128_value_max: u128,
u128_value_min: u128,
u16_value_max: u16,
u16_value_min: u16,
u32_value_max: u32,
u32_value_min: u32,
u64_value_max: u64,
u64_value_min: u64,
u8_value_max: u8,
u8_value_min: u8,
}

#[derive(Debug)]
#[ink::storage_item]
pub struct MappingTypes {
mapping_u128_u128_value: Mapping<u128, u128>,
mapping_account_balance_value: Mapping<AccountId, Balance>,
mapping_account_hash_value: Mapping<AccountId, Hash>,
mapping_account_account_balance_value: Mapping<(AccountId, AccountId), Balance>,
}

#[ink(storage)]
pub struct StorageTypes {
ink_prelude_types: InkPreludeTypes,
primitive_types: PrimitiveTypes,
signed_integers: SignedIntegers,
substrate_types: SubstrateTypes,
unsigned_integers: UnsignedIntegers,
mapping_account_balance: Mapping<AccountId, Balance>,
}

impl Default for StorageTypes {
fn default() -> Self {
Self::new()
}
}

impl StorageTypes {
#[ink(constructor)]
pub fn new() -> Self {
let vec_string_value = vec![
String::from("This is a String"),
String::from("This is another String"),
];
let vec_vec_string_value = vec![vec_string_value.clone()];

// Mappings
let mut mapping_account_balance = Mapping::new();
mapping_account_balance.insert(AccountId::from([0x01; 32]), &100);

Self {
unsigned_integers: UnsignedIntegers {
u128_value_max: u128::MAX,
u128_value_min: u128::MIN,
u16_value_max: u16::MAX,
u16_value_min: u16::MIN,
u32_value_max: u32::MAX,
u32_value_min: u32::MIN,
u64_value_max: u64::MAX,
u64_value_min: u64::MIN,
u8_value_max: u8::MAX,
u8_value_min: u8::MIN,
},
signed_integers: SignedIntegers {
i128_value_max: i128::MAX,
i128_value_min: i128::MIN,
i16_value_max: i16::MAX,
i16_value_min: i16::MIN,
i32_value_max: i32::MAX,
i32_value_min: i32::MIN,
i64_value_max: i64::MAX,
i64_value_min: i64::MIN,
i8_value_max: i8::MAX,
i8_value_min: i8::MIN,
},
ink_prelude_types: InkPreludeTypes {
string_value: String::from("This is a string"),
vec_string_value,
vec_vec_string_value,
},
primitive_types: PrimitiveTypes {
bool_value: true,
enum_with_values: EnumWithValues::ThreeValues(1, 2, 3),
enum_without_values: EnumWithoutValues::A,
array_value: [3, 2, 1],
tuple_value: (7, 8),
},
substrate_types: SubstrateTypes {
account_id_value: AccountId::from([0x00; 32]),
balance_value_max: Balance::MAX,
balance_value_min: Balance::MIN,
hash_value: Hash::from([0x00; 32]),
},
mapping_account_balance,
}
}

#[ink(message)]
pub fn get_unsigned_integers(&self) -> UnsignedIntegers {
self.unsigned_integers.clone()
}

#[ink(message)]
pub fn get_signed_integers(&self) -> SignedIntegers {
self.signed_integers.clone()
}

#[ink(message)]
pub fn get_ink_prelude_types(&self) -> InkPreludeTypes {
self.ink_prelude_types.clone()
}

#[ink(message)]
pub fn get_substrate_types(&self) -> SubstrateTypes {
self.substrate_types.clone()
}

#[ink(message)]
pub fn get_primitive_types(&self) -> PrimitiveTypes {
self.primitive_types.clone()
}

#[ink(message)]
pub fn get_option_some(&self) -> Option<()> {
Some(())
peetzweg marked this conversation as resolved.
Show resolved Hide resolved
}

#[ink(message)]
pub fn get_option_none(&self) -> Option<()> {
None
}

#[ink(message)]
pub fn get_result_ok(&self) -> Result<(), CustomError> {
Ok(())
peetzweg marked this conversation as resolved.
Show resolved Hide resolved
}

#[ink(message)]
pub fn get_result_error(&self) -> Result<(), CustomError> {
Err(CustomError::ErrorWithMessage(String::from(
"This is the Error Message.",
)))
}

#[ink(message)]
pub fn get_panic(&self) -> Result<(), ()> {
panic!("This is the Panic message.")
}

#[ink(message)]
pub fn get_mapping_account_balance(
&self,
account_id: AccountId,
) -> Option<Balance> {
self.mapping_account_balance.get(account_id)
}
}

#[cfg(test)]
mod tests {}
}