git.delta.rocks / unique-network / refs/commits / a848b6b044bc

difftreelog

source

tests/src/rmrk/util/fetch.ts3.8 KiBsourcehistory
1import {ApiPromise} from '@polkadot/api';2import type {Option} from '@polkadot/types-codec';3import type {4  RmrkTraitsCollectionCollectionInfo as Collection,5  RmrkTraitsNftNftInfo as Nft,6  RmrkTraitsResourceResourceInfo as Resource,7  RmrkTraitsBaseBaseInfo as Base,8  RmrkTraitsPartPartType as PartType,9  RmrkTraitsNftNftChild as NftChild,10  RmrkTraitsTheme as Theme,11  RmrkTraitsPropertyPropertyInfo as Property,12} from '../../interfaces/default/types'; // '@polkadot/types/lookup';13import '../../interfaces/augment-api';14import '../../interfaces/augment-api-query';15import privateKey from '../../substrate/privateKey';1617export type NftIdTuple = [number, number];1819export async function getCollectionsCount(api: ApiPromise): Promise<number> {20  return (await api.rpc.rmrk.lastCollectionIdx()).toNumber();21}2223export async function getCollection(api: ApiPromise, id: number): Promise<Option<Collection>> {24  return api.rpc.rmrk.collectionById(id);25}2627export async function getOwnedNfts(28  api: ApiPromise,29  ownerUri: string,30  collectionId: number,31): Promise<number[]> {32  const ss58Format = api.registry.getChainProperties()!.toJSON().ss58Format;33  const owner = privateKey(ownerUri, Number(ss58Format));3435  return (await api.rpc.rmrk.accountTokens(owner.address, collectionId))36    .map((value) => value.toNumber());37}3839export async function getNft(api: ApiPromise, collectionId: number, nftId: number): Promise<Option<Nft>> {40  return api.rpc.rmrk.nftById(collectionId, nftId);41}4243export async function getCollectionProperties(api: ApiPromise, collectionId: number): Promise<Property[]> {44  return (await api.rpc.rmrk.collectionProperties(collectionId)).toArray();45}4647export async function getNftProperties(api: ApiPromise, collectionId: number, nftId: number): Promise<Property[]> {48  return (await api.rpc.rmrk.nftProperties(collectionId, nftId)).toArray();49}5051export async function getChildren(52  api: ApiPromise,53  collectionId: number,54  nftId: number,55): Promise<NftChild[]> {56  return (await api.rpc.rmrk.nftChildren(collectionId, nftId)).toArray();57}5859export async function getBase(api: ApiPromise, baseId: number): Promise<Option<Base>> {60  return api.rpc.rmrk.base(baseId);61}6263export async function getParts(api: ApiPromise, baseId: number): Promise<PartType[]> {64  return (await api.rpc.rmrk.baseParts(baseId)).toArray();65}6667export async function getEquippableList(68  api: ApiPromise,69  baseId: number,70  slotId: number,71): Promise<'All' | 'Empty' | { 'Custom': number[] } | null> {72  const parts = await getParts(api, baseId);7374  const part = parts.find((part) => {75    if (part.isSlotPart) {76      return part.asSlotPart.id.toNumber() === slotId;77    } else {78      return false;79    }80  });8182  if (part) {83    const slot = part.asSlotPart;84    if (slot.equippable.isCustom) {85      return {86        'Custom': slot.equippable.asCustom87          .toArray()88          .map((collectionId) => collectionId.toNumber()),89      };90    } else if (slot.equippable.isAll) {91      return 'All';92    } else {93      return 'Empty';94    }95  } else {96    return null;97  }98}99100export async function getResourcePriority(101  api: ApiPromise,102  collectionId: number,103  nftId: number,104  resourceId: number,105): Promise<number> {106  return (await api.rpc.rmrk.nftResourcePriority(collectionId, nftId, resourceId))107    .unwrap().toNumber();108}109110export async function getThemeNames(api: ApiPromise, baseId: number): Promise<string[]> {111  return (await api.rpc.rmrk.themeNames(baseId))112    .map((name) => name.toUtf8());113}114115export async function getTheme(116  api: ApiPromise,117  baseId: number,118  themeName: string,119  keys: string[] | null = null,120): Promise<Option<Theme>> {121  return api.rpc.rmrk.themes(baseId, themeName, keys);122}123124export async function getResources(125  api: ApiPromise,126  collectionId: number,127  nftId: number,128): Promise<Resource[]> {129  return (await api.rpc.rmrk.nftResources(collectionId, nftId)).toArray();130}