-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtoken.go
234 lines (194 loc) · 5.38 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
package token
import (
"errors"
"fmt"
"google.golang.org/protobuf/types/known/emptypb"
"github.com/hyperledger-labs/cckit/router"
)
var (
ErrTokenAlreadyExists = errors.New(`token already exists`)
)
type TokenGetter interface {
GetToken(router.Context, *TokenId) (*Token, error)
GetDefaultToken(router.Context, *emptypb.Empty) (*Token, error)
}
type TokenService struct {
}
func NewTokenService() *TokenService {
return &TokenService{}
}
func (s *TokenService) SetConfig(ctx router.Context, config *Config) (*Config, error) {
err := State(ctx).Put(config)
if err != nil {
return nil, err
}
return config, nil
}
func (s *TokenService) GetConfig(ctx router.Context, _ *emptypb.Empty) (*Config, error) {
config, err := State(ctx).Get(&Config{}, &Config{})
if err != nil {
return nil, err
}
return config.(*Config), nil
}
// GetToken naming for token is[{TokenType}, {GroupIdPart1}, {GroupIdPart2}]
func (s *TokenService) GetToken(ctx router.Context, id *TokenId) (*Token, error) {
if err := router.ValidateRequest(id); err != nil {
return nil, err
}
var (
tokenType *TokenType
tokenGroup *TokenGroup
err error
)
tokenType, err = s.GetTokenType(ctx, &TokenTypeId{Symbol: id.Symbol})
if err != nil {
return nil, fmt.Errorf(`token type: %w`, err)
}
if len(id.Group) > 0 {
tokenGroup, err = s.GetTokenGroup(ctx, &TokenGroupId{Symbol: id.Symbol, Group: id.Group})
if err != nil {
return nil, fmt.Errorf(`token type: %w`, err)
}
}
return &Token{
Type: tokenType,
Group: tokenGroup,
}, nil
}
func (s *TokenService) GetDefaultToken(ctx router.Context, _ *emptypb.Empty) (*Token, error) {
config, err := s.GetConfig(ctx, nil)
if err != nil {
return nil, err
}
if config.DefaultToken != nil {
return s.GetToken(ctx, config.DefaultToken)
}
return nil, nil
}
func (s *TokenService) CreateTokenType(ctx router.Context, req *CreateTokenTypeRequest) (*TokenType, error) {
if err := router.ValidateRequest(req); err != nil {
return nil, err
}
tokenType := &TokenType{
Name: req.Name,
Symbol: req.Symbol,
Decimals: req.Decimals,
TotalSupply: req.TotalSupply,
GroupType: req.GroupType,
}
for _, m := range req.Meta {
tokenType.Meta = append(tokenType.Meta, &TokenMeta{
Key: m.Key,
Value: m.Value,
})
}
if err := State(ctx).Insert(tokenType); err != nil {
return nil, err
}
if err := Event(ctx).Set(&TokenTypeCreated{
Symbol: req.Symbol,
Name: req.Name,
}); err != nil {
return nil, err
}
return tokenType, nil
}
func (s *TokenService) GetTokenType(ctx router.Context, id *TokenTypeId) (*TokenType, error) {
if err := router.ValidateRequest(id); err != nil {
return nil, err
}
tokenType, err := State(ctx).Get(id, &TokenType{})
if err != nil {
return nil, err
}
return tokenType.(*TokenType), nil
}
func (s *TokenService) ListTokenTypes(ctx router.Context, _ *emptypb.Empty) (*TokenTypes, error) {
tokenTypes, err := State(ctx).List(&TokenType{})
if err != nil {
return nil, err
}
return tokenTypes.(*TokenTypes), nil
}
func (s *TokenService) UpdateTokenType(ctx router.Context, request *UpdateTokenTypeRequest) (*TokenType, error) {
//TODO implement me
panic("implement me")
}
func (s *TokenService) DeleteTokenType(ctx router.Context, id *TokenTypeId) (*TokenType, error) {
//TODO implement me
panic("implement me")
}
func (s *TokenService) GetTokenGroups(ctx router.Context, id *TokenTypeId) (*TokenGroups, error) {
if err := router.ValidateRequest(id); err != nil {
return nil, err
}
tokenGroups, err := State(ctx).ListWith(&TokenGroup{}, []string{id.Symbol})
if err != nil {
return nil, err
}
return tokenGroups.(*TokenGroups), nil
}
func (s *TokenService) CreateTokenGroup(ctx router.Context, req *CreateTokenGroupRequest) (*TokenGroup, error) {
if err := router.ValidateRequest(req); err != nil {
return nil, err
}
_, err := s.GetTokenType(ctx, &TokenTypeId{Symbol: req.Symbol})
if err != nil {
return nil, err
}
tokenGroup := &TokenGroup{
Symbol: req.Symbol,
Group: req.Group,
Name: req.Name,
TotalSupply: nil,
}
for _, m := range req.Meta {
tokenGroup.Meta = append(tokenGroup.Meta, &TokenMeta{
Key: m.Key,
Value: m.Value,
})
}
if err := State(ctx).Insert(tokenGroup); err != nil {
return nil, err
}
if err := Event(ctx).Set(&TokenGroupCreated{
Symbol: req.Symbol,
Group: req.Group,
Name: req.Name,
}); err != nil {
return nil, err
}
return tokenGroup, nil
}
func (s *TokenService) GetTokenGroup(ctx router.Context, id *TokenGroupId) (*TokenGroup, error) {
tokenGroup, err := State(ctx).Get(id, &TokenGroup{})
if err != nil {
return nil, err
}
return tokenGroup.(*TokenGroup), nil
}
func (s *TokenService) DeleteTokenGroup(ctx router.Context, id *TokenGroupId) (*Token, error) {
//TODO implement me
panic("implement me")
}
func CreateDefault(
ctx router.Context, configSvc TokenServiceChaincode, createToken *CreateTokenTypeRequest) (*TokenId, error) {
existsTokenType, _ := configSvc.GetTokenType(ctx, &TokenTypeId{Symbol: createToken.Symbol})
if existsTokenType != nil {
return nil, ErrTokenAlreadyExists
}
// init token on first Init call
_, err := configSvc.CreateTokenType(ctx, createToken)
if err != nil {
return nil, err
}
tokenId := &TokenId{
Symbol: createToken.Symbol,
Group: nil,
}
if _, err = configSvc.SetConfig(ctx, &Config{DefaultToken: tokenId}); err != nil {
return nil, err
}
return tokenId, nil
}