-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSalaryManagement.go
312 lines (259 loc) · 6.89 KB
/
SalaryManagement.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
/*
Edited by Li Yicong
2016/8/20
*/
package main
import (
"errors"
"fmt"
"strconv"
"github.com/hyperledger/fabric/core/chaincode/shim"
)
// SimpleChaincode example simple Chaincode implementation
type SimpleChaincode struct {
}
func (t *SimpleChaincode) Init(stub *shim.ChaincodeStub, function string, args []string) ([]byte, error) {
var A, B string // Entities
var Aval, Bval int // Asset holdings
var err error
if len(args) != 4 {
return nil, errors.New("Incorrect number of arguments. Expecting 4")
}
// Initialize the chaincode
A = args[0]
Aval, err = strconv.Atoi(args[1])
if err != nil {
return nil, errors.New("Expecting integer value for asset holding")
}
B = args[2]
Bval, err = strconv.Atoi(args[3])
if err != nil {
return nil, errors.New("Expecting integer value for asset holding")
}
fmt.Printf("Aval = %d, Bval = %d\n", Aval, Bval)
// Write the state to the ledger
err = stub.PutState(A, []byte(strconv.Itoa(Aval)))
if err != nil {
return nil, err
}
err = stub.PutState(B, []byte(strconv.Itoa(Bval)))
if err != nil {
return nil, err
}
return nil, nil
}
/*
func (t *SimpleChaincode) Init(stub *shim.ChaincodeStub, function string, args []string) ([]byte, error) {
var A string // Entities
var Aval int // Asset holdings
var err error
var length int
var i int
if len(args)%2 != 0 || len(args) == 0 {
return nil, errors.New("Incorrect number of arguments.")
}
length = len(args)
for i := 0; i < length; i += 2 {
A = args[i]
Aval, err = strconv.Atoi(args[i+1])
if err != nil {
return nil, errors.New("Expecting integer value for asset holding")
}
fmt.Printf("asdf")
err = stub.PutState(A, []byte(strconv.Itoa(Aval)))
if err != nil {
return nil, err
}
}
return nil, nil
}
*/
// give salary and late
func (t *SimpleChaincode) Invoke(stub *shim.ChaincodeStub, function string, args []string) ([]byte, error) {
if function == "delete"{
// Deletes an entity from its state
return t.delete(stub, args)
}
switch function {
case "giveSalary":
var A string // Entities
var Aval int // Asset holdings
var err error
if len(args) > 1 {
return nil, errors.New("Incorrect number of arguments. Expecting 1")
}
A = args[0]
// Get the state from the ledger
// TODO: will be nice to have a GetAllState call to ledger
Avalbytes, err := stub.GetState(A)
if err != nil {
return nil, errors.New("Failed to get state")
}
if Avalbytes == nil {
return nil, errors.New("Entity not found")
}
Aval, _ = strconv.Atoi(string(Avalbytes))
// Perform the execution
Aval = Aval + 5000
fmt.Printf("You have got your salary")
// Write the state back to the ledger
err = stub.PutState(A, []byte(strconv.Itoa(Aval)))
if err != nil {
return nil, err
}
return nil, nil
case "beLate":
var A string // Entities
var Aval int // Asset holdings
var X int
var err error
if len(args) < 2 {
return nil, errors.New("Incorrect number of arguments. Expecting 2")
}
A = args[0]
// Get the state from the ledger
// TODO: will be nice to have a GetAllState call to ledger
Avalbytes, err := stub.GetState(A)
if err != nil {
return nil, errors.New("Failed to get state")
}
if Avalbytes == nil {
return nil, errors.New("Entity not found")
}
Aval, _ = strconv.Atoi(string(Avalbytes))
// Perform the execution
X, err = strconv.Atoi(args[1])
if X > 0 && X <= 5 {
Aval = Aval - 50
}
if X > 5 && X <= 15 {
Aval = Aval - 75
}
if X > 15 {
Aval = Aval - 200
}
fmt.Printf("You are late")
// Write the state back to the ledger
err = stub.PutState(A, []byte(strconv.Itoa(Aval)))
if err != nil {
return nil, err
}
return nil, nil
case "workOvertime":
var A string // Entities
var Aval int // Asset holdings
var X int
var err error
if len(args) < 2 {
return nil, errors.New("Incorrect number of arguments. Expecting 2")
}
A = args[0]
// Get the state from the ledger
// TODO: will be nice to have a GetAllState call to ledger
Avalbytes, err := stub.GetState(A)
if err != nil {
return nil, errors.New("Failed to get state")
}
if Avalbytes == nil {
return nil, errors.New("Entity not found")
}
Aval, _ = strconv.Atoi(string(Avalbytes))
// Perform the execution
X, err = strconv.Atoi(args[1])
if X > 0 && X <= 2 {
Aval = Aval + 20
}
if X > 2 && X <= 4 {
Aval = Aval + 50
}
if X > 4 && X <= 8 {
Aval = Aval + 120
}
if X > 8 {
Aval = Aval + 200
}
fmt.Printf("You are late")
// Write the state back to the ledger
err = stub.PutState(A, []byte(strconv.Itoa(Aval)))
if err != nil {
return nil, err
}
return nil, nil
case "getSalary":
var A string // Entities
var Aval int // Asset holdings
var X int
var err error
if len(args) < 2 {
return nil, errors.New("Incorrect number of arguments. Expecting 2")
}
A = args[0]
// Get the state from the ledger
// TODO: will be nice to have a GetAllState call to ledger
Avalbytes, err := stub.GetState(A)
if err != nil {
return nil, errors.New("Failed to get state")
}
if Avalbytes == nil {
return nil, errors.New("Entity not found")
}
Aval, _ = strconv.Atoi(string(Avalbytes))
// Perform the execution
X, err = strconv.Atoi(args[1])
Aval = Aval - X
fmt.Printf("YOU HAVE GOT YOUR MONEY")
// Write the state back to the ledger
err = stub.PutState(A, []byte(strconv.Itoa(Aval)))
if err != nil {
return nil, err
}
return nil, nil
default:
return nil, errors.New("Unsupported operation")
}
}
// Deletes an entity from state
func (t *SimpleChaincode) delete(stub *shim.ChaincodeStub, args []string) ([]byte, error) {
if len(args) != 1 {
return nil, errors.New("Incorrect number of arguments. Expecting 1")
}
A := args[0]
// Delete the key from the state in ledger
err := stub.DelState(A)
if err != nil {
return nil, errors.New("Failed to delete state")
}
fmt.Printf("asdf")
return nil, nil
}
// Query callback representing the query of a chaincode
func (t *SimpleChaincode) Query(stub *shim.ChaincodeStub, function string, args []string) ([]byte, error) {
if function != "query" {
return nil, errors.New("Invalid query function name. Expecting \"query\"")
}
var A string // Entities
var err error
if len(args) != 1 {
return nil, errors.New("Incorrect number of arguments. Expecting name of the person to query")
}
A = args[0]
// Get the state from the ledger
Avalbytes, err := stub.GetState(A)
if err != nil {
jsonResp := "{\"Error\":\"Failed to get state for " + A + "\"}"
return nil, errors.New(jsonResp)
}
if Avalbytes == nil {
jsonResp := "{\"Error\":\"Nil amount for " + A + "\"}"
return nil, errors.New(jsonResp)
}
jsonResp := "{\"Name\":\"" + A + "\",\"Amount\":\"" + string(Avalbytes) + "\"}"
fmt.Printf("Query Response:%s\n", jsonResp)
return Avalbytes, nil
}
func main() {
err := shim.Start(new(SimpleChaincode))
if err != nil {
fmt.Printf("Error starting Simple chaincode: %s", err)
}
}