difftreelog
refactor Generalization some operations.
in: master
13 files changed
pallets/common/src/erc.rsdiffbeforeafterboth--- a/pallets/common/src/erc.rs
+++ b/pallets/common/src/erc.rs
@@ -222,12 +222,11 @@
fn set_collection_sponsor(&mut self, caller: caller, sponsor: address) -> Result<void> {
self.consume_store_reads_and_writes(1, 1)?;
- check_is_owner_or_admin(caller, self)?;
+ let caller = T::CrossAccountId::from_eth(caller);
let sponsor = T::CrossAccountId::from_eth(sponsor);
- self.set_sponsor(sponsor.as_sub().clone())
- .map_err(dispatch_to_evm::<T>)?;
- save(self)
+ self.set_sponsor(&caller, sponsor.as_sub().clone())
+ .map_err(dispatch_to_evm::<T>)
}
/// Set the sponsor of the collection.
@@ -242,12 +241,11 @@
) -> Result<void> {
self.consume_store_reads_and_writes(1, 1)?;
- check_is_owner_or_admin(caller, self)?;
+ let caller = T::CrossAccountId::from_eth(caller);
let sponsor = sponsor.into_sub_cross_account::<T>()?;
- self.set_sponsor(sponsor.as_sub().clone())
- .map_err(dispatch_to_evm::<T>)?;
- save(self)
+ self.set_sponsor(&caller, sponsor.as_sub().clone())
+ .map_err(dispatch_to_evm::<T>)
}
/// Whether there is a pending sponsor.
@@ -265,21 +263,15 @@
self.consume_store_writes(1)?;
let caller = T::CrossAccountId::from_eth(caller);
- if !self
- .confirm_sponsorship(caller.as_sub())
- .map_err(dispatch_to_evm::<T>)?
- {
- return Err("caller is not set as sponsor".into());
- }
- save(self)
+ self.confirm_sponsorship(caller.as_sub())
+ .map_err(dispatch_to_evm::<T>)
}
/// Remove collection sponsor.
fn remove_collection_sponsor(&mut self, caller: caller) -> Result<void> {
self.consume_store_reads_and_writes(1, 1)?;
- check_is_owner_or_admin(caller, self)?;
- self.remove_sponsor().map_err(dispatch_to_evm::<T>)?;
- save(self)
+ let caller = T::CrossAccountId::from_eth(caller);
+ self.remove_sponsor(&caller).map_err(dispatch_to_evm::<T>)
}
/// Get current sponsor.
@@ -333,7 +325,6 @@
}
};
- check_is_owner_or_admin(caller, self)?;
let mut limits = self.limits.clone();
match limit.as_str() {
@@ -366,9 +357,9 @@
}
_ => return Err(Error::Revert(format!("unknown limit \"{}\"", limit))),
}
- self.limits = <Pallet<T>>::clamp_limits(self.mode.clone(), &self.limits, limits)
- .map_err(dispatch_to_evm::<T>)?;
- save(self)
+
+ let caller = T::CrossAccountId::from_eth(caller);
+ <Pallet<T>>::update_limits(&caller, self, limits).map_err(dispatch_to_evm::<T>)
}
/// Get contract address.
@@ -383,7 +374,7 @@
caller: caller,
new_admin: EthCrossAccount,
) -> Result<void> {
- self.consume_store_writes(2)?;
+ self.consume_store_reads_and_writes(2, 2)?;
let caller = T::CrossAccountId::from_eth(caller);
let new_admin = new_admin.into_sub_cross_account::<T>()?;
@@ -398,7 +389,7 @@
caller: caller,
admin: EthCrossAccount,
) -> Result<void> {
- self.consume_store_writes(2)?;
+ self.consume_store_reads_and_writes(2, 2)?;
let caller = T::CrossAccountId::from_eth(caller);
let admin = admin.into_sub_cross_account::<T>()?;
@@ -410,7 +401,7 @@
/// @param newAdmin Address of the added administrator.
#[solidity(hide)]
fn add_collection_admin(&mut self, caller: caller, new_admin: address) -> Result<void> {
- self.consume_store_writes(2)?;
+ self.consume_store_reads_and_writes(2, 2)?;
let caller = T::CrossAccountId::from_eth(caller);
let new_admin = T::CrossAccountId::from_eth(new_admin);
@@ -423,7 +414,7 @@
/// @param admin Address of the removed administrator.
#[solidity(hide)]
fn remove_collection_admin(&mut self, caller: caller, admin: address) -> Result<void> {
- self.consume_store_writes(2)?;
+ self.consume_store_reads_and_writes(2, 2)?;
let caller = T::CrossAccountId::from_eth(caller);
let admin = T::CrossAccountId::from_eth(admin);
@@ -438,7 +429,7 @@
fn set_nesting_bool(&mut self, caller: caller, enable: bool) -> Result<void> {
self.consume_store_reads_and_writes(1, 1)?;
- check_is_owner_or_admin(caller, self)?;
+ let caller = T::CrossAccountId::from_eth(caller);
let mut permissions = self.collection.permissions.clone();
let mut nesting = permissions.nesting().clone();
@@ -446,14 +437,7 @@
nesting.restricted = None;
permissions.nesting = Some(nesting);
- self.collection.permissions = <Pallet<T>>::clamp_permissions(
- self.collection.mode.clone(),
- &self.collection.permissions,
- permissions,
- )
- .map_err(dispatch_to_evm::<T>)?;
-
- save(self)
+ <Pallet<T>>::update_permissions(&caller, self, permissions).map_err(dispatch_to_evm::<T>)
}
/// Toggle accessibility of collection nesting.
@@ -472,7 +456,7 @@
if collections.is_empty() {
return Err("no addresses provided".into());
}
- check_is_owner_or_admin(caller, self)?;
+ let caller = T::CrossAccountId::from_eth(caller);
let mut permissions = self.collection.permissions.clone();
match enable {
@@ -497,14 +481,7 @@
}
};
- self.collection.permissions = <Pallet<T>>::clamp_permissions(
- self.collection.mode.clone(),
- &self.collection.permissions,
- permissions,
- )
- .map_err(dispatch_to_evm::<T>)?;
-
- save(self)
+ <Pallet<T>>::update_permissions(&caller, self, permissions).map_err(dispatch_to_evm::<T>)
}
/// Set the collection access method.
@@ -514,7 +491,7 @@
fn set_collection_access(&mut self, caller: caller, mode: uint8) -> Result<void> {
self.consume_store_reads_and_writes(1, 1)?;
- check_is_owner_or_admin(caller, self)?;
+ let caller = T::CrossAccountId::from_eth(caller);
let permissions = CollectionPermissions {
access: Some(match mode {
0 => AccessMode::Normal,
@@ -523,14 +500,7 @@
}),
..Default::default()
};
- self.collection.permissions = <Pallet<T>>::clamp_permissions(
- self.collection.mode.clone(),
- &self.collection.permissions,
- permissions,
- )
- .map_err(dispatch_to_evm::<T>)?;
-
- save(self)
+ <Pallet<T>>::update_permissions(&caller, self, permissions).map_err(dispatch_to_evm::<T>)
}
/// Checks that user allowed to operate with collection.
@@ -605,19 +575,12 @@
fn set_collection_mint_mode(&mut self, caller: caller, mode: bool) -> Result<void> {
self.consume_store_reads_and_writes(1, 1)?;
- check_is_owner_or_admin(caller, self)?;
+ let caller = T::CrossAccountId::from_eth(caller);
let permissions = CollectionPermissions {
mint_mode: Some(mode),
..Default::default()
};
- self.collection.permissions = <Pallet<T>>::clamp_permissions(
- self.collection.mode.clone(),
- &self.collection.permissions,
- permissions,
- )
- .map_err(dispatch_to_evm::<T>)?;
-
- save(self)
+ <Pallet<T>>::update_permissions(&caller, self, permissions).map_err(dispatch_to_evm::<T>)
}
/// Check that account is the owner or admin of the collection
@@ -671,7 +634,7 @@
let caller = T::CrossAccountId::from_eth(caller);
let new_owner = T::CrossAccountId::from_eth(new_owner);
- self.set_owner_internal(caller, new_owner)
+ self.change_owner(caller, new_owner)
.map_err(dispatch_to_evm::<T>)
}
@@ -699,7 +662,7 @@
let caller = T::CrossAccountId::from_eth(caller);
let new_owner = new_owner.into_sub_cross_account::<T>()?;
- self.set_owner_internal(caller, new_owner)
+ self.change_owner(caller, new_owner)
.map_err(dispatch_to_evm::<T>)
}
}
pallets/common/src/lib.rsdiffbeforeafterboth--- a/pallets/common/src/lib.rs
+++ b/pallets/common/src/lib.rs
@@ -229,28 +229,111 @@
/// Unique collections allows sponsoring for certain actions.
/// This method allows you to set the sponsor of the collection.
/// In order for sponsorship to become active, it must be confirmed through [`Self::confirm_sponsorship`].
- pub fn set_sponsor(&mut self, sponsor: T::AccountId) -> DispatchResult {
- self.collection.sponsorship = SponsorshipState::Unconfirmed(sponsor);
- Ok(())
+ pub fn set_sponsor(
+ &mut self,
+ sender: &T::CrossAccountId,
+ sponsor: T::AccountId,
+ ) -> DispatchResult {
+ self.check_is_internal()?;
+ self.check_is_owner_or_admin(sender)?;
+
+ self.collection.sponsorship = SponsorshipState::Unconfirmed(sponsor.clone());
+
+ <Pallet<T>>::deposit_event(Event::<T>::CollectionSponsorSet(self.id, sponsor));
+ <PalletEvm<T>>::deposit_log(
+ erc::CollectionHelpersEvents::CollectionChanged {
+ collection_id: eth::collection_id_to_address(self.id),
+ }
+ .to_log(T::ContractAddress::get()),
+ );
+
+ self.save()
+ }
+
+ /// Force set `sponsor`.
+ ///
+ /// Differs from [`set_sponsor`][`Self::set_sponsor`] in that confirmation
+ /// from the `sponsor` is not required.
+ ///
+ /// # Arguments
+ ///
+ /// * `sender`: Caller's account.
+ /// * `sponsor`: ID of the account of the sponsor-to-be.
+ pub fn force_set_sponsor(&mut self, sponsor: T::AccountId) -> DispatchResult {
+ self.check_is_internal()?;
+
+ self.collection.sponsorship = SponsorshipState::Confirmed(sponsor.clone());
+
+ <Pallet<T>>::deposit_event(Event::<T>::CollectionSponsorSet(self.id, sponsor.clone()));
+ <Pallet<T>>::deposit_event(Event::<T>::SponsorshipConfirmed(self.id, sponsor));
+ <PalletEvm<T>>::deposit_log(
+ erc::CollectionHelpersEvents::CollectionChanged {
+ collection_id: eth::collection_id_to_address(self.id),
+ }
+ .to_log(T::ContractAddress::get()),
+ );
+
+ self.save()
}
/// Confirm sponsorship
///
/// In order for the sponsorship to become active, the user set as the sponsor must confirm their participation.
/// Before confirming sponsorship, the user must be specified as the sponsor of the collection via [`Self::set_sponsor`].
- pub fn confirm_sponsorship(&mut self, sender: &T::AccountId) -> Result<bool, DispatchError> {
- if self.collection.sponsorship.pending_sponsor() != Some(sender) {
- return Ok(false);
- }
+ pub fn confirm_sponsorship(&mut self, sender: &T::AccountId) -> DispatchResult {
+ self.check_is_internal()?;
+ ensure!(
+ self.collection.sponsorship.pending_sponsor() == Some(sender),
+ Error::<T>::ConfirmUnsetSponsorFail
+ );
self.collection.sponsorship = SponsorshipState::Confirmed(sender.clone());
- Ok(true)
+
+ <Pallet<T>>::deposit_event(Event::<T>::SponsorshipConfirmed(self.id, sender.clone()));
+ <PalletEvm<T>>::deposit_log(
+ erc::CollectionHelpersEvents::CollectionChanged {
+ collection_id: eth::collection_id_to_address(self.id),
+ }
+ .to_log(T::ContractAddress::get()),
+ );
+
+ self.save()
}
/// Remove collection sponsor.
- pub fn remove_sponsor(&mut self) -> DispatchResult {
+ pub fn remove_sponsor(&mut self, sender: &T::CrossAccountId) -> DispatchResult {
+ self.check_is_internal()?;
+ self.check_is_owner(sender)?;
+
+ self.collection.sponsorship = SponsorshipState::Disabled;
+
+ <Pallet<T>>::deposit_event(Event::<T>::CollectionSponsorRemoved(self.id));
+ <PalletEvm<T>>::deposit_log(
+ erc::CollectionHelpersEvents::CollectionChanged {
+ collection_id: eth::collection_id_to_address(self.id),
+ }
+ .to_log(T::ContractAddress::get()),
+ );
+ self.save()
+ }
+
+ /// Force remove `sponsor`.
+ ///
+ /// Differs from `remove_sponsor` in that
+ /// it doesn't require consent from the `owner` of the collection.
+ pub fn force_remove_sponsor(&mut self) -> DispatchResult {
+ self.check_is_internal()?;
+
self.collection.sponsorship = SponsorshipState::Disabled;
- Ok(())
+
+ <Pallet<T>>::deposit_event(Event::<T>::CollectionSponsorRemoved(self.id));
+ <PalletEvm<T>>::deposit_log(
+ erc::CollectionHelpersEvents::CollectionChanged {
+ collection_id: eth::collection_id_to_address(self.id),
+ }
+ .to_log(T::ContractAddress::get()),
+ );
+ self.save()
}
/// Checks that the collection was created with, and must be operated upon through **Unique API**.
@@ -328,13 +411,26 @@
/// Changes collection owner to another account
/// #### Store read/writes
/// 1 writes
- fn set_owner_internal(
+ pub fn change_owner(
&mut self,
caller: T::CrossAccountId,
new_owner: T::CrossAccountId,
) -> DispatchResult {
+ self.check_is_internal()?;
self.check_is_owner(&caller)?;
self.collection.owner = new_owner.as_sub().clone();
+
+ <Pallet<T>>::deposit_event(Event::<T>::CollectionOwnedChanged(
+ self.id,
+ new_owner.as_sub().clone(),
+ ));
+ <PalletEvm<T>>::deposit_log(
+ erc::CollectionHelpersEvents::CollectionChanged {
+ collection_id: eth::collection_id_to_address(self.id),
+ }
+ .to_log(T::ContractAddress::get()),
+ );
+
self.save()
}
}
@@ -527,6 +623,80 @@
/// The property permission that was set.
PropertyKey,
),
+
+ /// Address was added to the allow list.
+ AllowListAddressAdded(
+ /// ID of the affected collection.
+ CollectionId,
+ /// Address of the added account.
+ T::CrossAccountId,
+ ),
+
+ /// Address was removed from the allow list.
+ AllowListAddressRemoved(
+ /// ID of the affected collection.
+ CollectionId,
+ /// Address of the removed account.
+ T::CrossAccountId,
+ ),
+
+ /// Collection admin was added.
+ CollectionAdminAdded(
+ /// ID of the affected collection.
+ CollectionId,
+ /// Admin address.
+ T::CrossAccountId,
+ ),
+
+ /// Collection admin was removed.
+ CollectionAdminRemoved(
+ /// ID of the affected collection.
+ CollectionId,
+ /// Removed admin address.
+ T::CrossAccountId,
+ ),
+
+ /// Collection limits were set.
+ CollectionLimitSet(
+ /// ID of the affected collection.
+ CollectionId,
+ ),
+
+ /// Collection owned was changed.
+ CollectionOwnedChanged(
+ /// ID of the affected collection.
+ CollectionId,
+ /// New owner address.
+ T::AccountId,
+ ),
+
+ /// Collection permissions were set.
+ CollectionPermissionSet(
+ /// ID of the affected collection.
+ CollectionId,
+ ),
+
+ /// Collection sponsor was set.
+ CollectionSponsorSet(
+ /// ID of the affected collection.
+ CollectionId,
+ /// New sponsor address.
+ T::AccountId,
+ ),
+
+ /// New sponsor was confirm.
+ SponsorshipConfirmed(
+ /// ID of the affected collection.
+ CollectionId,
+ /// New sponsor address.
+ T::AccountId,
+ ),
+
+ /// Collection sponsor was removed.
+ CollectionSponsorRemoved(
+ /// ID of the affected collection.
+ CollectionId,
+ ),
}
#[pallet::error]
@@ -613,6 +783,12 @@
/// Tried to access an internal collection with an external API
CollectionIsInternal,
+
+ /// This address is not set as sponsor, use setCollectionSponsor first.
+ ConfirmUnsetSponsorFail,
+
+ /// The user is not an administrator.
+ UserIsNotAdmin,
}
/// Storage of the count of created collections. Essentially contains the last collection ID.
@@ -1363,27 +1539,47 @@
if allowed {
<Allowlist<T>>::insert((collection.id, user), true);
+ Self::deposit_event(Event::<T>::AllowListAddressAdded(
+ collection.id,
+ user.clone(),
+ ));
} else {
<Allowlist<T>>::remove((collection.id, user));
+ Self::deposit_event(Event::<T>::AllowListAddressRemoved(
+ collection.id,
+ user.clone(),
+ ));
}
+ <PalletEvm<T>>::deposit_log(
+ erc::CollectionHelpersEvents::CollectionChanged {
+ collection_id: eth::collection_id_to_address(collection.id),
+ }
+ .to_log(T::ContractAddress::get()),
+ );
+
Ok(())
}
/// Toggle `user` participation in the `collection`'s admin list.
/// #### Store read/writes
- /// 2 writes
+ /// 2 reads, 2 writes
pub fn toggle_admin(
collection: &CollectionHandle<T>,
sender: &T::CrossAccountId,
user: &T::CrossAccountId,
admin: bool,
) -> DispatchResult {
+ collection.check_is_internal()?;
collection.check_is_owner(sender)?;
- let was_admin = <IsAdmin<T>>::get((collection.id, user));
- if was_admin == admin {
- return Ok(());
+ let is_admin = <IsAdmin<T>>::get((collection.id, user));
+ if is_admin == admin {
+ if admin {
+ return Ok(());
+ } else {
+ ensure!(false, Error::<T>::UserIsNotAdmin);
+ }
}
let amount = <AdminAmount<T>>::get(collection.id);
@@ -1400,16 +1596,56 @@
<AdminAmount<T>>::insert(collection.id, amount);
<IsAdmin<T>>::insert((collection.id, user), true);
+
+ Self::deposit_event(Event::<T>::CollectionAdminAdded(
+ collection.id,
+ user.clone(),
+ ));
} else {
<AdminAmount<T>>::insert(collection.id, amount.saturating_sub(1));
<IsAdmin<T>>::remove((collection.id, user));
+
+ Self::deposit_event(Event::<T>::CollectionAdminRemoved(
+ collection.id,
+ user.clone(),
+ ));
}
+ <PalletEvm<T>>::deposit_log(
+ erc::CollectionHelpersEvents::CollectionChanged {
+ collection_id: eth::collection_id_to_address(collection.id),
+ }
+ .to_log(T::ContractAddress::get()),
+ );
+
Ok(())
}
+ /// Update collection limits.
+ pub fn update_limits(
+ user: &T::CrossAccountId,
+ collection: &mut CollectionHandle<T>,
+ new_limit: CollectionLimits,
+ ) -> DispatchResult {
+ collection.check_is_internal()?;
+ collection.check_is_owner_or_admin(user)?;
+
+ collection.limits =
+ Self::clamp_limits(collection.mode.clone(), &collection.limits, new_limit)?;
+
+ Self::deposit_event(Event::<T>::CollectionLimitSet(collection.id));
+ <PalletEvm<T>>::deposit_log(
+ erc::CollectionHelpersEvents::CollectionChanged {
+ collection_id: eth::collection_id_to_address(collection.id),
+ }
+ .to_log(T::ContractAddress::get()),
+ );
+
+ collection.save()
+ }
+
/// Merge set fields from `new_limit` to `old_limit`.
- pub fn clamp_limits(
+ fn clamp_limits(
mode: CollectionMode,
old_limit: &CollectionLimits,
mut new_limit: CollectionLimits,
@@ -1454,8 +1690,33 @@
Ok(new_limit)
}
+ /// Update collection permissions.
+ pub fn update_permissions(
+ user: &T::CrossAccountId,
+ collection: &mut CollectionHandle<T>,
+ new_permission: CollectionPermissions,
+ ) -> DispatchResult {
+ collection.check_is_internal()?;
+ collection.check_is_owner_or_admin(user)?;
+ collection.permissions = Self::clamp_permissions(
+ collection.mode.clone(),
+ &collection.permissions,
+ new_permission,
+ )?;
+
+ Self::deposit_event(Event::<T>::CollectionPermissionSet(collection.id));
+ <PalletEvm<T>>::deposit_log(
+ erc::CollectionHelpersEvents::CollectionChanged {
+ collection_id: eth::collection_id_to_address(collection.id),
+ }
+ .to_log(T::ContractAddress::get()),
+ );
+
+ collection.save()
+ }
+
/// Merge set fields from `new_permission` to `old_permission`.
- pub fn clamp_permissions(
+ fn clamp_permissions(
_mode: CollectionMode,
old_permission: &CollectionPermissions,
mut new_permission: CollectionPermissions,
pallets/unique/src/lib.rsdiffbeforeafterboth--- a/pallets/unique/src/lib.rs
+++ b/pallets/unique/src/lib.rs
@@ -89,10 +89,9 @@
MAX_PROPERTIES_PER_ITEM, MAX_PROPERTY_KEY_LENGTH, MAX_PROPERTY_VALUE_LENGTH,
MAX_COLLECTION_PROPERTIES_SIZE, COLLECTION_ADMINS_LIMIT, MAX_TOKEN_PROPERTIES_SIZE,
CreateItemData, CollectionLimits, CollectionPermissions, CollectionId, CollectionMode, TokenId,
- SponsorshipState, CreateCollectionData, CreateItemExData, budget, Property, PropertyKey,
- PropertyKeyPermission,
+ CreateCollectionData, CreateItemExData, budget, Property, PropertyKey, PropertyKeyPermission,
};
-use pallet_evm::account::CrossAccountId;
+use pallet_evm::{account::CrossAccountId};
use pallet_common::{
CollectionHandle, Pallet as PalletCommon, CommonWeightInfo, dispatch::dispatch_tx,
dispatch::CollectionDispatch, RefungibleExtensionsWeightInfo,
@@ -112,8 +111,6 @@
pub enum Error for Module<T: Config> {
/// Decimal_points parameter must be lower than [`up_data_structs::MAX_DECIMAL_POINTS`].
CollectionDecimalPointLimitExceeded,
- /// This address is not set as sponsor, use setCollectionSponsor first.
- ConfirmUnsetSponsorFail,
/// Length of items properties must be greater than 0.
EmptyArgument,
/// Repertition is only supported by refungible collection.
@@ -140,27 +137,12 @@
pub enum Event<T>
where
<T as frame_system::Config>::AccountId,
- <T as pallet_evm::Config>::CrossAccountId,
{
/// Collection sponsor was removed
///
/// # Arguments
/// * collection_id: ID of the affected collection.
CollectionSponsorRemoved(CollectionId),
-
- /// Collection admin was added
- ///
- /// # Arguments
- /// * collection_id: ID of the affected collection.
- /// * admin: Admin address.
- CollectionAdminAdded(CollectionId, CrossAccountId),
-
- /// Collection owned was changed
- ///
- /// # Arguments
- /// * collection_id: ID of the affected collection.
- /// * owner: New owner address.
- CollectionOwnedChanged(CollectionId, AccountId),
/// Collection sponsor was set
///
@@ -168,46 +150,7 @@
/// * collection_id: ID of the affected collection.
/// * owner: New sponsor address.
CollectionSponsorSet(CollectionId, AccountId),
-
- /// New sponsor was confirm
- ///
- /// # Arguments
- /// * collection_id: ID of the affected collection.
- /// * sponsor: New sponsor address.
- SponsorshipConfirmed(CollectionId, AccountId),
-
- /// Collection admin was removed
- ///
- /// # Arguments
- /// * collection_id: ID of the affected collection.
- /// * admin: Removed admin address.
- CollectionAdminRemoved(CollectionId, CrossAccountId),
- /// Address was removed from the allow list
- ///
- /// # Arguments
- /// * collection_id: ID of the affected collection.
- /// * user: Address of the removed account.
- AllowListAddressRemoved(CollectionId, CrossAccountId),
-
- /// Address was added to the allow list
- ///
- /// # Arguments
- /// * collection_id: ID of the affected collection.
- /// * user: Address of the added account.
- AllowListAddressAdded(CollectionId, CrossAccountId),
-
- /// Collection limits were set
- ///
- /// # Arguments
- /// * collection_id: ID of the affected collection.
- CollectionLimitSet(CollectionId),
-
- /// Collection permissions were set
- ///
- /// # Arguments
- /// * collection_id: ID of the affected collection.
- CollectionPermissionSet(CollectionId),
}
}
@@ -432,11 +375,6 @@
&address,
true,
)?;
-
- Self::deposit_event(Event::<T>::AllowListAddressAdded(
- collection_id,
- address
- ));
Ok(())
}
@@ -466,11 +404,6 @@
false,
)?;
- <Pallet<T>>::deposit_event(Event::<T>::AllowListAddressRemoved(
- collection_id,
- address
- ));
-
Ok(())
}
@@ -486,20 +419,10 @@
/// * `new_owner`: ID of the account that will become the owner.
#[weight = <SelfWeightOf<T>>::change_collection_owner()]
pub fn change_collection_owner(origin, collection_id: CollectionId, new_owner: T::AccountId) -> DispatchResult {
-
let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
-
+ let new_owner = T::CrossAccountId::from_sub(new_owner);
let mut target_collection = <CollectionHandle<T>>::try_get(collection_id)?;
- target_collection.check_is_internal()?;
- target_collection.check_is_owner(&sender)?;
-
- target_collection.owner = new_owner.clone();
- <Pallet<T>>::deposit_event(Event::<T>::CollectionOwnedChanged(
- collection_id,
- new_owner
- ));
-
- target_collection.save()
+ target_collection.change_owner(sender, new_owner.clone())
}
/// Add an admin to a collection.
@@ -522,13 +445,6 @@
pub fn add_collection_admin(origin, collection_id: CollectionId, new_admin_id: T::CrossAccountId) -> DispatchResult {
let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
let collection = <CollectionHandle<T>>::try_get(collection_id)?;
- collection.check_is_internal()?;
-
- <Pallet<T>>::deposit_event(Event::<T>::CollectionAdminAdded(
- collection_id,
- new_admin_id.clone()
- ));
-
<PalletCommon<T>>::toggle_admin(&collection, &sender, &new_admin_id, true)
}
@@ -550,13 +466,6 @@
pub fn remove_collection_admin(origin, collection_id: CollectionId, account_id: T::CrossAccountId) -> DispatchResult {
let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
let collection = <CollectionHandle<T>>::try_get(collection_id)?;
- collection.check_is_internal()?;
-
- <Pallet<T>>::deposit_event(Event::<T>::CollectionAdminRemoved(
- collection_id,
- account_id.clone()
- ));
-
<PalletCommon<T>>::toggle_admin(&collection, &sender, &account_id, false)
}
@@ -576,19 +485,8 @@
#[weight = <SelfWeightOf<T>>::set_collection_sponsor()]
pub fn set_collection_sponsor(origin, collection_id: CollectionId, new_sponsor: T::AccountId) -> DispatchResult {
let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
-
let mut target_collection = <CollectionHandle<T>>::try_get(collection_id)?;
- target_collection.check_is_owner_or_admin(&sender)?;
- target_collection.check_is_internal()?;
-
- target_collection.set_sponsor(new_sponsor.clone())?;
-
- <Pallet<T>>::deposit_event(Event::<T>::CollectionSponsorSet(
- collection_id,
- new_sponsor
- ));
-
- target_collection.save()
+ target_collection.set_sponsor(&sender, new_sponsor.clone())
}
/// Confirm own sponsorship of a collection, becoming the sponsor.
@@ -607,20 +505,8 @@
#[weight = <SelfWeightOf<T>>::confirm_sponsorship()]
pub fn confirm_sponsorship(origin, collection_id: CollectionId) -> DispatchResult {
let sender = ensure_signed(origin)?;
-
let mut target_collection = <CollectionHandle<T>>::try_get(collection_id)?;
- target_collection.check_is_internal()?;
- ensure!(
- target_collection.confirm_sponsorship(&sender)?,
- Error::<T>::ConfirmUnsetSponsorFail
- );
-
- <Pallet<T>>::deposit_event(Event::<T>::SponsorshipConfirmed(
- collection_id,
- sender
- ));
-
- target_collection.save()
+ target_collection.confirm_sponsorship(&sender)
}
/// Remove a collection's a sponsor, making everyone pay for their own transactions.
@@ -635,17 +521,8 @@
#[weight = <SelfWeightOf<T>>::remove_collection_sponsor()]
pub fn remove_collection_sponsor(origin, collection_id: CollectionId) -> DispatchResult {
let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
-
let mut target_collection = <CollectionHandle<T>>::try_get(collection_id)?;
- target_collection.check_is_internal()?;
- target_collection.check_is_owner(&sender)?;
-
- target_collection.sponsorship = SponsorshipState::Disabled;
-
- <Pallet<T>>::deposit_event(Event::<T>::CollectionSponsorRemoved(
- collection_id
- ));
- target_collection.save()
+ target_collection.remove_sponsor(&sender)
}
/// Mint an item within a collection.
@@ -1053,17 +930,7 @@
) -> DispatchResult {
let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
let mut target_collection = <CollectionHandle<T>>::try_get(collection_id)?;
- target_collection.check_is_internal()?;
- target_collection.check_is_owner_or_admin(&sender)?;
- let old_limit = &target_collection.limits;
-
- target_collection.limits = <PalletCommon<T>>::clamp_limits(target_collection.mode.clone(), &old_limit, new_limit)?;
-
- <Pallet<T>>::deposit_event(Event::<T>::CollectionLimitSet(
- collection_id
- ));
-
- target_collection.save()
+ <PalletCommon<T>>::update_limits(&sender, &mut target_collection, new_limit)
}
/// Set specific permissions of a collection. Empty, or None fields mean chain default.
@@ -1086,17 +953,11 @@
) -> DispatchResult {
let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
let mut target_collection = <CollectionHandle<T>>::try_get(collection_id)?;
- target_collection.check_is_internal()?;
- target_collection.check_is_owner_or_admin(&sender)?;
- let old_limit = &target_collection.permissions;
-
- target_collection.permissions = <PalletCommon<T>>::clamp_permissions(target_collection.mode.clone(), &old_limit, new_permission)?;
-
- <Pallet<T>>::deposit_event(Event::<T>::CollectionPermissionSet(
- collection_id
- ));
-
- target_collection.save()
+ <PalletCommon<T>>::update_permissions(
+ &sender,
+ &mut target_collection,
+ new_permission
+ )
}
/// Re-partition a refungible token, while owning all of its parts/pieces.
@@ -1163,22 +1024,7 @@
/// * `collection_id`: ID of the modified collection.
pub fn force_set_sponsor(sponsor: T::AccountId, collection_id: CollectionId) -> DispatchResult {
let mut target_collection = <CollectionHandle<T>>::try_get(collection_id)?;
- target_collection.check_is_internal()?;
- target_collection.set_sponsor(sponsor.clone())?;
-
- Self::deposit_event(Event::<T>::CollectionSponsorSet(
- collection_id,
- sponsor.clone(),
- ));
-
- ensure!(
- target_collection.confirm_sponsorship(&sponsor)?,
- Error::<T>::ConfirmUnsetSponsorFail
- );
-
- Self::deposit_event(Event::<T>::SponsorshipConfirmed(collection_id, sponsor));
-
- target_collection.save()
+ target_collection.force_set_sponsor(sponsor.clone())
}
/// Force remove `sponsor` for `collection`.
@@ -1191,12 +1037,7 @@
/// * `collection_id`: ID of the modified collection.
pub fn force_remove_collection_sponsor(collection_id: CollectionId) -> DispatchResult {
let mut target_collection = <CollectionHandle<T>>::try_get(collection_id)?;
- target_collection.check_is_internal()?;
- target_collection.sponsorship = SponsorshipState::Disabled;
-
- Self::deposit_event(Event::<T>::CollectionSponsorRemoved(collection_id));
-
- target_collection.save()
+ target_collection.force_remove_sponsor()
}
#[inline(always)]
tests/src/change-collection-owner.test.tsdiffbeforeafterboth--- a/tests/src/change-collection-owner.test.ts
+++ b/tests/src/change-collection-owner.test.ts
@@ -146,7 +146,7 @@
const confirmSponsorshipTx = () => collection.confirmSponsorship(alice);
const removeSponsorTx = () => collection.removeSponsor(alice);
await expect(setSponsorTx()).to.be.rejectedWith(/common\.NoPermission/);
- await expect(confirmSponsorshipTx()).to.be.rejectedWith(/unique\.ConfirmUnsetSponsorFail/);
+ await expect(confirmSponsorshipTx()).to.be.rejectedWith(/common\.ConfirmUnsetSponsorFail/);
await expect(removeSponsorTx()).to.be.rejectedWith(/common\.NoPermission/);
const limits = {
tests/src/confirmSponsorship.test.tsdiffbeforeafterboth--- a/tests/src/confirmSponsorship.test.ts
+++ b/tests/src/confirmSponsorship.test.ts
@@ -207,14 +207,14 @@
const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});
await collection.setSponsor(alice, bob.address);
const confirmSponsorshipTx = () => collection.confirmSponsorship(charlie);
- await expect(confirmSponsorshipTx()).to.be.rejectedWith(/unique\.ConfirmUnsetSponsorFail/);
+ await expect(confirmSponsorshipTx()).to.be.rejectedWith(/common\.ConfirmUnsetSponsorFail/);
});
itSub('(!negative test!) Confirm sponsorship using owner address', async ({helper}) => {
const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});
await collection.setSponsor(alice, bob.address);
const confirmSponsorshipTx = () => collection.confirmSponsorship(alice);
- await expect(confirmSponsorshipTx()).to.be.rejectedWith(/unique\.ConfirmUnsetSponsorFail/);
+ await expect(confirmSponsorshipTx()).to.be.rejectedWith(/common\.ConfirmUnsetSponsorFail/);
});
itSub('(!negative test!) Confirm sponsorship by collection admin', async ({helper}) => {
@@ -222,13 +222,13 @@
await collection.setSponsor(alice, bob.address);
await collection.addAdmin(alice, {Substrate: charlie.address});
const confirmSponsorshipTx = () => collection.confirmSponsorship(charlie);
- await expect(confirmSponsorshipTx()).to.be.rejectedWith(/unique\.ConfirmUnsetSponsorFail/);
+ await expect(confirmSponsorshipTx()).to.be.rejectedWith(/common\.ConfirmUnsetSponsorFail/);
});
itSub('(!negative test!) Confirm sponsorship without sponsor being set with setCollectionSponsor', async ({helper}) => {
const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});
const confirmSponsorshipTx = () => collection.confirmSponsorship(charlie);
- await expect(confirmSponsorshipTx()).to.be.rejectedWith(/unique\.ConfirmUnsetSponsorFail/);
+ await expect(confirmSponsorshipTx()).to.be.rejectedWith(/common\.ConfirmUnsetSponsorFail/);
});
itSub('(!negative test!) Confirm sponsorship in a collection that was destroyed', async ({helper}) => {
tests/src/eth/collectionSponsoring.test.tsdiffbeforeafterboth--- a/tests/src/eth/collectionSponsoring.test.ts
+++ b/tests/src/eth/collectionSponsoring.test.ts
@@ -258,7 +258,7 @@
await collectionEvm.methods[testCase](testCase === 'setCollectionSponsor' ? sponsor : sponsorCross).send();
let collectionData = (await collectionSub.getData())!;
expect(collectionData.raw.sponsorship.Unconfirmed).to.be.eq(helper.address.ethToSubstrate(sponsor, true));
- await expect(collectionEvm.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('caller is not set as sponsor');
+ await expect(collectionEvm.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('ConfirmUnsetSponsorFail');
await collectionEvm.methods.confirmCollectionSponsorship().send({from: sponsor});
collectionData = (await collectionSub.getData())!;
tests/src/eth/createFTCollection.test.tsdiffbeforeafterboth--- a/tests/src/eth/createFTCollection.test.ts
+++ b/tests/src/eth/createFTCollection.test.ts
@@ -46,7 +46,7 @@
let data = (await helper.rft.getData(collectionId))!;
expect(data.raw.sponsorship.Unconfirmed).to.be.equal(evmToAddress(sponsor, Number(ss58Format)));
- await expect(collection.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('caller is not set as sponsor');
+ await expect(collection.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('ConfirmUnsetSponsorFail');
const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'rft', sponsor, true);
await sponsorCollection.methods.confirmCollectionSponsorship().send();
@@ -69,7 +69,7 @@
let data = (await helper.rft.getData(collectionId))!;
expect(data.raw.sponsorship.Unconfirmed).to.be.equal(evmToAddress(sponsor, Number(ss58Format)));
- await expect(collection.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('caller is not set as sponsor');
+ await expect(collection.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('ConfirmUnsetSponsorFail');
const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'rft', sponsor);
await sponsorCollection.methods.confirmCollectionSponsorship().send();
@@ -192,11 +192,11 @@
const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'ft', sponsor, true);
await expect(sponsorCollection.methods
.confirmCollectionSponsorship()
- .call()).to.be.rejectedWith('caller is not set as sponsor');
+ .call()).to.be.rejectedWith('ConfirmUnsetSponsorFail');
}
{
await expect(peasantCollection.methods
- .setCollectionLimit('account_token_ownership_limit', '1000')
+ .setCollectionLimit('accountTokenOwnershipLimit', '1000')
.call()).to.be.rejectedWith(EXPECTED_ERROR);
}
});
@@ -217,11 +217,11 @@
const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'ft', sponsor);
await expect(sponsorCollection.methods
.confirmCollectionSponsorship()
- .call()).to.be.rejectedWith('caller is not set as sponsor');
+ .call()).to.be.rejectedWith('ConfirmUnsetSponsorFail');
}
{
await expect(peasantCollection.methods
- .setCollectionLimit('account_token_ownership_limit', '1000')
+ .setCollectionLimit('accountTokenOwnershipLimit', '1000')
.call()).to.be.rejectedWith(EXPECTED_ERROR);
}
});
tests/src/eth/createNFTCollection.test.tsdiffbeforeafterboth--- a/tests/src/eth/createNFTCollection.test.ts
+++ b/tests/src/eth/createNFTCollection.test.ts
@@ -86,7 +86,7 @@
let data = (await helper.nft.getData(collectionId))!;
expect(data.raw.sponsorship.Unconfirmed).to.be.equal(evmToAddress(sponsor, Number(ss58Format)));
- await expect(collection.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('caller is not set as sponsor');
+ await expect(collection.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('ConfirmUnsetSponsorFail');
const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'nft', sponsor, true);
await sponsorCollection.methods.confirmCollectionSponsorship().send();
@@ -109,7 +109,7 @@
let data = (await helper.nft.getData(collectionId))!;
expect(data.raw.sponsorship.Unconfirmed).to.be.equal(evmToAddress(sponsor, Number(ss58Format)));
- await expect(collection.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('caller is not set as sponsor');
+ await expect(collection.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('ConfirmUnsetSponsorFail');
const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'nft', sponsor);
await sponsorCollection.methods.confirmCollectionSponsorship().send();
@@ -203,11 +203,11 @@
const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'nft', sponsor, true);
await expect(sponsorCollection.methods
.confirmCollectionSponsorship()
- .call()).to.be.rejectedWith('caller is not set as sponsor');
+ .call()).to.be.rejectedWith('ConfirmUnsetSponsorFail');
}
{
await expect(malfeasantCollection.methods
- .setCollectionLimit('account_token_ownership_limit', '1000')
+ .setCollectionLimit('accountTokenOwnershipLimit', '1000')
.call()).to.be.rejectedWith(EXPECTED_ERROR);
}
});
@@ -228,11 +228,11 @@
const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'nft', sponsor);
await expect(sponsorCollection.methods
.confirmCollectionSponsorship()
- .call()).to.be.rejectedWith('caller is not set as sponsor');
+ .call()).to.be.rejectedWith('ConfirmUnsetSponsorFail');
}
{
await expect(malfeasantCollection.methods
- .setCollectionLimit('account_token_ownership_limit', '1000')
+ .setCollectionLimit('accountTokenOwnershipLimit', '1000')
.call()).to.be.rejectedWith(EXPECTED_ERROR);
}
});
tests/src/eth/createRFTCollection.test.tsdiffbeforeafterboth--- a/tests/src/eth/createRFTCollection.test.ts
+++ b/tests/src/eth/createRFTCollection.test.ts
@@ -121,7 +121,7 @@
let data = (await helper.rft.getData(collectionId))!;
expect(data.raw.sponsorship.Unconfirmed).to.be.equal(evmToAddress(sponsor, Number(ss58Format)));
- await expect(collection.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('caller is not set as sponsor');
+ await expect(collection.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('ConfirmUnsetSponsorFail');
const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'rft', sponsor, true);
await sponsorCollection.methods.confirmCollectionSponsorship().send();
@@ -143,7 +143,7 @@
let data = (await helper.rft.getData(collectionId))!;
expect(data.raw.sponsorship.Unconfirmed).to.be.equal(evmToAddress(sponsor, Number(ss58Format)));
- await expect(collection.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('caller is not set as sponsor');
+ await expect(collection.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('ConfirmUnsetSponsorFail');
const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'rft', sponsor);
await sponsorCollection.methods.confirmCollectionSponsorship().send();
@@ -235,11 +235,11 @@
const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'rft', sponsor, true);
await expect(sponsorCollection.methods
.confirmCollectionSponsorship()
- .call()).to.be.rejectedWith('caller is not set as sponsor');
+ .call()).to.be.rejectedWith('ConfirmUnsetSponsorFail');
}
{
await expect(peasantCollection.methods
- .setCollectionLimit('account_token_ownership_limit', '1000')
+ .setCollectionLimit('accountTokenOwnershipLimit', '1000')
.call()).to.be.rejectedWith(EXPECTED_ERROR);
}
});
@@ -260,11 +260,11 @@
const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'rft', sponsor);
await expect(sponsorCollection.methods
.confirmCollectionSponsorship()
- .call()).to.be.rejectedWith('caller is not set as sponsor');
+ .call()).to.be.rejectedWith('ConfirmUnsetSponsorFail');
}
{
await expect(peasantCollection.methods
- .setCollectionLimit('account_token_ownership_limit', '1000')
+ .setCollectionLimit('accountTokenOwnershipLimit', '1000')
.call()).to.be.rejectedWith(EXPECTED_ERROR);
}
});
tests/src/eth/events.test.tsdiffbeforeafterboth--- a/tests/src/eth/events.test.ts
+++ b/tests/src/eth/events.test.ts
@@ -14,11 +14,11 @@
// You should have received a copy of the GNU General Public License
// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.
+import { expect } from 'chai';
import {IKeyringPair} from '@polkadot/types/types';
-import { expect } from 'chai';
import { itEth, usingEthPlaygrounds } from './util';
-describe.only('NFT events', () => {
+describe('NFT events', () => {
let donor: IKeyringPair;
before(async function () {
@@ -29,8 +29,9 @@
itEth('Create event', async ({helper}) => {
const owner = await helper.eth.createAccountWithBalance(donor);
- const {collectionAddress, events} = await helper.eth.createCollecion('createNFTCollection', owner, 'A', 'B', 'C');
- expect(events).to.be.like([
+ const {unsubscribe, collectedEvents: subEvents} = await helper.subscribeEvents([{section: 'common', names: ['CollectionCreated']}]);
+ const {collectionAddress, events: ethEvents} = await helper.eth.createCollecion('createNFTCollection', owner, 'A', 'B', 'C');
+ expect(ethEvents).to.be.like([
{
event: 'CollectionCreated',
args: {
@@ -39,20 +40,25 @@
}
}
]);
+ expect(subEvents).to.be.like([{method: 'CollectionCreated'}]);
+ unsubscribe();
});
itEth('Destroy event', async ({helper}) => {
const owner = await helper.eth.createAccountWithBalance(donor);
const {collectionAddress} = await helper.eth.createCollecion('createNFTCollection', owner, 'A', 'B', 'C');
const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);
- let resutl = await collectionHelper.methods.destroyCollection(collectionAddress).send({from:owner});
- expect(resutl.events).to.be.like({
+ const {unsubscribe, collectedEvents} = await helper.subscribeEvents([{section: 'common', names: ['CollectionDestroyed']}]);
+ let result = await collectionHelper.methods.destroyCollection(collectionAddress).send({from:owner});
+ expect(result.events).to.be.like({
CollectionDestroyed: {
returnValues: {
collectionId: collectionAddress
}
}
});
+ expect(collectedEvents).to.be.like([{method: 'CollectionDestroyed'}]);
+ unsubscribe();
});
itEth('CollectionChanged event for CollectionPropertySet and CollectionPropertyDeleted', async ({helper}) => {
@@ -61,13 +67,14 @@
const collection = await helper.ethNativeContract.collection(collectionAddress, 'nft', owner);
const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);
+ let {unsubscribe, collectedEvents: subEvents} = await helper.subscribeEvents([{section: 'common', names: ['CollectionPropertySet', 'CollectionPropertyDeleted']}]);
{
- const events: any = [];
+ const ethEvents: any = [];
collectionHelper.events.allEvents((_: any, event: any) => {
- events.push(event);
+ ethEvents.push(event);
});
await collection.methods.setCollectionProperties([{key: 'A', value: [0,1,2,3]}]).send({from:owner});
- expect(events).to.be.like([
+ expect(ethEvents).to.be.like([
{
event: 'CollectionChanged',
returnValues: {
@@ -75,14 +82,16 @@
}
}
]);
+ expect(subEvents).to.be.like([{method: 'CollectionPropertySet'}]);
+ subEvents.pop();
}
{
- const events: any = [];
+ const ethEvents: any = [];
collectionHelper.events.allEvents((_: any, event: any) => {
- events.push(event);
+ ethEvents.push(event);
});
await collection.methods.deleteCollectionProperties(['A']).send({from:owner});
- expect(events).to.be.like([
+ expect(ethEvents).to.be.like([
{
event: 'CollectionChanged',
returnValues: {
@@ -90,8 +99,9 @@
}
}
]);
+ expect(subEvents).to.be.like([{method: 'CollectionPropertyDeleted'}]);
}
-
+ unsubscribe();
});
itEth('CollectionChanged event for PropertyPermissionSet', async ({helper}) => {
@@ -99,12 +109,13 @@
const {collectionAddress} = await helper.eth.createCollecion('createNFTCollection', owner, 'A', 'B', 'C');
const collection = await helper.ethNativeContract.collection(collectionAddress, 'nft', owner);
const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);
- const events: any = [];
+ const eethEvents: any = [];
collectionHelper.events.allEvents((_: any, event: any) => {
- events.push(event);
+ eethEvents.push(event);
});
+ const {unsubscribe, collectedEvents: subEvents} = await helper.subscribeEvents([{section: 'common', names: ['PropertyPermissionSet']}]);
await collection.methods.setTokenPropertyPermission('testKey', true, true, true).send({from: owner});
- expect(events).to.be.like([
+ expect(eethEvents).to.be.like([
{
event: 'CollectionChanged',
returnValues: {
@@ -112,28 +123,236 @@
}
}
]);
+ expect(subEvents).to.be.like([{method: 'PropertyPermissionSet'}]);
+ unsubscribe();
+ });
+
+ itEth('CollectionChanged event for AllowListAddressAdded, AllowListAddressRemoved', async ({helper}) => {
+ const owner = await helper.eth.createAccountWithBalance(donor);
+ const user = helper.ethCrossAccount.createAccount();
+ const {collectionAddress} = await helper.eth.createCollecion('createNFTCollection', owner, 'A', 'B', 'C');
+ const collection = await helper.ethNativeContract.collection(collectionAddress, 'nft', owner);
+ const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);
+ const ethEvents: any[] = [];
+ collectionHelper.events.allEvents((_: any, event: any) => {
+ ethEvents.push(event);
+ });
+
+ const {unsubscribe, collectedEvents: subEvents} = await helper.subscribeEvents([{section: 'common', names: ['AllowListAddressAdded', 'AllowListAddressRemoved']}]);
+ {
+ await collection.methods.addToCollectionAllowListCross(user).send({from: owner});
+ expect(ethEvents).to.be.like([
+ {
+ event: 'CollectionChanged',
+ returnValues: {
+ collectionId: collectionAddress
+ }
+ }
+ ]);
+ expect(subEvents).to.be.like([{method: 'AllowListAddressAdded'}]);
+ ethEvents.pop();
+ subEvents.pop();
+ }
+ {
+ await collection.methods.removeFromCollectionAllowListCross(user).send({from: owner});
+ expect(ethEvents.length).to.be.eq(1);
+ expect(ethEvents).to.be.like([
+ {
+ event: 'CollectionChanged',
+ returnValues: {
+ collectionId: collectionAddress
+ }
+ }
+ ]);
+ expect(subEvents).to.be.like([{method: 'AllowListAddressRemoved'}]);
+ }
+ unsubscribe();
});
- // itEth('CollectionChanged event for AllowListAddressAdded', async ({helper}) => {
- // const owner = await helper.eth.createAccountWithBalance(donor);
- // const user = await helper.eth.createAccount();
- // const userCross = helper.ethCrossAccount.fromAddress(user);
- // const {collectionId} = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});
- // // allow list does not need to be enabled to add someone in advance
- // const collection = await helper.ethNativeContract.collection(collectionAddress, 'nft', owner);
- // const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);
- // const events: any = [];
- // collectionHelper.events.allEvents((_: any, event: any) => {
- // events.push(event);
- // });
- // await helper.nft.addToAllowList(alice, collectionId, {Substrate: bob.address});
- // expect(events).to.be.like([
- // {
- // event: 'CollectionChanged',
- // returnValues: {
- // collectionId: collectionAddress
- // }
- // }
- // ]);
- // });
+ itEth('CollectionChanged event for CollectionAdminAdded, CollectionAdminRemoved', async ({helper}) => {
+ const owner = await helper.eth.createAccountWithBalance(donor);
+ const user = helper.ethCrossAccount.createAccount();
+ const {collectionAddress} = await helper.eth.createCollecion('createNFTCollection', owner, 'A', 'B', 'C');
+ const collection = await helper.ethNativeContract.collection(collectionAddress, 'nft', owner);
+ const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);
+ const ethEvents: any = [];
+ collectionHelper.events.allEvents((_: any, event: any) => {
+ ethEvents.push(event);
+ });
+ const {unsubscribe, collectedEvents: subEvents} = await helper.subscribeEvents([{section: 'common', names: ['CollectionAdminAdded', 'CollectionAdminRemoved']}]);
+ {
+ await collection.methods.addCollectionAdminCross(user).send({from: owner});
+ expect(ethEvents).to.be.like([
+ {
+ event: 'CollectionChanged',
+ returnValues: {
+ collectionId: collectionAddress
+ }
+ }
+ ]);
+ expect(subEvents).to.be.like([{method: 'CollectionAdminAdded'}]);
+ ethEvents.pop();
+ subEvents.pop();
+ }
+ {
+ await collection.methods.removeCollectionAdminCross(user).send({from: owner});
+ expect(ethEvents).to.be.like([
+ {
+ event: 'CollectionChanged',
+ returnValues: {
+ collectionId: collectionAddress
+ }
+ }
+ ]);
+ expect(subEvents).to.be.like([{method: 'CollectionAdminRemoved'}]);
+ }
+ unsubscribe();
+ });
+
+ itEth('CollectionChanged event for CollectionLimitSet', async ({helper}) => {
+ const owner = await helper.eth.createAccountWithBalance(donor);
+ const {collectionAddress} = await helper.eth.createCollecion('createNFTCollection', owner, 'A', 'B', 'C');
+ const collection = await helper.ethNativeContract.collection(collectionAddress, 'nft', owner);
+ const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);
+ const ethEvents: any = [];
+ collectionHelper.events.allEvents((_: any, event: any) => {
+ ethEvents.push(event);
+ });
+ const {unsubscribe, collectedEvents: subEvents} = await helper.subscribeEvents([{section: 'common', names: ['CollectionLimitSet']}]);
+ {
+ await collection.methods.setCollectionLimit('ownerCanTransfer', 0n).send({from: owner});
+ expect(ethEvents).to.be.like([
+ {
+ event: 'CollectionChanged',
+ returnValues: {
+ collectionId: collectionAddress
+ }
+ }
+ ]);
+ expect(subEvents).to.be.like([{method: 'CollectionLimitSet'}]);
+ }
+ unsubscribe();
+ });
+
+ itEth('CollectionChanged event for CollectionOwnedChanged', async ({helper}) => {
+ const owner = await helper.eth.createAccountWithBalance(donor);
+ const new_owner = helper.ethCrossAccount.createAccount();
+ const {collectionAddress} = await helper.eth.createCollecion('createNFTCollection', owner, 'A', 'B', 'C');
+ const collection = await helper.ethNativeContract.collection(collectionAddress, 'nft', owner);
+ const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);
+ const ethEvents: any = [];
+ collectionHelper.events.allEvents((_: any, event: any) => {
+ ethEvents.push(event);
+ });
+ const {unsubscribe, collectedEvents: subEvents} = await helper.subscribeEvents([{section: 'common', names: ['CollectionOwnedChanged']}]);
+ {
+ await collection.methods.changeCollectionOwnerCross(new_owner).send({from: owner});
+ expect(ethEvents).to.be.like([
+ {
+ event: 'CollectionChanged',
+ returnValues: {
+ collectionId: collectionAddress
+ }
+ }
+ ]);
+ expect(subEvents).to.be.like([{method: 'CollectionOwnedChanged'}]);
+ }
+ unsubscribe();
+ });
+
+ itEth('CollectionChanged event for CollectionPermissionSet', async ({helper}) => {
+ const owner = await helper.eth.createAccountWithBalance(donor);
+ const {collectionAddress} = await helper.eth.createCollecion('createNFTCollection', owner, 'A', 'B', 'C');
+ const collection = await helper.ethNativeContract.collection(collectionAddress, 'nft', owner);
+ const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);
+ const ethEvents: any = [];
+ collectionHelper.events.allEvents((_: any, event: any) => {
+ ethEvents.push(event);
+ });
+ const {unsubscribe, collectedEvents: subEvents} = await helper.subscribeEvents([{section: 'common', names: ['CollectionPermissionSet']}]);
+ {
+ await collection.methods.setCollectionMintMode(true).send({from: owner});
+ expect(ethEvents).to.be.like([
+ {
+ event: 'CollectionChanged',
+ returnValues: {
+ collectionId: collectionAddress
+ }
+ }
+ ]);
+ expect(subEvents).to.be.like([{method: 'CollectionPermissionSet'}]);
+ ethEvents.pop();
+ subEvents.pop();
+ }
+ {
+ await collection.methods.setCollectionAccess(1).send({from: owner});
+ expect(ethEvents).to.be.like([
+ {
+ event: 'CollectionChanged',
+ returnValues: {
+ collectionId: collectionAddress
+ }
+ }
+ ]);
+ expect(subEvents).to.be.like([{method: 'CollectionPermissionSet'}]);
+ }
+ unsubscribe();
+ });
+
+ itEth('CollectionChanged event for CollectionSponsorSet, SponsorshipConfirmed, CollectionSponsorRemoved', async ({helper}) => {
+ const owner = await helper.eth.createAccountWithBalance(donor);
+ const sponsor = await helper.ethCrossAccount.createAccountWithBalance(donor);
+ const {collectionAddress} = await helper.eth.createCollecion('createNFTCollection', owner, 'A', 'B', 'C');
+ const collection = await helper.ethNativeContract.collection(collectionAddress, 'nft', owner);
+ const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);
+ const ethEvents: any = [];
+ collectionHelper.events.allEvents((_: any, event: any) => {
+ ethEvents.push(event);
+ });
+ const {unsubscribe, collectedEvents: subEvents} = await helper.subscribeEvents([{
+ section: 'common', names: ['CollectionSponsorSet', 'SponsorshipConfirmed', 'CollectionSponsorRemoved'
+ ]}]);
+ {
+ await collection.methods.setCollectionSponsorCross(sponsor).send({from: owner});
+ expect(ethEvents).to.be.like([
+ {
+ event: 'CollectionChanged',
+ returnValues: {
+ collectionId: collectionAddress
+ }
+ }
+ ]);
+ expect(subEvents).to.be.like([{method: 'CollectionSponsorSet'}]);
+ ethEvents.pop();
+ subEvents.pop();
+ }
+ {
+ await collection.methods.confirmCollectionSponsorship().send({from: sponsor.eth});
+ expect(ethEvents).to.be.like([
+ {
+ event: 'CollectionChanged',
+ returnValues: {
+ collectionId: collectionAddress
+ }
+ }
+ ]);
+ expect(subEvents).to.be.like([{method: 'SponsorshipConfirmed'}]);
+ ethEvents.pop();
+ subEvents.pop();
+ }
+ {
+ await collection.methods.removeCollectionSponsor().send({from: owner});
+ expect(ethEvents).to.be.like([
+ {
+ event: 'CollectionChanged',
+ returnValues: {
+ collectionId: collectionAddress
+ }
+ }
+ ]);
+ expect(subEvents).to.be.like([{method: 'CollectionSponsorRemoved'}]);
+ }
+ unsubscribe();
+ });
+
});
\ No newline at end of file
tests/src/removeCollectionAdmin.test.tsdiffbeforeafterboth--- a/tests/src/removeCollectionAdmin.test.ts
+++ b/tests/src/removeCollectionAdmin.test.ts
@@ -51,7 +51,7 @@
const adminListBeforeAddAdmin = await collection.getAdmins();
expect(adminListBeforeAddAdmin).to.have.lengthOf(0);
- await collection.removeAdmin(alice, {Substrate: alice.address});
+ await expect(collection.removeAdmin(alice, {Substrate: alice.address})).to.be.rejectedWith('common.UserIsNotAdmin');
});
});
tests/src/removeCollectionSponsor.test.tsdiffbeforeafterboth--- a/tests/src/removeCollectionSponsor.test.ts
+++ b/tests/src/removeCollectionSponsor.test.ts
@@ -112,7 +112,7 @@
const collection = await helper.nft.mintCollection(alice, {name: 'RemoveCollectionSponsor-Neg-4', tokenPrefix: 'RCS'});
await collection.setSponsor(alice, bob.address);
await collection.removeSponsor(alice);
- await expect(collection.confirmSponsorship(bob)).to.be.rejectedWith(/unique\.ConfirmUnsetSponsorFail/);
+ await expect(collection.confirmSponsorship(bob)).to.be.rejectedWith(/common\.ConfirmUnsetSponsorFail/);
});
itSub('Set - confirm - remove - confirm: Sponsor cannot come back', async ({helper}) => {
@@ -120,6 +120,6 @@
await collection.setSponsor(alice, bob.address);
await collection.confirmSponsorship(bob);
await collection.removeSponsor(alice);
- await expect(collection.confirmSponsorship(bob)).to.be.rejectedWith(/unique\.ConfirmUnsetSponsorFail/);
+ await expect(collection.confirmSponsorship(bob)).to.be.rejectedWith(/common\.ConfirmUnsetSponsorFail/);
});
});
tests/src/util/playgrounds/unique.tsdiffbeforeafterboth1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// SPDX-License-Identifier: Apache-2.034/* eslint-disable @typescript-eslint/no-var-requires */5/* eslint-disable function-call-argument-newline */6/* eslint-disable no-prototype-builtins */78import {ApiPromise, WsProvider, Keyring} from '@polkadot/api';9import {ApiInterfaceEvents, SignerOptions} from '@polkadot/api/types';10import {encodeAddress, decodeAddress, keccakAsHex, evmToAddress, addressToEvm, base58Encode, blake2AsU8a} from '@polkadot/util-crypto';11import {IKeyringPair} from '@polkadot/types/types';12import {hexToU8a} from '@polkadot/util/hex';13import {u8aConcat} from '@polkadot/util/u8a';14import {15 IApiListeners,16 IBlock,17 IEvent,18 IChainProperties,19 ICollectionCreationOptions,20 ICollectionLimits,21 ICollectionPermissions,22 ICrossAccountId,23 ICrossAccountIdLower,24 ILogger,25 INestingPermissions,26 IProperty,27 IStakingInfo,28 ISchedulerOptions,29 ISubstrateBalance,30 IToken,31 ITokenPropertyPermission,32 ITransactionResult,33 IUniqueHelperLog,34 TApiAllowedListeners,35 TEthereumAccount,36 TSigner,37 TSubstrateAccount,38 TNetworks,39 IForeignAssetMetadata,40 AcalaAssetMetadata,41 MoonbeamAssetInfo,42 DemocracyStandardAccountVote,43 IEthCrossAccountId,44} from './types';45import {RuntimeDispatchInfo} from '@polkadot/types/interfaces';4647export class CrossAccountId implements ICrossAccountId {48 Substrate?: TSubstrateAccount;49 Ethereum?: TEthereumAccount;5051 constructor(account: ICrossAccountId) {52 if (account.Substrate) this.Substrate = account.Substrate;53 if (account.Ethereum) this.Ethereum = account.Ethereum;54 }5556 static fromKeyring(account: IKeyringPair, domain: 'Substrate' | 'Ethereum' = 'Substrate') {57 switch (domain) {58 case 'Substrate': return new CrossAccountId({Substrate: account.address});59 case 'Ethereum': return new CrossAccountId({Substrate: account.address}).toEthereum();60 }61 }6263 static fromLowerCaseKeys(address: ICrossAccountIdLower): CrossAccountId {64 return new CrossAccountId({Substrate: address.substrate, Ethereum: address.ethereum});65 }6667 static normalizeSubstrateAddress(address: TSubstrateAccount, ss58Format = 42): TSubstrateAccount {68 return encodeAddress(decodeAddress(address), ss58Format);69 }7071 static withNormalizedSubstrate(address: TSubstrateAccount, ss58Format = 42): CrossAccountId {72 return new CrossAccountId({Substrate: CrossAccountId.normalizeSubstrateAddress(address, ss58Format)});73 }7475 withNormalizedSubstrate(ss58Format = 42): CrossAccountId {76 if (this.Substrate) return CrossAccountId.withNormalizedSubstrate(this.Substrate, ss58Format);77 return this;78 }7980 static translateSubToEth(address: TSubstrateAccount): TEthereumAccount {81 return nesting.toChecksumAddress('0x' + Array.from(addressToEvm(address), i => i.toString(16).padStart(2, '0')).join(''));82 }8384 toEthereum(): CrossAccountId {85 if (this.Substrate) return new CrossAccountId({Ethereum: CrossAccountId.translateSubToEth(this.Substrate)});86 return this;87 }8889 static translateEthToSub(address: TEthereumAccount, ss58Format?: number): TSubstrateAccount {90 return evmToAddress(address, ss58Format);91 }9293 toSubstrate(ss58Format?: number): CrossAccountId {94 if (this.Ethereum) return new CrossAccountId({Substrate: CrossAccountId.translateEthToSub(this.Ethereum, ss58Format)});95 return this;96 }9798 toLowerCase(): CrossAccountId {99 if (this.Substrate) this.Substrate = this.Substrate.toLowerCase();100 if (this.Ethereum) this.Ethereum = this.Ethereum.toLowerCase();101 return this;102 }103}104105const nesting = {106 toChecksumAddress(address: string): string {107 if (typeof address === 'undefined') return '';108109 if(!/^(0x)?[0-9a-f]{40}$/i.test(address)) throw new Error(`Given address "${address}" is not a valid Ethereum address.`);110111 address = address.toLowerCase().replace(/^0x/i,'');112 const addressHash = keccakAsHex(address).replace(/^0x/i,'');113 const checksumAddress = ['0x'];114115 for (let i = 0; i < address.length; i++) {116 // If ith character is 8 to f then make it uppercase117 if (parseInt(addressHash[i], 16) > 7) {118 checksumAddress.push(address[i].toUpperCase());119 } else {120 checksumAddress.push(address[i]);121 }122 }123 return checksumAddress.join('');124 },125 tokenIdToAddress(collectionId: number, tokenId: number) {126 return this.toChecksumAddress(`0xf8238ccfff8ed887463fd5e0${collectionId.toString(16).padStart(8,'0')}${tokenId.toString(16).padStart(8,'0')}`);127 },128};129130class UniqueUtil {131 static transactionStatus = {132 NOT_READY: 'NotReady',133 FAIL: 'Fail',134 SUCCESS: 'Success',135 };136137 static chainLogType = {138 EXTRINSIC: 'extrinsic',139 RPC: 'rpc',140 };141142 static getTokenAccount(token: IToken): CrossAccountId {143 return new CrossAccountId({Ethereum: this.getTokenAddress(token)});144 }145146 static getTokenAddress(token: IToken): string {147 return nesting.tokenIdToAddress(token.collectionId, token.tokenId);148 }149150 static getDefaultLogger(): ILogger {151 return {152 log(msg: any, level = 'INFO') {153 console[level.toLocaleLowerCase() === 'error' ? 'error' : 'log'](...(Array.isArray(msg) ? msg : [msg]));154 },155 level: {156 ERROR: 'ERROR',157 WARNING: 'WARNING',158 INFO: 'INFO',159 },160 };161 }162163 static vec2str(arr: string[] | number[]) {164 return arr.map(x => String.fromCharCode(parseInt(x.toString()))).join('');165 }166167 static str2vec(string: string) {168 if (typeof string !== 'string') return string;169 return Array.from(string).map(x => x.charCodeAt(0));170 }171172 static fromSeed(seed: string, ss58Format = 42) {173 const keyring = new Keyring({type: 'sr25519', ss58Format});174 return keyring.addFromUri(seed);175 }176177 static extractCollectionIdFromCreationResult(creationResult: ITransactionResult): number {178 if (creationResult.status !== this.transactionStatus.SUCCESS) {179 throw Error('Unable to create collection!');180 }181182 let collectionId = null;183 creationResult.result.events.forEach(({event: {data, method, section}}) => {184 if ((section === 'common') && (method === 'CollectionCreated')) {185 collectionId = parseInt(data[0].toString(), 10);186 }187 });188189 if (collectionId === null) {190 throw Error('No CollectionCreated event was found!');191 }192193 return collectionId;194 }195196 static extractTokensFromCreationResult(creationResult: ITransactionResult): {197 success: boolean,198 tokens: {collectionId: number, tokenId: number, owner: CrossAccountId, amount: bigint}[],199 } {200 if (creationResult.status !== this.transactionStatus.SUCCESS) {201 throw Error('Unable to create tokens!');202 }203 let success = false;204 const tokens = [] as {collectionId: number, tokenId: number, owner: CrossAccountId, amount: bigint}[];205 creationResult.result.events.forEach(({event: {data, method, section}}) => {206 if (method === 'ExtrinsicSuccess') {207 success = true;208 } else if ((section === 'common') && (method === 'ItemCreated')) {209 tokens.push({210 collectionId: parseInt(data[0].toString(), 10),211 tokenId: parseInt(data[1].toString(), 10),212 owner: data[2].toHuman(),213 amount: data[3].toBigInt(),214 });215 }216 });217 return {success, tokens};218 }219220 static extractTokensFromBurnResult(burnResult: ITransactionResult): {221 success: boolean,222 tokens: {collectionId: number, tokenId: number, owner: CrossAccountId, amount: bigint}[],223 } {224 if (burnResult.status !== this.transactionStatus.SUCCESS) {225 throw Error('Unable to burn tokens!');226 }227 let success = false;228 const tokens = [] as {collectionId: number, tokenId: number, owner: CrossAccountId, amount: bigint}[];229 burnResult.result.events.forEach(({event: {data, method, section}}) => {230 if (method === 'ExtrinsicSuccess') {231 success = true;232 } else if ((section === 'common') && (method === 'ItemDestroyed')) {233 tokens.push({234 collectionId: parseInt(data[0].toString(), 10),235 tokenId: parseInt(data[1].toString(), 10),236 owner: data[2].toHuman(),237 amount: data[3].toBigInt(),238 });239 }240 });241 return {success, tokens};242 }243244 static findCollectionInEvents(events: {event: IEvent}[], collectionId: number, expectedSection: string, expectedMethod: string): boolean {245 let eventId = null;246 events.forEach(({event: {data, method, section}}) => {247 if ((section === expectedSection) && (method === expectedMethod)) {248 eventId = parseInt(data[0].toString(), 10);249 }250 });251252 if (eventId === null) {253 throw Error(`No ${expectedMethod} event was found!`);254 }255 return eventId === collectionId;256 }257258 static isTokenTransferSuccess(events: {event: IEvent}[], collectionId: number, tokenId: number, fromAddressObj: ICrossAccountId, toAddressObj: ICrossAccountId, amount=1n) {259 const normalizeAddress = (address: string | ICrossAccountId) => {260 if(typeof address === 'string') return address;261 const obj = {} as any;262 Object.keys(address).forEach(k => {263 obj[k.toLocaleLowerCase()] = address[k as 'Substrate' | 'Ethereum'];264 });265 if(obj.substrate) return CrossAccountId.withNormalizedSubstrate(obj.substrate);266 if(obj.ethereum) return CrossAccountId.fromLowerCaseKeys(obj).toLowerCase();267 return address;268 };269 let transfer = {collectionId: null, tokenId: null, from: null, to: null, amount: 1} as any;270 events.forEach(({event: {data, method, section}}) => {271 if ((section === 'common') && (method === 'Transfer')) {272 const hData = (data as any).toJSON();273 transfer = {274 collectionId: hData[0],275 tokenId: hData[1],276 from: normalizeAddress(hData[2]),277 to: normalizeAddress(hData[3]),278 amount: BigInt(hData[4]),279 };280 }281 });282 let isSuccess = parseInt(collectionId.toString()) === transfer.collectionId && parseInt(tokenId.toString()) === transfer.tokenId;283 isSuccess = isSuccess && JSON.stringify(normalizeAddress(fromAddressObj)) === JSON.stringify(transfer.from);284 isSuccess = isSuccess && JSON.stringify(normalizeAddress(toAddressObj)) === JSON.stringify(transfer.to);285 isSuccess = isSuccess && amount === transfer.amount;286 return isSuccess;287 }288289 static bigIntToDecimals(number: bigint, decimals = 18) {290 const numberStr = number.toString();291 const dotPos = numberStr.length - decimals;292293 if (dotPos <= 0) {294 return '0.' + '0'.repeat(Math.abs(dotPos)) + numberStr;295 } else {296 const intPart = numberStr.substring(0, dotPos);297 const fractPart = numberStr.substring(dotPos);298 return intPart + '.' + fractPart;299 }300 }301}302303class UniqueEventHelper {304 private static extractIndex(index: any): [number, number] | string {305 if(index.toRawType() === '[u8;2]') return [index[0], index[1]];306 return index.toJSON();307 }308309 private static extractSub(data: any, subTypes: any): {[key: string]: any} {310 let obj: any = {};311 let index = 0;312313 if (data.entries) {314 for(const [key, value] of data.entries()) {315 obj[key] = this.extractData(value, subTypes[index]);316 index++;317 }318 } else obj = data.toJSON();319320 return obj;321 }322323 private static extractData(data: any, type: any): any {324 if(!type) return data.toHuman();325 if (['u16', 'u32'].indexOf(type.type) > -1) return data.toNumber();326 if (['u64', 'u128', 'u256'].indexOf(type.type) > -1) return data.toBigInt();327 if(type.hasOwnProperty('sub')) return this.extractSub(data, type.sub);328 return data.toHuman();329 }330331 public static extractEvents(events: {event: any, phase: any}[]): IEvent[] {332 const parsedEvents: IEvent[] = [];333334 events.forEach((record) => {335 const {event, phase} = record;336 const types = event.typeDef;337338 const eventData: IEvent = {339 section: event.section.toString(),340 method: event.method.toString(),341 index: this.extractIndex(event.index),342 data: [],343 phase: phase.toJSON(),344 };345346 event.data.forEach((val: any, index: number) => {347 eventData.data.push(this.extractData(val, types[index]));348 });349350 parsedEvents.push(eventData);351 });352353 return parsedEvents;354 }355}356357export class ChainHelperBase {358 helperBase: any;359360 transactionStatus = UniqueUtil.transactionStatus;361 chainLogType = UniqueUtil.chainLogType;362 util: typeof UniqueUtil;363 eventHelper: typeof UniqueEventHelper;364 logger: ILogger;365 api: ApiPromise | null;366 forcedNetwork: TNetworks | null;367 network: TNetworks | null;368 chainLog: IUniqueHelperLog[];369 children: ChainHelperBase[];370 address: AddressGroup;371 chain: ChainGroup;372373 constructor(logger?: ILogger, helperBase?: any) {374 this.helperBase = helperBase;375376 this.util = UniqueUtil;377 this.eventHelper = UniqueEventHelper;378 if (typeof logger == 'undefined') logger = this.util.getDefaultLogger();379 this.logger = logger;380 this.api = null;381 this.forcedNetwork = null;382 this.network = null;383 this.chainLog = [];384 this.children = [];385 this.address = new AddressGroup(this);386 this.chain = new ChainGroup(this);387 }388389 clone(helperCls: ChainHelperBaseConstructor, options: {[key: string]: any} = {}) {390 Object.setPrototypeOf(helperCls.prototype, this);391 const newHelper = new helperCls(this.logger, options);392393 newHelper.api = this.api;394 newHelper.network = this.network;395 newHelper.forceNetwork = this.forceNetwork;396397 this.children.push(newHelper);398399 return newHelper;400 }401402 getApi(): ApiPromise {403 if(this.api === null) throw Error('API not initialized');404 return this.api;405 }406407 clearChainLog(): void {408 this.chainLog = [];409 }410411 forceNetwork(value: TNetworks): void {412 this.forcedNetwork = value;413 }414415 async connect(wsEndpoint: string, listeners?: IApiListeners) {416 if (this.api !== null) throw Error('Already connected');417 const {api, network} = await ChainHelperBase.createConnection(wsEndpoint, listeners, this.forcedNetwork);418 this.api = api;419 this.network = network;420 }421422 async disconnect() {423 for (const child of this.children) {424 child.clearApi();425 }426427 if (this.api === null) return;428 await this.api.disconnect();429 this.clearApi();430 }431432 clearApi() {433 this.api = null;434 this.network = null;435 }436437 static async detectNetwork(api: ApiPromise): Promise<TNetworks> {438 const spec = (await api.query.system.lastRuntimeUpgrade()).toJSON() as any;439 const xcmChains = ['rococo', 'westend', 'westmint', 'acala', 'karura', 'moonbeam', 'moonriver'];440441 if(xcmChains.indexOf(spec.specName) > -1) return spec.specName;442443 if(['quartz', 'unique'].indexOf(spec.specName) > -1) return spec.specName;444 return 'opal';445 }446447 static async detectNetworkByWsEndpoint(wsEndpoint: string): Promise<TNetworks> {448 const api = new ApiPromise({provider: new WsProvider(wsEndpoint)});449 await api.isReady;450451 const network = await this.detectNetwork(api);452453 await api.disconnect();454455 return network;456 }457458 static async createConnection(wsEndpoint: string, listeners?: IApiListeners, network?: TNetworks | null): Promise<{459 api: ApiPromise;460 network: TNetworks;461 }> {462 if(typeof network === 'undefined' || network === null) network = 'opal';463 const supportedRPC = {464 opal: {465 unique: require('@unique-nft/opal-testnet-types/definitions').unique.rpc,466 },467 quartz: {468 unique: require('@unique-nft/quartz-mainnet-types/definitions').unique.rpc,469 },470 unique: {471 unique: require('@unique-nft/unique-mainnet-types/definitions').unique.rpc,472 },473 rococo: {},474 westend: {},475 moonbeam: {},476 moonriver: {},477 acala: {},478 karura: {},479 westmint: {},480 };481 if(!supportedRPC.hasOwnProperty(network)) network = await this.detectNetworkByWsEndpoint(wsEndpoint);482 const rpc = supportedRPC[network];483484 // TODO: investigate how to replace rpc in runtime485 // api._rpcCore.addUserInterfaces(rpc);486487 const api = new ApiPromise({provider: new WsProvider(wsEndpoint), rpc});488489 await api.isReadyOrError;490491 if (typeof listeners === 'undefined') listeners = {};492 for (const event of ['connected', 'disconnected', 'error', 'ready', 'decorated']) {493 if (!listeners.hasOwnProperty(event) || typeof listeners[event as TApiAllowedListeners] === 'undefined') continue;494 api.on(event as ApiInterfaceEvents, listeners[event as TApiAllowedListeners] as (...args: any[]) => any);495 }496497 return {api, network};498 }499500 getTransactionStatus(data: {events: {event: IEvent}[], status: any}) {501 const {events, status} = data;502 if (status.isReady) {503 return this.transactionStatus.NOT_READY;504 }505 if (status.isBroadcast) {506 return this.transactionStatus.NOT_READY;507 }508 if (status.isInBlock || status.isFinalized) {509 const errors = events.filter(e => e.event.method === 'ExtrinsicFailed');510 if (errors.length > 0) {511 return this.transactionStatus.FAIL;512 }513 if (events.filter(e => e.event.method === 'ExtrinsicSuccess').length > 0) {514 return this.transactionStatus.SUCCESS;515 }516 }517518 return this.transactionStatus.FAIL;519 }520521 signTransaction(sender: TSigner, transaction: any, options: Partial<SignerOptions> | null = null, label = 'transaction') {522 const sign = (callback: any) => {523 if(options !== null) return transaction.signAndSend(sender, options, callback);524 return transaction.signAndSend(sender, callback);525 };526 // eslint-disable-next-line no-async-promise-executor527 return new Promise(async (resolve, reject) => {528 try {529 const unsub = await sign((result: any) => {530 const status = this.getTransactionStatus(result);531532 if (status === this.transactionStatus.SUCCESS) {533 this.logger.log(`${label} successful`);534 unsub();535 resolve({result, status});536 } else if (status === this.transactionStatus.FAIL) {537 let moduleError = null;538539 if (result.hasOwnProperty('dispatchError')) {540 const dispatchError = result['dispatchError'];541542 if (dispatchError) {543 if (dispatchError.isModule) {544 const modErr = dispatchError.asModule;545 const errorMeta = dispatchError.registry.findMetaError(modErr);546547 moduleError = `${errorMeta.section}.${errorMeta.name}`;548 } else {549 moduleError = dispatchError.toHuman();550 }551 } else {552 this.logger.log(result, this.logger.level.ERROR);553 }554 }555556 this.logger.log(`Something went wrong with ${label}. Status: ${status}`, this.logger.level.ERROR);557 unsub();558 reject({status, moduleError, result});559 }560 });561 } catch (e) {562 this.logger.log(e, this.logger.level.ERROR);563 reject(e);564 }565 });566 }567568 async getPaymentInfo(signer: TSigner, tx: any, len: number | null) {569 const api = this.getApi();570 const signingInfo = await api.derive.tx.signingInfo(signer.address);571572 // We need to sign the tx because573 // unsigned transactions does not have an inclusion fee574 tx.sign(signer, {575 blockHash: api.genesisHash,576 genesisHash: api.genesisHash,577 runtimeVersion: api.runtimeVersion,578 nonce: signingInfo.nonce,579 });580581 if (len === null) {582 return (await this.callRpc('api.rpc.payment.queryInfo', [tx.toHex()])) as RuntimeDispatchInfo;583 } else {584 return (await api.call.transactionPaymentApi.queryInfo(tx, len)) as RuntimeDispatchInfo;585 }586 }587588 constructApiCall(apiCall: string, params: any[]) {589 if(!apiCall.startsWith('api.')) throw Error(`Invalid api call: ${apiCall}`);590 let call = this.getApi() as any;591 for(const part of apiCall.slice(4).split('.')) {592 call = call[part];593 }594 return call(...params);595 }596597 async executeExtrinsic(sender: TSigner, extrinsic: string, params: any[], expectSuccess=true, options: Partial<SignerOptions>|null = null/*, failureMessage='expected success'*/) {598 if(this.api === null) throw Error('API not initialized');599 if(!extrinsic.startsWith('api.tx.')) throw Error(`${extrinsic} is not transaction`);600601 const startTime = (new Date()).getTime();602 let result: ITransactionResult;603 let events: IEvent[] = [];604 try {605 result = await this.signTransaction(sender, this.constructApiCall(extrinsic, params), options, extrinsic) as ITransactionResult;606 events = this.eventHelper.extractEvents(result.result.events);607 }608 catch(e) {609 if(!(e as object).hasOwnProperty('status')) throw e;610 result = e as ITransactionResult;611 }612613 const endTime = (new Date()).getTime();614615 const log = {616 executedAt: endTime,617 executionTime: endTime - startTime,618 type: this.chainLogType.EXTRINSIC,619 status: result.status,620 call: extrinsic,621 signer: this.getSignerAddress(sender),622 params,623 } as IUniqueHelperLog;624625 if(result.status !== this.transactionStatus.SUCCESS) {626 if (result.moduleError) log.moduleError = result.moduleError;627 else if (result.result.dispatchError) log.dispatchError = result.result.dispatchError;628 }629 if(events.length > 0) log.events = events;630631 this.chainLog.push(log);632633 if(expectSuccess && result.status !== this.transactionStatus.SUCCESS) {634 if (result.moduleError) throw Error(`${result.moduleError}`);635 else if (result.result.dispatchError) throw Error(JSON.stringify(result.result.dispatchError));636 }637 return result;638 }639640 async callRpc(rpc: string, params?: any[]) {641 if(typeof params === 'undefined') params = [];642 if(this.api === null) throw Error('API not initialized');643 if(!rpc.startsWith('api.rpc.') && !rpc.startsWith('api.query.')) throw Error(`${rpc} is not RPC call`);644645 const startTime = (new Date()).getTime();646 let result;647 let error = null;648 const log = {649 type: this.chainLogType.RPC,650 call: rpc,651 params,652 } as IUniqueHelperLog;653654 try {655 result = await this.constructApiCall(rpc, params);656 }657 catch(e) {658 error = e;659 }660661 const endTime = (new Date()).getTime();662663 log.executedAt = endTime;664 log.status = (error === null ? this.transactionStatus.SUCCESS : this.transactionStatus.FAIL) as 'Fail' | 'Success';665 log.executionTime = endTime - startTime;666667 this.chainLog.push(log);668669 if(error !== null) throw error;670671 return result;672 }673674 getSignerAddress(signer: IKeyringPair | string): string {675 if(typeof signer === 'string') return signer;676 return signer.address;677 }678679 fetchAllPalletNames(): string[] {680 if(this.api === null) throw Error('API not initialized');681 return this.api.runtimeMetadata.asLatest.pallets.map(m => m.name.toString().toLowerCase());682 }683684 fetchMissingPalletNames(requiredPallets: string[]): string[] {685 const palletNames = this.fetchAllPalletNames();686 return requiredPallets.filter(p => !palletNames.includes(p));687 }688}689690691class HelperGroup<T extends ChainHelperBase> {692 helper: T;693694 constructor(uniqueHelper: T) {695 this.helper = uniqueHelper;696 }697}698699700class CollectionGroup extends HelperGroup<UniqueHelper> {701 /**702 * Get number of blocks when sponsored transaction is available.703 *704 * @param collectionId ID of collection705 * @param tokenId ID of token706 * @param addressObj address for which the sponsorship is checked707 * @example await getTokenNextSponsored(1, 2, {Substrate: '5DfhbVfww7ThF8q6f3...'});708 * @returns number of blocks or null if sponsorship hasn't been set709 */710 async getTokenNextSponsored(collectionId: number, tokenId: number, addressObj: ICrossAccountId): Promise<number | null> {711 return (await this.helper.callRpc('api.rpc.unique.nextSponsored', [collectionId, addressObj, tokenId])).toJSON();712 }713714 /**715 * Get the number of created collections.716 *717 * @returns number of created collections718 */719 async getTotalCount(): Promise<number> {720 return (await this.helper.callRpc('api.rpc.unique.collectionStats')).created.toNumber();721 }722723 /**724 * Get information about the collection with additional data,725 * including the number of tokens it contains, its administrators,726 * the normalized address of the collection's owner, and decoded name and description.727 *728 * @param collectionId ID of collection729 * @example await getData(2)730 * @returns collection information object731 */732 async getData(collectionId: number): Promise<{733 id: number;734 name: string;735 description: string;736 tokensCount: number;737 admins: CrossAccountId[];738 normalizedOwner: TSubstrateAccount;739 raw: any740 } | null> {741 const collection = await this.helper.callRpc('api.rpc.unique.collectionById', [collectionId]);742 const humanCollection = collection.toHuman(), collectionData = {743 id: collectionId, name: null, description: null, tokensCount: 0, admins: [],744 raw: humanCollection,745 } as any, jsonCollection = collection.toJSON();746 if (humanCollection === null) return null;747 collectionData.raw.limits = jsonCollection.limits;748 collectionData.raw.permissions = jsonCollection.permissions;749 collectionData.normalizedOwner = this.helper.address.normalizeSubstrate(collectionData.raw.owner);750 for (const key of ['name', 'description']) {751 collectionData[key] = this.helper.util.vec2str(humanCollection[key]);752 }753754 collectionData.tokensCount = (['RFT', 'NFT'].includes(humanCollection.mode))755 ? await this.helper[humanCollection.mode.toLocaleLowerCase() as 'nft' | 'rft'].getLastTokenId(collectionId)756 : 0;757 collectionData.admins = await this.getAdmins(collectionId);758759 return collectionData;760 }761762 /**763 * Get the addresses of the collection's administrators, optionally normalized.764 *765 * @param collectionId ID of collection766 * @param normalize whether to normalize the addresses to the default ss58 format767 * @example await getAdmins(1)768 * @returns array of administrators769 */770 async getAdmins(collectionId: number, normalize = false): Promise<CrossAccountId[]> {771 const admins = (await this.helper.callRpc('api.rpc.unique.adminlist', [collectionId])).toHuman();772773 return normalize774 ? admins.map((address: CrossAccountId) => address.withNormalizedSubstrate())775 : admins;776 }777778 /**779 * Get the addresses added to the collection allow-list, optionally normalized.780 * @param collectionId ID of collection781 * @param normalize whether to normalize the addresses to the default ss58 format782 * @example await getAllowList(1)783 * @returns array of allow-listed addresses784 */785 async getAllowList(collectionId: number, normalize = false): Promise<CrossAccountId[]> {786 const allowListed = (await this.helper.callRpc('api.rpc.unique.allowlist', [collectionId])).toHuman();787 return normalize788 ? allowListed.map((address: CrossAccountId) => address.withNormalizedSubstrate())789 : allowListed;790 }791792 /**793 * Get the effective limits of the collection instead of null for default values794 *795 * @param collectionId ID of collection796 * @example await getEffectiveLimits(2)797 * @returns object of collection limits798 */799 async getEffectiveLimits(collectionId: number): Promise<ICollectionLimits> {800 return (await this.helper.callRpc('api.rpc.unique.effectiveCollectionLimits', [collectionId])).toJSON();801 }802803 /**804 * Burns the collection if the signer has sufficient permissions and collection is empty.805 *806 * @param signer keyring of signer807 * @param collectionId ID of collection808 * @example await helper.collection.burn(aliceKeyring, 3);809 * @returns ```true``` if extrinsic success, otherwise ```false```810 */811 async burn(signer: TSigner, collectionId: number): Promise<boolean> {812 const result = await this.helper.executeExtrinsic(813 signer,814 'api.tx.unique.destroyCollection', [collectionId],815 true,816 );817818 return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'common', 'CollectionDestroyed');819 }820821 /**822 * Sets the sponsor for the collection (Requires the Substrate address). Needs confirmation by the sponsor.823 *824 * @param signer keyring of signer825 * @param collectionId ID of collection826 * @param sponsorAddress Sponsor substrate address827 * @example setSponsor(aliceKeyring, 10, "5DyN4Y92vZCjv38fg...")828 * @returns ```true``` if extrinsic success, otherwise ```false```829 */830 async setSponsor(signer: TSigner, collectionId: number, sponsorAddress: TSubstrateAccount): Promise<boolean> {831 const result = await this.helper.executeExtrinsic(832 signer,833 'api.tx.unique.setCollectionSponsor', [collectionId, sponsorAddress],834 true,835 );836837 return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'unique', 'CollectionSponsorSet');838 }839840 /**841 * Confirms consent to sponsor the collection on behalf of the signer.842 *843 * @param signer keyring of signer844 * @param collectionId ID of collection845 * @example confirmSponsorship(aliceKeyring, 10)846 * @returns ```true``` if extrinsic success, otherwise ```false```847 */848 async confirmSponsorship(signer: TSigner, collectionId: number): Promise<boolean> {849 const result = await this.helper.executeExtrinsic(850 signer,851 'api.tx.unique.confirmSponsorship', [collectionId],852 true,853 );854855 return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'unique', 'SponsorshipConfirmed');856 }857858 /**859 * Removes the sponsor of a collection, regardless if it consented or not.860 *861 * @param signer keyring of signer862 * @param collectionId ID of collection863 * @example removeSponsor(aliceKeyring, 10)864 * @returns ```true``` if extrinsic success, otherwise ```false```865 */866 async removeSponsor(signer: TSigner, collectionId: number): Promise<boolean> {867 const result = await this.helper.executeExtrinsic(868 signer,869 'api.tx.unique.removeCollectionSponsor', [collectionId],870 true,871 );872873 return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'unique', 'CollectionSponsorRemoved');874 }875876 /**877 * Sets the limits of the collection. At least one limit must be specified for a correct call.878 *879 * @param signer keyring of signer880 * @param collectionId ID of collection881 * @param limits collection limits object882 * @example883 * await setLimits(884 * aliceKeyring,885 * 10,886 * {887 * sponsorTransferTimeout: 0,888 * ownerCanDestroy: false889 * }890 * )891 * @returns ```true``` if extrinsic success, otherwise ```false```892 */893 async setLimits(signer: TSigner, collectionId: number, limits: ICollectionLimits): Promise<boolean> {894 const result = await this.helper.executeExtrinsic(895 signer,896 'api.tx.unique.setCollectionLimits', [collectionId, limits],897 true,898 );899900 return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'unique', 'CollectionLimitSet');901 }902903 /**904 * Changes the owner of the collection to the new Substrate address.905 *906 * @param signer keyring of signer907 * @param collectionId ID of collection908 * @param ownerAddress substrate address of new owner909 * @example changeOwner(aliceKeyring, 10, "5DyN4Y92vZCjv38fg...")910 * @returns ```true``` if extrinsic success, otherwise ```false```911 */912 async changeOwner(signer: TSigner, collectionId: number, ownerAddress: TSubstrateAccount): Promise<boolean> {913 const result = await this.helper.executeExtrinsic(914 signer,915 'api.tx.unique.changeCollectionOwner', [collectionId, ownerAddress],916 true,917 );918919 return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'unique', 'CollectionOwnedChanged');920 }921922 /**923 * Adds a collection administrator.924 *925 * @param signer keyring of signer926 * @param collectionId ID of collection927 * @param adminAddressObj Administrator address (substrate or ethereum)928 * @example addAdmin(aliceKeyring, 10, {Substrate: "5DyN4Y92vZCjv38fg..."})929 * @returns ```true``` if extrinsic success, otherwise ```false```930 */931 async addAdmin(signer: TSigner, collectionId: number, adminAddressObj: ICrossAccountId): Promise<boolean> {932 const result = await this.helper.executeExtrinsic(933 signer,934 'api.tx.unique.addCollectionAdmin', [collectionId, adminAddressObj],935 true,936 );937938 return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'unique', 'CollectionAdminAdded');939 }940941 /**942 * Removes a collection administrator.943 *944 * @param signer keyring of signer945 * @param collectionId ID of collection946 * @param adminAddressObj Administrator address (substrate or ethereum)947 * @example removeAdmin(aliceKeyring, 10, {Substrate: "5DyN4Y92vZCjv38fg..."})948 * @returns ```true``` if extrinsic success, otherwise ```false```949 */950 async removeAdmin(signer: TSigner, collectionId: number, adminAddressObj: ICrossAccountId): Promise<boolean> {951 const result = await this.helper.executeExtrinsic(952 signer,953 'api.tx.unique.removeCollectionAdmin', [collectionId, adminAddressObj],954 true,955 );956957 return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'unique', 'CollectionAdminRemoved');958 }959960 /**961 * Check if user is in allow list.962 *963 * @param collectionId ID of collection964 * @param user Account to check965 * @example await getAdmins(1)966 * @returns is user in allow list967 */968 async allowed(collectionId: number, user: ICrossAccountId): Promise<boolean> {969 return (await this.helper.callRpc('api.rpc.unique.allowed', [collectionId, user])).toJSON();970 }971972 /**973 * Adds an address to allow list974 * @param signer keyring of signer975 * @param collectionId ID of collection976 * @param addressObj address to add to the allow list977 * @returns ```true``` if extrinsic success, otherwise ```false```978 */979 async addToAllowList(signer: TSigner, collectionId: number, addressObj: ICrossAccountId): Promise<boolean> {980 const result = await this.helper.executeExtrinsic(981 signer,982 'api.tx.unique.addToAllowList', [collectionId, addressObj],983 true,984 );985986 return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'unique', 'AllowListAddressAdded');987 }988989 /**990 * Removes an address from allow list991 *992 * @param signer keyring of signer993 * @param collectionId ID of collection994 * @param addressObj address to remove from the allow list995 * @returns ```true``` if extrinsic success, otherwise ```false```996 */997 async removeFromAllowList(signer: TSigner, collectionId: number, addressObj: ICrossAccountId): Promise<boolean> {998 const result = await this.helper.executeExtrinsic(999 signer,1000 'api.tx.unique.removeFromAllowList', [collectionId, addressObj],1001 true,1002 );10031004 return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'unique', 'AllowListAddressRemoved');1005 }10061007 /**1008 * Sets onchain permissions for selected collection.1009 *1010 * @param signer keyring of signer1011 * @param collectionId ID of collection1012 * @param permissions collection permissions object1013 * @example setPermissions(aliceKeyring, 10, {access:'AllowList', mintMode: true, nesting: {collectionAdmin: true, tokenOwner: true}});1014 * @returns ```true``` if extrinsic success, otherwise ```false```1015 */1016 async setPermissions(signer: TSigner, collectionId: number, permissions: ICollectionPermissions): Promise<boolean> {1017 const result = await this.helper.executeExtrinsic(1018 signer,1019 'api.tx.unique.setCollectionPermissions', [collectionId, permissions],1020 true,1021 );10221023 return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'unique', 'CollectionPermissionSet');1024 }10251026 /**1027 * Enables nesting for selected collection. If `restricted` set, you can nest only tokens from specified collections.1028 *1029 * @param signer keyring of signer1030 * @param collectionId ID of collection1031 * @param permissions nesting permissions object1032 * @example enableNesting(aliceKeyring, 10, {collectionAdmin: true, tokenOwner: true});1033 * @returns ```true``` if extrinsic success, otherwise ```false```1034 */1035 async enableNesting(signer: TSigner, collectionId: number, permissions: INestingPermissions): Promise<boolean> {1036 return await this.setPermissions(signer, collectionId, {nesting: permissions});1037 }10381039 /**1040 * Disables nesting for selected collection.1041 *1042 * @param signer keyring of signer1043 * @param collectionId ID of collection1044 * @example disableNesting(aliceKeyring, 10);1045 * @returns ```true``` if extrinsic success, otherwise ```false```1046 */1047 async disableNesting(signer: TSigner, collectionId: number): Promise<boolean> {1048 return await this.setPermissions(signer, collectionId, {nesting: {tokenOwner: false, collectionAdmin: false}});1049 }10501051 /**1052 * Sets onchain properties to the collection.1053 *1054 * @param signer keyring of signer1055 * @param collectionId ID of collection1056 * @param properties array of property objects1057 * @example setProperties(aliceKeyring, 10, [{key: "gender", value: "male"}]);1058 * @returns ```true``` if extrinsic success, otherwise ```false```1059 */1060 async setProperties(signer: TSigner, collectionId: number, properties: IProperty[]): Promise<boolean> {1061 const result = await this.helper.executeExtrinsic(1062 signer,1063 'api.tx.unique.setCollectionProperties', [collectionId, properties],1064 true,1065 );10661067 return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'common', 'CollectionPropertySet');1068 }10691070 /**1071 * Get collection properties.1072 *1073 * @param collectionId ID of collection1074 * @param propertyKeys optionally filter the returned properties to only these keys1075 * @example getProperties(1219, ['location', 'date', 'time', 'isParadise']);1076 * @returns array of key-value pairs1077 */1078 async getProperties(collectionId: number, propertyKeys?: string[] | null): Promise<IProperty[]> {1079 return (await this.helper.callRpc('api.rpc.unique.collectionProperties', [collectionId, propertyKeys])).toHuman();1080 }10811082 async getCollectionOptions(collectionId: number) {1083 return (await this.helper.callRpc('api.rpc.unique.collectionById', [collectionId])).toHuman();1084 }10851086 /**1087 * Deletes onchain properties from the collection.1088 *1089 * @param signer keyring of signer1090 * @param collectionId ID of collection1091 * @param propertyKeys array of property keys to delete1092 * @example deleteProperties(aliceKeyring, 10, ["gender", "age"]);1093 * @returns ```true``` if extrinsic success, otherwise ```false```1094 */1095 async deleteProperties(signer: TSigner, collectionId: number, propertyKeys: string[]): Promise<boolean> {1096 const result = await this.helper.executeExtrinsic(1097 signer,1098 'api.tx.unique.deleteCollectionProperties', [collectionId, propertyKeys],1099 true,1100 );11011102 return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'common', 'CollectionPropertyDeleted');1103 }11041105 /**1106 * Changes the owner of the token.1107 *1108 * @param signer keyring of signer1109 * @param collectionId ID of collection1110 * @param tokenId ID of token1111 * @param addressObj address of a new owner1112 * @param amount amount of tokens to be transfered. For NFT must be set to 1n1113 * @example transferToken(aliceKeyring, 10, 5, {Substrate: "5DyN4Y92vZCjv38fg..."})1114 * @returns true if the token success, otherwise false1115 */1116 async transferToken(signer: TSigner, collectionId: number, tokenId: number, addressObj: ICrossAccountId, amount=1n): Promise<boolean> {1117 const result = await this.helper.executeExtrinsic(1118 signer,1119 'api.tx.unique.transfer', [addressObj, collectionId, tokenId, amount],1120 true, // `Unable to transfer token #${tokenId} from collection #${collectionId}`,1121 );11221123 return this.helper.util.isTokenTransferSuccess(result.result.events, collectionId, tokenId, {Substrate: typeof signer === 'string' ? signer : signer.address}, addressObj, amount);1124 }11251126 /**1127 *1128 * Change ownership of a token(s) on behalf of the owner.1129 *1130 * @param signer keyring of signer1131 * @param collectionId ID of collection1132 * @param tokenId ID of token1133 * @param fromAddressObj address on behalf of which the token will be sent1134 * @param toAddressObj new token owner1135 * @param amount amount of tokens to be transfered. For NFT must be set to 1n1136 * @example transferTokenFrom(aliceKeyring, 10, 5, {Substrate: "5DyN4Y92vZCjv38fg"}, {Ethereum: "0x9F0583DbB85..."})1137 * @returns true if the token success, otherwise false1138 */1139 async transferTokenFrom(signer: TSigner, collectionId: number, tokenId: number, fromAddressObj: ICrossAccountId, toAddressObj: ICrossAccountId, amount=1n): Promise<boolean> {1140 const result = await this.helper.executeExtrinsic(1141 signer,1142 'api.tx.unique.transferFrom', [fromAddressObj, toAddressObj, collectionId, tokenId, amount],1143 true, // `Unable to transfer token #${tokenId} from collection #${collectionId}`,1144 );1145 return this.helper.util.isTokenTransferSuccess(result.result.events, collectionId, tokenId, fromAddressObj, toAddressObj, amount);1146 }11471148 /**1149 *1150 * Destroys a concrete instance of NFT/RFT or burns a specified amount of fungible tokens.1151 *1152 * @param signer keyring of signer1153 * @param collectionId ID of collection1154 * @param tokenId ID of token1155 * @param amount amount of tokens to be burned. For NFT must be set to 1n1156 * @example burnToken(aliceKeyring, 10, 5);1157 * @returns ```true``` if the extrinsic is successful, otherwise ```false```1158 */1159 async burnToken(signer: TSigner, collectionId: number, tokenId: number, amount=1n): Promise<boolean> {1160 const burnResult = await this.helper.executeExtrinsic(1161 signer,1162 'api.tx.unique.burnItem', [collectionId, tokenId, amount],1163 true, // `Unable to burn token for ${label}`,1164 );1165 const burnedTokens = this.helper.util.extractTokensFromBurnResult(burnResult);1166 if (burnedTokens.tokens.length > 1) throw Error('Burned multiple tokens');1167 return burnedTokens.success;1168 }11691170 /**1171 * Destroys a concrete instance of NFT on behalf of the owner1172 *1173 * @param signer keyring of signer1174 * @param collectionId ID of collection1175 * @param tokenId ID of token1176 * @param fromAddressObj address on behalf of which the token will be burnt1177 * @param amount amount of tokens to be burned. For NFT must be set to 1n1178 * @example burnTokenFrom(aliceKeyring, 10, {Substrate: "5DyN4Y92vZCjv38fg..."}, 5, {Ethereum: "0x9F0583DbB85..."})1179 * @returns ```true``` if extrinsic success, otherwise ```false```1180 */1181 async burnTokenFrom(signer: TSigner, collectionId: number, tokenId: number, fromAddressObj: ICrossAccountId, amount=1n): Promise<boolean> {1182 const burnResult = await this.helper.executeExtrinsic(1183 signer,1184 'api.tx.unique.burnFrom', [collectionId, fromAddressObj, tokenId, amount],1185 true, // `Unable to burn token from for ${label}`,1186 );1187 const burnedTokens = this.helper.util.extractTokensFromBurnResult(burnResult);1188 return burnedTokens.success && burnedTokens.tokens.length > 0;1189 }11901191 /**1192 * Set, change, or remove approved address to transfer the ownership of the NFT.1193 *1194 * @param signer keyring of signer1195 * @param collectionId ID of collection1196 * @param tokenId ID of token1197 * @param toAddressObj Substrate or Ethereum address which gets approved use of the signer's tokens1198 * @param amount amount of token to be approved. For NFT must be set to 1n1199 * @returns ```true``` if extrinsic success, otherwise ```false```1200 */1201 async approveToken(signer: IKeyringPair, collectionId: number, tokenId: number, toAddressObj: ICrossAccountId, amount=1n) {1202 const approveResult = await this.helper.executeExtrinsic(1203 signer,1204 'api.tx.unique.approve', [toAddressObj, collectionId, tokenId, amount],1205 true, // `Unable to approve token for ${label}`,1206 );12071208 return this.helper.util.findCollectionInEvents(approveResult.result.events, collectionId, 'common', 'Approved');1209 }12101211 /**1212 * Get the amount of token pieces approved to transfer or burn. Normally 0.1213 *1214 * @param collectionId ID of collection1215 * @param tokenId ID of token1216 * @param toAccountObj address which is approved to use token pieces1217 * @param fromAccountObj address which may have allowed the use of its owned tokens1218 * @example getTokenApprovedPieces(10, 5, {Substrate: "5DyN4Y92vZCjv38fg..."}, {Substrate: "5ERZNF88Mm7UGfPP3mdG..."})1219 * @returns number of approved to transfer pieces1220 */1221 async getTokenApprovedPieces(collectionId: number, tokenId: number, toAccountObj: ICrossAccountId, fromAccountObj: ICrossAccountId): Promise<bigint> {1222 return (await this.helper.callRpc('api.rpc.unique.allowance', [collectionId, fromAccountObj, toAccountObj, tokenId])).toBigInt();1223 }12241225 /**1226 * Get the last created token ID in a collection1227 *1228 * @param collectionId ID of collection1229 * @example getLastTokenId(10);1230 * @returns id of the last created token1231 */1232 async getLastTokenId(collectionId: number): Promise<number> {1233 return (await this.helper.callRpc('api.rpc.unique.lastTokenId', [collectionId])).toNumber();1234 }12351236 /**1237 * Check if token exists1238 *1239 * @param collectionId ID of collection1240 * @param tokenId ID of token1241 * @example doesTokenExist(10, 20);1242 * @returns true if the token exists, otherwise false1243 */1244 async doesTokenExist(collectionId: number, tokenId: number): Promise<boolean> {1245 return (await this.helper.callRpc('api.rpc.unique.tokenExists', [collectionId, tokenId])).toJSON();1246 }1247}12481249class NFTnRFT extends CollectionGroup {1250 /**1251 * Get tokens owned by account1252 *1253 * @param collectionId ID of collection1254 * @param addressObj tokens owner1255 * @example getTokensByAddress(10, {Substrate: "5DyN4Y92vZCjv38fg..."})1256 * @returns array of token ids owned by account1257 */1258 async getTokensByAddress(collectionId: number, addressObj: ICrossAccountId): Promise<number[]> {1259 return (await this.helper.callRpc('api.rpc.unique.accountTokens', [collectionId, addressObj])).toJSON();1260 }12611262 /**1263 * Get token data1264 *1265 * @param collectionId ID of collection1266 * @param tokenId ID of token1267 * @param propertyKeys optionally filter the token properties to only these keys1268 * @param blockHashAt optionally query the data at some block with this hash1269 * @example getToken(10, 5);1270 * @returns human readable token data1271 */1272 async getToken(collectionId: number, tokenId: number, propertyKeys: string[] = [], blockHashAt?: string): Promise<{1273 properties: IProperty[];1274 owner: CrossAccountId;1275 normalizedOwner: CrossAccountId;1276 }| null> {1277 let tokenData;1278 if(typeof blockHashAt === 'undefined') {1279 tokenData = await this.helper.callRpc('api.rpc.unique.tokenData', [collectionId, tokenId]);1280 }1281 else {1282 if(propertyKeys.length == 0) {1283 const collection = (await this.helper.callRpc('api.rpc.unique.collectionById', [collectionId])).toHuman();1284 if(!collection) return null;1285 propertyKeys = collection.tokenPropertyPermissions.map((x: ITokenPropertyPermission) => x.key);1286 }1287 tokenData = await this.helper.callRpc('api.rpc.unique.tokenData', [collectionId, tokenId, propertyKeys, blockHashAt]);1288 }1289 tokenData = tokenData.toHuman();1290 if (tokenData === null || tokenData.owner === null) return null;1291 const owner = {} as any;1292 for (const key of Object.keys(tokenData.owner)) {1293 owner[key.toLocaleLowerCase()] = key.toLocaleLowerCase() == 'substrate'1294 ? CrossAccountId.normalizeSubstrateAddress(tokenData.owner[key])1295 : tokenData.owner[key];1296 }1297 tokenData.normalizedOwner = CrossAccountId.fromLowerCaseKeys(owner);1298 return tokenData;1299 }13001301 /**1302 * Set permissions to change token properties1303 *1304 * @param signer keyring of signer1305 * @param collectionId ID of collection1306 * @param permissions permissions to change a property by the collection admin or token owner1307 * @example setTokenPropertyPermissions(1308 * aliceKeyring, 10, [{key: "gender", permission: {tokenOwner: true, mutable: true, collectionAdmin: true}}]1309 * )1310 * @returns true if extrinsic success otherwise false1311 */1312 async setTokenPropertyPermissions(signer: TSigner, collectionId: number, permissions: ITokenPropertyPermission[]): Promise<boolean> {1313 const result = await this.helper.executeExtrinsic(1314 signer,1315 'api.tx.unique.setTokenPropertyPermissions', [collectionId, permissions],1316 true,1317 );13181319 return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'common', 'PropertyPermissionSet');1320 }13211322 /**1323 * Get token property permissions.1324 *1325 * @param collectionId ID of collection1326 * @param propertyKeys optionally filter the returned property permissions to only these keys1327 * @example getPropertyPermissions(1219, ['location', 'date', 'time', 'isParadise']);1328 * @returns array of key-permission pairs1329 */1330 async getPropertyPermissions(collectionId: number, propertyKeys: string[] | null = null): Promise<ITokenPropertyPermission[]> {1331 return (await this.helper.callRpc('api.rpc.unique.propertyPermissions', [collectionId, ...(propertyKeys === null ? [] : [propertyKeys])])).toHuman();1332 }13331334 /**1335 * Set token properties1336 *1337 * @param signer keyring of signer1338 * @param collectionId ID of collection1339 * @param tokenId ID of token1340 * @param properties key-value pairs of metadata which to add to a token. Keys must be permitted in the collection1341 * @example setTokenProperties(aliceKeyring, 10, 5, [{key: "gender", value: "female"}, {key: "age", value: "23"}])1342 * @returns ```true``` if extrinsic success, otherwise ```false```1343 */1344 async setTokenProperties(signer: TSigner, collectionId: number, tokenId: number, properties: IProperty[]): Promise<boolean> {1345 const result = await this.helper.executeExtrinsic(1346 signer,1347 'api.tx.unique.setTokenProperties', [collectionId, tokenId, properties],1348 true,1349 );13501351 return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'common', 'TokenPropertySet');1352 }13531354 /**1355 * Get properties, metadata assigned to a token.1356 *1357 * @param collectionId ID of collection1358 * @param tokenId ID of token1359 * @param propertyKeys optionally filter the returned properties to only these keys1360 * @example getTokenProperties(1219, ['location', 'date', 'time', 'isParadise']);1361 * @returns array of key-value pairs1362 */1363 async getTokenProperties(collectionId: number, tokenId: number, propertyKeys?: string[] | null): Promise<IProperty[]> {1364 return (await this.helper.callRpc('api.rpc.unique.tokenProperties', [collectionId, tokenId, propertyKeys])).toHuman();1365 }13661367 /**1368 * Delete the provided properties of a token1369 * @param signer keyring of signer1370 * @param collectionId ID of collection1371 * @param tokenId ID of token1372 * @param propertyKeys property keys to be deleted1373 * @example deleteTokenProperties(aliceKeyring, 10, 5, ["gender", "age"])1374 * @returns ```true``` if extrinsic success, otherwise ```false```1375 */1376 async deleteTokenProperties(signer: TSigner, collectionId: number, tokenId: number, propertyKeys: string[]): Promise<boolean> {1377 const result = await this.helper.executeExtrinsic(1378 signer,1379 'api.tx.unique.deleteTokenProperties', [collectionId, tokenId, propertyKeys],1380 true,1381 );13821383 return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'common', 'TokenPropertyDeleted');1384 }13851386 /**1387 * Mint new collection1388 *1389 * @param signer keyring of signer1390 * @param collectionOptions basic collection options and properties1391 * @param mode NFT or RFT type of a collection1392 * @example mintCollection(aliceKeyring, {name: 'New', description: "New collection", tokenPrefix: "NEW"}, "NFT")1393 * @returns object of the created collection1394 */1395 async mintCollection(signer: TSigner, collectionOptions: ICollectionCreationOptions, mode: 'NFT' | 'RFT'): Promise<UniqueBaseCollection> {1396 collectionOptions = JSON.parse(JSON.stringify(collectionOptions)) as ICollectionCreationOptions; // Clone object1397 collectionOptions.mode = (mode === 'NFT') ? {nft: null} : {refungible: null};1398 for (const key of ['name', 'description', 'tokenPrefix']) {1399 if (typeof collectionOptions[key as 'name' | 'description' | 'tokenPrefix'] === 'string') collectionOptions[key as 'name' | 'description' | 'tokenPrefix'] = this.helper.util.str2vec(collectionOptions[key as 'name' | 'description' | 'tokenPrefix'] as string);1400 }1401 const creationResult = await this.helper.executeExtrinsic(1402 signer,1403 'api.tx.unique.createCollectionEx', [collectionOptions],1404 true, // errorLabel,1405 );1406 return this.getCollectionObject(this.helper.util.extractCollectionIdFromCreationResult(creationResult));1407 }14081409 getCollectionObject(_collectionId: number): any {1410 return null;1411 }14121413 getTokenObject(_collectionId: number, _tokenId: number): any {1414 return null;1415 }14161417 /**1418 * Tells whether the given `owner` approves the `operator`.1419 * @param collectionId ID of collection1420 * @param owner owner address1421 * @param operator operator addrees1422 * @returns true if operator is enabled1423 */1424 async allowanceForAll(collectionId: number, owner: ICrossAccountId, operator: ICrossAccountId): Promise<boolean> {1425 return (await this.helper.callRpc('api.rpc.unique.allowanceForAll', [collectionId, owner, operator])).toJSON();1426 }14271428 /** Sets or unsets the approval of a given operator.1429 * The `operator` is allowed to transfer all tokens of the `caller` on their behalf.1430 * @param operator Operator1431 * @param approved Should operator status be granted or revoked?1432 * @returns ```true``` if extrinsic success, otherwise ```false```1433 */1434 async setAllowanceForAll(signer: TSigner, collectionId: number, operator: ICrossAccountId, approved: boolean): Promise<boolean> {1435 const result = await this.helper.executeExtrinsic(1436 signer,1437 'api.tx.unique.setAllowanceForAll', [collectionId, operator, approved],1438 true,1439 );1440 return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'common', 'ApprovedForAll');1441 }1442}144314441445class NFTGroup extends NFTnRFT {1446 /**1447 * Get collection object1448 * @param collectionId ID of collection1449 * @example getCollectionObject(2);1450 * @returns instance of UniqueNFTCollection1451 */1452 getCollectionObject(collectionId: number): UniqueNFTCollection {1453 return new UniqueNFTCollection(collectionId, this.helper);1454 }14551456 /**1457 * Get token object1458 * @param collectionId ID of collection1459 * @param tokenId ID of token1460 * @example getTokenObject(10, 5);1461 * @returns instance of UniqueNFTToken1462 */1463 getTokenObject(collectionId: number, tokenId: number): UniqueNFToken {1464 return new UniqueNFToken(tokenId, this.getCollectionObject(collectionId));1465 }14661467 /**1468 * Get token's owner1469 * @param collectionId ID of collection1470 * @param tokenId ID of token1471 * @param blockHashAt optionally query the data at the block with this hash1472 * @example getTokenOwner(10, 5);1473 * @returns Address in CrossAccountId format, e.g. {Substrate: "5DnSF6RRjwteE3BrCj..."}1474 */1475 async getTokenOwner(collectionId: number, tokenId: number, blockHashAt?: string): Promise<CrossAccountId> {1476 let owner;1477 if (typeof blockHashAt === 'undefined') {1478 owner = await this.helper.callRpc('api.rpc.unique.tokenOwner', [collectionId, tokenId]);1479 } else {1480 owner = await this.helper.callRpc('api.rpc.unique.tokenOwner', [collectionId, tokenId, blockHashAt]);1481 }1482 return CrossAccountId.fromLowerCaseKeys(owner.toJSON());1483 }14841485 /**1486 * Is token approved to transfer1487 * @param collectionId ID of collection1488 * @param tokenId ID of token1489 * @param toAccountObj address to be approved1490 * @returns ```true``` if extrinsic success, otherwise ```false```1491 */1492 async isTokenApproved(collectionId: number, tokenId: number, toAccountObj: ICrossAccountId): Promise<boolean> {1493 return (await this.getTokenApprovedPieces(collectionId, tokenId, toAccountObj, await this.getTokenOwner(collectionId, tokenId))) === 1n;1494 }14951496 /**1497 * Changes the owner of the token.1498 *1499 * @param signer keyring of signer1500 * @param collectionId ID of collection1501 * @param tokenId ID of token1502 * @param addressObj address of a new owner1503 * @example transferToken(aliceKeyring, 10, 5, {Substrate: "5DyN4Y92vZCjv38fg..."})1504 * @returns ```true``` if extrinsic success, otherwise ```false```1505 */1506 async transferToken(signer: TSigner, collectionId: number, tokenId: number, addressObj: ICrossAccountId): Promise<boolean> {1507 return await super.transferToken(signer, collectionId, tokenId, addressObj, 1n);1508 }15091510 /**1511 *1512 * Change ownership of a NFT on behalf of the owner.1513 *1514 * @param signer keyring of signer1515 * @param collectionId ID of collection1516 * @param tokenId ID of token1517 * @param fromAddressObj address on behalf of which the token will be sent1518 * @param toAddressObj new token owner1519 * @example transferTokenFrom(aliceKeyring, 10, 5, {Substrate: "5DyN4Y92vZCjv38fg..."}, {Ethereum: "0x9F0583DbB85..."})1520 * @returns ```true``` if extrinsic success, otherwise ```false```1521 */1522 async transferTokenFrom(signer: TSigner, collectionId: number, tokenId: number, fromAddressObj: ICrossAccountId, toAddressObj: ICrossAccountId): Promise<boolean> {1523 return await super.transferTokenFrom(signer, collectionId, tokenId, fromAddressObj, toAddressObj, 1n);1524 }15251526 /**1527 * Recursively find the address that owns the token1528 * @param collectionId ID of collection1529 * @param tokenId ID of token1530 * @param blockHashAt1531 * @example getTokenTopmostOwner(10, 5);1532 * @returns address in CrossAccountId format, e.g. {Substrate: "5DyN4Y92vZCjv38fg..."}1533 */1534 async getTokenTopmostOwner(collectionId: number, tokenId: number, blockHashAt?: string): Promise<CrossAccountId | null> {1535 let owner;1536 if (typeof blockHashAt === 'undefined') {1537 owner = await this.helper.callRpc('api.rpc.unique.topmostTokenOwner', [collectionId, tokenId]);1538 } else {1539 owner = await this.helper.callRpc('api.rpc.unique.topmostTokenOwner', [collectionId, tokenId, blockHashAt]);1540 }15411542 if (owner === null) return null;15431544 return owner.toHuman();1545 }15461547 /**1548 * Get tokens nested in the provided token1549 * @param collectionId ID of collection1550 * @param tokenId ID of token1551 * @param blockHashAt optionally query the data at the block with this hash1552 * @example getTokenChildren(10, 5);1553 * @returns tokens whose depth of nesting is <= 51554 */1555 async getTokenChildren(collectionId: number, tokenId: number, blockHashAt?: string): Promise<IToken[]> {1556 let children;1557 if(typeof blockHashAt === 'undefined') {1558 children = await this.helper.callRpc('api.rpc.unique.tokenChildren', [collectionId, tokenId]);1559 } else {1560 children = await this.helper.callRpc('api.rpc.unique.tokenChildren', [collectionId, tokenId, blockHashAt]);1561 }15621563 return children.toJSON().map((x: any) => {1564 return {collectionId: x.collection, tokenId: x.token};1565 });1566 }15671568 /**1569 * Nest one token into another1570 * @param signer keyring of signer1571 * @param tokenObj token to be nested1572 * @param rootTokenObj token to be parent1573 * @example nestToken(aliceKeyring, {collectionId: 10, tokenId: 5}, {collectionId: 10, tokenId: 4});1574 * @returns ```true``` if extrinsic success, otherwise ```false```1575 */1576 async nestToken(signer: TSigner, tokenObj: IToken, rootTokenObj: IToken): Promise<boolean> {1577 const rootTokenAddress = this.helper.util.getTokenAccount(rootTokenObj);1578 const result = await this.transferToken(signer, tokenObj.collectionId, tokenObj.tokenId, rootTokenAddress);1579 if(!result) {1580 throw Error('Unable to nest token!');1581 }1582 return result;1583 }15841585 /**1586 * Remove token from nested state1587 * @param signer keyring of signer1588 * @param tokenObj token to unnest1589 * @param rootTokenObj parent of a token1590 * @param toAddressObj address of a new token owner1591 * @example unnestToken(aliceKeyring, {collectionId: 10, tokenId: 5}, {collectionId: 10, tokenId: 4}, {Substrate: "5DyN4Y92vZCjv38fg..."});1592 * @returns ```true``` if extrinsic success, otherwise ```false```1593 */1594 async unnestToken(signer: TSigner, tokenObj: IToken, rootTokenObj: IToken, toAddressObj: ICrossAccountId): Promise<boolean> {1595 const rootTokenAddress = this.helper.util.getTokenAccount(rootTokenObj);1596 const result = await this.transferTokenFrom(signer, tokenObj.collectionId, tokenObj.tokenId, rootTokenAddress, toAddressObj);1597 if(!result) {1598 throw Error('Unable to unnest token!');1599 }1600 return result;1601 }16021603 /**1604 * Mint new collection1605 * @param signer keyring of signer1606 * @param collectionOptions Collection options1607 * @example1608 * mintCollection(aliceKeyring, {1609 * name: 'New',1610 * description: 'New collection',1611 * tokenPrefix: 'NEW',1612 * })1613 * @returns object of the created collection1614 */1615 async mintCollection(signer: TSigner, collectionOptions: ICollectionCreationOptions = {}): Promise<UniqueNFTCollection> {1616 return await super.mintCollection(signer, collectionOptions, 'NFT') as UniqueNFTCollection;1617 }16181619 /**1620 * Mint new token1621 * @param signer keyring of signer1622 * @param data token data1623 * @returns created token object1624 */1625 async mintToken(signer: TSigner, data: { collectionId: number; owner: ICrossAccountId | string; properties?: IProperty[]; }): Promise<UniqueNFToken> {1626 const creationResult = await this.helper.executeExtrinsic(1627 signer,1628 'api.tx.unique.createItem', [data.collectionId, (typeof data.owner === 'string') ? {Substrate: data.owner} : data.owner, {1629 nft: {1630 properties: data.properties,1631 },1632 }],1633 true,1634 );1635 const createdTokens = this.helper.util.extractTokensFromCreationResult(creationResult);1636 if (createdTokens.tokens.length > 1) throw Error('Minted multiple tokens');1637 if (createdTokens.tokens.length < 1) throw Error('No tokens minted');1638 return this.getTokenObject(data.collectionId, createdTokens.tokens[0].tokenId);1639 }16401641 /**1642 * Mint multiple NFT tokens1643 * @param signer keyring of signer1644 * @param collectionId ID of collection1645 * @param tokens array of tokens with owner and properties1646 * @example1647 * mintMultipleTokens(aliceKeyring, 10, [{1648 * owner: {Substrate: "5DyN4Y92vZCjv38fg..."},1649 * properties: [{key: "gender", value: "male"},{key: "age", value: "45"}],1650 * },{1651 * owner: {Ethereum: "0x9F0583DbB855d..."},1652 * properties: [{key: "gender", value: "female"},{key: "age", value: "22"}],1653 * }]);1654 * @returns ```true``` if extrinsic success, otherwise ```false```1655 */1656 async mintMultipleTokens(signer: TSigner, collectionId: number, tokens: {owner: ICrossAccountId, properties?: IProperty[]}[]): Promise<UniqueNFToken[]> {1657 const creationResult = await this.helper.executeExtrinsic(1658 signer,1659 'api.tx.unique.createMultipleItemsEx', [collectionId, {NFT: tokens}],1660 true,1661 );1662 const collection = this.getCollectionObject(collectionId);1663 return this.helper.util.extractTokensFromCreationResult(creationResult).tokens.map((x: IToken) => collection.getTokenObject(x.tokenId));1664 }16651666 /**1667 * Mint multiple NFT tokens with one owner1668 * @param signer keyring of signer1669 * @param collectionId ID of collection1670 * @param owner tokens owner1671 * @param tokens array of tokens with owner and properties1672 * @example1673 * mintMultipleTokensWithOneOwner(aliceKeyring, 10, "5DyN4Y92vZCjv38fg...", [{1674 * properties: [{1675 * key: "gender",1676 * value: "female",1677 * },{1678 * key: "age",1679 * value: "33",1680 * }],1681 * }]);1682 * @returns array of newly created tokens1683 */1684 async mintMultipleTokensWithOneOwner(signer: TSigner, collectionId: number, owner: ICrossAccountId, tokens: {properties?: IProperty[]}[]): Promise<UniqueNFToken[]> {1685 const rawTokens = [];1686 for (const token of tokens) {1687 const raw = {NFT: {properties: token.properties}};1688 rawTokens.push(raw);1689 }1690 const creationResult = await this.helper.executeExtrinsic(1691 signer,1692 'api.tx.unique.createMultipleItems', [collectionId, owner, rawTokens],1693 true,1694 );1695 const collection = this.getCollectionObject(collectionId);1696 return this.helper.util.extractTokensFromCreationResult(creationResult).tokens.map((x: IToken) => collection.getTokenObject(x.tokenId));1697 }16981699 /**1700 * Set, change, or remove approved address to transfer the ownership of the NFT.1701 *1702 * @param signer keyring of signer1703 * @param collectionId ID of collection1704 * @param tokenId ID of token1705 * @param toAddressObj address to approve1706 * @example approveToken(aliceKeyring, 10, 5, {Substrate: "5DyN4Y92vZCjv38fg..."})1707 * @returns ```true``` if extrinsic success, otherwise ```false```1708 */1709 approveToken(signer: IKeyringPair, collectionId: number, tokenId: number, toAddressObj: ICrossAccountId) {1710 return super.approveToken(signer, collectionId, tokenId, toAddressObj, 1n);1711 }1712}171317141715class RFTGroup extends NFTnRFT {1716 /**1717 * Get collection object1718 * @param collectionId ID of collection1719 * @example getCollectionObject(2);1720 * @returns instance of UniqueRFTCollection1721 */1722 getCollectionObject(collectionId: number): UniqueRFTCollection {1723 return new UniqueRFTCollection(collectionId, this.helper);1724 }17251726 /**1727 * Get token object1728 * @param collectionId ID of collection1729 * @param tokenId ID of token1730 * @example getTokenObject(10, 5);1731 * @returns instance of UniqueNFTToken1732 */1733 getTokenObject(collectionId: number, tokenId: number): UniqueRFToken {1734 return new UniqueRFToken(tokenId, this.getCollectionObject(collectionId));1735 }17361737 /**1738 * Get top 10 token owners with the largest number of pieces1739 * @param collectionId ID of collection1740 * @param tokenId ID of token1741 * @example getTokenTop10Owners(10, 5);1742 * @returns array of top 10 owners1743 */1744 async getTokenTop10Owners(collectionId: number, tokenId: number): Promise<CrossAccountId[]> {1745 return (await this.helper.callRpc('api.rpc.unique.tokenOwners', [collectionId, tokenId])).toJSON().map(CrossAccountId.fromLowerCaseKeys);1746 }17471748 /**1749 * Get number of pieces owned by address1750 * @param collectionId ID of collection1751 * @param tokenId ID of token1752 * @param addressObj address token owner1753 * @example getTokenBalance(10, 5, {Substrate: "5DyN4Y92vZCjv38fg..."});1754 * @returns number of pieces ownerd by address1755 */1756 async getTokenBalance(collectionId: number, tokenId: number, addressObj: ICrossAccountId): Promise<bigint> {1757 return (await this.helper.callRpc('api.rpc.unique.balance', [collectionId, addressObj, tokenId])).toBigInt();1758 }17591760 /**1761 * Transfer pieces of token to another address1762 * @param signer keyring of signer1763 * @param collectionId ID of collection1764 * @param tokenId ID of token1765 * @param addressObj address of a new owner1766 * @param amount number of pieces to be transfered1767 * @example transferTokenFrom(aliceKeyring, 10, 5, {Substrate: "5DyN4Y92vZCjv38fg..."}, 2000n)1768 * @returns ```true``` if extrinsic success, otherwise ```false```1769 */1770 async transferToken(signer: TSigner, collectionId: number, tokenId: number, addressObj: ICrossAccountId, amount=1n): Promise<boolean> {1771 return await super.transferToken(signer, collectionId, tokenId, addressObj, amount);1772 }17731774 /**1775 * Change ownership of some pieces of RFT on behalf of the owner.1776 * @param signer keyring of signer1777 * @param collectionId ID of collection1778 * @param tokenId ID of token1779 * @param fromAddressObj address on behalf of which the token will be sent1780 * @param toAddressObj new token owner1781 * @param amount number of pieces to be transfered1782 * @example transferTokenFrom(aliceKeyring, 10, 5, {Substrate: "5DyN4Y92vZCjv38fg..."}, {Substrate: "5DfhbVfww7ThF8q6f3i..."}, 2000n)1783 * @returns ```true``` if extrinsic success, otherwise ```false```1784 */1785 async transferTokenFrom(signer: TSigner, collectionId: number, tokenId: number, fromAddressObj: ICrossAccountId, toAddressObj: ICrossAccountId, amount=1n): Promise<boolean> {1786 return await super.transferTokenFrom(signer, collectionId, tokenId, fromAddressObj, toAddressObj, amount);1787 }17881789 /**1790 * Mint new collection1791 * @param signer keyring of signer1792 * @param collectionOptions Collection options1793 * @example1794 * mintCollection(aliceKeyring, {1795 * name: 'New',1796 * description: 'New collection',1797 * tokenPrefix: 'NEW',1798 * })1799 * @returns object of the created collection1800 */1801 async mintCollection(signer: TSigner, collectionOptions: ICollectionCreationOptions = {}): Promise<UniqueRFTCollection> {1802 return await super.mintCollection(signer, collectionOptions, 'RFT') as UniqueRFTCollection;1803 }18041805 /**1806 * Mint new token1807 * @param signer keyring of signer1808 * @param data token data1809 * @example mintToken(aliceKeyring, {collectionId: 10, owner: {Substrate: '5GHoZe9c73RYbVzq...'}, pieces: 10000n});1810 * @returns created token object1811 */1812 async mintToken(signer: TSigner, data: { collectionId: number; owner: ICrossAccountId | string; pieces: bigint; properties?: IProperty[]; }): Promise<UniqueRFToken> {1813 const creationResult = await this.helper.executeExtrinsic(1814 signer,1815 'api.tx.unique.createItem', [data.collectionId, (typeof data.owner === 'string') ? {Substrate: data.owner} : data.owner, {1816 refungible: {1817 pieces: data.pieces,1818 properties: data.properties,1819 },1820 }],1821 true,1822 );1823 const createdTokens = this.helper.util.extractTokensFromCreationResult(creationResult);1824 if (createdTokens.tokens.length > 1) throw Error('Minted multiple tokens');1825 if (createdTokens.tokens.length < 1) throw Error('No tokens minted');1826 return this.getTokenObject(data.collectionId, createdTokens.tokens[0].tokenId);1827 }18281829 async mintMultipleTokens(signer: TSigner, collectionId: number, tokens: {owner: ICrossAccountId, pieces: bigint, properties?: IProperty[]}[]): Promise<UniqueRFToken[]> {1830 throw Error('Not implemented');1831 const creationResult = await this.helper.executeExtrinsic(1832 signer,1833 'api.tx.unique.createMultipleItemsEx', [collectionId, {RefungibleMultipleOwners: tokens}],1834 true, // `Unable to mint RFT tokens for ${label}`,1835 );1836 const collection = this.getCollectionObject(collectionId);1837 return this.helper.util.extractTokensFromCreationResult(creationResult).tokens.map((x: IToken) => collection.getTokenObject(x.tokenId));1838 }18391840 /**1841 * Mint multiple RFT tokens with one owner1842 * @param signer keyring of signer1843 * @param collectionId ID of collection1844 * @param owner tokens owner1845 * @param tokens array of tokens with properties and pieces1846 * @example mintMultipleTokensWithOneOwner(aliceKeyring, 10, {Substrate: "5GHoZe9c73RYbVzq..."}, [{pieces: 100000n, properties: [{key: "gender", value: "male"}]}]);1847 * @returns array of newly created RFT tokens1848 */1849 async mintMultipleTokensWithOneOwner(signer: TSigner, collectionId: number, owner: ICrossAccountId, tokens: {pieces: bigint, properties?: IProperty[]}[]): Promise<UniqueRFToken[]> {1850 const rawTokens = [];1851 for (const token of tokens) {1852 const raw = {ReFungible: {pieces: token.pieces, properties: token.properties}};1853 rawTokens.push(raw);1854 }1855 const creationResult = await this.helper.executeExtrinsic(1856 signer,1857 'api.tx.unique.createMultipleItems', [collectionId, owner, rawTokens],1858 true,1859 );1860 const collection = this.getCollectionObject(collectionId);1861 return this.helper.util.extractTokensFromCreationResult(creationResult).tokens.map((x: IToken) => collection.getTokenObject(x.tokenId));1862 }18631864 /**1865 * Destroys a concrete instance of RFT.1866 * @param signer keyring of signer1867 * @param collectionId ID of collection1868 * @param tokenId ID of token1869 * @param amount number of pieces to be burnt1870 * @example burnToken(aliceKeyring, 10, 5);1871 * @returns ```true``` if the extrinsic is successful, otherwise ```false```1872 */1873 async burnToken(signer: IKeyringPair, collectionId: number, tokenId: number, amount=1n): Promise<boolean> {1874 return await super.burnToken(signer, collectionId, tokenId, amount);1875 }18761877 /**1878 * Destroys a concrete instance of RFT on behalf of the owner.1879 * @param signer keyring of signer1880 * @param collectionId ID of collection1881 * @param tokenId ID of token1882 * @param fromAddressObj address on behalf of which the token will be burnt1883 * @param amount number of pieces to be burnt1884 * @example burnTokenFrom(aliceKeyring, 10, 5, {Substrate: "5DyN4Y92vZCjv38fg..."}, 2n)1885 * @returns ```true``` if extrinsic success, otherwise ```false```1886 */1887 async burnTokenFrom(signer: IKeyringPair, collectionId: number, tokenId: number, fromAddressObj: ICrossAccountId, amount=1n): Promise<boolean> {1888 return await super.burnTokenFrom(signer, collectionId, tokenId, fromAddressObj, amount);1889 }18901891 /**1892 * Set, change, or remove approved address to transfer the ownership of the RFT.1893 *1894 * @param signer keyring of signer1895 * @param collectionId ID of collection1896 * @param tokenId ID of token1897 * @param toAddressObj address to approve1898 * @param amount number of pieces to be approved1899 * @example approveToken(aliceKeyring, 10, 5, {Substrate: "5GHoZe9c73RYbVzq..."}, "", 10000n);1900 * @returns true if the token success, otherwise false1901 */1902 approveToken(signer: IKeyringPair, collectionId: number, tokenId: number, toAddressObj: ICrossAccountId, amount=1n) {1903 return super.approveToken(signer, collectionId, tokenId, toAddressObj, amount);1904 }19051906 /**1907 * Get total number of pieces1908 * @param collectionId ID of collection1909 * @param tokenId ID of token1910 * @example getTokenTotalPieces(10, 5);1911 * @returns number of pieces1912 */1913 async getTokenTotalPieces(collectionId: number, tokenId: number): Promise<bigint> {1914 return (await this.helper.callRpc('api.rpc.unique.totalPieces', [collectionId, tokenId])).unwrap().toBigInt();1915 }19161917 /**1918 * Change number of token pieces. Signer must be the owner of all token pieces.1919 * @param signer keyring of signer1920 * @param collectionId ID of collection1921 * @param tokenId ID of token1922 * @param amount new number of pieces1923 * @example repartitionToken(aliceKeyring, 10, 5, 12345n);1924 * @returns true if the repartion was success, otherwise false1925 */1926 async repartitionToken(signer: TSigner, collectionId: number, tokenId: number, amount: bigint): Promise<boolean> {1927 const currentAmount = await this.getTokenTotalPieces(collectionId, tokenId);1928 const repartitionResult = await this.helper.executeExtrinsic(1929 signer,1930 'api.tx.unique.repartition', [collectionId, tokenId, amount],1931 true,1932 );1933 if(currentAmount < amount) return this.helper.util.findCollectionInEvents(repartitionResult.result.events, collectionId, 'common', 'ItemCreated');1934 return this.helper.util.findCollectionInEvents(repartitionResult.result.events, collectionId, 'common', 'ItemDestroyed');1935 }1936}193719381939class FTGroup extends CollectionGroup {1940 /**1941 * Get collection object1942 * @param collectionId ID of collection1943 * @example getCollectionObject(2);1944 * @returns instance of UniqueFTCollection1945 */1946 getCollectionObject(collectionId: number): UniqueFTCollection {1947 return new UniqueFTCollection(collectionId, this.helper);1948 }19491950 /**1951 * Mint new fungible collection1952 * @param signer keyring of signer1953 * @param collectionOptions Collection options1954 * @param decimalPoints number of token decimals1955 * @example1956 * mintCollection(aliceKeyring, {1957 * name: 'New',1958 * description: 'New collection',1959 * tokenPrefix: 'NEW',1960 * }, 18)1961 * @returns newly created fungible collection1962 */1963 async mintCollection(signer: TSigner, collectionOptions: ICollectionCreationOptions = {}, decimalPoints = 0): Promise<UniqueFTCollection> {1964 collectionOptions = JSON.parse(JSON.stringify(collectionOptions)) as ICollectionCreationOptions; // Clone object1965 if(collectionOptions.tokenPropertyPermissions) throw Error('Fungible collections has no tokenPropertyPermissions');1966 collectionOptions.mode = {fungible: decimalPoints};1967 for (const key of ['name', 'description', 'tokenPrefix']) {1968 if (typeof collectionOptions[key as 'name' | 'description' | 'tokenPrefix'] === 'string') collectionOptions[key as 'name' | 'description' | 'tokenPrefix'] = this.helper.util.str2vec(collectionOptions[key as 'name' | 'description' | 'tokenPrefix'] as string);1969 }1970 const creationResult = await this.helper.executeExtrinsic(1971 signer,1972 'api.tx.unique.createCollectionEx', [collectionOptions],1973 true,1974 );1975 return this.getCollectionObject(this.helper.util.extractCollectionIdFromCreationResult(creationResult));1976 }19771978 /**1979 * Mint tokens1980 * @param signer keyring of signer1981 * @param collectionId ID of collection1982 * @param owner address owner of new tokens1983 * @param amount amount of tokens to be meanted1984 * @example mintTokens(aliceKeyring, 10, {Substrate: "5GHoZe9c73RYbVzq"}, 1000n);1985 * @returns ```true``` if extrinsic success, otherwise ```false```1986 */1987 async mintTokens(signer: TSigner, collectionId: number, amount: bigint, owner: ICrossAccountId | string): Promise<boolean> {1988 const creationResult = await this.helper.executeExtrinsic(1989 signer,1990 'api.tx.unique.createItem', [collectionId, (typeof owner === 'string') ? {Substrate: owner} : owner, {1991 fungible: {1992 value: amount,1993 },1994 }],1995 true, // `Unable to mint fungible tokens for ${label}`,1996 );1997 return this.helper.util.findCollectionInEvents(creationResult.result.events, collectionId, 'common', 'ItemCreated');1998 }19992000 /**2001 * Mint multiple Fungible tokens with one owner2002 * @param signer keyring of signer2003 * @param collectionId ID of collection2004 * @param owner tokens owner2005 * @param tokens array of tokens with properties and pieces2006 * @returns ```true``` if extrinsic success, otherwise ```false```2007 */2008 async mintMultipleTokensWithOneOwner(signer: TSigner, collectionId: number, tokens: {value: bigint}[], owner: ICrossAccountId): Promise<boolean> {2009 const rawTokens = [];2010 for (const token of tokens) {2011 const raw = {Fungible: {Value: token.value}};2012 rawTokens.push(raw);2013 }2014 const creationResult = await this.helper.executeExtrinsic(2015 signer,2016 'api.tx.unique.createMultipleItems', [collectionId, owner, rawTokens],2017 true,2018 );2019 return this.helper.util.findCollectionInEvents(creationResult.result.events, collectionId, 'common', 'ItemCreated');2020 }20212022 /**2023 * Get the top 10 owners with the largest balance for the Fungible collection2024 * @param collectionId ID of collection2025 * @example getTop10Owners(10);2026 * @returns array of ```ICrossAccountId```2027 */2028 async getTop10Owners(collectionId: number): Promise<CrossAccountId[]> {2029 return (await this.helper.callRpc('api.rpc.unique.tokenOwners', [collectionId, 0])).toJSON().map(CrossAccountId.fromLowerCaseKeys);2030 }20312032 /**2033 * Get account balance2034 * @param collectionId ID of collection2035 * @param addressObj address of owner2036 * @example getBalance(10, {Substrate: "5GHoZe9c73RYbVzq..."})2037 * @returns amount of fungible tokens owned by address2038 */2039 async getBalance(collectionId: number, addressObj: ICrossAccountId): Promise<bigint> {2040 return (await this.helper.callRpc('api.rpc.unique.balance', [collectionId, addressObj, 0])).toBigInt();2041 }20422043 /**2044 * Transfer tokens to address2045 * @param signer keyring of signer2046 * @param collectionId ID of collection2047 * @param toAddressObj address recipient2048 * @param amount amount of tokens to be sent2049 * @example transfer(aliceKeyring, 10, {Substrate: "5GHoZe9c73RYbVzq..."}, 1000n);2050 * @returns ```true``` if extrinsic success, otherwise ```false```2051 */2052 async transfer(signer: TSigner, collectionId: number, toAddressObj: ICrossAccountId, amount=1n) {2053 return await super.transferToken(signer, collectionId, 0, toAddressObj, amount);2054 }20552056 /**2057 * Transfer some tokens on behalf of the owner.2058 * @param signer keyring of signer2059 * @param collectionId ID of collection2060 * @param fromAddressObj address on behalf of which tokens will be sent2061 * @param toAddressObj address where token to be sent2062 * @param amount number of tokens to be sent2063 * @example transferFrom(aliceKeyring, 10, {Substrate: "5GHoZe9c73RYbVzq..."}, {Substrate: "5DfhbVfww7ThF8q6f3ij..."}, 10000n);2064 * @returns ```true``` if extrinsic success, otherwise ```false```2065 */2066 async transferFrom(signer: TSigner, collectionId: number, fromAddressObj: ICrossAccountId, toAddressObj: ICrossAccountId, amount=1n) {2067 return await super.transferTokenFrom(signer, collectionId, 0, fromAddressObj, toAddressObj, amount);2068 }20692070 /**2071 * Destroy some amount of tokens2072 * @param signer keyring of signer2073 * @param collectionId ID of collection2074 * @param amount amount of tokens to be destroyed2075 * @example burnTokens(aliceKeyring, 10, 1000n);2076 * @returns ```true``` if extrinsic success, otherwise ```false```2077 */2078 async burnTokens(signer: IKeyringPair, collectionId: number, amount=1n): Promise<boolean> {2079 return await super.burnToken(signer, collectionId, 0, amount);2080 }20812082 /**2083 * Burn some tokens on behalf of the owner.2084 * @param signer keyring of signer2085 * @param collectionId ID of collection2086 * @param fromAddressObj address on behalf of which tokens will be burnt2087 * @param amount amount of tokens to be burnt2088 * @example burnTokensFrom(aliceKeyring, 10, {Substrate: "5GHoZe9c73RYbVzq..."}, 1000n);2089 * @returns ```true``` if extrinsic success, otherwise ```false```2090 */2091 async burnTokensFrom(signer: IKeyringPair, collectionId: number, fromAddressObj: ICrossAccountId, amount=1n): Promise<boolean> {2092 return await super.burnTokenFrom(signer, collectionId, 0, fromAddressObj, amount);2093 }20942095 /**2096 * Get total collection supply2097 * @param collectionId2098 * @returns2099 */2100 async getTotalPieces(collectionId: number): Promise<bigint> {2101 return (await this.helper.callRpc('api.rpc.unique.totalPieces', [collectionId, 0])).unwrap().toBigInt();2102 }21032104 /**2105 * Set, change, or remove approved address to transfer tokens.2106 *2107 * @param signer keyring of signer2108 * @param collectionId ID of collection2109 * @param toAddressObj address to be approved2110 * @param amount amount of tokens to be approved2111 * @example approveTokens(aliceKeyring, 10, {Substrate: "5GHoZe9c73RYbVzq..."}, 1000n)2112 * @returns ```true``` if extrinsic success, otherwise ```false```2113 */2114 approveTokens(signer: IKeyringPair, collectionId: number, toAddressObj: ICrossAccountId, amount=1n) {2115 return super.approveToken(signer, collectionId, 0, toAddressObj, amount);2116 }21172118 /**2119 * Get amount of fungible tokens approved to transfer2120 * @param collectionId ID of collection2121 * @param fromAddressObj owner of tokens2122 * @param toAddressObj the address approved for the transfer of tokens on behalf of the owner2123 * @returns number of tokens approved for the transfer2124 */2125 getApprovedTokens(collectionId: number, fromAddressObj: ICrossAccountId, toAddressObj: ICrossAccountId) {2126 return super.getTokenApprovedPieces(collectionId, 0, toAddressObj, fromAddressObj);2127 }2128}212921302131class ChainGroup extends HelperGroup<ChainHelperBase> {2132 /**2133 * Get system properties of a chain2134 * @example getChainProperties();2135 * @returns ss58Format, token decimals, and token symbol2136 */2137 getChainProperties(): IChainProperties {2138 const properties = (this.helper.getApi() as any).registry.getChainProperties().toJSON();2139 return {2140 ss58Format: properties.ss58Format.toJSON(),2141 tokenDecimals: properties.tokenDecimals.toJSON(),2142 tokenSymbol: properties.tokenSymbol.toJSON(),2143 };2144 }21452146 /**2147 * Get chain header2148 * @example getLatestBlockNumber();2149 * @returns the number of the last block2150 */2151 async getLatestBlockNumber(): Promise<number> {2152 return (await this.helper.callRpc('api.rpc.chain.getHeader')).number.toNumber();2153 }21542155 /**2156 * Get block hash by block number2157 * @param blockNumber number of block2158 * @example getBlockHashByNumber(12345);2159 * @returns hash of a block2160 */2161 async getBlockHashByNumber(blockNumber: number): Promise<string | null> {2162 const blockHash = (await this.helper.callRpc('api.rpc.chain.getBlockHash', [blockNumber])).toJSON();2163 if(blockHash === '0x0000000000000000000000000000000000000000000000000000000000000000') return null;2164 return blockHash;2165 }21662167 // TODO add docs2168 async getBlock(blockHashOrNumber: string | number): Promise<IBlock | null> {2169 const blockHash = typeof blockHashOrNumber === 'string' ? blockHashOrNumber : await this.getBlockHashByNumber(blockHashOrNumber);2170 if (!blockHash) return null;2171 return (await this.helper.callRpc('api.rpc.chain.getBlock', [blockHash])).toHuman().block;2172 }21732174 /**2175 * Get account nonce2176 * @param address substrate address2177 * @example getNonce("5GrwvaEF5zXb26Fz...");2178 * @returns number, account's nonce2179 */2180 async getNonce(address: TSubstrateAccount): Promise<number> {2181 return (await this.helper.callRpc('api.query.system.account', [address])).nonce.toNumber();2182 }2183}21842185class SubstrateBalanceGroup<T extends ChainHelperBase> extends HelperGroup<T> {2186 /**2187 * Get substrate address balance2188 * @param address substrate address2189 * @example getSubstrate("5GrwvaEF5zXb26Fz...")2190 * @returns amount of tokens on address2191 */2192 async getSubstrate(address: TSubstrateAccount): Promise<bigint> {2193 return (await this.helper.callRpc('api.query.system.account', [address])).data.free.toBigInt();2194 }21952196 /**2197 * Transfer tokens to substrate address2198 * @param signer keyring of signer2199 * @param address substrate address of a recipient2200 * @param amount amount of tokens to be transfered2201 * @example transferToSubstrate(aliceKeyring, "5GrwvaEF5zXb26Fz...", 100_000_000_000n);2202 * @returns ```true``` if extrinsic success, otherwise ```false```2203 */2204 async transferToSubstrate(signer: TSigner, address: TSubstrateAccount, amount: bigint | string): Promise<boolean> {2205 const result = await this.helper.executeExtrinsic(signer, 'api.tx.balances.transfer', [address, amount], true/*, `Unable to transfer balance from ${this.helper.getSignerAddress(signer)} to ${address}`*/);22062207 let transfer = {from: null, to: null, amount: 0n} as any;2208 result.result.events.forEach(({event: {data, method, section}}) => {2209 if ((section === 'balances') && (method === 'Transfer')) {2210 transfer = {2211 from: this.helper.address.normalizeSubstrate(data[0]),2212 to: this.helper.address.normalizeSubstrate(data[1]),2213 amount: BigInt(data[2]),2214 };2215 }2216 });2217 const isSuccess = this.helper.address.normalizeSubstrate(typeof signer === 'string' ? signer : signer.address) === transfer.from2218 && this.helper.address.normalizeSubstrate(address) === transfer.to2219 && BigInt(amount) === transfer.amount;2220 return isSuccess;2221 }22222223 /**2224 * Get full substrate balance including free, miscFrozen, feeFrozen, and reserved2225 * @param address substrate address2226 * @returns2227 */2228 async getSubstrateFull(address: TSubstrateAccount): Promise<ISubstrateBalance> {2229 const accountInfo = (await this.helper.callRpc('api.query.system.account', [address])).data;2230 return {free: accountInfo.free.toBigInt(), miscFrozen: accountInfo.miscFrozen.toBigInt(), feeFrozen: accountInfo.feeFrozen.toBigInt(), reserved: accountInfo.reserved.toBigInt()};2231 }2232}22332234class EthereumBalanceGroup<T extends ChainHelperBase> extends HelperGroup<T> {2235 /**2236 * Get ethereum address balance2237 * @param address ethereum address2238 * @example getEthereum("0x9F0583DbB855d...")2239 * @returns amount of tokens on address2240 */2241 async getEthereum(address: TEthereumAccount): Promise<bigint> {2242 return (await this.helper.callRpc('api.rpc.eth.getBalance', [address])).toBigInt();2243 }22442245 /**2246 * Transfer tokens to address2247 * @param signer keyring of signer2248 * @param address Ethereum address of a recipient2249 * @param amount amount of tokens to be transfered2250 * @example transferToEthereum(alithKeyring, "0x9F0583DbB855d...", 100_000_000_000n);2251 * @returns ```true``` if extrinsic success, otherwise ```false```2252 */2253 async transferToEthereum(signer: TSigner, address: TEthereumAccount, amount: bigint | string): Promise<boolean> {2254 const result = await this.helper.executeExtrinsic(signer, 'api.tx.balances.transfer', [address, amount], true);22552256 let transfer = {from: null, to: null, amount: 0n} as any;2257 result.result.events.forEach(({event: {data, method, section}}) => {2258 if ((section === 'balances') && (method === 'Transfer')) {2259 transfer = {2260 from: data[0].toString(),2261 to: data[1].toString(),2262 amount: BigInt(data[2]),2263 };2264 }2265 });2266 const isSuccess = (typeof signer === 'string' ? signer : signer.address) === transfer.from2267 && address === transfer.to2268 && BigInt(amount) === transfer.amount;2269 return isSuccess;2270 }2271}22722273class BalanceGroup<T extends ChainHelperBase> extends HelperGroup<T> {2274 subBalanceGroup: SubstrateBalanceGroup<T>;2275 ethBalanceGroup: EthereumBalanceGroup<T>;22762277 constructor(helper: T) {2278 super(helper);2279 this.subBalanceGroup = new SubstrateBalanceGroup(helper);2280 this.ethBalanceGroup = new EthereumBalanceGroup(helper);2281 }22822283 getCollectionCreationPrice(): bigint {2284 return 2n * this.getOneTokenNominal();2285 }2286 /**2287 * Representation of the native token in the smallest unit - one OPAL (OPL), QUARTZ (QTZ), or UNIQUE (UNQ).2288 * @example getOneTokenNominal()2289 * @returns ```BigInt``` representation of the native token in the smallest unit, e.g. ```1_000_000_000_000_000_000n``` for QTZ.2290 */2291 getOneTokenNominal(): bigint {2292 const chainProperties = this.helper.chain.getChainProperties();2293 return 10n ** BigInt((chainProperties.tokenDecimals || [18])[0]);2294 }22952296 /**2297 * Get substrate address balance2298 * @param address substrate address2299 * @example getSubstrate("5GrwvaEF5zXb26Fz...")2300 * @returns amount of tokens on address2301 */2302 getSubstrate(address: TSubstrateAccount): Promise<bigint> {2303 return this.subBalanceGroup.getSubstrate(address);2304 }23052306 /**2307 * Get full substrate balance including free, miscFrozen, feeFrozen, and reserved2308 * @param address substrate address2309 * @returns2310 */2311 getSubstrateFull(address: TSubstrateAccount): Promise<ISubstrateBalance> {2312 return this.subBalanceGroup.getSubstrateFull(address);2313 }23142315 /**2316 * Get ethereum address balance2317 * @param address ethereum address2318 * @example getEthereum("0x9F0583DbB855d...")2319 * @returns amount of tokens on address2320 */2321 getEthereum(address: TEthereumAccount): Promise<bigint> {2322 return this.ethBalanceGroup.getEthereum(address);2323 }23242325 /**2326 * Transfer tokens to substrate address2327 * @param signer keyring of signer2328 * @param address substrate address of a recipient2329 * @param amount amount of tokens to be transfered2330 * @example transferToSubstrate(aliceKeyring, "5GrwvaEF5zXb26Fz...", 100_000_000_000n);2331 * @returns ```true``` if extrinsic success, otherwise ```false```2332 */2333 transferToSubstrate(signer: TSigner, address: TSubstrateAccount, amount: bigint | string): Promise<boolean> {2334 return this.subBalanceGroup.transferToSubstrate(signer, address, amount);2335 }23362337 async forceTransferToSubstrate(signer: TSigner, from: TSubstrateAccount, to: TSubstrateAccount, amount: bigint | string): Promise<boolean> {2338 const result = await this.helper.executeExtrinsic(signer, 'api.tx.balances.forceTransfer', [from, to, amount], true);23392340 let transfer = {from: null, to: null, amount: 0n} as any;2341 result.result.events.forEach(({event: {data, method, section}}) => {2342 if ((section === 'balances') && (method === 'Transfer')) {2343 transfer = {2344 from: this.helper.address.normalizeSubstrate(data[0]),2345 to: this.helper.address.normalizeSubstrate(data[1]),2346 amount: BigInt(data[2]),2347 };2348 }2349 });2350 let isSuccess = this.helper.address.normalizeSubstrate(from) === transfer.from;2351 isSuccess = isSuccess && this.helper.address.normalizeSubstrate(to) === transfer.to;2352 isSuccess = isSuccess && BigInt(amount) === transfer.amount;2353 return isSuccess;2354 }2355}23562357class AddressGroup extends HelperGroup<ChainHelperBase> {2358 /**2359 * Normalizes the address to the specified ss58 format, by default ```42```.2360 * @param address substrate address2361 * @param ss58Format format for address conversion, by default ```42```2362 * @example normalizeSubstrate("unjKJQJrRd238pkUZZvzDQrfKuM39zBSnQ5zjAGAGcdRhaJTx") // returns 5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY2363 * @returns substrate address converted to normalized (i.e., starting with 5) or specified explicitly representation2364 */2365 normalizeSubstrate(address: TSubstrateAccount, ss58Format = 42): TSubstrateAccount {2366 return CrossAccountId.normalizeSubstrateAddress(address, ss58Format);2367 }23682369 /**2370 * Get address in the connected chain format2371 * @param address substrate address2372 * @example normalizeSubstrateToChainFormat("5GrwvaEF5zXb26Fz...") // returns unjKJQJrRd238pkUZZ... for Unique Network2373 * @returns address in chain format2374 */2375 normalizeSubstrateToChainFormat(address: TSubstrateAccount): TSubstrateAccount {2376 return this.normalizeSubstrate(address, this.helper.chain.getChainProperties().ss58Format);2377 }23782379 /**2380 * Get substrate mirror of an ethereum address2381 * @param ethAddress ethereum address2382 * @param toChainFormat false for normalized account2383 * @example ethToSubstrate('0x9F0583DbB855d...')2384 * @returns substrate mirror of a provided ethereum address2385 */2386 ethToSubstrate(ethAddress: TEthereumAccount, toChainFormat=false): TSubstrateAccount {2387 return CrossAccountId.translateEthToSub(ethAddress, toChainFormat ? this.helper.chain.getChainProperties().ss58Format : undefined);2388 }23892390 /**2391 * Get ethereum mirror of a substrate address2392 * @param subAddress substrate account2393 * @example substrateToEth("5DnSF6RRjwteE3BrC...")2394 * @returns ethereum mirror of a provided substrate address2395 */2396 substrateToEth(subAddress: TSubstrateAccount): TEthereumAccount {2397 return CrossAccountId.translateSubToEth(subAddress);2398 }23992400 /**2401 * Encode key to substrate address2402 * @param key key for encoding address2403 * @param ss58Format prefix for encoding to the address of the corresponding network2404 * @returns encoded substrate address2405 */2406 encodeSubstrateAddress (key: Uint8Array | string | bigint, ss58Format = 42): string {2407 const u8a :Uint8Array = typeof key === 'string'2408 ? hexToU8a(key)2409 : typeof key === 'bigint'2410 ? hexToU8a(key.toString(16))2411 : key;2412 2413 if (ss58Format < 0 || ss58Format > 16383 || [46, 47].includes(ss58Format)) {2414 throw new Error(`ss58Format is not valid, received ${typeofss58Format} "${ss58Format}"`);2415 }2416 2417 const allowedDecodedLengths = [1, 2, 4, 8, 32, 33];2418 if (!allowedDecodedLengths.includes(u8a.length)) {2419 throw new Error(`key length is not valid, received ${u8a.length}, valid values are ${allowedDecodedLengths.join(', ')}`);2420 }2421 2422 const u8aPrefix = ss58Format < 642423 ? new Uint8Array([ss58Format])2424 : new Uint8Array([2425 ((ss58Format & 0xfc) >> 2) | 0x40,2426 (ss58Format >> 8) | ((ss58Format & 0x03) << 6),2427 ]);24282429 const input = u8aConcat(u8aPrefix, u8a);2430 2431 return base58Encode(u8aConcat(2432 input,2433 blake2AsU8a(input).subarray(0, [32, 33].includes(u8a.length) ? 2 : 1),2434 ));2435 }24362437 /**2438 * Restore substrate address from bigint representation2439 * @param number decimal representation of substrate address2440 * @returns substrate address2441 */2442 restoreCrossAccountFromBigInt(number: bigint): TSubstrateAccount {2443 if (this.helper.api === null) {2444 throw 'Not connected';2445 }2446 const res = this.helper.api.registry.createType('AccountId', '0x' + number.toString(16).padStart(64, '0')).toJSON();2447 if (res === undefined || res === null) {2448 throw 'Restore address error';2449 }2450 return res.toString();2451 }24522453 /**2454 * Convert etherium cross account id to substrate cross account id2455 * @param ethCrossAccount etherium cross account2456 * @returns substrate cross account id2457 */2458 convertCrossAccountFromEthCrossAccount(ethCrossAccount: IEthCrossAccountId): ICrossAccountId {2459 if (ethCrossAccount.sub === '0') {2460 return {Ethereum: ethCrossAccount.eth.toLocaleLowerCase()};2461 }2462 2463 const ss58 = this.restoreCrossAccountFromBigInt(BigInt(ethCrossAccount.sub));2464 return {Substrate: ss58};2465 }24662467 paraSiblingSovereignAccount(paraid: number) {2468 // We are getting a *sibling* parachain sovereign account,2469 // so we need a sibling prefix: encoded(b"sibl") == 0x7369626c2470 const siblingPrefix = '0x7369626c';24712472 const encodedParaId = this.helper.getApi().createType('u32', paraid).toHex(true).substring(2);2473 const suffix = '000000000000000000000000000000000000000000000000';24742475 return siblingPrefix + encodedParaId + suffix;2476 }2477}24782479class StakingGroup extends HelperGroup<UniqueHelper> {2480 /**2481 * Stake tokens for App Promotion2482 * @param signer keyring of signer2483 * @param amountToStake amount of tokens to stake2484 * @param label extra label for log2485 * @returns2486 */2487 async stake(signer: TSigner, amountToStake: bigint, label?: string): Promise<boolean> {2488 if(typeof label === 'undefined') label = `${signer.address} amount: ${amountToStake}`;2489 const _stakeResult = await this.helper.executeExtrinsic(2490 signer, 'api.tx.appPromotion.stake',2491 [amountToStake], true,2492 );2493 // TODO extract info from stakeResult2494 return true;2495 }24962497 /**2498 * Unstake tokens for App Promotion2499 * @param signer keyring of signer2500 * @param amountToUnstake amount of tokens to unstake2501 * @param label extra label for log2502 * @returns block number where balances will be unlocked2503 */2504 async unstake(signer: TSigner, label?: string): Promise<number> {2505 if(typeof label === 'undefined') label = `${signer.address}`;2506 const _unstakeResult = await this.helper.executeExtrinsic(2507 signer, 'api.tx.appPromotion.unstake',2508 [], true,2509 );2510 // TODO extract block number fron events2511 return 1;2512 }25132514 /**2515 * Get total staked amount for address2516 * @param address substrate or ethereum address2517 * @returns total staked amount2518 */2519 async getTotalStaked(address?: ICrossAccountId): Promise<bigint> {2520 if (address) return (await this.helper.callRpc('api.rpc.appPromotion.totalStaked', [address])).toBigInt();2521 return (await this.helper.callRpc('api.rpc.appPromotion.totalStaked')).toBigInt();2522 }25232524 /**2525 * Get total staked per block2526 * @param address substrate or ethereum address2527 * @returns array of stakes. `block` – the number of the block in which the stake was made. `amount` - the number of tokens staked in the block2528 */2529 async getTotalStakedPerBlock(address: ICrossAccountId): Promise<IStakingInfo[]> {2530 const rawTotalStakerdPerBlock = await this.helper.callRpc('api.rpc.appPromotion.totalStakedPerBlock', [address]);2531 return rawTotalStakerdPerBlock.map(([block, amount]: any[]) => {2532 return {2533 block: block.toBigInt(),2534 amount: amount.toBigInt(),2535 };2536 });2537 }25382539 /**2540 * Get total pending unstake amount for address2541 * @param address substrate or ethereum address2542 * @returns total pending unstake amount2543 */2544 async getPendingUnstake(address: ICrossAccountId): Promise<bigint> {2545 return (await this.helper.callRpc('api.rpc.appPromotion.pendingUnstake', [address])).toBigInt();2546 }25472548 /**2549 * Get pending unstake amount per block for address2550 * @param address substrate or ethereum address2551 * @returns array of pending stakes. `block` – the number of the block in which the unstake was made. `amount` - the number of tokens unstaked in the block2552 */2553 async getPendingUnstakePerBlock(address: ICrossAccountId): Promise<IStakingInfo[]> {2554 const rawUnstakedPerBlock = await this.helper.callRpc('api.rpc.appPromotion.pendingUnstakePerBlock', [address]);2555 const result = rawUnstakedPerBlock.map(([block, amount]: any[]) => {2556 return {2557 block: block.toBigInt(),2558 amount: amount.toBigInt(),2559 };2560 });2561 return result;2562 }2563}25642565class SchedulerGroup extends HelperGroup<UniqueHelper> {2566 constructor(helper: UniqueHelper) {2567 super(helper);2568 }25692570 cancelScheduled(signer: TSigner, scheduledId: string) {2571 return this.helper.executeExtrinsic(2572 signer,2573 'api.tx.scheduler.cancelNamed',2574 [scheduledId],2575 true,2576 );2577 }25782579 changePriority(signer: TSigner, scheduledId: string, priority: number) {2580 return this.helper.executeExtrinsic(2581 signer,2582 'api.tx.scheduler.changeNamedPriority',2583 [scheduledId, priority],2584 true,2585 );2586 }25872588 scheduleAt<T extends UniqueHelper>(2589 executionBlockNumber: number,2590 options: ISchedulerOptions = {},2591 ) {2592 return this.schedule<T>('schedule', executionBlockNumber, options);2593 }25942595 scheduleAfter<T extends UniqueHelper>(2596 blocksBeforeExecution: number,2597 options: ISchedulerOptions = {},2598 ) {2599 return this.schedule<T>('scheduleAfter', blocksBeforeExecution, options);2600 }26012602 schedule<T extends UniqueHelper>(2603 scheduleFn: 'schedule' | 'scheduleAfter',2604 blocksNum: number,2605 options: ISchedulerOptions = {},2606 ) {2607 // eslint-disable-next-line @typescript-eslint/naming-convention2608 const ScheduledHelperType = ScheduledUniqueHelper(this.helper.helperBase);2609 return this.helper.clone(ScheduledHelperType, {2610 scheduleFn,2611 blocksNum,2612 options,2613 }) as T;2614 }2615}26162617class ForeignAssetsGroup extends HelperGroup<UniqueHelper> {2618 async register(signer: TSigner, ownerAddress: TSubstrateAccount, location: any, metadata: IForeignAssetMetadata) {2619 await this.helper.executeExtrinsic(2620 signer,2621 'api.tx.foreignAssets.registerForeignAsset',2622 [ownerAddress, location, metadata],2623 true,2624 );2625 }26262627 async update(signer: TSigner, foreignAssetId: number, location: any, metadata: IForeignAssetMetadata) {2628 await this.helper.executeExtrinsic(2629 signer,2630 'api.tx.foreignAssets.updateForeignAsset',2631 [foreignAssetId, location, metadata],2632 true,2633 );2634 }2635}26362637class XcmGroup<T extends ChainHelperBase> extends HelperGroup<T> {2638 palletName: string;26392640 constructor(helper: T, palletName: string) {2641 super(helper);26422643 this.palletName = palletName;2644 }26452646 async limitedReserveTransferAssets(signer: TSigner, destination: any, beneficiary: any, assets: any, feeAssetItem: number, weightLimit: number) {2647 await this.helper.executeExtrinsic(signer, `api.tx.${this.palletName}.limitedReserveTransferAssets`, [destination, beneficiary, assets, feeAssetItem, {Limited: weightLimit}], true);2648 }2649}26502651class XTokensGroup<T extends ChainHelperBase> extends HelperGroup<T> {2652 async transfer(signer: TSigner, currencyId: any, amount: bigint, destination: any, destWeight: number) {2653 await this.helper.executeExtrinsic(signer, 'api.tx.xTokens.transfer', [currencyId, amount, destination, destWeight], true);2654 }26552656 async transferMultiasset(signer: TSigner, asset: any, destination: any, destWeight: number) {2657 await this.helper.executeExtrinsic(signer, 'api.tx.xTokens.transferMultiasset', [asset, destination, destWeight], true);2658 }26592660 async transferMulticurrencies(signer: TSigner, currencies: any[], feeItem: number, destLocation: any, destWeight: number) {2661 await this.helper.executeExtrinsic(signer, 'api.tx.xTokens.transferMulticurrencies', [currencies, feeItem, destLocation, destWeight], true);2662 }2663}26642665class TokensGroup<T extends ChainHelperBase> extends HelperGroup<T> {2666 async accounts(address: string, currencyId: any) {2667 const {free} = (await this.helper.callRpc('api.query.tokens.accounts', [address, currencyId])).toJSON() as any;2668 return BigInt(free);2669 }2670}26712672class AssetsGroup<T extends ChainHelperBase> extends HelperGroup<T> {2673 async create(signer: TSigner, assetId: number, admin: string, minimalBalance: bigint) {2674 await this.helper.executeExtrinsic(signer, 'api.tx.assets.create', [assetId, admin, minimalBalance], true);2675 }26762677 async setMetadata(signer: TSigner, assetId: number, name: string, symbol: string, decimals: number) {2678 await this.helper.executeExtrinsic(signer, 'api.tx.assets.setMetadata', [assetId, name, symbol, decimals], true);2679 }26802681 async mint(signer: TSigner, assetId: number, beneficiary: string, amount: bigint) {2682 await this.helper.executeExtrinsic(signer, 'api.tx.assets.mint', [assetId, beneficiary, amount], true);2683 }26842685 async account(assetId: string | number, address: string) {2686 const accountAsset = (2687 await this.helper.callRpc('api.query.assets.account', [assetId, address])2688 ).toJSON()! as any;26892690 if (accountAsset !== null) {2691 return BigInt(accountAsset['balance']);2692 } else {2693 return null;2694 }2695 }2696}26972698class AcalaAssetRegistryGroup extends HelperGroup<AcalaHelper> {2699 async registerForeignAsset(signer: TSigner, destination: any, metadata: AcalaAssetMetadata) {2700 await this.helper.executeExtrinsic(signer, 'api.tx.assetRegistry.registerForeignAsset', [destination, metadata], true);2701 }2702}27032704class MoonbeamAssetManagerGroup extends HelperGroup<MoonbeamHelper> {2705 makeRegisterForeignAssetProposal(assetInfo: MoonbeamAssetInfo) {2706 const apiPrefix = 'api.tx.assetManager.';27072708 const registerTx = this.helper.constructApiCall(2709 apiPrefix + 'registerForeignAsset',2710 [assetInfo.location, assetInfo.metadata, assetInfo.existentialDeposit, assetInfo.isSufficient],2711 );27122713 const setUnitsTx = this.helper.constructApiCall(2714 apiPrefix + 'setAssetUnitsPerSecond',2715 [assetInfo.location, assetInfo.unitsPerSecond, assetInfo.numAssetsWeightHint],2716 );27172718 const batchCall = this.helper.getApi().tx.utility.batchAll([registerTx, setUnitsTx]);2719 const encodedProposal = batchCall?.method.toHex() || '';2720 return encodedProposal;2721 }27222723 async assetTypeId(location: any) {2724 return await this.helper.callRpc('api.query.assetManager.assetTypeId', [location]);2725 }2726}27272728class MoonbeamDemocracyGroup extends HelperGroup<MoonbeamHelper> {2729 async notePreimage(signer: TSigner, encodedProposal: string) {2730 await this.helper.executeExtrinsic(signer, 'api.tx.democracy.notePreimage', [encodedProposal], true);2731 }27322733 externalProposeMajority(proposalHash: string) {2734 return this.helper.constructApiCall('api.tx.democracy.externalProposeMajority', [proposalHash]);2735 }27362737 fastTrack(proposalHash: string, votingPeriod: number, delayPeriod: number) {2738 return this.helper.constructApiCall('api.tx.democracy.fastTrack', [proposalHash, votingPeriod, delayPeriod]);2739 }27402741 async referendumVote(signer: TSigner, referendumIndex: number, accountVote: DemocracyStandardAccountVote) {2742 await this.helper.executeExtrinsic(signer, 'api.tx.democracy.vote', [referendumIndex, {Standard: accountVote}], true);2743 }2744}27452746class MoonbeamCollectiveGroup extends HelperGroup<MoonbeamHelper> {2747 collective: string;27482749 constructor(helper: MoonbeamHelper, collective: string) {2750 super(helper);27512752 this.collective = collective;2753 }27542755 async propose(signer: TSigner, threshold: number, proposalHash: string, lengthBound: number) {2756 await this.helper.executeExtrinsic(signer, `api.tx.${this.collective}.propose`, [threshold, proposalHash, lengthBound], true);2757 }27582759 async vote(signer: TSigner, proposalHash: string, proposalIndex: number, approve: boolean) {2760 await this.helper.executeExtrinsic(signer, `api.tx.${this.collective}.vote`, [proposalHash, proposalIndex, approve], true);2761 }27622763 async close(signer: TSigner, proposalHash: string, proposalIndex: number, weightBound: number, lengthBound: number) {2764 await this.helper.executeExtrinsic(signer, `api.tx.${this.collective}.close`, [proposalHash, proposalIndex, weightBound, lengthBound], true);2765 }27662767 async proposalCount() {2768 return Number(await this.helper.callRpc(`api.query.${this.collective}.proposalCount`, []));2769 }2770}27712772export type ChainHelperBaseConstructor = new(...args: any[]) => ChainHelperBase;2773export type UniqueHelperConstructor = new(...args: any[]) => UniqueHelper;27742775export class UniqueHelper extends ChainHelperBase {2776 balance: BalanceGroup<UniqueHelper>;2777 collection: CollectionGroup;2778 nft: NFTGroup;2779 rft: RFTGroup;2780 ft: FTGroup;2781 staking: StakingGroup;2782 scheduler: SchedulerGroup;2783 foreignAssets: ForeignAssetsGroup;2784 xcm: XcmGroup<UniqueHelper>;2785 xTokens: XTokensGroup<UniqueHelper>;2786 tokens: TokensGroup<UniqueHelper>;27872788 constructor(logger?: ILogger, options: {[key: string]: any} = {}) {2789 super(logger, options.helperBase ?? UniqueHelper);27902791 this.balance = new BalanceGroup(this);2792 this.collection = new CollectionGroup(this);2793 this.nft = new NFTGroup(this);2794 this.rft = new RFTGroup(this);2795 this.ft = new FTGroup(this);2796 this.staking = new StakingGroup(this);2797 this.scheduler = new SchedulerGroup(this);2798 this.foreignAssets = new ForeignAssetsGroup(this);2799 this.xcm = new XcmGroup(this, 'polkadotXcm');2800 this.xTokens = new XTokensGroup(this);2801 this.tokens = new TokensGroup(this);2802 }28032804 getSudo<T extends UniqueHelper>() {2805 // eslint-disable-next-line @typescript-eslint/naming-convention2806 const SudoHelperType = SudoHelper(this.helperBase);2807 return this.clone(SudoHelperType) as T;2808 }2809}28102811export class XcmChainHelper extends ChainHelperBase {2812 async connect(wsEndpoint: string, _listeners?: any): Promise<void> {2813 const wsProvider = new WsProvider(wsEndpoint);2814 this.api = new ApiPromise({2815 provider: wsProvider,2816 });2817 await this.api.isReadyOrError;2818 this.network = await UniqueHelper.detectNetwork(this.api);2819 }2820}28212822export class RelayHelper extends XcmChainHelper {2823 xcm: XcmGroup<RelayHelper>;28242825 constructor(logger?: ILogger, options: {[key: string]: any} = {}) {2826 super(logger, options.helperBase ?? RelayHelper);28272828 this.xcm = new XcmGroup(this, 'xcmPallet');2829 }2830}28312832export class WestmintHelper extends XcmChainHelper {2833 balance: SubstrateBalanceGroup<WestmintHelper>;2834 xcm: XcmGroup<WestmintHelper>;2835 assets: AssetsGroup<WestmintHelper>;2836 xTokens: XTokensGroup<WestmintHelper>;28372838 constructor(logger?: ILogger, options: {[key: string]: any} = {}) {2839 super(logger, options.helperBase ?? WestmintHelper);28402841 this.balance = new SubstrateBalanceGroup(this);2842 this.xcm = new XcmGroup(this, 'polkadotXcm');2843 this.assets = new AssetsGroup(this);2844 this.xTokens = new XTokensGroup(this);2845 }2846}28472848export class MoonbeamHelper extends XcmChainHelper {2849 balance: EthereumBalanceGroup<MoonbeamHelper>;2850 assetManager: MoonbeamAssetManagerGroup;2851 assets: AssetsGroup<MoonbeamHelper>;2852 xTokens: XTokensGroup<MoonbeamHelper>;2853 democracy: MoonbeamDemocracyGroup;2854 collective: {2855 council: MoonbeamCollectiveGroup,2856 techCommittee: MoonbeamCollectiveGroup,2857 };28582859 constructor(logger?: ILogger, options: {[key: string]: any} = {}) {2860 super(logger, options.helperBase ?? MoonbeamHelper);28612862 this.balance = new EthereumBalanceGroup(this);2863 this.assetManager = new MoonbeamAssetManagerGroup(this);2864 this.assets = new AssetsGroup(this);2865 this.xTokens = new XTokensGroup(this);2866 this.democracy = new MoonbeamDemocracyGroup(this);2867 this.collective = {2868 council: new MoonbeamCollectiveGroup(this, 'councilCollective'),2869 techCommittee: new MoonbeamCollectiveGroup(this, 'techCommitteeCollective'),2870 };2871 }2872}28732874export class AcalaHelper extends XcmChainHelper {2875 balance: SubstrateBalanceGroup<AcalaHelper>;2876 assetRegistry: AcalaAssetRegistryGroup;2877 xTokens: XTokensGroup<AcalaHelper>;2878 tokens: TokensGroup<AcalaHelper>;28792880 constructor(logger?: ILogger, options: {[key: string]: any} = {}) {2881 super(logger, options.helperBase ?? AcalaHelper);28822883 this.balance = new SubstrateBalanceGroup(this);2884 this.assetRegistry = new AcalaAssetRegistryGroup(this);2885 this.xTokens = new XTokensGroup(this);2886 this.tokens = new TokensGroup(this);2887 }28882889 getSudo<T extends AcalaHelper>() {2890 // eslint-disable-next-line @typescript-eslint/naming-convention2891 const SudoHelperType = SudoHelper(this.helperBase);2892 return this.clone(SudoHelperType) as T;2893 }2894}28952896// eslint-disable-next-line @typescript-eslint/naming-convention2897function ScheduledUniqueHelper<T extends UniqueHelperConstructor>(Base: T) {2898 return class extends Base {2899 scheduleFn: 'schedule' | 'scheduleAfter';2900 blocksNum: number;2901 options: ISchedulerOptions;29022903 constructor(...args: any[]) {2904 const logger = args[0] as ILogger;2905 const options = args[1] as {2906 scheduleFn: 'schedule' | 'scheduleAfter',2907 blocksNum: number,2908 options: ISchedulerOptions2909 };29102911 super(logger);29122913 this.scheduleFn = options.scheduleFn;2914 this.blocksNum = options.blocksNum;2915 this.options = options.options;2916 }29172918 executeExtrinsic(sender: IKeyringPair, scheduledExtrinsic: string, scheduledParams: any[], expectSuccess?: boolean): Promise<ITransactionResult> {2919 const scheduledTx = this.constructApiCall(scheduledExtrinsic, scheduledParams);2920 2921 const mandatorySchedArgs = [2922 this.blocksNum,2923 this.options.periodic ? [this.options.periodic.period, this.options.periodic.repetitions] : null,2924 this.options.priority ?? null,2925 scheduledTx,2926 ];2927 2928 let schedArgs;2929 let scheduleFn;29302931 if (this.options.scheduledId) {2932 schedArgs = [this.options.scheduledId!, ...mandatorySchedArgs];29332934 if (this.scheduleFn == 'schedule') {2935 scheduleFn = 'scheduleNamed';2936 } else if (this.scheduleFn == 'scheduleAfter') {2937 scheduleFn = 'scheduleNamedAfter';2938 }2939 } else {2940 schedArgs = mandatorySchedArgs;2941 scheduleFn = this.scheduleFn;2942 }29432944 const extrinsic = 'api.tx.scheduler.' + scheduleFn;29452946 return super.executeExtrinsic(2947 sender,2948 extrinsic,2949 schedArgs,2950 expectSuccess,2951 );2952 }2953 };2954}29552956// eslint-disable-next-line @typescript-eslint/naming-convention2957function SudoHelper<T extends ChainHelperBaseConstructor>(Base: T) {2958 return class extends Base {2959 constructor(...args: any[]) {2960 super(...args);2961 }29622963 executeExtrinsic (2964 sender: IKeyringPair,2965 extrinsic: string,2966 params: any[],2967 expectSuccess?: boolean,2968 ): Promise<ITransactionResult> {2969 const call = this.constructApiCall(extrinsic, params);2970 return super.executeExtrinsic(2971 sender,2972 'api.tx.sudo.sudo',2973 [call],2974 expectSuccess,2975 );2976 }2977 };2978}29792980export class UniqueBaseCollection {2981 helper: UniqueHelper;2982 collectionId: number;29832984 constructor(collectionId: number, uniqueHelper: UniqueHelper) {2985 this.collectionId = collectionId;2986 this.helper = uniqueHelper;2987 }29882989 async getData() {2990 return await this.helper.collection.getData(this.collectionId);2991 }29922993 async getLastTokenId() {2994 return await this.helper.collection.getLastTokenId(this.collectionId);2995 }29962997 async doesTokenExist(tokenId: number) {2998 return await this.helper.collection.doesTokenExist(this.collectionId, tokenId);2999 }30003001 async getAdmins() {3002 return await this.helper.collection.getAdmins(this.collectionId);3003 }30043005 async getAllowList() {3006 return await this.helper.collection.getAllowList(this.collectionId);3007 }30083009 async getEffectiveLimits() {3010 return await this.helper.collection.getEffectiveLimits(this.collectionId);3011 }30123013 async getProperties(propertyKeys?: string[] | null) {3014 return await this.helper.collection.getProperties(this.collectionId, propertyKeys);3015 }30163017 async getTokenNextSponsored(tokenId: number, addressObj: ICrossAccountId) {3018 return await this.helper.collection.getTokenNextSponsored(this.collectionId, tokenId, addressObj);3019 }30203021 async getOptions() {3022 return await this.helper.collection.getCollectionOptions(this.collectionId);3023 }30243025 async setSponsor(signer: TSigner, sponsorAddress: TSubstrateAccount) {3026 return await this.helper.collection.setSponsor(signer, this.collectionId, sponsorAddress);3027 }30283029 async confirmSponsorship(signer: TSigner) {3030 return await this.helper.collection.confirmSponsorship(signer, this.collectionId);3031 }30323033 async removeSponsor(signer: TSigner) {3034 return await this.helper.collection.removeSponsor(signer, this.collectionId);3035 }30363037 async setLimits(signer: TSigner, limits: ICollectionLimits) {3038 return await this.helper.collection.setLimits(signer, this.collectionId, limits);3039 }30403041 async changeOwner(signer: TSigner, ownerAddress: TSubstrateAccount) {3042 return await this.helper.collection.changeOwner(signer, this.collectionId, ownerAddress);3043 }30443045 async addAdmin(signer: TSigner, adminAddressObj: ICrossAccountId) {3046 return await this.helper.collection.addAdmin(signer, this.collectionId, adminAddressObj);3047 }30483049 async addToAllowList(signer: TSigner, addressObj: ICrossAccountId) {3050 return await this.helper.collection.addToAllowList(signer, this.collectionId, addressObj);3051 }30523053 async removeFromAllowList(signer: TSigner, addressObj: ICrossAccountId) {3054 return await this.helper.collection.removeFromAllowList(signer, this.collectionId, addressObj);3055 }30563057 async removeAdmin(signer: TSigner, adminAddressObj: ICrossAccountId) {3058 return await this.helper.collection.removeAdmin(signer, this.collectionId, adminAddressObj);3059 }30603061 async setProperties(signer: TSigner, properties: IProperty[]) {3062 return await this.helper.collection.setProperties(signer, this.collectionId, properties);3063 }30643065 async deleteProperties(signer: TSigner, propertyKeys: string[]) {3066 return await this.helper.collection.deleteProperties(signer, this.collectionId, propertyKeys);3067 }30683069 async setPermissions(signer: TSigner, permissions: ICollectionPermissions) {3070 return await this.helper.collection.setPermissions(signer, this.collectionId, permissions);3071 }30723073 async enableNesting(signer: TSigner, permissions: INestingPermissions) {3074 return await this.helper.collection.enableNesting(signer, this.collectionId, permissions);3075 }30763077 async disableNesting(signer: TSigner) {3078 return await this.helper.collection.disableNesting(signer, this.collectionId);3079 }30803081 async burn(signer: TSigner) {3082 return await this.helper.collection.burn(signer, this.collectionId);3083 }30843085 scheduleAt<T extends UniqueHelper>(3086 executionBlockNumber: number,3087 options: ISchedulerOptions = {},3088 ) {3089 const scheduledHelper = this.helper.scheduler.scheduleAt<T>(executionBlockNumber, options);3090 return new UniqueBaseCollection(this.collectionId, scheduledHelper);3091 }30923093 scheduleAfter<T extends UniqueHelper>(3094 blocksBeforeExecution: number,3095 options: ISchedulerOptions = {},3096 ) {3097 const scheduledHelper = this.helper.scheduler.scheduleAfter<T>(blocksBeforeExecution, options);3098 return new UniqueBaseCollection(this.collectionId, scheduledHelper);3099 }31003101 getSudo<T extends UniqueHelper>() {3102 return new UniqueBaseCollection(this.collectionId, this.helper.getSudo<T>());3103 }3104}310531063107export class UniqueNFTCollection extends UniqueBaseCollection {3108 getTokenObject(tokenId: number) {3109 return new UniqueNFToken(tokenId, this);3110 }31113112 async getTokensByAddress(addressObj: ICrossAccountId) {3113 return await this.helper.nft.getTokensByAddress(this.collectionId, addressObj);3114 }31153116 async getToken(tokenId: number, blockHashAt?: string) {3117 return await this.helper.nft.getToken(this.collectionId, tokenId, [], blockHashAt);3118 }31193120 async getTokenOwner(tokenId: number, blockHashAt?: string) {3121 return await this.helper.nft.getTokenOwner(this.collectionId, tokenId, blockHashAt);3122 }31233124 async getTokenTopmostOwner(tokenId: number, blockHashAt?: string) {3125 return await this.helper.nft.getTokenTopmostOwner(this.collectionId, tokenId, blockHashAt);3126 }31273128 async getTokenChildren(tokenId: number, blockHashAt?: string) {3129 return await this.helper.nft.getTokenChildren(this.collectionId, tokenId, blockHashAt);3130 }31313132 async getPropertyPermissions(propertyKeys: string[] | null = null) {3133 return await this.helper.nft.getPropertyPermissions(this.collectionId, propertyKeys);3134 }31353136 async getTokenProperties(tokenId: number, propertyKeys?: string[] | null) {3137 return await this.helper.nft.getTokenProperties(this.collectionId, tokenId, propertyKeys);3138 }31393140 async transferToken(signer: TSigner, tokenId: number, addressObj: ICrossAccountId) {3141 return await this.helper.nft.transferToken(signer, this.collectionId, tokenId, addressObj);3142 }31433144 async transferTokenFrom(signer: TSigner, tokenId: number, fromAddressObj: ICrossAccountId, toAddressObj: ICrossAccountId) {3145 return await this.helper.nft.transferTokenFrom(signer, this.collectionId, tokenId, fromAddressObj, toAddressObj);3146 }31473148 async approveToken(signer: TSigner, tokenId: number, toAddressObj: ICrossAccountId) {3149 return await this.helper.nft.approveToken(signer, this.collectionId, tokenId, toAddressObj);3150 }31513152 async isTokenApproved(tokenId: number, toAddressObj: ICrossAccountId) {3153 return await this.helper.nft.isTokenApproved(this.collectionId, tokenId, toAddressObj);3154 }31553156 async mintToken(signer: TSigner, owner: ICrossAccountId = {Substrate: signer.address}, properties?: IProperty[]) {3157 return await this.helper.nft.mintToken(signer, {collectionId: this.collectionId, owner, properties});3158 }31593160 async mintMultipleTokens(signer: TSigner, tokens: {owner: ICrossAccountId, properties?: IProperty[]}[]) {3161 return await this.helper.nft.mintMultipleTokens(signer, this.collectionId, tokens);3162 }31633164 async burnToken(signer: TSigner, tokenId: number) {3165 return await this.helper.nft.burnToken(signer, this.collectionId, tokenId);3166 }31673168 async burnTokenFrom(signer: TSigner, tokenId: number, fromAddressObj: ICrossAccountId) {3169 return await this.helper.nft.burnTokenFrom(signer, this.collectionId, tokenId, fromAddressObj);3170 }31713172 async setTokenProperties(signer: TSigner, tokenId: number, properties: IProperty[]) {3173 return await this.helper.nft.setTokenProperties(signer, this.collectionId, tokenId, properties);3174 }31753176 async deleteTokenProperties(signer: TSigner, tokenId: number, propertyKeys: string[]) {3177 return await this.helper.nft.deleteTokenProperties(signer, this.collectionId, tokenId, propertyKeys);3178 }31793180 async setTokenPropertyPermissions(signer: TSigner, permissions: ITokenPropertyPermission[]) {3181 return await this.helper.nft.setTokenPropertyPermissions(signer, this.collectionId, permissions);3182 }31833184 async nestToken(signer: TSigner, tokenId: number, toTokenObj: IToken) {3185 return await this.helper.nft.nestToken(signer, {collectionId: this.collectionId, tokenId}, toTokenObj);3186 }31873188 async unnestToken(signer: TSigner, tokenId: number, fromTokenObj: IToken, toAddressObj: ICrossAccountId) {3189 return await this.helper.nft.unnestToken(signer, {collectionId: this.collectionId, tokenId}, fromTokenObj, toAddressObj);3190 }31913192 scheduleAt<T extends UniqueHelper>(3193 executionBlockNumber: number,3194 options: ISchedulerOptions = {},3195 ) {3196 const scheduledHelper = this.helper.scheduler.scheduleAt<T>(executionBlockNumber, options);3197 return new UniqueNFTCollection(this.collectionId, scheduledHelper);3198 }31993200 scheduleAfter<T extends UniqueHelper>(3201 blocksBeforeExecution: number,3202 options: ISchedulerOptions = {},3203 ) {3204 const scheduledHelper = this.helper.scheduler.scheduleAfter<T>(blocksBeforeExecution, options);3205 return new UniqueNFTCollection(this.collectionId, scheduledHelper);3206 }32073208 getSudo<T extends UniqueHelper>() {3209 return new UniqueNFTCollection(this.collectionId, this.helper.getSudo<T>());3210 }3211}321232133214export class UniqueRFTCollection extends UniqueBaseCollection {3215 getTokenObject(tokenId: number) {3216 return new UniqueRFToken(tokenId, this);3217 }32183219 async getToken(tokenId: number, blockHashAt?: string) {3220 return await this.helper.rft.getToken(this.collectionId, tokenId, [], blockHashAt);3221 }32223223 async getTokensByAddress(addressObj: ICrossAccountId) {3224 return await this.helper.rft.getTokensByAddress(this.collectionId, addressObj);3225 }32263227 async getTop10TokenOwners(tokenId: number) {3228 return await this.helper.rft.getTokenTop10Owners(this.collectionId, tokenId);3229 }32303231 async getTokenBalance(tokenId: number, addressObj: ICrossAccountId) {3232 return await this.helper.rft.getTokenBalance(this.collectionId, tokenId, addressObj);3233 }32343235 async getTokenTotalPieces(tokenId: number) {3236 return await this.helper.rft.getTokenTotalPieces(this.collectionId, tokenId);3237 }32383239 async getTokenApprovedPieces(tokenId: number, fromAddressObj: ICrossAccountId, toAddressObj: ICrossAccountId) {3240 return await this.helper.rft.getTokenApprovedPieces(this.collectionId, tokenId, toAddressObj, fromAddressObj);3241 }32423243 async getPropertyPermissions(propertyKeys: string[] | null = null) {3244 return await this.helper.rft.getPropertyPermissions(this.collectionId, propertyKeys);3245 }32463247 async getTokenProperties(tokenId: number, propertyKeys?: string[] | null) {3248 return await this.helper.rft.getTokenProperties(this.collectionId, tokenId, propertyKeys);3249 }32503251 async transferToken(signer: TSigner, tokenId: number, addressObj: ICrossAccountId, amount=1n) {3252 return await this.helper.rft.transferToken(signer, this.collectionId, tokenId, addressObj, amount);3253 }32543255 async transferTokenFrom(signer: TSigner, tokenId: number, fromAddressObj: ICrossAccountId, toAddressObj: ICrossAccountId, amount=1n) {3256 return await this.helper.rft.transferTokenFrom(signer, this.collectionId, tokenId, fromAddressObj, toAddressObj, amount);3257 }32583259 async approveToken(signer: TSigner, tokenId: number, toAddressObj: ICrossAccountId, amount=1n) {3260 return await this.helper.rft.approveToken(signer, this.collectionId, tokenId, toAddressObj, amount);3261 }32623263 async repartitionToken(signer: TSigner, tokenId: number, amount: bigint) {3264 return await this.helper.rft.repartitionToken(signer, this.collectionId, tokenId, amount);3265 }32663267 async mintToken(signer: TSigner, pieces=1n, owner: ICrossAccountId = {Substrate: signer.address}, properties?: IProperty[]) {3268 return await this.helper.rft.mintToken(signer, {collectionId: this.collectionId, owner, pieces, properties});3269 }32703271 async mintMultipleTokens(signer: TSigner, tokens: {pieces: bigint, owner: ICrossAccountId, properties?: IProperty[]}[]) {3272 return await this.helper.rft.mintMultipleTokens(signer, this.collectionId, tokens);3273 }32743275 async burnToken(signer: TSigner, tokenId: number, amount=1n) {3276 return await this.helper.rft.burnToken(signer, this.collectionId, tokenId, amount);3277 }32783279 async burnTokenFrom(signer: TSigner, tokenId: number, fromAddressObj: ICrossAccountId, amount=1n) {3280 return await this.helper.rft.burnTokenFrom(signer, this.collectionId, tokenId, fromAddressObj, amount);3281 }32823283 async setTokenProperties(signer: TSigner, tokenId: number, properties: IProperty[]) {3284 return await this.helper.rft.setTokenProperties(signer, this.collectionId, tokenId, properties);3285 }32863287 async deleteTokenProperties(signer: TSigner, tokenId: number, propertyKeys: string[]) {3288 return await this.helper.rft.deleteTokenProperties(signer, this.collectionId, tokenId, propertyKeys);3289 }32903291 async setTokenPropertyPermissions(signer: TSigner, permissions: ITokenPropertyPermission[]) {3292 return await this.helper.rft.setTokenPropertyPermissions(signer, this.collectionId, permissions);3293 }32943295 scheduleAt<T extends UniqueHelper>(3296 executionBlockNumber: number,3297 options: ISchedulerOptions = {},3298 ) {3299 const scheduledHelper = this.helper.scheduler.scheduleAt<T>(executionBlockNumber, options);3300 return new UniqueRFTCollection(this.collectionId, scheduledHelper);3301 }33023303 scheduleAfter<T extends UniqueHelper>(3304 blocksBeforeExecution: number,3305 options: ISchedulerOptions = {},3306 ) {3307 const scheduledHelper = this.helper.scheduler.scheduleAfter<T>(blocksBeforeExecution, options);3308 return new UniqueRFTCollection(this.collectionId, scheduledHelper);3309 }33103311 getSudo<T extends UniqueHelper>() {3312 return new UniqueRFTCollection(this.collectionId, this.helper.getSudo<T>());3313 }3314}331533163317export class UniqueFTCollection extends UniqueBaseCollection {3318 async getBalance(addressObj: ICrossAccountId) {3319 return await this.helper.ft.getBalance(this.collectionId, addressObj);3320 }33213322 async getTotalPieces() {3323 return await this.helper.ft.getTotalPieces(this.collectionId);3324 }33253326 async getApprovedTokens(fromAddressObj: ICrossAccountId, toAddressObj: ICrossAccountId) {3327 return await this.helper.ft.getApprovedTokens(this.collectionId, fromAddressObj, toAddressObj);3328 }33293330 async getTop10Owners() {3331 return await this.helper.ft.getTop10Owners(this.collectionId);3332 }33333334 async mint(signer: TSigner, amount=1n, owner: ICrossAccountId = {Substrate: signer.address}) {3335 return await this.helper.ft.mintTokens(signer, this.collectionId, amount, owner);3336 }33373338 async mintWithOneOwner(signer: TSigner, tokens: {value: bigint}[], owner: ICrossAccountId = {Substrate: signer.address}) {3339 return await this.helper.ft.mintMultipleTokensWithOneOwner(signer, this.collectionId, tokens, owner);3340 }33413342 async transfer(signer: TSigner, toAddressObj: ICrossAccountId, amount=1n) {3343 return await this.helper.ft.transfer(signer, this.collectionId, toAddressObj, amount);3344 }33453346 async transferFrom(signer: TSigner, fromAddressObj: ICrossAccountId, toAddressObj: ICrossAccountId, amount=1n) {3347 return await this.helper.ft.transferFrom(signer, this.collectionId, fromAddressObj, toAddressObj, amount);3348 }33493350 async burnTokens(signer: TSigner, amount=1n) {3351 return await this.helper.ft.burnTokens(signer, this.collectionId, amount);3352 }33533354 async burnTokensFrom(signer: TSigner, fromAddressObj: ICrossAccountId, amount=1n) {3355 return await this.helper.ft.burnTokensFrom(signer, this.collectionId, fromAddressObj, amount);3356 }33573358 async approveTokens(signer: TSigner, toAddressObj: ICrossAccountId, amount=1n) {3359 return await this.helper.ft.approveTokens(signer, this.collectionId, toAddressObj, amount);3360 }33613362 scheduleAt<T extends UniqueHelper>(3363 executionBlockNumber: number,3364 options: ISchedulerOptions = {},3365 ) {3366 const scheduledHelper = this.helper.scheduler.scheduleAt<T>(executionBlockNumber, options);3367 return new UniqueFTCollection(this.collectionId, scheduledHelper);3368 }33693370 scheduleAfter<T extends UniqueHelper>(3371 blocksBeforeExecution: number,3372 options: ISchedulerOptions = {},3373 ) {3374 const scheduledHelper = this.helper.scheduler.scheduleAfter<T>(blocksBeforeExecution, options);3375 return new UniqueFTCollection(this.collectionId, scheduledHelper);3376 }33773378 getSudo<T extends UniqueHelper>() {3379 return new UniqueFTCollection(this.collectionId, this.helper.getSudo<T>());3380 }3381}338233833384export class UniqueBaseToken {3385 collection: UniqueNFTCollection | UniqueRFTCollection;3386 collectionId: number;3387 tokenId: number;33883389 constructor(tokenId: number, collection: UniqueNFTCollection | UniqueRFTCollection) {3390 this.collection = collection;3391 this.collectionId = collection.collectionId;3392 this.tokenId = tokenId;3393 }33943395 async getNextSponsored(addressObj: ICrossAccountId) {3396 return await this.collection.getTokenNextSponsored(this.tokenId, addressObj);3397 }33983399 async getProperties(propertyKeys?: string[] | null) {3400 return await this.collection.getTokenProperties(this.tokenId, propertyKeys);3401 }34023403 async setProperties(signer: TSigner, properties: IProperty[]) {3404 return await this.collection.setTokenProperties(signer, this.tokenId, properties);3405 }34063407 async deleteProperties(signer: TSigner, propertyKeys: string[]) {3408 return await this.collection.deleteTokenProperties(signer, this.tokenId, propertyKeys);3409 }34103411 async doesExist() {3412 return await this.collection.doesTokenExist(this.tokenId);3413 }34143415 nestingAccount() {3416 return this.collection.helper.util.getTokenAccount(this);3417 }34183419 scheduleAt<T extends UniqueHelper>(3420 executionBlockNumber: number,3421 options: ISchedulerOptions = {},3422 ) {3423 const scheduledCollection = this.collection.scheduleAt<T>(executionBlockNumber, options);3424 return new UniqueBaseToken(this.tokenId, scheduledCollection);3425 }34263427 scheduleAfter<T extends UniqueHelper>(3428 blocksBeforeExecution: number,3429 options: ISchedulerOptions = {},3430 ) {3431 const scheduledCollection = this.collection.scheduleAfter<T>(blocksBeforeExecution, options);3432 return new UniqueBaseToken(this.tokenId, scheduledCollection);3433 }34343435 getSudo<T extends UniqueHelper>() {3436 return new UniqueBaseToken(this.tokenId, this.collection.getSudo<T>());3437 }3438}343934403441export class UniqueNFToken extends UniqueBaseToken {3442 collection: UniqueNFTCollection;34433444 constructor(tokenId: number, collection: UniqueNFTCollection) {3445 super(tokenId, collection);3446 this.collection = collection;3447 }34483449 async getData(blockHashAt?: string) {3450 return await this.collection.getToken(this.tokenId, blockHashAt);3451 }34523453 async getOwner(blockHashAt?: string) {3454 return await this.collection.getTokenOwner(this.tokenId, blockHashAt);3455 }34563457 async getTopmostOwner(blockHashAt?: string) {3458 return await this.collection.getTokenTopmostOwner(this.tokenId, blockHashAt);3459 }34603461 async getChildren(blockHashAt?: string) {3462 return await this.collection.getTokenChildren(this.tokenId, blockHashAt);3463 }34643465 async nest(signer: TSigner, toTokenObj: IToken) {3466 return await this.collection.nestToken(signer, this.tokenId, toTokenObj);3467 }34683469 async unnest(signer: TSigner, fromTokenObj: IToken, toAddressObj: ICrossAccountId) {3470 return await this.collection.unnestToken(signer, this.tokenId, fromTokenObj, toAddressObj);3471 }34723473 async transfer(signer: TSigner, addressObj: ICrossAccountId) {3474 return await this.collection.transferToken(signer, this.tokenId, addressObj);3475 }34763477 async transferFrom(signer: TSigner, fromAddressObj: ICrossAccountId, toAddressObj: ICrossAccountId) {3478 return await this.collection.transferTokenFrom(signer, this.tokenId, fromAddressObj, toAddressObj);3479 }34803481 async approve(signer: TSigner, toAddressObj: ICrossAccountId) {3482 return await this.collection.approveToken(signer, this.tokenId, toAddressObj);3483 }34843485 async isApproved(toAddressObj: ICrossAccountId) {3486 return await this.collection.isTokenApproved(this.tokenId, toAddressObj);3487 }34883489 async burn(signer: TSigner) {3490 return await this.collection.burnToken(signer, this.tokenId);3491 }34923493 async burnFrom(signer: TSigner, fromAddressObj: ICrossAccountId) {3494 return await this.collection.burnTokenFrom(signer, this.tokenId, fromAddressObj);3495 }34963497 scheduleAt<T extends UniqueHelper>(3498 executionBlockNumber: number,3499 options: ISchedulerOptions = {},3500 ) {3501 const scheduledCollection = this.collection.scheduleAt<T>(executionBlockNumber, options);3502 return new UniqueNFToken(this.tokenId, scheduledCollection);3503 }35043505 scheduleAfter<T extends UniqueHelper>(3506 blocksBeforeExecution: number,3507 options: ISchedulerOptions = {},3508 ) {3509 const scheduledCollection = this.collection.scheduleAfter<T>(blocksBeforeExecution, options);3510 return new UniqueNFToken(this.tokenId, scheduledCollection);3511 }35123513 getSudo<T extends UniqueHelper>() {3514 return new UniqueNFToken(this.tokenId, this.collection.getSudo<T>());3515 }3516}35173518export class UniqueRFToken extends UniqueBaseToken {3519 collection: UniqueRFTCollection;35203521 constructor(tokenId: number, collection: UniqueRFTCollection) {3522 super(tokenId, collection);3523 this.collection = collection;3524 }35253526 async getData(blockHashAt?: string) {3527 return await this.collection.getToken(this.tokenId, blockHashAt);3528 }35293530 async getTop10Owners() {3531 return await this.collection.getTop10TokenOwners(this.tokenId);3532 }35333534 async getBalance(addressObj: ICrossAccountId) {3535 return await this.collection.getTokenBalance(this.tokenId, addressObj);3536 }35373538 async getTotalPieces() {3539 return await this.collection.getTokenTotalPieces(this.tokenId);3540 }35413542 async getApprovedPieces(fromAddressObj: ICrossAccountId, toAccountObj: ICrossAccountId) {3543 return await this.collection.getTokenApprovedPieces(this.tokenId, fromAddressObj, toAccountObj);3544 }35453546 async transfer(signer: TSigner, addressObj: ICrossAccountId, amount=1n) {3547 return await this.collection.transferToken(signer, this.tokenId, addressObj, amount);3548 }35493550 async transferFrom(signer: TSigner, fromAddressObj: ICrossAccountId, toAddressObj: ICrossAccountId, amount=1n) {3551 return await this.collection.transferTokenFrom(signer, this.tokenId, fromAddressObj, toAddressObj, amount);3552 }35533554 async approve(signer: TSigner, toAddressObj: ICrossAccountId, amount=1n) {3555 return await this.collection.approveToken(signer, this.tokenId, toAddressObj, amount);3556 }35573558 async repartition(signer: TSigner, amount: bigint) {3559 return await this.collection.repartitionToken(signer, this.tokenId, amount);3560 }35613562 async burn(signer: TSigner, amount=1n) {3563 return await this.collection.burnToken(signer, this.tokenId, amount);3564 }35653566 async burnFrom(signer: TSigner, fromAddressObj: ICrossAccountId, amount=1n) {3567 return await this.collection.burnTokenFrom(signer, this.tokenId, fromAddressObj, amount);3568 }35693570 scheduleAt<T extends UniqueHelper>(3571 executionBlockNumber: number,3572 options: ISchedulerOptions = {},3573 ) {3574 const scheduledCollection = this.collection.scheduleAt<T>(executionBlockNumber, options);3575 return new UniqueRFToken(this.tokenId, scheduledCollection);3576 }35773578 scheduleAfter<T extends UniqueHelper>(3579 blocksBeforeExecution: number,3580 options: ISchedulerOptions = {},3581 ) {3582 const scheduledCollection = this.collection.scheduleAfter<T>(blocksBeforeExecution, options);3583 return new UniqueRFToken(this.tokenId, scheduledCollection);3584 }35853586 getSudo<T extends UniqueHelper>() {3587 return new UniqueRFToken(this.tokenId, this.collection.getSudo<T>());3588 }3589}1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// SPDX-License-Identifier: Apache-2.034/* eslint-disable @typescript-eslint/no-var-requires */5/* eslint-disable function-call-argument-newline */6/* eslint-disable no-prototype-builtins */78import {ApiPromise, WsProvider, Keyring} from '@polkadot/api';9import {ApiInterfaceEvents, SignerOptions} from '@polkadot/api/types';10import {encodeAddress, decodeAddress, keccakAsHex, evmToAddress, addressToEvm, base58Encode, blake2AsU8a} from '@polkadot/util-crypto';11import {IKeyringPair} from '@polkadot/types/types';12import {hexToU8a} from '@polkadot/util/hex';13import {u8aConcat} from '@polkadot/util/u8a';14import {15 IApiListeners,16 IBlock,17 IEvent,18 IChainProperties,19 ICollectionCreationOptions,20 ICollectionLimits,21 ICollectionPermissions,22 ICrossAccountId,23 ICrossAccountIdLower,24 ILogger,25 INestingPermissions,26 IProperty,27 IStakingInfo,28 ISchedulerOptions,29 ISubstrateBalance,30 IToken,31 ITokenPropertyPermission,32 ITransactionResult,33 IUniqueHelperLog,34 TApiAllowedListeners,35 TEthereumAccount,36 TSigner,37 TSubstrateAccount,38 TNetworks,39 IForeignAssetMetadata,40 AcalaAssetMetadata,41 MoonbeamAssetInfo,42 DemocracyStandardAccountVote,43 IEthCrossAccountId,44} from './types';45import {RuntimeDispatchInfo} from '@polkadot/types/interfaces';46import type {Vec} from '@polkadot/types-codec';47import { FrameSystemEventRecord } from '@polkadot/types/lookup';4849export class CrossAccountId implements ICrossAccountId {50 Substrate?: TSubstrateAccount;51 Ethereum?: TEthereumAccount;5253 constructor(account: ICrossAccountId) {54 if (account.Substrate) this.Substrate = account.Substrate;55 if (account.Ethereum) this.Ethereum = account.Ethereum;56 }5758 static fromKeyring(account: IKeyringPair, domain: 'Substrate' | 'Ethereum' = 'Substrate') {59 switch (domain) {60 case 'Substrate': return new CrossAccountId({Substrate: account.address});61 case 'Ethereum': return new CrossAccountId({Substrate: account.address}).toEthereum();62 }63 }6465 static fromLowerCaseKeys(address: ICrossAccountIdLower): CrossAccountId {66 return new CrossAccountId({Substrate: address.substrate, Ethereum: address.ethereum});67 }6869 static normalizeSubstrateAddress(address: TSubstrateAccount, ss58Format = 42): TSubstrateAccount {70 return encodeAddress(decodeAddress(address), ss58Format);71 }7273 static withNormalizedSubstrate(address: TSubstrateAccount, ss58Format = 42): CrossAccountId {74 return new CrossAccountId({Substrate: CrossAccountId.normalizeSubstrateAddress(address, ss58Format)});75 }7677 withNormalizedSubstrate(ss58Format = 42): CrossAccountId {78 if (this.Substrate) return CrossAccountId.withNormalizedSubstrate(this.Substrate, ss58Format);79 return this;80 }8182 static translateSubToEth(address: TSubstrateAccount): TEthereumAccount {83 return nesting.toChecksumAddress('0x' + Array.from(addressToEvm(address), i => i.toString(16).padStart(2, '0')).join(''));84 }8586 toEthereum(): CrossAccountId {87 if (this.Substrate) return new CrossAccountId({Ethereum: CrossAccountId.translateSubToEth(this.Substrate)});88 return this;89 }9091 static translateEthToSub(address: TEthereumAccount, ss58Format?: number): TSubstrateAccount {92 return evmToAddress(address, ss58Format);93 }9495 toSubstrate(ss58Format?: number): CrossAccountId {96 if (this.Ethereum) return new CrossAccountId({Substrate: CrossAccountId.translateEthToSub(this.Ethereum, ss58Format)});97 return this;98 }99100 toLowerCase(): CrossAccountId {101 if (this.Substrate) this.Substrate = this.Substrate.toLowerCase();102 if (this.Ethereum) this.Ethereum = this.Ethereum.toLowerCase();103 return this;104 }105}106107const nesting = {108 toChecksumAddress(address: string): string {109 if (typeof address === 'undefined') return '';110111 if(!/^(0x)?[0-9a-f]{40}$/i.test(address)) throw new Error(`Given address "${address}" is not a valid Ethereum address.`);112113 address = address.toLowerCase().replace(/^0x/i,'');114 const addressHash = keccakAsHex(address).replace(/^0x/i,'');115 const checksumAddress = ['0x'];116117 for (let i = 0; i < address.length; i++) {118 // If ith character is 8 to f then make it uppercase119 if (parseInt(addressHash[i], 16) > 7) {120 checksumAddress.push(address[i].toUpperCase());121 } else {122 checksumAddress.push(address[i]);123 }124 }125 return checksumAddress.join('');126 },127 tokenIdToAddress(collectionId: number, tokenId: number) {128 return this.toChecksumAddress(`0xf8238ccfff8ed887463fd5e0${collectionId.toString(16).padStart(8,'0')}${tokenId.toString(16).padStart(8,'0')}`);129 },130};131132class UniqueUtil {133 static transactionStatus = {134 NOT_READY: 'NotReady',135 FAIL: 'Fail',136 SUCCESS: 'Success',137 };138139 static chainLogType = {140 EXTRINSIC: 'extrinsic',141 RPC: 'rpc',142 };143144 static getTokenAccount(token: IToken): CrossAccountId {145 return new CrossAccountId({Ethereum: this.getTokenAddress(token)});146 }147148 static getTokenAddress(token: IToken): string {149 return nesting.tokenIdToAddress(token.collectionId, token.tokenId);150 }151152 static getDefaultLogger(): ILogger {153 return {154 log(msg: any, level = 'INFO') {155 console[level.toLocaleLowerCase() === 'error' ? 'error' : 'log'](...(Array.isArray(msg) ? msg : [msg]));156 },157 level: {158 ERROR: 'ERROR',159 WARNING: 'WARNING',160 INFO: 'INFO',161 },162 };163 }164165 static vec2str(arr: string[] | number[]) {166 return arr.map(x => String.fromCharCode(parseInt(x.toString()))).join('');167 }168169 static str2vec(string: string) {170 if (typeof string !== 'string') return string;171 return Array.from(string).map(x => x.charCodeAt(0));172 }173174 static fromSeed(seed: string, ss58Format = 42) {175 const keyring = new Keyring({type: 'sr25519', ss58Format});176 return keyring.addFromUri(seed);177 }178179 static extractCollectionIdFromCreationResult(creationResult: ITransactionResult): number {180 if (creationResult.status !== this.transactionStatus.SUCCESS) {181 throw Error('Unable to create collection!');182 }183184 let collectionId = null;185 creationResult.result.events.forEach(({event: {data, method, section}}) => {186 if ((section === 'common') && (method === 'CollectionCreated')) {187 collectionId = parseInt(data[0].toString(), 10);188 }189 });190191 if (collectionId === null) {192 throw Error('No CollectionCreated event was found!');193 }194195 return collectionId;196 }197198 static extractTokensFromCreationResult(creationResult: ITransactionResult): {199 success: boolean,200 tokens: {collectionId: number, tokenId: number, owner: CrossAccountId, amount: bigint}[],201 } {202 if (creationResult.status !== this.transactionStatus.SUCCESS) {203 throw Error('Unable to create tokens!');204 }205 let success = false;206 const tokens = [] as {collectionId: number, tokenId: number, owner: CrossAccountId, amount: bigint}[];207 creationResult.result.events.forEach(({event: {data, method, section}}) => {208 if (method === 'ExtrinsicSuccess') {209 success = true;210 } else if ((section === 'common') && (method === 'ItemCreated')) {211 tokens.push({212 collectionId: parseInt(data[0].toString(), 10),213 tokenId: parseInt(data[1].toString(), 10),214 owner: data[2].toHuman(),215 amount: data[3].toBigInt(),216 });217 }218 });219 return {success, tokens};220 }221222 static extractTokensFromBurnResult(burnResult: ITransactionResult): {223 success: boolean,224 tokens: {collectionId: number, tokenId: number, owner: CrossAccountId, amount: bigint}[],225 } {226 if (burnResult.status !== this.transactionStatus.SUCCESS) {227 throw Error('Unable to burn tokens!');228 }229 let success = false;230 const tokens = [] as {collectionId: number, tokenId: number, owner: CrossAccountId, amount: bigint}[];231 burnResult.result.events.forEach(({event: {data, method, section}}) => {232 if (method === 'ExtrinsicSuccess') {233 success = true;234 } else if ((section === 'common') && (method === 'ItemDestroyed')) {235 tokens.push({236 collectionId: parseInt(data[0].toString(), 10),237 tokenId: parseInt(data[1].toString(), 10),238 owner: data[2].toHuman(),239 amount: data[3].toBigInt(),240 });241 }242 });243 return {success, tokens};244 }245246 static findCollectionInEvents(events: {event: IEvent}[], collectionId: number, expectedSection: string, expectedMethod: string): boolean {247 let eventId = null;248 events.forEach(({event: {data, method, section}}) => {249 if ((section === expectedSection) && (method === expectedMethod)) {250 eventId = parseInt(data[0].toString(), 10);251 }252 });253254 if (eventId === null) {255 throw Error(`No ${expectedMethod} event was found!`);256 }257 return eventId === collectionId;258 }259260 static isTokenTransferSuccess(events: {event: IEvent}[], collectionId: number, tokenId: number, fromAddressObj: ICrossAccountId, toAddressObj: ICrossAccountId, amount=1n) {261 const normalizeAddress = (address: string | ICrossAccountId) => {262 if(typeof address === 'string') return address;263 const obj = {} as any;264 Object.keys(address).forEach(k => {265 obj[k.toLocaleLowerCase()] = address[k as 'Substrate' | 'Ethereum'];266 });267 if(obj.substrate) return CrossAccountId.withNormalizedSubstrate(obj.substrate);268 if(obj.ethereum) return CrossAccountId.fromLowerCaseKeys(obj).toLowerCase();269 return address;270 };271 let transfer = {collectionId: null, tokenId: null, from: null, to: null, amount: 1} as any;272 events.forEach(({event: {data, method, section}}) => {273 if ((section === 'common') && (method === 'Transfer')) {274 const hData = (data as any).toJSON();275 transfer = {276 collectionId: hData[0],277 tokenId: hData[1],278 from: normalizeAddress(hData[2]),279 to: normalizeAddress(hData[3]),280 amount: BigInt(hData[4]),281 };282 }283 });284 let isSuccess = parseInt(collectionId.toString()) === transfer.collectionId && parseInt(tokenId.toString()) === transfer.tokenId;285 isSuccess = isSuccess && JSON.stringify(normalizeAddress(fromAddressObj)) === JSON.stringify(transfer.from);286 isSuccess = isSuccess && JSON.stringify(normalizeAddress(toAddressObj)) === JSON.stringify(transfer.to);287 isSuccess = isSuccess && amount === transfer.amount;288 return isSuccess;289 }290291 static bigIntToDecimals(number: bigint, decimals = 18) {292 const numberStr = number.toString();293 const dotPos = numberStr.length - decimals;294295 if (dotPos <= 0) {296 return '0.' + '0'.repeat(Math.abs(dotPos)) + numberStr;297 } else {298 const intPart = numberStr.substring(0, dotPos);299 const fractPart = numberStr.substring(dotPos);300 return intPart + '.' + fractPart;301 }302 }303}304305class UniqueEventHelper {306 private static extractIndex(index: any): [number, number] | string {307 if(index.toRawType() === '[u8;2]') return [index[0], index[1]];308 return index.toJSON();309 }310311 private static extractSub(data: any, subTypes: any): {[key: string]: any} {312 let obj: any = {};313 let index = 0;314315 if (data.entries) {316 for(const [key, value] of data.entries()) {317 obj[key] = this.extractData(value, subTypes[index]);318 index++;319 }320 } else obj = data.toJSON();321322 return obj;323 }324325 private static extractData(data: any, type: any): any {326 if(!type) return data.toHuman();327 if (['u16', 'u32'].indexOf(type.type) > -1) return data.toNumber();328 if (['u64', 'u128', 'u256'].indexOf(type.type) > -1) return data.toBigInt();329 if(type.hasOwnProperty('sub')) return this.extractSub(data, type.sub);330 return data.toHuman();331 }332333 public static extractEvents(events: {event: any, phase: any}[]): IEvent[] {334 const parsedEvents: IEvent[] = [];335336 events.forEach((record) => {337 const {event, phase} = record;338 const types = event.typeDef;339340 const eventData: IEvent = {341 section: event.section.toString(),342 method: event.method.toString(),343 index: this.extractIndex(event.index),344 data: [],345 phase: phase.toJSON(),346 };347348 event.data.forEach((val: any, index: number) => {349 eventData.data.push(this.extractData(val, types[index]));350 });351352 parsedEvents.push(eventData);353 });354355 return parsedEvents;356 }357}358359export class ChainHelperBase {360 helperBase: any;361362 transactionStatus = UniqueUtil.transactionStatus;363 chainLogType = UniqueUtil.chainLogType;364 util: typeof UniqueUtil;365 eventHelper: typeof UniqueEventHelper;366 logger: ILogger;367 api: ApiPromise | null;368 forcedNetwork: TNetworks | null;369 network: TNetworks | null;370 chainLog: IUniqueHelperLog[];371 children: ChainHelperBase[];372 address: AddressGroup;373 chain: ChainGroup;374375 constructor(logger?: ILogger, helperBase?: any) {376 this.helperBase = helperBase;377378 this.util = UniqueUtil;379 this.eventHelper = UniqueEventHelper;380 if (typeof logger == 'undefined') logger = this.util.getDefaultLogger();381 this.logger = logger;382 this.api = null;383 this.forcedNetwork = null;384 this.network = null;385 this.chainLog = [];386 this.children = [];387 this.address = new AddressGroup(this);388 this.chain = new ChainGroup(this);389 }390391 clone(helperCls: ChainHelperBaseConstructor, options: {[key: string]: any} = {}) {392 Object.setPrototypeOf(helperCls.prototype, this);393 const newHelper = new helperCls(this.logger, options);394395 newHelper.api = this.api;396 newHelper.network = this.network;397 newHelper.forceNetwork = this.forceNetwork;398399 this.children.push(newHelper);400401 return newHelper;402 }403404 getApi(): ApiPromise {405 if(this.api === null) throw Error('API not initialized');406 return this.api;407 }408409 async subscribeEvents(expectedEvents: {section: string, names: string[]}[]) {410 const collectedEvents: IEvent[] = [];411 const unsubscribe = await this.getApi().query.system.events((events: Vec<FrameSystemEventRecord>) => {412 const ievents = this.eventHelper.extractEvents(events);413 ievents.forEach((event) => {414 expectedEvents.forEach((e => {415 if (event.section === e.section && e.names.includes(event.method)) {416 collectedEvents.push(event);417 }418 }))419 });420 });421 return {unsubscribe: unsubscribe as any, collectedEvents};422}423424 clearChainLog(): void {425 this.chainLog = [];426 }427428 forceNetwork(value: TNetworks): void {429 this.forcedNetwork = value;430 }431432 async connect(wsEndpoint: string, listeners?: IApiListeners) {433 if (this.api !== null) throw Error('Already connected');434 const {api, network} = await ChainHelperBase.createConnection(wsEndpoint, listeners, this.forcedNetwork);435 this.api = api;436 this.network = network;437 }438439 async disconnect() {440 for (const child of this.children) {441 child.clearApi();442 }443444 if (this.api === null) return;445 await this.api.disconnect();446 this.clearApi();447 }448449 clearApi() {450 this.api = null;451 this.network = null;452 }453454 static async detectNetwork(api: ApiPromise): Promise<TNetworks> {455 const spec = (await api.query.system.lastRuntimeUpgrade()).toJSON() as any;456 const xcmChains = ['rococo', 'westend', 'westmint', 'acala', 'karura', 'moonbeam', 'moonriver'];457458 if(xcmChains.indexOf(spec.specName) > -1) return spec.specName;459460 if(['quartz', 'unique'].indexOf(spec.specName) > -1) return spec.specName;461 return 'opal';462 }463464 static async detectNetworkByWsEndpoint(wsEndpoint: string): Promise<TNetworks> {465 const api = new ApiPromise({provider: new WsProvider(wsEndpoint)});466 await api.isReady;467468 const network = await this.detectNetwork(api);469470 await api.disconnect();471472 return network;473 }474475 static async createConnection(wsEndpoint: string, listeners?: IApiListeners, network?: TNetworks | null): Promise<{476 api: ApiPromise;477 network: TNetworks;478 }> {479 if(typeof network === 'undefined' || network === null) network = 'opal';480 const supportedRPC = {481 opal: {482 unique: require('@unique-nft/opal-testnet-types/definitions').unique.rpc,483 },484 quartz: {485 unique: require('@unique-nft/quartz-mainnet-types/definitions').unique.rpc,486 },487 unique: {488 unique: require('@unique-nft/unique-mainnet-types/definitions').unique.rpc,489 },490 rococo: {},491 westend: {},492 moonbeam: {},493 moonriver: {},494 acala: {},495 karura: {},496 westmint: {},497 };498 if(!supportedRPC.hasOwnProperty(network)) network = await this.detectNetworkByWsEndpoint(wsEndpoint);499 const rpc = supportedRPC[network];500501 // TODO: investigate how to replace rpc in runtime502 // api._rpcCore.addUserInterfaces(rpc);503504 const api = new ApiPromise({provider: new WsProvider(wsEndpoint), rpc});505506 await api.isReadyOrError;507508 if (typeof listeners === 'undefined') listeners = {};509 for (const event of ['connected', 'disconnected', 'error', 'ready', 'decorated']) {510 if (!listeners.hasOwnProperty(event) || typeof listeners[event as TApiAllowedListeners] === 'undefined') continue;511 api.on(event as ApiInterfaceEvents, listeners[event as TApiAllowedListeners] as (...args: any[]) => any);512 }513514 return {api, network};515 }516517 getTransactionStatus(data: {events: {event: IEvent}[], status: any}) {518 const {events, status} = data;519 if (status.isReady) {520 return this.transactionStatus.NOT_READY;521 }522 if (status.isBroadcast) {523 return this.transactionStatus.NOT_READY;524 }525 if (status.isInBlock || status.isFinalized) {526 const errors = events.filter(e => e.event.method === 'ExtrinsicFailed');527 if (errors.length > 0) {528 return this.transactionStatus.FAIL;529 }530 if (events.filter(e => e.event.method === 'ExtrinsicSuccess').length > 0) {531 return this.transactionStatus.SUCCESS;532 }533 }534535 return this.transactionStatus.FAIL;536 }537538 signTransaction(sender: TSigner, transaction: any, options: Partial<SignerOptions> | null = null, label = 'transaction') {539 const sign = (callback: any) => {540 if(options !== null) return transaction.signAndSend(sender, options, callback);541 return transaction.signAndSend(sender, callback);542 };543 // eslint-disable-next-line no-async-promise-executor544 return new Promise(async (resolve, reject) => {545 try {546 const unsub = await sign((result: any) => {547 const status = this.getTransactionStatus(result);548549 if (status === this.transactionStatus.SUCCESS) {550 this.logger.log(`${label} successful`);551 unsub();552 resolve({result, status});553 } else if (status === this.transactionStatus.FAIL) {554 let moduleError = null;555556 if (result.hasOwnProperty('dispatchError')) {557 const dispatchError = result['dispatchError'];558559 if (dispatchError) {560 if (dispatchError.isModule) {561 const modErr = dispatchError.asModule;562 const errorMeta = dispatchError.registry.findMetaError(modErr);563564 moduleError = `${errorMeta.section}.${errorMeta.name}`;565 } else {566 moduleError = dispatchError.toHuman();567 }568 } else {569 this.logger.log(result, this.logger.level.ERROR);570 }571 }572573 this.logger.log(`Something went wrong with ${label}. Status: ${status}`, this.logger.level.ERROR);574 unsub();575 reject({status, moduleError, result});576 }577 });578 } catch (e) {579 this.logger.log(e, this.logger.level.ERROR);580 reject(e);581 }582 });583 }584585 async getPaymentInfo(signer: TSigner, tx: any, len: number | null) {586 const api = this.getApi();587 const signingInfo = await api.derive.tx.signingInfo(signer.address);588589 // We need to sign the tx because590 // unsigned transactions does not have an inclusion fee591 tx.sign(signer, {592 blockHash: api.genesisHash,593 genesisHash: api.genesisHash,594 runtimeVersion: api.runtimeVersion,595 nonce: signingInfo.nonce,596 });597598 if (len === null) {599 return (await this.callRpc('api.rpc.payment.queryInfo', [tx.toHex()])) as RuntimeDispatchInfo;600 } else {601 return (await api.call.transactionPaymentApi.queryInfo(tx, len)) as RuntimeDispatchInfo;602 }603 }604605 constructApiCall(apiCall: string, params: any[]) {606 if(!apiCall.startsWith('api.')) throw Error(`Invalid api call: ${apiCall}`);607 let call = this.getApi() as any;608 for(const part of apiCall.slice(4).split('.')) {609 call = call[part];610 }611 return call(...params);612 }613614 async executeExtrinsic(sender: TSigner, extrinsic: string, params: any[], expectSuccess=true, options: Partial<SignerOptions>|null = null/*, failureMessage='expected success'*/) {615 if(this.api === null) throw Error('API not initialized');616 if(!extrinsic.startsWith('api.tx.')) throw Error(`${extrinsic} is not transaction`);617618 const startTime = (new Date()).getTime();619 let result: ITransactionResult;620 let events: IEvent[] = [];621 try {622 result = await this.signTransaction(sender, this.constructApiCall(extrinsic, params), options, extrinsic) as ITransactionResult;623 events = this.eventHelper.extractEvents(result.result.events);624 }625 catch(e) {626 if(!(e as object).hasOwnProperty('status')) throw e;627 result = e as ITransactionResult;628 }629630 const endTime = (new Date()).getTime();631632 const log = {633 executedAt: endTime,634 executionTime: endTime - startTime,635 type: this.chainLogType.EXTRINSIC,636 status: result.status,637 call: extrinsic,638 signer: this.getSignerAddress(sender),639 params,640 } as IUniqueHelperLog;641642 if(result.status !== this.transactionStatus.SUCCESS) {643 if (result.moduleError) log.moduleError = result.moduleError;644 else if (result.result.dispatchError) log.dispatchError = result.result.dispatchError;645 }646 if(events.length > 0) log.events = events;647648 this.chainLog.push(log);649650 if(expectSuccess && result.status !== this.transactionStatus.SUCCESS) {651 if (result.moduleError) throw Error(`${result.moduleError}`);652 else if (result.result.dispatchError) throw Error(JSON.stringify(result.result.dispatchError));653 }654 return result;655 }656657 async callRpc(rpc: string, params?: any[]) {658 if(typeof params === 'undefined') params = [];659 if(this.api === null) throw Error('API not initialized');660 if(!rpc.startsWith('api.rpc.') && !rpc.startsWith('api.query.')) throw Error(`${rpc} is not RPC call`);661662 const startTime = (new Date()).getTime();663 let result;664 let error = null;665 const log = {666 type: this.chainLogType.RPC,667 call: rpc,668 params,669 } as IUniqueHelperLog;670671 try {672 result = await this.constructApiCall(rpc, params);673 }674 catch(e) {675 error = e;676 }677678 const endTime = (new Date()).getTime();679680 log.executedAt = endTime;681 log.status = (error === null ? this.transactionStatus.SUCCESS : this.transactionStatus.FAIL) as 'Fail' | 'Success';682 log.executionTime = endTime - startTime;683684 this.chainLog.push(log);685686 if(error !== null) throw error;687688 return result;689 }690691 getSignerAddress(signer: IKeyringPair | string): string {692 if(typeof signer === 'string') return signer;693 return signer.address;694 }695696 fetchAllPalletNames(): string[] {697 if(this.api === null) throw Error('API not initialized');698 return this.api.runtimeMetadata.asLatest.pallets.map(m => m.name.toString().toLowerCase());699 }700701 fetchMissingPalletNames(requiredPallets: string[]): string[] {702 const palletNames = this.fetchAllPalletNames();703 return requiredPallets.filter(p => !palletNames.includes(p));704 }705}706707708class HelperGroup<T extends ChainHelperBase> {709 helper: T;710711 constructor(uniqueHelper: T) {712 this.helper = uniqueHelper;713 }714}715716717class CollectionGroup extends HelperGroup<UniqueHelper> {718 /**719 * Get number of blocks when sponsored transaction is available.720 *721 * @param collectionId ID of collection722 * @param tokenId ID of token723 * @param addressObj address for which the sponsorship is checked724 * @example await getTokenNextSponsored(1, 2, {Substrate: '5DfhbVfww7ThF8q6f3...'});725 * @returns number of blocks or null if sponsorship hasn't been set726 */727 async getTokenNextSponsored(collectionId: number, tokenId: number, addressObj: ICrossAccountId): Promise<number | null> {728 return (await this.helper.callRpc('api.rpc.unique.nextSponsored', [collectionId, addressObj, tokenId])).toJSON();729 }730731 /**732 * Get the number of created collections.733 *734 * @returns number of created collections735 */736 async getTotalCount(): Promise<number> {737 return (await this.helper.callRpc('api.rpc.unique.collectionStats')).created.toNumber();738 }739740 /**741 * Get information about the collection with additional data,742 * including the number of tokens it contains, its administrators,743 * the normalized address of the collection's owner, and decoded name and description.744 *745 * @param collectionId ID of collection746 * @example await getData(2)747 * @returns collection information object748 */749 async getData(collectionId: number): Promise<{750 id: number;751 name: string;752 description: string;753 tokensCount: number;754 admins: CrossAccountId[];755 normalizedOwner: TSubstrateAccount;756 raw: any757 } | null> {758 const collection = await this.helper.callRpc('api.rpc.unique.collectionById', [collectionId]);759 const humanCollection = collection.toHuman(), collectionData = {760 id: collectionId, name: null, description: null, tokensCount: 0, admins: [],761 raw: humanCollection,762 } as any, jsonCollection = collection.toJSON();763 if (humanCollection === null) return null;764 collectionData.raw.limits = jsonCollection.limits;765 collectionData.raw.permissions = jsonCollection.permissions;766 collectionData.normalizedOwner = this.helper.address.normalizeSubstrate(collectionData.raw.owner);767 for (const key of ['name', 'description']) {768 collectionData[key] = this.helper.util.vec2str(humanCollection[key]);769 }770771 collectionData.tokensCount = (['RFT', 'NFT'].includes(humanCollection.mode))772 ? await this.helper[humanCollection.mode.toLocaleLowerCase() as 'nft' | 'rft'].getLastTokenId(collectionId)773 : 0;774 collectionData.admins = await this.getAdmins(collectionId);775776 return collectionData;777 }778779 /**780 * Get the addresses of the collection's administrators, optionally normalized.781 *782 * @param collectionId ID of collection783 * @param normalize whether to normalize the addresses to the default ss58 format784 * @example await getAdmins(1)785 * @returns array of administrators786 */787 async getAdmins(collectionId: number, normalize = false): Promise<CrossAccountId[]> {788 const admins = (await this.helper.callRpc('api.rpc.unique.adminlist', [collectionId])).toHuman();789790 return normalize791 ? admins.map((address: CrossAccountId) => address.withNormalizedSubstrate())792 : admins;793 }794795 /**796 * Get the addresses added to the collection allow-list, optionally normalized.797 * @param collectionId ID of collection798 * @param normalize whether to normalize the addresses to the default ss58 format799 * @example await getAllowList(1)800 * @returns array of allow-listed addresses801 */802 async getAllowList(collectionId: number, normalize = false): Promise<CrossAccountId[]> {803 const allowListed = (await this.helper.callRpc('api.rpc.unique.allowlist', [collectionId])).toHuman();804 return normalize805 ? allowListed.map((address: CrossAccountId) => address.withNormalizedSubstrate())806 : allowListed;807 }808809 /**810 * Get the effective limits of the collection instead of null for default values811 *812 * @param collectionId ID of collection813 * @example await getEffectiveLimits(2)814 * @returns object of collection limits815 */816 async getEffectiveLimits(collectionId: number): Promise<ICollectionLimits> {817 return (await this.helper.callRpc('api.rpc.unique.effectiveCollectionLimits', [collectionId])).toJSON();818 }819820 /**821 * Burns the collection if the signer has sufficient permissions and collection is empty.822 *823 * @param signer keyring of signer824 * @param collectionId ID of collection825 * @example await helper.collection.burn(aliceKeyring, 3);826 * @returns ```true``` if extrinsic success, otherwise ```false```827 */828 async burn(signer: TSigner, collectionId: number): Promise<boolean> {829 const result = await this.helper.executeExtrinsic(830 signer,831 'api.tx.unique.destroyCollection', [collectionId],832 true,833 );834835 return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'common', 'CollectionDestroyed');836 }837838 /**839 * Sets the sponsor for the collection (Requires the Substrate address). Needs confirmation by the sponsor.840 *841 * @param signer keyring of signer842 * @param collectionId ID of collection843 * @param sponsorAddress Sponsor substrate address844 * @example setSponsor(aliceKeyring, 10, "5DyN4Y92vZCjv38fg...")845 * @returns ```true``` if extrinsic success, otherwise ```false```846 */847 async setSponsor(signer: TSigner, collectionId: number, sponsorAddress: TSubstrateAccount): Promise<boolean> {848 const result = await this.helper.executeExtrinsic(849 signer,850 'api.tx.unique.setCollectionSponsor', [collectionId, sponsorAddress],851 true,852 );853854 return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'common', 'CollectionSponsorSet');855 }856857 /**858 * Confirms consent to sponsor the collection on behalf of the signer.859 *860 * @param signer keyring of signer861 * @param collectionId ID of collection862 * @example confirmSponsorship(aliceKeyring, 10)863 * @returns ```true``` if extrinsic success, otherwise ```false```864 */865 async confirmSponsorship(signer: TSigner, collectionId: number): Promise<boolean> {866 const result = await this.helper.executeExtrinsic(867 signer,868 'api.tx.unique.confirmSponsorship', [collectionId],869 true,870 );871872 return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'common', 'SponsorshipConfirmed');873 }874875 /**876 * Removes the sponsor of a collection, regardless if it consented or not.877 *878 * @param signer keyring of signer879 * @param collectionId ID of collection880 * @example removeSponsor(aliceKeyring, 10)881 * @returns ```true``` if extrinsic success, otherwise ```false```882 */883 async removeSponsor(signer: TSigner, collectionId: number): Promise<boolean> {884 const result = await this.helper.executeExtrinsic(885 signer,886 'api.tx.unique.removeCollectionSponsor', [collectionId],887 true,888 );889890 return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'common', 'CollectionSponsorRemoved');891 }892893 /**894 * Sets the limits of the collection. At least one limit must be specified for a correct call.895 *896 * @param signer keyring of signer897 * @param collectionId ID of collection898 * @param limits collection limits object899 * @example900 * await setLimits(901 * aliceKeyring,902 * 10,903 * {904 * sponsorTransferTimeout: 0,905 * ownerCanDestroy: false906 * }907 * )908 * @returns ```true``` if extrinsic success, otherwise ```false```909 */910 async setLimits(signer: TSigner, collectionId: number, limits: ICollectionLimits): Promise<boolean> {911 const result = await this.helper.executeExtrinsic(912 signer,913 'api.tx.unique.setCollectionLimits', [collectionId, limits],914 true,915 );916917 return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'common', 'CollectionLimitSet');918 }919920 /**921 * Changes the owner of the collection to the new Substrate address.922 *923 * @param signer keyring of signer924 * @param collectionId ID of collection925 * @param ownerAddress substrate address of new owner926 * @example changeOwner(aliceKeyring, 10, "5DyN4Y92vZCjv38fg...")927 * @returns ```true``` if extrinsic success, otherwise ```false```928 */929 async changeOwner(signer: TSigner, collectionId: number, ownerAddress: TSubstrateAccount): Promise<boolean> {930 const result = await this.helper.executeExtrinsic(931 signer,932 'api.tx.unique.changeCollectionOwner', [collectionId, ownerAddress],933 true,934 );935936 return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'common', 'CollectionOwnedChanged');937 }938939 /**940 * Adds a collection administrator.941 *942 * @param signer keyring of signer943 * @param collectionId ID of collection944 * @param adminAddressObj Administrator address (substrate or ethereum)945 * @example addAdmin(aliceKeyring, 10, {Substrate: "5DyN4Y92vZCjv38fg..."})946 * @returns ```true``` if extrinsic success, otherwise ```false```947 */948 async addAdmin(signer: TSigner, collectionId: number, adminAddressObj: ICrossAccountId): Promise<boolean> {949 const result = await this.helper.executeExtrinsic(950 signer,951 'api.tx.unique.addCollectionAdmin', [collectionId, adminAddressObj],952 true,953 );954955 return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'common', 'CollectionAdminAdded');956 }957958 /**959 * Removes a collection administrator.960 *961 * @param signer keyring of signer962 * @param collectionId ID of collection963 * @param adminAddressObj Administrator address (substrate or ethereum)964 * @example removeAdmin(aliceKeyring, 10, {Substrate: "5DyN4Y92vZCjv38fg..."})965 * @returns ```true``` if extrinsic success, otherwise ```false```966 */967 async removeAdmin(signer: TSigner, collectionId: number, adminAddressObj: ICrossAccountId): Promise<boolean> {968 const result = await this.helper.executeExtrinsic(969 signer,970 'api.tx.unique.removeCollectionAdmin', [collectionId, adminAddressObj],971 true,972 );973974 return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'common', 'CollectionAdminRemoved');975 }976977 /**978 * Check if user is in allow list.979 *980 * @param collectionId ID of collection981 * @param user Account to check982 * @example await getAdmins(1)983 * @returns is user in allow list984 */985 async allowed(collectionId: number, user: ICrossAccountId): Promise<boolean> {986 return (await this.helper.callRpc('api.rpc.unique.allowed', [collectionId, user])).toJSON();987 }988989 /**990 * Adds an address to allow list991 * @param signer keyring of signer992 * @param collectionId ID of collection993 * @param addressObj address to add to the allow list994 * @returns ```true``` if extrinsic success, otherwise ```false```995 */996 async addToAllowList(signer: TSigner, collectionId: number, addressObj: ICrossAccountId): Promise<boolean> {997 const result = await this.helper.executeExtrinsic(998 signer,999 'api.tx.unique.addToAllowList', [collectionId, addressObj],1000 true,1001 );10021003 return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'common', 'AllowListAddressAdded');1004 }10051006 /**1007 * Removes an address from allow list1008 *1009 * @param signer keyring of signer1010 * @param collectionId ID of collection1011 * @param addressObj address to remove from the allow list1012 * @returns ```true``` if extrinsic success, otherwise ```false```1013 */1014 async removeFromAllowList(signer: TSigner, collectionId: number, addressObj: ICrossAccountId): Promise<boolean> {1015 const result = await this.helper.executeExtrinsic(1016 signer,1017 'api.tx.unique.removeFromAllowList', [collectionId, addressObj],1018 true,1019 );10201021 return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'common', 'AllowListAddressRemoved');1022 }10231024 /**1025 * Sets onchain permissions for selected collection.1026 *1027 * @param signer keyring of signer1028 * @param collectionId ID of collection1029 * @param permissions collection permissions object1030 * @example setPermissions(aliceKeyring, 10, {access:'AllowList', mintMode: true, nesting: {collectionAdmin: true, tokenOwner: true}});1031 * @returns ```true``` if extrinsic success, otherwise ```false```1032 */1033 async setPermissions(signer: TSigner, collectionId: number, permissions: ICollectionPermissions): Promise<boolean> {1034 const result = await this.helper.executeExtrinsic(1035 signer,1036 'api.tx.unique.setCollectionPermissions', [collectionId, permissions],1037 true,1038 );10391040 return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'common', 'CollectionPermissionSet');1041 }10421043 /**1044 * Enables nesting for selected collection. If `restricted` set, you can nest only tokens from specified collections.1045 *1046 * @param signer keyring of signer1047 * @param collectionId ID of collection1048 * @param permissions nesting permissions object1049 * @example enableNesting(aliceKeyring, 10, {collectionAdmin: true, tokenOwner: true});1050 * @returns ```true``` if extrinsic success, otherwise ```false```1051 */1052 async enableNesting(signer: TSigner, collectionId: number, permissions: INestingPermissions): Promise<boolean> {1053 return await this.setPermissions(signer, collectionId, {nesting: permissions});1054 }10551056 /**1057 * Disables nesting for selected collection.1058 *1059 * @param signer keyring of signer1060 * @param collectionId ID of collection1061 * @example disableNesting(aliceKeyring, 10);1062 * @returns ```true``` if extrinsic success, otherwise ```false```1063 */1064 async disableNesting(signer: TSigner, collectionId: number): Promise<boolean> {1065 return await this.setPermissions(signer, collectionId, {nesting: {tokenOwner: false, collectionAdmin: false}});1066 }10671068 /**1069 * Sets onchain properties to the collection.1070 *1071 * @param signer keyring of signer1072 * @param collectionId ID of collection1073 * @param properties array of property objects1074 * @example setProperties(aliceKeyring, 10, [{key: "gender", value: "male"}]);1075 * @returns ```true``` if extrinsic success, otherwise ```false```1076 */1077 async setProperties(signer: TSigner, collectionId: number, properties: IProperty[]): Promise<boolean> {1078 const result = await this.helper.executeExtrinsic(1079 signer,1080 'api.tx.unique.setCollectionProperties', [collectionId, properties],1081 true,1082 );10831084 return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'common', 'CollectionPropertySet');1085 }10861087 /**1088 * Get collection properties.1089 *1090 * @param collectionId ID of collection1091 * @param propertyKeys optionally filter the returned properties to only these keys1092 * @example getProperties(1219, ['location', 'date', 'time', 'isParadise']);1093 * @returns array of key-value pairs1094 */1095 async getProperties(collectionId: number, propertyKeys?: string[] | null): Promise<IProperty[]> {1096 return (await this.helper.callRpc('api.rpc.unique.collectionProperties', [collectionId, propertyKeys])).toHuman();1097 }10981099 async getCollectionOptions(collectionId: number) {1100 return (await this.helper.callRpc('api.rpc.unique.collectionById', [collectionId])).toHuman();1101 }11021103 /**1104 * Deletes onchain properties from the collection.1105 *1106 * @param signer keyring of signer1107 * @param collectionId ID of collection1108 * @param propertyKeys array of property keys to delete1109 * @example deleteProperties(aliceKeyring, 10, ["gender", "age"]);1110 * @returns ```true``` if extrinsic success, otherwise ```false```1111 */1112 async deleteProperties(signer: TSigner, collectionId: number, propertyKeys: string[]): Promise<boolean> {1113 const result = await this.helper.executeExtrinsic(1114 signer,1115 'api.tx.unique.deleteCollectionProperties', [collectionId, propertyKeys],1116 true,1117 );11181119 return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'common', 'CollectionPropertyDeleted');1120 }11211122 /**1123 * Changes the owner of the token.1124 *1125 * @param signer keyring of signer1126 * @param collectionId ID of collection1127 * @param tokenId ID of token1128 * @param addressObj address of a new owner1129 * @param amount amount of tokens to be transfered. For NFT must be set to 1n1130 * @example transferToken(aliceKeyring, 10, 5, {Substrate: "5DyN4Y92vZCjv38fg..."})1131 * @returns true if the token success, otherwise false1132 */1133 async transferToken(signer: TSigner, collectionId: number, tokenId: number, addressObj: ICrossAccountId, amount=1n): Promise<boolean> {1134 const result = await this.helper.executeExtrinsic(1135 signer,1136 'api.tx.unique.transfer', [addressObj, collectionId, tokenId, amount],1137 true, // `Unable to transfer token #${tokenId} from collection #${collectionId}`,1138 );11391140 return this.helper.util.isTokenTransferSuccess(result.result.events, collectionId, tokenId, {Substrate: typeof signer === 'string' ? signer : signer.address}, addressObj, amount);1141 }11421143 /**1144 *1145 * Change ownership of a token(s) on behalf of the owner.1146 *1147 * @param signer keyring of signer1148 * @param collectionId ID of collection1149 * @param tokenId ID of token1150 * @param fromAddressObj address on behalf of which the token will be sent1151 * @param toAddressObj new token owner1152 * @param amount amount of tokens to be transfered. For NFT must be set to 1n1153 * @example transferTokenFrom(aliceKeyring, 10, 5, {Substrate: "5DyN4Y92vZCjv38fg"}, {Ethereum: "0x9F0583DbB85..."})1154 * @returns true if the token success, otherwise false1155 */1156 async transferTokenFrom(signer: TSigner, collectionId: number, tokenId: number, fromAddressObj: ICrossAccountId, toAddressObj: ICrossAccountId, amount=1n): Promise<boolean> {1157 const result = await this.helper.executeExtrinsic(1158 signer,1159 'api.tx.unique.transferFrom', [fromAddressObj, toAddressObj, collectionId, tokenId, amount],1160 true, // `Unable to transfer token #${tokenId} from collection #${collectionId}`,1161 );1162 return this.helper.util.isTokenTransferSuccess(result.result.events, collectionId, tokenId, fromAddressObj, toAddressObj, amount);1163 }11641165 /**1166 *1167 * Destroys a concrete instance of NFT/RFT or burns a specified amount of fungible tokens.1168 *1169 * @param signer keyring of signer1170 * @param collectionId ID of collection1171 * @param tokenId ID of token1172 * @param amount amount of tokens to be burned. For NFT must be set to 1n1173 * @example burnToken(aliceKeyring, 10, 5);1174 * @returns ```true``` if the extrinsic is successful, otherwise ```false```1175 */1176 async burnToken(signer: TSigner, collectionId: number, tokenId: number, amount=1n): Promise<boolean> {1177 const burnResult = await this.helper.executeExtrinsic(1178 signer,1179 'api.tx.unique.burnItem', [collectionId, tokenId, amount],1180 true, // `Unable to burn token for ${label}`,1181 );1182 const burnedTokens = this.helper.util.extractTokensFromBurnResult(burnResult);1183 if (burnedTokens.tokens.length > 1) throw Error('Burned multiple tokens');1184 return burnedTokens.success;1185 }11861187 /**1188 * Destroys a concrete instance of NFT on behalf of the owner1189 *1190 * @param signer keyring of signer1191 * @param collectionId ID of collection1192 * @param tokenId ID of token1193 * @param fromAddressObj address on behalf of which the token will be burnt1194 * @param amount amount of tokens to be burned. For NFT must be set to 1n1195 * @example burnTokenFrom(aliceKeyring, 10, {Substrate: "5DyN4Y92vZCjv38fg..."}, 5, {Ethereum: "0x9F0583DbB85..."})1196 * @returns ```true``` if extrinsic success, otherwise ```false```1197 */1198 async burnTokenFrom(signer: TSigner, collectionId: number, tokenId: number, fromAddressObj: ICrossAccountId, amount=1n): Promise<boolean> {1199 const burnResult = await this.helper.executeExtrinsic(1200 signer,1201 'api.tx.unique.burnFrom', [collectionId, fromAddressObj, tokenId, amount],1202 true, // `Unable to burn token from for ${label}`,1203 );1204 const burnedTokens = this.helper.util.extractTokensFromBurnResult(burnResult);1205 return burnedTokens.success && burnedTokens.tokens.length > 0;1206 }12071208 /**1209 * Set, change, or remove approved address to transfer the ownership of the NFT.1210 *1211 * @param signer keyring of signer1212 * @param collectionId ID of collection1213 * @param tokenId ID of token1214 * @param toAddressObj Substrate or Ethereum address which gets approved use of the signer's tokens1215 * @param amount amount of token to be approved. For NFT must be set to 1n1216 * @returns ```true``` if extrinsic success, otherwise ```false```1217 */1218 async approveToken(signer: IKeyringPair, collectionId: number, tokenId: number, toAddressObj: ICrossAccountId, amount=1n) {1219 const approveResult = await this.helper.executeExtrinsic(1220 signer,1221 'api.tx.unique.approve', [toAddressObj, collectionId, tokenId, amount],1222 true, // `Unable to approve token for ${label}`,1223 );12241225 return this.helper.util.findCollectionInEvents(approveResult.result.events, collectionId, 'common', 'Approved');1226 }12271228 /**1229 * Get the amount of token pieces approved to transfer or burn. Normally 0.1230 *1231 * @param collectionId ID of collection1232 * @param tokenId ID of token1233 * @param toAccountObj address which is approved to use token pieces1234 * @param fromAccountObj address which may have allowed the use of its owned tokens1235 * @example getTokenApprovedPieces(10, 5, {Substrate: "5DyN4Y92vZCjv38fg..."}, {Substrate: "5ERZNF88Mm7UGfPP3mdG..."})1236 * @returns number of approved to transfer pieces1237 */1238 async getTokenApprovedPieces(collectionId: number, tokenId: number, toAccountObj: ICrossAccountId, fromAccountObj: ICrossAccountId): Promise<bigint> {1239 return (await this.helper.callRpc('api.rpc.unique.allowance', [collectionId, fromAccountObj, toAccountObj, tokenId])).toBigInt();1240 }12411242 /**1243 * Get the last created token ID in a collection1244 *1245 * @param collectionId ID of collection1246 * @example getLastTokenId(10);1247 * @returns id of the last created token1248 */1249 async getLastTokenId(collectionId: number): Promise<number> {1250 return (await this.helper.callRpc('api.rpc.unique.lastTokenId', [collectionId])).toNumber();1251 }12521253 /**1254 * Check if token exists1255 *1256 * @param collectionId ID of collection1257 * @param tokenId ID of token1258 * @example doesTokenExist(10, 20);1259 * @returns true if the token exists, otherwise false1260 */1261 async doesTokenExist(collectionId: number, tokenId: number): Promise<boolean> {1262 return (await this.helper.callRpc('api.rpc.unique.tokenExists', [collectionId, tokenId])).toJSON();1263 }1264}12651266class NFTnRFT extends CollectionGroup {1267 /**1268 * Get tokens owned by account1269 *1270 * @param collectionId ID of collection1271 * @param addressObj tokens owner1272 * @example getTokensByAddress(10, {Substrate: "5DyN4Y92vZCjv38fg..."})1273 * @returns array of token ids owned by account1274 */1275 async getTokensByAddress(collectionId: number, addressObj: ICrossAccountId): Promise<number[]> {1276 return (await this.helper.callRpc('api.rpc.unique.accountTokens', [collectionId, addressObj])).toJSON();1277 }12781279 /**1280 * Get token data1281 *1282 * @param collectionId ID of collection1283 * @param tokenId ID of token1284 * @param propertyKeys optionally filter the token properties to only these keys1285 * @param blockHashAt optionally query the data at some block with this hash1286 * @example getToken(10, 5);1287 * @returns human readable token data1288 */1289 async getToken(collectionId: number, tokenId: number, propertyKeys: string[] = [], blockHashAt?: string): Promise<{1290 properties: IProperty[];1291 owner: CrossAccountId;1292 normalizedOwner: CrossAccountId;1293 }| null> {1294 let tokenData;1295 if(typeof blockHashAt === 'undefined') {1296 tokenData = await this.helper.callRpc('api.rpc.unique.tokenData', [collectionId, tokenId]);1297 }1298 else {1299 if(propertyKeys.length == 0) {1300 const collection = (await this.helper.callRpc('api.rpc.unique.collectionById', [collectionId])).toHuman();1301 if(!collection) return null;1302 propertyKeys = collection.tokenPropertyPermissions.map((x: ITokenPropertyPermission) => x.key);1303 }1304 tokenData = await this.helper.callRpc('api.rpc.unique.tokenData', [collectionId, tokenId, propertyKeys, blockHashAt]);1305 }1306 tokenData = tokenData.toHuman();1307 if (tokenData === null || tokenData.owner === null) return null;1308 const owner = {} as any;1309 for (const key of Object.keys(tokenData.owner)) {1310 owner[key.toLocaleLowerCase()] = key.toLocaleLowerCase() == 'substrate'1311 ? CrossAccountId.normalizeSubstrateAddress(tokenData.owner[key])1312 : tokenData.owner[key];1313 }1314 tokenData.normalizedOwner = CrossAccountId.fromLowerCaseKeys(owner);1315 return tokenData;1316 }13171318 /**1319 * Set permissions to change token properties1320 *1321 * @param signer keyring of signer1322 * @param collectionId ID of collection1323 * @param permissions permissions to change a property by the collection admin or token owner1324 * @example setTokenPropertyPermissions(1325 * aliceKeyring, 10, [{key: "gender", permission: {tokenOwner: true, mutable: true, collectionAdmin: true}}]1326 * )1327 * @returns true if extrinsic success otherwise false1328 */1329 async setTokenPropertyPermissions(signer: TSigner, collectionId: number, permissions: ITokenPropertyPermission[]): Promise<boolean> {1330 const result = await this.helper.executeExtrinsic(1331 signer,1332 'api.tx.unique.setTokenPropertyPermissions', [collectionId, permissions],1333 true,1334 );13351336 return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'common', 'PropertyPermissionSet');1337 }13381339 /**1340 * Get token property permissions.1341 *1342 * @param collectionId ID of collection1343 * @param propertyKeys optionally filter the returned property permissions to only these keys1344 * @example getPropertyPermissions(1219, ['location', 'date', 'time', 'isParadise']);1345 * @returns array of key-permission pairs1346 */1347 async getPropertyPermissions(collectionId: number, propertyKeys: string[] | null = null): Promise<ITokenPropertyPermission[]> {1348 return (await this.helper.callRpc('api.rpc.unique.propertyPermissions', [collectionId, ...(propertyKeys === null ? [] : [propertyKeys])])).toHuman();1349 }13501351 /**1352 * Set token properties1353 *1354 * @param signer keyring of signer1355 * @param collectionId ID of collection1356 * @param tokenId ID of token1357 * @param properties key-value pairs of metadata which to add to a token. Keys must be permitted in the collection1358 * @example setTokenProperties(aliceKeyring, 10, 5, [{key: "gender", value: "female"}, {key: "age", value: "23"}])1359 * @returns ```true``` if extrinsic success, otherwise ```false```1360 */1361 async setTokenProperties(signer: TSigner, collectionId: number, tokenId: number, properties: IProperty[]): Promise<boolean> {1362 const result = await this.helper.executeExtrinsic(1363 signer,1364 'api.tx.unique.setTokenProperties', [collectionId, tokenId, properties],1365 true,1366 );13671368 return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'common', 'TokenPropertySet');1369 }13701371 /**1372 * Get properties, metadata assigned to a token.1373 *1374 * @param collectionId ID of collection1375 * @param tokenId ID of token1376 * @param propertyKeys optionally filter the returned properties to only these keys1377 * @example getTokenProperties(1219, ['location', 'date', 'time', 'isParadise']);1378 * @returns array of key-value pairs1379 */1380 async getTokenProperties(collectionId: number, tokenId: number, propertyKeys?: string[] | null): Promise<IProperty[]> {1381 return (await this.helper.callRpc('api.rpc.unique.tokenProperties', [collectionId, tokenId, propertyKeys])).toHuman();1382 }13831384 /**1385 * Delete the provided properties of a token1386 * @param signer keyring of signer1387 * @param collectionId ID of collection1388 * @param tokenId ID of token1389 * @param propertyKeys property keys to be deleted1390 * @example deleteTokenProperties(aliceKeyring, 10, 5, ["gender", "age"])1391 * @returns ```true``` if extrinsic success, otherwise ```false```1392 */1393 async deleteTokenProperties(signer: TSigner, collectionId: number, tokenId: number, propertyKeys: string[]): Promise<boolean> {1394 const result = await this.helper.executeExtrinsic(1395 signer,1396 'api.tx.unique.deleteTokenProperties', [collectionId, tokenId, propertyKeys],1397 true,1398 );13991400 return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'common', 'TokenPropertyDeleted');1401 }14021403 /**1404 * Mint new collection1405 *1406 * @param signer keyring of signer1407 * @param collectionOptions basic collection options and properties1408 * @param mode NFT or RFT type of a collection1409 * @example mintCollection(aliceKeyring, {name: 'New', description: "New collection", tokenPrefix: "NEW"}, "NFT")1410 * @returns object of the created collection1411 */1412 async mintCollection(signer: TSigner, collectionOptions: ICollectionCreationOptions, mode: 'NFT' | 'RFT'): Promise<UniqueBaseCollection> {1413 collectionOptions = JSON.parse(JSON.stringify(collectionOptions)) as ICollectionCreationOptions; // Clone object1414 collectionOptions.mode = (mode === 'NFT') ? {nft: null} : {refungible: null};1415 for (const key of ['name', 'description', 'tokenPrefix']) {1416 if (typeof collectionOptions[key as 'name' | 'description' | 'tokenPrefix'] === 'string') collectionOptions[key as 'name' | 'description' | 'tokenPrefix'] = this.helper.util.str2vec(collectionOptions[key as 'name' | 'description' | 'tokenPrefix'] as string);1417 }1418 const creationResult = await this.helper.executeExtrinsic(1419 signer,1420 'api.tx.unique.createCollectionEx', [collectionOptions],1421 true, // errorLabel,1422 );1423 return this.getCollectionObject(this.helper.util.extractCollectionIdFromCreationResult(creationResult));1424 }14251426 getCollectionObject(_collectionId: number): any {1427 return null;1428 }14291430 getTokenObject(_collectionId: number, _tokenId: number): any {1431 return null;1432 }14331434 /**1435 * Tells whether the given `owner` approves the `operator`.1436 * @param collectionId ID of collection1437 * @param owner owner address1438 * @param operator operator addrees1439 * @returns true if operator is enabled1440 */1441 async allowanceForAll(collectionId: number, owner: ICrossAccountId, operator: ICrossAccountId): Promise<boolean> {1442 return (await this.helper.callRpc('api.rpc.unique.allowanceForAll', [collectionId, owner, operator])).toJSON();1443 }14441445 /** Sets or unsets the approval of a given operator.1446 * The `operator` is allowed to transfer all tokens of the `caller` on their behalf.1447 * @param operator Operator1448 * @param approved Should operator status be granted or revoked?1449 * @returns ```true``` if extrinsic success, otherwise ```false```1450 */1451 async setAllowanceForAll(signer: TSigner, collectionId: number, operator: ICrossAccountId, approved: boolean): Promise<boolean> {1452 const result = await this.helper.executeExtrinsic(1453 signer,1454 'api.tx.unique.setAllowanceForAll', [collectionId, operator, approved],1455 true,1456 );1457 return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'common', 'ApprovedForAll');1458 }1459}146014611462class NFTGroup extends NFTnRFT {1463 /**1464 * Get collection object1465 * @param collectionId ID of collection1466 * @example getCollectionObject(2);1467 * @returns instance of UniqueNFTCollection1468 */1469 getCollectionObject(collectionId: number): UniqueNFTCollection {1470 return new UniqueNFTCollection(collectionId, this.helper);1471 }14721473 /**1474 * Get token object1475 * @param collectionId ID of collection1476 * @param tokenId ID of token1477 * @example getTokenObject(10, 5);1478 * @returns instance of UniqueNFTToken1479 */1480 getTokenObject(collectionId: number, tokenId: number): UniqueNFToken {1481 return new UniqueNFToken(tokenId, this.getCollectionObject(collectionId));1482 }14831484 /**1485 * Get token's owner1486 * @param collectionId ID of collection1487 * @param tokenId ID of token1488 * @param blockHashAt optionally query the data at the block with this hash1489 * @example getTokenOwner(10, 5);1490 * @returns Address in CrossAccountId format, e.g. {Substrate: "5DnSF6RRjwteE3BrCj..."}1491 */1492 async getTokenOwner(collectionId: number, tokenId: number, blockHashAt?: string): Promise<CrossAccountId> {1493 let owner;1494 if (typeof blockHashAt === 'undefined') {1495 owner = await this.helper.callRpc('api.rpc.unique.tokenOwner', [collectionId, tokenId]);1496 } else {1497 owner = await this.helper.callRpc('api.rpc.unique.tokenOwner', [collectionId, tokenId, blockHashAt]);1498 }1499 return CrossAccountId.fromLowerCaseKeys(owner.toJSON());1500 }15011502 /**1503 * Is token approved to transfer1504 * @param collectionId ID of collection1505 * @param tokenId ID of token1506 * @param toAccountObj address to be approved1507 * @returns ```true``` if extrinsic success, otherwise ```false```1508 */1509 async isTokenApproved(collectionId: number, tokenId: number, toAccountObj: ICrossAccountId): Promise<boolean> {1510 return (await this.getTokenApprovedPieces(collectionId, tokenId, toAccountObj, await this.getTokenOwner(collectionId, tokenId))) === 1n;1511 }15121513 /**1514 * Changes the owner of the token.1515 *1516 * @param signer keyring of signer1517 * @param collectionId ID of collection1518 * @param tokenId ID of token1519 * @param addressObj address of a new owner1520 * @example transferToken(aliceKeyring, 10, 5, {Substrate: "5DyN4Y92vZCjv38fg..."})1521 * @returns ```true``` if extrinsic success, otherwise ```false```1522 */1523 async transferToken(signer: TSigner, collectionId: number, tokenId: number, addressObj: ICrossAccountId): Promise<boolean> {1524 return await super.transferToken(signer, collectionId, tokenId, addressObj, 1n);1525 }15261527 /**1528 *1529 * Change ownership of a NFT on behalf of the owner.1530 *1531 * @param signer keyring of signer1532 * @param collectionId ID of collection1533 * @param tokenId ID of token1534 * @param fromAddressObj address on behalf of which the token will be sent1535 * @param toAddressObj new token owner1536 * @example transferTokenFrom(aliceKeyring, 10, 5, {Substrate: "5DyN4Y92vZCjv38fg..."}, {Ethereum: "0x9F0583DbB85..."})1537 * @returns ```true``` if extrinsic success, otherwise ```false```1538 */1539 async transferTokenFrom(signer: TSigner, collectionId: number, tokenId: number, fromAddressObj: ICrossAccountId, toAddressObj: ICrossAccountId): Promise<boolean> {1540 return await super.transferTokenFrom(signer, collectionId, tokenId, fromAddressObj, toAddressObj, 1n);1541 }15421543 /**1544 * Recursively find the address that owns the token1545 * @param collectionId ID of collection1546 * @param tokenId ID of token1547 * @param blockHashAt1548 * @example getTokenTopmostOwner(10, 5);1549 * @returns address in CrossAccountId format, e.g. {Substrate: "5DyN4Y92vZCjv38fg..."}1550 */1551 async getTokenTopmostOwner(collectionId: number, tokenId: number, blockHashAt?: string): Promise<CrossAccountId | null> {1552 let owner;1553 if (typeof blockHashAt === 'undefined') {1554 owner = await this.helper.callRpc('api.rpc.unique.topmostTokenOwner', [collectionId, tokenId]);1555 } else {1556 owner = await this.helper.callRpc('api.rpc.unique.topmostTokenOwner', [collectionId, tokenId, blockHashAt]);1557 }15581559 if (owner === null) return null;15601561 return owner.toHuman();1562 }15631564 /**1565 * Get tokens nested in the provided token1566 * @param collectionId ID of collection1567 * @param tokenId ID of token1568 * @param blockHashAt optionally query the data at the block with this hash1569 * @example getTokenChildren(10, 5);1570 * @returns tokens whose depth of nesting is <= 51571 */1572 async getTokenChildren(collectionId: number, tokenId: number, blockHashAt?: string): Promise<IToken[]> {1573 let children;1574 if(typeof blockHashAt === 'undefined') {1575 children = await this.helper.callRpc('api.rpc.unique.tokenChildren', [collectionId, tokenId]);1576 } else {1577 children = await this.helper.callRpc('api.rpc.unique.tokenChildren', [collectionId, tokenId, blockHashAt]);1578 }15791580 return children.toJSON().map((x: any) => {1581 return {collectionId: x.collection, tokenId: x.token};1582 });1583 }15841585 /**1586 * Nest one token into another1587 * @param signer keyring of signer1588 * @param tokenObj token to be nested1589 * @param rootTokenObj token to be parent1590 * @example nestToken(aliceKeyring, {collectionId: 10, tokenId: 5}, {collectionId: 10, tokenId: 4});1591 * @returns ```true``` if extrinsic success, otherwise ```false```1592 */1593 async nestToken(signer: TSigner, tokenObj: IToken, rootTokenObj: IToken): Promise<boolean> {1594 const rootTokenAddress = this.helper.util.getTokenAccount(rootTokenObj);1595 const result = await this.transferToken(signer, tokenObj.collectionId, tokenObj.tokenId, rootTokenAddress);1596 if(!result) {1597 throw Error('Unable to nest token!');1598 }1599 return result;1600 }16011602 /**1603 * Remove token from nested state1604 * @param signer keyring of signer1605 * @param tokenObj token to unnest1606 * @param rootTokenObj parent of a token1607 * @param toAddressObj address of a new token owner1608 * @example unnestToken(aliceKeyring, {collectionId: 10, tokenId: 5}, {collectionId: 10, tokenId: 4}, {Substrate: "5DyN4Y92vZCjv38fg..."});1609 * @returns ```true``` if extrinsic success, otherwise ```false```1610 */1611 async unnestToken(signer: TSigner, tokenObj: IToken, rootTokenObj: IToken, toAddressObj: ICrossAccountId): Promise<boolean> {1612 const rootTokenAddress = this.helper.util.getTokenAccount(rootTokenObj);1613 const result = await this.transferTokenFrom(signer, tokenObj.collectionId, tokenObj.tokenId, rootTokenAddress, toAddressObj);1614 if(!result) {1615 throw Error('Unable to unnest token!');1616 }1617 return result;1618 }16191620 /**1621 * Mint new collection1622 * @param signer keyring of signer1623 * @param collectionOptions Collection options1624 * @example1625 * mintCollection(aliceKeyring, {1626 * name: 'New',1627 * description: 'New collection',1628 * tokenPrefix: 'NEW',1629 * })1630 * @returns object of the created collection1631 */1632 async mintCollection(signer: TSigner, collectionOptions: ICollectionCreationOptions = {}): Promise<UniqueNFTCollection> {1633 return await super.mintCollection(signer, collectionOptions, 'NFT') as UniqueNFTCollection;1634 }16351636 /**1637 * Mint new token1638 * @param signer keyring of signer1639 * @param data token data1640 * @returns created token object1641 */1642 async mintToken(signer: TSigner, data: { collectionId: number; owner: ICrossAccountId | string; properties?: IProperty[]; }): Promise<UniqueNFToken> {1643 const creationResult = await this.helper.executeExtrinsic(1644 signer,1645 'api.tx.unique.createItem', [data.collectionId, (typeof data.owner === 'string') ? {Substrate: data.owner} : data.owner, {1646 nft: {1647 properties: data.properties,1648 },1649 }],1650 true,1651 );1652 const createdTokens = this.helper.util.extractTokensFromCreationResult(creationResult);1653 if (createdTokens.tokens.length > 1) throw Error('Minted multiple tokens');1654 if (createdTokens.tokens.length < 1) throw Error('No tokens minted');1655 return this.getTokenObject(data.collectionId, createdTokens.tokens[0].tokenId);1656 }16571658 /**1659 * Mint multiple NFT tokens1660 * @param signer keyring of signer1661 * @param collectionId ID of collection1662 * @param tokens array of tokens with owner and properties1663 * @example1664 * mintMultipleTokens(aliceKeyring, 10, [{1665 * owner: {Substrate: "5DyN4Y92vZCjv38fg..."},1666 * properties: [{key: "gender", value: "male"},{key: "age", value: "45"}],1667 * },{1668 * owner: {Ethereum: "0x9F0583DbB855d..."},1669 * properties: [{key: "gender", value: "female"},{key: "age", value: "22"}],1670 * }]);1671 * @returns ```true``` if extrinsic success, otherwise ```false```1672 */1673 async mintMultipleTokens(signer: TSigner, collectionId: number, tokens: {owner: ICrossAccountId, properties?: IProperty[]}[]): Promise<UniqueNFToken[]> {1674 const creationResult = await this.helper.executeExtrinsic(1675 signer,1676 'api.tx.unique.createMultipleItemsEx', [collectionId, {NFT: tokens}],1677 true,1678 );1679 const collection = this.getCollectionObject(collectionId);1680 return this.helper.util.extractTokensFromCreationResult(creationResult).tokens.map((x: IToken) => collection.getTokenObject(x.tokenId));1681 }16821683 /**1684 * Mint multiple NFT tokens with one owner1685 * @param signer keyring of signer1686 * @param collectionId ID of collection1687 * @param owner tokens owner1688 * @param tokens array of tokens with owner and properties1689 * @example1690 * mintMultipleTokensWithOneOwner(aliceKeyring, 10, "5DyN4Y92vZCjv38fg...", [{1691 * properties: [{1692 * key: "gender",1693 * value: "female",1694 * },{1695 * key: "age",1696 * value: "33",1697 * }],1698 * }]);1699 * @returns array of newly created tokens1700 */1701 async mintMultipleTokensWithOneOwner(signer: TSigner, collectionId: number, owner: ICrossAccountId, tokens: {properties?: IProperty[]}[]): Promise<UniqueNFToken[]> {1702 const rawTokens = [];1703 for (const token of tokens) {1704 const raw = {NFT: {properties: token.properties}};1705 rawTokens.push(raw);1706 }1707 const creationResult = await this.helper.executeExtrinsic(1708 signer,1709 'api.tx.unique.createMultipleItems', [collectionId, owner, rawTokens],1710 true,1711 );1712 const collection = this.getCollectionObject(collectionId);1713 return this.helper.util.extractTokensFromCreationResult(creationResult).tokens.map((x: IToken) => collection.getTokenObject(x.tokenId));1714 }17151716 /**1717 * Set, change, or remove approved address to transfer the ownership of the NFT.1718 *1719 * @param signer keyring of signer1720 * @param collectionId ID of collection1721 * @param tokenId ID of token1722 * @param toAddressObj address to approve1723 * @example approveToken(aliceKeyring, 10, 5, {Substrate: "5DyN4Y92vZCjv38fg..."})1724 * @returns ```true``` if extrinsic success, otherwise ```false```1725 */1726 approveToken(signer: IKeyringPair, collectionId: number, tokenId: number, toAddressObj: ICrossAccountId) {1727 return super.approveToken(signer, collectionId, tokenId, toAddressObj, 1n);1728 }1729}173017311732class RFTGroup extends NFTnRFT {1733 /**1734 * Get collection object1735 * @param collectionId ID of collection1736 * @example getCollectionObject(2);1737 * @returns instance of UniqueRFTCollection1738 */1739 getCollectionObject(collectionId: number): UniqueRFTCollection {1740 return new UniqueRFTCollection(collectionId, this.helper);1741 }17421743 /**1744 * Get token object1745 * @param collectionId ID of collection1746 * @param tokenId ID of token1747 * @example getTokenObject(10, 5);1748 * @returns instance of UniqueNFTToken1749 */1750 getTokenObject(collectionId: number, tokenId: number): UniqueRFToken {1751 return new UniqueRFToken(tokenId, this.getCollectionObject(collectionId));1752 }17531754 /**1755 * Get top 10 token owners with the largest number of pieces1756 * @param collectionId ID of collection1757 * @param tokenId ID of token1758 * @example getTokenTop10Owners(10, 5);1759 * @returns array of top 10 owners1760 */1761 async getTokenTop10Owners(collectionId: number, tokenId: number): Promise<CrossAccountId[]> {1762 return (await this.helper.callRpc('api.rpc.unique.tokenOwners', [collectionId, tokenId])).toJSON().map(CrossAccountId.fromLowerCaseKeys);1763 }17641765 /**1766 * Get number of pieces owned by address1767 * @param collectionId ID of collection1768 * @param tokenId ID of token1769 * @param addressObj address token owner1770 * @example getTokenBalance(10, 5, {Substrate: "5DyN4Y92vZCjv38fg..."});1771 * @returns number of pieces ownerd by address1772 */1773 async getTokenBalance(collectionId: number, tokenId: number, addressObj: ICrossAccountId): Promise<bigint> {1774 return (await this.helper.callRpc('api.rpc.unique.balance', [collectionId, addressObj, tokenId])).toBigInt();1775 }17761777 /**1778 * Transfer pieces of token to another address1779 * @param signer keyring of signer1780 * @param collectionId ID of collection1781 * @param tokenId ID of token1782 * @param addressObj address of a new owner1783 * @param amount number of pieces to be transfered1784 * @example transferTokenFrom(aliceKeyring, 10, 5, {Substrate: "5DyN4Y92vZCjv38fg..."}, 2000n)1785 * @returns ```true``` if extrinsic success, otherwise ```false```1786 */1787 async transferToken(signer: TSigner, collectionId: number, tokenId: number, addressObj: ICrossAccountId, amount=1n): Promise<boolean> {1788 return await super.transferToken(signer, collectionId, tokenId, addressObj, amount);1789 }17901791 /**1792 * Change ownership of some pieces of RFT on behalf of the owner.1793 * @param signer keyring of signer1794 * @param collectionId ID of collection1795 * @param tokenId ID of token1796 * @param fromAddressObj address on behalf of which the token will be sent1797 * @param toAddressObj new token owner1798 * @param amount number of pieces to be transfered1799 * @example transferTokenFrom(aliceKeyring, 10, 5, {Substrate: "5DyN4Y92vZCjv38fg..."}, {Substrate: "5DfhbVfww7ThF8q6f3i..."}, 2000n)1800 * @returns ```true``` if extrinsic success, otherwise ```false```1801 */1802 async transferTokenFrom(signer: TSigner, collectionId: number, tokenId: number, fromAddressObj: ICrossAccountId, toAddressObj: ICrossAccountId, amount=1n): Promise<boolean> {1803 return await super.transferTokenFrom(signer, collectionId, tokenId, fromAddressObj, toAddressObj, amount);1804 }18051806 /**1807 * Mint new collection1808 * @param signer keyring of signer1809 * @param collectionOptions Collection options1810 * @example1811 * mintCollection(aliceKeyring, {1812 * name: 'New',1813 * description: 'New collection',1814 * tokenPrefix: 'NEW',1815 * })1816 * @returns object of the created collection1817 */1818 async mintCollection(signer: TSigner, collectionOptions: ICollectionCreationOptions = {}): Promise<UniqueRFTCollection> {1819 return await super.mintCollection(signer, collectionOptions, 'RFT') as UniqueRFTCollection;1820 }18211822 /**1823 * Mint new token1824 * @param signer keyring of signer1825 * @param data token data1826 * @example mintToken(aliceKeyring, {collectionId: 10, owner: {Substrate: '5GHoZe9c73RYbVzq...'}, pieces: 10000n});1827 * @returns created token object1828 */1829 async mintToken(signer: TSigner, data: { collectionId: number; owner: ICrossAccountId | string; pieces: bigint; properties?: IProperty[]; }): Promise<UniqueRFToken> {1830 const creationResult = await this.helper.executeExtrinsic(1831 signer,1832 'api.tx.unique.createItem', [data.collectionId, (typeof data.owner === 'string') ? {Substrate: data.owner} : data.owner, {1833 refungible: {1834 pieces: data.pieces,1835 properties: data.properties,1836 },1837 }],1838 true,1839 );1840 const createdTokens = this.helper.util.extractTokensFromCreationResult(creationResult);1841 if (createdTokens.tokens.length > 1) throw Error('Minted multiple tokens');1842 if (createdTokens.tokens.length < 1) throw Error('No tokens minted');1843 return this.getTokenObject(data.collectionId, createdTokens.tokens[0].tokenId);1844 }18451846 async mintMultipleTokens(signer: TSigner, collectionId: number, tokens: {owner: ICrossAccountId, pieces: bigint, properties?: IProperty[]}[]): Promise<UniqueRFToken[]> {1847 throw Error('Not implemented');1848 const creationResult = await this.helper.executeExtrinsic(1849 signer,1850 'api.tx.unique.createMultipleItemsEx', [collectionId, {RefungibleMultipleOwners: tokens}],1851 true, // `Unable to mint RFT tokens for ${label}`,1852 );1853 const collection = this.getCollectionObject(collectionId);1854 return this.helper.util.extractTokensFromCreationResult(creationResult).tokens.map((x: IToken) => collection.getTokenObject(x.tokenId));1855 }18561857 /**1858 * Mint multiple RFT tokens with one owner1859 * @param signer keyring of signer1860 * @param collectionId ID of collection1861 * @param owner tokens owner1862 * @param tokens array of tokens with properties and pieces1863 * @example mintMultipleTokensWithOneOwner(aliceKeyring, 10, {Substrate: "5GHoZe9c73RYbVzq..."}, [{pieces: 100000n, properties: [{key: "gender", value: "male"}]}]);1864 * @returns array of newly created RFT tokens1865 */1866 async mintMultipleTokensWithOneOwner(signer: TSigner, collectionId: number, owner: ICrossAccountId, tokens: {pieces: bigint, properties?: IProperty[]}[]): Promise<UniqueRFToken[]> {1867 const rawTokens = [];1868 for (const token of tokens) {1869 const raw = {ReFungible: {pieces: token.pieces, properties: token.properties}};1870 rawTokens.push(raw);1871 }1872 const creationResult = await this.helper.executeExtrinsic(1873 signer,1874 'api.tx.unique.createMultipleItems', [collectionId, owner, rawTokens],1875 true,1876 );1877 const collection = this.getCollectionObject(collectionId);1878 return this.helper.util.extractTokensFromCreationResult(creationResult).tokens.map((x: IToken) => collection.getTokenObject(x.tokenId));1879 }18801881 /**1882 * Destroys a concrete instance of RFT.1883 * @param signer keyring of signer1884 * @param collectionId ID of collection1885 * @param tokenId ID of token1886 * @param amount number of pieces to be burnt1887 * @example burnToken(aliceKeyring, 10, 5);1888 * @returns ```true``` if the extrinsic is successful, otherwise ```false```1889 */1890 async burnToken(signer: IKeyringPair, collectionId: number, tokenId: number, amount=1n): Promise<boolean> {1891 return await super.burnToken(signer, collectionId, tokenId, amount);1892 }18931894 /**1895 * Destroys a concrete instance of RFT on behalf of the owner.1896 * @param signer keyring of signer1897 * @param collectionId ID of collection1898 * @param tokenId ID of token1899 * @param fromAddressObj address on behalf of which the token will be burnt1900 * @param amount number of pieces to be burnt1901 * @example burnTokenFrom(aliceKeyring, 10, 5, {Substrate: "5DyN4Y92vZCjv38fg..."}, 2n)1902 * @returns ```true``` if extrinsic success, otherwise ```false```1903 */1904 async burnTokenFrom(signer: IKeyringPair, collectionId: number, tokenId: number, fromAddressObj: ICrossAccountId, amount=1n): Promise<boolean> {1905 return await super.burnTokenFrom(signer, collectionId, tokenId, fromAddressObj, amount);1906 }19071908 /**1909 * Set, change, or remove approved address to transfer the ownership of the RFT.1910 *1911 * @param signer keyring of signer1912 * @param collectionId ID of collection1913 * @param tokenId ID of token1914 * @param toAddressObj address to approve1915 * @param amount number of pieces to be approved1916 * @example approveToken(aliceKeyring, 10, 5, {Substrate: "5GHoZe9c73RYbVzq..."}, "", 10000n);1917 * @returns true if the token success, otherwise false1918 */1919 approveToken(signer: IKeyringPair, collectionId: number, tokenId: number, toAddressObj: ICrossAccountId, amount=1n) {1920 return super.approveToken(signer, collectionId, tokenId, toAddressObj, amount);1921 }19221923 /**1924 * Get total number of pieces1925 * @param collectionId ID of collection1926 * @param tokenId ID of token1927 * @example getTokenTotalPieces(10, 5);1928 * @returns number of pieces1929 */1930 async getTokenTotalPieces(collectionId: number, tokenId: number): Promise<bigint> {1931 return (await this.helper.callRpc('api.rpc.unique.totalPieces', [collectionId, tokenId])).unwrap().toBigInt();1932 }19331934 /**1935 * Change number of token pieces. Signer must be the owner of all token pieces.1936 * @param signer keyring of signer1937 * @param collectionId ID of collection1938 * @param tokenId ID of token1939 * @param amount new number of pieces1940 * @example repartitionToken(aliceKeyring, 10, 5, 12345n);1941 * @returns true if the repartion was success, otherwise false1942 */1943 async repartitionToken(signer: TSigner, collectionId: number, tokenId: number, amount: bigint): Promise<boolean> {1944 const currentAmount = await this.getTokenTotalPieces(collectionId, tokenId);1945 const repartitionResult = await this.helper.executeExtrinsic(1946 signer,1947 'api.tx.unique.repartition', [collectionId, tokenId, amount],1948 true,1949 );1950 if(currentAmount < amount) return this.helper.util.findCollectionInEvents(repartitionResult.result.events, collectionId, 'common', 'ItemCreated');1951 return this.helper.util.findCollectionInEvents(repartitionResult.result.events, collectionId, 'common', 'ItemDestroyed');1952 }1953}195419551956class FTGroup extends CollectionGroup {1957 /**1958 * Get collection object1959 * @param collectionId ID of collection1960 * @example getCollectionObject(2);1961 * @returns instance of UniqueFTCollection1962 */1963 getCollectionObject(collectionId: number): UniqueFTCollection {1964 return new UniqueFTCollection(collectionId, this.helper);1965 }19661967 /**1968 * Mint new fungible collection1969 * @param signer keyring of signer1970 * @param collectionOptions Collection options1971 * @param decimalPoints number of token decimals1972 * @example1973 * mintCollection(aliceKeyring, {1974 * name: 'New',1975 * description: 'New collection',1976 * tokenPrefix: 'NEW',1977 * }, 18)1978 * @returns newly created fungible collection1979 */1980 async mintCollection(signer: TSigner, collectionOptions: ICollectionCreationOptions = {}, decimalPoints = 0): Promise<UniqueFTCollection> {1981 collectionOptions = JSON.parse(JSON.stringify(collectionOptions)) as ICollectionCreationOptions; // Clone object1982 if(collectionOptions.tokenPropertyPermissions) throw Error('Fungible collections has no tokenPropertyPermissions');1983 collectionOptions.mode = {fungible: decimalPoints};1984 for (const key of ['name', 'description', 'tokenPrefix']) {1985 if (typeof collectionOptions[key as 'name' | 'description' | 'tokenPrefix'] === 'string') collectionOptions[key as 'name' | 'description' | 'tokenPrefix'] = this.helper.util.str2vec(collectionOptions[key as 'name' | 'description' | 'tokenPrefix'] as string);1986 }1987 const creationResult = await this.helper.executeExtrinsic(1988 signer,1989 'api.tx.unique.createCollectionEx', [collectionOptions],1990 true,1991 );1992 return this.getCollectionObject(this.helper.util.extractCollectionIdFromCreationResult(creationResult));1993 }19941995 /**1996 * Mint tokens1997 * @param signer keyring of signer1998 * @param collectionId ID of collection1999 * @param owner address owner of new tokens2000 * @param amount amount of tokens to be meanted2001 * @example mintTokens(aliceKeyring, 10, {Substrate: "5GHoZe9c73RYbVzq"}, 1000n);2002 * @returns ```true``` if extrinsic success, otherwise ```false```2003 */2004 async mintTokens(signer: TSigner, collectionId: number, amount: bigint, owner: ICrossAccountId | string): Promise<boolean> {2005 const creationResult = await this.helper.executeExtrinsic(2006 signer,2007 'api.tx.unique.createItem', [collectionId, (typeof owner === 'string') ? {Substrate: owner} : owner, {2008 fungible: {2009 value: amount,2010 },2011 }],2012 true, // `Unable to mint fungible tokens for ${label}`,2013 );2014 return this.helper.util.findCollectionInEvents(creationResult.result.events, collectionId, 'common', 'ItemCreated');2015 }20162017 /**2018 * Mint multiple Fungible tokens with one owner2019 * @param signer keyring of signer2020 * @param collectionId ID of collection2021 * @param owner tokens owner2022 * @param tokens array of tokens with properties and pieces2023 * @returns ```true``` if extrinsic success, otherwise ```false```2024 */2025 async mintMultipleTokensWithOneOwner(signer: TSigner, collectionId: number, tokens: {value: bigint}[], owner: ICrossAccountId): Promise<boolean> {2026 const rawTokens = [];2027 for (const token of tokens) {2028 const raw = {Fungible: {Value: token.value}};2029 rawTokens.push(raw);2030 }2031 const creationResult = await this.helper.executeExtrinsic(2032 signer,2033 'api.tx.unique.createMultipleItems', [collectionId, owner, rawTokens],2034 true,2035 );2036 return this.helper.util.findCollectionInEvents(creationResult.result.events, collectionId, 'common', 'ItemCreated');2037 }20382039 /**2040 * Get the top 10 owners with the largest balance for the Fungible collection2041 * @param collectionId ID of collection2042 * @example getTop10Owners(10);2043 * @returns array of ```ICrossAccountId```2044 */2045 async getTop10Owners(collectionId: number): Promise<CrossAccountId[]> {2046 return (await this.helper.callRpc('api.rpc.unique.tokenOwners', [collectionId, 0])).toJSON().map(CrossAccountId.fromLowerCaseKeys);2047 }20482049 /**2050 * Get account balance2051 * @param collectionId ID of collection2052 * @param addressObj address of owner2053 * @example getBalance(10, {Substrate: "5GHoZe9c73RYbVzq..."})2054 * @returns amount of fungible tokens owned by address2055 */2056 async getBalance(collectionId: number, addressObj: ICrossAccountId): Promise<bigint> {2057 return (await this.helper.callRpc('api.rpc.unique.balance', [collectionId, addressObj, 0])).toBigInt();2058 }20592060 /**2061 * Transfer tokens to address2062 * @param signer keyring of signer2063 * @param collectionId ID of collection2064 * @param toAddressObj address recipient2065 * @param amount amount of tokens to be sent2066 * @example transfer(aliceKeyring, 10, {Substrate: "5GHoZe9c73RYbVzq..."}, 1000n);2067 * @returns ```true``` if extrinsic success, otherwise ```false```2068 */2069 async transfer(signer: TSigner, collectionId: number, toAddressObj: ICrossAccountId, amount=1n) {2070 return await super.transferToken(signer, collectionId, 0, toAddressObj, amount);2071 }20722073 /**2074 * Transfer some tokens on behalf of the owner.2075 * @param signer keyring of signer2076 * @param collectionId ID of collection2077 * @param fromAddressObj address on behalf of which tokens will be sent2078 * @param toAddressObj address where token to be sent2079 * @param amount number of tokens to be sent2080 * @example transferFrom(aliceKeyring, 10, {Substrate: "5GHoZe9c73RYbVzq..."}, {Substrate: "5DfhbVfww7ThF8q6f3ij..."}, 10000n);2081 * @returns ```true``` if extrinsic success, otherwise ```false```2082 */2083 async transferFrom(signer: TSigner, collectionId: number, fromAddressObj: ICrossAccountId, toAddressObj: ICrossAccountId, amount=1n) {2084 return await super.transferTokenFrom(signer, collectionId, 0, fromAddressObj, toAddressObj, amount);2085 }20862087 /**2088 * Destroy some amount of tokens2089 * @param signer keyring of signer2090 * @param collectionId ID of collection2091 * @param amount amount of tokens to be destroyed2092 * @example burnTokens(aliceKeyring, 10, 1000n);2093 * @returns ```true``` if extrinsic success, otherwise ```false```2094 */2095 async burnTokens(signer: IKeyringPair, collectionId: number, amount=1n): Promise<boolean> {2096 return await super.burnToken(signer, collectionId, 0, amount);2097 }20982099 /**2100 * Burn some tokens on behalf of the owner.2101 * @param signer keyring of signer2102 * @param collectionId ID of collection2103 * @param fromAddressObj address on behalf of which tokens will be burnt2104 * @param amount amount of tokens to be burnt2105 * @example burnTokensFrom(aliceKeyring, 10, {Substrate: "5GHoZe9c73RYbVzq..."}, 1000n);2106 * @returns ```true``` if extrinsic success, otherwise ```false```2107 */2108 async burnTokensFrom(signer: IKeyringPair, collectionId: number, fromAddressObj: ICrossAccountId, amount=1n): Promise<boolean> {2109 return await super.burnTokenFrom(signer, collectionId, 0, fromAddressObj, amount);2110 }21112112 /**2113 * Get total collection supply2114 * @param collectionId2115 * @returns2116 */2117 async getTotalPieces(collectionId: number): Promise<bigint> {2118 return (await this.helper.callRpc('api.rpc.unique.totalPieces', [collectionId, 0])).unwrap().toBigInt();2119 }21202121 /**2122 * Set, change, or remove approved address to transfer tokens.2123 *2124 * @param signer keyring of signer2125 * @param collectionId ID of collection2126 * @param toAddressObj address to be approved2127 * @param amount amount of tokens to be approved2128 * @example approveTokens(aliceKeyring, 10, {Substrate: "5GHoZe9c73RYbVzq..."}, 1000n)2129 * @returns ```true``` if extrinsic success, otherwise ```false```2130 */2131 approveTokens(signer: IKeyringPair, collectionId: number, toAddressObj: ICrossAccountId, amount=1n) {2132 return super.approveToken(signer, collectionId, 0, toAddressObj, amount);2133 }21342135 /**2136 * Get amount of fungible tokens approved to transfer2137 * @param collectionId ID of collection2138 * @param fromAddressObj owner of tokens2139 * @param toAddressObj the address approved for the transfer of tokens on behalf of the owner2140 * @returns number of tokens approved for the transfer2141 */2142 getApprovedTokens(collectionId: number, fromAddressObj: ICrossAccountId, toAddressObj: ICrossAccountId) {2143 return super.getTokenApprovedPieces(collectionId, 0, toAddressObj, fromAddressObj);2144 }2145}214621472148class ChainGroup extends HelperGroup<ChainHelperBase> {2149 /**2150 * Get system properties of a chain2151 * @example getChainProperties();2152 * @returns ss58Format, token decimals, and token symbol2153 */2154 getChainProperties(): IChainProperties {2155 const properties = (this.helper.getApi() as any).registry.getChainProperties().toJSON();2156 return {2157 ss58Format: properties.ss58Format.toJSON(),2158 tokenDecimals: properties.tokenDecimals.toJSON(),2159 tokenSymbol: properties.tokenSymbol.toJSON(),2160 };2161 }21622163 /**2164 * Get chain header2165 * @example getLatestBlockNumber();2166 * @returns the number of the last block2167 */2168 async getLatestBlockNumber(): Promise<number> {2169 return (await this.helper.callRpc('api.rpc.chain.getHeader')).number.toNumber();2170 }21712172 /**2173 * Get block hash by block number2174 * @param blockNumber number of block2175 * @example getBlockHashByNumber(12345);2176 * @returns hash of a block2177 */2178 async getBlockHashByNumber(blockNumber: number): Promise<string | null> {2179 const blockHash = (await this.helper.callRpc('api.rpc.chain.getBlockHash', [blockNumber])).toJSON();2180 if(blockHash === '0x0000000000000000000000000000000000000000000000000000000000000000') return null;2181 return blockHash;2182 }21832184 // TODO add docs2185 async getBlock(blockHashOrNumber: string | number): Promise<IBlock | null> {2186 const blockHash = typeof blockHashOrNumber === 'string' ? blockHashOrNumber : await this.getBlockHashByNumber(blockHashOrNumber);2187 if (!blockHash) return null;2188 return (await this.helper.callRpc('api.rpc.chain.getBlock', [blockHash])).toHuman().block;2189 }21902191 /**2192 * Get account nonce2193 * @param address substrate address2194 * @example getNonce("5GrwvaEF5zXb26Fz...");2195 * @returns number, account's nonce2196 */2197 async getNonce(address: TSubstrateAccount): Promise<number> {2198 return (await this.helper.callRpc('api.query.system.account', [address])).nonce.toNumber();2199 }2200}22012202class SubstrateBalanceGroup<T extends ChainHelperBase> extends HelperGroup<T> {2203 /**2204 * Get substrate address balance2205 * @param address substrate address2206 * @example getSubstrate("5GrwvaEF5zXb26Fz...")2207 * @returns amount of tokens on address2208 */2209 async getSubstrate(address: TSubstrateAccount): Promise<bigint> {2210 return (await this.helper.callRpc('api.query.system.account', [address])).data.free.toBigInt();2211 }22122213 /**2214 * Transfer tokens to substrate address2215 * @param signer keyring of signer2216 * @param address substrate address of a recipient2217 * @param amount amount of tokens to be transfered2218 * @example transferToSubstrate(aliceKeyring, "5GrwvaEF5zXb26Fz...", 100_000_000_000n);2219 * @returns ```true``` if extrinsic success, otherwise ```false```2220 */2221 async transferToSubstrate(signer: TSigner, address: TSubstrateAccount, amount: bigint | string): Promise<boolean> {2222 const result = await this.helper.executeExtrinsic(signer, 'api.tx.balances.transfer', [address, amount], true/*, `Unable to transfer balance from ${this.helper.getSignerAddress(signer)} to ${address}`*/);22232224 let transfer = {from: null, to: null, amount: 0n} as any;2225 result.result.events.forEach(({event: {data, method, section}}) => {2226 if ((section === 'balances') && (method === 'Transfer')) {2227 transfer = {2228 from: this.helper.address.normalizeSubstrate(data[0]),2229 to: this.helper.address.normalizeSubstrate(data[1]),2230 amount: BigInt(data[2]),2231 };2232 }2233 });2234 const isSuccess = this.helper.address.normalizeSubstrate(typeof signer === 'string' ? signer : signer.address) === transfer.from2235 && this.helper.address.normalizeSubstrate(address) === transfer.to2236 && BigInt(amount) === transfer.amount;2237 return isSuccess;2238 }22392240 /**2241 * Get full substrate balance including free, miscFrozen, feeFrozen, and reserved2242 * @param address substrate address2243 * @returns2244 */2245 async getSubstrateFull(address: TSubstrateAccount): Promise<ISubstrateBalance> {2246 const accountInfo = (await this.helper.callRpc('api.query.system.account', [address])).data;2247 return {free: accountInfo.free.toBigInt(), miscFrozen: accountInfo.miscFrozen.toBigInt(), feeFrozen: accountInfo.feeFrozen.toBigInt(), reserved: accountInfo.reserved.toBigInt()};2248 }2249}22502251class EthereumBalanceGroup<T extends ChainHelperBase> extends HelperGroup<T> {2252 /**2253 * Get ethereum address balance2254 * @param address ethereum address2255 * @example getEthereum("0x9F0583DbB855d...")2256 * @returns amount of tokens on address2257 */2258 async getEthereum(address: TEthereumAccount): Promise<bigint> {2259 return (await this.helper.callRpc('api.rpc.eth.getBalance', [address])).toBigInt();2260 }22612262 /**2263 * Transfer tokens to address2264 * @param signer keyring of signer2265 * @param address Ethereum address of a recipient2266 * @param amount amount of tokens to be transfered2267 * @example transferToEthereum(alithKeyring, "0x9F0583DbB855d...", 100_000_000_000n);2268 * @returns ```true``` if extrinsic success, otherwise ```false```2269 */2270 async transferToEthereum(signer: TSigner, address: TEthereumAccount, amount: bigint | string): Promise<boolean> {2271 const result = await this.helper.executeExtrinsic(signer, 'api.tx.balances.transfer', [address, amount], true);22722273 let transfer = {from: null, to: null, amount: 0n} as any;2274 result.result.events.forEach(({event: {data, method, section}}) => {2275 if ((section === 'balances') && (method === 'Transfer')) {2276 transfer = {2277 from: data[0].toString(),2278 to: data[1].toString(),2279 amount: BigInt(data[2]),2280 };2281 }2282 });2283 const isSuccess = (typeof signer === 'string' ? signer : signer.address) === transfer.from2284 && address === transfer.to2285 && BigInt(amount) === transfer.amount;2286 return isSuccess;2287 }2288}22892290class BalanceGroup<T extends ChainHelperBase> extends HelperGroup<T> {2291 subBalanceGroup: SubstrateBalanceGroup<T>;2292 ethBalanceGroup: EthereumBalanceGroup<T>;22932294 constructor(helper: T) {2295 super(helper);2296 this.subBalanceGroup = new SubstrateBalanceGroup(helper);2297 this.ethBalanceGroup = new EthereumBalanceGroup(helper);2298 }22992300 getCollectionCreationPrice(): bigint {2301 return 2n * this.getOneTokenNominal();2302 }2303 /**2304 * Representation of the native token in the smallest unit - one OPAL (OPL), QUARTZ (QTZ), or UNIQUE (UNQ).2305 * @example getOneTokenNominal()2306 * @returns ```BigInt``` representation of the native token in the smallest unit, e.g. ```1_000_000_000_000_000_000n``` for QTZ.2307 */2308 getOneTokenNominal(): bigint {2309 const chainProperties = this.helper.chain.getChainProperties();2310 return 10n ** BigInt((chainProperties.tokenDecimals || [18])[0]);2311 }23122313 /**2314 * Get substrate address balance2315 * @param address substrate address2316 * @example getSubstrate("5GrwvaEF5zXb26Fz...")2317 * @returns amount of tokens on address2318 */2319 getSubstrate(address: TSubstrateAccount): Promise<bigint> {2320 return this.subBalanceGroup.getSubstrate(address);2321 }23222323 /**2324 * Get full substrate balance including free, miscFrozen, feeFrozen, and reserved2325 * @param address substrate address2326 * @returns2327 */2328 getSubstrateFull(address: TSubstrateAccount): Promise<ISubstrateBalance> {2329 return this.subBalanceGroup.getSubstrateFull(address);2330 }23312332 /**2333 * Get ethereum address balance2334 * @param address ethereum address2335 * @example getEthereum("0x9F0583DbB855d...")2336 * @returns amount of tokens on address2337 */2338 getEthereum(address: TEthereumAccount): Promise<bigint> {2339 return this.ethBalanceGroup.getEthereum(address);2340 }23412342 /**2343 * Transfer tokens to substrate address2344 * @param signer keyring of signer2345 * @param address substrate address of a recipient2346 * @param amount amount of tokens to be transfered2347 * @example transferToSubstrate(aliceKeyring, "5GrwvaEF5zXb26Fz...", 100_000_000_000n);2348 * @returns ```true``` if extrinsic success, otherwise ```false```2349 */2350 transferToSubstrate(signer: TSigner, address: TSubstrateAccount, amount: bigint | string): Promise<boolean> {2351 return this.subBalanceGroup.transferToSubstrate(signer, address, amount);2352 }23532354 async forceTransferToSubstrate(signer: TSigner, from: TSubstrateAccount, to: TSubstrateAccount, amount: bigint | string): Promise<boolean> {2355 const result = await this.helper.executeExtrinsic(signer, 'api.tx.balances.forceTransfer', [from, to, amount], true);23562357 let transfer = {from: null, to: null, amount: 0n} as any;2358 result.result.events.forEach(({event: {data, method, section}}) => {2359 if ((section === 'balances') && (method === 'Transfer')) {2360 transfer = {2361 from: this.helper.address.normalizeSubstrate(data[0]),2362 to: this.helper.address.normalizeSubstrate(data[1]),2363 amount: BigInt(data[2]),2364 };2365 }2366 });2367 let isSuccess = this.helper.address.normalizeSubstrate(from) === transfer.from;2368 isSuccess = isSuccess && this.helper.address.normalizeSubstrate(to) === transfer.to;2369 isSuccess = isSuccess && BigInt(amount) === transfer.amount;2370 return isSuccess;2371 }2372}23732374class AddressGroup extends HelperGroup<ChainHelperBase> {2375 /**2376 * Normalizes the address to the specified ss58 format, by default ```42```.2377 * @param address substrate address2378 * @param ss58Format format for address conversion, by default ```42```2379 * @example normalizeSubstrate("unjKJQJrRd238pkUZZvzDQrfKuM39zBSnQ5zjAGAGcdRhaJTx") // returns 5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY2380 * @returns substrate address converted to normalized (i.e., starting with 5) or specified explicitly representation2381 */2382 normalizeSubstrate(address: TSubstrateAccount, ss58Format = 42): TSubstrateAccount {2383 return CrossAccountId.normalizeSubstrateAddress(address, ss58Format);2384 }23852386 /**2387 * Get address in the connected chain format2388 * @param address substrate address2389 * @example normalizeSubstrateToChainFormat("5GrwvaEF5zXb26Fz...") // returns unjKJQJrRd238pkUZZ... for Unique Network2390 * @returns address in chain format2391 */2392 normalizeSubstrateToChainFormat(address: TSubstrateAccount): TSubstrateAccount {2393 return this.normalizeSubstrate(address, this.helper.chain.getChainProperties().ss58Format);2394 }23952396 /**2397 * Get substrate mirror of an ethereum address2398 * @param ethAddress ethereum address2399 * @param toChainFormat false for normalized account2400 * @example ethToSubstrate('0x9F0583DbB855d...')2401 * @returns substrate mirror of a provided ethereum address2402 */2403 ethToSubstrate(ethAddress: TEthereumAccount, toChainFormat=false): TSubstrateAccount {2404 return CrossAccountId.translateEthToSub(ethAddress, toChainFormat ? this.helper.chain.getChainProperties().ss58Format : undefined);2405 }24062407 /**2408 * Get ethereum mirror of a substrate address2409 * @param subAddress substrate account2410 * @example substrateToEth("5DnSF6RRjwteE3BrC...")2411 * @returns ethereum mirror of a provided substrate address2412 */2413 substrateToEth(subAddress: TSubstrateAccount): TEthereumAccount {2414 return CrossAccountId.translateSubToEth(subAddress);2415 }24162417 /**2418 * Encode key to substrate address2419 * @param key key for encoding address2420 * @param ss58Format prefix for encoding to the address of the corresponding network2421 * @returns encoded substrate address2422 */2423 encodeSubstrateAddress (key: Uint8Array | string | bigint, ss58Format = 42): string {2424 const u8a :Uint8Array = typeof key === 'string'2425 ? hexToU8a(key)2426 : typeof key === 'bigint'2427 ? hexToU8a(key.toString(16))2428 : key;2429 2430 if (ss58Format < 0 || ss58Format > 16383 || [46, 47].includes(ss58Format)) {2431 throw new Error(`ss58Format is not valid, received ${typeofss58Format} "${ss58Format}"`);2432 }2433 2434 const allowedDecodedLengths = [1, 2, 4, 8, 32, 33];2435 if (!allowedDecodedLengths.includes(u8a.length)) {2436 throw new Error(`key length is not valid, received ${u8a.length}, valid values are ${allowedDecodedLengths.join(', ')}`);2437 }2438 2439 const u8aPrefix = ss58Format < 642440 ? new Uint8Array([ss58Format])2441 : new Uint8Array([2442 ((ss58Format & 0xfc) >> 2) | 0x40,2443 (ss58Format >> 8) | ((ss58Format & 0x03) << 6),2444 ]);24452446 const input = u8aConcat(u8aPrefix, u8a);2447 2448 return base58Encode(u8aConcat(2449 input,2450 blake2AsU8a(input).subarray(0, [32, 33].includes(u8a.length) ? 2 : 1),2451 ));2452 }24532454 /**2455 * Restore substrate address from bigint representation2456 * @param number decimal representation of substrate address2457 * @returns substrate address2458 */2459 restoreCrossAccountFromBigInt(number: bigint): TSubstrateAccount {2460 if (this.helper.api === null) {2461 throw 'Not connected';2462 }2463 const res = this.helper.api.registry.createType('AccountId', '0x' + number.toString(16).padStart(64, '0')).toJSON();2464 if (res === undefined || res === null) {2465 throw 'Restore address error';2466 }2467 return res.toString();2468 }24692470 /**2471 * Convert etherium cross account id to substrate cross account id2472 * @param ethCrossAccount etherium cross account2473 * @returns substrate cross account id2474 */2475 convertCrossAccountFromEthCrossAccount(ethCrossAccount: IEthCrossAccountId): ICrossAccountId {2476 if (ethCrossAccount.sub === '0') {2477 return {Ethereum: ethCrossAccount.eth.toLocaleLowerCase()};2478 }2479 2480 const ss58 = this.restoreCrossAccountFromBigInt(BigInt(ethCrossAccount.sub));2481 return {Substrate: ss58};2482 }24832484 paraSiblingSovereignAccount(paraid: number) {2485 // We are getting a *sibling* parachain sovereign account,2486 // so we need a sibling prefix: encoded(b"sibl") == 0x7369626c2487 const siblingPrefix = '0x7369626c';24882489 const encodedParaId = this.helper.getApi().createType('u32', paraid).toHex(true).substring(2);2490 const suffix = '000000000000000000000000000000000000000000000000';24912492 return siblingPrefix + encodedParaId + suffix;2493 }2494}24952496class StakingGroup extends HelperGroup<UniqueHelper> {2497 /**2498 * Stake tokens for App Promotion2499 * @param signer keyring of signer2500 * @param amountToStake amount of tokens to stake2501 * @param label extra label for log2502 * @returns2503 */2504 async stake(signer: TSigner, amountToStake: bigint, label?: string): Promise<boolean> {2505 if(typeof label === 'undefined') label = `${signer.address} amount: ${amountToStake}`;2506 const _stakeResult = await this.helper.executeExtrinsic(2507 signer, 'api.tx.appPromotion.stake',2508 [amountToStake], true,2509 );2510 // TODO extract info from stakeResult2511 return true;2512 }25132514 /**2515 * Unstake tokens for App Promotion2516 * @param signer keyring of signer2517 * @param amountToUnstake amount of tokens to unstake2518 * @param label extra label for log2519 * @returns block number where balances will be unlocked2520 */2521 async unstake(signer: TSigner, label?: string): Promise<number> {2522 if(typeof label === 'undefined') label = `${signer.address}`;2523 const _unstakeResult = await this.helper.executeExtrinsic(2524 signer, 'api.tx.appPromotion.unstake',2525 [], true,2526 );2527 // TODO extract block number fron events2528 return 1;2529 }25302531 /**2532 * Get total staked amount for address2533 * @param address substrate or ethereum address2534 * @returns total staked amount2535 */2536 async getTotalStaked(address?: ICrossAccountId): Promise<bigint> {2537 if (address) return (await this.helper.callRpc('api.rpc.appPromotion.totalStaked', [address])).toBigInt();2538 return (await this.helper.callRpc('api.rpc.appPromotion.totalStaked')).toBigInt();2539 }25402541 /**2542 * Get total staked per block2543 * @param address substrate or ethereum address2544 * @returns array of stakes. `block` – the number of the block in which the stake was made. `amount` - the number of tokens staked in the block2545 */2546 async getTotalStakedPerBlock(address: ICrossAccountId): Promise<IStakingInfo[]> {2547 const rawTotalStakerdPerBlock = await this.helper.callRpc('api.rpc.appPromotion.totalStakedPerBlock', [address]);2548 return rawTotalStakerdPerBlock.map(([block, amount]: any[]) => {2549 return {2550 block: block.toBigInt(),2551 amount: amount.toBigInt(),2552 };2553 });2554 }25552556 /**2557 * Get total pending unstake amount for address2558 * @param address substrate or ethereum address2559 * @returns total pending unstake amount2560 */2561 async getPendingUnstake(address: ICrossAccountId): Promise<bigint> {2562 return (await this.helper.callRpc('api.rpc.appPromotion.pendingUnstake', [address])).toBigInt();2563 }25642565 /**2566 * Get pending unstake amount per block for address2567 * @param address substrate or ethereum address2568 * @returns array of pending stakes. `block` – the number of the block in which the unstake was made. `amount` - the number of tokens unstaked in the block2569 */2570 async getPendingUnstakePerBlock(address: ICrossAccountId): Promise<IStakingInfo[]> {2571 const rawUnstakedPerBlock = await this.helper.callRpc('api.rpc.appPromotion.pendingUnstakePerBlock', [address]);2572 const result = rawUnstakedPerBlock.map(([block, amount]: any[]) => {2573 return {2574 block: block.toBigInt(),2575 amount: amount.toBigInt(),2576 };2577 });2578 return result;2579 }2580}25812582class SchedulerGroup extends HelperGroup<UniqueHelper> {2583 constructor(helper: UniqueHelper) {2584 super(helper);2585 }25862587 cancelScheduled(signer: TSigner, scheduledId: string) {2588 return this.helper.executeExtrinsic(2589 signer,2590 'api.tx.scheduler.cancelNamed',2591 [scheduledId],2592 true,2593 );2594 }25952596 changePriority(signer: TSigner, scheduledId: string, priority: number) {2597 return this.helper.executeExtrinsic(2598 signer,2599 'api.tx.scheduler.changeNamedPriority',2600 [scheduledId, priority],2601 true,2602 );2603 }26042605 scheduleAt<T extends UniqueHelper>(2606 executionBlockNumber: number,2607 options: ISchedulerOptions = {},2608 ) {2609 return this.schedule<T>('schedule', executionBlockNumber, options);2610 }26112612 scheduleAfter<T extends UniqueHelper>(2613 blocksBeforeExecution: number,2614 options: ISchedulerOptions = {},2615 ) {2616 return this.schedule<T>('scheduleAfter', blocksBeforeExecution, options);2617 }26182619 schedule<T extends UniqueHelper>(2620 scheduleFn: 'schedule' | 'scheduleAfter',2621 blocksNum: number,2622 options: ISchedulerOptions = {},2623 ) {2624 // eslint-disable-next-line @typescript-eslint/naming-convention2625 const ScheduledHelperType = ScheduledUniqueHelper(this.helper.helperBase);2626 return this.helper.clone(ScheduledHelperType, {2627 scheduleFn,2628 blocksNum,2629 options,2630 }) as T;2631 }2632}26332634class ForeignAssetsGroup extends HelperGroup<UniqueHelper> {2635 async register(signer: TSigner, ownerAddress: TSubstrateAccount, location: any, metadata: IForeignAssetMetadata) {2636 await this.helper.executeExtrinsic(2637 signer,2638 'api.tx.foreignAssets.registerForeignAsset',2639 [ownerAddress, location, metadata],2640 true,2641 );2642 }26432644 async update(signer: TSigner, foreignAssetId: number, location: any, metadata: IForeignAssetMetadata) {2645 await this.helper.executeExtrinsic(2646 signer,2647 'api.tx.foreignAssets.updateForeignAsset',2648 [foreignAssetId, location, metadata],2649 true,2650 );2651 }2652}26532654class XcmGroup<T extends ChainHelperBase> extends HelperGroup<T> {2655 palletName: string;26562657 constructor(helper: T, palletName: string) {2658 super(helper);26592660 this.palletName = palletName;2661 }26622663 async limitedReserveTransferAssets(signer: TSigner, destination: any, beneficiary: any, assets: any, feeAssetItem: number, weightLimit: number) {2664 await this.helper.executeExtrinsic(signer, `api.tx.${this.palletName}.limitedReserveTransferAssets`, [destination, beneficiary, assets, feeAssetItem, {Limited: weightLimit}], true);2665 }2666}26672668class XTokensGroup<T extends ChainHelperBase> extends HelperGroup<T> {2669 async transfer(signer: TSigner, currencyId: any, amount: bigint, destination: any, destWeight: number) {2670 await this.helper.executeExtrinsic(signer, 'api.tx.xTokens.transfer', [currencyId, amount, destination, destWeight], true);2671 }26722673 async transferMultiasset(signer: TSigner, asset: any, destination: any, destWeight: number) {2674 await this.helper.executeExtrinsic(signer, 'api.tx.xTokens.transferMultiasset', [asset, destination, destWeight], true);2675 }26762677 async transferMulticurrencies(signer: TSigner, currencies: any[], feeItem: number, destLocation: any, destWeight: number) {2678 await this.helper.executeExtrinsic(signer, 'api.tx.xTokens.transferMulticurrencies', [currencies, feeItem, destLocation, destWeight], true);2679 }2680}26812682class TokensGroup<T extends ChainHelperBase> extends HelperGroup<T> {2683 async accounts(address: string, currencyId: any) {2684 const {free} = (await this.helper.callRpc('api.query.tokens.accounts', [address, currencyId])).toJSON() as any;2685 return BigInt(free);2686 }2687}26882689class AssetsGroup<T extends ChainHelperBase> extends HelperGroup<T> {2690 async create(signer: TSigner, assetId: number, admin: string, minimalBalance: bigint) {2691 await this.helper.executeExtrinsic(signer, 'api.tx.assets.create', [assetId, admin, minimalBalance], true);2692 }26932694 async setMetadata(signer: TSigner, assetId: number, name: string, symbol: string, decimals: number) {2695 await this.helper.executeExtrinsic(signer, 'api.tx.assets.setMetadata', [assetId, name, symbol, decimals], true);2696 }26972698 async mint(signer: TSigner, assetId: number, beneficiary: string, amount: bigint) {2699 await this.helper.executeExtrinsic(signer, 'api.tx.assets.mint', [assetId, beneficiary, amount], true);2700 }27012702 async account(assetId: string | number, address: string) {2703 const accountAsset = (2704 await this.helper.callRpc('api.query.assets.account', [assetId, address])2705 ).toJSON()! as any;27062707 if (accountAsset !== null) {2708 return BigInt(accountAsset['balance']);2709 } else {2710 return null;2711 }2712 }2713}27142715class AcalaAssetRegistryGroup extends HelperGroup<AcalaHelper> {2716 async registerForeignAsset(signer: TSigner, destination: any, metadata: AcalaAssetMetadata) {2717 await this.helper.executeExtrinsic(signer, 'api.tx.assetRegistry.registerForeignAsset', [destination, metadata], true);2718 }2719}27202721class MoonbeamAssetManagerGroup extends HelperGroup<MoonbeamHelper> {2722 makeRegisterForeignAssetProposal(assetInfo: MoonbeamAssetInfo) {2723 const apiPrefix = 'api.tx.assetManager.';27242725 const registerTx = this.helper.constructApiCall(2726 apiPrefix + 'registerForeignAsset',2727 [assetInfo.location, assetInfo.metadata, assetInfo.existentialDeposit, assetInfo.isSufficient],2728 );27292730 const setUnitsTx = this.helper.constructApiCall(2731 apiPrefix + 'setAssetUnitsPerSecond',2732 [assetInfo.location, assetInfo.unitsPerSecond, assetInfo.numAssetsWeightHint],2733 );27342735 const batchCall = this.helper.getApi().tx.utility.batchAll([registerTx, setUnitsTx]);2736 const encodedProposal = batchCall?.method.toHex() || '';2737 return encodedProposal;2738 }27392740 async assetTypeId(location: any) {2741 return await this.helper.callRpc('api.query.assetManager.assetTypeId', [location]);2742 }2743}27442745class MoonbeamDemocracyGroup extends HelperGroup<MoonbeamHelper> {2746 async notePreimage(signer: TSigner, encodedProposal: string) {2747 await this.helper.executeExtrinsic(signer, 'api.tx.democracy.notePreimage', [encodedProposal], true);2748 }27492750 externalProposeMajority(proposalHash: string) {2751 return this.helper.constructApiCall('api.tx.democracy.externalProposeMajority', [proposalHash]);2752 }27532754 fastTrack(proposalHash: string, votingPeriod: number, delayPeriod: number) {2755 return this.helper.constructApiCall('api.tx.democracy.fastTrack', [proposalHash, votingPeriod, delayPeriod]);2756 }27572758 async referendumVote(signer: TSigner, referendumIndex: number, accountVote: DemocracyStandardAccountVote) {2759 await this.helper.executeExtrinsic(signer, 'api.tx.democracy.vote', [referendumIndex, {Standard: accountVote}], true);2760 }2761}27622763class MoonbeamCollectiveGroup extends HelperGroup<MoonbeamHelper> {2764 collective: string;27652766 constructor(helper: MoonbeamHelper, collective: string) {2767 super(helper);27682769 this.collective = collective;2770 }27712772 async propose(signer: TSigner, threshold: number, proposalHash: string, lengthBound: number) {2773 await this.helper.executeExtrinsic(signer, `api.tx.${this.collective}.propose`, [threshold, proposalHash, lengthBound], true);2774 }27752776 async vote(signer: TSigner, proposalHash: string, proposalIndex: number, approve: boolean) {2777 await this.helper.executeExtrinsic(signer, `api.tx.${this.collective}.vote`, [proposalHash, proposalIndex, approve], true);2778 }27792780 async close(signer: TSigner, proposalHash: string, proposalIndex: number, weightBound: number, lengthBound: number) {2781 await this.helper.executeExtrinsic(signer, `api.tx.${this.collective}.close`, [proposalHash, proposalIndex, weightBound, lengthBound], true);2782 }27832784 async proposalCount() {2785 return Number(await this.helper.callRpc(`api.query.${this.collective}.proposalCount`, []));2786 }2787}27882789export type ChainHelperBaseConstructor = new(...args: any[]) => ChainHelperBase;2790export type UniqueHelperConstructor = new(...args: any[]) => UniqueHelper;27912792export class UniqueHelper extends ChainHelperBase {2793 balance: BalanceGroup<UniqueHelper>;2794 collection: CollectionGroup;2795 nft: NFTGroup;2796 rft: RFTGroup;2797 ft: FTGroup;2798 staking: StakingGroup;2799 scheduler: SchedulerGroup;2800 foreignAssets: ForeignAssetsGroup;2801 xcm: XcmGroup<UniqueHelper>;2802 xTokens: XTokensGroup<UniqueHelper>;2803 tokens: TokensGroup<UniqueHelper>;28042805 constructor(logger?: ILogger, options: {[key: string]: any} = {}) {2806 super(logger, options.helperBase ?? UniqueHelper);28072808 this.balance = new BalanceGroup(this);2809 this.collection = new CollectionGroup(this);2810 this.nft = new NFTGroup(this);2811 this.rft = new RFTGroup(this);2812 this.ft = new FTGroup(this);2813 this.staking = new StakingGroup(this);2814 this.scheduler = new SchedulerGroup(this);2815 this.foreignAssets = new ForeignAssetsGroup(this);2816 this.xcm = new XcmGroup(this, 'polkadotXcm');2817 this.xTokens = new XTokensGroup(this);2818 this.tokens = new TokensGroup(this);2819 }28202821 getSudo<T extends UniqueHelper>() {2822 // eslint-disable-next-line @typescript-eslint/naming-convention2823 const SudoHelperType = SudoHelper(this.helperBase);2824 return this.clone(SudoHelperType) as T;2825 }2826}28272828export class XcmChainHelper extends ChainHelperBase {2829 async connect(wsEndpoint: string, _listeners?: any): Promise<void> {2830 const wsProvider = new WsProvider(wsEndpoint);2831 this.api = new ApiPromise({2832 provider: wsProvider,2833 });2834 await this.api.isReadyOrError;2835 this.network = await UniqueHelper.detectNetwork(this.api);2836 }2837}28382839export class RelayHelper extends XcmChainHelper {2840 xcm: XcmGroup<RelayHelper>;28412842 constructor(logger?: ILogger, options: {[key: string]: any} = {}) {2843 super(logger, options.helperBase ?? RelayHelper);28442845 this.xcm = new XcmGroup(this, 'xcmPallet');2846 }2847}28482849export class WestmintHelper extends XcmChainHelper {2850 balance: SubstrateBalanceGroup<WestmintHelper>;2851 xcm: XcmGroup<WestmintHelper>;2852 assets: AssetsGroup<WestmintHelper>;2853 xTokens: XTokensGroup<WestmintHelper>;28542855 constructor(logger?: ILogger, options: {[key: string]: any} = {}) {2856 super(logger, options.helperBase ?? WestmintHelper);28572858 this.balance = new SubstrateBalanceGroup(this);2859 this.xcm = new XcmGroup(this, 'polkadotXcm');2860 this.assets = new AssetsGroup(this);2861 this.xTokens = new XTokensGroup(this);2862 }2863}28642865export class MoonbeamHelper extends XcmChainHelper {2866 balance: EthereumBalanceGroup<MoonbeamHelper>;2867 assetManager: MoonbeamAssetManagerGroup;2868 assets: AssetsGroup<MoonbeamHelper>;2869 xTokens: XTokensGroup<MoonbeamHelper>;2870 democracy: MoonbeamDemocracyGroup;2871 collective: {2872 council: MoonbeamCollectiveGroup,2873 techCommittee: MoonbeamCollectiveGroup,2874 };28752876 constructor(logger?: ILogger, options: {[key: string]: any} = {}) {2877 super(logger, options.helperBase ?? MoonbeamHelper);28782879 this.balance = new EthereumBalanceGroup(this);2880 this.assetManager = new MoonbeamAssetManagerGroup(this);2881 this.assets = new AssetsGroup(this);2882 this.xTokens = new XTokensGroup(this);2883 this.democracy = new MoonbeamDemocracyGroup(this);2884 this.collective = {2885 council: new MoonbeamCollectiveGroup(this, 'councilCollective'),2886 techCommittee: new MoonbeamCollectiveGroup(this, 'techCommitteeCollective'),2887 };2888 }2889}28902891export class AcalaHelper extends XcmChainHelper {2892 balance: SubstrateBalanceGroup<AcalaHelper>;2893 assetRegistry: AcalaAssetRegistryGroup;2894 xTokens: XTokensGroup<AcalaHelper>;2895 tokens: TokensGroup<AcalaHelper>;28962897 constructor(logger?: ILogger, options: {[key: string]: any} = {}) {2898 super(logger, options.helperBase ?? AcalaHelper);28992900 this.balance = new SubstrateBalanceGroup(this);2901 this.assetRegistry = new AcalaAssetRegistryGroup(this);2902 this.xTokens = new XTokensGroup(this);2903 this.tokens = new TokensGroup(this);2904 }29052906 getSudo<T extends AcalaHelper>() {2907 // eslint-disable-next-line @typescript-eslint/naming-convention2908 const SudoHelperType = SudoHelper(this.helperBase);2909 return this.clone(SudoHelperType) as T;2910 }2911}29122913// eslint-disable-next-line @typescript-eslint/naming-convention2914function ScheduledUniqueHelper<T extends UniqueHelperConstructor>(Base: T) {2915 return class extends Base {2916 scheduleFn: 'schedule' | 'scheduleAfter';2917 blocksNum: number;2918 options: ISchedulerOptions;29192920 constructor(...args: any[]) {2921 const logger = args[0] as ILogger;2922 const options = args[1] as {2923 scheduleFn: 'schedule' | 'scheduleAfter',2924 blocksNum: number,2925 options: ISchedulerOptions2926 };29272928 super(logger);29292930 this.scheduleFn = options.scheduleFn;2931 this.blocksNum = options.blocksNum;2932 this.options = options.options;2933 }29342935 executeExtrinsic(sender: IKeyringPair, scheduledExtrinsic: string, scheduledParams: any[], expectSuccess?: boolean): Promise<ITransactionResult> {2936 const scheduledTx = this.constructApiCall(scheduledExtrinsic, scheduledParams);2937 2938 const mandatorySchedArgs = [2939 this.blocksNum,2940 this.options.periodic ? [this.options.periodic.period, this.options.periodic.repetitions] : null,2941 this.options.priority ?? null,2942 scheduledTx,2943 ];2944 2945 let schedArgs;2946 let scheduleFn;29472948 if (this.options.scheduledId) {2949 schedArgs = [this.options.scheduledId!, ...mandatorySchedArgs];29502951 if (this.scheduleFn == 'schedule') {2952 scheduleFn = 'scheduleNamed';2953 } else if (this.scheduleFn == 'scheduleAfter') {2954 scheduleFn = 'scheduleNamedAfter';2955 }2956 } else {2957 schedArgs = mandatorySchedArgs;2958 scheduleFn = this.scheduleFn;2959 }29602961 const extrinsic = 'api.tx.scheduler.' + scheduleFn;29622963 return super.executeExtrinsic(2964 sender,2965 extrinsic,2966 schedArgs,2967 expectSuccess,2968 );2969 }2970 };2971}29722973// eslint-disable-next-line @typescript-eslint/naming-convention2974function SudoHelper<T extends ChainHelperBaseConstructor>(Base: T) {2975 return class extends Base {2976 constructor(...args: any[]) {2977 super(...args);2978 }29792980 executeExtrinsic (2981 sender: IKeyringPair,2982 extrinsic: string,2983 params: any[],2984 expectSuccess?: boolean,2985 ): Promise<ITransactionResult> {2986 const call = this.constructApiCall(extrinsic, params);2987 return super.executeExtrinsic(2988 sender,2989 'api.tx.sudo.sudo',2990 [call],2991 expectSuccess,2992 );2993 }2994 };2995}29962997export class UniqueBaseCollection {2998 helper: UniqueHelper;2999 collectionId: number;30003001 constructor(collectionId: number, uniqueHelper: UniqueHelper) {3002 this.collectionId = collectionId;3003 this.helper = uniqueHelper;3004 }30053006 async getData() {3007 return await this.helper.collection.getData(this.collectionId);3008 }30093010 async getLastTokenId() {3011 return await this.helper.collection.getLastTokenId(this.collectionId);3012 }30133014 async doesTokenExist(tokenId: number) {3015 return await this.helper.collection.doesTokenExist(this.collectionId, tokenId);3016 }30173018 async getAdmins() {3019 return await this.helper.collection.getAdmins(this.collectionId);3020 }30213022 async getAllowList() {3023 return await this.helper.collection.getAllowList(this.collectionId);3024 }30253026 async getEffectiveLimits() {3027 return await this.helper.collection.getEffectiveLimits(this.collectionId);3028 }30293030 async getProperties(propertyKeys?: string[] | null) {3031 return await this.helper.collection.getProperties(this.collectionId, propertyKeys);3032 }30333034 async getTokenNextSponsored(tokenId: number, addressObj: ICrossAccountId) {3035 return await this.helper.collection.getTokenNextSponsored(this.collectionId, tokenId, addressObj);3036 }30373038 async getOptions() {3039 return await this.helper.collection.getCollectionOptions(this.collectionId);3040 }30413042 async setSponsor(signer: TSigner, sponsorAddress: TSubstrateAccount) {3043 return await this.helper.collection.setSponsor(signer, this.collectionId, sponsorAddress);3044 }30453046 async confirmSponsorship(signer: TSigner) {3047 return await this.helper.collection.confirmSponsorship(signer, this.collectionId);3048 }30493050 async removeSponsor(signer: TSigner) {3051 return await this.helper.collection.removeSponsor(signer, this.collectionId);3052 }30533054 async setLimits(signer: TSigner, limits: ICollectionLimits) {3055 return await this.helper.collection.setLimits(signer, this.collectionId, limits);3056 }30573058 async changeOwner(signer: TSigner, ownerAddress: TSubstrateAccount) {3059 return await this.helper.collection.changeOwner(signer, this.collectionId, ownerAddress);3060 }30613062 async addAdmin(signer: TSigner, adminAddressObj: ICrossAccountId) {3063 return await this.helper.collection.addAdmin(signer, this.collectionId, adminAddressObj);3064 }30653066 async addToAllowList(signer: TSigner, addressObj: ICrossAccountId) {3067 return await this.helper.collection.addToAllowList(signer, this.collectionId, addressObj);3068 }30693070 async removeFromAllowList(signer: TSigner, addressObj: ICrossAccountId) {3071 return await this.helper.collection.removeFromAllowList(signer, this.collectionId, addressObj);3072 }30733074 async removeAdmin(signer: TSigner, adminAddressObj: ICrossAccountId) {3075 return await this.helper.collection.removeAdmin(signer, this.collectionId, adminAddressObj);3076 }30773078 async setProperties(signer: TSigner, properties: IProperty[]) {3079 return await this.helper.collection.setProperties(signer, this.collectionId, properties);3080 }30813082 async deleteProperties(signer: TSigner, propertyKeys: string[]) {3083 return await this.helper.collection.deleteProperties(signer, this.collectionId, propertyKeys);3084 }30853086 async setPermissions(signer: TSigner, permissions: ICollectionPermissions) {3087 return await this.helper.collection.setPermissions(signer, this.collectionId, permissions);3088 }30893090 async enableNesting(signer: TSigner, permissions: INestingPermissions) {3091 return await this.helper.collection.enableNesting(signer, this.collectionId, permissions);3092 }30933094 async disableNesting(signer: TSigner) {3095 return await this.helper.collection.disableNesting(signer, this.collectionId);3096 }30973098 async burn(signer: TSigner) {3099 return await this.helper.collection.burn(signer, this.collectionId);3100 }31013102 scheduleAt<T extends UniqueHelper>(3103 executionBlockNumber: number,3104 options: ISchedulerOptions = {},3105 ) {3106 const scheduledHelper = this.helper.scheduler.scheduleAt<T>(executionBlockNumber, options);3107 return new UniqueBaseCollection(this.collectionId, scheduledHelper);3108 }31093110 scheduleAfter<T extends UniqueHelper>(3111 blocksBeforeExecution: number,3112 options: ISchedulerOptions = {},3113 ) {3114 const scheduledHelper = this.helper.scheduler.scheduleAfter<T>(blocksBeforeExecution, options);3115 return new UniqueBaseCollection(this.collectionId, scheduledHelper);3116 }31173118 getSudo<T extends UniqueHelper>() {3119 return new UniqueBaseCollection(this.collectionId, this.helper.getSudo<T>());3120 }3121}312231233124export class UniqueNFTCollection extends UniqueBaseCollection {3125 getTokenObject(tokenId: number) {3126 return new UniqueNFToken(tokenId, this);3127 }31283129 async getTokensByAddress(addressObj: ICrossAccountId) {3130 return await this.helper.nft.getTokensByAddress(this.collectionId, addressObj);3131 }31323133 async getToken(tokenId: number, blockHashAt?: string) {3134 return await this.helper.nft.getToken(this.collectionId, tokenId, [], blockHashAt);3135 }31363137 async getTokenOwner(tokenId: number, blockHashAt?: string) {3138 return await this.helper.nft.getTokenOwner(this.collectionId, tokenId, blockHashAt);3139 }31403141 async getTokenTopmostOwner(tokenId: number, blockHashAt?: string) {3142 return await this.helper.nft.getTokenTopmostOwner(this.collectionId, tokenId, blockHashAt);3143 }31443145 async getTokenChildren(tokenId: number, blockHashAt?: string) {3146 return await this.helper.nft.getTokenChildren(this.collectionId, tokenId, blockHashAt);3147 }31483149 async getPropertyPermissions(propertyKeys: string[] | null = null) {3150 return await this.helper.nft.getPropertyPermissions(this.collectionId, propertyKeys);3151 }31523153 async getTokenProperties(tokenId: number, propertyKeys?: string[] | null) {3154 return await this.helper.nft.getTokenProperties(this.collectionId, tokenId, propertyKeys);3155 }31563157 async transferToken(signer: TSigner, tokenId: number, addressObj: ICrossAccountId) {3158 return await this.helper.nft.transferToken(signer, this.collectionId, tokenId, addressObj);3159 }31603161 async transferTokenFrom(signer: TSigner, tokenId: number, fromAddressObj: ICrossAccountId, toAddressObj: ICrossAccountId) {3162 return await this.helper.nft.transferTokenFrom(signer, this.collectionId, tokenId, fromAddressObj, toAddressObj);3163 }31643165 async approveToken(signer: TSigner, tokenId: number, toAddressObj: ICrossAccountId) {3166 return await this.helper.nft.approveToken(signer, this.collectionId, tokenId, toAddressObj);3167 }31683169 async isTokenApproved(tokenId: number, toAddressObj: ICrossAccountId) {3170 return await this.helper.nft.isTokenApproved(this.collectionId, tokenId, toAddressObj);3171 }31723173 async mintToken(signer: TSigner, owner: ICrossAccountId = {Substrate: signer.address}, properties?: IProperty[]) {3174 return await this.helper.nft.mintToken(signer, {collectionId: this.collectionId, owner, properties});3175 }31763177 async mintMultipleTokens(signer: TSigner, tokens: {owner: ICrossAccountId, properties?: IProperty[]}[]) {3178 return await this.helper.nft.mintMultipleTokens(signer, this.collectionId, tokens);3179 }31803181 async burnToken(signer: TSigner, tokenId: number) {3182 return await this.helper.nft.burnToken(signer, this.collectionId, tokenId);3183 }31843185 async burnTokenFrom(signer: TSigner, tokenId: number, fromAddressObj: ICrossAccountId) {3186 return await this.helper.nft.burnTokenFrom(signer, this.collectionId, tokenId, fromAddressObj);3187 }31883189 async setTokenProperties(signer: TSigner, tokenId: number, properties: IProperty[]) {3190 return await this.helper.nft.setTokenProperties(signer, this.collectionId, tokenId, properties);3191 }31923193 async deleteTokenProperties(signer: TSigner, tokenId: number, propertyKeys: string[]) {3194 return await this.helper.nft.deleteTokenProperties(signer, this.collectionId, tokenId, propertyKeys);3195 }31963197 async setTokenPropertyPermissions(signer: TSigner, permissions: ITokenPropertyPermission[]) {3198 return await this.helper.nft.setTokenPropertyPermissions(signer, this.collectionId, permissions);3199 }32003201 async nestToken(signer: TSigner, tokenId: number, toTokenObj: IToken) {3202 return await this.helper.nft.nestToken(signer, {collectionId: this.collectionId, tokenId}, toTokenObj);3203 }32043205 async unnestToken(signer: TSigner, tokenId: number, fromTokenObj: IToken, toAddressObj: ICrossAccountId) {3206 return await this.helper.nft.unnestToken(signer, {collectionId: this.collectionId, tokenId}, fromTokenObj, toAddressObj);3207 }32083209 scheduleAt<T extends UniqueHelper>(3210 executionBlockNumber: number,3211 options: ISchedulerOptions = {},3212 ) {3213 const scheduledHelper = this.helper.scheduler.scheduleAt<T>(executionBlockNumber, options);3214 return new UniqueNFTCollection(this.collectionId, scheduledHelper);3215 }32163217 scheduleAfter<T extends UniqueHelper>(3218 blocksBeforeExecution: number,3219 options: ISchedulerOptions = {},3220 ) {3221 const scheduledHelper = this.helper.scheduler.scheduleAfter<T>(blocksBeforeExecution, options);3222 return new UniqueNFTCollection(this.collectionId, scheduledHelper);3223 }32243225 getSudo<T extends UniqueHelper>() {3226 return new UniqueNFTCollection(this.collectionId, this.helper.getSudo<T>());3227 }3228}322932303231export class UniqueRFTCollection extends UniqueBaseCollection {3232 getTokenObject(tokenId: number) {3233 return new UniqueRFToken(tokenId, this);3234 }32353236 async getToken(tokenId: number, blockHashAt?: string) {3237 return await this.helper.rft.getToken(this.collectionId, tokenId, [], blockHashAt);3238 }32393240 async getTokensByAddress(addressObj: ICrossAccountId) {3241 return await this.helper.rft.getTokensByAddress(this.collectionId, addressObj);3242 }32433244 async getTop10TokenOwners(tokenId: number) {3245 return await this.helper.rft.getTokenTop10Owners(this.collectionId, tokenId);3246 }32473248 async getTokenBalance(tokenId: number, addressObj: ICrossAccountId) {3249 return await this.helper.rft.getTokenBalance(this.collectionId, tokenId, addressObj);3250 }32513252 async getTokenTotalPieces(tokenId: number) {3253 return await this.helper.rft.getTokenTotalPieces(this.collectionId, tokenId);3254 }32553256 async getTokenApprovedPieces(tokenId: number, fromAddressObj: ICrossAccountId, toAddressObj: ICrossAccountId) {3257 return await this.helper.rft.getTokenApprovedPieces(this.collectionId, tokenId, toAddressObj, fromAddressObj);3258 }32593260 async getPropertyPermissions(propertyKeys: string[] | null = null) {3261 return await this.helper.rft.getPropertyPermissions(this.collectionId, propertyKeys);3262 }32633264 async getTokenProperties(tokenId: number, propertyKeys?: string[] | null) {3265 return await this.helper.rft.getTokenProperties(this.collectionId, tokenId, propertyKeys);3266 }32673268 async transferToken(signer: TSigner, tokenId: number, addressObj: ICrossAccountId, amount=1n) {3269 return await this.helper.rft.transferToken(signer, this.collectionId, tokenId, addressObj, amount);3270 }32713272 async transferTokenFrom(signer: TSigner, tokenId: number, fromAddressObj: ICrossAccountId, toAddressObj: ICrossAccountId, amount=1n) {3273 return await this.helper.rft.transferTokenFrom(signer, this.collectionId, tokenId, fromAddressObj, toAddressObj, amount);3274 }32753276 async approveToken(signer: TSigner, tokenId: number, toAddressObj: ICrossAccountId, amount=1n) {3277 return await this.helper.rft.approveToken(signer, this.collectionId, tokenId, toAddressObj, amount);3278 }32793280 async repartitionToken(signer: TSigner, tokenId: number, amount: bigint) {3281 return await this.helper.rft.repartitionToken(signer, this.collectionId, tokenId, amount);3282 }32833284 async mintToken(signer: TSigner, pieces=1n, owner: ICrossAccountId = {Substrate: signer.address}, properties?: IProperty[]) {3285 return await this.helper.rft.mintToken(signer, {collectionId: this.collectionId, owner, pieces, properties});3286 }32873288 async mintMultipleTokens(signer: TSigner, tokens: {pieces: bigint, owner: ICrossAccountId, properties?: IProperty[]}[]) {3289 return await this.helper.rft.mintMultipleTokens(signer, this.collectionId, tokens);3290 }32913292 async burnToken(signer: TSigner, tokenId: number, amount=1n) {3293 return await this.helper.rft.burnToken(signer, this.collectionId, tokenId, amount);3294 }32953296 async burnTokenFrom(signer: TSigner, tokenId: number, fromAddressObj: ICrossAccountId, amount=1n) {3297 return await this.helper.rft.burnTokenFrom(signer, this.collectionId, tokenId, fromAddressObj, amount);3298 }32993300 async setTokenProperties(signer: TSigner, tokenId: number, properties: IProperty[]) {3301 return await this.helper.rft.setTokenProperties(signer, this.collectionId, tokenId, properties);3302 }33033304 async deleteTokenProperties(signer: TSigner, tokenId: number, propertyKeys: string[]) {3305 return await this.helper.rft.deleteTokenProperties(signer, this.collectionId, tokenId, propertyKeys);3306 }33073308 async setTokenPropertyPermissions(signer: TSigner, permissions: ITokenPropertyPermission[]) {3309 return await this.helper.rft.setTokenPropertyPermissions(signer, this.collectionId, permissions);3310 }33113312 scheduleAt<T extends UniqueHelper>(3313 executionBlockNumber: number,3314 options: ISchedulerOptions = {},3315 ) {3316 const scheduledHelper = this.helper.scheduler.scheduleAt<T>(executionBlockNumber, options);3317 return new UniqueRFTCollection(this.collectionId, scheduledHelper);3318 }33193320 scheduleAfter<T extends UniqueHelper>(3321 blocksBeforeExecution: number,3322 options: ISchedulerOptions = {},3323 ) {3324 const scheduledHelper = this.helper.scheduler.scheduleAfter<T>(blocksBeforeExecution, options);3325 return new UniqueRFTCollection(this.collectionId, scheduledHelper);3326 }33273328 getSudo<T extends UniqueHelper>() {3329 return new UniqueRFTCollection(this.collectionId, this.helper.getSudo<T>());3330 }3331}333233333334export class UniqueFTCollection extends UniqueBaseCollection {3335 async getBalance(addressObj: ICrossAccountId) {3336 return await this.helper.ft.getBalance(this.collectionId, addressObj);3337 }33383339 async getTotalPieces() {3340 return await this.helper.ft.getTotalPieces(this.collectionId);3341 }33423343 async getApprovedTokens(fromAddressObj: ICrossAccountId, toAddressObj: ICrossAccountId) {3344 return await this.helper.ft.getApprovedTokens(this.collectionId, fromAddressObj, toAddressObj);3345 }33463347 async getTop10Owners() {3348 return await this.helper.ft.getTop10Owners(this.collectionId);3349 }33503351 async mint(signer: TSigner, amount=1n, owner: ICrossAccountId = {Substrate: signer.address}) {3352 return await this.helper.ft.mintTokens(signer, this.collectionId, amount, owner);3353 }33543355 async mintWithOneOwner(signer: TSigner, tokens: {value: bigint}[], owner: ICrossAccountId = {Substrate: signer.address}) {3356 return await this.helper.ft.mintMultipleTokensWithOneOwner(signer, this.collectionId, tokens, owner);3357 }33583359 async transfer(signer: TSigner, toAddressObj: ICrossAccountId, amount=1n) {3360 return await this.helper.ft.transfer(signer, this.collectionId, toAddressObj, amount);3361 }33623363 async transferFrom(signer: TSigner, fromAddressObj: ICrossAccountId, toAddressObj: ICrossAccountId, amount=1n) {3364 return await this.helper.ft.transferFrom(signer, this.collectionId, fromAddressObj, toAddressObj, amount);3365 }33663367 async burnTokens(signer: TSigner, amount=1n) {3368 return await this.helper.ft.burnTokens(signer, this.collectionId, amount);3369 }33703371 async burnTokensFrom(signer: TSigner, fromAddressObj: ICrossAccountId, amount=1n) {3372 return await this.helper.ft.burnTokensFrom(signer, this.collectionId, fromAddressObj, amount);3373 }33743375 async approveTokens(signer: TSigner, toAddressObj: ICrossAccountId, amount=1n) {3376 return await this.helper.ft.approveTokens(signer, this.collectionId, toAddressObj, amount);3377 }33783379 scheduleAt<T extends UniqueHelper>(3380 executionBlockNumber: number,3381 options: ISchedulerOptions = {},3382 ) {3383 const scheduledHelper = this.helper.scheduler.scheduleAt<T>(executionBlockNumber, options);3384 return new UniqueFTCollection(this.collectionId, scheduledHelper);3385 }33863387 scheduleAfter<T extends UniqueHelper>(3388 blocksBeforeExecution: number,3389 options: ISchedulerOptions = {},3390 ) {3391 const scheduledHelper = this.helper.scheduler.scheduleAfter<T>(blocksBeforeExecution, options);3392 return new UniqueFTCollection(this.collectionId, scheduledHelper);3393 }33943395 getSudo<T extends UniqueHelper>() {3396 return new UniqueFTCollection(this.collectionId, this.helper.getSudo<T>());3397 }3398}339934003401export class UniqueBaseToken {3402 collection: UniqueNFTCollection | UniqueRFTCollection;3403 collectionId: number;3404 tokenId: number;34053406 constructor(tokenId: number, collection: UniqueNFTCollection | UniqueRFTCollection) {3407 this.collection = collection;3408 this.collectionId = collection.collectionId;3409 this.tokenId = tokenId;3410 }34113412 async getNextSponsored(addressObj: ICrossAccountId) {3413 return await this.collection.getTokenNextSponsored(this.tokenId, addressObj);3414 }34153416 async getProperties(propertyKeys?: string[] | null) {3417 return await this.collection.getTokenProperties(this.tokenId, propertyKeys);3418 }34193420 async setProperties(signer: TSigner, properties: IProperty[]) {3421 return await this.collection.setTokenProperties(signer, this.tokenId, properties);3422 }34233424 async deleteProperties(signer: TSigner, propertyKeys: string[]) {3425 return await this.collection.deleteTokenProperties(signer, this.tokenId, propertyKeys);3426 }34273428 async doesExist() {3429 return await this.collection.doesTokenExist(this.tokenId);3430 }34313432 nestingAccount() {3433 return this.collection.helper.util.getTokenAccount(this);3434 }34353436 scheduleAt<T extends UniqueHelper>(3437 executionBlockNumber: number,3438 options: ISchedulerOptions = {},3439 ) {3440 const scheduledCollection = this.collection.scheduleAt<T>(executionBlockNumber, options);3441 return new UniqueBaseToken(this.tokenId, scheduledCollection);3442 }34433444 scheduleAfter<T extends UniqueHelper>(3445 blocksBeforeExecution: number,3446 options: ISchedulerOptions = {},3447 ) {3448 const scheduledCollection = this.collection.scheduleAfter<T>(blocksBeforeExecution, options);3449 return new UniqueBaseToken(this.tokenId, scheduledCollection);3450 }34513452 getSudo<T extends UniqueHelper>() {3453 return new UniqueBaseToken(this.tokenId, this.collection.getSudo<T>());3454 }3455}345634573458export class UniqueNFToken extends UniqueBaseToken {3459 collection: UniqueNFTCollection;34603461 constructor(tokenId: number, collection: UniqueNFTCollection) {3462 super(tokenId, collection);3463 this.collection = collection;3464 }34653466 async getData(blockHashAt?: string) {3467 return await this.collection.getToken(this.tokenId, blockHashAt);3468 }34693470 async getOwner(blockHashAt?: string) {3471 return await this.collection.getTokenOwner(this.tokenId, blockHashAt);3472 }34733474 async getTopmostOwner(blockHashAt?: string) {3475 return await this.collection.getTokenTopmostOwner(this.tokenId, blockHashAt);3476 }34773478 async getChildren(blockHashAt?: string) {3479 return await this.collection.getTokenChildren(this.tokenId, blockHashAt);3480 }34813482 async nest(signer: TSigner, toTokenObj: IToken) {3483 return await this.collection.nestToken(signer, this.tokenId, toTokenObj);3484 }34853486 async unnest(signer: TSigner, fromTokenObj: IToken, toAddressObj: ICrossAccountId) {3487 return await this.collection.unnestToken(signer, this.tokenId, fromTokenObj, toAddressObj);3488 }34893490 async transfer(signer: TSigner, addressObj: ICrossAccountId) {3491 return await this.collection.transferToken(signer, this.tokenId, addressObj);3492 }34933494 async transferFrom(signer: TSigner, fromAddressObj: ICrossAccountId, toAddressObj: ICrossAccountId) {3495 return await this.collection.transferTokenFrom(signer, this.tokenId, fromAddressObj, toAddressObj);3496 }34973498 async approve(signer: TSigner, toAddressObj: ICrossAccountId) {3499 return await this.collection.approveToken(signer, this.tokenId, toAddressObj);3500 }35013502 async isApproved(toAddressObj: ICrossAccountId) {3503 return await this.collection.isTokenApproved(this.tokenId, toAddressObj);3504 }35053506 async burn(signer: TSigner) {3507 return await this.collection.burnToken(signer, this.tokenId);3508 }35093510 async burnFrom(signer: TSigner, fromAddressObj: ICrossAccountId) {3511 return await this.collection.burnTokenFrom(signer, this.tokenId, fromAddressObj);3512 }35133514 scheduleAt<T extends UniqueHelper>(3515 executionBlockNumber: number,3516 options: ISchedulerOptions = {},3517 ) {3518 const scheduledCollection = this.collection.scheduleAt<T>(executionBlockNumber, options);3519 return new UniqueNFToken(this.tokenId, scheduledCollection);3520 }35213522 scheduleAfter<T extends UniqueHelper>(3523 blocksBeforeExecution: number,3524 options: ISchedulerOptions = {},3525 ) {3526 const scheduledCollection = this.collection.scheduleAfter<T>(blocksBeforeExecution, options);3527 return new UniqueNFToken(this.tokenId, scheduledCollection);3528 }35293530 getSudo<T extends UniqueHelper>() {3531 return new UniqueNFToken(this.tokenId, this.collection.getSudo<T>());3532 }3533}35343535export class UniqueRFToken extends UniqueBaseToken {3536 collection: UniqueRFTCollection;35373538 constructor(tokenId: number, collection: UniqueRFTCollection) {3539 super(tokenId, collection);3540 this.collection = collection;3541 }35423543 async getData(blockHashAt?: string) {3544 return await this.collection.getToken(this.tokenId, blockHashAt);3545 }35463547 async getTop10Owners() {3548 return await this.collection.getTop10TokenOwners(this.tokenId);3549 }35503551 async getBalance(addressObj: ICrossAccountId) {3552 return await this.collection.getTokenBalance(this.tokenId, addressObj);3553 }35543555 async getTotalPieces() {3556 return await this.collection.getTokenTotalPieces(this.tokenId);3557 }35583559 async getApprovedPieces(fromAddressObj: ICrossAccountId, toAccountObj: ICrossAccountId) {3560 return await this.collection.getTokenApprovedPieces(this.tokenId, fromAddressObj, toAccountObj);3561 }35623563 async transfer(signer: TSigner, addressObj: ICrossAccountId, amount=1n) {3564 return await this.collection.transferToken(signer, this.tokenId, addressObj, amount);3565 }35663567 async transferFrom(signer: TSigner, fromAddressObj: ICrossAccountId, toAddressObj: ICrossAccountId, amount=1n) {3568 return await this.collection.transferTokenFrom(signer, this.tokenId, fromAddressObj, toAddressObj, amount);3569 }35703571 async approve(signer: TSigner, toAddressObj: ICrossAccountId, amount=1n) {3572 return await this.collection.approveToken(signer, this.tokenId, toAddressObj, amount);3573 }35743575 async repartition(signer: TSigner, amount: bigint) {3576 return await this.collection.repartitionToken(signer, this.tokenId, amount);3577 }35783579 async burn(signer: TSigner, amount=1n) {3580 return await this.collection.burnToken(signer, this.tokenId, amount);3581 }35823583 async burnFrom(signer: TSigner, fromAddressObj: ICrossAccountId, amount=1n) {3584 return await this.collection.burnTokenFrom(signer, this.tokenId, fromAddressObj, amount);3585 }35863587 scheduleAt<T extends UniqueHelper>(3588 executionBlockNumber: number,3589 options: ISchedulerOptions = {},3590 ) {3591 const scheduledCollection = this.collection.scheduleAt<T>(executionBlockNumber, options);3592 return new UniqueRFToken(this.tokenId, scheduledCollection);3593 }35943595 scheduleAfter<T extends UniqueHelper>(3596 blocksBeforeExecution: number,3597 options: ISchedulerOptions = {},3598 ) {3599 const scheduledCollection = this.collection.scheduleAfter<T>(blocksBeforeExecution, options);3600 return new UniqueRFToken(this.tokenId, scheduledCollection);3601 }36023603 getSudo<T extends UniqueHelper>() {3604 return new UniqueRFToken(this.tokenId, this.collection.getSudo<T>());3605 }3606}