Create a dedicated crate for the extension

This commit is contained in:
Luke Parker
2022-07-17 20:56:10 -04:00
parent 5583bf3447
commit 43c4487804
6 changed files with 115 additions and 97 deletions

View File

@@ -0,0 +1,16 @@
[package]
name = "serai-extension"
version = "0.1.0"
description = "An ink! extension for exposing Serai to ink"
license = "AGPL-3.0-only"
authors = ["Luke Parker <lukeparker5132@gmail.com>"]
edition = "2021"
[dependencies]
ink_env = { version = "3", default-features = false }
ink_lang = { version = "3", default-features = false }
[features]
default = ["std"]
std = ["ink_env/std"]
ink-as-dependency = []

View File

@@ -0,0 +1,35 @@
#![cfg_attr(not(feature = "std"), no_std)]
use ink_lang as ink;
use ink_env::{Environment, DefaultEnvironment, AccountId};
#[ink::chain_extension]
pub trait SeraiExtension {
type ErrorCode = ();
/// Returns the amount of active validators on the current chain.
#[ink(extension = 0, handle_status = false, returns_result = false)]
fn active_validators_len() -> u16;
/// Returns the ID for the current validator set for the current chain.
// TODO: Decide if this should be an increasing unsigned integer instead of a hash.
#[ink(extension = 1, handle_status = false, returns_result = false)]
fn validator_set_id() -> [u8; 32];
/// Returns if the specified account is an active validator for the current chain.
#[ink(extension = 2, handle_status = false, returns_result = false)]
fn is_active_validator(account: &AccountId) -> bool;
}
pub struct SeraiEnvironment;
impl Environment for SeraiEnvironment {
const MAX_EVENT_TOPICS: usize = <DefaultEnvironment as Environment>::MAX_EVENT_TOPICS;
type AccountId = <DefaultEnvironment as Environment>::AccountId;
type Balance = <DefaultEnvironment as Environment>::Balance;
type Hash = <DefaultEnvironment as Environment>::Hash;
type BlockNumber = <DefaultEnvironment as Environment>::BlockNumber;
type Timestamp = <DefaultEnvironment as Environment>::Timestamp;
type ChainExtension = SeraiExtension;
}