-
Notifications
You must be signed in to change notification settings - Fork 1
/
erc721metadata.go
116 lines (99 loc) · 3.27 KB
/
erc721metadata.go
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package erc721metadata
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
)
// ERC721Metadata is ERC721 Metadata JSON
type ERC721Metadata struct {
// ERC721 EIP1047
Name string `json:"name"`
Description string `json:"description,omitempty"`
Image string `json:"image,omitempty"`
// Extra
Timestamp int64 `json:"timestamp,omitempty"`
Language string `json:"language,omitempty"`
// OpenSea
Attributes *json.RawMessage `json:"attributes,omitempty"` // EIPs#1071
ExternalURL string `json:"external_url,omitempty"`
BackgroundColor string `json:"background_color,omitempty"`
AnimationURL string `json:"animation_url,omitempty"`
YoutubeURL string `json:"youtube_url,omitempty"`
// Rare Bits
ImageURL string `json:"image_url,omitempty"`
HomeURL string `json:"home_url,omitempty"`
Tags []string `json:"tags,omitempty"`
Properties []*RareBitsProperty `json:"properties,omitempty"`
// MCH
ExtraData map[string]interface{} `json:"extra_data,omitempty"`
}
// OpenSeaAttributes is attrebute object defined by OpenSea
type OpenSeaAttributes struct {
TraitType string `json:"trait_type"`
Value interface{} `json:"value"`
DisplayType string `json:"display_type,omitempty"`
MaxValue int64 `json:"max_value,omitempty"`
}
// RareBitsProperty is property object defined by RareBits
type RareBitsProperty struct {
Key string `json:"key"`
Value interface{} `json:"value"`
Type string `json:"type"`
}
// FetchERC721Metadata returns *ERC721Metadata fetch Metadata from tokenURI
func FetchERC721Metadata(tokenURI string) (*ERC721Metadata, error) {
return FetchERC721MetadataWithContext(context.TODO(), tokenURI)
}
// FetchERC721MetadataWithContext returns *ERC721Metadata fetch Metadata from tokenURI with Context
func FetchERC721MetadataWithContext(ctx context.Context, tokenURI string) (*ERC721Metadata, error) {
client := new(http.Client)
req, err := http.NewRequestWithContext(ctx, "GET", tokenURI, nil)
if err != nil {
return nil, err
}
req.Header.Add("Accept", "application/json")
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("Status: %d, msg: %s", resp.StatusCode, string(body))
}
return UnmarshalERC721Metadata(body)
}
// NewERC721Metadata returns *NewERC721Metadata
func NewERC721Metadata() (*ERC721Metadata, error) {
ret := new(ERC721Metadata)
ret.Attributes = nil
ret.Timestamp = time.Now().Unix()
ret.Language = "en-US"
return ret, nil
}
// UnmarshalERC721Metadata unmarshals ERC721Metadata
func UnmarshalERC721Metadata(data []byte) (*ERC721Metadata, error) {
var r = &ERC721Metadata{}
err := json.Unmarshal(data, r)
return r, err
}
// Marshal marshals ERC721Metadata
func (e *ERC721Metadata) Marshal() ([]byte, error) {
return json.Marshal(e)
}
// SetAttributes convert to JSON.RawMessage and set to Attributes
func (e *ERC721Metadata) SetAttributes(attributes interface{}) error {
byte, err := json.Marshal(attributes)
if err != nil {
return err
}
raw := json.RawMessage(byte)
e.Attributes = &raw
return nil
}