# Chaincode as external service

Spydra runs chaincode as an external service to be able to gain more control over and to manage chaincode runtime more effectively.

<br>

Steps to write a chaincode compatible for running as an external service

<mark style="color:purple;">**Golang**</mark>:

* Import the shim package and create the ChaincodeServer struct from the shim package.&#x20;
* The ChaincodeServer struct takes the following values:
  * **CC**: It takes contractapi.ContractInterface which was created using contractapi.NewChaincode() function.
  * **CCID**: It takes the chaincode package id. This can be accessed from the CORE\_CHAINCODE\_ID environment variable.
  * **Address**: Chaincode server address to expose the chaincode. This can be accessed from the CORE\_CHAINCODE\_ADDRESS environment variable.
  * **TLSProps**: It accepts the struct value of shim.TLSProperties
* Then call the start function from the created ChaincodeServer struct value (server.Start())

Eg: <https://github.com/spydra-tech/external-chaincode-reference/blob/main/go/main.go>

***

<mark style="color:purple;">**Javascript:**</mark>

1. If chaincode is written using contract api:

In package.json, modify the value of scripts.start section to ”fabric-chaincode-node server”

Eg: <https://github.com/spydra-tech/external-chaincode-reference/blob/main/javascript/using-contract-api/package.json>

2. If chaincode is written using fabric-shim:

* In the file where the chaincode is initialised / started, create the shim.server by passing ccid and address which are taken from the runtime environment variables CORE\_CHAINCODE\_ID and CHAINCODE\_SERVER\_ADDRESS
* Then call the start function from the created shim.server object (server.start())

Eg: <https://github.com/spydra-tech/external-chaincode-reference/blob/main/javascript/using-shim-api/cc.js>

***

<mark style="color:purple;">**Java:**</mark>

* Create a new java file (ContractBootstrap.java) that will import NettyChaincodeServer and will have a ContractBootstrap class. This will be the main class.
* The main function in ContractBootstrap class will take chaincode metadata from the environment variables such as:&#x20;
  * CHAINCODE\_SERVER\_ADDRESS
  * CORE\_CHAINCODE\_ID
  * CHAINCODE\_TLS\_DISABLED
  * CHAINCODE\_TLS\_KEY
  * CHAINCODE\_TLS\_CERT
* These env variables will be created inside the chaincode container when the chaincode is deployed.
* Using fabric’s NettyChaincodeServer and ContractRouter, the chaincode server will be started.\
  Eg: <https://github.com/spydra-tech/fabric-java-externalcc-sample/blob/main/src/main/java/org/hyperledger/fabric/samples/ContractBootstrap.java>
* Modify the build.gradle file to include the plugin com.github.johnrengelman.shadow and add shadowJar section with basename parameter as chaincode. Create manifest section inside shadowJar with attribute: Main-Class with the ContractBootstrap class’s path.\
  Eg: <https://github.com/spydra-tech/fabric-java-externalcc-sample/blob/main/build.gradle>
* If remote git is used, make sure the gradle/wrapper directory contains the gradle-wrapper.jar file after publishing as .jar is normally added in the . gitignore file.
* Make sure the gradlew file is present at the root directory of your repository.
