Skip to content

Commit

Permalink
chore: updates
Browse files Browse the repository at this point in the history
  • Loading branch information
bucurdavid committed Jul 29, 2024
1 parent 35f7f5a commit 71c6400
Show file tree
Hide file tree
Showing 9 changed files with 364 additions and 335 deletions.
574 changes: 273 additions & 301 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "core-mx-life-bonding-sc"
version = "2.0.0"
version = "3.0.0"
authors = ["Bucur David - Itheum"]
edition = "2021"
publish = false
Expand All @@ -14,6 +14,7 @@ path = "src/lib.rs"
[dependencies.multiversx-sc]
version = "0.51.0"


[dev-dependencies.multiversx-sc-scenario]
version = "0.51.0"

Expand Down
22 changes: 10 additions & 12 deletions src/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,12 @@ pub trait ViewsModule:
let compensation_id = self.compensations_ids().get_id((token_identifier, nonce));
if compensation_id == 0 {
None
} else if self.address_refund(&address, compensation_id).is_empty() {
None
} else {
if self.address_refund(&address, compensation_id).is_empty() {
None
} else {
self.address_refund(&address, compensation_id).get();
let refund = self.address_refund(&address, compensation_id).get();
Some(refund)
}
self.address_refund(&address, compensation_id).get();
let refund = self.address_refund(&address, compensation_id).get();
Some(refund)
}
}

Expand Down Expand Up @@ -172,20 +170,20 @@ pub trait ViewsModule:

// Calculate the average bond score
let bond_count = BigUint::from(bonds.len() as u64);
let average_score = total_score / bond_count;


average_score
total_score / bond_count
}

#[view(getAddressBondsTotalValue)]
fn get_address_bonds_total_value(&self, address: ManagedAddress<Self::Api>) -> BigUint {
let total_value = self

self
.address_bonds(&address)
.into_iter()
.fold(BigUint::zero(), |acc, bond_id| {
acc + self.remaining_amount(bond_id).get()
});
total_value
})
}

