Skip to content

Commit

Permalink
fix: address refund view
Browse files Browse the repository at this point in the history
  • Loading branch information
bucurdavid committed May 2, 2024
1 parent 2380606 commit 4a902f8
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 28 deletions.
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
23 changes: 12 additions & 11 deletions tests/endpoints/proof.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use core_mx_life_bonding_sc::{
storage::{Compensation, PenaltyType, Refund},
storage::{PenaltyType, Refund},
views::ProxyTrait,
};
use multiversx_sc::{codec::multi_types::OptionalValue, types::EsdtTokenPayment};
Expand Down Expand Up @@ -118,6 +118,16 @@ fn proof_test() {
.world
.set_state_step(SetStateStep::new().block_timestamp(12u64));

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 +148,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 +165,6 @@ fn proof_test() {
managed_token_id!(DATA_NFT_IDENTIFIER),
1u64,
))
.expect_value(Some((compensation, Some(refund)))),
.expect_value(Some(refund)),
);
}
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

0 comments on commit 4a902f8

Please sign in to comment.