Skip to content

Commit

Permalink
refactor: storage organization
Browse files Browse the repository at this point in the history
Refs: #5
  • Loading branch information
bucurdavid committed Mar 15, 2024
1 parent 5527f06 commit 0847323
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 52 deletions.
44 changes: 36 additions & 8 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,6 @@ pub trait ConfigModule: storage::StorageModule + events::EventsModule {
self.administrator().set(administrator);
}

#[view(getContractState)]
#[storage_mapper("contract_state")]
fn contract_state(&self) -> SingleValueMapper<State>;

#[view(getAdministrator)]
#[storage_mapper("administrator")]
fn administrator(&self) -> SingleValueMapper<ManagedAddress>;

#[inline]
fn is_contract_owner(&self, address: &ManagedAddress) -> bool {
&(self.blockchain().get_owner_address()) == address
Expand Down Expand Up @@ -88,4 +80,40 @@ pub trait ConfigModule: storage::StorageModule + events::EventsModule {
fn is_state_active(&self, state: State) -> bool {
state == State::Active
}

#[view(getContractState)]
#[storage_mapper("contract_state")]
fn contract_state(&self) -> SingleValueMapper<State>;

#[view(getAdministrator)]
#[storage_mapper("administrator")]
fn administrator(&self) -> SingleValueMapper<ManagedAddress>;

#[view(getAcceptedCallers)]
#[storage_mapper("accepted_callers")]
fn accepted_callers(&self) -> UnorderedSetMapper<ManagedAddress>;

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

#[view(getLockPeriods)]
#[storage_mapper("lock_periods")]
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>;

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

#[view(getMaximumPenalty)]
#[storage_mapper("maximum_penalty")]
fn maximum_penalty(&self) -> SingleValueMapper<u64>;

#[view(getWithdrawPenalty)]
#[storage_mapper("withdraw_penalty")]
fn withdraw_penalty(&self) -> SingleValueMapper<u64>;
}
46 changes: 10 additions & 36 deletions src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,33 +91,12 @@ pub struct Refund<M: ManagedTypeApi> {

#[multiversx_sc::module]
pub trait StorageModule {
#[view(getAcceptedCallers)]
#[storage_mapper("accepted_callers")]
fn accepted_callers(&self) -> UnorderedSetMapper<ManagedAddress>;

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

#[view(getLockPeriods)]
#[storage_mapper("lock_periods")]
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>;

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

#[view(getMaximumPenalty)]
#[storage_mapper("maximum_penalty")]
fn maximum_penalty(&self) -> SingleValueMapper<u64>;
// Compensation storage
#[storage_mapper("compensations_ids")]
fn compensations_ids(&self) -> ObjectToIdMapper<Self::Api, (TokenIdentifier, u64)>;

#[view(getWithdrawPenalty)]
#[storage_mapper("withdraw_penalty")]
fn withdraw_penalty(&self) -> SingleValueMapper<u64>;
#[storage_mapper("compensations_ids")]
fn compensations(&self) -> UnorderedSetMapper<u64>;

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

#[storage_mapper("compensations_ids")]
fn compensations_ids(&self) -> ObjectToIdMapper<Self::Api, (TokenIdentifier, u64)>;
// Bond storage
#[storage_mapper("bonds_ids")]
fn bonds_ids(&self) -> ObjectToIdMapper<Self::Api, (TokenIdentifier, u64)>;

#[storage_mapper("compensations_ids")]
fn compensations(&self) -> UnorderedSetMapper<u64>;
#[storage_mapper("bonds")]
fn bonds(&self) -> UnorderedSetMapper<u64>;

#[storage_mapper("bond_address")]
fn bond_address(&self, bond_id: u64) -> SingleValueMapper<ManagedAddress>;
Expand All @@ -171,15 +151,9 @@ pub trait StorageModule {
#[storage_mapper("remaining_amount")]
fn remaining_amount(&self, bond_id: u64) -> SingleValueMapper<BigUint>;

#[storage_mapper("bonds_ids")]
fn bonds_ids(&self) -> ObjectToIdMapper<Self::Api, (TokenIdentifier, u64)>;

#[storage_mapper("address_bonds")]
fn address_bonds(&self, address: &ManagedAddress) -> UnorderedSetMapper<u64>;

#[storage_mapper("bonds")]
fn bonds(&self) -> UnorderedSetMapper<u64>;

#[storage_mapper("address_refund")]
fn address_refund(
&self,
Expand Down
1 change: 0 additions & 1 deletion tests/unit_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ mod endpoints;

use core_mx_life_bonding_sc::{
config::{ConfigModule, State},
storage::StorageModule,
LifeBondingContract,
};
use multiversx_sc_scenario::{
Expand Down
14 changes: 7 additions & 7 deletions wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,6 @@ multiversx_sc_wasm_adapter::endpoints! {
renew => renew
proof => add_proof
claimRefund => claim_refund
getAcceptedCallers => accepted_callers
getBondPaymentToken => bond_payment_token
getLockPeriods => lock_periods
getLockPeriodBondAmount => lock_period_bond_amount
getMinimumPenalty => minimum_penalty
getMaximumPenalty => maximum_penalty
getWithdrawPenalty => withdraw_penalty
getCompensationBlacklist => compensation_blacklist
getBond => get_bond
getCompensation => get_compensation
Expand Down Expand Up @@ -66,6 +59,13 @@ multiversx_sc_wasm_adapter::endpoints! {
setAdministrator => set_administrator
getContractState => contract_state
getAdministrator => administrator
getAcceptedCallers => accepted_callers
getBondPaymentToken => bond_payment_token
getLockPeriods => lock_periods
getLockPeriodBondAmount => lock_period_bond_amount
getMinimumPenalty => minimum_penalty
getMaximumPenalty => maximum_penalty
getWithdrawPenalty => withdraw_penalty
)
}

Expand Down

0 comments on commit 0847323

Please sign in to comment.