-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement beneficiary FIP-0029 (#496)
- Loading branch information
Showing
8 changed files
with
1,030 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
use fvm_ipld_encoding::tuple::*; | ||
use fvm_ipld_encoding::Cbor; | ||
use fvm_shared::address::Address; | ||
use fvm_shared::bigint::bigint_ser; | ||
use fvm_shared::clock::ChainEpoch; | ||
use fvm_shared::econ::TokenAmount; | ||
use num_traits::Zero; | ||
use std::ops::Sub; | ||
|
||
#[derive(Debug, PartialEq, Eq, Clone, Serialize_tuple, Deserialize_tuple)] | ||
pub struct BeneficiaryTerm { | ||
/// The total amount the current beneficiary can withdraw. Monotonic, but reset when beneficiary changes. | ||
#[serde(with = "bigint_ser")] | ||
pub quota: TokenAmount, | ||
/// The amount of quota the current beneficiary has already withdrawn | ||
#[serde(with = "bigint_ser")] | ||
pub used_quota: TokenAmount, | ||
/// The epoch at which the beneficiary's rights expire and revert to the owner | ||
pub expiration: ChainEpoch, | ||
} | ||
|
||
impl Cbor for BeneficiaryTerm {} | ||
|
||
impl BeneficiaryTerm { | ||
pub fn default() -> BeneficiaryTerm { | ||
BeneficiaryTerm { | ||
quota: TokenAmount::zero(), | ||
expiration: 0, | ||
used_quota: TokenAmount::zero(), | ||
} | ||
} | ||
|
||
pub fn new( | ||
quota: TokenAmount, | ||
used_quota: TokenAmount, | ||
expiration: ChainEpoch, | ||
) -> BeneficiaryTerm { | ||
BeneficiaryTerm { quota, expiration, used_quota } | ||
} | ||
|
||
/// Get the amount that the beneficiary has not yet withdrawn | ||
/// return 0 when expired | ||
/// return 0 when the usedQuota >= Quota for safe | ||
/// otherwise Return quota-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, PartialEq, Eq, Serialize_tuple, Deserialize_tuple)] | ||
pub struct PendingBeneficiaryChange { | ||
pub new_beneficiary: Address, | ||
#[serde(with = "bigint_ser")] | ||
pub new_quota: TokenAmount, | ||
pub new_expiration: ChainEpoch, | ||
pub approved_by_beneficiary: bool, | ||
pub approved_by_nominee: bool, | ||
} | ||
|
||
impl Cbor for PendingBeneficiaryChange {} | ||
|
||
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, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.