Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: bump substrate version from 0.9.36 to 0.9.38 #141

Merged
merged 3 commits into from
Mar 9, 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
38 changes: 19 additions & 19 deletions battlepass/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
[package]
name = "gamedao-battlepass"
version = "1.2.0"
version = "1.3.0"
authors = ["zero.io","gamedao.co"]
repository = "https://github.com/gamedaoco/gamedao-protocol"
edition = "2018"
Expand All @@ -14,33 +14,33 @@ description = "BattlePass pallet provides functionality to create, manage and pa

[dependencies]
serde = { version = "1.0.143", default-features = false }
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive"] }
codec = { package = "parity-scale-codec", version = "3.2.2", default-features = false, features = ["derive"] }
scale-info = { version = "2.1.1", default-features = false, features = ["derive"] }
sp-core = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }
sp-runtime = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }
sp-std = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }
sp-storage = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }
sp-io = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.36", default-features=false }
frame-support = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36", default-features = false }
frame-system = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36", default-features = false }
frame-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36", default-features = false, optional = true }
sp-core = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.38" }
sp-runtime = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.38" }
sp-std = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.38" }
sp-storage = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.38" }
sp-io = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.38", default-features=false }
frame-support = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.38", default-features = false }
frame-system = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.38", default-features = false }
frame-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.38", default-features = false, optional = true }

pallet-uniques = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }
pallet-uniques = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.38" }

orml-traits = { git = "https://github.com/open-web3-stack/open-runtime-module-library", branch= "polkadot-v0.9.36", default-features = false }
orml-traits = { git = "https://github.com/open-web3-stack/open-runtime-module-library", branch= "polkadot-v0.9.38", default-features = false }

gamedao-control = { package = "gamedao-control", path = "../control", default-features = false }
gamedao-traits = { package = "gamedao-traits", path = "../traits", default-features = false }

[dev-dependencies]
sp-core = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.36" }
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive"] }
sp-core = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.38" }
codec = { package = "parity-scale-codec", version = "3.2.2", default-features = false, features = ["derive"] }

pallet-balances = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }
pallet-uniques = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.36" }
orml-currencies = { git = "https://github.com/open-web3-stack/open-runtime-module-library", branch= "polkadot-v0.9.36", default-features = false }
orml-tokens = { git = "https://github.com/open-web3-stack/open-runtime-module-library", branch= "polkadot-v0.9.36", default-features = false }
orml-traits = { git = "https://github.com/open-web3-stack/open-runtime-module-library", branch= "polkadot-v0.9.36", default-features = false }
pallet-balances = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.38" }
pallet-uniques = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.38" }
orml-currencies = { git = "https://github.com/open-web3-stack/open-runtime-module-library", branch= "polkadot-v0.9.38", default-features = false }
orml-tokens = { git = "https://github.com/open-web3-stack/open-runtime-module-library", branch= "polkadot-v0.9.38", default-features = false }
orml-traits = { git = "https://github.com/open-web3-stack/open-runtime-module-library", branch= "polkadot-v0.9.38", default-features = false }

[features]
default = ['std']
Expand Down
4 changes: 3 additions & 1 deletion battlepass/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ benchmarks! {
set_bot::<T>(caller.clone(), battlepass_id, bot.clone());
}: _(RawOrigin::Signed(bot), battlepass_id, caller.clone())
verify {
assert!(ClaimedBattlepasses::<T>::get(battlepass_id, caller).is_some());
let collection_id = T::BattlepassHelper::collection(0);
let item_id = T::BattlepassHelper::item(0);
assert!(<pallet_uniques::Pallet<T> as InspectEnumerable<T::AccountId>>::items(&collection_id).any(|x| x == item_id));
}

