use fvm_ipld_encoding::tuple::*;
use fvm_shared4::address::Address;
use fvm_shared4::clock::ChainEpoch;
use fvm_shared4::econ::TokenAmount;
use num_traits::Zero;
use std::ops::Sub;
#[derive(Debug, PartialEq, Eq, Clone, Serialize_tuple, Deserialize_tuple)]
pub struct BeneficiaryTerm {
pub quota: TokenAmount,
pub used_quota: TokenAmount,
pub expiration: ChainEpoch,
}
impl Default for BeneficiaryTerm {
fn default() -> BeneficiaryTerm {
BeneficiaryTerm {
quota: TokenAmount::zero(),
expiration: 0,
used_quota: TokenAmount::zero(),
}
}
}
impl BeneficiaryTerm {
pub fn new(
quota: TokenAmount,
used_quota: TokenAmount,
expiration: ChainEpoch,
) -> BeneficiaryTerm {
BeneficiaryTerm {
quota,
expiration,
used_quota,
}
}
pub fn available(&self, cur: ChainEpoch) -> TokenAmount {
if self.expiration > cur {
(&self.quota).sub(&self.used_quota).max(TokenAmount::zero())
} else {
TokenAmount::zero()
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize_tuple, Deserialize_tuple)]
pub struct PendingBeneficiaryChange {
pub new_beneficiary: Address,
pub new_quota: TokenAmount,
pub new_expiration: ChainEpoch,
pub approved_by_beneficiary: bool,
pub approved_by_nominee: bool,
}
impl PendingBeneficiaryChange {
pub fn new(
new_beneficiary: Address,
new_quota: TokenAmount,
new_expiration: ChainEpoch,
) -> Self {
PendingBeneficiaryChange {
new_beneficiary,
new_quota,
new_expiration,
approved_by_beneficiary: false,
approved_by_nominee: false,
}
}
}