Skip to content

Commit

Permalink
Add NativeTokenFeatureDto
Browse files Browse the repository at this point in the history
  • Loading branch information
thibault-martinez committed Nov 17, 2023
1 parent 96d4ddd commit c713100
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion sdk/src/types/block/output/feature/native_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use derive_more::{Deref, From};
use crate::types::block::output::{NativeToken, StorageScore};

#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Deref, From, packable::Packable)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct NativeTokenFeature(NativeToken);

impl NativeTokenFeature {
Expand All @@ -25,3 +24,40 @@ impl NativeTokenFeature {
}

impl StorageScore for NativeTokenFeature {}

#[cfg(feature = "serde")]
mod dto {
use primitive_types::U256;
use serde::{Deserialize, Serialize};

use super::*;
use crate::types::block::{output::TokenId, Error};

#[derive(Serialize, Deserialize)]
struct NativeTokenFeatureDto {
#[serde(rename = "type")]
kind: u8,
token_id: TokenId,
amount: U256,
}

impl From<&NativeTokenFeature> for NativeTokenFeatureDto {
fn from(value: &NativeTokenFeature) -> Self {
Self {
kind: NativeTokenFeature::KIND,
token_id: value.token_id().clone(),
amount: value.amount(),
}
}
}

impl TryFrom<NativeTokenFeatureDto> for NativeTokenFeature {
type Error = Error;

fn try_from(value: NativeTokenFeatureDto) -> Result<Self, Error> {
Ok(Self::new(NativeToken::new(value.token_id, value.amount)?))
}
}

crate::impl_serde_typed_dto!(NativeTokenFeature, NativeTokenFeatureDto, "native token feature");
}

0 comments on commit c713100

Please sign in to comment.