This repository was archived by the owner on Jul 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathwrite_ledger.go
1184 lines (1028 loc) · 38.3 KB
/
write_ledger.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
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you 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.
*/
package main
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"reflect"
"math"
"time"
// "encoding/gob"
// "bytes"
"github.com/hyperledger/fabric/core/chaincode/shim"
pb "github.com/hyperledger/fabric/protos/peer"
)
// ============================================================================================================================
// write() - genric write variable into ledger
//
// Shows Off PutState() - writting a key/value into the ledger
//
// Inputs - Array of strings
// 0 , 1
// key , value
// "abc" , "test"
// ============================================================================================================================
func write(stub shim.ChaincodeStubInterface, args []string) pb.Response {
var key, value string
var err error
fmt.Println("starting write")
if len(args) != 2 {
return shim.Error("Incorrect number of arguments. Expecting 2. key of the variable and value to set")
}
// input sanitation
err = sanitize_arguments(args)
if err != nil {
return shim.Error(err.Error())
}
key = args[0] //rename for funsies
value = args[1]
err = stub.PutState(key, []byte(value)) //write the variable into the ledger
if err != nil {
return shim.Error(err.Error())
}
fmt.Println("- end write")
return shim.Success(nil)
}
// ============================================================================================================================
// delete_marble() - remove a marble from state and from marble index
//
// Shows Off DelState() - "removing"" a key/value from the ledger
//
// Inputs - Array of strings
// 0 , 1
// id , authed_by_company
// "m999999999", "united marbles"
// ============================================================================================================================
func delete(stub shim.ChaincodeStubInterface, args []string) (pb.Response) {
fmt.Println("starting delete")
id := args[0]
err := stub.DelState(id) //remove the key from chaincode state
if err != nil {
return shim.Error("Failed to delete state")
}
fmt.Println("- end delete")
return shim.Success(nil)
}
// ============================================================================================================================
// Init Asset - create a new asset, store into chaincode state
//
// Shows off building a key's JSON value manually
//
// Inputs - Array of strings
// 0 , 1 , 2 , 3 , 4 , 5 , 6
// id , loan amount , borrower_info , , state , interest , balance due , grade
// "m999999999", "545,000" , object deliquent/in payment , 3.0 , 520,000 , BBB
// credit/income verification/debt to income,
// ============================================================================================================================
func init_asset(stub shim.ChaincodeStubInterface, args []string) (pb.Response) {
var err error
fmt.Println("starting init_asset")
// if len(args) != 5 {
// return shim.Error("Incorrect number of arguments. Expecting 5")
// }
//input sanitation
// err = sanitize_arguments(args)
// if err != nil {
// return shim.Error(err.Error())
// }
// asset_id := args[0]
// // initial_amount := args[1] //strings.ToLower(args[1])
// balance := args[1]
// // state := args[2]
// interest := args[2]
// // underwriting, err := json.Marshal(args[5]) //make(map(args[5]))
// // monthlyPayment := args[3]
// remainingpayments := args[3]
// underwriting := args[4] //make(map(args[5]))
// state := "active"
var asset Asset
asset.Id = args[0]
asset.Balance = args[1]
asset.InterestRate = args[2]
asset.RemainingPayments = args[3]
fmt.Println(asset)
// asset.Underwriting = args[4]
asset.State = "active"
// asset.ObjectType = "fin_asset"
// originator.Username = strings.ToLower(args[1])
// originator.Enabled = true
//check if user already exists
// TODO, uncomment
// _, err = get_originator(stub, originator.Id)
// if err == nil {
// fmt.Println("This originator already exists - " + originator.Id)
// return shim.Error("This originator already exists - " + originator.Id)
// }
//store user
balance, _ := strconv.ParseFloat(asset.Balance, 32)
// fmt.Println("balance set")
interest, _ := strconv.ParseFloat(asset.InterestRate, 32)
// fmt.Println("interest set")
remainingPayments, _ := strconv.ParseFloat(asset.RemainingPayments, 32)
// fmt.Println("remaining set")
trueValue := (((interest / 12.0) * balance * remainingPayments) / (1.0 - math.Pow( ( (1.0 + ( interest / 12.0)) ), (-1.0 * remainingPayments) )))
// fmt.Println("trueValue")
// fmt.Println(trueValue)
// if math.IsNaN(trueValue) {
// fmt.Println("missing parameters")
// return shim.Error("missing parameters")
// }
asset.ExpectedPayoffAmount = strconv.FormatFloat(trueValue, 'f', 2, 64)
assetAsBytes, _ := json.Marshal(asset) //convert to array of bytes
fmt.Println("writing asset to state")
fmt.Println(string(assetAsBytes))
err = stub.PutState(asset.Id, assetAsBytes) //store owner by its Id
if err != nil {
fmt.Println("Could not store asset")
return shim.Error(err.Error())
}
// build the marble json string manually
// monthlyPayment := calculate_monthly_payment(balance, interest, remainingpayments)
// str := `{
// "docType":"asset",
// "id": "` + asset_id + `",
// "state": "` + state + `",
// "interest": "` + interest + `",
// "balance": "` + balance + `",
// "remainingpayments": "` + remainingpayments + `",
// "underwriting":"` + underwriting + `"
// }`
// fmt.Println("asset str")
// fmt.Println(str)
// err = stub.PutState(asset_id, []byte(str)) //store marble with id as key
// if err != nil {
// return shim.Error(err.Error())
// }
fmt.Println("- end init_asset")
// calculate payoff amount
// value_asset(stub, []string{asset_id})
return shim.Success(nil)
}
// init_asset_pool
// str := `{
// "docType":"pool",
// "id": "` + id + `",
// "rating": "` + rating + `",
// "assets": "` + [] + `",
// }`
// update_asset
// generate_securities
// pool_asset (asset)
// underwriting info gives grade
// grade determines pool
// pool determines return, but also likliehood of failure/delinquency
// ============================================================================================================================
// Init Owner - create a new owner aka end user, store into chaincode state
//
// Shows off building key's value from GoLang Structure
//
// Inputs - Array of Strings
// 0 , 1 , 2
// owner id , username, company
// "o9999999999999", bob", "united marbles"
// ============================================================================================================================
func init_originator(stub shim.ChaincodeStubInterface, args []string) pb.Response {
var err error
fmt.Println("starting init_originator")
// if len(args) != 3 {
// return shim.Error("Incorrect number of arguments. Expecting 3")
// }
//input sanitation
err = sanitize_arguments(args)
if err != nil {
return shim.Error(err.Error())
}
var originator Originator
// originator.ObjectType = "asset_originator"
originator.Id = args[0]
// originator.Username = strings.ToLower(args[1])
originator.Company = args[1]
originator.ProcessingFee = args[2]
// originator.Enabled = true
fmt.Println(originator)
//check if user already exists
// TODO, uncomment
// _, err = get_originator(stub, originator.Id)
// if err == nil {
// fmt.Println("This originator already exists - " + originator.Id)
// return shim.Error("This originator already exists - " + originator.Id)
// }
//store user
originatorAsBytes, _ := json.Marshal(originator) //convert to array of bytes
err = stub.PutState(originator.Id, originatorAsBytes) //store owner by its Id
if err != nil {
fmt.Println("Could not store originator")
return shim.Error(err.Error())
}
fmt.Println("- end init_originator")
return shim.Success(nil)
}
// each asset should have an originator. originator and investor will receive set portion of mortgage payments
// originator has to be created here before bringing in an asset
func set_originator(stub shim.ChaincodeStubInterface, args []string) pb.Response {
var err error
fmt.Println("starting set_originator")
// this is quirky
// todo - get the "company that authed the transfer" from the certificate instead of an argument
// should be possible since we can now add attributes to the enrollment cert
// as is.. this is a bit broken (security wise), but it's much much easier to demo! holding off for demos sake
// if len(args) != 3 {
// return shim.Error("Incorrect number of arguments. Expecting 3")
// }
// input sanitation
err = sanitize_arguments(args)
if err != nil {
return shim.Error(err.Error())
}
// asset id
// processing fee
var asset_id = args[0]
var originator_id = args[1]
assetAsBytes, err := stub.GetState(asset_id)
if err != nil {
return shim.Error("Failed to get asset")
}
asset := Asset{}
err = json.Unmarshal(assetAsBytes, &asset) //un stringify it aka JSON.parse()
if err != nil {
// fmt.Printf("%+v\n", security)
fmt.Println(string(assetAsBytes))
return shim.Error(err.Error())
}
originatorAsBytes, err := stub.GetState(originator_id)
if err != nil {
return shim.Error("Failed to get originator")
}
originator := Originator{}
json.Unmarshal(originatorAsBytes, &originator) //un stringify it aka JSON.parse()
fmt.Println("asset.Id")
fmt.Println(asset.Id)
fmt.Println("originator.Assets")
fmt.Println(originator.Assets)
fmt.Println("checking for asset")
assetInArray, _ := InArray(asset.Id , originator.Assets)
fmt.Println("assetInArray")
fmt.Println(assetInArray)
if !assetInArray {
fmt.Println("adding asset to originator array")
// return shim.Error("security already purchased")
originator.Assets = append( originator.Assets, asset.Id )
originatorAsBytes, _ := json.Marshal(originator) //convert to array of bytes
fmt.Println(string(originatorAsBytes))
fmt.Println("originator_id")
fmt.Println(originator_id)
err = stub.PutState(originator_id, originatorAsBytes) //rewrite the marble with id as key
if err != nil {
return shim.Error(err.Error())
}
}
// check authorizing company
// if res.Owner.Company != authed_by_company{
// return shim.Error("The company '" + authed_by_company + "' cannot authorize transfers for '" + res.Owner.Company + "'.")
// }
// transfer the asset
asset.Originator = originator_id //change the owner
asset_balance, _ := strconv.ParseFloat(asset.Balance, 32)
originator_processing_fee, _ := strconv.ParseFloat(originator.ProcessingFee, 32)
asset_remaining_payments, _ := strconv.ParseFloat(asset.RemainingPayments, 32)
asset.ProcessingPayment = strconv.FormatFloat(((asset_balance * originator_processing_fee) / asset_remaining_payments ), 'f', 6, 64)
// res.Owner.Username = owner.Username
// res.Owner.Company = owner.Company
assetAsBytes, _ = json.Marshal(asset) //convert to array of bytes
fmt.Println(string(assetAsBytes))
err = stub.PutState(asset_id, assetAsBytes) //rewrite the marble with id as key
if err != nil {
return shim.Error(err.Error())
}
fmt.Println("- end set_originator")
return shim.Success(nil)
}
func init_asset_pool(stub shim.ChaincodeStubInterface, args []string) (pb.Response) {
var err error
fmt.Println("starting init_asset_pool")
// if len(args) != 5 {
// return shim.Error("Incorrect number of arguments. Expecting 5")
// }
//input sanitation
// err = sanitize_arguments(args)
// if err != nil {
// return shim.Error(err.Error())
// }
// id := args[0]
// rating := args[1] //strings.ToLower(args[1])
var pool Pool
pool.Id = args[0]
// Grades
// AAA, AA, A, BAA, BA, B, CAA, CA, C
// pool, err := get_asset_pool(stub, id)
// if err == nil {
// fmt.Println("This asset pool already exists - " + id)
// fmt.Println(asset)
// return shim.Error("This asset pool already exists - " + id) //all stop a marble by this id exists
// }
//build the marble json string manually
// str := `{
// "docType":"assetPool",
// "id": "` + id + `",
// }`
poolAsBytes, _ := json.Marshal(pool) //convert to array of bytes
err = stub.PutState(pool.Id, poolAsBytes) //store owner by its Id
// err = stub.PutState(id, []byte(str)) //store marble with id as key
if err != nil {
return shim.Error(err.Error())
}
fmt.Println("- end init_asset_pool")
return shim.Success(nil)
}
// pool_asset (asset_id, pool_id)
// func loadAssetJSON(stub, id, type) {
// objectAsBytes, err := stub.GetState(id)
// if err != nil {
// return shim.Error("Failed to get asset")
// }
// object := type{}
// json.Unmarshal(objectAsBytes, &object) //un stringify it aka JSON.parse()
// return object
// }
// fmt.Println(reflect.TypeOf("Asset"))
func pool_asset(stub shim.ChaincodeStubInterface, args []string) pb.Response {
var err error
fmt.Println("starting pool_asset")
// input sanitation
err = sanitize_arguments(args)
if err != nil {
return shim.Error(err.Error())
}
var asset_id = args[0]
var pool_id = args[1]
// var authed_by_company = args[2]
// fmt.Println(marble_id + "->" + new_owner_id + " - |" + authed_by_company)
// check if user already exists
// owner, err := get_originator(stub, new_owner_id)
// if err != nil {
// return shim.Error("This owner does not exist - " + new_owner_id)
// }
// set pool id in Asset object
assetAsBytes, err := stub.GetState(asset_id)
if err != nil {
return shim.Error("Failed to get asset")
}
asset := Asset{}
json.Unmarshal(assetAsBytes, &asset) //un stringify it aka JSON.parse()
asset.Pool = pool_id //change the owner
jsonAsBytes, _ := json.Marshal(asset) //convert to array of bytes
err = stub.PutState(asset_id, jsonAsBytes) //rewrite the marble with id as key
if err != nil {
return shim.Error(err.Error())
}
// add asset id to "Assets" array in pool
poolAsBytes, err := stub.GetState(pool_id)
if err != nil {
return shim.Error("Failed to get asset pool")
}
pool := Pool{}
json.Unmarshal(poolAsBytes, &pool) //un stringify it aka JSON.parse()
assetInArray, _ := InArray(asset.Id, pool.Assets)
if !assetInArray {
fmt.Println("adding asset to pool")
pool.Assets = append(pool.Assets, asset.Id)
poolAsBytes, _ = json.Marshal(pool) //convert to array of bytes
err = stub.PutState(pool_id, poolAsBytes) //rewrite the marble with id as key
if err != nil {
return shim.Error(err.Error())
}
} else {
fmt.Println("asset already in pool, skipping")
}
// pool.Assets = append(pool.Assets, asset_id)
// assetArray := []string{asset_id} //[]string{pool_id, resPool.Assets}
// resPool.Assets = assetArray
// updatedAssets := [string]{resPool.Assets, pool_id}
// poolAsBytes, _ = json.Marshal(pool) //convert to array of bytes
// err = stub.PutState(pool_id, poolAsBytes) //rewrite the marble with id as key
// if err != nil {
// return shim.Error(err.Error())
// }
fmt.Println("- end pool_asset")
return shim.Success(nil)
}
// To keep things simple we'll just give ratings of A, B, C
// Ratings should be updated when underwriting information or asset state change
func rate_asset(stub shim.ChaincodeStubInterface, args []string) pb.Response {
var err error
fmt.Println("starting rate_asset")
// input sanitation
err = sanitize_arguments(args)
if err != nil {
return shim.Error(err.Error())
}
var asset_id = args[0]
// get marble's current state
assetAsBytes, err := stub.GetState(asset_id)
if err != nil {
return shim.Error("Failed to get asset")
}
res := Asset{}
json.Unmarshal(assetAsBytes, &res) //un stringify it aka JSON.parse()
// res.Underwriting
// check authorizing company
// if res.Owner.Company != authed_by_company{
// return shim.Error("The company '" + authed_by_company + "' cannot authorize transfers for '" + res.Owner.Company + "'.")
// }
// for simplicity, lets just use FICO to determine grade
// 615 minimum
// FICO, err := strconv.Atoi(res.Underwriting.FICO)
FICO, err := strconv.Atoi(res.Underwriting)
rating := ""
switch {
case FICO < 615:
rating = "C"
case 615 < FICO && FICO < 699:
rating = "B"
case 700 < FICO:
rating = "A"
}
res.Rating = rating
//change the owner
// res.Underwriting.DebtToIncome
// res.Owner.Username = owner.Username
// res.Owner.Company = owner.Company
jsonAsBytes, _ := json.Marshal(res) //convert to array of bytes
err = stub.PutState(args[0], jsonAsBytes) //rewrite the marble with id as key
if err != nil {
return shim.Error(err.Error())
}
fmt.Println("- end rate_asset")
return shim.Success(nil)
}
// https://onlinelibrary.wiley.com/doi/full/10.1002/9780470404324.hof003039
// the value of the security is equal to the value of the expected cash flows
// generate_securities (pool, rating)
// sell_securities (investor, amount)
// function value_securities (investor, pool, amount)
// process_payment (asset)
// each time a payment is processed, security value should drop?
func process_payment(stub shim.ChaincodeStubInterface, args []string) pb.Response {
var err error
fmt.Println("starting process_payment")
// input sanitation
err = sanitize_arguments(args)
if err != nil {
return shim.Error(err.Error())
}
var asset_id = args[0]
assetAsBytes, err := stub.GetState(asset_id)
if err != nil {
return shim.Error("Failed to get asset")
}
asset := Asset{}
fmt.Println("assetAsBytes")
fmt.Println(string(assetAsBytes))
err = json.Unmarshal(assetAsBytes, &asset) //un stringify it aka JSON.parse()
if err != nil {
return shim.Error(err.Error())
// return shim.Error("Failed to unmarshal json")
}
originator := Originator{}
originatorAsBytes, err := stub.GetState(asset.Originator)
err = json.Unmarshal(originatorAsBytes, &originator) //un stringify it aka JSON.parse()
paymentAmount, err := strconv.ParseFloat(args[1], 32)
balance, err := strconv.ParseFloat(asset.Balance, 32)
interestRate, err := strconv.ParseFloat(asset.InterestRate, 32)
processingFee, err := strconv.ParseFloat(originator.ProcessingFee, 32)
paymentsLeft, err := strconv.Atoi(asset.RemainingPayments)
// TODO
// percent.Percent(25, 2000) // return 500
paymentsPerYear := 12.0 // assuming interest rate is a yearly percentage
interestPayment := ((balance * interestRate) / paymentsPerYear)
processingPayment, _ := strconv.ParseFloat(asset.ProcessingPayment, 32) //:= processingFee * paymentAmount
principalPayment := paymentAmount - interestPayment - processingFee
// updated := balance - principalPayment
updatedBalance := balance - principalPayment
// updatedBalance := strconv.FormatFloat((balance - principalPayment), 'f', 2, 64)
fmt.Println("initial asset.Balance")
fmt.Println(asset.Balance)
asset.Balance = strconv.FormatFloat(updatedBalance, 'f', 2, 64)
asset.RemainingPayments = strconv.Itoa(paymentsLeft - 1)
// asset.Balance = updatedBalance
// investorBalance = strconv.FormatFloat(investor.Balance, 'f', 2, 64)
assetAsBytes, err = json.Marshal(asset)
if err != nil {
fmt.Println("Failed to Marshal asset")
fmt.Println(err)
return shim.Error("Failed to Marshal asset")
}
fmt.Println("Marshal assetAsBytes")
fmt.Println(string(assetAsBytes))
err = stub.PutState(asset.Id, assetAsBytes)
if err != nil {
return shim.Error("Failed to save asset")
fmt.Println("Failed to save asset")
fmt.Println(err)
}
// get investors
// get originator
// if asset.Originator != nil {
// originator = stub.GetState(asset.Originator)
// originatorBalance, err := strconv.ParseFloat(asset.Originator.Balance, 32)
fmt.Println("asset.Originator.Balance before")
fmt.Println(originator.Balance)
fmt.Println("asset.ProcessingPayment")
fmt.Println(asset.ProcessingPayment)
originatorBalance, _ := strconv.ParseFloat(originator.Balance, 32)
updatedOriginatorBalance := processingPayment + originatorBalance
originator.Balance = strconv.FormatFloat(updatedOriginatorBalance, 'f', 2, 64)
originatorAsBytes, err = json.Marshal(originator)
fmt.Println("asset.Originator.Id")
err = stub.PutState(asset.Originator, originatorAsBytes)
// }
// pay their portion
// TODO, remove this
// get remaining assets from pool
pool_id := asset.Pool
poolAsBytes, err := stub.GetState(pool_id)
if err != nil {
return shim.Error("Failed to get pool")
}
pool := Pool{}
fmt.Println("poolAsBytes")
fmt.Println(string(poolAsBytes))
err = json.Unmarshal(poolAsBytes, &pool) //un stringify it aka JSON.parse()
fmt.Println("pool")
fmt.Printf("%+v\n", pool)
// fmt.Println(pool.Assets)
// fmt.Println("pool.Investors")
// fmt.Println(pool.Investors)
// fmt.Println("security.Investor")
// TODO, protect against panic when array empty
// if ( len(pool.Securities) > 0) {
// fmt.Printf("%+v\n", pool.Securities)
// }
// fmt.Println("security")
// fmt.Printf("%+v\n", security)
for _, security_id := range pool.Securities {
// load each security and investor
// increase investor balance
securityAsBytes, err := stub.GetState(security_id)
security := Security{}
err = json.Unmarshal(securityAsBytes, &security) //un stringify it aka JSON.parse()
if err != nil {
return shim.Error("Failed to get security")
}
fmt.Println("processingPayment for amount:")
fmt.Println(processingPayment)
if security.Investor != "" {
fmt.Println("fetching investor: " + security.Investor)
investorAsBytes, err := stub.GetState(security.Investor)
investor := Investor{}
json.Unmarshal(investorAsBytes, &investor) //un stringify it aka JSON.parse()
investor.Balance = investor.Balance + (security.CouponRate * paymentAmount)
investorAsBytes, _ = json.Marshal(investor) //convert to array of bytes
fmt.Println("investorAsBytes")
fmt.Println(string(investorAsBytes))
err = stub.PutState(investor.Id, investorAsBytes) //store owner by its Id
if err != nil {
fmt.Println("Could not store investor")
return shim.Error(err.Error())
}
}
if security.RemainingPayments != "" {
securityRemainingPayments, err := strconv.Atoi(security.RemainingPayments)
if err != nil {
return shim.Error(err.Error())
}
securityRemainingPayments = securityRemainingPayments - 1
security.RemainingPayments = strconv.Itoa(securityRemainingPayments)
securityAsBytes, _ = json.Marshal(security)
err = stub.PutState(security.Id, securityAsBytes) //store owner by its Id
if err != nil {
return shim.Error(err.Error())
}
}
// fmt.Println(pool.Investors)
// security.investor.balance =+ interestPayment
}
// TODO
// recreate originator with processingfee, test out
// res.Originator.ProcessingFee, err
// originatorPayment := processingFee * float64(paymentAmount)
// res.Pool
// receipt :=
// jsonAsBytes, _ := json.Marshal(`"{payment: ` + strconv.FormatFloat(processingPayment, 'f', 2, 64) + `, receipient: ` + asset.Originator.Username + ` }"`) //convert to array of bytes
// err = stub.SetEvent("Payment dispersed: ", jsonAsBytes) //rewrite the marble with id as key
if err != nil {
return shim.Error(err.Error())
}
// TODO
// lookup all associated investors and pay them
// asset -> pool -> derivative (based on rating) -> investor
// portion of investor original investment should be returned plus rate of return
// err = stub.PutState(args[0], jsonAsBytes) //rewrite the marble with id as key
// fmt.Println("stub")
// fmt.Println(stub)
// f := "invoke"
// strs := []string{"pool1"}
// buf := &bytes.Buffer{}
// gob.NewEncoder(buf).Encode(strs)
// bs := buf.Bytes()
//
//
// strBytes := [1][1]byte{}
// strBytes[0][0] = []byte("pool1")
//
// strBytes = []byte("pool1")
// input := []string{"value_pool", "pool1"}
// output := make([][]byte, len(input))
// for i, v := range input {
// output[i] = []byte(v)
// }
// TODO, make the value_pool function a normal one instead of using shim
// invoke := stub.InvokeChaincode("invoke", output, "")
// update pool value
// value_pool(stub, []string{pool.Id})
// update amortization value
// value_asset(stub, []string{asset.Id})
fmt.Println("- end process_payments")
return shim.Success(nil)
}
// calculate monthly payment required for amortization
// expects interest rate and period
func calculate_monthly_payment(balance float64, interestRate float64, months float64) float64 {
// balance := 450000.00
monthlyInterestRate := interestRate / 12.0
// months := 3.0 * 12.0
monthlyPayment := ((balance * ( monthlyInterestRate * math.Pow((1 + monthlyInterestRate) , months))) / ( ( math.Pow ( (1 + monthlyInterestRate ) , months)) - 1 ))
return monthlyPayment
}
//
// TODO, this doesn't seem to be working, fix
// func calculate_payments_to_amoritization (balance float64, interestRate float64, monthlyPayment float64) int {
// monthlyInterestRate := interestRate / 12.0
// // months := 3.0 * 12.0
// // monthlyPayment := ((balance * ( monthlyInterestRate * math.Pow((1 + monthlyInterestRate) , months))) / ( ( math.Pow ( (1 + monthlyInterestRate ) , months)) - 1 ))
// paymentsLeft := ( ( -1.0 * math.Log( (1.0 - interest * balance / monthlyPayment ) )) / math.Log( 1 + interestRate) )
// return monthlyPayment
// }
// calculate the total value of a pool
// this should be done when a payment is applied toward an asset in a pool, and when an asset is added to a pool
func value_asset_pool(stub shim.ChaincodeStubInterface, args []string) pb.Response {
// Calculates the total amount that a homeowner will pay on their mortgage by amortization
fmt.Println("start -value_asset_pool")
asset_id := args[0]
var err error
var poolAsBytes []byte
var assetAsBytes []byte
var expectedPayoffAmount float64
// for i := 0; i < retries; i++ {
// fmt.Println("on iteration")
// fmt.Println(i)
// poolAsBytes, err = stub.GetState(pool_id)
//
// // if err != nil {
// if (len(poolAsBytes) == 0) {
// fmt.Println("Could not load pool, retrying")
// fmt.Println(string(assetAsBytes))
// // return shim.Error(err.Error())
// time.Sleep(2 * time.Second) // adding sleep
// } else {
// fmt.Println("Asset pool loaded, continuing")
// break
// }
// }
pool := Pool{}
asset := Asset{}
fmt.Println("asset_id")
fmt.Println(asset_id)
assetAsBytes, err = stub.GetState(asset_id)
err = json.Unmarshal(assetAsBytes, &asset)
fmt.Println("asset")
fmt.Println(asset)
poolAsBytes, err = stub.GetState(asset.Pool)
err = json.Unmarshal(poolAsBytes, &pool)
fmt.Println("poolAsBytes")
fmt.Println(string(poolAsBytes))
fmt.Println("assetAsBytes")
fmt.Println(string(assetAsBytes))
if err != nil {
fmt.Println("Could not load pool")
return shim.Error(err.Error())
}
poolValue := 0.0
for _, asset_id := range pool.Assets {
fmt.Println("loading asset")
fmt.Println(asset_id)
assetAsBytes, err = stub.GetState(asset_id)
asset = Asset{}
err = json.Unmarshal(assetAsBytes, &asset)
expectedPayoffAmount, _ = strconv.ParseFloat(asset.ExpectedPayoffAmount, 32)
fmt.Println("expectedPayoffAmount")
fmt.Println(expectedPayoffAmount)
poolValue = poolValue + expectedPayoffAmount
}
// fmt.Println("setting updated pool value")
pool.Value = poolValue
// balance, _ := strconv.ParseFloat(asset.Balance, 32)
// interest, _ := strconv.ParseFloat(asset.InterestRate, 32)
// remainingPayments, _ := strconv.ParseFloat(asset.RemainingPayments, 32)
// trueValue := (((interest / 12.0) * balance * remainingPayments) / (1.0 - math.Pow( ( (1.0 + ( interest / 12.0)) ), (-1.0 * remainingPayments) )))
// fmt.Println("trueValue")
// fmt.Println(trueValue)
// if math.IsNaN(trueValue) {
// fmt.Println("missing parameters")
// return shim.Error("missing parameters")
// }
// asset.ExpectedPayoffAmount = strconv.FormatFloat(trueValue, 'f', 2, 64)
// fmt.Println("write updated pool")
poolAsBytes, _ = json.Marshal(pool) //convert to array of bytes
err = stub.PutState(pool.Id, poolAsBytes) //store owner by its Id
if err != nil {
fmt.Println("Could not store pool")
return shim.Error(err.Error())
}
fmt.Println("end -value_asset_pool")
return shim.Success(nil)
}
// func value_pool(stub shim.ChaincodeStubInterface, args []string) pb.Response {
// // Calculates the total amount that a homeowner will pay on their mortgage by amortization
// pool_id := args[0]
// retries := 5
// var err error
// var poolAsBytes []byte
// var assetAsBytes []byte
// var expectedPayoffAmount float64
// for i := 0; i < retries; i++ {
// fmt.Println("on iteration")
// fmt.Println(i)
// poolAsBytes, err = stub.GetState(pool_id)
//
// // if err != nil {
// if (len(poolAsBytes) == 0) {
// fmt.Println("Could not load pool, retrying")
// fmt.Println(string(assetAsBytes))
// // return shim.Error(err.Error())
// time.Sleep(2 * time.Second) // adding sleep
// } else {
// fmt.Println("Asset pool loaded, continuing")
// break
// }
// }
//
// // if err != nil {
// if (len(poolAsBytes) == 0) {
// fmt.Println("Could not load pool after multiple tries, exiting")
// // return shim.Error(err.Error())
// return shim.Error("Could not load pool after multiple tries, exiting")
// }
//
// pool := Pool{}
// asset := Asset{}
// err = json.Unmarshal(poolAsBytes, &pool)
// if err != nil {
// fmt.Println("Could not load pool")
// return shim.Error(err.Error())
// }
// poolValue := 0.0
// for _, asset_id := range pool.Assets {
// fmt.Println("loading asset")
// fmt.Println(asset_id)
// assetAsBytes, err = stub.GetState(asset_id)
// asset = Asset{}
// expectedPayoffAmount, _ = strconv.ParseFloat(asset.ExpectedPayoffAmount, 32)
// fmt.Println("expectedPayoffAmount")
// fmt.Println(expectedPayoffAmount)
// poolValue = poolValue + expectedPayoffAmount
// }
// pool.Value = poolValue
// // balance, _ := strconv.ParseFloat(asset.Balance, 32)
// // interest, _ := strconv.ParseFloat(asset.InterestRate, 32)
// // remainingPayments, _ := strconv.ParseFloat(asset.RemainingPayments, 32)
// // trueValue := (((interest / 12.0) * balance * remainingPayments) / (1.0 - math.Pow( ( (1.0 + ( interest / 12.0)) ), (-1.0 * remainingPayments) )))
// // fmt.Println("trueValue")
// // fmt.Println(trueValue)
// // if math.IsNaN(trueValue) {
// // fmt.Println("missing parameters")
// // return shim.Error("missing parameters")
// // }
// // asset.ExpectedPayoffAmount = strconv.FormatFloat(trueValue, 'f', 2, 64)
// poolAsBytes, _ = json.Marshal(pool) //convert to array of bytes
// err = stub.PutState(pool.Id, poolAsBytes) //store owner by its Id
// if err != nil {
// fmt.Println("Could not store pool")
// return shim.Error(err.Error())
// }
// return shim.Success(nil)
// }
func value_asset(stub shim.ChaincodeStubInterface, args []string) pb.Response {
// Calculates the total amount that a homeowner will pay on their mortgage by amortization
asset_id := args[0]
retries := 5
var err error
var assetAsBytes []byte
for i := 0; i < retries; i++ {
fmt.Println("on iteration")
fmt.Println(i)
assetAsBytes, err = stub.GetState(asset_id)
// if err != nil {
if (len(assetAsBytes) == 0) {
fmt.Println("Could not load asset, retrying")
fmt.Println(asset_id)
fmt.Println(string(assetAsBytes))
// return shim.Error(err.Error())
time.Sleep(2 * time.Second) // adding sleep
} else {
fmt.Println("Asset loaded, continuing")
break
}
}
// if err != nil {
if (len(assetAsBytes) == 0) {
fmt.Println("Could not load asset after multiple tries, exiting")
// return shim.Error(err.Error())
return shim.Error("Could not load asset after multiple tries, exiting")
}
asset := Asset{}
err = json.Unmarshal(assetAsBytes, &asset)
if err != nil {
fmt.Println("Could not load asset")
return shim.Error(err.Error())
}
balance, _ := strconv.ParseFloat(asset.Balance, 32)
interest, _ := strconv.ParseFloat(asset.InterestRate, 32)
remainingPayments, _ := strconv.ParseFloat(asset.RemainingPayments, 32)
trueValue := (((interest / 12.0) * balance * remainingPayments) / (1.0 - math.Pow( ( (1.0 + ( interest / 12.0)) ), (-1.0 * remainingPayments) )))
fmt.Println("trueValue")
fmt.Println(trueValue)