1use bp::bc::stl::bp_tx_stl;
23use bp::stl::bp_core_stl;
24use commit_verify::stl::commit_verify_stl;
25pub use rgb::stl::{aluvm_stl, rgb_core_stl, LIB_ID_RGB};
26use strict_types::stl::{std_stl, strict_types_stl};
27use strict_types::typesys::SystemBuilder;
28use strict_types::{CompileError, LibBuilder, SemId, SymbolicSys, TypeLib, TypeSystem};
29
30use super::{
31 Amount, BurnMeta, ContractData, DivisibleAssetSpec, Error, IssueMeta, MediaType,
32 RicardianContract, Timestamp, LIB_NAME_RGB_CONTRACT,
33};
34use crate::containers::{Contract, Transfer};
35use crate::stl::ProofOfReserves;
36use crate::LIB_NAME_RGB_STD;
37
38pub const LIB_ID_RGB_CONTRACT: &str =
41 "urn:ubideco:stl:6vbr9ZrtsD9aBjo5qRQ36QEZPVucqvRRjKCPqE8yPeJr#choice-little-boxer";
42
43pub const LIB_ID_RGB_STD: &str =
45 "urn:ubideco:stl:3KXsWZ6hSKRbPjSVwRGbwnwJp3ZNQ2tfe6QUwLJEDG6K#twist-paul-carlo";
46
47fn _rgb_std_stl() -> Result<TypeLib, CompileError> {
48 LibBuilder::new(libname!(LIB_NAME_RGB_STD), tiny_bset! {
49 std_stl().to_dependency(),
50 strict_types_stl().to_dependency(),
51 commit_verify_stl().to_dependency(),
52 bp_tx_stl().to_dependency(),
53 bp_core_stl().to_dependency(),
54 aluvm_stl().to_dependency(),
55 rgb_core_stl().to_dependency()
56 })
57 .transpile::<Transfer>()
58 .transpile::<Contract>()
59 .compile()
60}
61
62fn _rgb_contract_stl() -> Result<TypeLib, CompileError> {
63 LibBuilder::new(libname!(LIB_NAME_RGB_CONTRACT), tiny_bset! {
64 std_stl().to_dependency(),
65 bp_tx_stl().to_dependency()
66 })
67 .transpile::<Amount>()
68 .transpile::<Timestamp>()
69 .transpile::<DivisibleAssetSpec>()
70 .transpile::<RicardianContract>()
71 .transpile::<ContractData>()
72 .transpile::<MediaType>()
73 .transpile::<ProofOfReserves>()
74 .transpile::<BurnMeta>()
75 .transpile::<IssueMeta>()
76 .compile()
77}
78
79pub fn rgb_std_stl() -> TypeLib { _rgb_std_stl().expect("invalid strict type RGBStd library") }
81
82pub fn rgb_contract_stl() -> TypeLib {
85 _rgb_contract_stl().expect("invalid strict type RGBContract library")
86}
87
88#[derive(Debug)]
89pub struct StandardTypes(SymbolicSys);
90
91impl Default for StandardTypes {
92 fn default() -> Self { StandardTypes::new() }
93}
94
95impl StandardTypes {
96 pub fn new() -> Self {
97 Self::try_with([std_stl(), bp_tx_stl(), rgb_contract_stl()])
98 .expect("error in standard RGBContract type system")
99 }
100
101 pub fn with(lib: TypeLib) -> Self {
102 Self::try_with([std_stl(), bp_tx_stl(), rgb_contract_stl(), lib])
103 .expect("error in standard RGBContract type system")
104 }
105
106 #[allow(clippy::result_large_err)]
107 fn try_with(libs: impl IntoIterator<Item = TypeLib>) -> Result<Self, Error> {
108 let mut builder = SystemBuilder::new();
109 for lib in libs.into_iter() {
110 builder = builder.import(lib)?;
111 }
112 let sys = builder.finalize()?;
113 Ok(Self(sys))
114 }
115
116 pub fn type_system(&self) -> TypeSystem { self.0.as_types().clone() }
117
118 pub fn get(&self, name: &'static str) -> SemId {
119 *self.0.resolve(name).unwrap_or_else(|| {
120 panic!("type '{name}' is absent in standard RGBContract type library")
121 })
122 }
123}
124
125#[cfg(test)]
126mod test {
127 use super::*;
128
129 #[test]
130 fn contract_lib_id() {
131 let lib = rgb_contract_stl();
132 assert_eq!(lib.id().to_string(), LIB_ID_RGB_CONTRACT);
133 }
134
135 #[test]
136 fn std_lib_id() {
137 let lib = rgb_std_stl();
138 assert_eq!(lib.id().to_string(), LIB_ID_RGB_STD);
139 }
140}