use super::errors::{AssetError, TokenSymbolError};
use super::utils::serde::{
ByteReader,
ByteWriter,
Deserializable,
DeserializationError,
Serializable,
};
use super::{Felt, Word};
use crate::account::AccountId;
mod asset_amount;
pub use asset_amount::AssetAmount;
mod fungible;
pub use fungible::FungibleAsset;
mod nonfungible;
pub use nonfungible::{NonFungibleAsset, NonFungibleAssetDetails};
mod token_symbol;
pub use token_symbol::TokenSymbol;
mod asset_callbacks;
pub use asset_callbacks::AssetCallbacks;
mod asset_callbacks_flag;
pub use asset_callbacks_flag::AssetCallbackFlag;
mod asset_composition;
pub use asset_composition::AssetComposition;
mod vault;
pub use vault::{AssetId, AssetVault, AssetVaultKey, AssetWitness, PartialVault};
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Asset {
Fungible(FungibleAsset),
NonFungible(NonFungibleAsset),
}
impl Asset {
pub fn from_key_value(key: AssetVaultKey, value: Word) -> Result<Self, AssetError> {
match key.composition() {
AssetComposition::Fungible => {
FungibleAsset::from_key_value(key, value).map(Asset::Fungible)
},
AssetComposition::None => {
NonFungibleAsset::from_key_value(key, value).map(Asset::NonFungible)
},
AssetComposition::Custom => {
Err(AssetError::UnsupportedAssetComposition(AssetComposition::Custom))
},
}
}
pub fn from_key_value_words(key: Word, value: Word) -> Result<Self, AssetError> {
let vault_key = AssetVaultKey::try_from(key)?;
Self::from_key_value(vault_key, value)
}
pub fn with_callbacks(self, callbacks: AssetCallbackFlag) -> Self {
match self {
Asset::Fungible(fungible_asset) => fungible_asset.with_callbacks(callbacks).into(),
Asset::NonFungible(non_fungible_asset) => {
non_fungible_asset.with_callbacks(callbacks).into()
},
}
}
pub fn is_same(&self, other: &Self) -> bool {
self.vault_key() == other.vault_key()
}
pub fn is_fungible(&self) -> bool {
matches!(self, Self::Fungible(_))
}
pub fn is_non_fungible(&self) -> bool {
matches!(self, Self::NonFungible(_))
}
pub fn faucet_id(&self) -> AccountId {
match self {
Self::Fungible(asset) => asset.faucet_id(),
Self::NonFungible(asset) => asset.faucet_id(),
}
}
pub fn vault_key(&self) -> AssetVaultKey {
match self {
Self::Fungible(asset) => asset.vault_key(),
Self::NonFungible(asset) => asset.vault_key(),
}
}
pub fn to_key_word(&self) -> Word {
self.vault_key().to_word()
}
pub fn to_value_word(&self) -> Word {
match self {
Asset::Fungible(fungible_asset) => fungible_asset.to_value_word(),
Asset::NonFungible(non_fungible_asset) => non_fungible_asset.to_value_word(),
}
}
pub fn as_elements(&self) -> [Felt; 8] {
let mut elements = [Felt::ZERO; 8];
elements[0..4].copy_from_slice(self.to_key_word().as_elements());
elements[4..8].copy_from_slice(self.to_value_word().as_elements());
elements
}
pub fn unwrap_fungible(&self) -> FungibleAsset {
match self {
Asset::Fungible(asset) => *asset,
Asset::NonFungible(_) => panic!("the asset is non-fungible"),
}
}
pub fn unwrap_non_fungible(&self) -> NonFungibleAsset {
match self {
Asset::Fungible(_) => panic!("the asset is fungible"),
Asset::NonFungible(asset) => *asset,
}
}
}
impl Serializable for Asset {
fn write_into<W: ByteWriter>(&self, target: &mut W) {
match self {
Asset::Fungible(fungible_asset) => fungible_asset.write_into(target),
Asset::NonFungible(non_fungible_asset) => non_fungible_asset.write_into(target),
}
}
fn get_size_hint(&self) -> usize {
match self {
Asset::Fungible(fungible_asset) => fungible_asset.get_size_hint(),
Asset::NonFungible(non_fungible_asset) => non_fungible_asset.get_size_hint(),
}
}
}
impl Deserializable for Asset {
fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
let composition: AssetComposition = source.read()?;
match composition {
AssetComposition::Fungible => FungibleAsset::deserialize_body(source).map(Asset::from),
AssetComposition::None => NonFungibleAsset::deserialize_body(source).map(Asset::from),
AssetComposition::Custom => Err(DeserializationError::InvalidValue(
"Custom asset composition is not supported".into(),
)),
}
}
}
#[cfg(test)]
mod tests {
use assert_matches::assert_matches;
use miden_core::Word;
use miden_crypto::utils::{Deserializable, Serializable};
use super::{Asset, FungibleAsset, NonFungibleAsset, NonFungibleAssetDetails};
use crate::Felt;
use crate::account::AccountId;
use crate::asset::{AssetCallbackFlag, AssetComposition, AssetId, AssetVaultKey};
use crate::errors::AssetError;
use crate::testing::account_id::{
ACCOUNT_ID_PRIVATE_FUNGIBLE_FAUCET,
ACCOUNT_ID_PRIVATE_NON_FUNGIBLE_FAUCET,
ACCOUNT_ID_PUBLIC_FUNGIBLE_FAUCET,
ACCOUNT_ID_PUBLIC_FUNGIBLE_FAUCET_1,
ACCOUNT_ID_PUBLIC_FUNGIBLE_FAUCET_2,
ACCOUNT_ID_PUBLIC_FUNGIBLE_FAUCET_3,
ACCOUNT_ID_PUBLIC_NON_FUNGIBLE_FAUCET,
ACCOUNT_ID_PUBLIC_NON_FUNGIBLE_FAUCET_1,
};
pub(super) fn asset_metadata(key: AssetVaultKey) -> u8 {
(key.to_word()[2].as_canonical_u64() & AssetVaultKey::METADATA_BYTE_MASK as u64) as u8
}
pub(super) fn set_asset_metadata(key: AssetVaultKey, byte: u8) -> Word {
let mut key = key.to_word();
let raw = key[2].as_canonical_u64();
let new_raw = (raw & !(AssetVaultKey::METADATA_BYTE_MASK as u64)) | byte as u64;
key[2] = Felt::try_from(new_raw).expect("clearing lower bits should produce a valid felt");
key
}
#[test]
fn test_asset_serde() -> anyhow::Result<()> {
for fungible_account_id in [
ACCOUNT_ID_PRIVATE_FUNGIBLE_FAUCET,
ACCOUNT_ID_PUBLIC_FUNGIBLE_FAUCET,
ACCOUNT_ID_PUBLIC_FUNGIBLE_FAUCET_1,
ACCOUNT_ID_PUBLIC_FUNGIBLE_FAUCET_2,
ACCOUNT_ID_PUBLIC_FUNGIBLE_FAUCET_3,
] {
let account_id = AccountId::try_from(fungible_account_id).unwrap();
let fungible_asset: Asset = FungibleAsset::new(account_id, 10).unwrap().into();
assert_eq!(fungible_asset, Asset::read_from_bytes(&fungible_asset.to_bytes()).unwrap());
assert_eq!(
fungible_asset,
Asset::from_key_value_words(
fungible_asset.to_key_word(),
fungible_asset.to_value_word()
)?,
);
}
for non_fungible_account_id in [
ACCOUNT_ID_PRIVATE_NON_FUNGIBLE_FAUCET,
ACCOUNT_ID_PUBLIC_NON_FUNGIBLE_FAUCET,
ACCOUNT_ID_PUBLIC_NON_FUNGIBLE_FAUCET_1,
] {
let account_id = AccountId::try_from(non_fungible_account_id).unwrap();
let details = NonFungibleAssetDetails::new(account_id, vec![1, 2, 3]);
let non_fungible_asset: Asset = NonFungibleAsset::new(&details).into();
assert_eq!(
non_fungible_asset,
Asset::read_from_bytes(&non_fungible_asset.to_bytes()).unwrap()
);
assert_eq!(
non_fungible_asset,
Asset::from_key_value_words(
non_fungible_asset.to_key_word(),
non_fungible_asset.to_value_word()
)?
);
}
Ok(())
}
#[test]
fn test_composition_byte_is_serialized_first() {
let fungible_bytes = FungibleAsset::mock(300).to_bytes();
assert_eq!(fungible_bytes[0], AssetComposition::Fungible.as_u8());
let non_fungible_bytes = NonFungibleAsset::mock(&[0xaa, 0xbb]).to_bytes();
assert_eq!(non_fungible_bytes[0], AssetComposition::None.as_u8());
}
#[test]
fn test_from_key_value_rejects_custom_composition() -> anyhow::Result<()> {
let err = AssetVaultKey::new(
AssetId::default(),
ACCOUNT_ID_PRIVATE_FUNGIBLE_FAUCET.try_into()?,
AssetComposition::Custom,
AssetCallbackFlag::Disabled,
)
.unwrap_err();
assert_matches!(err, AssetError::UnsupportedAssetComposition(AssetComposition::Custom));
Ok(())
}
}