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

Commit

Permalink
feat: remove NextItemId to store AvatarId directly
Browse files Browse the repository at this point in the history
* rename all asset -> item
* remove nft-staking integration for now
* tidy ups
  • Loading branch information
cowboy-bebug committed Mar 30, 2023
1 parent 066feb8 commit e4f142b
Show file tree
Hide file tree
Showing 15 changed files with 265 additions and 282 deletions.
1 change: 0 additions & 1 deletion node/service/src/chain_spec/bajun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,5 @@ fn testnet_genesis(
parachain_system: Default::default(),
polkadot_xcm: bajun_runtime::PolkadotXcmConfig { safe_xcm_version: Some(SAFE_XCM_VERSION) },
awesome_avatars: Default::default(),
nft_stake: Default::default(),
}
}
1 change: 0 additions & 1 deletion node/service/src/chain_spec/solo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,5 @@ fn compose_genesis_config(config: Config) -> GenesisConfig {
treasury: Default::default(),
democracy: Default::default(),
awesome_avatars: Default::default(),
nft_staking: Default::default(),
}
}
2 changes: 1 addition & 1 deletion pallets/ajuna-awesome-avatars/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ benchmarks! {
set_collection_id {
let organizer = account::<T>("organizer");
Organizer::<T>::put(&organizer);
let collection_id = CollectionIdOf::<T>::unique_saturated_from(u64::MAX);
let collection_id = CollectionIdOf::<T>::from(u32::MAX);
}: _(RawOrigin::Signed(organizer), collection_id.clone())
verify {
assert_last_event::<T>(Event::CollectionIdSet { collection_id }.into())
Expand Down
25 changes: 10 additions & 15 deletions pallets/ajuna-awesome-avatars/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,9 @@ pub mod pallet {
pub(crate) type BoundedAvatarIdsOf<T> = BoundedVec<AvatarIdOf<T>, MaxAvatarsPerPlayer>;
pub(crate) type GlobalConfigOf<T> = GlobalConfig<BalanceOf<T>, BlockNumberFor<T>>;

pub(crate) type AssetIdOf<T> = <<T as Config>::NftHandler as NftHandler<
AccountIdOf<T>,
Avatar,
<T as Config>::AvatarNftConfig,
>>::AssetId;
pub(crate) type CollectionIdOf<T> = <<T as Config>::NftHandler as NftHandler<
AccountIdOf<T>,
AvatarIdOf<T>,
Avatar,
<T as Config>::AvatarNftConfig,
>>::CollectionId;
Expand Down Expand Up @@ -134,7 +130,7 @@ pub mod pallet {
+ MaxEncodedLen
+ TypeInfo;

type NftHandler: NftHandler<Self::AccountId, Avatar, Self::AvatarNftConfig>;
type NftHandler: NftHandler<Self::AccountId, Self::Hash, Avatar, Self::AvatarNftConfig>;

type WeightInfo: WeightInfo;
}
Expand Down Expand Up @@ -180,7 +176,7 @@ pub mod pallet {

#[pallet::storage]
#[pallet::getter(fn locked_avatars)]
pub type LockedAvatars<T: Config> = StorageMap<_, Identity, AvatarIdOf<T>, AssetIdOf<T>>;
pub type LockedAvatars<T: Config> = StorageMap<_, Identity, AvatarIdOf<T>, ()>;

#[pallet::storage]
#[pallet::getter(fn collection_id)]
Expand Down Expand Up @@ -283,7 +279,7 @@ pub mod pallet {
/// Avatar has been traded.
AvatarTraded { avatar_id: AvatarIdOf<T>, from: T::AccountId, to: T::AccountId },
/// Avatar locked.
AvatarLocked { avatar_id: AvatarIdOf<T>, asset_id: AssetIdOf<T> },
AvatarLocked { avatar_id: AvatarIdOf<T> },
/// Avatar unlocked.
AvatarUnlocked { avatar_id: AvatarIdOf<T> },
/// Storage tier has been upgraded.
Expand Down Expand Up @@ -790,13 +786,12 @@ pub mod pallet {

Self::do_transfer_avatar(&player, &Self::technical_account_id(), &avatar_id)?;
// TODO: Use a defined config, either as parameter or as a constant in the pallet config
let asset_config = T::AvatarNftConfig::default();
let item_config = T::AvatarNftConfig::default();
let collection_id = Self::collection_id().ok_or(Error::<T>::CollectionIdNotSet)?;
let asset_id =
T::NftHandler::store_as_nft(player, collection_id, avatar, asset_config)?;
T::NftHandler::store_as_nft(player, collection_id, avatar_id, avatar, item_config)?;

LockedAvatars::<T>::insert(avatar_id, &asset_id);
Self::deposit_event(Event::AvatarLocked { avatar_id, asset_id });
LockedAvatars::<T>::insert(avatar_id, ());
Self::deposit_event(Event::AvatarLocked { avatar_id });
Ok(())
}

Expand All @@ -807,11 +802,11 @@ pub mod pallet {
let _ = Self::ensure_ownership(&Self::technical_account_id(), &avatar_id)?;
ensure!(Self::ensure_for_trade(&avatar_id).is_err(), Error::<T>::AvatarInTrade);
ensure!(Self::global_configs().nft_transfer.open, Error::<T>::NftTransferClosed);
ensure!(LockedAvatars::<T>::contains_key(avatar_id), Error::<T>::AvatarUnlocked);

Self::do_transfer_avatar(&Self::technical_account_id(), &player, &avatar_id)?;
let collection_id = Self::collection_id().ok_or(Error::<T>::CollectionIdNotSet)?;
let asset_id = Self::locked_avatars(avatar_id).ok_or(Error::<T>::AvatarUnlocked)?;
let _ = T::NftHandler::recover_from_nft(player, collection_id, asset_id)?;
let _ = T::NftHandler::recover_from_nft(player, collection_id, avatar_id)?;

LockedAvatars::<T>::remove(avatar_id);
Self::deposit_event(Event::AvatarUnlocked { avatar_id });
Expand Down
24 changes: 18 additions & 6 deletions pallets/ajuna-awesome-avatars/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub type MockAccountId = u32;
pub type MockBlockNumber = u64;
pub type MockBalance = u64;
pub type MockIndex = u64;
pub type MockCollectionId = u32;

pub const ALICE: MockAccountId = 1;
pub const BOB: MockAccountId = 2;
Expand Down Expand Up @@ -118,13 +119,24 @@ parameter_types! {
pub ConfigFeatures: pallet_nfts::PalletFeatures = pallet_nfts::PalletFeatures::all_enabled();
}

pub type MockCollectionId = u32;
pub type MockItemId = u128;
#[cfg(feature = "runtime-benchmarks")]
pub struct Helper;
#[cfg(feature = "runtime-benchmarks")]
impl<CollectionId: From<u16>, ItemId: From<[u8; 32]>>
pallet_nfts::BenchmarkHelper<CollectionId, ItemId> for Helper
{
fn collection(i: u16) -> CollectionId {
i.into()
}
fn item(i: u16) -> ItemId {
[i as u8; 32].into()
}
}

impl pallet_nfts::Config for Test {
type RuntimeEvent = RuntimeEvent;
type CollectionId = MockCollectionId;
type ItemId = MockItemId;
type ItemId = H256;
type Currency = Balances;
type ForceOrigin = EnsureRoot<MockAccountId>;
type CreateOrigin = AsEnsureOriginWithArg<EnsureSigned<MockAccountId>>;
Expand All @@ -143,7 +155,7 @@ impl pallet_nfts::Config for Test {
type MaxDeadlineDuration = MaxDeadlineDuration;
type Features = ConfigFeatures;
pallet_nfts::runtime_benchmarks_enabled! {
type Helper = ();
type Helper = Helper;
}
type WeightInfo = ();
}
Expand All @@ -169,9 +181,9 @@ parameter_types! {
impl pallet_ajuna_nft_transfer::Config for Test {
type PalletId = NftTransferPalletId;
type RuntimeEvent = RuntimeEvent;
type MaxAssetEncodedSize = ValueLimit;
type MaxItemEncodedSize = ValueLimit;
type CollectionId = MockCollectionId;
type ItemId = MockItemId;
type ItemId = H256;
type ItemConfig = pallet_nfts::ItemConfig;
type NftHelper = Nft;
}
Expand Down
17 changes: 8 additions & 9 deletions pallets/ajuna-awesome-avatars/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2837,9 +2837,9 @@ mod nft_transfer {
let avatar_id = avatar_ids[0];

assert_ok!(AAvatars::lock_avatar(RuntimeOrigin::signed(ALICE), avatar_id));
let asset_id = LockedAvatars::<Test>::get(avatar_id).unwrap();
assert!(LockedAvatars::<Test>::contains_key(avatar_id));
System::assert_last_event(mock::RuntimeEvent::AAvatars(
crate::Event::AvatarLocked { avatar_id, asset_id },
crate::Event::AvatarLocked { avatar_id },
));

let (_, avatar) = AAvatars::avatars(avatar_id).unwrap();
Expand All @@ -2855,13 +2855,13 @@ mod nft_transfer {
// Ensure correct encoding
assert_eq!(
<Nft as Inspect<MockAccountId>>::typed_attribute::<
pallet_ajuna_nft_transfer::traits::AssetCode,
pallet_ajuna_nft_transfer::EncodedAssetOf<Test>,
pallet_ajuna_nft_transfer::traits::ItemCode,
pallet_ajuna_nft_transfer::EncodedItemOf<Test>,
>(
&AAvatars::collection_id().unwrap(),
&asset_id,
&avatar_id,
&AttributeNamespace::Pallet,
&<Avatar as NftConvertible>::ASSET_CODE,
&<Avatar as NftConvertible>::ITEM_CODE,
)
.unwrap(),
AvatarCodec {
Expand All @@ -2885,7 +2885,7 @@ mod nft_transfer {
assert_eq!(
<Nft as Inspect<MockAccountId>>::typed_attribute::<AttributeCode, Vec<u8>>(
&AAvatars::collection_id().unwrap(),
&asset_id,
&avatar_id,
&AttributeNamespace::Pallet,
attribute_code,
)
Expand Down Expand Up @@ -3052,10 +3052,9 @@ mod nft_transfer {
let avatar_id = create_avatars(1, ALICE, 1)[0];
assert_ok!(AAvatars::lock_avatar(RuntimeOrigin::signed(ALICE), avatar_id));

let asset_id = LockedAvatars::<Test>::get(avatar_id).unwrap();
pallet_ajuna_nft_transfer::LockItemStatus::<Test>::insert(
AAvatars::collection_id().unwrap(),
asset_id,
avatar_id,
pallet_ajuna_nft_transfer::NftStatus::Uploaded,
);

Expand Down
2 changes: 1 addition & 1 deletion pallets/ajuna-awesome-avatars/src/types/avatar/nft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub const RARITY: AttributeCode = 12;
pub const FORCE: AttributeCode = 13;

impl NftConvertible for Avatar {
const ASSET_CODE: u16 = 0;
const ITEM_CODE: u16 = 0;

fn encode_into(self) -> Vec<u8> {
let avatar_codec = AvatarCodec::from(self);
Expand Down
Loading

0 comments on commit e4f142b

Please sign in to comment.