-
Notifications
You must be signed in to change notification settings - Fork 27
/
cp_cc.go
811 lines (700 loc) · 22.2 KB
/
cp_cc.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
/*
Copyright 2016 IBM
Licensed under the Apache License, Version 2.0 (the "License")
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Licensed Materials - Property of IBM
© Copyright IBM Corp. 2016
*/
package main
import (
"encoding/json"
"errors"
"fmt"
"strconv"
"time"
"strings"
"github.com/openblockchain/obc-peer/openchain/chaincode/shim"
)
var cpPrefix = "cp:"
var accountPrefix = "acct:"
var accountsKey = "accounts"
var recentLeapYear = 2016
// SimpleChaincode example simple Chaincode implementation
type SimpleChaincode struct {
}
func generateCUSIPSuffix(issueDate string, days int) (string, error) {
t, err := msToTime(issueDate)
if err != nil {
return "", err
}
maturityDate := t.AddDate(0, 0, days)
month := int(maturityDate.Month())
day := maturityDate.Day()
suffix := seventhDigit[month] + eigthDigit[day]
return suffix, nil
}
const (
millisPerSecond = int64(time.Second / time.Millisecond)
nanosPerMillisecond = int64(time.Millisecond / time.Nanosecond)
)
func msToTime(ms string) (time.Time, error) {
msInt, err := strconv.ParseInt(ms, 10, 64)
if err != nil {
return time.Time{}, err
}
return time.Unix(msInt/millisPerSecond,
(msInt%millisPerSecond)*nanosPerMillisecond), nil
}
type Owner struct {
Company string `json:"company"`
Quantity int `json:"quantity"`
}
type CP struct {
CUSIP string `json:"cusip"`
Ticker string `json:"ticker"`
Par float64 `json:"par"`
Qty int `json:"qty"`
Discount float64 `json:"discount"`
Maturity int `json:"maturity"`
Owners []Owner `json:"owner"`
Issuer string `json:"issuer"`
IssueDate string `json:"issueDate"`
}
type Account struct {
ID string `json:"id"`
Prefix string `json:"prefix"`
CashBalance float64 `json:"cashBalance"`
AssetsIds []string `json:"assetIds"`
}
type Transaction struct {
CUSIP string `json:"cusip"`
FromCompany string `json:"fromCompany"`
ToCompany string `json:"toCompany"`
Quantity int `json:"quantity"`
Discount float64 `json:"discount"`
}
func (t *SimpleChaincode) init(stub *shim.ChaincodeStub, args []string) ([]byte, error) {
// Initialize the collection of commercial paper keys
fmt.Println("Initializing paper keys collection")
var blank []string
blankBytes, _ := json.Marshal(&blank)
err := stub.PutState("PaperKeys", blankBytes)
if err != nil {
fmt.Println("Failed to initialize paper key collection")
}
fmt.Println("Initialization complete")
return nil, nil
}
func (t *SimpleChaincode) createAccounts(stub *shim.ChaincodeStub, args []string) ([]byte, error) {
// 0
// "number of accounts to create"
var err error
numAccounts, err := strconv.Atoi(args[0])
if err != nil {
fmt.Println("error creating accounts with input")
return nil, errors.New("createAccounts accepts a single integer argument")
}
//create a bunch of accounts
var account Account
counter := 1
for counter <= numAccounts {
var prefix string
suffix := "000A"
if counter < 10 {
prefix = strconv.Itoa(counter) + "0" + suffix
} else {
prefix = strconv.Itoa(counter) + suffix
}
var assetIds []string
account = Account{ID: "company" + strconv.Itoa(counter), Prefix: prefix, CashBalance: 10000000.0, AssetsIds: assetIds}
accountBytes, err := json.Marshal(&account)
if err != nil {
fmt.Println("error creating account" + account.ID)
return nil, errors.New("Error creating account " + account.ID)
}
err = stub.PutState(accountPrefix+account.ID, accountBytes)
counter++
fmt.Println("created account" + accountPrefix + account.ID)
}
fmt.Println("Accounts created")
return nil, nil
}
func (t *SimpleChaincode) createAccount(stub *shim.ChaincodeStub, args []string) ([]byte, error) {
// Obtain the username to associate with the account
if len(args) != 1 {
fmt.Println("Error obtaining username")
return nil, errors.New("createAccount accepts a single username argument")
}
username := args[0]
// Build an account object for the user
var assetIds []string
suffix := "000A"
prefix := username + suffix
var account = Account{ID: username, Prefix: prefix, CashBalance: 10000000.0, AssetsIds: assetIds}
accountBytes, err := json.Marshal(&account)
if err != nil {
fmt.Println("error creating account" + account.ID)
return nil, errors.New("Error creating account " + account.ID)
}
fmt.Println("Attempting to get state of any existing account for " + account.ID)
existingBytes, err := stub.GetState(accountPrefix + account.ID)
if err == nil {
var company Account
err = json.Unmarshal(existingBytes, &company)
if err != nil {
fmt.Println("Error unmarshalling account " + account.ID + "\n--->: " + err.Error())
if strings.Contains(err.Error(), "unexpected end") {
fmt.Println("No data means existing account found for " + account.ID + ", initializing account.")
err = stub.PutState(accountPrefix+account.ID, accountBytes)
if err == nil {
fmt.Println("created account" + accountPrefix + account.ID)
return nil, nil
} else {
fmt.Println("failed to create initialize account for " + account.ID)
return nil, errors.New("failed to initialize an account for " + account.ID + " => " + err.Error())
}
} else {
return nil, errors.New("Error unmarshalling existing account " + account.ID)
}
} else {
fmt.Println("Account already exists for " + account.ID + " " + company.ID)
return nil, errors.New("Can't reinitialize existing user " + account.ID)
}
} else {
fmt.Println("No existing account found for " + account.ID + ", initializing account.")
err = stub.PutState(accountPrefix+account.ID, accountBytes)
if err == nil {
fmt.Println("created account" + accountPrefix + account.ID)
return nil, nil
} else {
fmt.Println("failed to create initialize account for " + account.ID)
return nil, errors.New("failed to initialize an account for " + account.ID + " => " + err.Error())
}
}
}
func (t *SimpleChaincode) issueCommercialPaper(stub *shim.ChaincodeStub, args []string) ([]byte, error) {
/* 0
json
{
"ticker": "string",
"par": 0.00,
"qty": 10,
"discount": 7.5,
"maturity": 30,
"owners": [ // This one is not required
{
"company": "company1",
"quantity": 5
},
{
"company": "company3",
"quantity": 3
},
{
"company": "company4",
"quantity": 2
}
],
"issuer":"company2",
"issueDate":"1456161763790" (current time in milliseconds as a string)
}
*/
//need one arg
if len(args) != 1 {
fmt.Println("error invalid arguments")
return nil, errors.New("Incorrect number of arguments. Expecting commercial paper record")
}
var cp CP
var err error
var account Account
fmt.Println("Unmarshalling CP")
err = json.Unmarshal([]byte(args[0]), &cp)
if err != nil {
fmt.Println("error invalid paper issue")
return nil, errors.New("Invalid commercial paper issue")
}
//generate the CUSIP
//get account prefix
fmt.Println("Getting state of - " + accountPrefix + cp.Issuer)
accountBytes, err := stub.GetState(accountPrefix + cp.Issuer)
if err != nil {
fmt.Println("Error Getting state of - " + accountPrefix + cp.Issuer)
return nil, errors.New("Error retrieving account " + cp.Issuer)
}
err = json.Unmarshal(accountBytes, &account)
if err != nil {
fmt.Println("Error Unmarshalling accountBytes")
return nil, errors.New("Error retrieving account " + cp.Issuer)
}
account.AssetsIds = append(account.AssetsIds, cp.CUSIP)
// Set the issuer to be the owner of all quantity
var owner Owner
owner.Company = cp.Issuer
owner.Quantity = cp.Qty
cp.Owners = append(cp.Owners, owner)
suffix, err := generateCUSIPSuffix(cp.IssueDate, cp.Maturity)
if err != nil {
fmt.Println("Error generating cusip")
return nil, errors.New("Error generating CUSIP")
}
fmt.Println("Marshalling CP bytes")
cp.CUSIP = account.Prefix + suffix
fmt.Println("Getting State on CP " + cp.CUSIP)
cpRxBytes, err := stub.GetState(cpPrefix+cp.CUSIP)
if cpRxBytes == nil {
fmt.Println("CUSIP does not exist, creating it")
cpBytes, err := json.Marshal(&cp)
if err != nil {
fmt.Println("Error marshalling cp")
return nil, errors.New("Error issuing commercial paper")
}
err = stub.PutState(cpPrefix+cp.CUSIP, cpBytes)
if err != nil {
fmt.Println("Error issuing paper")
return nil, errors.New("Error issuing commercial paper")
}
fmt.Println("Marshalling account bytes to write")
accountBytesToWrite, err := json.Marshal(&account)
if err != nil {
fmt.Println("Error marshalling account")
return nil, errors.New("Error issuing commercial paper")
}
err = stub.PutState(accountPrefix + cp.Issuer, accountBytesToWrite)
if err != nil {
fmt.Println("Error putting state on accountBytesToWrite")
return nil, errors.New("Error issuing commercial paper")
}
// Update the paper keys by adding the new key
fmt.Println("Getting Paper Keys")
keysBytes, err := stub.GetState("PaperKeys")
if err != nil {
fmt.Println("Error retrieving paper keys")
return nil, errors.New("Error retrieving paper keys")
}
var keys []string
err = json.Unmarshal(keysBytes, &keys)
if err != nil {
fmt.Println("Error unmarshel keys")
return nil, errors.New("Error unmarshalling paper keys ")
}
fmt.Println("Appending the new key to Paper Keys")
foundKey := false
for _, key := range keys {
if key == cpPrefix+cp.CUSIP {
foundKey = true
}
}
if foundKey == false {
keys = append(keys, cpPrefix+cp.CUSIP)
keysBytesToWrite, err := json.Marshal(&keys)
if err != nil {
fmt.Println("Error marshalling keys")
return nil, errors.New("Error marshalling the keys")
}
fmt.Println("Put state on PaperKeys")
err = stub.PutState("PaperKeys", keysBytesToWrite)
if err != nil {
fmt.Println("Error writting keys back")
return nil, errors.New("Error writing the keys back")
}
}
fmt.Println("Issue commercial paper %+v\n", cp)
return nil, nil
} else {
fmt.Println("CUSIP exists")
var cprx CP
fmt.Println("Unmarshalling CP " + cp.CUSIP)
err = json.Unmarshal(cpRxBytes, &cprx)
if err != nil {
fmt.Println("Error unmarshalling cp " + cp.CUSIP)
return nil, errors.New("Error unmarshalling cp " + cp.CUSIP)
}
cprx.Qty = cprx.Qty + cp.Qty
for key, val := range cprx.Owners {
if val.Company == cp.Issuer {
cprx.Owners[key].Quantity += cp.Qty
break
}
}
cpWriteBytes, err := json.Marshal(&cprx)
if err != nil {
fmt.Println("Error marshalling cp")
return nil, errors.New("Error issuing commercial paper")
}
err = stub.PutState(cpPrefix+cp.CUSIP, cpWriteBytes)
if err != nil {
fmt.Println("Error issuing paper")
return nil, errors.New("Error issuing commercial paper")
}
fmt.Println("Updated commercial paper %+v\n", cprx)
return nil, nil
}
}
func GetAllCPs(stub *shim.ChaincodeStub) ([]CP, error){
var allCPs []CP
// Get list of all the keys
keysBytes, err := stub.GetState("PaperKeys")
if err != nil {
fmt.Println("Error retrieving paper keys")
return nil, errors.New("Error retrieving paper keys")
}
var keys []string
err = json.Unmarshal(keysBytes, &keys)
if err != nil {
fmt.Println("Error unmarshalling paper keys")
return nil, errors.New("Error unmarshalling paper keys")
}
// Get all the cps
for _, value := range keys {
cpBytes, err := stub.GetState(value)
var cp CP
err = json.Unmarshal(cpBytes, &cp)
if err != nil {
fmt.Println("Error retrieving cp " + value)
return nil, errors.New("Error retrieving cp " + value)
}
fmt.Println("Appending CP" + value)
allCPs = append(allCPs, cp)
}
return allCPs, nil
}
func GetCP(cpid string, stub *shim.ChaincodeStub) (CP, error){
var cp CP
cpBytes, err := stub.GetState(cpid)
if err != nil {
fmt.Println("Error retrieving cp " + cpid)
return cp, errors.New("Error retrieving cp " + cpid)
}
err = json.Unmarshal(cpBytes, &cp)
if err != nil {
fmt.Println("Error unmarshalling cp " + cpid)
return cp, errors.New("Error unmarshalling cp " + cpid)
}
return cp, nil
}
func GetCompany(companyID string, stub *shim.ChaincodeStub) (Account, error){
var company Account
companyBytes, err := stub.GetState(accountPrefix+companyID)
if err != nil {
fmt.Println("Account not found " + companyID)
return company, errors.New("Account not found " + companyID)
}
err = json.Unmarshal(companyBytes, &company)
if err != nil {
fmt.Println("Error unmarshalling account " + companyID + "\n err:" + err.Error())
return company, errors.New("Error unmarshalling account " + companyID)
}
return company, nil
}
// Still working on this one
func (t *SimpleChaincode) transferPaper(stub *shim.ChaincodeStub, args []string) ([]byte, error) {
/* 0
json
{
"CUSIP": "",
"fromCompany":"",
"toCompany":"",
"quantity": 1
}
*/
//need one arg
if len(args) != 1 {
return nil, errors.New("Incorrect number of arguments. Expecting commercial paper record")
}
var tr Transaction
fmt.Println("Unmarshalling Transaction")
err := json.Unmarshal([]byte(args[0]), &tr)
if err != nil {
fmt.Println("Error Unmarshalling Transaction")
return nil, errors.New("Invalid commercial paper issue")
}
fmt.Println("Getting State on CP " + tr.CUSIP)
cpBytes, err := stub.GetState(cpPrefix+tr.CUSIP)
if err != nil {
fmt.Println("CUSIP not found")
return nil, errors.New("CUSIP not found " + tr.CUSIP)
}
var cp CP
fmt.Println("Unmarshalling CP " + tr.CUSIP)
err = json.Unmarshal(cpBytes, &cp)
if err != nil {
fmt.Println("Error unmarshalling cp " + tr.CUSIP)
return nil, errors.New("Error unmarshalling cp " + tr.CUSIP)
}
var fromCompany Account
fmt.Println("Getting State on fromCompany " + tr.FromCompany)
fromCompanyBytes, err := stub.GetState(accountPrefix+tr.FromCompany)
if err != nil {
fmt.Println("Account not found " + tr.FromCompany)
return nil, errors.New("Account not found " + tr.FromCompany)
}
fmt.Println("Unmarshalling FromCompany ")
err = json.Unmarshal(fromCompanyBytes, &fromCompany)
if err != nil {
fmt.Println("Error unmarshalling account " + tr.FromCompany)
return nil, errors.New("Error unmarshalling account " + tr.FromCompany)
}
var toCompany Account
fmt.Println("Getting State on ToCompany " + tr.ToCompany)
toCompanyBytes, err := stub.GetState(accountPrefix+tr.ToCompany)
if err != nil {
fmt.Println("Account not found " + tr.ToCompany)
return nil, errors.New("Account not found " + tr.ToCompany)
}
fmt.Println("Unmarshalling tocompany")
err = json.Unmarshal(toCompanyBytes, &toCompany)
if err != nil {
fmt.Println("Error unmarshalling account " + tr.ToCompany)
return nil, errors.New("Error unmarshalling account " + tr.ToCompany)
}
// Check for all the possible errors
ownerFound := false
quantity := 0
for _, owner := range cp.Owners {
if owner.Company == tr.FromCompany {
ownerFound = true
quantity = owner.Quantity
}
}
// If fromCompany doesn't own this paper
if ownerFound == false {
fmt.Println("The company " + tr.FromCompany + "doesn't own any of this paper")
return nil, errors.New("The company " + tr.FromCompany + "doesn't own any of this paper")
} else {
fmt.Println("The FromCompany does own this paper")
}
// If fromCompany doesn't own enough quantity of this paper
if quantity < tr.Quantity {
fmt.Println("The company " + tr.FromCompany + "doesn't own enough of this paper")
return nil, errors.New("The company " + tr.FromCompany + "doesn't own enough of this paper")
} else {
fmt.Println("The FromCompany owns enough of this paper")
}
amountToBeTransferred := float64(tr.Quantity) * cp.Par
amountToBeTransferred -= (amountToBeTransferred) * (cp.Discount / 100.0) * (float64(cp.Maturity) / 360.0)
// If toCompany doesn't have enough cash to buy the papers
if toCompany.CashBalance < amountToBeTransferred {
fmt.Println("The company " + tr.ToCompany + "doesn't have enough cash to purchase the papers")
return nil, errors.New("The company " + tr.ToCompany + "doesn't have enough cash to purchase the papers")
} else {
fmt.Println("The ToCompany has enough money to be transferred for this paper")
}
toCompany.CashBalance -= amountToBeTransferred
fromCompany.CashBalance += amountToBeTransferred
toOwnerFound := false
for key, owner := range cp.Owners {
if owner.Company == tr.FromCompany {
fmt.Println("Reducing Quantity from the FromCompany")
cp.Owners[key].Quantity -= tr.Quantity
// owner.Quantity -= tr.Quantity
}
if owner.Company == tr.ToCompany {
fmt.Println("Increasing Quantity from the ToCompany")
toOwnerFound = true
cp.Owners[key].Quantity += tr.Quantity
// owner.Quantity += tr.Quantity
}
}
if toOwnerFound == false {
var newOwner Owner
fmt.Println("As ToOwner was not found, appending the owner to the CP")
newOwner.Quantity = tr.Quantity
newOwner.Company = tr.ToCompany
cp.Owners = append(cp.Owners, newOwner)
}
fromCompany.AssetsIds = append(fromCompany.AssetsIds, tr.CUSIP)
// Write everything back
// To Company
toCompanyBytesToWrite, err := json.Marshal(&toCompany)
if err != nil {
fmt.Println("Error marshalling the toCompany")
return nil, errors.New("Error marshalling the toCompany")
}
fmt.Println("Put state on toCompany")
err = stub.PutState(accountPrefix+tr.ToCompany, toCompanyBytesToWrite)
if err != nil {
fmt.Println("Error writing the toCompany back")
return nil, errors.New("Error writing the toCompany back")
}
// From company
fromCompanyBytesToWrite, err := json.Marshal(&fromCompany)
if err != nil {
fmt.Println("Error marshalling the fromCompany")
return nil, errors.New("Error marshalling the fromCompany")
}
fmt.Println("Put state on fromCompany")
err = stub.PutState(accountPrefix+tr.FromCompany, fromCompanyBytesToWrite)
if err != nil {
fmt.Println("Error writing the fromCompany back")
return nil, errors.New("Error writing the fromCompany back")
}
// cp
cpBytesToWrite, err := json.Marshal(&cp)
if err != nil {
fmt.Println("Error marshalling the cp")
return nil, errors.New("Error marshalling the cp")
}
fmt.Println("Put state on CP")
err = stub.PutState(cpPrefix+tr.CUSIP, cpBytesToWrite)
if err != nil {
fmt.Println("Error writing the cp back")
return nil, errors.New("Error writing the cp back")
}
fmt.Println("Successfully completed Invoke")
return nil, nil
}
func (t *SimpleChaincode) Query(stub *shim.ChaincodeStub, function string, args []string) ([]byte, error) {
//need one arg
if len(args) < 1 {
return nil, errors.New("Incorrect number of arguments. Expecting ......")
}
if args[0] == "GetAllCPs" {
fmt.Println("Getting all CPs")
allCPs, err := GetAllCPs(stub)
if err != nil {
fmt.Println("Error from getallcps")
return nil, err
} else {
allCPsBytes, err1 := json.Marshal(&allCPs)
if err1 != nil {
fmt.Println("Error marshalling allcps")
return nil, err1
}
fmt.Println("All success, returning allcps")
return allCPsBytes, nil
}
} else if args[0] == "GetCP" {
fmt.Println("Getting particular cp")
cp, err := GetCP(args[1], stub)
if err != nil {
fmt.Println("Error Getting particular cp")
return nil, err
} else {
cpBytes, err1 := json.Marshal(&cp)
if err1 != nil {
fmt.Println("Error marshalling the cp")
return nil, err1
}
fmt.Println("All success, returning the cp")
return cpBytes, nil
}
} else if args[0] == "GetCompany" {
fmt.Println("Getting the company")
company, err := GetCompany(args[1], stub)
if err != nil {
fmt.Println("Error from getCompany")
return nil, err
} else {
companyBytes, err1 := json.Marshal(&company)
if err1 != nil {
fmt.Println("Error marshalling the company")
return nil, err1
}
fmt.Println("All success, returning the company")
return companyBytes, nil
}
} else {
fmt.Println("Generic Query call")
bytes, err := stub.GetState(args[0])
if err != nil {
fmt.Println("Some error happenend")
return nil, errors.New("Some Error happened")
}
fmt.Println("All success, returning from generic")
return bytes, nil
}
}
func (t *SimpleChaincode) Run(stub *shim.ChaincodeStub, function string, args []string) ([]byte, error) {
fmt.Println("run is running " + function)
if function == "issueCommercialPaper" {
fmt.Println("Firing issueCommercialPaper")
//Create an asset with some value
return t.issueCommercialPaper(stub, args)
} else if function == "transferPaper" {
fmt.Println("Firing cretransferPaperateAccounts")
return t.transferPaper(stub, args)
} else if function == "createAccounts" {
fmt.Println("Firing createAccounts")
return t.createAccounts(stub, args)
} else if function == "createAccount" {
fmt.Println("Firing createAccount")
return t.createAccount(stub, args)
} else if function == "init" {
fmt.Println("Firing init")
return t.init(stub, args)
}
return nil, errors.New("Received unknown function invocation")
}
func main() {
err := shim.Start(new(SimpleChaincode))
if err != nil {
fmt.Println("Error starting Simple chaincode: %s", err)
}
}
//lookup tables for last two digits of CUSIP
var seventhDigit = map[int]string{
1: "A",
2: "B",
3: "C",
4: "D",
5: "E",
6: "F",
7: "G",
8: "H",
9: "J",
10: "K",
11: "L",
12: "M",
13: "N",
14: "P",
15: "Q",
16: "R",
17: "S",
18: "T",
19: "U",
20: "V",
21: "W",
22: "X",
23: "Y",
24: "Z",
}
var eigthDigit = map[int]string{
1: "1",
2: "2",
3: "3",
4: "4",
5: "5",
6: "6",
7: "7",
8: "8",
9: "9",
10: "A",
11: "B",
12: "C",
13: "D",
14: "E",
15: "F",
16: "G",
17: "H",
18: "J",
19: "K",
20: "L",
21: "M",
22: "N",
23: "P",
24: "Q",
25: "R",
26: "S",
27: "T",
28: "U",
29: "V",
30: "W",
31: "X",
}