refactor(traverse): Unify mint and output asset artifacts (#231)
This commit is contained in:
parent
d9b73a3651
commit
4a7afe94f0
6 changed files with 124 additions and 143 deletions
|
|
@ -1,110 +1,63 @@
|
||||||
use std::ops::Deref;
|
|
||||||
|
|
||||||
use pallas_codec::utils::{Bytes, KeyValuePairs};
|
|
||||||
use pallas_crypto::hash::Hash;
|
use pallas_crypto::hash::Hash;
|
||||||
use pallas_primitives::{alonzo, babbage};
|
use pallas_primitives::alonzo;
|
||||||
|
|
||||||
use crate::{Asset, MultiEraOutput};
|
use crate::MultiEraAsset;
|
||||||
|
|
||||||
fn iter_policy_assets<'b>(
|
impl<'b> MultiEraAsset<'b> {
|
||||||
policy: &'b Hash<28>,
|
pub fn collect_alonzo_compatible_output(source: &'b alonzo::Multiasset<u64>) -> Vec<Self> {
|
||||||
assets: &'b KeyValuePairs<Bytes, u64>,
|
source
|
||||||
) -> impl Iterator<Item = Asset> + 'b {
|
.iter()
|
||||||
assets
|
.flat_map(|(policy, assets)| {
|
||||||
.iter()
|
assets.iter().map(|(name, amount)| {
|
||||||
.map(|(name, amount)| Asset::NativeAsset(*policy, Vec::<u8>::clone(name), *amount))
|
MultiEraAsset::AlonzoCompatible(policy, name, *amount as i64)
|
||||||
}
|
})
|
||||||
|
})
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
}
|
||||||
|
|
||||||
fn collect_multiassets(multiassets: &alonzo::Multiasset<alonzo::Coin>) -> Vec<Asset> {
|
pub fn collect_alonzo_compatible_mint(source: &'b alonzo::Multiasset<i64>) -> Vec<Self> {
|
||||||
multiassets
|
source
|
||||||
.iter()
|
.iter()
|
||||||
.flat_map(|(p, a)| iter_policy_assets(p, a))
|
.flat_map(|(policy, assets)| {
|
||||||
.collect::<Vec<_>>()
|
assets
|
||||||
}
|
.iter()
|
||||||
|
.map(|(name, amount)| MultiEraAsset::AlonzoCompatible(policy, name, *amount))
|
||||||
|
})
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
}
|
||||||
|
|
||||||
impl Asset {
|
pub fn policy_id(&self) -> Option<&Hash<28>> {
|
||||||
pub fn subject(&self) -> String {
|
|
||||||
match self {
|
match self {
|
||||||
Self::Ada(_) => String::from("ada"),
|
Self::AlonzoCompatible(x, ..) => Some(*x),
|
||||||
Self::NativeAsset(p, n, _) => format!("{p}.{}", hex::encode(n)),
|
Self::Lovelace(_) => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn ascii_name(&self) -> Option<String> {
|
pub fn coin(&self) -> i64 {
|
||||||
match self {
|
match self {
|
||||||
Self::Ada(_) => None,
|
Self::AlonzoCompatible(_, _, x) => *x,
|
||||||
Self::NativeAsset(_, n, _) => String::from_utf8(n.clone()).ok(),
|
Self::Lovelace(x) => *x as i64,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn policy_hex(&self) -> Option<String> {
|
pub fn as_alonzo(&self) -> Option<(&alonzo::PolicyId, &alonzo::AssetName, i64)> {
|
||||||
match self {
|
match self {
|
||||||
Asset::Ada(_) => None,
|
Self::AlonzoCompatible(a, b, c) => Some((*a, *b, *c)),
|
||||||
Asset::NativeAsset(p, _, _) => Some(p.to_string()),
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn to_subject(&self) -> Option<String> {
|
||||||
|
match self {
|
||||||
|
Self::AlonzoCompatible(p, n, _) => Some(format!("{p}.{}", hex::encode(n.to_vec()))),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn to_ascii_name(&self) -> Option<String> {
|
||||||
|
match self {
|
||||||
|
Self::AlonzoCompatible(_, n, _) => String::from_utf8(n.to_vec()).ok(),
|
||||||
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'b> MultiEraOutput<'b> {
|
|
||||||
/// The amount of ADA asset expressed in Lovelace unit
|
|
||||||
///
|
|
||||||
/// The value returned provides the amount of the ADA in a particular
|
|
||||||
/// output. The value is expressed in 'lovelace' (1 ADA = 1,000,000
|
|
||||||
/// lovelace).
|
|
||||||
pub fn lovelace_amount(&self) -> u64 {
|
|
||||||
match self {
|
|
||||||
MultiEraOutput::Byron(x) => x.amount,
|
|
||||||
MultiEraOutput::Babbage(x) => match x.deref().deref() {
|
|
||||||
babbage::MintedTransactionOutput::Legacy(x) => match x.amount {
|
|
||||||
babbage::Value::Coin(c) => c,
|
|
||||||
babbage::Value::Multiasset(c, _) => c,
|
|
||||||
},
|
|
||||||
babbage::MintedTransactionOutput::PostAlonzo(x) => match x.value {
|
|
||||||
babbage::Value::Coin(c) => c,
|
|
||||||
babbage::Value::Multiasset(c, _) => c,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
MultiEraOutput::AlonzoCompatible(x) => match x.amount {
|
|
||||||
alonzo::Value::Coin(c) => c,
|
|
||||||
alonzo::Value::Multiasset(c, _) => c,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// List of native assets in the output
|
|
||||||
///
|
|
||||||
/// Returns a list of Asset structs where each one represent a native asset
|
|
||||||
/// present in the output of the tx. ADA assets are not included in this
|
|
||||||
/// list.
|
|
||||||
pub fn non_ada_assets(&self) -> Vec<Asset> {
|
|
||||||
match self {
|
|
||||||
MultiEraOutput::Byron(_) => vec![],
|
|
||||||
MultiEraOutput::Babbage(x) => match x.deref().deref() {
|
|
||||||
babbage::MintedTransactionOutput::Legacy(x) => match &x.amount {
|
|
||||||
babbage::Value::Coin(_) => vec![],
|
|
||||||
babbage::Value::Multiasset(_, x) => collect_multiassets(x),
|
|
||||||
},
|
|
||||||
babbage::MintedTransactionOutput::PostAlonzo(x) => match &x.value {
|
|
||||||
babbage::Value::Coin(_) => vec![],
|
|
||||||
babbage::Value::Multiasset(_, x) => collect_multiassets(x),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
MultiEraOutput::AlonzoCompatible(x) => match &x.amount {
|
|
||||||
alonzo::Value::Coin(_) => vec![],
|
|
||||||
alonzo::Value::Multiasset(_, x) => collect_multiassets(x),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// List of all assets in the output
|
|
||||||
///
|
|
||||||
/// Returns a list of Asset structs where each one represent either ADA or a
|
|
||||||
/// native asset present in the output of the tx.
|
|
||||||
pub fn assets(&self) -> Vec<Asset> {
|
|
||||||
[
|
|
||||||
vec![Asset::Ada(self.lovelace_amount())],
|
|
||||||
self.non_ada_assets(),
|
|
||||||
]
|
|
||||||
.concat()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
use std::borrow::Cow;
|
use std::{borrow::Cow, ops::Deref};
|
||||||
|
|
||||||
use pallas_codec::minicbor;
|
use pallas_codec::minicbor;
|
||||||
use pallas_crypto::hash::Hash;
|
use pallas_crypto::hash::Hash;
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,6 @@ pub mod hashes;
|
||||||
pub mod header;
|
pub mod header;
|
||||||
pub mod input;
|
pub mod input;
|
||||||
pub mod meta;
|
pub mod meta;
|
||||||
pub mod mint;
|
|
||||||
pub mod output;
|
pub mod output;
|
||||||
pub mod probe;
|
pub mod probe;
|
||||||
pub mod signers;
|
pub mod signers;
|
||||||
|
|
@ -108,10 +107,9 @@ pub enum MultiEraMeta<'b> {
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
#[non_exhaustive]
|
#[non_exhaustive]
|
||||||
pub enum MultiEraMint<'b> {
|
pub enum MultiEraAsset<'b> {
|
||||||
NotApplicable,
|
Lovelace(u64),
|
||||||
Empty,
|
AlonzoCompatible(&'b alonzo::PolicyId, &'b alonzo::AssetName, i64),
|
||||||
AlonzoCompatible(&'b alonzo::Mint),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
|
|
@ -133,12 +131,6 @@ pub enum MultiEraSigners<'b> {
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct OutputRef(Hash<32>, u64);
|
pub struct OutputRef(Hash<32>, u64);
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub enum Asset {
|
|
||||||
Ada(u64),
|
|
||||||
NativeAsset(Hash<28>, Vec<u8>, u64),
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Error)]
|
#[derive(Debug, Error)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
#[error("Invalid CBOR structure: {0}")]
|
#[error("Invalid CBOR structure: {0}")]
|
||||||
|
|
|
||||||
|
|
@ -1,32 +0,0 @@
|
||||||
use pallas_primitives::alonzo;
|
|
||||||
|
|
||||||
use crate::MultiEraMint;
|
|
||||||
|
|
||||||
impl Default for MultiEraMint<'_> {
|
|
||||||
fn default() -> Self {
|
|
||||||
MultiEraMint::Empty
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'b> MultiEraMint<'b> {
|
|
||||||
pub fn len(&self) -> usize {
|
|
||||||
match self {
|
|
||||||
MultiEraMint::AlonzoCompatible(x) => x.len(),
|
|
||||||
_ => 0,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn is_empty(&self) -> bool {
|
|
||||||
match self {
|
|
||||||
MultiEraMint::AlonzoCompatible(x) => x.is_empty(),
|
|
||||||
_ => true,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn as_alonzo(&self) -> Option<&alonzo::Mint> {
|
|
||||||
match self {
|
|
||||||
Self::AlonzoCompatible(x) => Some(x),
|
|
||||||
_ => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -4,7 +4,7 @@ use pallas_addresses::{Address, ByronAddress, Error as AddressError};
|
||||||
use pallas_codec::minicbor;
|
use pallas_codec::minicbor;
|
||||||
use pallas_primitives::{alonzo, babbage, byron};
|
use pallas_primitives::{alonzo, babbage, byron};
|
||||||
|
|
||||||
use crate::{Era, MultiEraOutput};
|
use crate::{Era, MultiEraAsset, MultiEraOutput};
|
||||||
|
|
||||||
impl<'b> MultiEraOutput<'b> {
|
impl<'b> MultiEraOutput<'b> {
|
||||||
pub fn from_byron(output: &'b byron::TxOut) -> Self {
|
pub fn from_byron(output: &'b byron::TxOut) -> Self {
|
||||||
|
|
@ -109,4 +109,72 @@ impl<'b> MultiEraOutput<'b> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The amount of ADA asset expressed in Lovelace unit
|
||||||
|
///
|
||||||
|
/// The value returned provides the amount of the ADA in a particular
|
||||||
|
/// output. The value is expressed in 'lovelace' (1 ADA = 1,000,000
|
||||||
|
/// lovelace).
|
||||||
|
pub fn lovelace_amount(&self) -> u64 {
|
||||||
|
match self {
|
||||||
|
MultiEraOutput::Byron(x) => x.amount,
|
||||||
|
MultiEraOutput::Babbage(x) => match x.deref().deref() {
|
||||||
|
babbage::MintedTransactionOutput::Legacy(x) => match x.amount {
|
||||||
|
babbage::Value::Coin(c) => c,
|
||||||
|
babbage::Value::Multiasset(c, _) => c,
|
||||||
|
},
|
||||||
|
babbage::MintedTransactionOutput::PostAlonzo(x) => match x.value {
|
||||||
|
babbage::Value::Coin(c) => c,
|
||||||
|
babbage::Value::Multiasset(c, _) => c,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
MultiEraOutput::AlonzoCompatible(x) => match x.amount {
|
||||||
|
alonzo::Value::Coin(c) => c,
|
||||||
|
alonzo::Value::Multiasset(c, _) => c,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// List of native assets in the output
|
||||||
|
///
|
||||||
|
/// Returns a list of Asset structs where each one represent a native asset
|
||||||
|
/// present in the output of the tx. ADA assets are not included in this
|
||||||
|
/// list.
|
||||||
|
pub fn non_ada_assets(&self) -> Vec<MultiEraAsset> {
|
||||||
|
match self {
|
||||||
|
MultiEraOutput::Byron(_) => vec![],
|
||||||
|
MultiEraOutput::Babbage(x) => match x.deref().deref() {
|
||||||
|
babbage::MintedTransactionOutput::Legacy(x) => match &x.amount {
|
||||||
|
babbage::Value::Coin(_) => vec![],
|
||||||
|
babbage::Value::Multiasset(_, x) => {
|
||||||
|
MultiEraAsset::collect_alonzo_compatible_output(x)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
babbage::MintedTransactionOutput::PostAlonzo(x) => match &x.value {
|
||||||
|
babbage::Value::Coin(_) => vec![],
|
||||||
|
babbage::Value::Multiasset(_, x) => {
|
||||||
|
MultiEraAsset::collect_alonzo_compatible_output(x)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
MultiEraOutput::AlonzoCompatible(x) => match &x.amount {
|
||||||
|
alonzo::Value::Coin(_) => vec![],
|
||||||
|
alonzo::Value::Multiasset(_, x) => {
|
||||||
|
MultiEraAsset::collect_alonzo_compatible_output(x)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// List of all assets in the output
|
||||||
|
///
|
||||||
|
/// Returns a list of Asset structs where each one represent either ADA or a
|
||||||
|
/// native asset present in the output of the tx.
|
||||||
|
pub fn assets(&self) -> Vec<MultiEraAsset> {
|
||||||
|
[
|
||||||
|
vec![MultiEraAsset::Lovelace(self.lovelace_amount())],
|
||||||
|
self.non_ada_assets(),
|
||||||
|
]
|
||||||
|
.concat()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ use pallas_crypto::hash::Hash;
|
||||||
use pallas_primitives::{alonzo, babbage, byron};
|
use pallas_primitives::{alonzo, babbage, byron};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
Era, MultiEraCert, MultiEraInput, MultiEraMeta, MultiEraMint, MultiEraOutput, MultiEraSigners,
|
Era, MultiEraAsset, MultiEraCert, MultiEraInput, MultiEraMeta, MultiEraOutput, MultiEraSigners,
|
||||||
MultiEraTx, MultiEraWithdrawals, OriginalHash,
|
MultiEraTx, MultiEraWithdrawals, OriginalHash,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -170,21 +170,21 @@ impl<'b> MultiEraTx<'b> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn mint(&self) -> MultiEraMint {
|
pub fn mints(&self) -> Vec<MultiEraAsset> {
|
||||||
match self {
|
match self {
|
||||||
MultiEraTx::AlonzoCompatible(x, _) => x
|
MultiEraTx::AlonzoCompatible(x, _) => x
|
||||||
.transaction_body
|
.transaction_body
|
||||||
.mint
|
.mint
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.map(MultiEraMint::AlonzoCompatible)
|
.map(MultiEraAsset::collect_alonzo_compatible_mint)
|
||||||
.unwrap_or_default(),
|
.unwrap_or_default(),
|
||||||
MultiEraTx::Babbage(x) => x
|
MultiEraTx::Babbage(x) => x
|
||||||
.transaction_body
|
.transaction_body
|
||||||
.mint
|
.mint
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.map(MultiEraMint::AlonzoCompatible)
|
.map(MultiEraAsset::collect_alonzo_compatible_mint)
|
||||||
.unwrap_or_default(),
|
.unwrap_or_default(),
|
||||||
MultiEraTx::Byron(_) => MultiEraMint::NotApplicable,
|
MultiEraTx::Byron(_) => vec![],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue