Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: creator can withdraw and bond again #6

Merged
merged 2 commits into from
Mar 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 30 additions & 26 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ use crate::{
config::COMPENSATION_SAFE_PERIOD,
contexts::{bond_cache::BondCache, compensation_cache::CompensationCache},
errors::{
ERR_BOND_ALREADY_CREATED, ERR_BOND_NOT_FOUND, ERR_CONTRACT_NOT_READY,
ERR_ENDPOINT_CALLABLE_ONLY_BY_ACCEPTED_CALLERS, ERR_INVALID_AMOUNT,
ERR_INVALID_LOCK_PERIOD, ERR_INVALID_TIMELINE_TO_PROOF, ERR_INVALID_TIMELINE_TO_REFUND,
ERR_INVALID_TOKEN_IDENTIFIER, ERR_PENALTIES_EXCEED_WITHDRAWAL_AMOUNT, ERR_REFUND_NOT_FOUND,
ERR_BOND_NOT_FOUND, ERR_CONTRACT_NOT_READY, ERR_ENDPOINT_CALLABLE_ONLY_BY_ACCEPTED_CALLERS,
ERR_INVALID_AMOUNT, ERR_INVALID_LOCK_PERIOD, ERR_INVALID_TIMELINE_TO_PROOF,
ERR_INVALID_TIMELINE_TO_REFUND, ERR_INVALID_TOKEN_IDENTIFIER,
ERR_PENALTIES_EXCEED_WITHDRAWAL_AMOUNT, ERR_REFUND_NOT_FOUND,
},
storage::Refund,
};
Expand Down Expand Up @@ -63,13 +63,18 @@ pub trait LifeBondingContract:
) {
require_contract_ready!(self, ERR_CONTRACT_NOT_READY);
let caller = self.blockchain().get_caller();

let bond_id = self
.bonds_ids()
.get_id_or_insert((token_identifier.clone(), nonce));
require!(
self.blockchain()
.is_smart_contract(&self.blockchain().get_caller())
&& self
.accepted_callers()
.contains(&self.blockchain().get_caller())
|| self.is_privileged(&caller),
|| self.is_privileged(&caller)
|| self.address_bonds(&caller).contains(&bond_id),
ERR_ENDPOINT_CALLABLE_ONLY_BY_ACCEPTED_CALLERS
);

Expand Down Expand Up @@ -98,16 +103,12 @@ pub trait LifeBondingContract:
let current_timestamp = self.blockchain().get_block_timestamp();
let unbound_timestamp = current_timestamp + lock_period;

let check_bond_id = self.bonds_ids().get_id((token_identifier.clone(), nonce));
// let check_bond_id = self.bonds_ids().get_id((token_identifier.clone(), nonce));

require!(
!self.bonds_ids().contains_id(check_bond_id),
ERR_BOND_ALREADY_CREATED
);

let bond_id = self
.bonds_ids()
.insert_new((token_identifier.clone(), nonce));
// require!(
// !self.bonds_ids().contains_id(check_bond_id),
// ERR_BOND_ALREADY_CREATED
// );

self.bond_address(bond_id).set(original_caller.clone());
self.bond_token_identifier(bond_id)
Expand Down Expand Up @@ -153,7 +154,7 @@ pub trait LifeBondingContract:
.compensations_ids()
.get_id_non_zero((token_identifier, nonce));

let mut bond_cache = BondCache::new(self, bond_id);
let bond_cache = BondCache::new(self, bond_id);

let current_timestamp = self.blockchain().get_block_timestamp();

Expand Down Expand Up @@ -182,12 +183,12 @@ pub trait LifeBondingContract:
&caller,
&self.bond_payment_token().get(),
0u64,
&bond_cache.bond_amount,
&bond_cache.remaining_amount,
);
// clear compensations as the entire bond is withdrawn
compensation_cache.clear();
self.compensations_ids().remove_by_id(compensation_id);
self.compensations().swap_remove(&compensation_id);
// compensation_cache.clear();
// self.compensations_ids().remove_by_id(compensation_id);
self.compensations().swap_remove(&compensation_id); // remove from showing if it's withdrawn
}

self.withdraw_event(
Expand All @@ -197,10 +198,11 @@ pub trait LifeBondingContract:
&penalty_amount,
);

bond_cache.clear();
self.bonds_ids().remove_by_id(bond_id);
self.address_bonds(&caller).swap_remove(&bond_id);
self.bonds().swap_remove(&bond_id);
// Do not clear bond totally as creator can come and bond/renew again
// bond_cache.clear();
// self.bonds_ids().remove_by_id(bond_id);
// self.address_bonds(&caller).swap_remove(&bond_id);
self.bonds().swap_remove(&bond_id); // remove from showing if it's withdrawn
}

#[endpoint(renew)]
Expand Down Expand Up @@ -352,9 +354,11 @@ pub trait LifeBondingContract:
if compensation_cache.accumulated_amount == BigUint::zero() // remove compensation if there is no more accumulated amount
&& compensation_cache.proof_amount == BigUint::zero()
{
self.compensations().swap_remove(&compensation_id);
compensation_cache.clear();
self.compensations_ids().remove_by_id(compensation_id);
self.compensations().swap_remove(&compensation_id); // remove from showing if it's empty

// Do not clear compensation totally as creator can come and bond/renew again
// compensation_cache.clear();
// self.compensations_ids().remove_by_id(compensation_id);
}
}
}
Loading