Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v2.0 : View fixes only for compensation metadata exposure #18

Merged
merged 7 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ pub trait LifeBondingContract:

let compensation_id = self
.compensations_ids()
.insert_new((token_identifier.clone(), nonce));
.get_id_or_insert((token_identifier.clone(), nonce));

self.compensations().insert(compensation_id);

Expand Down
11 changes: 10 additions & 1 deletion src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,16 @@ pub struct Compensation<M: ManagedTypeApi> {
}

#[derive(
TopEncode, TopDecode, NestedEncode, NestedDecode, TypeAbi, Clone, PartialEq, Eq, Debug,
TopEncode,
TopDecode,
NestedEncode,
NestedDecode,
TypeAbi,
Clone,
PartialEq,
Eq,
Debug,
ManagedVecItem,
)]
pub struct Refund<M: ManagedTypeApi> {
pub address: ManagedAddress<M>,
Expand Down
37 changes: 23 additions & 14 deletions src/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,30 +76,39 @@ pub trait ViewsModule:
address: ManagedAddress,
token_identifier: TokenIdentifier,
nonce: u64,
) -> Option<(Compensation<Self::Api>, Option<Refund<Self::Api>>)> {
) -> Option<Refund<Self::Api>> {
let compensation_id = self.compensations_ids().get_id((token_identifier, nonce));
if compensation_id == 0 {
None
} else {
let compensation = Compensation {
compensation_id,
token_identifier: self.compensation_token_identifer(compensation_id).get(),
nonce: self.compensation_nonce(compensation_id).get(),
accumulated_amount: self.compensation_accumulated_amount(compensation_id).get(),
proof_amount: self.compensation_proof_amount(compensation_id).get(),
end_date: self.compensation_end_date(compensation_id).get(),
};

let refund = self.address_refund(&address, compensation_id).get();

if self.address_refund(&address, compensation_id).is_empty() {
Some((compensation, None))
None
} else {
Some((compensation, Some(refund)))
self.address_refund(&address, compensation_id).get();
let refund = self.address_refund(&address, compensation_id).get();
Some(refund)
}
}
}

#[view(getAddressRefundForCompensations)]
fn get_address_refund_for_compensations(
&self,
address: ManagedAddress,
compensation_ids: MultiValueEncoded<u64>,
) -> ManagedVec<Refund<Self::Api>> {
compensation_ids
.into_iter()
.filter_map(|compensation_id| {
if self.address_refund(&address, compensation_id).is_empty() {
None
} else {
Some(self.address_refund(&address, compensation_id).get())
}
})
.collect::<ManagedVec<Refund<Self::Api>>>()
}

#[view(getBondsByTokenIdentifierNonce)]
fn get_bonds_by_token_identifier_nonce(
&self,
Expand Down
54 changes: 42 additions & 12 deletions tests/endpoints/proof.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use core_mx_life_bonding_sc::{
storage::{Compensation, PenaltyType, Refund},
storage::{PenaltyType, Refund},
views::ProxyTrait,
};
use multiversx_sc::{codec::multi_types::OptionalValue, types::EsdtTokenPayment};
use multiversx_sc::{
codec::multi_types::OptionalValue,
types::{EsdtTokenPayment, ManagedVec, MultiValueEncoded},
};
use multiversx_sc_scenario::{
managed_address, managed_token_id,
scenario_model::{
Expand Down Expand Up @@ -118,6 +121,29 @@ fn proof_test() {
.world
.set_state_step(SetStateStep::new().block_timestamp(12u64));

let mut multiValue = MultiValueEncoded::new();

multiValue.push(1u64);

state.world.sc_query(
ScQueryStep::new()
.call(state.contract.get_address_refund_for_compensations(
managed_address!(&first_user_address),
multiValue.clone(),
))
.expect_value(ManagedVec::new()),
);

state.world.sc_query(
ScQueryStep::new()
.call(state.contract.get_address_refund_for_compensation(
managed_address!(&first_user_address),
managed_token_id!(DATA_NFT_IDENTIFIER),
1u64,
))
.expect_value(None),
);

state.proof(
FIRST_USER_ADDRESS_EXPR,
DATA_NFT_IDENTIFIER,
Expand All @@ -138,15 +164,6 @@ fn proof_test() {
),
));

let compensation = Compensation {
compensation_id: 1u64,
token_identifier: managed_token_id!(DATA_NFT_IDENTIFIER),
nonce: 1u64,
accumulated_amount: 100u64.into(),
proof_amount: 2u64.into(),
end_date: 12u64,
};

let refund = Refund {
address: managed_address!(&first_user_address),
proof_of_refund: EsdtTokenPayment {
Expand All @@ -164,6 +181,19 @@ fn proof_test() {
managed_token_id!(DATA_NFT_IDENTIFIER),
1u64,
))
.expect_value(Some((compensation, Some(refund)))),
.expect_value(Some(refund.clone())),
);

let mut managedVec = ManagedVec::new();

managedVec.push(refund.clone());

state.world.sc_query(
ScQueryStep::new()
.call(state.contract.get_address_refund_for_compensations(
managed_address!(&first_user_address),
multiValue,
))
.expect_value(managedVec),
);
}
5 changes: 3 additions & 2 deletions wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
////////////////////////////////////////////////////

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

#![no_std]
#![allow(internal_features)]
Expand All @@ -32,6 +32,7 @@ multiversx_sc_wasm_adapter::endpoints! {
getCompensations => get_compensations
getPagedCompensations => get_paged_compensations
getAddressRefundForCompensation => get_address_refund_for_compensation
getAddressRefundForCompensations => get_address_refund_for_compensations
getBondsByTokenIdentifierNonce => get_bonds_by_token_identifier_nonce
getBonds => get_bonds
getAddressBonds => get_address_bonds
Expand Down
Loading