Skip to content

Commit

Permalink
feat: max quantity per address per offer
Browse files Browse the repository at this point in the history
Refs: #45
  • Loading branch information
bucurdavid committed Apr 17, 2024
1 parent 48ebb41 commit c5ccc2a
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,8 +383,13 @@ pub trait DataMarket:

let mut offer = self.try_get_offer(offer_id);

let address_limit = self.max_quantity_per_address(&offer.owner, offer_id).get();

if offer.max_quantity > BigUint::zero() {
require!(quantity <= offer.max_quantity, ERR_MAX_QUANTITY_EXCEEDED);
require!(
&quantity + &address_limit <= offer.max_quantity,
ERR_MAX_QUANTITY_EXCEEDED
);
}

let payment = self.call_value().egld_or_single_esdt();
Expand Down
9 changes: 9 additions & 0 deletions src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ pub struct OfferOut<M: ManagedTypeApi> {
pub wanted_token_nonce: u64,
pub wanted_token_amount: BigUint<M>,
pub quantity: BigUint<M>,
pub max_quantity_per_address: BigUint<M>,
}

#[derive(TopEncode, TopDecode, NestedEncode, NestedDecode, PartialEq, Clone, Debug, TypeAbi)]
Expand Down Expand Up @@ -191,6 +192,14 @@ pub trait StorageModule {
#[storage_mapper("max_default_quantity")]
fn max_default_quantity(&self) -> SingleValueMapper<BigUint>;

#[view(getMaxQuantityPerAddress)]
#[storage_mapper("max_quantity_per_address")]
fn max_quantity_per_address(
&self,
address: &ManagedAddress,
offer_id: u64,
) -> SingleValueMapper<BigUint>;

#[view(getRoyaltiesClaimToken)]
#[storage_mapper("royalties_accepted_token")]
fn royalties_claim_token(&self) -> SingleValueMapper<TokenIdentifier>;
Expand Down
1 change: 1 addition & 0 deletions src/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ pub trait ViewsModule: crate::storage::StorageModule {
wanted_token_amount: offer.wanted_token.amount * (fee + &BigUint::from(10000u64))
/ BigUint::from(10000u64),
quantity: offer.quantity,
max_quantity_per_address: offer.max_quantity,
}
}
}
10 changes: 10 additions & 0 deletions tests/rust_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1821,6 +1821,7 @@ fn cancel_offer_test() {
wanted_token_nonce: 0u64,
wanted_token_amount: managed_biguint!(204u64),
quantity: managed_biguint!(1u64),
max_quantity_per_address: managed_biguint!(0u64),
};

let view_cancelled_offer = sc
Expand Down Expand Up @@ -1942,6 +1943,7 @@ fn cancel_offer_test() {
wanted_token_nonce: 0u64,
wanted_token_amount: managed_biguint!(204u64),
quantity: managed_biguint!(5u64),
max_quantity_per_address: managed_biguint!(0u64),
};

let view_offer = sc.view_offer(3u64).unwrap();
Expand Down Expand Up @@ -1999,6 +2001,7 @@ fn cancel_offer_test() {
wanted_token_nonce: 0u64,
wanted_token_amount: managed_biguint!(204u64),
quantity: managed_biguint!(3u64),
max_quantity_per_address: managed_biguint!(0u64),
};

let correct_remaining_offer: OfferOut<DebugApi> = OfferOut {
Expand All @@ -2011,6 +2014,7 @@ fn cancel_offer_test() {
wanted_token_nonce: 0u64,
wanted_token_amount: managed_biguint!(204u64),
quantity: managed_biguint!(2u64),
max_quantity_per_address: managed_biguint!(0u64),
};

let view_remained_offer = sc.view_offer(3u64).unwrap();
Expand Down Expand Up @@ -2076,6 +2080,7 @@ fn cancel_offer_test() {
wanted_token_nonce: 0u64,
wanted_token_amount: managed_biguint!(204u64),
quantity: managed_biguint!(5u64),
max_quantity_per_address: managed_biguint!(0u64),
};

let is_listed = sc
Expand Down Expand Up @@ -3207,6 +3212,7 @@ fn views_test() {
wanted_token_nonce: 0u64,
wanted_token_amount: managed_biguint!(204u64),
quantity: managed_biguint!(1u64),
max_quantity_per_address: managed_biguint!(0u64),
};

let offer_out = sc.view_offer(1u64).unwrap();
Expand Down Expand Up @@ -3263,6 +3269,7 @@ fn views_test() {
wanted_token_nonce: 0u64,
wanted_token_amount: managed_biguint!(204u64),
quantity: managed_biguint!(1u64),
max_quantity_per_address: managed_biguint!(0u64),
};

let offer_mock_2 = OfferOut::<DebugApi> {
Expand All @@ -3275,6 +3282,7 @@ fn views_test() {
wanted_token_nonce: 0u64,
wanted_token_amount: managed_biguint!(204u64),
quantity: managed_biguint!(4u64),
max_quantity_per_address: managed_biguint!(0u64),
};

let offers = sc.view_paged_offers(0u64, 1u64, OptionalValue::None);
Expand Down Expand Up @@ -3455,6 +3463,7 @@ fn views_test() {
wanted_token_nonce: 0u64,
wanted_token_amount: managed_biguint!(204u64),
quantity: managed_biguint!(1u64),
max_quantity_per_address: managed_biguint!(0u64),
};

let offers_4 = sc.view_user_listed_offers(&managed_address!(first_user_address));
Expand All @@ -3480,6 +3489,7 @@ fn views_test() {
wanted_token_nonce: 0u64,
wanted_token_amount: managed_biguint!(204u64),
quantity: managed_biguint!(4u64),
max_quantity_per_address: managed_biguint!(0u64),
};

let offers_5 = sc.view_user_listed_offers(&managed_address!(second_user_address));
Expand Down
6 changes: 4 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: 44
// Endpoints: 46
// Async Callback (empty): 1
// Total number of exported functions: 46
// Total number of exported functions: 48

#![no_std]
#![allow(internal_features)]
Expand All @@ -20,6 +20,7 @@ multiversx_sc_wasm_adapter::endpoints! {
data_market
(
init => init
upgrade => upgrade
initializeContract => initialize_contract
setDiscounts => set_discounts
setFees => set_fees
Expand Down Expand Up @@ -53,6 +54,7 @@ multiversx_sc_wasm_adapter::endpoints! {
getTreasuryAddress => treasury_address
getClaimsAddress => claims_address
getMaxDefaultQuantity => max_default_quantity
getMaxQuantityPerAddress => max_quantity_per_address
getRoyaltiesClaimToken => royalties_claim_token
getAdministrator => administrator
getClaimIsEnabled => claim_is_enabled
Expand Down

0 comments on commit c5ccc2a

Please sign in to comment.