use super::account::AccountType;
use super::errors::{AssetError, TokenSymbolError};
use super::utils::serde::{
ByteReader,
ByteWriter,
Deserializable,
DeserializationError,
Serializable,
};
use super::{Felt, Word};
use crate::account::AccountId;
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 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> {
if matches!(key.faucet_id().account_type(), AccountType::FungibleFaucet) {
FungibleAsset::from_key_value(key, value).map(Asset::Fungible)
} else {
NonFungibleAsset::from_key_value(key, value).map(Asset::NonFungible)
}
}
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 faucet_id: AccountId = source.read()?;
match faucet_id.account_type() {
AccountType::FungibleFaucet => {
FungibleAsset::deserialize_with_faucet_id(faucet_id, source).map(Asset::from)
},
AccountType::NonFungibleFaucet => {
NonFungibleAsset::deserialize_with_faucet_id(faucet_id, source).map(Asset::from)
},
other_type => Err(DeserializationError::InvalidValue(format!(
"failed to deserialize asset: expected an account ID prefix of type faucet, found {other_type}"
))),
}
}
}
#[cfg(test)]
mod tests {
use miden_crypto::utils::{Deserializable, Serializable};
use super::{Asset, FungibleAsset, NonFungibleAsset, NonFungibleAssetDetails};
use crate::account::AccountId;
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,
};
#[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]).unwrap();
let non_fungible_asset: Asset = NonFungibleAsset::new(&details).unwrap().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_account_id_is_serialized_first() {
for asset in [FungibleAsset::mock(300), NonFungibleAsset::mock(&[0xaa, 0xbb])] {
let serialized_asset = asset.to_bytes();
let prefix = AccountId::read_from_bytes(&serialized_asset).unwrap();
assert_eq!(prefix, asset.faucet_id());
}
}
}