-
Notifications
You must be signed in to change notification settings - Fork 9
/
token_gallery.go
112 lines (97 loc) · 3.22 KB
/
token_gallery.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
package persist
import (
"encoding/json"
"fmt"
"strconv"
"strings"
)
// TokenIdentifiers represents a unique identifier for a token
type TokenIdentifiers struct {
TokenID HexTokenID `json:"token_id"`
ContractAddress Address `json:"contract_address"`
Chain Chain `json:"chain"`
}
func (t TokenIdentifiers) String() string {
return fmt.Sprintf("token(chain=%s, contract=%s, tokenID=%s)", t.Chain, t.ContractAddress, t.TokenID.ToDecimalTokenID())
}
// NewTokenIdentifiers creates a new token identifiers
func NewTokenIdentifiers(pContractAddress Address, pTokenID HexTokenID, pChain Chain) TokenIdentifiers {
return TokenIdentifiers{
TokenID: HexTokenID(pTokenID.BigInt().Text(16)),
ContractAddress: pContractAddress,
Chain: pChain,
}
}
type TokenUniqueIdentifiers struct {
Chain Chain `json:"chain"`
ContractAddress Address `json:"contract_address"`
TokenID HexTokenID `json:"token_id"`
OwnerAddress Address `json:"owner_address"`
}
func (t TokenUniqueIdentifiers) String() string {
return fmt.Sprintf("token(chain=%s, contract=%s, tokenID=%s, owner=%s)", t.Chain, t.ContractAddress, t.TokenID.ToDecimalTokenID(), t.OwnerAddress)
}
func (t TokenUniqueIdentifiers) AsJSONKey() string {
return fmt.Sprintf("%s+%s+%s+%d", t.ContractAddress, t.TokenID, t.OwnerAddress, t.Chain)
}
func (t *TokenUniqueIdentifiers) FromJSONKey(key string) error {
res := strings.Split(key, "+")
if len(res) != 4 {
return fmt.Errorf("invalid token unique identifiers: %v", key)
}
chain, err := strconv.Atoi(res[3])
if err != nil {
return err
}
t.Chain = Chain(chain)
t.ContractAddress = Address(res[0])
t.TokenID = HexTokenID(res[1])
t.OwnerAddress = Address(res[2])
return nil
}
func (t *TokenUniqueIdentifiers) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]any{
"chain": t.Chain,
"contract_address": t.ContractAddress,
"token_id": t.TokenID,
"owner_address": t.OwnerAddress,
})
}
func (t *TokenUniqueIdentifiers) UnmarshalJSON(data []byte) error {
var tmp struct {
Chain Chain `json:"chain"`
ContractAddress Address `json:"contract_address"`
TokenID HexTokenID `json:"token_id"`
OwnerAddress Address `json:"owner_address"`
}
err := json.Unmarshal(data, &tmp)
if err != nil {
return err
}
t.Chain = tmp.Chain
t.ContractAddress = tmp.ContractAddress
t.TokenID = tmp.TokenID
t.OwnerAddress = tmp.OwnerAddress
return nil
}
// ContractIdentifiers represents a unique identifier for a contract
type ContractIdentifiers struct {
ContractAddress Address `json:"contract_address"`
Chain Chain `json:"chain"`
}
func (c ContractIdentifiers) String() string {
return fmt.Sprintf("contract(chain=%s, address=%s)", c.Chain, c.ContractAddress)
}
// NewContractIdentifiers creates a new contract identifiers
func NewContractIdentifiers(pContractAddress Address, pChain Chain) ContractIdentifiers {
return ContractIdentifiers{
ContractAddress: pContractAddress,
Chain: pChain,
}
}
type ErrContractCreatorNotFound struct {
ContractID DBID
}
func (e ErrContractCreatorNotFound) Error() string {
return fmt.Sprintf("ContractCreator not found for contractID %s", e.ContractID)
}