whisky/utils/
blueprint.rs

1use crate::data::*;
2use crate::*;
3use std::marker::PhantomData;
4
5#[derive(Debug, Clone)]
6pub struct MintingBlueprint<P = (), R = ByteString>
7where
8    P: PlutusDataJson,
9    R: PlutusDataJson,
10{
11    pub version: LanguageVersion,
12    pub cbor: String,
13    pub hash: String,
14    _phantom_param: PhantomData<P>,
15    _phantom_redeemer: PhantomData<R>,
16}
17
18impl<P, R> MintingBlueprint<P, R>
19where
20    P: PlutusDataJson,
21    R: PlutusDataJson,
22{
23    pub fn new(version: LanguageVersion) -> Self {
24        Self {
25            version,
26            cbor: "".to_string(),
27            hash: "".to_string(),
28            _phantom_param: PhantomData,
29            _phantom_redeemer: PhantomData,
30        }
31    }
32
33    pub fn param_script(
34        &mut self,
35        compiled_code: &str,
36        params: &[&str],
37        params_type: BuilderDataType,
38    ) -> Result<&mut Self, WError> {
39        let cbor = apply_params_to_script(compiled_code, params, params_type)?;
40        let hash = get_script_hash(&cbor, self.version.clone())?;
41        self.hash = hash;
42        self.cbor = cbor;
43        Ok(self)
44    }
45
46    pub fn no_param_script(&mut self, compiled_code: &str) -> Result<&mut Self, WError> {
47        let cbor = apply_params_to_script(compiled_code, &[], BuilderDataType::CBOR)?;
48        let hash = get_script_hash(&cbor, self.version.clone())?;
49        self.hash = hash;
50        self.cbor = cbor;
51        Ok(self)
52    }
53
54    pub fn params(&self, params: &[P]) -> Vec<String> {
55        params
56            .iter()
57            .map(|p| p.to_json_string())
58            .collect::<Vec<String>>()
59    }
60
61    pub fn redeemer(&self, redeemer: R) -> WData {
62        WData::JSON(redeemer.to_json_string())
63    }
64}
65
66#[derive(Debug, Clone)]
67pub struct WithdrawalBlueprint<P = ByteString, R = ByteString>
68where
69    P: PlutusDataJson,
70    R: PlutusDataJson,
71{
72    pub version: LanguageVersion,
73    pub network_id: u8,
74    pub cbor: String,
75    pub hash: String,
76    pub address: String,
77    _phantom_param: PhantomData<P>,
78    _phantom_redeemer: PhantomData<R>,
79}
80
81impl<P, R> WithdrawalBlueprint<P, R>
82where
83    P: PlutusDataJson,
84    R: PlutusDataJson,
85{
86    pub fn new(version: LanguageVersion, network_id: u8) -> Self {
87        Self {
88            version,
89            network_id,
90            cbor: "".to_string(),
91            hash: "".to_string(),
92            address: "".to_string(),
93            _phantom_param: PhantomData,
94            _phantom_redeemer: PhantomData,
95        }
96    }
97
98    pub fn param_script(
99        &mut self,
100        compiled_code: &str,
101        params: &[&str],
102        params_type: BuilderDataType,
103    ) -> Result<&mut Self, WError> {
104        let cbor = apply_params_to_script(compiled_code, params, params_type).unwrap();
105        let hash = get_script_hash(&cbor, self.version.clone()).unwrap();
106        self.address = script_hash_to_stake_address(&hash, self.network_id)?;
107        self.hash = hash;
108        self.cbor = cbor;
109        Ok(self)
110    }
111
112    pub fn no_param_script(&mut self, compiled_code: &str) -> Result<&mut Self, WError> {
113        let cbor = apply_params_to_script(compiled_code, &[], BuilderDataType::CBOR)?;
114        let hash = get_script_hash(&cbor, self.version.clone())?;
115        self.address = script_hash_to_stake_address(&hash, self.network_id)?;
116        self.hash = hash;
117        self.cbor = cbor;
118        Ok(self)
119    }
120
121    pub fn params(&self, params: &[P]) -> Vec<String> {
122        params
123            .iter()
124            .map(|p| p.to_json_string())
125            .collect::<Vec<String>>()
126    }
127
128    pub fn redeemer(&self, redeemer: R) -> WData {
129        WData::JSON(redeemer.to_json_string())
130    }
131}
132
133#[derive(Debug, Clone)]
134pub struct SpendingBlueprint<P = ByteString, R = ByteString, D = ByteString>
135where
136    P: PlutusDataJson,
137    R: PlutusDataJson,
138    D: PlutusDataJson,
139{
140    pub version: LanguageVersion,
141    pub network_id: u8,
142    pub stake_hash: Option<(String, bool)>,
143    pub cbor: String,
144    pub hash: String,
145    pub address: String,
146    _phantom_param: PhantomData<[P]>,
147    _phantom_redeemer: PhantomData<R>,
148    _phantom_datum: PhantomData<D>,
149}
150
151impl<P, R, D> SpendingBlueprint<P, R, D>
152where
153    P: PlutusDataJson,
154    R: PlutusDataJson,
155    D: PlutusDataJson,
156{
157    pub fn new(
158        version: LanguageVersion,
159        network_id: u8,
160        stake_hash: Option<(String, bool)>,
161    ) -> Self {
162        Self {
163            version,
164            network_id,
165            stake_hash,
166            cbor: "".to_string(),
167            hash: "".to_string(),
168            address: "".to_string(),
169            _phantom_param: PhantomData,
170            _phantom_redeemer: PhantomData,
171            _phantom_datum: PhantomData,
172        }
173    }
174
175    pub fn param_script(
176        &mut self,
177        compiled_code: &str,
178        params: &[&str],
179        params_type: BuilderDataType,
180    ) -> Result<&mut Self, WError> {
181        let cbor = apply_params_to_script(compiled_code, params, params_type)?;
182        let hash = get_script_hash(&cbor, self.version.clone())?;
183        let stake_hash: Option<(&str, bool)> = self
184            .stake_hash
185            .as_ref()
186            .map(|(hash, is_script)| (hash.as_str(), *is_script));
187
188        let address = script_to_address(self.network_id, &hash, stake_hash);
189        self.hash = hash;
190        self.cbor = cbor;
191        self.address = address;
192        Ok(self)
193    }
194
195    pub fn no_param_script(&mut self, compiled_code: &str) -> Result<&mut Self, WError> {
196        let cbor = apply_params_to_script(compiled_code, &[], BuilderDataType::CBOR)?;
197        let hash = get_script_hash(&cbor, self.version.clone())?;
198        let stake_hash: Option<(&str, bool)> = self
199            .stake_hash
200            .as_ref()
201            .map(|(hash, is_script)| (hash.as_str(), *is_script));
202        let address = script_to_address(self.network_id, &hash, stake_hash);
203        self.hash = hash;
204        self.cbor = cbor;
205        self.address = address;
206        Ok(self)
207    }
208
209    pub fn params(&self, params: &[P]) -> Vec<String> {
210        params
211            .iter()
212            .map(|p| p.to_json_string())
213            .collect::<Vec<String>>()
214    }
215
216    pub fn redeemer(&self, redeemer: R) -> WData {
217        WData::JSON(redeemer.to_json_string())
218    }
219
220    pub fn datum(&self, datum: D) -> WData {
221        WData::JSON(datum.to_json_string())
222    }
223}