Skip to content

Commit

Permalink
refactor: remove comments
Browse files Browse the repository at this point in the history
Refs: #5
  • Loading branch information
bucurdavid committed Mar 15, 2024
1 parent d80d8d3 commit 5527f06
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 43 deletions.
2 changes: 0 additions & 2 deletions src/events.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// [TO DO] implement events for endpoints

use multiversx_sc::types::MultiValueEncoded;

use crate::{
Expand Down
47 changes: 14 additions & 33 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub trait LifeBondingContract:
original_caller: ManagedAddress,
token_identifier: TokenIdentifier,
nonce: u64,
lock_period: u64, //seconds
lock_period_seconds: u64,
) {
require_contract_ready!(self, ERR_CONTRACT_NOT_READY);
let caller = self.blockchain().get_caller();
Expand All @@ -85,36 +85,27 @@ pub trait LifeBondingContract:
ERR_INVALID_TOKEN_IDENTIFIER
);

// check token_identifier is accepted (not really needed as this endpoint will be called by the minting contract)

require!(
self.lock_periods().contains(&lock_period),
self.lock_periods().contains(&lock_period_seconds),
ERR_INVALID_LOCK_PERIOD
);
require!(
!self.lock_period_bond_amount(lock_period).is_empty(),
!self.lock_period_bond_amount(lock_period_seconds).is_empty(),
ERR_INVALID_LOCK_PERIOD
); // check not really needed
);

let bond_amount = self.lock_period_bond_amount(lock_period).get();
let bond_amount = self.lock_period_bond_amount(lock_period_seconds).get();

require!(payment.amount == bond_amount, ERR_INVALID_AMOUNT);

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));

// require!(
// !self.bonds_ids().contains_id(check_bond_id),
// ERR_BOND_ALREADY_CREATED
// );
let unbound_timestamp = current_timestamp + lock_period_seconds;

self.bond_address(bond_id).set(original_caller.clone());
self.bond_token_identifier(bond_id)
.set(token_identifier.clone());
self.bond_nonce(bond_id).set(nonce);
self.bond_lock_period(bond_id).set(lock_period);
self.bond_lock_period(bond_id).set(lock_period_seconds);
self.bond_timestamp(bond_id).set(current_timestamp);
self.unbound_timestamp(bond_id).set(unbound_timestamp);
self.bond_amount(bond_id).set(payment.amount.clone());
Expand Down Expand Up @@ -185,10 +176,8 @@ pub trait LifeBondingContract:
0u64,
&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); // remove from showing if it's withdrawn

self.compensations().swap_remove(&compensation_id);
}

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

// 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
self.bonds().swap_remove(&bond_id);
}

#[endpoint(renew)]
Expand Down Expand Up @@ -281,7 +266,7 @@ pub trait LifeBondingContract:
let current_timestamp = self.blockchain().get_block_timestamp();

require!(
current_timestamp > compensation_cache.end_date + COMPENSATION_SAFE_PERIOD, // 86_400 seconds safe period for black list to be uploaded
current_timestamp > compensation_cache.end_date + COMPENSATION_SAFE_PERIOD,
ERR_INVALID_TIMELINE_TO_REFUND
);

Expand All @@ -297,7 +282,7 @@ pub trait LifeBondingContract:
let address_refund = self.address_refund(&caller, compensation_id).get();

self.send()
.direct_non_zero_esdt_payment(&caller, &address_refund.proof_of_refund); // sending back the nfts
.direct_non_zero_esdt_payment(&caller, &address_refund.proof_of_refund);

compensation_cache.proof_amount -= &address_refund.proof_of_refund.amount;
self.compensation_blacklist(compensation_id)
Expand Down Expand Up @@ -351,14 +336,10 @@ pub trait LifeBondingContract:
self.address_refund(&caller, compensation_id).clear();
}

if compensation_cache.accumulated_amount == BigUint::zero() // remove compensation if there is no more accumulated amount
if compensation_cache.accumulated_amount == BigUint::zero()
&& compensation_cache.proof_amount == BigUint::zero()
{
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);
self.compensations().swap_remove(&compensation_id);
}
}
}
14 changes: 6 additions & 8 deletions src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,27 +97,27 @@ pub trait StorageModule {

#[view(getBondPaymentToken)]
#[storage_mapper("bond_payment_token")]
fn bond_payment_token(&self) -> SingleValueMapper<TokenIdentifier>; // bonding token
fn bond_payment_token(&self) -> SingleValueMapper<TokenIdentifier>;

#[view(getLockPeriods)]
#[storage_mapper("lock_periods")]
fn lock_periods(&self) -> SetMapper<u64>; // list of lock periods in days // max_value = 65535 ~ 179 years
fn lock_periods(&self) -> SetMapper<u64>;

#[view(getLockPeriodBondAmount)]
#[storage_mapper("lock_period_bond_amount")]
fn lock_period_bond_amount(&self, lock_period: u64) -> SingleValueMapper<BigUint>; // bonds based on lock_period if 0 then period not accepted
fn lock_period_bond_amount(&self, lock_period: u64) -> SingleValueMapper<BigUint>;

#[view(getMinimumPenalty)]
#[storage_mapper("minimum_penalty")]
fn minimum_penalty(&self) -> SingleValueMapper<u64>; // percentage
fn minimum_penalty(&self) -> SingleValueMapper<u64>;

#[view(getMaximumPenalty)]
#[storage_mapper("maximum_penalty")]
fn maximum_penalty(&self) -> SingleValueMapper<u64>; // percentage 100% = 10_000
fn maximum_penalty(&self) -> SingleValueMapper<u64>;

#[view(getWithdrawPenalty)]
#[storage_mapper("withdraw_penalty")]
fn withdraw_penalty(&self) -> SingleValueMapper<u64>; // percentage
fn withdraw_penalty(&self) -> SingleValueMapper<u64>;

#[storage_mapper("compensation_token_identifer")]
fn compensation_token_identifer(
Expand All @@ -141,7 +141,6 @@ pub trait StorageModule {
#[storage_mapper("refund_blacklist")]
fn compensation_blacklist(&self, compensation_id: u64) -> UnorderedSetMapper<ManagedAddress>;

// do not use view annotation
#[storage_mapper("compensations_ids")]
fn compensations_ids(&self) -> ObjectToIdMapper<Self::Api, (TokenIdentifier, u64)>;

Expand Down Expand Up @@ -172,7 +171,6 @@ pub trait StorageModule {
#[storage_mapper("remaining_amount")]
fn remaining_amount(&self, bond_id: u64) -> SingleValueMapper<BigUint>;

// do not use view annotation
#[storage_mapper("bonds_ids")]
fn bonds_ids(&self) -> ObjectToIdMapper<Self::Api, (TokenIdentifier, u64)>;

Expand Down

0 comments on commit 5527f06

Please sign in to comment.