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

[PLAT-756] Add treasury account #233

Merged
merged 5 commits into from
Mar 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pallets/ajuna-awesome-avatars/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,9 @@ benchmarks! {
create_seasons::<T>(3)?;
let season_id = 1;
let treasurer = account::<T>("treasurer");
let amount = BalanceOf::<T>::unique_saturated_from(333_u128);
let amount = AAvatars::<T>::global_configs().transfer.avatar_transfer_fee;
Treasurer::<T>::insert(season_id, treasurer.clone());
Treasury::<T>::insert(season_id, amount);
AAvatars::<T>::deposit_into_treasury(&season_id, amount);
T::Currency::make_free_balance_be(&treasurer, T::Currency::minimum_balance());
}: _(RawOrigin::Signed(treasurer.clone()), season_id)
verify {
Expand Down
41 changes: 35 additions & 6 deletions pallets/ajuna-awesome-avatars/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,12 @@ use crate::{types::*, weights::WeightInfo};
use frame_support::{
pallet_prelude::*,
traits::{Currency, ExistenceRequirement::AllowDeath, Randomness, WithdrawReasons},
PalletId,
};
use frame_system::{ensure_root, ensure_signed, pallet_prelude::*};
use pallet_ajuna_nft_transfer::traits::NftHandler;
use sp_runtime::{
traits::{Hash, Saturating, TrailingZeroInput, UniqueSaturatedInto, Zero},
traits::{AccountIdConversion, Hash, Saturating, TrailingZeroInput, UniqueSaturatedInto, Zero},
ArithmeticError,
};
use sp_std::{collections::btree_set::BTreeSet, vec::Vec};
Expand Down Expand Up @@ -108,6 +109,9 @@ pub mod pallet {

#[pallet::config]
pub trait Config: frame_system::Config {
#[pallet::constant]
type PalletId: Get<PalletId>;

type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;

type Currency: Currency<Self::AccountId>;
Expand Down Expand Up @@ -301,6 +305,8 @@ pub mod pallet {
TooManyRarityPercentages,
/// Some rarity tier are duplicated.
DuplicatedRarityTier,
/// Attempt to set fees lower than the existential deposit amount.
TooLowFees,
/// Minting is not available at the moment.
MintClosed,
/// Forging is not available at the moment.
Expand Down Expand Up @@ -447,7 +453,7 @@ pub mod pallet {
let avatar = Self::ensure_ownership(&from, &avatar_id)?;
let fee = transfer.avatar_transfer_fee;
T::Currency::withdraw(&from, fee, WithdrawReasons::FEE, AllowDeath)?;
Treasury::<T>::mutate(avatar.season_id, |bal| bal.saturating_accrue(fee));
Self::deposit_into_treasury(&avatar.season_id, fee);

Self::do_transfer_avatar(&from, &to, &avatar_id)?;
Self::deposit_event(Event::AvatarTransferred { from, to, avatar_id });
Expand Down Expand Up @@ -565,7 +571,7 @@ pub mod pallet {
MAX_PERCENTAGE.unique_saturated_into(),
);
T::Currency::withdraw(&buyer, trade_fee, WithdrawReasons::FEE, AllowDeath)?;
Treasury::<T>::mutate(season_id, |bal| bal.saturating_accrue(trade_fee));
Self::deposit_into_treasury(&season_id, trade_fee);

Self::do_transfer_avatar(&seller, &buyer, &avatar_id)?;
Trade::<T>::remove(avatar_id);
Expand Down Expand Up @@ -593,7 +599,7 @@ pub mod pallet {
T::Currency::withdraw(&player, upgrade_fee, WithdrawReasons::FEE, AllowDeath)?;

let season_id = Self::current_season_id();
Treasury::<T>::mutate(season_id, |bal| bal.saturating_accrue(upgrade_fee));
Self::deposit_into_treasury(&season_id, upgrade_fee);

Accounts::<T>::mutate(&player, |account| account.storage_tier = storage_tier.upgrade());
Self::deposit_event(Event::StorageTierUpgraded);
Expand Down Expand Up @@ -660,7 +666,7 @@ pub mod pallet {
let amount = Treasury::<T>::take(season_id);
ensure!(!amount.is_zero(), Error::<T>::CannotClaimZero);

T::Currency::deposit_into_existing(&treasurer, amount)?;
T::Currency::transfer(&Self::account_id(), &treasurer, amount, AllowDeath)?;
Self::deposit_event(Event::TreasuryClaimed { season_id, treasurer, amount });
Ok(())
}
Expand Down Expand Up @@ -702,6 +708,19 @@ pub mod pallet {
new_global_config: GlobalConfigOf<T>,
) -> DispatchResult {
Self::ensure_organizer(origin)?;
ensure!(
[
new_global_config.mint.fees.one,
new_global_config.mint.fees.three,
new_global_config.mint.fees.six,
new_global_config.transfer.avatar_transfer_fee,
new_global_config.trade.min_fee,
new_global_config.account.storage_upgrade_fee
]
.iter()
.all(|x| x > &T::Currency::minimum_balance()),
Error::<T>::TooLowFees
);
GlobalConfigs::<T>::put(&new_global_config);
Self::deposit_event(Event::UpdatedGlobalConfig(new_global_config));
Ok(())
Expand Down Expand Up @@ -757,6 +776,16 @@ pub mod pallet {
}

impl<T: Config> Pallet<T> {
/// The account ID of the treasury.
pub fn account_id() -> T::AccountId {
T::PalletId::get().into_account_truncating()
}

pub(crate) fn deposit_into_treasury(season_id: &SeasonId, amount: BalanceOf<T>) {
Treasury::<T>::mutate(season_id, |bal| bal.saturating_accrue(amount));
T::Currency::deposit_creating(&Self::account_id(), amount);
}

/// Check that the origin is an organizer account.
pub(crate) fn ensure_organizer(
origin: OriginFor<T>,
Expand Down Expand Up @@ -878,7 +907,7 @@ pub mod pallet {
MintType::Normal => {
let fee = mint.fees.fee_for(&mint_option.count);
T::Currency::withdraw(player, fee, WithdrawReasons::FEE, AllowDeath)?;
Treasury::<T>::mutate(season_id, |bal| bal.saturating_accrue(fee));
Self::deposit_into_treasury(&season_id, fee);
},
MintType::Free => {
let fee = (mint_option.count as MintCount)
Expand Down
54 changes: 52 additions & 2 deletions pallets/ajuna-awesome-avatars/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl frame_system::Config for Test {
}

parameter_types! {
pub const MockExistentialDeposit: MockBalance = 321;
pub static MockExistentialDeposit: MockBalance = 321;
}

impl pallet_balances::Config for Test {
Expand Down Expand Up @@ -146,10 +146,12 @@ impl pallet_nfts::Config for Test {
}

parameter_types! {
pub const AwesomeAvatarsPalletId: PalletId = PalletId(*b"aj/aaatr");
pub const MockAvatarCollectionId: MockCollectionId = 0;
}

impl pallet_ajuna_awesome_avatars::Config for Test {
type PalletId = AwesomeAvatarsPalletId;
type RuntimeEvent = RuntimeEvent;
type Currency = Balances;
type Randomness = Randomness;
Expand All @@ -175,8 +177,8 @@ impl pallet_ajuna_nft_transfer::Config for Test {
type WeightInfo = ();
}

#[derive(Default)]
pub struct ExtBuilder {
existential_deposit: MockBalance,
organizer: Option<MockAccountId>,
seasons: Vec<(SeasonId, Season<MockBlockNumber>)>,
mint_cooldown: Option<MockBlockNumber>,
Expand All @@ -187,7 +189,27 @@ pub struct ExtBuilder {
avatar_transfer_fee: Option<MockBalance>,
}

impl Default for ExtBuilder {
fn default() -> Self {
Self {
existential_deposit: MockExistentialDeposit::get(),
organizer: Default::default(),
seasons: Default::default(),
mint_cooldown: Default::default(),
mint_fees: Default::default(),
trade_min_fee: Default::default(),
balances: Default::default(),
free_mints: Default::default(),
avatar_transfer_fee: Default::default(),
}
}
}

impl ExtBuilder {
pub fn existential_deposit(mut self, existential_deposit: MockBalance) -> Self {
self.existential_deposit = existential_deposit;
self
}
pub fn organizer(mut self, organizer: MockAccountId) -> Self {
self.organizer = Some(organizer);
self
Expand Down Expand Up @@ -222,6 +244,7 @@ impl ExtBuilder {
}

pub fn build(self) -> sp_io::TestExternalities {
MOCK_EXISTENTIAL_DEPOSIT.with(|v| *v.borrow_mut() = self.existential_deposit);
let mut t = frame_system::GenesisConfig::default().build_storage::<Test>().unwrap();
pallet_balances::GenesisConfig::<Test> { balances: self.balances }
.assimilate_storage(&mut t)
Expand Down Expand Up @@ -288,3 +311,30 @@ pub fn run_to_block(n: u64) {
AAvatars::on_initialize(System::block_number());
}
}

impl GlobalConfigOf<Test> {
pub(crate) fn mint_fees_one(mut self, amount: MockBalance) -> Self {
self.mint.fees.one = amount;
self
}
pub(crate) fn mint_fees_three(mut self, amount: MockBalance) -> Self {
self.mint.fees.three = amount;
self
}
pub(crate) fn mint_fees_six(mut self, amount: MockBalance) -> Self {
self.mint.fees.six = amount;
self
}
pub(crate) fn transfer_avatar_transfer_fee(mut self, amount: MockBalance) -> Self {
self.transfer.avatar_transfer_fee = amount;
self
}
pub(crate) fn trade_min_fee(mut self, amount: MockBalance) -> Self {
self.trade.min_fee = amount;
self
}
pub(crate) fn account_storage_upgrade_fe(mut self, amount: MockBalance) -> Self {
self.account.storage_upgrade_fee = amount;
self
}
}
Loading