Skip to content

Commit

Permalink
Stefan/fix apis (#315)
Browse files Browse the repository at this point in the history
* decrease supply

* edit comments

* re-name
  • Loading branch information
mutobui authored Aug 14, 2023
1 parent 6a7c5fe commit 0adad6e
Show file tree
Hide file tree
Showing 14 changed files with 309 additions and 259 deletions.
16 changes: 8 additions & 8 deletions game/pallet-game/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,18 @@ The highest bid when the auction expired is the winner.
### Trade dispatchables
* `set_price`: Set price for NFTs.
* `buy_item`: Buy NFTs from `set_price`.
* `add_retail_supply`: Add NFTs for `set_price`.
* `add_set_price`: Add NFTs for `set_price`.
* `set_bundle`: Set a price for a bundle.
* `buy_bundle`: Buy a bundle from `set_bundle`.
* `set_buy`: Place a buy trade for NFTs.
* `claim_set_buy`: Sell NFTs for `set_buy`.
* `set_wishlist`: Order a buy-all for a bundle.
* `claim_wishlist`: Sell a bundle for `set_wishlist`.
* `set_swap`: Set an exchange a bundle for a bundle may have an additional cost.
* `claim_swap`: Make an exchange from `set_swap`.
* `set_order`: Place a buy trade for NFTs.
* `sell_item`: Sell NFTs for `set_order`.
* `order_bundle`: Order a buy-all for a bundle.
* `sell_bundle`: Sell a bundle for `order_bundle`.
* `create_swap`: Set an exchange a bundle for a bundle may have an additional cost.
* `make_swap`: Make an exchange from `create_swap`.
* `set_auction`: Bid for a bundle, starting on a specific block with a minimum bid and duration.
* `bid_auction`: Make a bid.
* `claim_auction`: End the auction when it expires.
* `close_auction`: End the auction when it expires.
* `cancel_trade`: Cancel a trade with trade id.
* `create_dynamic_pool`: Create a minting pool with a dynamic weight loot table.

Expand Down
43 changes: 21 additions & 22 deletions game/pallet-game/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ fn do_create_item<T: Config<I>, I: 'static>(
RawOrigin::Signed(admin.clone()).into(),
<T as pallet_nfts::Config>::Helper::collection(collection),
<T as pallet_nfts::Config>::Helper::item(item),
default_item_config(),
supply,
));
}
Expand Down Expand Up @@ -268,7 +267,7 @@ fn do_set_wishlist<T: Config<I>, I: 'static>(who: &T::AccountId) {
},
];

