Skip to content

Commit

Permalink
Merge pull request #264 from Emurgo/cip30-multiasset-largest-first
Browse files Browse the repository at this point in the history
TxBuilder: CIP2 MultiAsset variants added
  • Loading branch information
vsubhuman committed Jan 23, 2022
2 parents b0930b2 + c7ecdd2 commit aa08824
Show file tree
Hide file tree
Showing 3 changed files with 489 additions and 92 deletions.
55 changes: 50 additions & 5 deletions rust/pkg/cardano_serialization_lib.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ declare export function encode_json_str_to_native_script(
schema: number
): NativeScript;

/**
* @param {Transaction} tx
* @param {LinearFee} linear_fee
* @returns {BigNum}
*/
declare export function min_fee(tx: Transaction, linear_fee: LinearFee): BigNum;

/**
* @param {Uint8Array} bytes
* @returns {TransactionMetadatum}
Expand Down Expand Up @@ -336,6 +343,16 @@ declare export var StakeCredKind: {|
declare export var CoinSelectionStrategyCIP2: {|
+LargestFirst: 0, // 0
+RandomImprove: 1, // 1
+LargestFirstMultiAsset: 2, // 2
+RandomImproveMultiAsset: 3, // 3
|};

/**
*/

declare export var StakeCredKind: {|
+Key: 0, // 0
+Script: 1, // 1
|};

/**
Expand Down Expand Up @@ -2620,30 +2637,58 @@ declare export class MultiAsset {
static new(): MultiAsset;

/**
* the number of unique policy IDs in the multiasset
* @returns {number}
*/
len(): number;

/**
* @param {ScriptHash} key
* @param {Assets} value
* set (and replace if it exists) all assets with policy {policy_id} to a copy of {assets}
* @param {ScriptHash} policy_id
* @param {Assets} assets
* @returns {Assets | void}
*/
insert(key: ScriptHash, value: Assets): Assets | void;
insert(policy_id: ScriptHash, assets: Assets): Assets | void;

/**
* @param {ScriptHash} key
* all assets under {policy_id}, if any exist, or else None (undefined in JS)
* @param {ScriptHash} policy_id
* @returns {Assets | void}
*/
get(key: ScriptHash): Assets | void;
get(policy_id: ScriptHash): Assets | void;

/**
* sets the asset {asset_name} to {value} under policy {policy_id}
* returns the previous amount if it was set, or else None (undefined in JS)
* @param {ScriptHash} policy_id
* @param {AssetName} asset_name
* @param {BigNum} value
* @returns {BigNum | void}
*/
set_asset(
policy_id: ScriptHash,
asset_name: AssetName,
value: BigNum
): BigNum | void;

/**
* returns the amount of asset {asset_name} under policy {policy_id}
* If such an asset does not exist, 0 is returned.
* @param {ScriptHash} policy_id
* @param {AssetName} asset_name
* @returns {BigNum}
*/
get_asset(policy_id: ScriptHash, asset_name: AssetName): BigNum;

/**
* returns all policy IDs used by assets in this multiasset
* @returns {ScriptHashes}
*/
keys(): ScriptHashes;

/**
* removes an asset from the list if the result is 0 or less
* does not modify this object, instead the result is returned
* @param {MultiAsset} rhs_ma
* @returns {MultiAsset}
*/
Expand Down
27 changes: 22 additions & 5 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2537,7 +2537,7 @@ pub type PolicyID = ScriptHash;
pub type PolicyIDs = ScriptHashes;

#[wasm_bindgen]
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
#[derive(Clone, Debug, Default, Eq, Ord, PartialEq, PartialOrd)]
pub struct Assets(pub (crate) std::collections::BTreeMap<AssetName, BigNum>);

to_from_bytes!(Assets);
Expand Down Expand Up @@ -2577,23 +2577,40 @@ impl MultiAsset {
Self(std::collections::BTreeMap::new())
}

/// the number of unique policy IDs in the multiasset
pub fn len(&self) -> usize {
self.0.len()
}

pub fn insert(&mut self, key: &PolicyID, value: &Assets) -> Option<Assets> {
self.0.insert(key.clone(), value.clone())
/// set (and replace if it exists) all assets with policy {policy_id} to a copy of {assets}
pub fn insert(&mut self, policy_id: &PolicyID, assets: &Assets) -> Option<Assets> {
self.0.insert(policy_id.clone(), assets.clone())
}

pub fn get(&self, key: &PolicyID) -> Option<Assets> {
self.0.get(key).map(|v| v.clone())
/// all assets under {policy_id}, if any exist, or else None (undefined in JS)
pub fn get(&self, policy_id: &PolicyID) -> Option<Assets> {
self.0.get(policy_id).map(|v| v.clone())
}

/// sets the asset {asset_name} to {value} under policy {policy_id}
/// returns the previous amount if it was set, or else None (undefined in JS)
pub fn set_asset(&mut self, policy_id: &PolicyID, asset_name: &AssetName, value: BigNum) -> Option<BigNum> {
self.0.entry(policy_id.clone()).or_default().insert(asset_name, &value)
}

/// returns the amount of asset {asset_name} under policy {policy_id}
/// If such an asset does not exist, 0 is returned.
pub fn get_asset(&self, policy_id: &PolicyID, asset_name: &AssetName) -> BigNum {
(|| self.0.get(policy_id)?.get(asset_name))().unwrap_or(BigNum::zero())
}

/// returns all policy IDs used by assets in this multiasset
pub fn keys(&self) -> PolicyIDs {
ScriptHashes(self.0.iter().map(|(k, _v)| k.clone()).collect::<Vec<PolicyID>>())
}

/// removes an asset from the list if the result is 0 or less
/// does not modify this object, instead the result is returned
pub fn sub(&self, rhs_ma: &MultiAsset) -> MultiAsset {
let mut lhs_ma = self.clone();
for (policy, assets) in &rhs_ma.0 {
Expand Down
Loading

0 comments on commit aa08824

Please sign in to comment.