Skip to content

Commit ed86ae8

Browse files
committed
Started changing API for mint-scripts
1 parent 94460c2 commit ed86ae8

File tree

1 file changed

+41
-26
lines changed

1 file changed

+41
-26
lines changed

rust/src/tx_builder.rs

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -129,20 +129,32 @@ fn fake_full_tx(tx_builder: &TransactionBuilder, body: TransactionBody) -> Resul
129129
})
130130
}
131131

132-
fn min_fee(tx_builder: &TransactionBuilder) -> Result<Coin, JsError> {
133-
if let Some(mint) = tx_builder.mint.as_ref() {
134-
if let Some(witness_scripts) = tx_builder.mint_scripts.as_ref() {
135-
let witness_hashes: HashSet<ScriptHash> = witness_scripts.0.iter().map(|script| {
136-
script.hash(ScriptHashNamespace::NativeScript)
137-
}).collect();
138-
for mint_hash in mint.keys().0.iter() {
139-
if !witness_hashes.contains(mint_hash) {
140-
return Err(JsError::from_str(&format!("No witness script is found for mint policy '{:?}'! Impossible to estimate fee", hex::encode(mint_hash.to_bytes()))));
141-
}
132+
fn assert_required_mint_scripts(mint: &Mint, maybe_mint_scripts: Option<&NativeScripts>) -> Result<(), JsError> {
133+
if let Some(mint_scripts) = maybe_mint_scripts {
134+
let witness_hashes: HashSet<ScriptHash> = witness_scripts.0.iter().map(|script| {
135+
script.hash(ScriptHashNamespace::NativeScript)
136+
}).collect();
137+
for mint_hash in mint.keys().0.iter() {
138+
if !witness_hashes.contains(mint_hash) {
139+
return Err(JsError::from_str(
140+
&format!(
141+
"No witness script is found for mint policy '{:?}'! Script is required!",
142+
hex::encode(mint_hash.to_bytes()),
143+
))
144+
);
142145
}
143-
} else {
144-
return Err(JsError::from_str("Impossible to estimate fee if mint is present in the builder, but witness scripts are not provided!"));
145146
}
147+
} else {
148+
return Err(JsError::from_str(
149+
"Mint is present in the builder, but witness scripts are not provided!",
150+
));
151+
}
152+
Ok(())
153+
}
154+
155+
fn min_fee(tx_builder: &TransactionBuilder) -> Result<Coin, JsError> {
156+
if let Some(mint) = tx_builder.mint.as_ref() {
157+
assert_required_mint_scripts(mint, tx_builder.mint_scripts.as_ref())?;
146158
}
147159
let full_tx = fake_full_tx(tx_builder, tx_builder.build()?)?;
148160
fees::min_fee(&full_tx, &tx_builder.config.fee_algo)
@@ -654,27 +666,21 @@ impl TransactionBuilder {
654666
Ok(())
655667
}
656668

657-
/// Set explicit Mint object to this builder
658-
/// it will replace any previously existing mint
659-
/// NOTE! If you use `set_mint` manually - you must use `set_mint_scripts`
660-
/// to provide matching policy scripts or min-fee calculation will be rejected!
661-
pub fn set_mint(&mut self, mint: &Mint) {
669+
/// Set explicit Mint object and the required witnesses to this builder
670+
/// it will replace any previously existing mint and mint scripts
671+
/// NOTE! Error will be returned in case a mint policy does not have a matching script
672+
pub fn set_mint(&mut self, mint: &Mint, mint_scripts: &NativeScripts) -> Result<(), JsError> {
673+
assert_required_mint_scripts(mint, Some(mint_scripts))?;
662674
self.mint = Some(mint.clone());
675+
self.mint_scripts = Some(mint_scripts.clone());
676+
Ok(())
663677
}
664678

665679
/// Returns a copy of the current mint state in the builder
666680
pub fn get_mint(&self) -> Option<Mint> {
667681
self.mint.clone()
668682
}
669683

670-
/// Set explicit witness set to this builder
671-
/// It will replace any previously existing witnesses
672-
/// NOTE! Use carefully! If you are using `set_mint` - then you must be using
673-
/// this setter as well to be able to calculate fee automatically!
674-
pub fn set_mint_scripts(&mut self, mint_scripts: &NativeScripts) {
675-
self.mint_scripts = Some(mint_scripts.clone());
676-
}
677-
678684
/// Returns a copy of the current mint witness scripts in the builder
679685
pub fn get_mint_scripts(&self) -> Option<NativeScripts> {
680686
self.mint_scripts.clone()
@@ -683,7 +689,16 @@ impl TransactionBuilder {
683689
fn _set_mint_asset(&mut self, policy_id: &PolicyID, policy_script: &NativeScript, mint_assets: &MintAssets) {
684690
let mut mint = self.mint.as_ref().cloned().unwrap_or(Mint::new());
685691
let is_new_policy = mint.insert(&policy_id, mint_assets).is_none();
686-
self.set_mint(&mint);
692+
let mint_scripts = {
693+
let mut witness_scripts = self.mint_scripts.as_ref().cloned()
694+
.unwrap_or(NativeScripts::new());
695+
if is_new_policy {
696+
// If policy has not been encountered before - insert the script into witnesses
697+
witness_scripts.add(&policy_script.clone());
698+
}
699+
witness_scripts
700+
};
701+
self.set_mint(&mint, &mint_scripts);
687702
if is_new_policy {
688703
// If policy has not been encountered before - insert the script into witnesses
689704
let mut witness_scripts = self.mint_scripts.as_ref().cloned()

0 commit comments

Comments
 (0)