Guest User

Untitled

a guest
Mar 24th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.73 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "bytes"
  5. "encoding/json"
  6. "fmt"
  7. "strconv"
  8.  
  9. "github.com/hyperledger/fabric/core/chaincode/shim"
  10. sc "github.com/hyperledger/fabric/protos/peer"
  11. )
  12.  
  13. // Define the Smart Contract structure
  14.  
  15. type SmartContract struct {
  16. }
  17.  
  18. //Define assets structure
  19.  
  20. type Asset struct {
  21. asset_id string `json:"asset_id"`
  22. asset_name string `json:"asset_name"`
  23. owner_aadhar int `json:"owner_aadhar"`
  24. owner_name string `json:"owner_name"`
  25. debt bool `json:"debt"`
  26. lamount int `json:"lamount"`
  27. objection string `json:"objection"`
  28. }
  29.  
  30.  
  31. func (s *SmartContract) Init(APIstub shim.ChaincodeStubInterface) sc.Response {
  32. return shim.Success(nil)
  33. }
  34.  
  35. // The calling application program has also specified the particular smart contract function to be called, with arguments
  36.  
  37. func (s *SmartContract) Invoke(APIstub shim.ChaincodeStubInterface) sc.Response {
  38.  
  39. function, args := APIstub.GetFunctionAndParameters()
  40.  
  41. if function == "initLedger"{
  42. return s.initLedger(APIstub)
  43. }
  44.  
  45. if function == "transfer_asset"{
  46. return s.transfer_asset(APIstub,args)
  47. }
  48.  
  49. if function == "create_asset"{
  50. return s.create_asset(APIstub,args)
  51. }
  52.  
  53. if function == "view_asset" {
  54. return s.view_asset(APIstub,args)
  55. }
  56.  
  57.  
  58. return shim.Error("Invalid Smart Contract function name.")
  59. }
  60.  
  61. //stores some sample assets
  62.  
  63. func (s *SmartContract) initLedger(APIstub shim.ChaincodeStubInterface) sc.Response {
  64. assets := []Asset{
  65. Asset{asset_id:"A101", asset_name:"1bhk", owner_aadhar:101, owner_name:"praveen", debt:false, lamount:0, objection:"nill"},
  66. Asset{asset_id:"A102", asset_name:"factory",owner_aadhar:102, owner_name:"arun", debt:false, lamount:0, objection:"nill"},
  67. Asset{asset_id:"A103", asset_name:"abt school", owner_aadhar:103, owner_name:"kevin", debt:true, lamount:100000, objection:"nill"},
  68. Asset{asset_id:"A104", asset_name:"2bhk", owner_aadhar:104, owner_name:"baaruni",debt:false, lamount:0, objection:"1case"},
  69. }
  70.  
  71. i := 0
  72.  
  73. for i< len(assets) {
  74. assetAsBytes, _ :=json.Marshal(assets[i])
  75. APIstub.PutState(assets[i].asset_id, assetAsBytes)
  76. i++
  77. }
  78.  
  79. return shim.Success(nil)
  80. }
  81.  
  82. func (s *SmartContract) create_asset(APIstub shim.ChaincodeStubInterface, args[] string) sc.Response {
  83. if len(args)!=7 {
  84. return shim.Error("Incorrect number of arguments expected 7")
  85. }
  86. taadhar, _ :=strconv.Atoi(args[2])
  87. tdept, _ := strconv.ParseBool(args[4])
  88. tlamount, _ := strconv.Atoi(args[5])
  89. var asset = Asset{asset_id:args[0], asset_name:args[1], owner_aadhar:taadhar, owner_name:args[3], debt:tdept, lamount:tlamount, objection:args[6]}
  90. assetAsBytes, _ := json.Marshal(asset)
  91. APIstub.PutState(asset.asset_id,assetAsBytes)
  92. return shim.Success(nil)
  93. }
  94.  
  95. func (s *SmartContract) transfer_asset(APIstub shim.ChaincodeStubInterface, args[] string) sc.Response{
  96.  
  97. if len(args) !=3 {
  98. return shim.Error("Incorrect number of arguments expected 3")
  99. }
  100.  
  101. assetAsBytes, _ :=APIstub.GetState(args[0])
  102. asset := Asset{}
  103.  
  104. json.Unmarshal(assetAsBytes, &asset)
  105.  
  106. if (asset.objection!="nill")||(asset.debt!=false){
  107. return shim.Error("Asset cannot be transfer as it has some issues")
  108. }
  109. asset.owner_aadhar, _ = strconv.Atoi(args[1])
  110.  
  111. assetAsBytes, _ =json.Marshal(asset)
  112. APIstub.PutState(args[0],assetAsBytes)
  113.  
  114. return shim.Success(nil)
  115.  
  116. }
  117.  
  118. func (s *SmartContract) view_asset(APIstub shim.ChaincodeStubInterface, args[] string) sc.Response {
  119. if len(args) !=1{
  120. return shim.Error("Invalid number of arguments expected 1")
  121. }
  122.  
  123. assetAsBytes, _ :=APIstub.GetState(args[0])
  124. asset := Asset{}
  125.  
  126. json.Unmarshal(assetAsBytes, &asset)
  127. return shim.Success(assetAsBytes)
  128. }
Add Comment
Please, Sign In to add comment