Can I invoke a chaincode function from the chaincode itself

+1 vote
I have a chaincode and have a certain number of functions in it. I want to invoke a function that is in the chain code by the chaincode itself. I don’t want to use any external invoking. How can I do it?
Jul 26, 2018 in Blockchain by digger
• 26,740 points
3,447 views

2 answers to this question.

0 votes

Yes, you can invoke a function from the chaincode itself.

The general format to invoke a chaincode is as follows:

stub.InvokeChaincode(chaincodeName, queryArgs, channelName)

But this is used to invoke another chaincode. So if you want to invoke the function by the chaincode itself, you have to modify the above code. If you want to call the chaincode in same channel, leave the channelName parameter empty.

So you can just pass the first two parameters

stub.InvokeChaincode(chaincodeName, queryArgs)

Source: https://github.com/hyperledger/fabric/blob/release/examples/chaincode/go/chaincode_example05/chaincode_example05.go#L90

answered Jul 26, 2018 by slayer
• 29,370 points
Hi @Slayer, I wanted to know if there are any criteria to be able to call a chaincode from another chaincode. I have different hyperledger networks and projects. Some times the chaincode calling another chaincode works and some times it doesn't.
As much as I know, the chaincode should be installed on each endorsing peer.
HI @Yash. Another thing I have observed is that if the chaincodes are on different channels, then only read is allowed.
0 votes

Here's an example on how you can achieve it:

// 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))
}
answered May 7, 2019 by Shir

Related Questions In Blockchain

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How do I withdraw the balance from a contract on Ethereum test blockchain?

Your need to improvise your code a ...READ MORE

answered Aug 3, 2018 in Blockchain by Perry
• 17,100 points
2,692 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,942 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,468 views
+1 vote
1 answer

Language in which cryptokitties is programmed?

Cryptokitties is built on the Ethereum network. The ...READ MORE

answered Mar 27, 2018 in Blockchain by Johnathon
• 9,090 points
681 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP