-
Notifications
You must be signed in to change notification settings - Fork 28
/
nft.go
114 lines (91 loc) · 2.5 KB
/
nft.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
package service
import (
"bytes"
"encoding/json"
"net/http"
"net/url"
"github.com/authgear/authgear-server/pkg/api/apierrors"
"github.com/authgear/authgear-server/pkg/api/model"
apimodel "github.com/authgear/authgear-server/pkg/api/model"
"github.com/authgear/authgear-server/pkg/lib/config"
"github.com/authgear/authgear-server/pkg/util/web3"
)
type ProbeColletionResponse struct {
Result apimodel.ProbeCollectionResult `json:"result"`
Error *apierrors.APIError `json:"error"`
}
type GetContractMetadataResponse struct {
Result apimodel.GetContractMetadataResult `json:"result"`
Error *apierrors.APIError `json:"error"`
}
type NFTService struct {
APIEndpoint config.NFTIndexerAPIEndpoint
}
func (s *NFTService) ProbeNFTCollection(contractID web3.ContractID) (*apimodel.ProbeCollectionResult, error) {
endpoint, err := url.Parse(string(s.APIEndpoint))
if err != nil {
return nil, err
}
endpoint.Path = "probe"
contractURL, err := contractID.URL()
if err != nil {
return nil, err
}
request := model.ProbeCollectionRequest{
ContractID: contractURL.String(),
}
data, err := json.Marshal(request)
if err != nil {
return nil, err
}
res, err := http.Post(endpoint.String(), "application/json", bytes.NewBuffer(data))
if err != nil {
return nil, err
}
defer res.Body.Close()
var response ProbeColletionResponse
err = json.NewDecoder(res.Body).Decode(&response)
if err != nil {
return nil, err
}
if response.Error != nil {
return nil, response.Error
}
return &response.Result, nil
}
func (s *NFTService) GetContractMetadata(contracts []web3.ContractID) ([]apimodel.NFTCollection, error) {
endpoint, err := url.Parse(string(s.APIEndpoint))
if err != nil {
return nil, err
}
endpoint.Path = "metadata"
contractURLs := make([]string, 0, len(contracts))
for _, contract := range contracts {
contractURL, err := contract.URL()
if err != nil {
return nil, err
}
contractURLs = append(contractURLs, contractURL.String())
}
request := model.GetContractMetadataRequest{
ContractIDs: contractURLs,
}
data, err := json.Marshal(request)
if err != nil {
return nil, err
}
res, err := http.Post(endpoint.String(), "application/json", bytes.NewBuffer(data))
if err != nil {
return nil, err
}
defer res.Body.Close()
var response GetContractMetadataResponse
err = json.NewDecoder(res.Body).Decode(&response)
if err != nil {
return nil, err
}
if response.Error != nil {
return nil, response.Error
}
return response.Result.Collections, nil
}