Skip to content

Commit

Permalink
Merge pull request #18 from Itheum/develop
Browse files Browse the repository at this point in the history
v2.0 : View fixes only for compensation metadata exposure
  • Loading branch information
newbreedofgeek authored May 7, 2024
2 parents 3bc0613 + 9ffc404 commit 2f473c5
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 38 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

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

2 changes: 1 addition & 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 = "1.0.0"
version = "2.0.0"
authors = ["Bucur David - Itheum"]
edition = "2021"
publish = false
Expand Down
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ Understanding this document is also easier if one knows how [ESDT token transact

## Itheum deployed contract addresses

| Devnet | Mainnet |
| -------------------------------------------------------------- | ------- |
| erd1qqqqqqqqqqqqqpgq4xqxlq8p8zenrq4f0htgcwjzdlwmrhwdfsxsmavcuq | |
| Devnet | Mainnet |
| -------------------------------------------------------------- | -------------------------------------------------------------- |
| erd1qqqqqqqqqqqqqpgqhlyaj872kyh620zsfew64l2k4djerw2tfsxsmrxlan | erd1qqqqqqqqqqqqqpgq9yfa4vcmtmn55z0e5n84zphf2uuuxxw9c77qgqqwkn |

## Endpoints

Expand All @@ -24,9 +24,15 @@ See `devnet.snippets.sh` for list of available endpoints for user testing.

### Setting up dev environment (project development bootstrap) + how to build (and upgrade)

<<<<<<< HEAD
- Uses `multiversx-sc-* 0.47.5` (In v2.0.0, we used 0.47.8) SDK libs (see Cargo.toml)
- Building requires minimum **mxpy 9.5.1** (In v2.0.0, we used mxpy 9.5.1). Check version using `mxpy --version`
- To build the project, requires minimum Rust version `1.78.0-nightly` (In v2.0.0, we used 1.78.0-nightly). Check your Rust version by running `rustc --version`. To update your Rust, run `rustup update`. To set to nightly run `rustup default nightly`. Note that `mxpy deps install rust --overwrite` also brings in it's own compatible rust version so running `rustup default nightly` might have a higher rust version than what is used via `mxpy deps install rust --overwrite`.
=======
- Uses `multiversx-sc-* 0.47.8` (In v1.0.0, we used 0.47.8) SDK libs (see Cargo.toml)
- Building requires minimum **mxpy 9.5.1** (In v1.0.0, we used mxpy 9.5.1). Check version using `mxpy --version`
- To build the project, requires minimum Rust version `1.78.0-nightly` (In v1.0.0, we used 1.78.0-nightly). Check your Rust version by running `rustc --version`. To update your Rust, run `rustup update`. To set to nightly run `rustup default nightly`. Note that `mxpy deps install rust --overwrite` also brings in it's own compatible rust version so running `rustup default nightly` might have a higher rust version than what is used via `mxpy deps install rust --overwrite`.
>>>>>>> main
- After you make sure you have the minimum Rust version you can then begin development. After you clone repo and before you run build, deploy or run the tests - follow these steps (most likely only needed the 1st time)
- [Upgrades] Note that when we upgrade smart contract, we should again follow the steps below too as lib version may have changed (but for upgrade I skipped the rustup default nightly cmd and did the others)

Expand Down
2 changes: 1 addition & 1 deletion meta/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "core-mx-life-bonding-sc-meta"
version = "1.0.0"
version = "2.0.0"
edition = "2021"
publish = false

Expand Down
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),
);
}
2 changes: 1 addition & 1 deletion wasm/Cargo.lock

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

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 2f473c5

Please sign in to comment.