use std::{
collections::{HashMap, HashSet},
str::FromStr,
};
use serde::{Deserialize, Serialize};
use crate::{
types::block::{
address::Bech32Address,
output::{dto::FoundryOutputDto, FoundryId, OutputId},
payload::transaction::TransactionId,
},
wallet::{
account::{
types::{AccountAddress, AddressWithUnspentOutputs, TransactionDto},
AccountDetails, OutputDataDto,
},
AddressWithAmount,
},
};
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AddressWithAmountDto {
pub address: String,
pub amount: String,
pub return_address: Option<String>,
pub expiration: Option<u32>,
}
impl TryFrom<&AddressWithAmountDto> for AddressWithAmount {
type Error = crate::wallet::Error;
fn try_from(value: &AddressWithAmountDto) -> crate::wallet::Result<Self> {
Ok(Self::new(
value.address.clone(),
u64::from_str(&value.amount).map_err(|_| crate::client::Error::InvalidAmount(value.amount.clone()))?,
)
.with_return_address(value.return_address.clone())
.with_expiration(value.expiration))
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AddressWithUnspentOutputsDto {
pub address: Bech32Address,
pub key_index: u32,
pub internal: bool,
pub output_ids: Vec<OutputId>,
}
impl From<&AddressWithUnspentOutputs> for AddressWithUnspentOutputsDto {
fn from(value: &AddressWithUnspentOutputs) -> Self {
Self {
address: value.address.clone(),
key_index: value.key_index,
internal: value.internal,
output_ids: value.output_ids.clone(),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AccountDetailsDto {
pub index: u32,
pub coin_type: u32,
pub alias: String,
pub public_addresses: Vec<AccountAddress>,
pub internal_addresses: Vec<AccountAddress>,
pub addresses_with_unspent_outputs: Vec<AddressWithUnspentOutputsDto>,
pub outputs: HashMap<OutputId, OutputDataDto>,
pub locked_outputs: HashSet<OutputId>,
pub unspent_outputs: HashMap<OutputId, OutputDataDto>,
pub transactions: HashMap<TransactionId, TransactionDto>,
pub pending_transactions: HashSet<TransactionId>,
pub incoming_transactions: HashMap<TransactionId, TransactionDto>,
#[serde(default)]
pub native_token_foundries: HashMap<FoundryId, FoundryOutputDto>,
}
impl From<&AccountDetails> for AccountDetailsDto {
fn from(value: &AccountDetails) -> Self {
Self {
index: *value.index(),
coin_type: *value.coin_type(),
alias: value.alias().clone(),
public_addresses: value.public_addresses.clone(),
internal_addresses: value.internal_addresses.clone(),
addresses_with_unspent_outputs: value
.addresses_with_unspent_outputs()
.iter()
.map(AddressWithUnspentOutputsDto::from)
.collect(),
outputs: value
.outputs()
.iter()
.map(|(id, output)| (*id, OutputDataDto::from(output)))
.collect(),
locked_outputs: value.locked_outputs().clone(),
unspent_outputs: value
.unspent_outputs()
.iter()
.map(|(id, output)| (*id, OutputDataDto::from(output)))
.collect(),
transactions: value
.transactions()
.iter()
.map(|(id, transaction)| (*id, TransactionDto::from(transaction)))
.collect(),
pending_transactions: value.pending_transactions().clone(),
incoming_transactions: value
.incoming_transactions()
.iter()
.map(|(id, transaction)| (*id, TransactionDto::from(transaction)))
.collect(),
native_token_foundries: value
.native_token_foundries()
.iter()
.map(|(id, foundry)| (*id, FoundryOutputDto::from(foundry)))
.collect(),
}
}
}