Skip to content

Commit

Permalink
feat: total bond amount storage + total address bonds view
Browse files Browse the repository at this point in the history
  • Loading branch information
bucurdavid committed Jul 3, 2024
1 parent de4fe57 commit 2d9d07a
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 2 deletions.
22 changes: 22 additions & 0 deletions proxy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,15 @@ where
.original_result()
}

pub fn total_bond_amount(
self,
) -> TxTypedCall<Env, From, To, NotPayable, Gas, BigUint<Env::Api>> {
self.wrapped_tx
.payment(NotPayable)
.raw_call("getTotalBondAmount")
.original_result()
}

pub fn get_bond<
Arg0: ProxyArg<u64>,
>(
Expand Down Expand Up @@ -300,6 +309,19 @@ where
.original_result()
}

pub fn get_address_bonds_total_value<
Arg0: ProxyArg<ManagedAddress<Env::Api>>,
>(
self,
address: Arg0,
) -> TxTypedCall<Env, From, To, NotPayable, Gas, BigUint<Env::Api>> {
self.wrapped_tx
.payment(NotPayable)
.raw_call("getAddressBondsTotalValue")
.argument(&address)
.original_result()
}

pub fn get_all_bonds(
self,
) -> TxTypedCall<Env, From, To, NotPayable, Gas, ManagedVec<Env::Api, Bond<Env::Api>>> {
Expand Down
3 changes: 3 additions & 0 deletions src/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ pub trait AdminModule:
ERR_INVALID_PENALTY_VALUE
);

self.total_bond_amount()
.update(|value| *value -= &penalty_amount);

self.sanction_event(
&bond_id,
&compensation_id,
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ pub trait LifeBondingContract:
let current_timestamp = self.blockchain().get_block_timestamp();
let unbond_timestamp = current_timestamp + lock_period_seconds;

self.total_bond_amount()
.update(|value| *value += bond_amount);

self.bond_address(bond_id).set(original_caller.clone());
self.bond_token_identifier(bond_id)
.set(token_identifier.clone());
Expand Down
4 changes: 4 additions & 0 deletions src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,8 @@ pub trait StorageModule {
address: &ManagedAddress,
compensation_id: u64,
) -> SingleValueMapper<Refund<Self::Api>>;

#[view(getTotalBondAmount)]
#[storage_mapper("total_bond_amount")]
fn total_bond_amount(&self) -> SingleValueMapper<BigUint>;
}
11 changes: 11 additions & 0 deletions src/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,17 @@ pub trait ViewsModule:
.collect::<ManagedVec<Bond<Self::Api>>>()
}

#[view(getAddressBondsTotalValue)]
fn get_address_bonds_total_value(&self, address: ManagedAddress<Self::Api>) -> BigUint {
let total_value = self
.address_bonds(&address)
.into_iter()
.fold(BigUint::zero(), |acc, bond_id| {
acc + self.remaining_amount(bond_id).get()
});
total_value
}

#[view(getAllBonds)]
fn get_all_bonds(&self) -> ManagedVec<Bond<Self::Api>> {
self.bonds()
Expand Down
6 changes: 4 additions & 2 deletions wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

// Init: 1
// Upgrade: 1
// Endpoints: 47
// Endpoints: 49
// Async Callback (empty): 1
// Total number of exported functions: 50
// Total number of exported functions: 52

#![no_std]

Expand All @@ -26,6 +26,7 @@ multiversx_sc_wasm_adapter::endpoints! {
proof => add_proof
claimRefund => claim_refund
getCompensationBlacklist => compensation_blacklist
getTotalBondAmount => total_bond_amount
getBond => get_bond
getCompensation => get_compensation
getCompensations => get_compensations
Expand All @@ -35,6 +36,7 @@ multiversx_sc_wasm_adapter::endpoints! {
getBondsByTokenIdentifierNonce => get_bonds_by_token_identifier_nonce
getBonds => get_bonds
getAddressBonds => get_address_bonds
getAddressBondsTotalValue => get_address_bonds_total_value
getAllBonds => get_all_bonds
getPagedBonds => get_paged_bonds
getBondsLen => get_bonds_len
Expand Down

0 comments on commit 2d9d07a

Please sign in to comment.