UNPKG

2.35 kBPlain TextView Raw
1import { CollectionTraits, Trait, TraitType } from './types'
2// @ts-ignore ts error TS7016
3import Web3 from 'web3'
4import { Network } from 'opensea-js/lib'
5
6export const formatTraitType = (traitType: string) =>
7 traitType.replace(/_/g, ' ')
8
9const isBoost = (trait: Trait) =>
10 trait.display_type && trait.display_type.includes('boost')
11
12const isRanking = (trait: Trait, collectionTraits: CollectionTraits) =>
13 trait.display_type === null &&
14 trait.trait_type in collectionTraits &&
15 'max' in collectionTraits[trait.trait_type]
16
17/**
18 * IsStat - Checks to see if the given trait is a 'Stat'
19 * A 'Stat' is defined as any trait that has a `display_type` of 'number'
20 *
21 * @param trait - The object containing an asset's trait
22 * @return true if the trait is a 'Stat' and false otherwise
23 */
24const isStat = (trait: Trait) => trait.display_type === 'number'
25
26/**
27 * IsProperty - Checks to see if the given trait is a 'Property'.
28 * A 'Property' is defined as any trait that has a `display_type` of null
29 * and does not have a min/max value
30 *
31 * @param trait - The object containing an asset's trait
32 * @param collectionTraits - List of collection traits
33 * @return true if the trait is a 'Property' and false otherwise
34 */
35const isProperty = (trait: Trait, collectionTraits: CollectionTraits) =>
36 (trait.display_type === null &&
37 trait.trait_type in collectionTraits &&
38 !('max' in collectionTraits[trait.trait_type])) ||
39 !(trait.trait_type in collectionTraits)
40
41export const toBaseDenomination = (value: number, decimals: number) =>
42 +value.toFixed() / Math.pow(10, decimals)
43
44export const getTraitType = (
45 trait: Trait,
46 collectionTraits: CollectionTraits
47) => {
48 if (isProperty(trait, collectionTraits)) {
49 return TraitType.Property
50 }
51 if (isRanking(trait, collectionTraits)) {
52 return TraitType.Ranking
53 }
54 if (isStat(trait)) {
55 return TraitType.Stat
56 }
57 if (isBoost(trait)) {
58 return TraitType.Boost
59 }
60 return null // Default return statement
61}
62
63export const getProvider = () =>
64 (Web3.givenProvider || new Web3.providers.HttpProvider('https://mainnet.infura.io'))
65
66export const networkFromString = (name: string) => {
67 switch (name) {
68 case 'rinkeby':
69 case 'testnet':
70 case 'testnets':
71 return Network.Rinkeby
72 case 'mainnet':
73 case 'main':
74 default:
75 return Network.Main
76 }
77}