/
state.go
146 lines (126 loc) · 3.24 KB
/
state.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
package tokenmeta
import (
"fmt"
"strings"
"github.com/near/borsh-go"
"github.com/portto/solana-go-sdk/common"
)
const EDITION_MARKER_BIT_SIZE uint64 = 248
type Key borsh.Enum
const (
KeyUninitialized Key = iota
KeyEditionV1
KeyMasterEditionV1
KeyReservationListV1
KeyMetadataV1
KeyReservationListV2
KeyMasterEditionV2
KeyEditionMarker
KeyUseAuthorityRecord
KeyCollectionAuthorityRecord
)
type Creator struct {
Address common.PublicKey
Verified bool
Share uint8
}
type Data struct {
Name string
Symbol string
Uri string
SellerFeeBasisPoints uint16
Creators *[]Creator
}
type DataV2 struct {
Name string
Symbol string
Uri string
SellerFeeBasisPoints uint16
Creators *[]Creator
Collection *Collection
Uses *Uses
}
type metadataPreV11 struct {
Key Key
UpdateAuthority common.PublicKey
Mint common.PublicKey
Data Data
PrimarySaleHappened bool
IsMutable bool
EditionNonce *uint8
}
type Metadata struct {
Key Key
UpdateAuthority common.PublicKey
Mint common.PublicKey
Data Data
PrimarySaleHappened bool
IsMutable bool
EditionNonce *uint8
TokenStandard *TokenStandard
Collection *Collection
Uses *Uses
CollectionDetails *CollectionDetails
}
type TokenStandard borsh.Enum
const (
NonFungible TokenStandard = iota
FungibleAsset
Fungible
NonFungibleEdition
ProgrammableNonFungible
)
type Collection struct {
Verified bool
Key common.PublicKey
}
type Uses struct {
UseMethod UseMethod
Remaining uint64
Total uint64
}
type UseMethod borsh.Enum
const (
Burn UseMethod = iota
Multiple
Single
)
type CollectionDetails struct {
Enum borsh.Enum `borsh_enum:"true"`
V1 CollectionDetailsV1
}
type CollectionDetailsV1 struct {
Size uint64
}
func MetadataDeserialize(data []byte) (Metadata, error) {
var metadata Metadata
err := borsh.Deserialize(&metadata, data)
if err != nil {
// https://github.com/samuelvanderwaal/metaboss/issues/121
// https://github.com/metaplex-foundation/metaplex-program-library/pull/407
// C.f. https://github.com/metaplex-foundation/metaplex-program-library/blob/master/token-metadata/program/src/deser.rs#L12
var metadataPreV11 metadataPreV11
err := borsh.Deserialize(&metadataPreV11, data)
if err != nil {
return Metadata{}, fmt.Errorf("failed to deserialize data, err: %v", err)
} else {
metadata.Key = metadataPreV11.Key
metadata.UpdateAuthority = metadataPreV11.UpdateAuthority
metadata.Mint = metadataPreV11.Mint
metadata.Data = metadataPreV11.Data
metadata.PrimarySaleHappened = metadataPreV11.PrimarySaleHappened
metadata.IsMutable = metadataPreV11.IsMutable
metadata.EditionNonce = metadataPreV11.EditionNonce
}
}
// trim null byte
metadata.Data.Name = strings.TrimRight(metadata.Data.Name, "\x00")
metadata.Data.Symbol = strings.TrimRight(metadata.Data.Symbol, "\x00")
metadata.Data.Uri = strings.TrimRight(metadata.Data.Uri, "\x00")
return metadata, nil
}
type MasterEditionV2 struct {
Key Key
Supply uint64
MaxSupply *uint64
}