Skip to content

Commit

Permalink
feat: liveliness score view
Browse files Browse the repository at this point in the history
  • Loading branch information
bucurdavid committed Jul 3, 2024
1 parent 1682409 commit 2077984
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
16 changes: 16 additions & 0 deletions src/life_bonding_sc_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,22 @@ where
.original_result()
}

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

pub fn get_address_bonds_total_value<
Arg0: ProxyArg<ManagedAddress<Env::Api>>,
>(
Expand Down
29 changes: 29 additions & 0 deletions src/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,35 @@ pub trait ViewsModule:
.collect::<ManagedVec<Bond<Self::Api>>>()
}

#[view(getAddressBondsAvgScore)]
fn get_address_bonds_avg_score(
&self,
address: ManagedAddress,
optional_timestamp: Option<u64>,
) -> BigUint<Self::Api> {
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<Self::Api>) -> BigUint {
let total_value = self
Expand Down
5 changes: 3 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: 49
// Endpoints: 50
// Async Callback (empty): 1
// Total number of exported functions: 52
// Total number of exported functions: 53

#![no_std]

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2077984

Please sign in to comment.