Struct cardano_serialization_lib::AssetNames
source · pub struct AssetNames(_);
Implementations§
source§impl AssetNames
impl AssetNames
pub fn from_bytes(bytes: Vec<u8>) -> Result<AssetNames, DeserializeError>
source§impl AssetNames
impl AssetNames
pub fn from_hex(hex_str: &str) -> Result<AssetNames, DeserializeError>
source§impl AssetNames
impl AssetNames
source§impl AssetNames
impl AssetNames
pub fn new() -> Self
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Examples found in repository?
src/tx_builder.rs (line 1448)
1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507
fn pack_nfts_for_change(
max_value_size: u32,
data_cost: &DataCost,
change_address: &Address,
change_estimator: &Value,
plutus_data: &Option<DataOption>,
script_ref: &Option<ScriptRef>,
) -> Result<Vec<MultiAsset>, JsError> {
// we insert the entire available ADA temporarily here since that could potentially impact the size
// as it could be 1, 2 3 or 4 bytes for Coin.
let mut change_assets: Vec<MultiAsset> = Vec::new();
let mut base_coin = Value::new(&change_estimator.coin());
base_coin.set_multiasset(&MultiAsset::new());
let mut output = TransactionOutput {
address: change_address.clone(),
amount: base_coin.clone(),
plutus_data: plutus_data.clone(),
script_ref: script_ref.clone(),
};
// If this becomes slow on large TXs we can optimize it like the following
// to avoid cloning + reserializing the entire output.
// This would probably be more relevant if we use a smarter packing algorithm
// which might need to compare more size differences than greedy
//let mut bytes_used = output.to_bytes().len();
// a greedy packing is done here to avoid an exponential bin-packing
// which in most cases likely shouldn't be the difference between
// having an extra change output or not unless there are gigantic
// differences in NFT policy sizes
for (policy, assets) in change_estimator.multiasset().unwrap().0.iter() {
// for simplicity we also don't split assets within a single policy since
// you would need to have a very high amoun of assets (which add 1-36 bytes each)
// in a single policy to make a difference. In the future if this becomes an issue
// we can change that here.
// this is the other part of the optimization but we need to take into account
// the difference between CBOR encoding which can change which happens in two places:
// a) length within assets of one policy id
// b) length of the entire multiasset
// so for simplicity we will just do it the safe, naive way unless
// performance becomes an issue.
//let extra_bytes = policy.to_bytes().len() + assets.to_bytes().len() + 2 + cbor_len_diff;
//if bytes_used + extra_bytes <= max_value_size as usize {
let mut old_amount = output.amount.clone();
let mut val = Value::new(&Coin::zero());
let mut next_nft = MultiAsset::new();
let asset_names = assets.keys();
let mut rebuilt_assets = Assets::new();
for n in 0..asset_names.len() {
let asset_name = asset_names.get(n);
let value = assets.get(&asset_name).unwrap();
if will_adding_asset_make_output_overflow(
&output,
&rebuilt_assets,
(policy.clone(), asset_name.clone(), value),
max_value_size,
data_cost,
)? {
// if we got here, this means we will run into a overflow error,
// so we want to split into multiple outputs, for that we...
// 1. insert the current assets as they are, as this won't overflow
next_nft.insert(policy, &rebuilt_assets);
val.set_multiasset(&next_nft);
output.amount = output.amount.checked_add(&val)?;
change_assets.push(output.amount.multiasset().unwrap());
// 2. create a new output with the base coin value as zero
base_coin = Value::new(&Coin::zero());
base_coin.set_multiasset(&MultiAsset::new());
output = TransactionOutput {
address: change_address.clone(),
amount: base_coin.clone(),
plutus_data: plutus_data.clone(),
script_ref: script_ref.clone(),
};
// 3. continue building the new output from the asset we stopped
old_amount = output.amount.clone();
val = Value::new(&Coin::zero());
next_nft = MultiAsset::new();
rebuilt_assets = Assets::new();
}
rebuilt_assets.insert(&asset_name, &value);
}
next_nft.insert(policy, &rebuilt_assets);
val.set_multiasset(&next_nft);
output.amount = output.amount.checked_add(&val)?;
// calculate minADA for more precise max value size
let mut amount_clone = output.amount.clone();
let mut calc = MinOutputAdaCalculator::new_empty(data_cost)?;
calc.set_amount(&val);
let min_ada = calc.calculate_ada()?;
amount_clone.set_coin(&min_ada);
if amount_clone.to_bytes().len() > max_value_size as usize {
output.amount = old_amount;
break;
}
}
change_assets.push(output.amount.multiasset().unwrap());
Ok(change_assets)
}
sourcepub fn get(&self, index: usize) -> AssetName
pub fn get(&self, index: usize) -> AssetName
Examples found in repository?
src/tx_builder.rs (line 1449)
1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507
fn pack_nfts_for_change(
max_value_size: u32,
data_cost: &DataCost,
change_address: &Address,
change_estimator: &Value,
plutus_data: &Option<DataOption>,
script_ref: &Option<ScriptRef>,
) -> Result<Vec<MultiAsset>, JsError> {
// we insert the entire available ADA temporarily here since that could potentially impact the size
// as it could be 1, 2 3 or 4 bytes for Coin.
let mut change_assets: Vec<MultiAsset> = Vec::new();
let mut base_coin = Value::new(&change_estimator.coin());
base_coin.set_multiasset(&MultiAsset::new());
let mut output = TransactionOutput {
address: change_address.clone(),
amount: base_coin.clone(),
plutus_data: plutus_data.clone(),
script_ref: script_ref.clone(),
};
// If this becomes slow on large TXs we can optimize it like the following
// to avoid cloning + reserializing the entire output.
// This would probably be more relevant if we use a smarter packing algorithm
// which might need to compare more size differences than greedy
//let mut bytes_used = output.to_bytes().len();
// a greedy packing is done here to avoid an exponential bin-packing
// which in most cases likely shouldn't be the difference between
// having an extra change output or not unless there are gigantic
// differences in NFT policy sizes
for (policy, assets) in change_estimator.multiasset().unwrap().0.iter() {
// for simplicity we also don't split assets within a single policy since
// you would need to have a very high amoun of assets (which add 1-36 bytes each)
// in a single policy to make a difference. In the future if this becomes an issue
// we can change that here.
// this is the other part of the optimization but we need to take into account
// the difference between CBOR encoding which can change which happens in two places:
// a) length within assets of one policy id
// b) length of the entire multiasset
// so for simplicity we will just do it the safe, naive way unless
// performance becomes an issue.
//let extra_bytes = policy.to_bytes().len() + assets.to_bytes().len() + 2 + cbor_len_diff;
//if bytes_used + extra_bytes <= max_value_size as usize {
let mut old_amount = output.amount.clone();
let mut val = Value::new(&Coin::zero());
let mut next_nft = MultiAsset::new();
let asset_names = assets.keys();
let mut rebuilt_assets = Assets::new();
for n in 0..asset_names.len() {
let asset_name = asset_names.get(n);
let value = assets.get(&asset_name).unwrap();
if will_adding_asset_make_output_overflow(
&output,
&rebuilt_assets,
(policy.clone(), asset_name.clone(), value),
max_value_size,
data_cost,
)? {
// if we got here, this means we will run into a overflow error,
// so we want to split into multiple outputs, for that we...
// 1. insert the current assets as they are, as this won't overflow
next_nft.insert(policy, &rebuilt_assets);
val.set_multiasset(&next_nft);
output.amount = output.amount.checked_add(&val)?;
change_assets.push(output.amount.multiasset().unwrap());
// 2. create a new output with the base coin value as zero
base_coin = Value::new(&Coin::zero());
base_coin.set_multiasset(&MultiAsset::new());
output = TransactionOutput {
address: change_address.clone(),
amount: base_coin.clone(),
plutus_data: plutus_data.clone(),
script_ref: script_ref.clone(),
};
// 3. continue building the new output from the asset we stopped
old_amount = output.amount.clone();
val = Value::new(&Coin::zero());
next_nft = MultiAsset::new();
rebuilt_assets = Assets::new();
}
rebuilt_assets.insert(&asset_name, &value);
}
next_nft.insert(policy, &rebuilt_assets);
val.set_multiasset(&next_nft);
output.amount = output.amount.checked_add(&val)?;
// calculate minADA for more precise max value size
let mut amount_clone = output.amount.clone();
let mut calc = MinOutputAdaCalculator::new_empty(data_cost)?;
calc.set_amount(&val);
let min_ada = calc.calculate_ada()?;
amount_clone.set_coin(&min_ada);
if amount_clone.to_bytes().len() > max_value_size as usize {
output.amount = old_amount;
break;
}
}
change_assets.push(output.amount.multiasset().unwrap());
Ok(change_assets)
}
pub fn add(&mut self, elem: &AssetName)
Trait Implementations§
source§impl Clone for AssetNames
impl Clone for AssetNames
source§fn clone(&self) -> AssetNames
fn clone(&self) -> AssetNames
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moresource§impl Debug for AssetNames
impl Debug for AssetNames
source§impl<'de> Deserialize<'de> for AssetNames
impl<'de> Deserialize<'de> for AssetNames
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
source§impl Deserialize for AssetNames
impl Deserialize for AssetNames
fn deserialize<R: BufRead + Seek>(
raw: &mut Deserializer<R>
) -> Result<Self, DeserializeError>
source§impl JsonSchema for AssetNames
impl JsonSchema for AssetNames
source§fn schema_name() -> String
fn schema_name() -> String
The name of the generated JSON Schema. Read more
source§fn json_schema(gen: &mut SchemaGenerator) -> Schema
fn json_schema(gen: &mut SchemaGenerator) -> Schema
Generates a JSON Schema for this type. Read more
source§fn is_referenceable() -> bool
fn is_referenceable() -> bool
Whether JSON Schemas generated for this type should be re-used where possible using the
$ref
keyword. Read moresource§impl Ord for AssetNames
impl Ord for AssetNames
source§fn cmp(&self, other: &AssetNames) -> Ordering
fn cmp(&self, other: &AssetNames) -> Ordering
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the maximum of two values. Read more
source§impl PartialEq<AssetNames> for AssetNames
impl PartialEq<AssetNames> for AssetNames
source§fn eq(&self, other: &AssetNames) -> bool
fn eq(&self, other: &AssetNames) -> bool
This method tests for
self
and other
values to be equal, and is used
by ==
.source§impl PartialOrd<AssetNames> for AssetNames
impl PartialOrd<AssetNames> for AssetNames
source§fn partial_cmp(&self, other: &AssetNames) -> Option<Ordering>
fn partial_cmp(&self, other: &AssetNames) -> Option<Ordering>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for
self
and other
) and is used by the <=
operator. Read more