7

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.

Tejal tandale
  • 87
  • 1
  • 10

1 Answers1

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:

  1. https://github.com/asararatnakar/fabric_v1_Chaincode_instructions/blob/master/call-chaincode-to-chaincode-nondefault-chain.md
  2. https://jira.hyperledger.org/browse/FAB-1788
  3. http://hyperledger-fabric.readthedocs.io/en/release-1.0/chaincode.html
Artem Barger
  • 40,769
  • 9
  • 59
  • 81