Skip to content

Commit

Permalink
add types for asset names
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Dec 16, 2024
1 parent 05b2ef1 commit 729f03a
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 2 deletions.
33 changes: 33 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ serde = { version = "1.0", optional = true, features = ["derive"] }
[features]
default = []
all = ["serde"]
serde = ["dep:serde"]
serde = ["dep:serde", "strict_encoding/serde"]

[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen = "0.2"
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ extern crate serde;

mod fungible;
mod types;
mod names;

pub use fungible::*;
pub use names::{AssetName, Ticker};
pub use types::{rgb_contract_stl, CommonTypes};

pub const LIB_NAME_RGB_CONTRACT: &str = "RGBContract";
68 changes: 68 additions & 0 deletions src/names.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Collection of the standard RGB smart contract interface
//
// SPDX-License-Identifier: Apache-2.0
//
// Designed in 2019-2025 by RGB Consortium members & contributors
// Written in 2024-2025 by Dr Maxim Orlovsky <[email protected]>
//
// Copyright (C) 2019-2025 RGB Consortium members & contributors
// All rights under the above copyrights are reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
// or implied. See the License for the specific language governing permissions and limitations under
// the License.

use std::hash::{Hash, Hasher};
use std::str::FromStr;

use strict_encoding::stl::{Alpha, AlphaNum, AsciiPrintable};
use strict_encoding::RString;
use strict_types::StrictVal;

use crate::LIB_NAME_RGB_CONTRACT;

#[derive(Wrapper, Clone, Ord, PartialOrd, Eq, From)]
#[wrapper(Deref, Display, FromStr)]
#[derive(StrictDumb, StrictType, StrictEncode, StrictDecode)]
#[strict_type(lib = LIB_NAME_RGB_CONTRACT, dumb = Self::from(RString::strict_dumb()))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(transparent))]
pub struct Ticker(RString<Alpha, AlphaNum, 2, 8>);

impl PartialEq for Ticker {
fn eq(&self, other: &Self) -> bool {
self.as_str()
.to_uppercase()
.eq(&other.as_str().to_uppercase())
}
}

impl Hash for Ticker {
fn hash<H: Hasher>(&self, state: &mut H) { self.as_str().to_uppercase().hash(state) }
}

impl_ident_type!(Ticker);
impl_ident_subtype!(Ticker);

impl Ticker {
pub fn from_strict_val_unchecked(value: &StrictVal) -> Self { Self::from_str(&value.unwrap_string()).unwrap() }
}

#[derive(Wrapper, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, From)]
#[wrapper(Deref, Display, FromStr)]
#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
#[strict_type(lib = LIB_NAME_RGB_CONTRACT)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(transparent))]
pub struct AssetName(RString<AsciiPrintable, AsciiPrintable, 1, 40>);

impl_ident_type!(AssetName);
impl_ident_subtype!(AssetName);

impl AssetName {
pub fn from_strict_val_unchecked(value: &StrictVal) -> Self { Self::from_str(&value.unwrap_string()).unwrap() }
}
4 changes: 3 additions & 1 deletion src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
use strict_types::stl::std_stl;
use strict_types::{LibBuilder, SemId, SymbolicSys, SystemBuilder, TypeLib, TypeSystem};

use crate::{Amount, Precision, LIB_NAME_RGB_CONTRACT};
use crate::{Amount, AssetName, Precision, Ticker, LIB_NAME_RGB_CONTRACT};

#[derive(Debug)]
pub struct CommonTypes(SymbolicSys);
Expand All @@ -36,6 +36,8 @@ pub fn rgb_contract_stl() -> TypeLib {
})
.transpile::<Amount>()
.transpile::<Precision>()
.transpile::<Ticker>()
.transpile::<AssetName>()
.compile()
.expect("invalid common types library")
}
Expand Down

0 comments on commit 729f03a

Please sign in to comment.