/
token.go
334 lines (308 loc) · 9.95 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/**
* Copyright (c) 2018, 2019 National Digital ID COMPANY LIMITED
*
* This file is part of NDID software.
*
* NDID is the free software: you can redistribute it and/or modify it under
* the terms of the Affero GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or any later
* version.
*
* NDID is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the Affero GNU General Public License for more details.
*
* You should have received a copy of the Affero GNU General Public License
* along with the NDID source code. If not, see https://www.gnu.org/licenses/agpl.txt.
*
* Please contact info@ndid.co.th for any further questions
*
*/
package app
import (
"encoding/json"
"errors"
"github.com/tendermint/tendermint/abci/types"
"google.golang.org/protobuf/proto"
"github.com/ndidplatform/smart-contract/v7/abci/code"
"github.com/ndidplatform/smart-contract/v7/abci/utils"
data "github.com/ndidplatform/smart-contract/v7/protos/data"
)
func (app *ABCIApplication) getTokenPriceByFunc(fnName string, committedState bool) float64 {
key := tokenPriceFuncKeyPrefix + keySeparator + fnName
value, err := app.state.Get([]byte(key), committedState)
if err != nil {
panic(err)
}
if value == nil {
// if not set price of Function --> return price=1
return 1.0
}
var tokenPrice data.TokenPrice
err = proto.Unmarshal(value, &tokenPrice)
if err != nil {
return 1.0
}
return tokenPrice.Price
}
func (app *ABCIApplication) setTokenPriceByFunc(fnName string, price float64) error {
key := tokenPriceFuncKeyPrefix + keySeparator + fnName
var tokenPrice data.TokenPrice
tokenPrice.Price = price
value, err := utils.ProtoDeterministicMarshal(&tokenPrice)
if err != nil {
return err
}
app.state.Set([]byte(key), []byte(value))
return nil
}
func (app *ABCIApplication) createTokenAccount(nodeID string) {
key := tokenKeyPrefix + keySeparator + nodeID
var token data.Token
token.Amount = 0
value, _ := utils.ProtoDeterministicMarshal(&token)
app.state.Set([]byte(key), []byte(value))
}
func (app *ABCIApplication) setToken(nodeID string, amount float64) error {
key := tokenKeyPrefix + keySeparator + nodeID
value, err := app.state.Get([]byte(key), false)
if err != nil {
return err
}
if value == nil {
return errors.New("token account not found")
}
var token data.Token
err = proto.Unmarshal(value, &token)
if err != nil {
return errors.New("token account not found")
}
token.Amount = amount
value, err = utils.ProtoDeterministicMarshal(&token)
if err != nil {
return errors.New("token account not found")
}
app.state.Set([]byte(key), []byte(value))
return nil
}
type SetPriceFuncParam struct {
Func string `json:"func"`
Price float64 `json:"price"`
}
func (app *ABCIApplication) setPriceFunc(param string, nodeID string) types.ResponseDeliverTx {
app.logger.Infof("SetPriceFunc, Parameter: %s", param)
var funcParam SetPriceFuncParam
err := json.Unmarshal([]byte(param), &funcParam)
if err != nil {
return app.ReturnDeliverTxLog(code.UnmarshalError, err.Error(), "")
}
err = app.setTokenPriceByFunc(funcParam.Func, funcParam.Price)
if err != nil {
return app.ReturnDeliverTxLog(code.UnmarshalError, err.Error(), "")
}
return app.ReturnDeliverTxLog(code.OK, "success", "")
}
type GetPriceFuncParam struct {
Func string `json:"func"`
}
type GetPriceFuncResult struct {
Price float64 `json:"price"`
}
func (app *ABCIApplication) getPriceFunc(param string, committedState bool) types.ResponseQuery {
app.logger.Infof("GetPriceFunc, Parameter: %s", param)
var funcParam GetPriceFuncParam
err := json.Unmarshal([]byte(param), &funcParam)
if err != nil {
return app.ReturnQuery(nil, err.Error(), app.state.Height)
}
price := app.getTokenPriceByFunc(funcParam.Func, committedState)
var res = GetPriceFuncResult{
price,
}
value, err := json.Marshal(res)
if err != nil {
return app.ReturnQuery(nil, err.Error(), app.state.Height)
}
return app.ReturnQuery(value, "success", app.state.Height)
}
func (app *ABCIApplication) addToken(nodeID string, amount float64) error {
key := tokenKeyPrefix + keySeparator + nodeID
value, err := app.state.Get([]byte(key), false)
if err != nil {
return err
}
if value == nil {
return errors.New("token account not found")
}
var token data.Token
err = proto.Unmarshal(value, &token)
if err != nil {
return errors.New("token account not found")
}
token.Amount = token.Amount + amount
value, err = utils.ProtoDeterministicMarshal(&token)
if err != nil {
return errors.New("token account not found")
}
app.state.Set([]byte(key), []byte(value))
return nil
}
func (app *ABCIApplication) checkTokenAccount(nodeID string) bool {
key := tokenKeyPrefix + keySeparator + nodeID
value, err := app.state.Get([]byte(key), false)
if err != nil {
panic(err)
}
if value == nil {
return false
}
var token data.Token
err = proto.Unmarshal(value, &token)
if err != nil {
return false
}
return true
}
func (app *ABCIApplication) reduceToken(nodeID string, amount float64) (errorCode uint32, errorLog string) {
key := tokenKeyPrefix + keySeparator + nodeID
value, err := app.state.Get([]byte(key), false)
if err != nil {
return code.AppStateError, ""
}
if value == nil {
return code.TokenAccountNotFound, "token account not found"
}
var token data.Token
err = proto.Unmarshal(value, &token)
if err != nil {
return code.TokenAccountNotFound, "token account not found"
}
if amount > token.Amount {
return code.TokenNotEnough, "token not enough"
}
token.Amount = token.Amount - amount
value, err = utils.ProtoDeterministicMarshal(&token)
if err != nil {
return code.TokenAccountNotFound, "token account not found"
}
app.state.Set([]byte(key), []byte(value))
return code.OK, ""
}
func (app *ABCIApplication) getToken(nodeID string, committedState bool) (float64, error) {
key := tokenKeyPrefix + keySeparator + nodeID
value, err := app.state.Get([]byte(key), committedState)
if err != nil {
return 0, err
}
if value == nil {
return 0, errors.New("token account not found")
}
var token data.Token
err = proto.Unmarshal(value, &token)
if err != nil {
return 0, errors.New("token account not found")
}
return token.Amount, nil
}
type SetNodeTokenParam struct {
NodeID string `json:"node_id"`
Amount float64 `json:"amount"`
}
func (app *ABCIApplication) setNodeToken(param string, nodeID string) types.ResponseDeliverTx {
app.logger.Infof("SetNodeToken, Parameter: %s", param)
var funcParam SetNodeTokenParam
err := json.Unmarshal([]byte(param), &funcParam)
if err != nil {
return app.ReturnDeliverTxLog(code.UnmarshalError, err.Error(), "")
}
// Validate parameter
if funcParam.Amount < 0 {
return app.ReturnDeliverTxLog(code.AmountMustBeGreaterOrEqualToZero, "Amount must be greater than or equal to zero", "")
}
// Check token account
if !app.checkTokenAccount(funcParam.NodeID) {
return app.ReturnDeliverTxLog(code.TokenAccountNotFound, "token account not found", "")
}
err = app.setToken(funcParam.NodeID, funcParam.Amount)
if err != nil {
return app.ReturnDeliverTxLog(code.TokenAccountNotFound, err.Error(), "")
}
return app.ReturnDeliverTxLog(code.OK, "success", "")
}
type AddNodeTokenParam struct {
NodeID string `json:"node_id"`
Amount float64 `json:"amount"`
}
func (app *ABCIApplication) addNodeToken(param string, nodeID string) types.ResponseDeliverTx {
app.logger.Infof("AddNodeToken, Parameter: %s", param)
var funcParam AddNodeTokenParam
err := json.Unmarshal([]byte(param), &funcParam)
if err != nil {
return app.ReturnDeliverTxLog(code.UnmarshalError, err.Error(), "")
}
// Validate parameter
if funcParam.Amount < 0 {
return app.ReturnDeliverTxLog(code.AmountMustBeGreaterOrEqualToZero, "Amount must be greater than or equal to zero", "")
}
// Check token account
if !app.checkTokenAccount(funcParam.NodeID) {
return app.ReturnDeliverTxLog(code.TokenAccountNotFound, "token account not found", "")
}
err = app.addToken(funcParam.NodeID, funcParam.Amount)
if err != nil {
return app.ReturnDeliverTxLog(code.TokenAccountNotFound, err.Error(), "")
}
return app.ReturnDeliverTxLog(code.OK, "success", "")
}
type ReduceNodeTokenParam struct {
NodeID string `json:"node_id"`
Amount float64 `json:"amount"`
}
func (app *ABCIApplication) reduceNodeToken(param string, nodeID string) types.ResponseDeliverTx {
app.logger.Infof("ReduceNodeToken, Parameter: %s", param)
var funcParam ReduceNodeTokenParam
err := json.Unmarshal([]byte(param), &funcParam)
if err != nil {
return app.ReturnDeliverTxLog(code.UnmarshalError, err.Error(), "")
}
// Validate parameter
if funcParam.Amount < 0 {
return app.ReturnDeliverTxLog(code.AmountMustBeGreaterOrEqualToZero, "Amount must be greater than or equal to zero", "")
}
// Check token account
if !app.checkTokenAccount(funcParam.NodeID) {
return app.ReturnDeliverTxLog(code.TokenAccountNotFound, "token account not found", "")
}
errCode, errLog := app.reduceToken(funcParam.NodeID, funcParam.Amount)
if errCode != code.OK {
return app.ReturnDeliverTxLog(errCode, errLog, "")
}
return app.ReturnDeliverTxLog(code.OK, "success", "")
}
type GetNodeTokenParam struct {
NodeID string `json:"node_id"`
}
type GetNodeTokenResult struct {
Amount float64 `json:"amount"`
}
func (app *ABCIApplication) getNodeToken(param string, committedState bool) types.ResponseQuery {
app.logger.Infof("GetNodeToken, Parameter: %s", param)
var funcParam GetNodeTokenParam
err := json.Unmarshal([]byte(param), &funcParam)
if err != nil {
return app.ReturnQuery([]byte("{}"), err.Error(), app.state.Height)
}
tokenAmount, err := app.getToken(funcParam.NodeID, committedState)
if err != nil {
return app.ReturnQuery([]byte("{}"), "not found", app.state.Height)
}
var res = GetNodeTokenResult{
tokenAmount,
}
value, err := json.Marshal(res)
if err != nil {
return app.ReturnQuery(nil, err.Error(), app.state.Height)
}
return app.ReturnQuery(value, "success", app.state.Height)
}