-
Notifications
You must be signed in to change notification settings - Fork 24
/
nft_converters.go
193 lines (182 loc) · 5.58 KB
/
nft_converters.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
package api
import (
"context"
"encoding/json"
"errors"
"fmt"
"strings"
"github.com/tonkeeper/opentonapi/internal/g"
"github.com/tonkeeper/opentonapi/pkg/bath"
imgGenerator "github.com/tonkeeper/opentonapi/pkg/image"
"github.com/tonkeeper/tongo"
"github.com/go-faster/jx"
"github.com/tonkeeper/opentonapi/pkg/core"
"github.com/tonkeeper/opentonapi/pkg/oas"
"github.com/tonkeeper/opentonapi/pkg/references"
)
func convertNFT(ctx context.Context, item core.NftItem, book addressBook, metaCache metadataCache) oas.NftItem {
i := oas.NftItem{
Address: item.Address.ToRaw(),
Index: item.Index.BigInt().Int64(),
Owner: convertOptAccountAddress(item.OwnerAddress, book),
Verified: item.Verified,
Metadata: anyToJSONRawMap(item.Metadata),
DNS: g.Opt(item.DNS),
}
if item.Sale != nil {
tokenName := "TON"
if item.Sale.Price.Token != nil {
m, _ := metaCache.getJettonMeta(ctx, *item.Sale.Price.Token)
tokenName = m.Name
if tokenName == "" {
tokenName = UnknownJettonName
}
}
i.SetSale(oas.OptSale{
Value: oas.Sale{
Address: item.Sale.Contract.ToRaw(),
Market: convertAccountAddress(item.Sale.Marketplace, book),
Owner: convertOptAccountAddress(item.Sale.Seller, book),
Price: oas.Price{
Value: fmt.Sprintf("%v", item.Sale.Price.Amount),
TokenName: tokenName,
},
},
Set: true,
})
}
var image string
if item.CollectionAddress != nil {
if cc, prs := book.GetCollectionInfoByAddress(*item.CollectionAddress); prs {
for _, a := range cc.Approvers {
i.ApprovedBy = append(i.ApprovedBy, a)
}
}
cInfo, _ := metaCache.getCollectionMeta(ctx, *item.CollectionAddress)
// TODO: REMOVE, FAST HACK
if strings.Contains(cInfo.Description, "ton-staker.com") || strings.Contains(cInfo.Description, "scaleton.xyz") {
cInfo.Description = "SCAM"
}
i.Collection.SetTo(oas.NftItemCollection{
Address: item.CollectionAddress.ToRaw(),
Name: cInfo.Name,
Description: cInfo.Description,
})
if *item.CollectionAddress == references.RootDotTon && item.DNS != nil && item.Verified {
image = "https://cache.tonapi.io/dns/preview/" + *item.DNS + ".png"
i.Metadata["name"] = []byte(fmt.Sprintf(`"%v"`, *item.DNS))
}
}
if item.Metadata != nil {
if imageI, prs := item.Metadata["image"]; prs {
image, _ = imageI.(string)
}
// TODO: REMOVE, FAST HACK
if description, ok := item.Metadata["description"]; ok {
if value, ok := description.(string); ok && strings.Contains(value, "ton-staker.com") {
i.Metadata["description"] = []byte(`"SCAM"`)
}
}
}
if image == "" {
image = references.Placeholder
}
for _, size := range []int{5, 100, 500, 1500} {
url := imgGenerator.DefaultGenerator.GenerateImageUrl(image, size, size)
i.Previews = append(i.Previews, oas.ImagePreview{
Resolution: fmt.Sprintf("%vx%v", size, size),
URL: url,
})
}
return i
}
func convertNftCollection(collection core.NftCollection, book addressBook) oas.NftCollection {
c := oas.NftCollection{
Address: collection.Address.ToRaw(),
NextItemIndex: int64(collection.NextItemIndex),
RawCollectionContent: fmt.Sprintf("%x", collection.CollectionContent[:]),
Owner: convertOptAccountAddress(collection.OwnerAddress, book),
}
if len(collection.Metadata) == 0 {
return c
}
metadata := map[string]jx.Raw{}
image := references.Placeholder
for k, v := range collection.Metadata {
// TODO: REMOVE, FAST HACK
if k == "description" {
if value, ok := v.(string); ok && strings.Contains(value, "ton-staker.com") {
v = "SCAM"
}
}
var err error
if k == "image" {
if i, ok := v.(string); ok && i != "" {
image = i
}
}
metadata[k], err = json.Marshal(v)
if err != nil {
continue
}
}
if known, ok := book.GetCollectionInfoByAddress(collection.Address); ok {
for _, a := range known.Approvers {
c.ApprovedBy = append(c.ApprovedBy, a)
}
}
c.Metadata.SetTo(metadata)
for _, size := range []int{5, 100, 500, 1500} {
url := imgGenerator.DefaultGenerator.GenerateImageUrl(image, size, size)
c.Previews = append(c.Previews, oas.ImagePreview{
Resolution: fmt.Sprintf("%vx%v", size, size),
URL: url,
})
}
return c
}
func (h *Handler) convertNftHistory(ctx context.Context, account tongo.AccountID, traceIDs []tongo.Bits256, acceptLanguage oas.OptString) ([]oas.AccountEvent, int64, error) {
var lastLT uint64
events := make([]oas.AccountEvent, 0, len(traceIDs))
for _, traceID := range traceIDs {
trace, err := h.storage.GetTrace(ctx, traceID)
if err != nil {
if errors.Is(err, core.ErrTraceIsTooLong) {
// we ignore this for now, because we believe that this case is extremely rare.
continue
}
return nil, 0, err
}
result, err := bath.FindActions(ctx, trace, bath.WithInformationSource(h.storage), bath.WithStraws(bath.NFTStraws))
if err != nil {
return nil, 0, err
}
event := oas.AccountEvent{
EventID: trace.Hash.Hex(),
Account: convertAccountAddress(account, h.addressBook),
Timestamp: trace.Utime,
IsScam: false,
Lt: int64(trace.Lt),
InProgress: trace.InProgress(),
}
for _, action := range result.Actions {
if action.Type != bath.NftItemTransfer {
continue
}
convertedAction, spamDetected, err := h.convertAction(ctx, &account, action, acceptLanguage)
if err != nil {
return nil, 0, err
}
if !event.IsScam && spamDetected {
event.IsScam = true
}
event.Actions = append(event.Actions, convertedAction)
}
if len(event.Actions) == 0 {
continue
}
events = append(events, event)
lastLT = trace.Lt
}
return events, int64(lastLT), nil
}