kodadot/nft-gallery

View on GitHub
components/unique/utils.ts

Summary

Maintainability
A
0 mins
Test Coverage
import BN from 'bn.js'
import { destructTokenId } from '@/utils/gallery/abstractCalls'

const LEGACY_PREFIX = /^u-/

export const tokenIdToRoute = (
  tokenId: string,
): { id: string, item: string } => {
  const { collectionId: id, tokenId: item } = destructTokenId(tokenId)

  return {
    id,
    item,
  }
}

export const correctId = (id: string): string => id.replace(LEGACY_PREFIX, '')

export const isLegacy = (id: string): boolean => LEGACY_PREFIX.test(id)

export const createTokenId = (collection: string, id: string): string =>
  `${collection}-${id}`

export const getRandomValues = (length: number): number[] => {
  const values = new Uint32Array(length)
  window.crypto.getRandomValues(values)
  return Array.from(values)
}

export const hasEnoughToken = (balance: string, ...fees: string[]): boolean => {
  const balanceAmount = new BN(balance)
  const zero = new BN(0)
  const feesAmount = fees.map(fee => new BN(fee))
  return feesAmount
    .reduce((total, fee) => total.add(fee), zero)
    .lt(balanceAmount)
}