use crate::Bytes;
use codec::{Decode, Encode, MaxEncodedLen};
#[cfg(feature = "std")]
use core::fmt::{Display, Formatter, Result as DisplayResult};
use scale_info::TypeInfo;
#[cfg(feature = "std")]
use serde::{Deserialize, Serialize, Serializer};
use sp_runtime::RuntimeDebug;
use sp_std::{vec, vec::Vec};
#[derive(RuntimeDebug)]
pub struct Source {
hash: [u8; 32],
language: SourceLanguage,
compiler: SourceCompiler,
}
impl Source {
pub fn new(hash: [u8; 32], language: SourceLanguage, compiler: SourceCompiler) -> Self {
Source {
hash,
language,
compiler,
}
}
}
#[derive(RuntimeDebug)]
pub struct SourceLanguage {
language: Language,
version: Bytes,
}
impl SourceLanguage {
pub fn new(language: Language, version: Vec<u8>) -> Self {
SourceLanguage { language, version }
}
}
#[cfg(feature = "std")]
impl Serialize for SourceLanguage {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&self.to_string())
}
}
#[cfg(feature = "std")]
impl Display for SourceLanguage {
fn fmt(&self, f: &mut Formatter<'_>) -> DisplayResult {
write!(f, "{} {:?}", self.language, self.version)
}
}
#[derive(RuntimeDebug)]
pub enum Language {
Ink,
Solidity,
AssemblyScript,
}
#[cfg(feature = "std")]
impl Display for Language {
fn fmt(&self, f: &mut Formatter<'_>) -> DisplayResult {
match self {
Self::Ink => write!(f, "ink!"),
Self::Solidity => write!(f, "Solidity"),
Self::AssemblyScript => write!(f, "AssemblyScript"),
}
}
}
#[derive(RuntimeDebug)]
pub struct SourceCompiler {
compiler: Compiler,
version: Vec<u8>,
}
#[cfg(feature = "std")]
impl Display for SourceCompiler {
fn fmt(&self, f: &mut Formatter<'_>) -> DisplayResult {
write!(f, "{} {:?}", self.compiler, self.version)
}
}
#[cfg(feature = "std")]
impl Serialize for SourceCompiler {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&self.to_string())
}
}
impl SourceCompiler {
pub fn new(compiler: Compiler, version: Vec<u8>) -> Self {
SourceCompiler { compiler, version }
}
}
#[derive(RuntimeDebug)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
pub enum Compiler {
RustC,
Solang,
}
#[cfg(feature = "std")]
impl Display for Compiler {
fn fmt(&self, f: &mut Formatter<'_>) -> DisplayResult {
match self {
Self::RustC => write!(f, "rustc"),
Self::Solang => write!(f, "solang"),
}
}
}
#[derive(Clone, Debug, Eq, PartialEq, Encode, Decode, TypeInfo, Copy, Default, MaxEncodedLen)]
pub enum ContractType {
#[default]
System,
VanillaEvm,
VanillaWasm,
VolatileEvm,
VolatileWasm,
}
impl From<ContractType> for u8 {
fn from(value: ContractType) -> Self {
match value {
ContractType::System => 0,
ContractType::VanillaEvm => 1,
ContractType::VanillaWasm => 2,
ContractType::VolatileEvm => 3,
ContractType::VolatileWasm => 4,
}
}
}
#[derive(Clone, Debug, Eq, PartialEq, Encode, Decode, TypeInfo)]
pub struct ContractMetadata {
metadata_version: Vec<u8>,
name: Vec<u8>,
contract_type: ContractType,
version: Vec<u8>,
authors: Vec<Vec<u8>>,
description: Option<Vec<u8>>,
documentation: Option<Vec<u8>>,
repository: Option<Vec<u8>>,
homepage: Option<Vec<u8>>,
license: Option<Vec<u8>>,
}
impl Default for ContractMetadata {
fn default() -> Self {
ContractMetadata {
metadata_version: b"0.0.1".encode(),
name: b"Default contract".encode(),
contract_type: ContractType::VolatileWasm,
version: b"0.0.1".encode(),
authors: vec![b"Some author".encode()],
description: None,
documentation: None,
repository: None,
homepage: None,
license: None,
}
}
}
impl ContractMetadata {
pub fn new(
metadata_version: Vec<u8>,
name: Vec<u8>,
contract_type: ContractType,
version: Vec<u8>,
authors: Vec<Vec<u8>>,
description: Option<Vec<u8>>,
documentation: Option<Vec<u8>>,
repository: Option<Vec<u8>>,
homepage: Option<Vec<u8>>,
license: Option<Vec<u8>>,
) -> ContractMetadata {
ContractMetadata {
metadata_version,
name,
contract_type,
version,
authors,
description,
documentation,
repository,
homepage,
license,
}
}
pub fn system_contract() -> Self {
ContractMetadata {
metadata_version: b"0.0.1".encode(),
name: b"Default contract".encode(),
contract_type: ContractType::System,
version: b"0.0.1".encode(),
authors: vec![b"Some author".encode()],
description: None,
documentation: None,
repository: None,
homepage: None,
license: None,
}
}
pub fn get_contract_type(&self) -> &ContractType {
&self.contract_type
}
pub fn with_type(mut self, kind: ContractType) -> Self {
self.contract_type = kind;
self
}
}