1use bc::stl::bp_tx_stl;
22use commit_verify::stl::commit_verify_stl;
23use strict_types::stl::std_stl;
24use strict_types::{LibBuilder, SemId, SymbolicSys, SystemBuilder, TypeLib, TypeSystem};
25
26use crate::{
27 Amount, AssetName, Details, EmbeddedMedia, Nft, NftSpec, OwnedNft, Precision, ProofOfReserves, Ticker,
28 LIB_NAME_RGB21, LIB_NAME_RGB_CONTRACT,
29};
30
31pub const LIB_ID_RGB_INTERFACES: &str = "stl:SwzsMZmH-_Bp~u1Y-sYRyzR9-sj3ZgR7-JNCrNuP-PudeT5c#viva-comrade-bernard";
33
34pub const LIB_ID_RGB21: &str = "stl:hS_TcpfI-JCz36Es-E8NtlsS-bG0A68X-Te6ouRs-7_FNDhE#prelude-chicago-filter";
36
37pub fn rgb_contract_stl() -> TypeLib {
38 LibBuilder::with(libname!(LIB_NAME_RGB_CONTRACT), [
39 std_stl().to_dependency_types(),
40 bp_tx_stl().to_dependency_types(),
41 ])
42 .transpile::<Amount>()
43 .transpile::<Precision>()
44 .transpile::<Ticker>()
45 .transpile::<AssetName>()
46 .transpile::<Details>()
47 .transpile::<ProofOfReserves>()
48 .compile()
49 .expect("invalid common types library")
50}
51
52pub fn rgb21_stl() -> TypeLib {
53 LibBuilder::with(libname!(LIB_NAME_RGB21), [
54 std_stl().to_dependency_types(),
55 rgb_contract_stl().to_dependency_types(),
56 commit_verify_stl().to_dependency_types(),
57 bp_tx_stl().to_dependency_types(),
58 ])
59 .transpile::<Nft>()
60 .transpile::<OwnedNft>()
61 .transpile::<NftSpec>()
62 .transpile::<EmbeddedMedia>()
63 .compile()
64 .expect("invalid common types library")
65}
66
67#[derive(Debug)]
68pub struct CommonTypes(SymbolicSys);
69
70impl Default for CommonTypes {
71 fn default() -> Self { CommonTypes::new() }
72}
73
74impl CommonTypes {
75 pub fn new() -> Self {
76 Self(
77 SystemBuilder::new()
78 .import(std_stl())
79 .unwrap()
80 .import(bp_tx_stl())
81 .unwrap()
82 .import(rgb_contract_stl())
83 .unwrap()
84 .finalize()
85 .unwrap(),
86 )
87 }
88
89 pub fn type_system(&self) -> TypeSystem {
90 let types = rgb_contract_stl().types;
91 let types = types.iter().map(|(tn, ty)| ty.sem_id_named(tn));
92 self.0.as_types().extract(types).unwrap()
93 }
94
95 pub fn get(&self, name: &'static str) -> SemId {
96 *self
97 .0
98 .resolve(name)
99 .unwrap_or_else(|| panic!("type '{name}' is absent in RGB contract common type library"))
100 }
101}
102
103#[derive(Debug)]
104pub struct Rgb21Types(SymbolicSys);
105
106impl Default for Rgb21Types {
107 fn default() -> Self { Rgb21Types::new() }
108}
109
110impl Rgb21Types {
111 pub fn new() -> Self {
112 Self(
113 SystemBuilder::new()
114 .import(std_stl())
115 .unwrap()
116 .import(rgb_contract_stl())
117 .unwrap()
118 .import(commit_verify_stl())
119 .unwrap()
120 .import(bp_tx_stl())
121 .unwrap()
122 .import(rgb21_stl())
123 .unwrap()
124 .finalize()
125 .unwrap(),
126 )
127 }
128
129 pub fn type_system(&self) -> TypeSystem {
130 let types = rgb21_stl()
131 .types
132 .into_iter()
133 .chain(rgb_contract_stl().types)
134 .map(|(tn, ty)| ty.sem_id_named(&tn));
135 self.0.as_types().extract(types).unwrap()
136 }
137
138 pub fn get(&self, name: &'static str) -> SemId {
139 *self
140 .0
141 .resolve(name)
142 .unwrap_or_else(|| panic!("type '{name}' is absent in RGB21 type library"))
143 }
144}
145
146#[cfg(test)]
147mod test {
148 use super::*;
149
150 #[test]
151 fn common_lib_id() {
152 let lib = rgb_contract_stl();
153 assert_eq!(lib.id().to_string(), LIB_ID_RGB_INTERFACES);
154 }
155
156 #[test]
157 fn rgb21_lib_id() {
158 let lib = rgb21_stl();
159 assert_eq!(lib.id().to_string(), LIB_ID_RGB21);
160 }
161}