github.com/kchristidis/fabric@v1.0.4-0.20171028114726-837acd08cde1/examples/chaincode/go/invokereturnsvalue/invokereturnsvalue.go (about) 1 /* 2 Copyright IBM Corp. 2016 All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package main 18 19 //WARNING - this chaincode's ID is hard-coded in chaincode_example04 to illustrate one way of 20 //calling chaincode from a chaincode. If this example is modified, chaincode_example04.go has 21 //to be modified as well with the new ID of chaincode_example02. 22 //chaincode_example05 show's how chaincode ID can be passed in as a parameter instead of 23 //hard-coding. 24 25 import ( 26 "fmt" 27 "strconv" 28 29 "github.com/hyperledger/fabric/core/chaincode/shim" 30 pb "github.com/hyperledger/fabric/protos/peer" 31 ) 32 33 // SimpleChaincode example simple Chaincode implementation 34 type SimpleChaincode struct { 35 } 36 37 // Init method of chaincode 38 func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response { 39 _, args := stub.GetFunctionAndParameters() 40 var A, B string // Entities 41 var Aval, Bval int // Asset holdings 42 var err error 43 44 if len(args) != 4 { 45 return shim.Error("Incorrect number of arguments. Expecting 4") 46 } 47 48 // Initialize the chaincode 49 A = args[0] 50 Aval, err = strconv.Atoi(args[1]) 51 if err != nil { 52 return shim.Error("Expecting integer value for asset holding") 53 } 54 B = args[2] 55 Bval, err = strconv.Atoi(args[3]) 56 if err != nil { 57 return shim.Error("Expecting integer value for asset holding") 58 } 59 fmt.Printf("Aval = %d, Bval = %d\n", Aval, Bval) 60 61 // Write the state to the ledger 62 err = stub.PutState(A, []byte(strconv.Itoa(Aval))) 63 if err != nil { 64 return shim.Error(err.Error()) 65 } 66 67 err = stub.PutState(B, []byte(strconv.Itoa(Bval))) 68 if err != nil { 69 return shim.Error(err.Error()) 70 } 71 72 return shim.Success([]byte("OK")) 73 } 74 75 // Invoke transaction makes payment of X units from A to B 76 func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response { 77 _, args := stub.GetFunctionAndParameters() 78 79 var A, B string // Entities 80 var Aval, Bval int // Asset holdings 81 var X int // Transaction value 82 var err error 83 84 if len(args) != 3 { 85 return shim.Error("Incorrect number of arguments. Expecting 3") 86 } 87 88 A = args[0] 89 B = args[1] 90 91 // Get the state from the ledger 92 // TODO: will be nice to have a GetAllState call to ledger 93 Avalbytes, err := stub.GetState(A) 94 if err != nil { 95 return shim.Error("Failed to get state") 96 } 97 if Avalbytes == nil { 98 return shim.Error("Entity not found") 99 } 100 Aval, _ = strconv.Atoi(string(Avalbytes)) 101 102 Bvalbytes, err := stub.GetState(B) 103 if err != nil { 104 return shim.Error("Failed to get state") 105 } 106 if Bvalbytes == nil { 107 return shim.Error("Entity not found") 108 } 109 Bval, _ = strconv.Atoi(string(Bvalbytes)) 110 111 // Perform the execution 112 X, err = strconv.Atoi(args[2]) 113 if err != nil { 114 return shim.Error("Invalid transaction amount, expecting a integer value") 115 } 116 Aval = Aval - X 117 Bval = Bval + X 118 fmt.Printf("Aval = %d, Bval = %d\n", Aval, Bval) 119 120 // Write the state back to the ledger 121 err = stub.PutState(A, []byte(strconv.Itoa(Aval))) 122 if err != nil { 123 return shim.Error(err.Error()) 124 } 125 126 err = stub.PutState(B, []byte(strconv.Itoa(Bval))) 127 if err != nil { 128 return shim.Error(err.Error()) 129 } 130 131 return shim.Success([]byte(fmt.Sprintf("{%d,%d}", Aval, Bval))) 132 } 133 134 func main() { 135 err := shim.Start(new(SimpleChaincode)) 136 if err != nil { 137 fmt.Printf("Error starting Simple chaincode: %s", err) 138 } 139 }