diff --git a/src/life_bonding_sc_proxy.rs b/src/life_bonding_sc_proxy.rs index 7b046b8..277f147 100644 --- a/src/life_bonding_sc_proxy.rs +++ b/src/life_bonding_sc_proxy.rs @@ -309,6 +309,22 @@ where .original_result() } + pub fn get_address_bonds_avg_score< + Arg0: ProxyArg>, + Arg1: ProxyArg>, + >( + self, + address: Arg0, + optional_timestamp: Arg1, + ) -> TxTypedCall> { + self.wrapped_tx + .payment(NotPayable) + .raw_call("getAddressBondsAvgScore") + .argument(&address) + .argument(&optional_timestamp) + .original_result() + } + pub fn get_address_bonds_total_value< Arg0: ProxyArg>, >( diff --git a/src/views.rs b/src/views.rs index 7aa7450..1d588c7 100644 --- a/src/views.rs +++ b/src/views.rs @@ -144,6 +144,35 @@ pub trait ViewsModule: .collect::>>() } + #[view(getAddressBondsAvgScore)] + fn get_address_bonds_avg_score( + &self, + address: ManagedAddress, + optional_timestamp: Option, + ) -> BigUint { + let timestamp = optional_timestamp.unwrap_or(self.blockchain().get_block_timestamp()); + let bonds = self.address_bonds(&address); + + // Return zero if there are no bonds + if bonds.is_empty() { + return BigUint::zero(); + } + + // Calculate the total bond score + let total_score: BigUint = bonds.iter().fold(BigUint::zero(), |acc, bond_id| { + let bond = self.get_bond(bond_id); + let bond_score = BigUint::from(timestamp) * BigUint::from(10_000u64) + / BigUint::from(bond.unbond_timestamp); + acc + bond_score + }); + + // Calculate the average bond score + let bond_count = BigUint::from(bonds.len() as u64); + let average_score = total_score / bond_count; + + average_score + } + #[view(getAddressBondsTotalValue)] fn get_address_bonds_total_value(&self, address: ManagedAddress) -> BigUint { let total_value = self diff --git a/wasm/src/lib.rs b/wasm/src/lib.rs index ef715b3..c40f5ad 100644 --- a/wasm/src/lib.rs +++ b/wasm/src/lib.rs @@ -6,9 +6,9 @@ // Init: 1 // Upgrade: 1 -// Endpoints: 49 +// Endpoints: 50 // Async Callback (empty): 1 -// Total number of exported functions: 52 +// Total number of exported functions: 53 #![no_std] @@ -36,6 +36,7 @@ multiversx_sc_wasm_adapter::endpoints! { getBondsByTokenIdentifierNonce => get_bonds_by_token_identifier_nonce getBonds => get_bonds getAddressBonds => get_address_bonds + getAddressBondsAvgScore => get_address_bonds_avg_score getAddressBondsTotalValue => get_address_bonds_total_value getAllBonds => get_all_bonds getPagedBonds => get_paged_bonds