-
-
Notifications
You must be signed in to change notification settings - Fork 366
/
Copy pathutils.ts
37 lines (29 loc) · 1.01 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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)
}