This repository has been archived by the owner on May 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
token.go
285 lines (257 loc) · 7.36 KB
/
token.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
// Copyright (c) 2020-2022 Blockwatch Data Inc.
// Author: alex@blockwatch.cc
package contract
import (
"context"
"encoding/json"
"fmt"
"strconv"
"time"
"blockwatch.cc/tzgo/micheline"
"blockwatch.cc/tzgo/tezos"
)
type TokenKind byte
const (
TokenKindInvalid TokenKind = iota
TokenKindTez
TokenKindFA1
TokenKindFA1_2
TokenKindFA2
TokenKindNFT
TokenKindNoView
)
func (k TokenKind) String() string {
switch k {
case TokenKindTez:
return "tez"
case TokenKindFA1:
return "fa1"
case TokenKindFA1_2:
return "fa1_2"
case TokenKindFA2:
return "fa2"
case TokenKindNFT:
return "nft"
case TokenKindNoView:
return "noview"
default:
return ""
}
}
func (k TokenKind) IsValid() bool {
return k != TokenKindInvalid
}
const TOKEN_METADATA = "token_metadata"
// Represents Tzip12 token metadata used by FA1 and FA2 tokens
// mixed with TZip21 metadata for NFTs
type TokenMetadata struct {
// TZip12 normative (only decimals is required)
Name string `json:"name"`
Symbol string `json:"symbol"`
Decimals int `json:"decimals"`
// Tzip21
Description string `json:"description,omitempty"`
ShouldPreferSymbol bool `json:"shouldPreferSymbol,omitempty"`
IsBooleanAmount bool `json:"isBooleanAmount,omitempty"`
IsTransferable bool `json:"isTransferable,omitempty"`
ArtifactUri string `json:"artifactUri,omitempty"`
DisplayUri string `json:"displayUri,omitempty"`
ThumbnailUri string `json:"thumbnailUri,omitempty"`
Minter string `json:"minter,omitempty"`
Creators []string `json:"creators,omitempty"`
Contributors []string `json:"contributors,omitempty"`
Publishers []string `json:"publishers,omitempty"`
Date time.Time `json:"date,omitempty"`
Type string `json:"type,omitempty"`
Tags []string `json:"tags,omitempty"`
Genres []string `json:"genres,omitempty"`
Language string `json:"language,omitempty"`
Identifier string `json:"identifier,omitempty"`
Rights string `json:"rights,omitempty"`
RightUri string `json:"rightUri,omitempty"`
ExternalUri string `json:"externalUri,omitempty"`
Formats []Tz21Format `json:"formats,omitempty"`
Attributes []Tz21Attribute `json:"attributes,omitempty"`
// internal
uri string `json:"-"`
raw json.RawMessage `json:"-"`
}
type Tz21Format struct {
Uri string `json:"uri"`
Hash string `json:"hash"`
MimeType string `json:"mimeType"`
FileSize int64 `json:"fileSize"`
FileName string `json:"fileName"`
Duration string `json:"duration"`
Dimensions Tz21Dimension `json:"dimensions"`
DataRate Tz21DataRate `json:"dataRate"`
}
type Tz21Attribute struct {
Name string `json:"name"`
Value string `json:"value"`
Type string `json:"type,omitempty"`
}
type Tz21Dimension struct {
Value string `json:"value"`
Unit string `json:"unit"`
}
type Tz21DataRate struct {
Value string `json:"value"`
Unit string `json:"unit"`
}
// (pair (nat %token_id) (map %token_info string bytes))
func (t *TokenMetadata) UnmarshalPrim(prim micheline.Prim) error {
if len(prim.Args) < 2 {
return fmt.Errorf("invalid metadata prim %s", prim.Dump())
}
t.IsTransferable = true // default
err := prim.Args[1].Walk(func(p micheline.Prim) error {
if p.IsSequence() {
return nil
}
if !p.IsElt() {
return fmt.Errorf("unexpected map item %q", p.Dump())
}
field := p.Args[0].String
data := p.Args[1].Bytes
// unpack packed bytes (some people do that, yes)
if p.Args[1].IsPacked() {
p, _ := p.Args[1].Unpack()
if p.Type == micheline.PrimBytes {
data = p.Bytes
} else {
data = []byte(p.String)
}
}
switch field {
case "":
t.uri = string(data)
case "name":
t.Name = string(data)
case "description":
t.Description = string(data)
case "symbol":
t.Symbol = string(data)
case "icon", "logo", "thumbnailUri", "thumbnail_uri":
t.ThumbnailUri = string(data)
case "artifactUri", "artifact_uri":
t.ArtifactUri = string(data)
case "displayUri", "display_uri":
t.DisplayUri = string(data)
case "decimals":
d, err := strconv.Atoi(string(data))
if err != nil {
return fmt.Errorf("%q: %v", field, err)
}
t.Decimals = d
case "shouldPreferSymbol", "should_prefer_symbol":
b, err := strconv.ParseBool(string(data))
if err != nil {
return fmt.Errorf("%q: %v", field, err)
}
t.ShouldPreferSymbol = b
case "isBooleanAmount", "is_boolean_amount":
b, err := strconv.ParseBool(string(data))
if err != nil {
return fmt.Errorf("%q: %v", field, err)
}
t.IsBooleanAmount = b
case "isTransferable", "is_transferable":
b, err := strconv.ParseBool(string(data))
if err != nil {
return fmt.Errorf("%q: %v", field, err)
}
t.IsTransferable = b
case "nonTransferable": // non-standard
b, err := strconv.ParseBool(string(data))
if err != nil {
return fmt.Errorf("%q: %v", field, err)
}
t.IsTransferable = !b
default:
log.Errorf("token metadata: unsupported field %q\n", field)
}
return micheline.PrimSkip
})
return err
}
func (t *TokenMetadata) UnmarshalJSON(data []byte) error {
type Alias *TokenMetadata
err := json.Unmarshal(data, Alias(t))
if err != nil {
return err
}
t.raw = json.RawMessage(data)
return nil
}
func (t TokenMetadata) URI() string {
return t.uri
}
func (t TokenMetadata) Raw() []byte {
if t.raw != nil {
return t.raw
}
buf, _ := json.Marshal(t)
return buf
}
func ResolveTokenMetadata(ctx context.Context, contract *Contract, tokenid tezos.Z) (*TokenMetadata, error) {
var (
store micheline.Prim
err error
)
// we need contract script and storage
if err = contract.Resolve(ctx); err != nil {
return nil, err
}
// lookup well known (pre-tz16 or wrong) tokens
if m, ok := wellKnown[contract.Address().String()]; ok {
return m, nil
}
// prefer off-chain view via run_code, but don't fail if not present
tz16, _ := contract.ResolveMetadata(ctx)
if tz16 != nil && tz16.HasView(TOKEN_METADATA) {
view := tz16.GetView(TOKEN_METADATA)
args := micheline.NewNat(tokenid.Big())
store, err = view.Run(ctx, contract, args)
if err != nil {
return nil, err
}
} else {
// read token_metadata bigmap
bigmaps := contract.script.BigmapsByName()
id, ok := bigmaps[TOKEN_METADATA]
if !ok {
return nil, fmt.Errorf("%s/%d: missing token metadata, have %v", contract.addr, tokenid.Int64(), bigmaps)
}
hash := (micheline.Key{
Type: micheline.NewType(micheline.NewPrim(micheline.T_NAT)),
IntKey: tokenid.Big(),
}).Hash()
store, err = contract.rpc.GetActiveBigmapValue(ctx, id, hash)
if err != nil {
return nil, err
}
}
// parse storage: (pair (nat %token_id) (map %token_info string bytes))
meta := &TokenMetadata{}
if err := meta.UnmarshalPrim(store); err != nil {
return nil, err
}
// should forward?
if meta.uri != "" {
if err := contract.ResolveTz16Uri(ctx, meta.uri, meta, nil); err != nil {
return nil, err
}
}
// fill empty token name from contract metadata
if meta.Name == "" && tz16 != nil {
meta.Name = tz16.Name
}
return meta, nil
}
type TokenBalance struct {
Owner tezos.Address
Token tezos.Address
TokenId tezos.Z
Balance tezos.Z
}