Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

Commit

Permalink
[PLAT-754] Remove unused free mint calls (#230)
Browse files Browse the repository at this point in the history
* feat(aaa): remove unused {issue,withdraw}_free_mints

* fix(aaa): add missing imports

* test(aaa): add non organizer test
  • Loading branch information
cowboy-bebug authored Feb 27, 2023
1 parent 3026ab0 commit 3181f09
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 145 deletions.
23 changes: 0 additions & 23 deletions pallets/ajuna-awesome-avatars/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,29 +337,6 @@ benchmarks! {
assert_last_event::<T>(Event::UpdatedGlobalConfig(config).into())
}

issue_free_mints {
let organizer = account::<T>("organizer");
Organizer::<T>::put(&organizer);

let to = account::<T>("to");
let how_many = MintCount::MAX;
}: _(RawOrigin::Signed(organizer), to.clone(), how_many)
verify {
assert_last_event::<T>(Event::FreeMintsIssued { to, how_many }.into())
}

withdraw_free_mints {
let organizer = account::<T>("organizer");
Organizer::<T>::put(&organizer);

let from = account::<T>("from");
let how_many = MintCount::MAX;
Accounts::<T>::mutate(&from, |account|account.free_mints = how_many);
}: _(RawOrigin::Signed(organizer), from.clone(), how_many)
verify {
assert_last_event::<T>(Event::FreeMintsWithdrawn { from, how_many }.into())
}

set_free_mints {
let organizer = account::<T>("organizer");
Organizer::<T>::put(&organizer);
Expand Down
50 changes: 5 additions & 45 deletions pallets/ajuna-awesome-avatars/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
//! * `set_treasurer` - Set the treasurer.
//! * `set_season` - Add a new season.
//! * `update_global_config` - Update the configuration.
//! * `issue_free_mints` - Give a number of free mints to a player.
//! * `set_free_mints` - Set a number of free mints to a player.
//!
//! ### Public Functions
//!
Expand Down Expand Up @@ -250,10 +250,6 @@ pub mod pallet {
SeasonFinished(SeasonId),
/// Free mints transferred between accounts.
FreeMintsTransferred { from: T::AccountId, to: T::AccountId, how_many: MintCount },
/// Free mints issued to account.
FreeMintsIssued { to: T::AccountId, how_many: MintCount },
/// Free mints withdrawn from account.
FreeMintsWithdrawn { from: T::AccountId, how_many: MintCount },
/// Free mints set for target account.
FreeMintsSet { target: T::AccountId, how_many: MintCount },
/// Avatar has price set for trade.
Expand Down Expand Up @@ -710,50 +706,14 @@ pub mod pallet {
Ok(())
}

/// Issue free mints.
/// Set free mints.
///
/// It can only be called by an organizer account.
///
/// Emits `FreeMintsIssued` event when successful.
/// Emits `FreeMintSet` event when successful.
///
/// Weight: `O(1)`
#[pallet::call_index(13)]
#[pallet::weight(T::WeightInfo::issue_free_mints())]
pub fn issue_free_mints(
origin: OriginFor<T>,
to: T::AccountId,
how_many: MintCount,
) -> DispatchResult {
Self::ensure_organizer(origin)?;
ensure!(!how_many.is_zero(), Error::<T>::TooLowFreeMints);
let new_free_mints = Self::accounts(&to)
.free_mints
.checked_add(how_many)
.ok_or(ArithmeticError::Overflow)?;
Accounts::<T>::mutate(&to, |account| account.free_mints = new_free_mints);
Self::deposit_event(Event::FreeMintsIssued { to, how_many });
Ok(())
}

#[pallet::call_index(14)]
#[pallet::weight(T::WeightInfo::withdraw_free_mints())]
pub fn withdraw_free_mints(
origin: OriginFor<T>,
from: T::AccountId,
how_many: MintCount,
) -> DispatchResult {
Self::ensure_organizer(origin)?;
ensure!(!how_many.is_zero(), Error::<T>::TooLowFreeMints);
let new_free_mints = Self::accounts(&from)
.free_mints
.checked_sub(how_many)
.ok_or(ArithmeticError::Underflow)?;
Accounts::<T>::mutate(&from, |account| account.free_mints = new_free_mints);
Self::deposit_event(Event::FreeMintsWithdrawn { from, how_many });
Ok(())
}

#[pallet::call_index(15)]
#[pallet::weight(T::WeightInfo::set_free_mints())]
pub fn set_free_mints(
origin: OriginFor<T>,
Expand All @@ -766,7 +726,7 @@ pub mod pallet {
Ok(())
}

#[pallet::call_index(16)]
#[pallet::call_index(14)]
#[pallet::weight(10_000)]
pub fn lock_avatar(origin: OriginFor<T>, avatar_id: AvatarIdOf<T>) -> DispatchResult {
let account = ensure_signed(origin)?;
Expand All @@ -780,7 +740,7 @@ pub mod pallet {
Ok(())
}

#[pallet::call_index(17)]
#[pallet::call_index(15)]
#[pallet::weight(10_000)]
pub fn unlock_avatar(origin: OriginFor<T>, avatar_id: AvatarIdOf<T>) -> DispatchResult {
let account = ensure_signed(origin)?;
Expand Down
81 changes: 4 additions & 77 deletions pallets/ajuna-awesome-avatars/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1236,64 +1236,6 @@ mod minting {
});
}

#[test]
fn issue_free_mints_should_work() {
ExtBuilder::default()
.organizer(ALICE)
.free_mints(&[(BOB, 7)])
.build()
.execute_with(|| {
assert_ok!(AAvatars::issue_free_mints(RuntimeOrigin::signed(ALICE), BOB, 7));
System::assert_last_event(mock::RuntimeEvent::AAvatars(
crate::Event::FreeMintsIssued { to: BOB, how_many: 7 },
));
assert_eq!(AAvatars::accounts(BOB).free_mints, 7 + 7);
});
}

#[test]
fn issue_free_mints_rejects_overflow() {
ExtBuilder::default()
.organizer(ALICE)
.free_mints(&[(BOB, MintCount::MAX)])
.build()
.execute_with(|| {
assert_noop!(
AAvatars::issue_free_mints(RuntimeOrigin::signed(ALICE), BOB, 1),
ArithmeticError::Overflow
);
})
}

#[test]
fn withdraw_free_mints_works() {
ExtBuilder::default()
.organizer(BOB)
.free_mints(&[(ALICE, 369)])
.build()
.execute_with(|| {
assert_ok!(AAvatars::withdraw_free_mints(RuntimeOrigin::signed(BOB), ALICE, 135));
assert_eq!(AAvatars::accounts(ALICE).free_mints, 369 - 135);
System::assert_last_event(mock::RuntimeEvent::AAvatars(
crate::Event::FreeMintsWithdrawn { from: ALICE, how_many: 135 },
));
})
}

#[test]
fn withdraw_free_mints_rejects_underflow() {
ExtBuilder::default()
.organizer(ALICE)
.free_mints(&[(BOB, 123)])
.build()
.execute_with(|| {
assert_noop!(
AAvatars::withdraw_free_mints(RuntimeOrigin::signed(ALICE), BOB, 124),
ArithmeticError::Underflow
);
})
}

#[test]
fn set_free_mints_works() {
ExtBuilder::default()
Expand All @@ -1310,28 +1252,13 @@ mod minting {
}

#[test]
fn managing_free_mints_rejects_zero_amount() {
ExtBuilder::default().organizer(ALICE).build().execute_with(|| {
for extrinsic in [
AAvatars::issue_free_mints(RuntimeOrigin::signed(ALICE), BOB, 0),
AAvatars::withdraw_free_mints(RuntimeOrigin::signed(ALICE), BOB, 0),
] {
assert_noop!(extrinsic, Error::<Test>::TooLowFreeMints);
}
})
}

#[test]
fn managing_free_mints_requires_organizer() {
fn set_free_mints_rejects_non_organizer_calls() {
ExtBuilder::default().organizer(ALICE).build().execute_with(|| {
for not_organizer in [BOB, CHARLIE] {
for extrinsic in [
AAvatars::issue_free_mints(RuntimeOrigin::signed(not_organizer), ALICE, 123),
AAvatars::withdraw_free_mints(RuntimeOrigin::signed(not_organizer), ALICE, 123),
assert_noop!(
AAvatars::set_free_mints(RuntimeOrigin::signed(not_organizer), ALICE, 123),
] {
assert_noop!(extrinsic, DispatchError::BadOrigin);
}
DispatchError::BadOrigin
);
}
})
}
Expand Down
1 change: 1 addition & 0 deletions pallets/ajuna-awesome-avatars/src/types/avatar/nft.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::types::{Avatar, AvatarCodec, Force, RarityTier};
use codec::{Decode, Encode};
use pallet_ajuna_nft_transfer::traits::{AttributeCode, NftConvertible};
use sp_std::prelude::*;

pub const DNA_ATTRIBUTE_CODE: u16 = 10;
pub const SOUL_POINTS_ATTRIBUTE_CODE: u16 = 11;
Expand Down

0 comments on commit 3181f09

Please sign in to comment.