assert_ok!(PalletGame::<T, I>::set_wishlist(
assert_ok!(PalletGame::<T, I>::order_bundle(
RawOrigin::Signed(who.clone()).into(),
bundle,
<T as pallet::Config<I>>::Currency::minimum_balance(),
Expand Down Expand Up @@ -328,8 +327,8 @@ benchmarks_instance_pallet! {
create_item {
let (caller, admin) = do_create_collection::<T, I>();
let call = Call::<T, I>::create_item { collection: <T as pallet_nfts::Config>::Helper::collection(0),
item: <T as pallet_nfts::Config>::Helper::item(0),
config: default_item_config(), maybe_supply: Some(10) };
item: <T as pallet_nfts::Config>::Helper::item(0),
maybe_supply: Some(10) };
}: { call.dispatch_bypass_filter(RawOrigin::Signed(admin.clone()).into())? }
verify {
assert_last_event::<T, I>(Event::ItemCreated { who: admin,
Expand Down Expand Up @@ -523,7 +522,7 @@ benchmarks_instance_pallet! {
}.into() );
}

set_wishlist {
order_bundle {
let s in 0 .. <T as pallet::Config<I>>::MaxBundle::get();
let (who, _, _) = new_account_with_item::<T, I>(0);
let bundle = vec![
Expand All @@ -533,7 +532,7 @@ benchmarks_instance_pallet! {
amount: 1,
}; s as usize];

let call = Call::<T, I>::set_wishlist {
let call = Call::<T, I>::order_bundle {
bundle: bundle.clone(),
price: <T as pallet::Config<I>>::Currency::minimum_balance(),
start_block: None,
Expand All @@ -549,13 +548,13 @@ benchmarks_instance_pallet! {
}.into() );
}

claim_wishlist {
sell_bundle {
let player = new_funded_account::<T, I>(1, 1, 1000_000_000u128 * UNIT);
do_set_wishlist::<T, I>(&player);

let (who, _, _) = new_account_with_item::<T, I>(0);

let call = Call::<T, I>::claim_wishlist {
let call = Call::<T, I>::sell_bundle {
trade: <T as pallet::Config<I>>::Helper::trade(0),
ask_price: <T as pallet::Config<I>>::Currency::minimum_balance(),
};
Expand Down Expand Up @@ -584,7 +583,7 @@ benchmarks_instance_pallet! {
}.into() );
}

set_swap {
create_swap {
let s in 0 .. <T as pallet::Config<I>>::MaxBundle::get();
let x in 0 .. <T as pallet::Config<I>>::MaxBundle::get();

Expand All @@ -603,7 +602,7 @@ benchmarks_instance_pallet! {
amount: 1,
}; x as usize];

let call = Call::<T, I>::set_swap {
let call = Call::<T, I>::create_swap {
source: bundle.clone(),
required: required.clone(),
maybe_price: Some(<T as pallet::Config<I>>::Currency::minimum_balance()),
Expand All @@ -621,7 +620,7 @@ benchmarks_instance_pallet! {
}.into() );
}

claim_swap {
make_swap {
let (player1, _, _) = new_account_with_item::<T, I>(0);
let (player2, _, _) = new_account_with_item::<T, I>(1);

Expand Down Expand Up @@ -649,7 +648,7 @@ benchmarks_instance_pallet! {
amount: 10,
}];

assert_ok!(PalletGame::<T, I>::set_swap(
assert_ok!(PalletGame::<T, I>::create_swap(
RawOrigin::Signed(player1.clone()).into(),
source.clone(),
required.clone(),
Expand All @@ -658,7 +657,7 @@ benchmarks_instance_pallet! {
None,
));

let call = Call::<T, I>::claim_swap {
let call = Call::<T, I>::make_swap {
trade: <T as pallet::Config<I>>::Helper::trade(0),
maybe_bid_price: Some(<T as pallet::Config<I>>::Currency::minimum_balance()),
};
Expand Down Expand Up @@ -717,7 +716,7 @@ benchmarks_instance_pallet! {
}.into() );
}

claim_auction {
close_auction {
let _ = do_set_auction::<T, I>();

let bidder = new_funded_account::<T, I>(0, 0, 1000_000_000u128 * UNIT);
Expand All @@ -731,7 +730,7 @@ benchmarks_instance_pallet! {

frame_system::Pallet::<T>::set_block_number(<T as pallet::Config<I>>::Helper::block(10));

let call = Call::<T, I>::claim_auction {
let call = Call::<T, I>::close_auction {
trade: <T as pallet::Config<I>>::Helper::trade(0),
};
}: { call.dispatch_bypass_filter(RawOrigin::Signed(caller.clone()).into())? }
Expand All @@ -742,7 +741,7 @@ benchmarks_instance_pallet! {
}.into() );
}

set_buy {
set_order {
let caller = new_funded_account::<T, I>(0, 0, 1000_000_000u128 * UNIT);

let package = Package {
Expand All @@ -751,7 +750,7 @@ benchmarks_instance_pallet! {
amount: 10,
};

let call = Call::<T, I>::set_buy {
let call = Call::<T, I>::set_order {
package: package.clone(),
unit_price: <T as pallet::Config<I>>::Currency::minimum_balance(),
start_block: None,
Expand All @@ -769,7 +768,7 @@ benchmarks_instance_pallet! {
}.into() );
}

claim_set_buy {
sell_item {
let (who, _, _) = new_account_with_item::<T, I>(0);

let player = new_funded_account::<T, I>(0, 0, 1000_000_000u128 * UNIT);
Expand All @@ -778,15 +777,15 @@ benchmarks_instance_pallet! {
item: <T as pallet_nfts::Config>::Helper::item(0),
amount: 10,
};
assert_ok!(PalletGame::<T, I>::set_buy(
assert_ok!(PalletGame::<T, I>::set_order(
RawOrigin::Signed(player.clone()).into(),
package.clone(),
<T as pallet::Config<I>>::Currency::minimum_balance(),
None,
None,
));

let call = Call::<T, I>::claim_set_buy {
let call = Call::<T, I>::sell_item {
trade: <T as pallet::Config<I>>::Helper::trade(0),
amount: 10,
ask_price: <T as pallet::Config<I>>::Currency::minimum_balance(),
Expand Down Expand Up @@ -863,7 +862,7 @@ benchmarks_instance_pallet! {
}.into() );
}

add_retail_supply {
add_set_price {
let (who, _, _) = new_account_with_item::<T, I>(0);
do_set_price::<T, I>(&who);
let package = Package {
Expand All @@ -872,7 +871,7 @@ benchmarks_instance_pallet! {
amount: 5,
};

let call = Call::<T, I>::add_retail_supply {
let call = Call::<T, I>::add_set_price {
trade: <T as pallet::Config<I>>::Helper::trade(0),
supply: package,
};
Expand Down
66 changes: 57 additions & 9 deletions game/pallet-game/src/features/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
collection: &T::CollectionId,
item: &T::ItemId,
to: &T::AccountId,
amount: u32,
amount: Amount,
) -> Result<(), Error<T, I>> {
Self::sub_item_balance(from, collection, item, amount)?;
Self::add_item_balance(to, collection, item, amount)?;
Expand All @@ -22,7 +22,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
collection: &T::CollectionId,
old_item: &T::ItemId,
new_item: &T::ItemId,
amount: u32,
amount: Amount,
) -> Result<(), Error<T, I>> {
Self::sub_item_balance(who, collection, old_item, amount)?;
Self::add_item_balance(who, collection, new_item, amount)?;
Expand All @@ -34,7 +34,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
who: &T::AccountId,
collection: &T::CollectionId,
item: &T::ItemId,
amount: u32,
amount: Amount,
) -> Result<(), Error<T, I>> {
ensure!(amount > 0, Error::<T, I>::InvalidAmount);
let balance = ItemBalanceOf::<T, I>::get((&who, &collection, &item));
Expand All @@ -47,7 +47,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
who: &T::AccountId,
collection: &T::CollectionId,
item: &T::ItemId,
amount: u32,
amount: Amount,
) -> Result<(), Error<T, I>> {
ensure!(amount > 0, Error::<T, I>::InvalidAmount);
let balance = ItemBalanceOf::<T, I>::get((&who, &collection, &item));
Expand All @@ -67,7 +67,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
who: &T::AccountId,
collection: &T::CollectionId,
item: &T::ItemId,
amount: u32,
amount: Amount,
) -> Result<(), Error<T, I>> {
ensure!(amount > 0, Error::<T, I>::InvalidAmount);
let balance = ReservedBalanceOf::<T, I>::get((&who, &collection, &item));
Expand All @@ -80,7 +80,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
who: &T::AccountId,
collection: &T::CollectionId,
item: &T::ItemId,
amount: u32,
amount: Amount,
) -> Result<(), Error<T, I>> {
ensure!(amount > 0, Error::<T, I>::InvalidAmount);
let balance = ReservedBalanceOf::<T, I>::get((who, collection, item));
Expand All @@ -102,7 +102,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
who: &T::AccountId,
collection: &T::CollectionId,
item: &T::ItemId,
amount: u32,
amount: Amount,
) -> Result<(), Error<T, I>> {
Self::sub_item_balance(who, collection, item, amount)?;
Self::add_reserved_balance(who, collection, item, amount)?;
Expand All @@ -123,7 +123,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
who: &T::AccountId,
collection: &T::CollectionId,
item: &T::ItemId,
amount: u32,
amount: Amount,
) -> Result<(), Error<T, I>> {
Self::sub_reserved_balance(who, collection, item, amount)?;
Self::add_item_balance(who, collection, item, amount)?;
Expand All @@ -137,7 +137,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
collection: &T::CollectionId,
item: &T::ItemId,
beneficiary: &T::AccountId,
amount: u32,
amount: Amount,
status: ItemBalanceStatus,
) -> Result<(), Error<T, I>> {
Self::sub_reserved_balance(slashed, collection, item, amount)?;
Expand Down Expand Up @@ -179,4 +179,52 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
.map(|maybe_supply| maybe_supply.is_none())
.unwrap_or_default()
}

/// Decreases the finite supply of a specific item within a collection by a given amount.
///
/// If the collection and item exist, their finite supply is decremented by the specified
/// amount, with saturation to prevent underflow.
///
/// # Parameters
///
/// - `collection`: The identifier of the collection.
/// - `item`: The identifier of the item.
/// - `amount`: The amount to decrease the item's supply by.
pub(crate) fn decrease_finite_item_supply(
collection: &T::CollectionId,
item: &T::ItemId,
amount: Amount,
) {
if let Some(Some(existing_supply)) = SupplyOf::<T, I>::get(collection, item) {
SupplyOf::<T, I>::insert(
collection,
item,
Some(existing_supply.saturating_sub(amount)),
);
}
}

/// Increases the finite supply of a specific item within a collection by a given amount.
///
/// If the collection and item exist, their finite supply is incremented by the specified
/// amount, with saturation to prevent overflow.
///
/// # Parameters
///
/// - `collection`: The identifier of the collection.
/// - `item`: The identifier of the item.
/// - `amount`: The amount to increase the item's supply by.
pub(crate) fn increase_finite_item_supply(
collection: &T::CollectionId,
item: &T::ItemId,
amount: Amount,
) {
if let Some(Some(existing_supply)) = SupplyOf::<T, I>::get(collection, item) {
SupplyOf::<T, I>::insert(
collection,
item,
Some(existing_supply.saturating_add(amount)),
);
}
}
}
Loading

0 comments on commit 0adad6e

Please sign in to comment.