use iota_stardust_sdk::types::block::address::Address;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use serde_with::serde_as;
use crate::{base_types::IotaAddress, stardust::stardust_to_iota_address};
#[serde_as]
#[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq, JsonSchema)]
pub struct ExpirationUnlockCondition {
pub owner: IotaAddress,
pub return_address: IotaAddress,
pub unix_time: u32,
}
impl ExpirationUnlockCondition {
pub(crate) fn new(
owner_address: &Address,
expiration_unlock_condition: &iota_stardust_sdk::types::block::output::unlock_condition::ExpirationUnlockCondition,
) -> anyhow::Result<Self> {
let owner = stardust_to_iota_address(owner_address)?;
let return_address =
stardust_to_iota_address(expiration_unlock_condition.return_address())?;
let unix_time = expiration_unlock_condition.timestamp();
Ok(Self {
owner,
return_address,
unix_time,
})
}
}
#[serde_as]
#[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq, JsonSchema)]
pub struct StorageDepositReturnUnlockCondition {
pub return_address: IotaAddress,
pub return_amount: u64,
}
impl TryFrom<&iota_stardust_sdk::types::block::output::unlock_condition::StorageDepositReturnUnlockCondition>
for StorageDepositReturnUnlockCondition
{
type Error = anyhow::Error;
fn try_from(
unlock: &iota_stardust_sdk::types::block::output::unlock_condition::StorageDepositReturnUnlockCondition,
) -> Result<Self, Self::Error> {
let return_address = unlock.return_address().to_string().parse()?;
let return_amount = unlock.amount();
Ok(Self {
return_address,
return_amount,
})
}
}
#[serde_as]
#[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq, JsonSchema)]
pub struct TimelockUnlockCondition {
pub unix_time: u32,
}
impl From<&iota_stardust_sdk::types::block::output::unlock_condition::TimelockUnlockCondition>
for TimelockUnlockCondition
{
fn from(
unlock: &iota_stardust_sdk::types::block::output::unlock_condition::TimelockUnlockCondition,
) -> Self {
Self {
unix_time: unlock.timestamp(),
}
}
}