use bc::stl::bp_tx_stl;
use commit_verify::stl::commit_verify_stl;
use strict_types::stl::std_stl;
use strict_types::{LibBuilder, SemId, SymbolicSys, SystemBuilder, TypeLib, TypeSystem};
use crate::{
Amount, AssetName, Details, EmbeddedMedia, Nft, NftSpec, OwnedNft, Precision, ProofOfReserves, Ticker,
LIB_NAME_RGB21, LIB_NAME_RGB_CONTRACT,
};
pub const LIB_ID_RGB_INTERFACES: &str = "stl:SwzsMZmH-_Bp~u1Y-sYRyzR9-sj3ZgR7-JNCrNuP-PudeT5c#viva-comrade-bernard";
pub const LIB_ID_RGB21: &str = "stl:hS_TcpfI-JCz36Es-E8NtlsS-bG0A68X-Te6ouRs-7_FNDhE#prelude-chicago-filter";
pub fn rgb_contract_stl() -> TypeLib {
LibBuilder::with(libname!(LIB_NAME_RGB_CONTRACT), [
std_stl().to_dependency_types(),
bp_tx_stl().to_dependency_types(),
])
.transpile::<Amount>()
.transpile::<Precision>()
.transpile::<Ticker>()
.transpile::<AssetName>()
.transpile::<Details>()
.transpile::<ProofOfReserves>()
.compile()
.expect("invalid common types library")
}
pub fn rgb21_stl() -> TypeLib {
LibBuilder::with(libname!(LIB_NAME_RGB21), [
std_stl().to_dependency_types(),
rgb_contract_stl().to_dependency_types(),
commit_verify_stl().to_dependency_types(),
bp_tx_stl().to_dependency_types(),
])
.transpile::<Nft>()
.transpile::<OwnedNft>()
.transpile::<NftSpec>()
.transpile::<EmbeddedMedia>()
.compile()
.expect("invalid common types library")
}
#[derive(Debug)]
pub struct CommonTypes(SymbolicSys);
impl Default for CommonTypes {
fn default() -> Self { CommonTypes::new() }
}
impl CommonTypes {
pub fn new() -> Self {
Self(
SystemBuilder::new()
.import(std_stl())
.unwrap()
.import(bp_tx_stl())
.unwrap()
.import(rgb_contract_stl())
.unwrap()
.finalize()
.unwrap(),
)
}
pub fn type_system(&self) -> TypeSystem {
let types = rgb_contract_stl().types;
let types = types.iter().map(|(tn, ty)| ty.sem_id_named(tn));
self.0.as_types().extract(types).unwrap()
}
pub fn get(&self, name: &'static str) -> SemId {
*self
.0
.resolve(name)
.unwrap_or_else(|| panic!("type '{name}' is absent in RGB contract common type library"))
}
}
#[derive(Debug)]
pub struct Rgb21Types(SymbolicSys);
impl Default for Rgb21Types {
fn default() -> Self { Rgb21Types::new() }
}
impl Rgb21Types {
pub fn new() -> Self {
Self(
SystemBuilder::new()
.import(std_stl())
.unwrap()
.import(rgb_contract_stl())
.unwrap()
.import(commit_verify_stl())
.unwrap()
.import(bp_tx_stl())
.unwrap()
.import(rgb21_stl())
.unwrap()
.finalize()
.unwrap(),
)
}
pub fn type_system(&self) -> TypeSystem {
let types = rgb21_stl()
.types
.into_iter()
.chain(rgb_contract_stl().types)
.map(|(tn, ty)| ty.sem_id_named(&tn));
self.0.as_types().extract(types).unwrap()
}
pub fn get(&self, name: &'static str) -> SemId {
*self
.0
.resolve(name)
.unwrap_or_else(|| panic!("type '{name}' is absent in RGB21 type library"))
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn common_lib_id() {
let lib = rgb_contract_stl();
assert_eq!(lib.id().to_string(), LIB_ID_RGB_INTERFACES);
}
#[test]
fn rgb21_lib_id() {
let lib = rgb21_stl();
assert_eq!(lib.id().to_string(), LIB_ID_RGB21);
}
}