-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnft.go
123 lines (106 loc) · 3.48 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
115
116
117
118
119
120
121
122
123
package opensea
import (
"context"
"encoding/json"
"fmt"
)
// NFTFilter represents parameters for filtering NFTs
type NFTFilter struct {
Collection string `json:"collection,omitempty"`
TokenIDs []string `json:"token_ids,omitempty"`
Owner string `json:"owner,omitempty"`
Limit int `json:"limit,omitempty"`
Offset int `json:"offset,omitempty"`
OrderBy string `json:"order_by,omitempty"` // created_date, sale_date, etc.
OrderDir string `json:"order_direction,omitempty"` // desc or asc
}
// NFTResponse represents the API response for NFTs
type NFTResponse struct {
Assets []Asset `json:"assets"`
Next string `json:"next"`
}
// GetNFT retrieves a single NFT by contract address and token ID
func (c *Client) GetNFT(ctx context.Context, contractAddress, tokenID string) (*Asset, error) {
if contractAddress == "" {
return nil, fmt.Errorf("contract address cannot be empty")
}
if tokenID == "" {
return nil, fmt.Errorf("token ID cannot be empty")
}
path := fmt.Sprintf("%s/%s/%s", assetEP, contractAddress, tokenID)
resp, err := c.get(ctx, path)
if err != nil {
return nil, fmt.Errorf("failed to get NFT: %w", err)
}
var asset Asset
if err := json.Unmarshal(resp, &asset); err != nil {
return nil, fmt.Errorf("failed to unmarshal NFT: %w", err)
}
return &asset, nil
}
// GetNFTs retrieves multiple NFTs based on the provided filters
func (c *Client) GetNFTs(ctx context.Context, filter NFTFilter) (*NFTResponse, error) {
if filter.Limit == 0 {
filter.Limit = 20 // Default limit
}
// Construct query parameters
query := fmt.Sprintf("%s?limit=%d", assetEP, filter.Limit)
if filter.Collection != "" {
query += fmt.Sprintf("&collection=%s", filter.Collection)
}
if filter.Owner != "" {
query += fmt.Sprintf("&owner=%s", filter.Owner)
}
if filter.Offset > 0 {
query += fmt.Sprintf("&offset=%d", filter.Offset)
}
if len(filter.TokenIDs) > 0 {
for _, id := range filter.TokenIDs {
query += fmt.Sprintf("&token_ids=%s", id)
}
}
if filter.OrderBy != "" {
query += fmt.Sprintf("&order_by=%s", filter.OrderBy)
}
if filter.OrderDir != "" {
query += fmt.Sprintf("&order_direction=%s", filter.OrderDir)
}
resp, err := c.get(ctx, query)
if err != nil {
return nil, fmt.Errorf("failed to get NFTs: %w", err)
}
var nftResp NFTResponse
if err := json.Unmarshal(resp, &nftResp); err != nil {
return nil, fmt.Errorf("failed to unmarshal NFTs response: %w", err)
}
return &nftResp, nil
}
// GetNFTsByCollection is a convenience method to get NFTs from a specific collection
func (c *Client) GetNFTsByCollection(ctx context.Context, collectionSlug string) (*NFTResponse, error) {
return c.GetNFTs(ctx, NFTFilter{
Collection: collectionSlug,
Limit: 50,
OrderBy: "created_date",
OrderDir: "desc",
})
}
// GetNFTsByOwner is a convenience method to get NFTs owned by a specific address
func (c *Client) GetNFTsByOwner(ctx context.Context, ownerAddress string) (*NFTResponse, error) {
return c.GetNFTs(ctx, NFTFilter{
Owner: ownerAddress,
Limit: 50,
OrderBy: "created_date",
OrderDir: "desc",
})
}
// GetNFTsByTokenIDs is a convenience method to get NFTs by their token IDs
func (c *Client) GetNFTsByTokenIDs(ctx context.Context, contractAddress string, tokenIDs []string) (*NFTResponse, error) {
if contractAddress == "" {
return nil, fmt.Errorf("contract address cannot be empty")
}
return c.GetNFTs(ctx, NFTFilter{
Collection: contractAddress,
TokenIDs: tokenIDs,
Limit: len(tokenIDs),
})
}