From 2d9d07aaf43a2343d6adbdf4c496e46721a57a35 Mon Sep 17 00:00:00 2001 From: Bucur David Date: Wed, 3 Jul 2024 17:50:17 +0300 Subject: [PATCH] feat: total bond amount storage + total address bonds view --- proxy/src/lib.rs | 22 ++++++++++++++++++++++ src/admin.rs | 3 +++ src/lib.rs | 3 +++ src/storage.rs | 4 ++++ src/views.rs | 11 +++++++++++ wasm/src/lib.rs | 6 ++++-- 6 files changed, 47 insertions(+), 2 deletions(-) diff --git a/proxy/src/lib.rs b/proxy/src/lib.rs index f753469..c773b3d 100644 --- a/proxy/src/lib.rs +++ b/proxy/src/lib.rs @@ -171,6 +171,15 @@ where .original_result() } + pub fn total_bond_amount( + self, + ) -> TxTypedCall> { + self.wrapped_tx + .payment(NotPayable) + .raw_call("getTotalBondAmount") + .original_result() + } + pub fn get_bond< Arg0: ProxyArg, >( @@ -300,6 +309,19 @@ where .original_result() } + pub fn get_address_bonds_total_value< + Arg0: ProxyArg>, + >( + self, + address: Arg0, + ) -> TxTypedCall> { + self.wrapped_tx + .payment(NotPayable) + .raw_call("getAddressBondsTotalValue") + .argument(&address) + .original_result() + } + pub fn get_all_bonds( self, ) -> TxTypedCall>> { diff --git a/src/admin.rs b/src/admin.rs index a1a8c43..5c9b117 100644 --- a/src/admin.rs +++ b/src/admin.rs @@ -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, diff --git a/src/lib.rs b/src/lib.rs index a57fe11..1494af9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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()); diff --git a/src/storage.rs b/src/storage.rs index 6983bf6..07bcff1 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -169,4 +169,8 @@ pub trait StorageModule { address: &ManagedAddress, compensation_id: u64, ) -> SingleValueMapper>; + + #[view(getTotalBondAmount)] + #[storage_mapper("total_bond_amount")] + fn total_bond_amount(&self) -> SingleValueMapper; } diff --git a/src/views.rs b/src/views.rs index 5afd276..7aa7450 100644 --- a/src/views.rs +++ b/src/views.rs @@ -144,6 +144,17 @@ pub trait ViewsModule: .collect::>>() } + #[view(getAddressBondsTotalValue)] + fn get_address_bonds_total_value(&self, address: ManagedAddress) -> 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> { self.bonds() diff --git a/wasm/src/lib.rs b/wasm/src/lib.rs index 95368c7..ef715b3 100644 --- a/wasm/src/lib.rs +++ b/wasm/src/lib.rs @@ -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] @@ -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 @@ -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