#[view(getAllBonds)]
Expand Down
60 changes: 60 additions & 0 deletions tests/bonding_state/bonding_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use multiversx_sc::{
};
use multiversx_sc_scenario::{
api::StaticApi,
imports::MxscPath,
managed_address, managed_biguint, managed_token_id,
num_bigint::BigUint,
scenario_model::{
Expand All @@ -24,6 +25,13 @@ use multiversx_sc_scenario::{
pub const BONDING_CONTRACT_PATH: &str = "mxsc:output/core-mx-life-bonding-sc.msxc.json";
pub const BONDING_CONTRACT_ADDRESS_EXPR: &str = "sc:core-mx-life-bonding-sc";

pub const LIVELINESS_STAKE_OWNER_ADDRESS_EXPR: &str = "address:core-mx-life-bonding-sc";

pub const LIVELINESS_STAKE_PATH: &str =
"mxsc:test_external_contracts/core-mx-life-bonding-sc.mxsc.json";

pub const LIVELINESS_STAKE_CONTRACT_ADDRESS_EXPR: &str = "sc:core-mx-liveliness-stake";

pub const MINTER_CONTRACT_ADDRESS_EXPR: &str = "sc:minter";

pub const OWNER_BONDING_CONTRACT_ADDRESS_EXPR: &str = "address:owner";
Expand Down Expand Up @@ -52,6 +60,12 @@ pub fn world() -> ScenarioWorld {
BONDING_CONTRACT_PATH,
core_mx_life_bonding_sc::ContractBuilder,
);

blockchain.register_contract(
LIVELINESS_STAKE_PATH,
core_mx_liveliness_stake::ContractBuilder,
);

blockchain
}

Expand Down Expand Up @@ -83,6 +97,15 @@ impl ContractState {
1,
BONDING_CONTRACT_ADDRESS_EXPR,
)
.put_account(
LIVELINESS_STAKE_OWNER_ADDRESS_EXPR,
Account::new().nonce(1).balance("1_000"),
)
.new_address(
LIVELINESS_STAKE_OWNER_ADDRESS_EXPR,
1,
LIVELINESS_STAKE_CONTRACT_ADDRESS_EXPR,
)
.put_account(
ADMIN_BONDING_CONTRACT_ADDRESS_EXPR,
Account::new()
Expand Down Expand Up @@ -147,6 +170,7 @@ impl ContractState {
pub fn default_deploy_and_set(&mut self, lock_period: u64, bond_amount: u64) -> &mut Self {
let admin = self.admin.clone();
self.deploy()
.deploy_liveliness_stake()
.set_administrator(OWNER_BONDING_CONTRACT_ADDRESS_EXPR, admin.clone(), None)
.set_accepted_caller(OWNER_BONDING_CONTRACT_ADDRESS_EXPR, admin.clone(), None)
.set_bond_token(
Expand All @@ -159,11 +183,28 @@ impl ContractState {
lock_period,
bond_amount,
None,
)
.set_liveliness_stake_contract(
OWNER_BONDING_CONTRACT_ADDRESS_EXPR,
AddressValue::from(LIVELINESS_STAKE_CONTRACT_ADDRESS_EXPR).to_address(),
None,
);

self
}

pub fn deploy_liveliness_stake(&mut self) -> &mut Self {
let liveliness_stake_contract_code = self.world.code_expression(LIVELINESS_STAKE_PATH);

self.world.sc_deploy(
ScDeployStep::new()
.from(LIVELINESS_STAKE_OWNER_ADDRESS_EXPR)
.code(liveliness_stake_contract_code)
.call(self.contract.init()),
);
self
}

pub fn deploy(&mut self) -> &mut Self {
let bonding_contract_code = self.world.code_expression(BONDING_CONTRACT_PATH);

Expand Down Expand Up @@ -195,6 +236,25 @@ impl ContractState {
self
}

pub fn set_liveliness_stake_contract(
&mut self,
caller: &str,
liveliness_stake: Address,
expect: Option<TxExpect>,
) -> &mut Self {
let tx_expect = expect.unwrap_or(TxExpect::ok());
self.world.sc_call(
ScCallStep::new()
.from(caller)
.call(
self.contract
.set_liveliness_stake_address(managed_address!(&liveliness_stake)),
)
.expect(tx_expect),
);
self
}

pub fn initiate_bond_for_address(
&mut self,
caller: &str,
Expand Down
2 changes: 0 additions & 2 deletions tests/endpoints/proof.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::cell::Ref;

use core_mx_life_bonding_sc::{
storage::{PenaltyType, Refund},
Expand All @@ -11,7 +10,6 @@ use multiversx_sc::{
use multiversx_sc_scenario::{
api::StaticApi,
managed_address, managed_token_id,
multiversx_chain_vm::vm_hooks::StaticApiVMHooksHandler,
scenario_model::{
AddressValue, CheckAccount, CheckStateStep, ScQueryStep, SetStateStep, TransferStep,
TxExpect,
Expand Down
4 changes: 2 additions & 2 deletions tests/endpoints/vault.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use multiversx_sc::{imports::SingleValue, types::BigUint};
use multiversx_sc_scenario::{
imports::{
Account, AddressValue, BytesValue, CheckAccount, CheckStateStep, ScQueryMandos,
Account, AddressValue, BytesValue, CheckAccount, CheckStateStep,
ScQueryStep, SetStateStep, TransferStep, TxExpect,
},
managed_address, managed_token_id,
Expand All @@ -11,7 +11,7 @@ use core_mx_life_bonding_sc::storage::{Bond, ProxyTrait};
use core_mx_life_bonding_sc::views::ProxyTrait as _;

use crate::bonding_state::bonding_state::{
ContractState, BONDING_CONTRACT_ADDRESS_EXPR, DATA_NFT_IDENTIFIER, DATA_NFT_IDENTIFIER_EXPR,
ContractState, DATA_NFT_IDENTIFIER, DATA_NFT_IDENTIFIER_EXPR,
FIRST_USER_ADDRESS_EXPR, ITHEUM_TOKEN_IDENTIFIER, ITHEUM_TOKEN_IDENTIFIER_EXPR,
MINTER_CONTRACT_ADDRESS_EXPR, OWNER_BONDING_CONTRACT_ADDRESS_EXPR,
};
Expand Down
6 changes: 3 additions & 3 deletions tests/mapper_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ fn test_mapper() {

assert_eq!(id, 1u64); // same id as before

assert_eq!(mapper.contains_id(1u64), true);
assert_eq!(mapper.contains_id(2u64), false);
assert!(mapper.contains_id(1u64));
assert!(!mapper.contains_id(2u64));

let (token_identifier_from_storage, nonce_from_storage) = mapper.get_object(1u64).unwrap();

Expand Down Expand Up @@ -62,7 +62,7 @@ fn test_mapper() {

assert_eq!(same_id, 2u64);

assert_eq!(mapper.contains_id(2u64), true);
assert!(mapper.contains_id(2u64));

// remove by id

Expand Down
18 changes: 9 additions & 9 deletions tests/unit_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,45 +15,45 @@ fn bond_contract_ready_test() {

bond_contract.init();

assert_eq!(false, bond_contract.contract_is_ready());
assert!(!bond_contract.contract_is_ready());

bond_contract.administrator().set(managed_address!(
&AddressValue::from("address:admin").to_address()
));

assert_eq!(false, bond_contract.contract_is_ready());
assert!(!bond_contract.contract_is_ready());

bond_contract.contract_state().set(State::Active);

assert_eq!(false, bond_contract.contract_is_ready());
assert!(!bond_contract.contract_is_ready());

bond_contract
.accepted_callers()
.insert(managed_address!(
&AddressValue::from("address:caller").to_address()
));

assert_eq!(false, bond_contract.contract_is_ready());
assert!(!bond_contract.contract_is_ready());

bond_contract
.bond_payment_token()
.set(managed_token_id!(b"TEST-1234"));

assert_eq!(false, bond_contract.contract_is_ready());
assert!(!bond_contract.contract_is_ready());

bond_contract.lock_periods().insert(1);

assert_eq!(true, bond_contract.contract_is_ready());
assert!(bond_contract.contract_is_ready());

bond_contract.lock_periods().remove(&1);

assert_eq!(false, bond_contract.contract_is_ready());
assert!(!bond_contract.contract_is_ready());

bond_contract.lock_periods().insert(1);

assert_eq!(true, bond_contract.contract_is_ready());
assert!(bond_contract.contract_is_ready());

bond_contract.contract_state().clear();

assert_eq!(false, bond_contract.contract_is_ready());
assert!(!bond_contract.contract_is_ready());
}
10 changes: 5 additions & 5 deletions wasm/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 71c6400

Please sign in to comment.