diff --git a/rand-extension/README.md b/rand-extension/README.md index 19d4cc48..5b2731a4 100644 --- a/rand-extension/README.md +++ b/rand-extension/README.md @@ -22,6 +22,7 @@ To integrate this example into Substrate you need to do two things: * Use the implementation as the associated type `ChainExtension` of the trait `pallet_contracts::Config`: + ```rust impl pallet_contracts::Config for Runtime { … @@ -32,4 +33,4 @@ To integrate this example into Substrate you need to do two things: ## ink! Integration -See the example contract in [`lib.rs`](lib.rs). +See the example contract in [`contract/lib.rs`](contract/lib.rs). diff --git a/rand-extension/contract/Cargo.toml b/rand-extension/contract/Cargo.toml new file mode 100644 index 00000000..51092957 --- /dev/null +++ b/rand-extension/contract/Cargo.toml @@ -0,0 +1,26 @@ +[package] +name = "rand_contract" +version = "4.2.0" +authors = ["Parity Technologies "] +edition = "2021" +publish = false + +[dependencies] +ink = { version = "4.2", default-features = false } +scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] } +scale-info = { version = "2.5", default-features = false, features = ["derive"] } +rand_extension = { version = "1.0.0", path = "../rand_extension", default-features = false } + +[lib] +path = "lib.rs" + +[features] +default = ["std"] +std = [ + "ink/std", + "scale/std", + "scale-info/std", + "rand_extension/std" +] +ink-as-dependency = [] + diff --git a/rand-extension/lib.rs b/rand-extension/contract/lib.rs old mode 100755 new mode 100644 similarity index 78% rename from rand-extension/lib.rs rename to rand-extension/contract/lib.rs index 3b61b057..e738df75 --- a/rand-extension/lib.rs +++ b/rand-extension/contract/lib.rs @@ -1,38 +1,7 @@ #![cfg_attr(not(feature = "std"), no_std, no_main)] use ink::env::Environment; - -/// This is an example of how an ink! contract may call the Substrate -/// runtime function `RandomnessCollectiveFlip::random_seed`. See the -/// file `runtime/chain-extension-example.rs` for that implementation. -/// -/// Here we define the operations to interact with the Substrate runtime. -#[ink::chain_extension] -pub trait FetchRandom { - type ErrorCode = RandomReadErr; - - /// Note: this gives the operation a corresponding `func_id` (1101 in this case), - /// and the chain-side chain extension will get the `func_id` to do further - /// operations. - #[ink(extension = 1101)] - fn fetch_random(subject: [u8; 32]) -> [u8; 32]; -} - -#[derive(Debug, Copy, Clone, PartialEq, Eq, scale::Encode, scale::Decode)] -#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] -pub enum RandomReadErr { - FailGetRandomSource, -} - -impl ink::env::chain_extension::FromStatusCode for RandomReadErr { - fn from_status_code(status_code: u32) -> Result<(), Self> { - match status_code { - 0 => Ok(()), - 1 => Err(Self::FailGetRandomSource), - _ => panic!("encountered unknown status code"), - } - } -} +use rand_extension::FetchRandom; #[derive(Debug, Clone, PartialEq, Eq)] #[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] @@ -52,8 +21,8 @@ impl Environment for CustomEnvironment { } #[ink::contract(env = crate::CustomEnvironment)] -mod rand_extension { - use super::RandomReadErr; +mod rand_contract { + use rand_extension::RandomReadErr; /// Defines the storage of our contract. /// diff --git a/rand-extension/Cargo.toml b/rand-extension/rand_extension/Cargo.toml old mode 100755 new mode 100644 similarity index 86% rename from rand-extension/Cargo.toml rename to rand-extension/rand_extension/Cargo.toml index 194390ab..c505f8b5 --- a/rand-extension/Cargo.toml +++ b/rand-extension/rand_extension/Cargo.toml @@ -1,18 +1,14 @@ [package] name = "rand_extension" -version = "4.2.0" +version = "1.0.0" authors = ["Parity Technologies "] edition = "2021" publish = false [dependencies] ink = { version = "4.2", default-features = false } - scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] } -scale-info = { version = "2.5", default-features = false, features = ["derive"], optional = true } - -[lib] -path = "lib.rs" +scale-info = { version = "2.5", default-features = false, features = ["derive"] } [features] default = ["std"] diff --git a/rand-extension/rand_extension/src/lib.rs b/rand-extension/rand_extension/src/lib.rs new file mode 100644 index 00000000..7dfc7adc --- /dev/null +++ b/rand-extension/rand_extension/src/lib.rs @@ -0,0 +1,33 @@ +#![cfg_attr(not(feature = "std"), no_std, no_main)] + +/// This is an example of how an ink! contract may call the Substrate +/// runtime function `RandomnessCollectiveFlip::random_seed`. See the +/// file `runtime/chain-extension-example.rs` for that implementation. +/// +/// Here we define the operations to interact with the Substrate runtime. +#[ink::chain_extension] +pub trait FetchRandom { + type ErrorCode = RandomReadErr; + + /// Note: this gives the operation a corresponding `func_id` (1101 in this case), + /// and the chain-side chain extension will get the `func_id` to do further + /// operations. + #[ink(extension = 1101)] + fn fetch_random(subject: [u8; 32]) -> [u8; 32]; +} + +#[derive(Debug, Copy, Clone, PartialEq, Eq, scale::Encode, scale::Decode)] +#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] +pub enum RandomReadErr { + FailGetRandomSource, +} + +impl ink::env::chain_extension::FromStatusCode for RandomReadErr { + fn from_status_code(status_code: u32) -> Result<(), Self> { + match status_code { + 0 => Ok(()), + 1 => Err(Self::FailGetRandomSource), + _ => panic!("encountered unknown status code"), + } + } +}