diff --git a/Cargo.lock b/Cargo.lock index a2b7d18..4b132c5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -319,7 +319,7 @@ checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f" [[package]] name = "core-mx-life-bonding-sc" -version = "1.0.0" +version = "2.0.0" dependencies = [ "multiversx-sc", "multiversx-sc-scenario", @@ -327,7 +327,7 @@ dependencies = [ [[package]] name = "core-mx-life-bonding-sc-meta" -version = "1.0.0" +version = "2.0.0" dependencies = [ "core-mx-life-bonding-sc", "multiversx-sc-meta", diff --git a/Cargo.toml b/Cargo.toml index 98f48c6..592b761 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 diff --git a/README.md b/README.md index 80f8f1c..e44062c 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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) diff --git a/meta/Cargo.toml b/meta/Cargo.toml index 51dfaaf..17ba42f 100644 --- a/meta/Cargo.toml +++ b/meta/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "core-mx-life-bonding-sc-meta" -version = "1.0.0" +version = "2.0.0" edition = "2021" publish = false diff --git a/src/lib.rs b/src/lib.rs index 47cb54c..a57fe11 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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); diff --git a/src/storage.rs b/src/storage.rs index b0e4e4f..6983bf6 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -81,7 +81,16 @@ pub struct Compensation { } #[derive( - TopEncode, TopDecode, NestedEncode, NestedDecode, TypeAbi, Clone, PartialEq, Eq, Debug, + TopEncode, + TopDecode, + NestedEncode, + NestedDecode, + TypeAbi, + Clone, + PartialEq, + Eq, + Debug, + ManagedVecItem, )] pub struct Refund { pub address: ManagedAddress, diff --git a/src/views.rs b/src/views.rs index b12f340..5afd276 100644 --- a/src/views.rs +++ b/src/views.rs @@ -76,30 +76,39 @@ pub trait ViewsModule: address: ManagedAddress, token_identifier: TokenIdentifier, nonce: u64, - ) -> Option<(Compensation, Option>)> { + ) -> Option> { 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, + ) -> ManagedVec> { + 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::>>() + } + #[view(getBondsByTokenIdentifierNonce)] fn get_bonds_by_token_identifier_nonce( &self, diff --git a/tests/endpoints/proof.rs b/tests/endpoints/proof.rs index b95d1c7..2bfa7ff 100644 --- a/tests/endpoints/proof.rs +++ b/tests/endpoints/proof.rs @@ -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::{ @@ -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, @@ -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 { @@ -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), ); } diff --git a/wasm/Cargo.lock b/wasm/Cargo.lock index e7d2212..3701001 100644 --- a/wasm/Cargo.lock +++ b/wasm/Cargo.lock @@ -22,7 +22,7 @@ checksum = "ed570934406eb16438a4e976b1b4500774099c13b8cb96eec99f620f05090ddf" [[package]] name = "core-mx-life-bonding-sc" -version = "1.0.0" +version = "2.0.0" dependencies = [ "multiversx-sc", ] diff --git a/wasm/src/lib.rs b/wasm/src/lib.rs index 3e55911..fc51246 100644 --- a/wasm/src/lib.rs +++ b/wasm/src/lib.rs @@ -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)] @@ -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