activate_battlepass {
Expand Down
64 changes: 20 additions & 44 deletions battlepass/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

pub use pallet::*;
use frame_support::{pallet_prelude::*, transactional, dispatch::RawOrigin};
use frame_support::traits::tokens::nonfungibles::Inspect;
use frame_support::traits::tokens::nonfungibles::InspectEnumerable;
use frame_system::pallet_prelude::*;
use sp_std::convert::TryInto;
use sp_runtime::traits::{AtLeast32BitUnsigned, Hash};
Expand Down Expand Up @@ -94,7 +94,7 @@ pub mod pallet {

type BattlepassHelper: BattlepassHelper<Self::CollectionId, Self::ItemId>;

/// The maximum length of a name, cid or metadata strings stored on-chain.
/// The maximum length of a name, cid strings stored on-chain.
#[pallet::constant]
type StringLimit: Get<u32>;

Expand Down Expand Up @@ -212,20 +212,17 @@ pub mod pallet {
AuthorizationError,
CollectionUnknown,
BattlepassExists,
BattlepassClaimed,
BattlepassNotClaimed,
BattlepassOwnershipExists,
BattlepassOwnershipDoesntExist,
BattlepassUnknown,
BattlepassStateUnknown,
BattlepassStateWrong,
BattlepassInfoUnknown,
BattlepassNftUnknown,
BattlepassNftInvalid,
LevelNotReached,
LevelUnknown,
NoAvailableCollectionId,
NoAvailableNftId,
NoChangesProvided,
NotOwnNft,
OrgPrimeUnknown,
OrgUnknownOrInactive,
RewardClaimed,
Expand Down Expand Up @@ -254,18 +251,6 @@ pub mod pallet {
#[pallet::storage]
pub type BattlepassInfoByOrg<T: Config> = StorageMap<_, Blake2_128Concat, T::Hash, BattlepassInfo<T::Hash, T::AccountId>, OptionQuery>;

/// Claimed Battlepass-NFT by user and battlepass.
///
/// ClaimedBattlepasses: map (Hash, AccountId) => ItemId
#[pallet::storage]
#[pallet::getter(fn get_claimed_battlepass)]
pub(super) type ClaimedBattlepasses<T: Config> = StorageDoubleMap<_,
Blake2_128Concat, T::Hash,
Blake2_128Concat, T::AccountId,
T::ItemId,
OptionQuery
>;

/// Total earned Points for users per each Battlepass.
///
/// Points: map (Hash, AccountId) => u32
Expand Down Expand Up @@ -361,7 +346,7 @@ pub mod pallet {
let new_season = battlepass_count + 1;

// Create a collection to store Battlepass NFTs
let collection_id = Self::create_collection(creator.clone(), None)?;
let collection_id = Self::create_collection(creator.clone(), None, cid.clone())?;
let battlepass_id = Self::do_create_battlepass(creator, org_id, name, cid, collection_id, price, new_season)?;

Self::deposit_event(Event::BattlepassCreated { org_id, battlepass_id, season: new_season });
Expand Down Expand Up @@ -435,15 +420,17 @@ pub mod pallet {
// check if Battlepass in ACTIVE state
ensure!(Self::check_battlepass_state(battlepass_id, BattlepassState::ACTIVE)?, Error::<T>::BattlepassStateWrong);
// check if Org is active
ensure!(T::Control::is_org_active(&battlepass.org_id), Error::<T>::OrgUnknownOrInactive);
let org_id = battlepass.org_id.clone();
ensure!(T::Control::is_org_active(&org_id), Error::<T>::OrgUnknownOrInactive);
// check permissions (prime, bot)
ensure!(Self::is_prime_or_bot(&battlepass.org_id, by_who.clone())?, Error::<T>::AuthorizationError);
// check if Battlepass already claimed
ensure!(!ClaimedBattlepasses::<T>::contains_key(battlepass_id, for_who.clone()), Error::<T>::BattlepassClaimed);
ensure!(Self::is_prime_or_bot(&org_id, by_who.clone())?, Error::<T>::AuthorizationError);
// check if user has access to Battlepass
ensure!(!Self::has_battlepass_ownership(for_who.clone(), battlepass.collection_id), Error::<T>::BattlepassOwnershipExists);


let nft_id = Self::do_claim_battlepass(battlepass.creator.clone(), for_who.clone(), battlepass_id, battlepass.collection_id)?;

Self::deposit_event(Event::BattlepassClaimed { by_who, for_who, org_id: battlepass.org_id, battlepass_id, nft_id });
Self::deposit_event(Event::BattlepassClaimed { by_who, for_who, org_id, battlepass_id, nft_id });

Ok(())
}
Expand Down Expand Up @@ -537,7 +524,7 @@ pub mod pallet {
// check permissions (prime, bot)
ensure!(Self::is_prime_or_bot(&battlepass.org_id, sender.clone())?, Error::<T>::AuthorizationError);
// check if user has access to Battlepass
Self::validate_battlepass_ownership(account.clone(), battlepass_id, battlepass)?;
ensure!(Self::has_battlepass_ownership(account.clone(), battlepass.collection_id), Error::<T>::BattlepassOwnershipDoesntExist);

Points::<T>::insert(battlepass_id, &account, amount);

Expand Down Expand Up @@ -580,7 +567,7 @@ pub mod pallet {
ensure!(Self::is_prime_or_bot(&battlepass.org_id, caller)?, Error::<T>::AuthorizationError);

let prime = T::Control::org_prime_account(&battlepass.org_id).ok_or(Error::<T>::OrgPrimeUnknown)?;
let collection_id = Self::create_collection(prime, max)?;
let collection_id = Self::create_collection(prime, max, cid.clone())?;
let reward_id = Self::do_create_reward(battlepass_id, name, cid, level, transferable, collection_id)?;

Self::deposit_event(Event::RewardCreated { reward_id, battlepass_id, level });
Expand Down Expand Up @@ -703,7 +690,7 @@ pub mod pallet {
// check permissions (self, prime or bot)
ensure!(by_who == for_who || Self::is_prime_or_bot(&battlepass.org_id, by_who.clone())?, Error::<T>::AuthorizationError);
// check if user has access to Battlepass
Self::validate_battlepass_ownership(for_who.clone(), reward.battlepass_id, battlepass)?;
ensure!(Self::has_battlepass_ownership(for_who.clone(), battlepass.collection_id), Error::<T>::BattlepassOwnershipDoesntExist);

// check if user has reached the required Level
ensure!(Self::is_level_reached(&reward.battlepass_id, &for_who, reward.level), Error::<T>::LevelNotReached);
Expand Down Expand Up @@ -871,8 +858,8 @@ impl<T: Config> Pallet<T> {
})
}

fn create_collection(owner: T::AccountId, max: Option<u32>) -> Result<T::CollectionId, DispatchError> {
let metadata = BoundedVec::truncate_from(b"meta".to_vec()); // TODO: what should be here?
fn create_collection(owner: T::AccountId, max: Option<u32>, cid: String<T>) -> Result<T::CollectionId, DispatchError> {
let metadata = BoundedVec::truncate_from(cid.into());
let collection_index = Self::bump_collection_index()?;
let collection_id = T::BattlepassHelper::collection(collection_index);
let origin = OriginFor::<T>::from(RawOrigin::Signed(owner.clone()));
Expand Down Expand Up @@ -966,8 +953,6 @@ impl<T: Config> Pallet<T> {

// Create Battlepass NFT
Self::create_nft(creator, for_who.clone(), collection_id, nft_id, metadata)?;

ClaimedBattlepasses::<T>::insert(battlepass_id, &for_who, nft_id);

Ok(nft_id)
}
Expand Down Expand Up @@ -1018,17 +1003,8 @@ impl<T: Config> Pallet<T> {
Ok(nft_id)
}

fn validate_battlepass_ownership(account: T::AccountId, battlepass_id: T::Hash, battlepass: Battlepass<T::Hash, T::AccountId, String<T>, T::CollectionId>) -> DispatchResult {
// check if user claimed Battlepass NFT
let bp_nft_id = Self::get_claimed_battlepass(battlepass_id, &account).ok_or(Error::<T>::BattlepassNotClaimed)?;
// check if Battlepass NFT exists
let bp_nft_owner = pallet_uniques::Pallet::<T>::owner(battlepass.collection_id, bp_nft_id).ok_or(Error::<T>::BattlepassNftUnknown)?;
// validate Battlepass NFT ownership
ensure!(account.clone() == bp_nft_owner, Error::<T>::NotOwnNft);
// validate Battlepass NFT metadata
let bp_nft_metadata = <pallet_uniques::Pallet<T> as Inspect<T::AccountId>>::attribute(&battlepass.collection_id, &bp_nft_id, &[]);
ensure!(Some(battlepass_id.encode()) == bp_nft_metadata, Error::<T>::BattlepassNftInvalid);

Ok(())
fn has_battlepass_ownership(account: T::AccountId, bp_collection_id: T::CollectionId) -> bool {
let bp_owned_count = <pallet_uniques::Pallet<T> as InspectEnumerable<T::AccountId>>::owned_in_collection(&bp_collection_id, &account).count();
bp_owned_count > 0
}
}
Loading