use alloc::collections::BTreeMap;
use std::collections::{HashMap, HashSet};
use primitive_types::U256;
use serde::{Deserialize, Serialize};
use crate::types::block::output::{AccountId, DelegationId, FoundryId, NativeToken, NftId, TokenId};
#[derive(Debug, Default, Clone, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Burn {
#[serde(default)]
pub(crate) mana: bool,
#[serde(default)]
pub(crate) generated_mana: bool,
#[serde(default, skip_serializing_if = "HashSet::is_empty")]
pub(crate) accounts: HashSet<AccountId>,
#[serde(default, skip_serializing_if = "HashSet::is_empty")]
pub(crate) foundries: HashSet<FoundryId>,
#[serde(default, skip_serializing_if = "HashSet::is_empty")]
pub(crate) nfts: HashSet<NftId>,
#[serde(default, skip_serializing_if = "HashSet::is_empty")]
pub(crate) delegations: HashSet<DelegationId>,
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub(crate) native_tokens: BTreeMap<TokenId, U256>,
}
impl Burn {
pub fn new() -> Self {
Self::default()
}
pub fn set_mana(mut self, burn_mana: bool) -> Self {
self.mana = burn_mana;
self
}
pub fn mana(&self) -> bool {
self.mana
}
pub fn set_generated_mana(mut self, burn_generated_mana: bool) -> Self {
self.generated_mana = burn_generated_mana;
self
}
pub fn generated_mana(&self) -> bool {
self.generated_mana
}
pub fn add_account(mut self, account_id: AccountId) -> Self {
self.accounts.insert(account_id);
self
}
pub fn set_accounts(mut self, accounts: HashSet<AccountId>) -> Self {
self.accounts = accounts;
self
}
pub fn accounts(&self) -> &HashSet<AccountId> {
&self.accounts
}
pub fn add_foundry(mut self, foundry_id: FoundryId) -> Self {
self.foundries.insert(foundry_id);
self
}
pub fn set_foundries(mut self, foundries: HashSet<FoundryId>) -> Self {
self.foundries = foundries;
self
}
pub fn foundries(&self) -> &HashSet<FoundryId> {
&self.foundries
}
pub fn add_nft(mut self, nft_id: NftId) -> Self {
self.nfts.insert(nft_id);
self
}
pub fn set_nfts(mut self, nfts: HashSet<NftId>) -> Self {
self.nfts = nfts;
self
}
pub fn nfts(&self) -> &HashSet<NftId> {
&self.nfts
}
pub fn add_delegation(mut self, delegation_id: DelegationId) -> Self {
self.delegations.insert(delegation_id);
self
}
pub fn set_delegation(mut self, delegations: HashSet<DelegationId>) -> Self {
self.delegations = delegations;
self
}
pub fn delegations(&self) -> &HashSet<DelegationId> {
&self.delegations
}
pub fn add_native_token(mut self, token_id: TokenId, amount: impl Into<U256>) -> Self {
self.native_tokens.insert(token_id, amount.into());
self
}
pub fn set_native_tokens(mut self, native_tokens: HashMap<TokenId, impl Into<U256>>) -> Self {
self.native_tokens = native_tokens
.into_iter()
.map(|(token_id, amount)| (token_id, amount.into()))
.collect();
self
}
pub fn native_tokens(&self) -> &BTreeMap<TokenId, U256> {
&self.native_tokens
}
}
impl From<AccountId> for Burn {
fn from(id: AccountId) -> Self {
Self::new().add_account(id)
}
}
impl From<FoundryId> for Burn {
fn from(id: FoundryId) -> Self {
Self::new().add_foundry(id)
}
}
impl From<NftId> for Burn {
fn from(id: NftId) -> Self {
Self::new().add_nft(id)
}
}
impl From<DelegationId> for Burn {
fn from(id: DelegationId) -> Self {
Self::new().add_delegation(id)
}
}
impl From<NativeToken> for Burn {
fn from(native_token: NativeToken) -> Self {
Self::new().add_native_token(*native_token.token_id(), native_token.amount())
}
}