/* Copyright IBM Corp. 2016 All Rights Reserved. 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. */ package main //WARNING - this chaincode's ID is hard-coded in chaincode_example04 to illustrate one way of //calling chaincode from a chaincode. If this example is modified, chaincode_example04.go has //to be modified as well with the new ID of chaincode_example02. //chaincode_example05 show's how chaincode ID can be passed in as a parameter instead of //hard-coding. import ( "fmt" "strconv" "github.com/hyperledger/fabric/core/chaincode/shim" pb "github.com/hyperledger/fabric/protos/peer" ) type SimpleChaincode2 struct { } func (t *SimpleChaincode2) Init(stub shim.ChaincodeStubInterface) pb.Response { fmt.Printf("init successful") return shim.Success(nil) } func (t *SimpleChaincode2) Invoke(stub shim.ChaincodeStubInterface) pb.Response { _, args := stub.GetFunctionAndParameters() function := args[0] fmt.Printf("method =%s and args = %s", function, args) if function == "init" { return t.init(stub, args[1:len(args)]) } else if function == "add" { return t.add(stub, args[1:len(args)]) } else if function == "query" { return t.query(stub, args[1:len(args)]) } return shim.Error("Invalid invoke function name. Expecting \"invoke\" \"delete\" \"query\"") } func (t *SimpleChaincode2) add(stub shim.ChaincodeStubInterface, args []string) pb.Response { var A string var Aval int var X int if len(args) != 2 { return shim.Error("Incorrect number of arguments. Expecting 3") } A = args[0] Avalbytes, err := stub.GetState(A) if err != nil { return shim.Error("Failed to get state") } if Avalbytes == nil { return shim.Error("Entity not found") } Aval, _ = strconv.Atoi(string(Avalbytes)) fmt.Printf("Aval=%d", Aval) X, err = strconv.Atoi(args[1]) if err != nil { return shim.Error("Invalid transaction amount, expecting a integer value") } Aval = Aval + X err = stub.PutState(A, []byte(strconv.Itoa(Aval))) if err != nil { return shim.Error(err.Error()) } return shim.Success(nil) } func (t *SimpleChaincode2) init(stub shim.ChaincodeStubInterface, args []string) pb.Response { if len(args) != 1 { return shim.Error("Incorrect number of arguments. Expecting 1") } A := args[0] err := stub.PutState(A, []byte(args[1])) if err != nil { return shim.Error("Failed to delete state") } return shim.Success(nil) } func (t *SimpleChaincode2) query(stub shim.ChaincodeStubInterface, args []string) pb.Response { var A string var err error if len(args) != 1 { return shim.Error("Incorrect number of arguments. Expecting name of the person to query") } A = args[0] Avalbytes, err := stub.GetState(A) if err != nil { jsonResp := "{\"Error\":\"Failed to get state for " + A + "\"}" return shim.Error(jsonResp) } if Avalbytes == nil { jsonResp := "{\"Error\":\"Nil amount for " + A + "\"}" return shim.Error(jsonResp) } jsonResp := "{\"Name\":\"" + A + "\",\"Amount\":\"" + string(Avalbytes) + "\"}" fmt.Printf("Query Response:%s\n", jsonResp) return shim.Success(Avalbytes) } func main() { err := shim.Start(new(SimpleChaincode2)) fmt.Println("success") if err != nil { fmt.Printf("Error starting Simple chaincode: %s", err) } }