This repository has been archived by the owner on Jul 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy paththegraph.go
350 lines (314 loc) · 8.66 KB
/
thegraph.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
// Copyright (C) 2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package thegraph
import (
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"strconv"
"strings"
"time"
"github.com/ava-labs/hypersdk/codec"
"github.com/ava-labs/indexvm/actions"
"github.com/hasura/go-graphql-client"
ipfs "github.com/ipfs/go-ipfs-api"
)
const (
nftURL = "https://api.thegraph.com/subgraphs/name/traderjoe-xyz/nft-contracts"
queryLimit = 10
)
type TheGraphClient struct {
c *graphql.Client
hc http.Client
ic *ipfs.Shell
shouldSkip func(string) (bool, error)
failed func(string) error
}
func New(
ipfsAddress string,
shouldSkip func(contract string) (bool, error),
failed func(contract string) error,
) *TheGraphClient {
t := http.DefaultTransport.(*http.Transport).Clone()
t.MaxIdleConns = 100_000
t.MaxConnsPerHost = 100_000
ic := ipfs.NewShell(ipfsAddress)
ic.SetTimeout(3 * time.Minute)
return &TheGraphClient{
c: graphql.NewClient(nftURL, nil),
hc: http.Client{
Transport: t,
Timeout: 30 * time.Second,
},
ic: ic,
shouldSkip: shouldSkip,
failed: failed,
}
}
type NFTQuery struct {
NFTContracts []struct {
ID string `graphql:"id"`
CreatedAt string `graphql:"createdAt"`
Name string `graphql:"name"`
Symbol string `graphql:"symbol"`
NFTs []struct {
TokenURI string `graphql:"tokenURI"`
TokenID string `graphql:"tokenID"`
} `graphql:"nfts"`
} `graphql:"nftContracts(first: 10, orderBy:createdAt, orderDirection:asc, where:{createdAt_gt: $timestamp})"`
// TODO: handle multiple deploys at same second
}
type NFT struct {
Contract string
TokenID int
CreatedAt int64
Symbol string
Name string
Description string
Image string
}
func (n *NFT) Pack() ([]byte, error) {
p := codec.NewWriter(actions.MaxContentSize)
p.PackString(n.Contract)
p.PackInt(n.TokenID)
p.PackInt64(n.CreatedAt)
p.PackString(n.Symbol)
p.PackString(n.Name)
p.PackString(n.Description)
p.PackString(n.Image)
return p.Bytes(), p.Err()
}
func Unpack(b []byte) (*NFT, error) {
var n NFT
p := codec.NewReader(b, actions.MaxContentSize)
n.Contract = p.UnpackString(true)
n.TokenID = p.UnpackInt(false) // could be 0
n.CreatedAt = p.UnpackInt64(true)
n.Symbol = p.UnpackString(false)
n.Name = p.UnpackString(true)
n.Description = p.UnpackString(false)
n.Image = p.UnpackString(true)
return &n, p.Err()
}
func (n *NFT) ID() string {
return fmt.Sprintf("%s-%d", n.Contract, n.TokenID)
}
type NFTMeta struct {
Name string `json:"name"`
Description string `json:"description"`
Image string `json:"image"`
}
// Cat the content at the given path. Callers need to drain and close the returned reader after usage.
// Inspired by: https://github.com/ipfs/go-ipfs-api/blob/cb1fca1e60b1fb653ad10dd77f10c48b55fea867/shell.go#L162
func (c *TheGraphClient) Cat(ctx context.Context, path string) (io.ReadCloser, error) {
resp, err := c.ic.Request("cat", path).Send(ctx)
if err != nil {
return nil, err
}
if resp.Error != nil {
return nil, resp.Error
}
return resp.Output, nil
}
func fetchMetadata(c *TheGraphClient, tokenURI string) (string, *NFTMeta) {
metaAddr := strings.TrimPrefix(tokenURI, "ipfs://")
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute)
defer cancel()
reader, err := c.Cat(ctx, metaAddr)
if err != nil {
fmt.Println("unable to get object", metaAddr, err)
return "", nil
}
defer reader.Close()
var meta NFTMeta
r, err := ioutil.ReadAll(reader)
if err != nil {
return "", nil // skip entire contract
}
if err := json.Unmarshal(r, &meta); err != nil {
fmt.Println("unable to marshal data", r, err)
return "", nil // skip entire contract
}
return metaAddr, &meta
}
func parseURI(uri string) string {
// Happy path
if strings.HasPrefix(uri, "ipfs://") {
return uri
}
// Attempt to overwrite (assume some URL)
splits := strings.Split(uri, "ipfs/")
if len(splits) == 2 {
return fmt.Sprintf("ipfs://%s", splits[1])
}
return ""
}
func cleanString(str string) string {
n := strings.ReplaceAll(str, "™", "")
n = strings.ReplaceAll(n, "\n\n", " ")
n = strings.ReplaceAll(n, "\n", " ")
n = strings.ReplaceAll(n, "®", "")
n = strings.ReplaceAll(n, "\"", "")
return n
}
func (q *NFTQuery) process(
ctx context.Context,
c *TheGraphClient,
) (map[string][]*NFT, int64, error) {
l := len(q.NFTContracts)
validNFTs := map[string][]*NFT{}
next := int64(-1)
fmt.Println("starting processing contract batch")
for _, contract := range q.NFTContracts {
if err := ctx.Err(); err != nil {
return nil, -1, err
}
// Regardless of if we should skip the contract, we need to update the next
// timestamp marker we are pulling from.
ca, err := strconv.ParseInt(contract.CreatedAt, 10, 64)
if err != nil {
continue
}
next = ca
if len(contract.Name) == 0 {
fmt.Println("skipping contract with empty name:", contract.ID)
continue
}
shouldSkip, err := c.shouldSkip(contract.ID)
if err != nil {
fmt.Println("cannot determine if should skip", "err:", err)
}
if shouldSkip {
fmt.Printf(
"skipping previously inaccessible contract (name=%s address=%s)\n",
contract.Name,
contract.ID,
)
continue
}
fmt.Printf("processing contract (name=\"%s\" address=%s)\n", contract.Name, contract.ID)
var (
tokenURIs = map[string]struct{}{}
repeats = 0
nfts = []*NFT{}
)
for i, nft := range contract.NFTs {
if err := ctx.Err(); err != nil {
return nil, -1, err
}
// Periodically log NFT download progress
if i%10 == 0 && i > 0 {
fmt.Printf("processed %d/%d NFTs\n", i, len(contract.NFTs))
}
// Sanity check token fields
metaURI := parseURI(nft.TokenURI)
if len(metaURI) == 0 {
fmt.Println("skipping because meta not ipfs: ", nft.TokenURI)
break // skip entire contract
}
if strings.HasSuffix(metaURI, "unrevealed") {
fmt.Println("skipping because unrevealed")
// TODO: trigger retry (joepegs used to hide)
break // skip entire contract
}
s, err := strconv.Atoi(nft.TokenID)
if err != nil {
// TODO: figure out why it would not work?
break // skip entire contract
}
// Some early collections use the same TokenURI for all NFTs. This makes
// things look quite boring.
_, ok := tokenURIs[metaURI]
if ok {
repeats++
continue
}
tokenURIs[metaURI] = struct{}{}
// Fetch metadata
addr, meta := fetchMetadata(c, metaURI)
if meta == nil {
break // skip contract
}
// Ensure image is valid
imageURI := parseURI(meta.Image)
if len(imageURI) == 0 {
fmt.Println("skipping because image not ipfs: ", meta.Image)
break // skip entire contract
}
// Skip projects that refer to things as a "pack"
if strings.Contains(strings.ToLower(meta.Name), "pack") {
break
}
if strings.Contains(strings.ToLower(meta.Description), "pack") {
break
}
// Pin metadata
//
// We can't pass context here because the client library doesn't
// support it, so we check context one last time before kicking this off.
if err := ctx.Err(); err != nil {
return nil, -1, err
}
if err := c.ic.Pin(addr); err != nil {
fmt.Printf("unable to pin metadata %s: %v\n", addr, err)
break // skip entire contract
}
// TODO: Pin images ASYNC
// if err := c.ic.Pin(strings.TrimPrefix(imageURI, "ipfs://")); err != nil {
// fmt.Printf("unable to pin image %s: %v\n", imageURI, err)
// break // skip entire contract
// }
// Add NFT
nfts = append(nfts, &NFT{
Contract: contract.ID,
Symbol: contract.Symbol,
TokenID: s,
CreatedAt: ca,
Name: cleanString(meta.Name),
Description: cleanString(meta.Description),
Image: imageURI,
})
}
if len(nfts)+repeats < len(contract.NFTs) {
if err := c.failed(contract.ID); err != nil {
fmt.Println("unable to persist failed contract:", err)
}
fmt.Println(
"failed to parse contract:",
contract.ID,
"nfts",
len(nfts),
"repeats",
repeats,
"expected",
len(contract.NFTs),
)
} else {
// TODO: broadcast NFTs here instead of waiting for batch to complete
validNFTs[contract.ID] = nfts
fmt.Printf("proccessing %s completed successfully (valid=%d repeat=%d)\n", contract.ID, len(nfts), repeats)
}
}
fmt.Println("finished processing contract batch", "next:", next)
if l < queryLimit {
next = -1
}
return validNFTs, next, nil
}
func (c *TheGraphClient) Fetch(
ctx context.Context,
timestamp int64,
) (map[string][]*NFT, int64, error) {
var q NFTQuery
variables := map[string]interface{}{
"timestamp": timestamp,
}
err := c.c.Query(ctx, &q, variables)
if err != nil {
return nil, -1, err
}
return q.process(ctx, c)
}