forked from use-ink/ink-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move rand-extension to it's own package (use-ink#29)
* Move rand-extension to it's own package * fixes
- Loading branch information
Showing
5 changed files
with
66 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = [] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 2 additions & 6 deletions
8
rand-extension/Cargo.toml → rand-extension/rand_extension/Cargo.toml
100755 → 100644
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"), | ||
} | ||
} | ||
} |