whisky/builder/mint.rs
1use crate::*;
2
3use super::{TxBuilder, WRedeemer};
4
5impl TxBuilder {
6 /// ## Transaction building method
7 ///
8 /// Indicate that the transaction is withdrawing using a plutus staking script in the TxBuilder instance
9 ///
10 /// ### Arguments
11 ///
12 /// * `language_version` - The language version of the script
13 ///
14 /// ### Returns
15 ///
16 /// * `Self` - The TxBuilder instance
17 pub fn mint_plutus_script(&mut self, language_version: &LanguageVersion) -> &mut Self {
18 match language_version {
19 LanguageVersion::V1 => self.mint_plutus_script_v1(),
20 LanguageVersion::V2 => self.mint_plutus_script_v2(),
21 LanguageVersion::V3 => self.mint_plutus_script_v3(),
22 }
23 }
24
25 /// ## Transaction building method
26 ///
27 /// Indicate that the transaction is minting a Plutus script v1 in the TxBuilder instance
28 ///
29 /// ### Returns
30 ///
31 /// * `Self` - The TxBuilder instance
32 pub fn mint_plutus_script_v1(&mut self) -> &mut Self {
33 self.adding_plutus_mint = Some(LanguageVersion::V1);
34 self
35 }
36
37 /// ## Transaction building method
38 ///
39 /// Indicate that the transaction is minting a Plutus script v2 in the TxBuilder instance
40 ///
41 /// ### Returns
42 ///
43 /// * `Self` - The TxBuilder instance
44 pub fn mint_plutus_script_v2(&mut self) -> &mut Self {
45 self.adding_plutus_mint = Some(LanguageVersion::V2);
46 self
47 }
48
49 /// ## Transaction building method
50 ///
51 /// Indicate that the transaction is minting a Plutus script v2 in the TxBuilder instance
52 ///
53 /// ### Returns
54 ///
55 /// * `Self` - The TxBuilder instance
56 pub fn mint_plutus_script_v3(&mut self) -> &mut Self {
57 self.adding_plutus_mint = Some(LanguageVersion::V3);
58 self
59 }
60
61 /// ## Transaction building method
62 ///
63 /// Mint assets in the TxBuilder instance
64 ///
65 /// ### Arguments
66 ///
67 /// * `quantity` - The quantity of assets to mint
68 /// * `policy` - The policy
69 /// * `name` - The name of the asset
70 ///
71 /// ### Returns
72 ///
73 /// * `Self` - The TxBuilder instance
74 pub fn mint(&mut self, quantity: i128, policy: &str, name: &str) -> &mut Self {
75 if self.mint_item.is_some() {
76 self.queue_mint();
77 }
78 match &self.adding_plutus_mint {
79 Some(_) => {
80 self.mint_item = Some(MintItem::ScriptMint(ScriptMint {
81 mint: MintParameter {
82 policy_id: policy.to_string(),
83 asset_name: name.to_string(),
84 amount: quantity,
85 },
86 redeemer: None,
87 script_source: None,
88 }))
89 }
90 None => {
91 self.mint_item = Some(MintItem::SimpleScriptMint(SimpleScriptMint {
92 mint: MintParameter {
93 policy_id: policy.to_string(),
94 asset_name: name.to_string(),
95 amount: quantity,
96 },
97 script_source: None,
98 }))
99 }
100 }
101 self
102 }
103
104 /// ## Transaction building method
105 ///
106 /// Add a minting script to the TxBuilder instance
107 ///
108 /// ### Arguments
109 ///
110 /// * `script_cbor` - The script in CBOR format
111 /// * `version` - The language version, if the language version is None, the script is assumed to be a Native Script
112 ///
113 /// ### Returns
114 ///
115 /// * `Self` - The TxBuilder instance
116 pub fn minting_script(&mut self, script_cbor: &str) -> &mut Self {
117 let mint_item = self.mint_item.take();
118 if mint_item.is_none() {
119 panic!("Undefined mint");
120 }
121 let mint_item = mint_item.unwrap();
122 match mint_item {
123 MintItem::ScriptMint(mut script_mint) => {
124 script_mint.script_source =
125 Some(ScriptSource::ProvidedScriptSource(ProvidedScriptSource {
126 script_cbor: script_cbor.to_string(),
127 language_version: self
128 .adding_plutus_mint
129 .clone()
130 .expect("Plutus mints must have version specified"),
131 }));
132 self.adding_plutus_mint = None;
133 self.mint_item = Some(MintItem::ScriptMint(script_mint));
134 }
135 MintItem::SimpleScriptMint(mut simple_script_mint) => {
136 simple_script_mint.script_source = Some(
137 SimpleScriptSource::ProvidedSimpleScriptSource(ProvidedSimpleScriptSource {
138 script_cbor: script_cbor.to_string(),
139 }),
140 );
141 self.mint_item = Some(MintItem::SimpleScriptMint(simple_script_mint));
142 }
143 }
144 self
145 }
146
147 /// ## Transaction building method
148 ///
149 /// Add a minting transaction input reference to the TxBuilder instance
150 ///
151 /// ### Arguments
152 ///
153 /// * `tx_hash` - The transaction hash
154 /// * `tx_index` - The transaction index
155 /// * `script_hash` - The script hash
156 /// * `version` - The language version, if the language version is None, the script is assumed to be a Native Script
157 /// * `script_size` - Size of the script
158 ///
159 /// ### Returns
160 ///
161 /// * `Self` - The TxBuilder instance
162 pub fn mint_tx_in_reference(
163 &mut self,
164 tx_hash: &str,
165 tx_index: u32,
166 script_hash: &str,
167 script_size: usize,
168 ) -> &mut Self {
169 let mint_item = self.mint_item.take();
170 if mint_item.is_none() {
171 panic!("Undefined mint");
172 }
173 let mint_item = mint_item.unwrap();
174 match mint_item {
175 MintItem::ScriptMint(mut script_mint) => {
176 script_mint.script_source =
177 Some(ScriptSource::InlineScriptSource(InlineScriptSource {
178 ref_tx_in: RefTxIn {
179 tx_hash: tx_hash.to_string(),
180 tx_index,
181 // Script size is already accounted for in script source
182 script_size: None,
183 },
184 script_hash: script_hash.to_string(),
185 language_version: self
186 .adding_plutus_mint
187 .clone()
188 .expect("Plutus mints must have version specified"),
189 script_size,
190 }));
191 self.mint_item = Some(MintItem::ScriptMint(script_mint));
192 }
193 MintItem::SimpleScriptMint(mut simple_script_mint) => {
194 simple_script_mint.script_source = Some(
195 SimpleScriptSource::InlineSimpleScriptSource(InlineSimpleScriptSource {
196 ref_tx_in: RefTxIn {
197 tx_hash: tx_hash.to_string(),
198 tx_index,
199 // Script size is already accounted for in script source
200 script_size: None,
201 },
202 simple_script_hash: script_hash.to_string(),
203 script_size,
204 }),
205 );
206 self.mint_item = Some(MintItem::SimpleScriptMint(simple_script_mint));
207 }
208 }
209 self
210 }
211
212 /// ## Transaction building method
213 ///
214 /// Set the minting redeemer value in the TxBuilder instance
215 ///
216 /// ### Arguments
217 ///
218 /// * `redeemer` - The redeemer value
219 ///
220 /// ### Returns
221 ///
222 /// * `Self` - The TxBuilder instance
223 pub fn mint_redeemer_value(&mut self, redeemer: &WRedeemer) -> &mut Self {
224 let mint_item = self.mint_item.take();
225 if mint_item.is_none() {
226 panic!("Undefined mint");
227 }
228 let mint_item = mint_item.unwrap();
229 match mint_item {
230 MintItem::ScriptMint(mut script_mint) => match redeemer.data.to_cbor() {
231 Ok(raw_redeemer) => {
232 script_mint.redeemer = Some(Redeemer {
233 data: raw_redeemer,
234 ex_units: redeemer.clone().ex_units,
235 });
236 self.mint_item = Some(MintItem::ScriptMint(script_mint));
237 }
238 Err(_) => {
239 panic!("Error converting redeemer to CBOR");
240 }
241 },
242 MintItem::SimpleScriptMint(_) => {
243 panic!("Redeemer values cannot be defined for native script mints")
244 }
245 }
246
247 self
248 }
249
250 /// ## Transaction building method
251 ///
252 /// Set the minting reference transaction input redeemer value in the TxBuilder instance
253 ///
254 /// ### Arguments
255 ///
256 /// * `redeemer` - The redeemer value
257 ///
258 /// ### Returns
259 ///
260 /// * `Self` - The TxBuilder instance
261 pub fn mint_reference_tx_in_redeemer_value(&mut self, redeemer: &WRedeemer) -> &mut Self {
262 self.mint_redeemer_value(redeemer)
263 }
264}