I want to call one chaincode from another chaincode in fabric 1.0 so I have some questions: 1) can we install two chaincode on single peer 2) if we install two chaincode on different peer, how we can call one into another? 3)if anybody having sample example please share.
Asked
Active
Viewed 4,406 times
1 Answers
13
This is should be pretty straight forward to achieve, here is an example:
// Invoke
func (am *accountManagement) Invoke(stub shim.ChaincodeStubInterface) peer.Response {
actionName, params := stub.GetFunctionAndParameters()
if actionName == "callAnotherCC" {
chainCodeArgs := util.ToChaincodeArgs("anotherCCFunc", "paramA")
response := stub.InvokeChaincode("anotherCCName", chainCodeArgs, "channelName")
if response.Status != shim.OK {
return shim.Error(response.Message)
}
return shim.Success(nil)
}
// NOTE: This is an example, hence assuming only valid call is to call another chaincode
return shim.Error(fmt.Sprintf("[ERROR] No <%s> action defined", actionName))
}
UPDTAE
As @Gari, correctly stated in the comment:
It's very important to make sure that both chaincodes are installed on each endorsing peer
Consider to read also following material:

Artem Barger
- 40,769
- 9
- 59
- 81
-
@ArtemBarger Can I call chaincode of other channel? – Akshay Sood Sep 30 '18 at 12:58
-
@AkshaySood only if peer participates in both channels and has second chaincode installed. – Artem Barger Sep 30 '18 at 20:23