Skip to content

Commit

Permalink
Move rand-extension to it's own package (use-ink#29)
Browse files Browse the repository at this point in the history
* Move rand-extension to it's own package

* fixes
  • Loading branch information
pgherveou authored Jun 21, 2023
1 parent 5fd51e2 commit 3254a29
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 41 deletions.
3 changes: 2 additions & 1 deletion rand-extension/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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).
26 changes: 26 additions & 0 deletions rand-extension/contract/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[package]
name = "rand_contract"
version = "4.2.0"
authors = ["Parity Technologies <[email protected]>"]
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 = []

37 changes: 3 additions & 34 deletions rand-extension/lib.rs → rand-extension/contract/lib.rs
100755 → 100644
Original file line number Diff line number Diff line change
@@ -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))]
Expand All @@ -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.
///
Expand Down
8 changes: 2 additions & 6 deletions rand-extension/Cargo.toml → rand-extension/rand_extension/Cargo.toml
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
[package]
name = "rand_extension"
version = "4.2.0"
version = "1.0.0"
authors = ["Parity Technologies <[email protected]>"]
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"]
Expand Down
33 changes: 33 additions & 0 deletions rand-extension/rand_extension/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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"),
}
}
}

0 comments on commit 3254a29

Please sign in to comment.