Skip to content

Commit

Permalink
feat(objects): Fix Deserializable for Non Fungible Asset
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippGackstatter committed Oct 14, 2024
1 parent 0bbe3e6 commit 2ea5138
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 2 deletions.
39 changes: 39 additions & 0 deletions objects/src/assets/fungible.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,42 @@ impl FungibleAsset {
.map_err(|err| DeserializationError::InvalidValue(err.to_string()))
}
}

// TESTS
// ================================================================================================

#[cfg(test)]
mod tests {
use super::*;
use crate::accounts::account_id::testing::{
ACCOUNT_ID_FUNGIBLE_FAUCET_OFF_CHAIN, ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN,
ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN_1, ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN_2,
ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN_3, ACCOUNT_ID_NON_FUNGIBLE_FAUCET_OFF_CHAIN,
};

#[test]
fn test_fungible_asset_serde() {
for fungible_account_id in [
ACCOUNT_ID_FUNGIBLE_FAUCET_OFF_CHAIN,
ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN,
ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN_1,
ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN_2,
ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN_3,
] {
let account_id = AccountId::try_from(fungible_account_id).unwrap();
let fungible_asset = FungibleAsset::new(account_id, 10).unwrap();
assert_eq!(
fungible_asset,
FungibleAsset::read_from_bytes(&fungible_asset.to_bytes()).unwrap()
);
}

let account_id = AccountId::try_from(ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN_3).unwrap();
let asset = FungibleAsset::new(account_id, 50).unwrap();
let mut asset_bytes = asset.to_bytes();
// Set invalid Faucet ID.
asset_bytes[0..8].copy_from_slice(&ACCOUNT_ID_NON_FUNGIBLE_FAUCET_OFF_CHAIN.to_le_bytes());
let err = FungibleAsset::read_from_bytes(&asset_bytes).unwrap_err();
assert!(matches!(err, DeserializationError::InvalidValue(_)));
}
}
46 changes: 44 additions & 2 deletions objects/src/assets/nonfungible.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,10 @@ impl Serializable for NonFungibleAsset {

impl Deserializable for NonFungibleAsset {
fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
let value: Word = source.read()?;
Self::try_from(value).map_err(|err| DeserializationError::InvalidValue(err.to_string()))
let faucet_id: AccountId = source.read()?;

Self::deserialize_with_account_id(faucet_id, source)
.map_err(|err| DeserializationError::InvalidValue(err.to_string()))
}
}

Expand All @@ -204,6 +206,8 @@ impl NonFungibleAsset {
let hash_2: Felt = source.read()?;
let hash_3: Felt = source.read()?;

// The second felt in the data_hash will be replaced by the faucet id, so we can set it to
// zero here.
NonFungibleAsset::from_parts(faucet_id, [hash_0, Felt::ZERO, hash_2, hash_3])
.map_err(|err| DeserializationError::InvalidValue(err.to_string()))
}
Expand Down Expand Up @@ -244,3 +248,41 @@ impl NonFungibleAssetDetails {
&self.asset_data
}
}

// TESTS
// ================================================================================================

#[cfg(test)]
mod tests {
use super::*;
use crate::accounts::account_id::testing::{
ACCOUNT_ID_FUNGIBLE_FAUCET_OFF_CHAIN, ACCOUNT_ID_NON_FUNGIBLE_FAUCET_OFF_CHAIN,
ACCOUNT_ID_NON_FUNGIBLE_FAUCET_ON_CHAIN, ACCOUNT_ID_NON_FUNGIBLE_FAUCET_ON_CHAIN_1,
};

#[test]
fn test_non_fungible_asset_serde() {
for non_fungible_account_id in [
ACCOUNT_ID_NON_FUNGIBLE_FAUCET_OFF_CHAIN,
ACCOUNT_ID_NON_FUNGIBLE_FAUCET_ON_CHAIN,
ACCOUNT_ID_NON_FUNGIBLE_FAUCET_ON_CHAIN_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 = NonFungibleAsset::new(&details).unwrap();
assert_eq!(
non_fungible_asset,
NonFungibleAsset::read_from_bytes(&non_fungible_asset.to_bytes()).unwrap()
);
}

let account = AccountId::try_from(ACCOUNT_ID_NON_FUNGIBLE_FAUCET_OFF_CHAIN).unwrap();
let details = NonFungibleAssetDetails::new(account, vec![4, 5, 6, 7]).unwrap();
let asset = NonFungibleAsset::new(&details).unwrap();
let mut asset_bytes = asset.to_bytes();
// Set invalid Faucet ID.
asset_bytes[0..8].copy_from_slice(&ACCOUNT_ID_FUNGIBLE_FAUCET_OFF_CHAIN.to_le_bytes());
let err = NonFungibleAsset::read_from_bytes(&asset_bytes).unwrap_err();
assert!(matches!(err, DeserializationError::InvalidValue(_)));
}
}

0 comments on commit 2ea5138

Please sign in to comment.