This repository has been archived by the owner on May 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMetadataParser.ts
96 lines (81 loc) · 2.95 KB
/
MetadataParser.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import Drive from '@ioc:Adonis/Core/Drive'
import MetadataDefinition from './metadata.json'
import { Attribute, MetadataProvenance, ContractMetadata, TokenMetadata } from './MetadataTypes'
/**
* A class for parsing metadata and metadata provenance.
*/
export default class MetadataParser {
metadata: MetadataProvenance
/**
* Initialize the MetadataParser with metadata from a JSON file or a default template
*
* @returns {Promise} A promise that resolves to an instance of MetadataParser
*/
public async initialize (): Promise<MetadataParser> {
let metadata: MetadataProvenance = MetadataDefinition
// Read config file from s3 storage. Currently disabled.
if (false && await Drive.exists('metadata.json')) {
metadata = JSON.parse((await Drive.get('metadata.json')).toString())
}
this.metadata = metadata
return this
}
/**
* Get the contract metadata
*
* @returns {Promise} A promise that resolves to the contract metadata
*/
public async contract (): Promise<ContractMetadata> {
return this.metadata.contract
}
/**
* Get the base token metadata
*
* @returns {Promise} A promise that resolves to the base metadata
*/
public async base (): Promise<TokenMetadata> {
return this.metadata.base
}
/**
* Get the metadata for a specified tokenID
*
* @param {String|Number} id The ID of the token to provide metadata for
* @returns {Promise} A promise that resolves to the metadata for the specified ID
*/
public async forId (id: string|number): Promise<TokenMetadata> {
const tokenDefinition = this.metadata.tokens[id]
const isOneOfOne = typeof tokenDefinition === 'object'
const isEditioned = typeof tokenDefinition === 'string'
const isUnRevealed = isEditioned && tokenDefinition.startsWith('rare-')
const definition = isOneOfOne
? tokenDefinition
: isEditioned
? this.metadata.editions[tokenDefinition]
: this.metadata.base
const name = this.getAttribute('name', definition) as string
return {
name: isOneOfOne ? name : name + ` ${id}`,
description: this.getAttribute('description', definition) as string,
image: this.getAttribute('image', definition) as string,
image_dark: this.getAttribute('image_dark', definition) as string,
animation_url: this.getAttribute('animation_url', definition) as string,
embed_url: this.getAttribute('embed_url', definition) as string,
download_url: this.getAttribute('download_url', definition) as string,
attributes: [
...this.getAttribute('attributes', definition) as Attribute[],
{
trait_type: 'Revealed',
value: isUnRevealed ? 'No' : 'Yes'
},
{
trait_type: 'Number',
value: parseInt(`${id}`)
}
],
}
}
getAttribute (attribute: keyof TokenMetadata, bag: TokenMetadata) {
if (bag[attribute]) return bag[attribute]
return this.metadata.base[attribute]
}
}