Skip to content

Commit

Permalink
Added tx-builder functions for simpler metadata and mint
Browse files Browse the repository at this point in the history
  • Loading branch information
vsubhuman committed Oct 26, 2021
1 parent 86dd1df commit e5e5eaa
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
2 changes: 1 addition & 1 deletion rust/src/metadata.rs
Expand Up @@ -219,7 +219,7 @@ impl TransactionMetadatum {
}
}

type TransactionMetadatumLabel = BigNum;
pub type TransactionMetadatumLabel = BigNum;

#[wasm_bindgen]
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
Expand Down
48 changes: 48 additions & 0 deletions rust/src/tx_builder.rs
Expand Up @@ -306,6 +306,54 @@ impl TransactionBuilder {
self.auxiliary_data = Some(auxiliary_data.clone())
}

pub fn set_metadata(&mut self, metadata: &GeneralTransactionMetadata) {
if self.auxiliary_data.is_none() {
self.auxiliary_data = Some(AuxiliaryData::new());
}
self.auxiliary_data?.set_metadata(metadata);
}

pub fn add_metadatum(&mut self, key: &TransactionMetadatumLabel, val: &TransactionMetadatum) {
let mut metadata = self.auxiliary_data
.map(|aux| { aux.metadata() })
.unwrap_or(None)
.unwrap_or(GeneralTransactionMetadata::new());
metadata.insert(key, val);
self.set_metadata(&metadata);
}

pub fn add_json_metadatum(&mut self, key: &TransactionMetadatumLabel, val: String) {
self.add_json_metadatum_with_schema(key, val, MetadataJsonSchema::NoConversions);
}

pub fn add_json_metadatum_with_schema(
&mut self,
key: &TransactionMetadatumLabel,
val: String, schema: MetadataJsonSchema,
) {
let metadatum = encode_json_str_to_metadatum(val, schema)?;
self.add_metadatum(key, &metadatum);
}

pub fn set_mint(&mut self, mint: &Mint) {
self.mint = Some(mint.clone());
}

pub fn set_mint_asset(&mut self, key: &PolicyID, value: &MintAssets) {
let mut mint = self.mint.unwrap_or(Mint::new());
mint.insert(key, value);
self.set_mint(&mint);
}

pub fn add_mint_asset(&mut self, key: &PolicyID, name: &AssetName, value: Int) {
let mut asset = self.mint
.map(|m| { m.get(key) })
.unwrap_or(None)
.unwrap_or(MintAssets::new());
asset.insert(name, value);
self.set_mint_asset(key, &asset);
}

pub fn set_prefer_pure_change(&mut self, prefer_pure_change: bool) {
self.prefer_pure_change = prefer_pure_change;
}
Expand Down

0 comments on commit e5e5eaa

Please sign in to comment.