-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcarbonTrade.go
More file actions
287 lines (233 loc) · 9.63 KB
/
carbonTrade.go
File metadata and controls
287 lines (233 loc) · 9.63 KB
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
/*
used for CodeJam 2017
*/
package main
import (
"errors"
"fmt"
"strconv"
"encoding/json"
"github.com/hyperledger/fabric/core/chaincode/shim"
)
//==============================================================================================================================
// Carrier - Defines the structure for a Carrier object. JSON on right tells it what JSON fields to map to
// that element when reading a JSON object into the struct e.g. JSON owner -> Struct owner.
//==============================================================================================================================
type Carrier struct {
Name string `json:"name"`
CurrentBal string `json:"currentBal"`
}
//==============================================================================================================================
// customer - Defines the structure for a customer object. JSON on right tells it what JSON fields to map to
// that element when reading a JSON object into the struct e.g. JSON owner -> Struct owner.
//==============================================================================================================================
type Customer struct {
Name string `json:"name"`
CurrentBal string `json:"currentBal"`
}
//==============================================================================================================================
// XOM - Defines the structure for a XOM object. JSON on right tells it what JSON fields to map to
// that element when reading a JSON object into the struct e.g. JSON owner -> Struct owner.
//==============================================================================================================================
type XOM struct {
Name string `json:"name"`
CurrentBal string `json:"currentBal"`
}
//==============================================================================================================================
// Order - Defines the structure for a Order object. JSON on right tells it what JSON fields to map to
// that element when reading a JSON object into the struct e.g. JSON owner -> Struct owner.
//==============================================================================================================================
type Order struct{
SerialNum string `json:"serialNum"`
AckCus string `json:"ackCus"` //customer acknowledgement
AckXom string `json:"ackXom"` //customer acknowledgement
AckCarrier string `json:"ackCarrier"`//customer acknowledgement
Cost string `json:"cost"`
}
// SimpleChaincode example simple Chaincode implementation
type SimpleChaincode struct {
}
func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) {
return nil, nil
}
func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) {
switch function {
case "createOrder":
return t.createOrder(stub,args)
case "createCustomer":
return t.createCustomer(stub,args)
case "createXOM":
return t.createXOM(stub,args)
case "tradeFunds":
return t.tradeFunds(stub,args)
case "createCarrier":
return t.createCarrier(stub,args)
}
return nil,nil
}
func (t *SimpleChaincode) Query(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) {
switch function {
case "readOrder":
return t.readOrder(stub,args)
}
return nil,nil
}
//==============================================================================================================================
// Create Carrier
//==============================================================================================================================
func (t *SimpleChaincode) createCarrier(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) {
if len(args) != 2 {
return nil, errors.New("Incorrect number of arguments. Expecting 3")
}
C:=args[0]//serial number
valueC, err := stub.GetState(C)//get state of previous Order
var carrier Carrier//create order object
err = json.Unmarshal(valueC, &carrier)
C2 := &Carrier{
Name: args[0],
CurrentBal:args[1],
}
c1,_:=json.Marshal(C2)
serialnum:= args[0]
err = stub.PutState(serialnum, []byte(c1)) // passes serialNum as the key value for searching blockchain
if err != nil {
return nil, err
}
return nil,nil
}
//==============================================================================================================================
// Transfer funds
// Transfers from B to A for the amount of C
//==============================================================================================================================
func (t *SimpleChaincode) tradeFunds(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) {
A:=args[0]
B:=args[1]
C:=args[2]
amount,_:=strconv.Atoi(C)
//get the balances
var provider XOM
valueA, err := stub.GetState(A)
err=json.Unmarshal(valueA, &provider)
CBX,err:=strconv.Atoi(provider.CurrentBal)
var customer Customer
valueB, err := stub.GetState(B)
err=json.Unmarshal(valueB, &customer)
CBC,err:=strconv.Atoi(customer.CurrentBal)
//now the trade
CBC=CBC-amount
CBX=CBX+amount
customer.CurrentBal=strconv.Itoa(CBC)
provider.CurrentBal=strconv.Itoa(CBX)
a1,_:=json.Marshal(provider)
err = stub.PutState(A,[]byte(a1))
b1,_:=json.Marshal(customer)
err = stub.PutState(B,[]byte(b1))
if err != nil {
return nil, err
}
return nil,nil
}
//==============================================================================================================================
// XOM Creation
//==============================================================================================================================
func (t *SimpleChaincode) createXOM(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) {
if len(args) != 2 {
return nil, errors.New("Incorrect number of arguments. Expecting 3")
}
X:=args[0]//serial number
valueC, err := stub.GetState(X)//get state of previous Order
var carbon XOM//create order object
err = json.Unmarshal(valueC, &carbon)
X2 := &XOM{
Name: args[0],
CurrentBal:args[1],
}
x1,_:=json.Marshal(X2)
serialnum:= args[0]
err = stub.PutState(serialnum, []byte(x1)) // passes serialNum as the key value for searching blockchain
if err != nil {
return nil, err
}
return nil,nil
}
//==============================================================================================================================
// Customer Creation
//==============================================================================================================================
func (t *SimpleChaincode) createCustomer(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) {
if len(args) != 2 {
return nil, errors.New("Incorrect number of arguments. Expecting 3")
}
C:=args[0]//serial number
valueC, err := stub.GetState(C)//get state of previous Order
var customer Customer//create order object
err = json.Unmarshal(valueC, &customer)
C2 := &Customer{
Name: args[0],
CurrentBal:args[1],
}
c1,_:=json.Marshal(C2)
serialnum:= args[0]
err = stub.PutState(serialnum, []byte(c1)) // passes serialNum as the key value for searching blockchain
if err != nil {
return nil, err
}
return nil,nil
}
//==============================================================================================================================
// Read Customer
//==============================================================================================================================
func (t *SimpleChaincode) readCustomer(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) {
if len(args) != 1 {
return nil, errors.New("Incorrect number of arguments. Expecting Serial Number")
}
value, err := stub.GetState(args[0]) //args 0 = serialNum
if err != nil {
return nil, errors.New("blank error msg")
}
return value,nil
}
//==============================================================================================================================
// Order Creation
//==============================================================================================================================
func (t *SimpleChaincode) createOrder(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) {
if len(args) != 5 {
return nil, errors.New("Incorrect number of arguments. Expecting 5")
}
O:=args[0]//serial number
valueO, err := stub.GetState(O)//get state of previous Order
var order Order//create order object
err = json.Unmarshal(valueO, &order)
O2 := &Order{
SerialNum: args[0],
AckCus: args[1],
AckXom: args[2],
AckCarrier: args[3],
Cost: args[4],
}
o1,_:=json.Marshal(O2)
serialnum:= args[0]
err = stub.PutState(serialnum, []byte(o1)) // passes serialNum as the key value for searching blockchain
if err != nil {
return nil, err
}
return nil,nil
}
//==============================================================================================================================
// Read Order
//==============================================================================================================================
func (t *SimpleChaincode) readOrder(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) {
if len(args) != 1 {
return nil, errors.New("Incorrect number of arguments. Expecting Serial Number")
}
value, err := stub.GetState(args[0]) //args 0 = serialNum
if err != nil {
return nil, errors.New("blank error msg")
}
return value,nil
}
func main() {
err := shim.Start(new(SimpleChaincode))
if err != nil {
fmt.Printf("Error starting Simple chaincode: %s", err)
}
}