Search code examples
hyperledger-fabricblockchainhyperledger

Query on Contract JAVA API Hyperledger Fabric


I am analyzing the Fabcar Java Project in Hyperledger Fabric. Below are the dependencies in project. snippet from pom.xml

 <dependencies>
      <dependency>
         <groupId>org.hyperledger.fabric</groupId>
         <artifactId>fabric-gateway-java</artifactId>
         <version>2.1.1</version>
      </dependency>
      <dependency>
         <groupId>org.junit.platform</groupId>
         <artifactId>junit-platform-launcher</artifactId>
         <version>1.4.2</version>
      </dependency>
      <dependency>
         <groupId>org.junit.jupiter</groupId>
         <artifactId>junit-jupiter-engine</artifactId>
         <version>5.4.1</version>
         <scope>test</scope>
      </dependency>
      <dependency>
         <groupId>org.junit.vintage</groupId>
         <artifactId>junit-vintage-engine</artifactId>
         <version>5.4.2</version>
      </dependency>
      <dependency>
         <groupId>org.assertj</groupId>
         <artifactId>assertj-core</artifactId>
         <version>3.12.2</version>
         <scope>test</scope>
      </dependency>

I am able to run the Programs in it. like

EnrollAdmin.main(null);
RegisterUser.main(null);
ClientApp.main(null);

Then I wanted to write my own smart contract from scratch.so started research/analysis and stumbled upon Video By Hyperledger Foundation

here in this video , presenter said every smart contract has to extend from Contract class. Contract class contains methods like

beforeTransaction()
afterTransaction()
 createContext()

etc

so I searched Contract class in my Java workspace and found different methods present in it. such as

Transaction createTransaction(String name);
submitTransaction(String name, String... args) 
evaluateTransaction(String name, String... args)

So my questions

  1. has Contract class upgraded and I am watching old tutorials or I am referring to OLD APIs ?

  2. Also I found that , there is a dependency fabric-chaincode-shim 2.2.4 API, which contains the methods which matches with Video tutorial.

    package: org.hyperledger.fabric.contract

    interface: ContractInterface

Do i have to import this dependency or my existing Contract interface(org.hyperledger.fabric.gateway.Contract) enough to write a new smart contract?


Solution

  • There are two coding aspects to writing an application for Fabric. One is the Smart Contract (also referred to as chaincode). The other is the client side application that will send in transaction requests that result in functions in the Smart Contract being executed.

    Taking the SmartContract first, that will extend the Contract Interface, and then you'll write various transaction functions (for example createAsset).

    To 'run' these transaction functions a client application needs to call submit or evaluate transaction. Only if a transaction function is submitted will changes be made to the ledger. Evaluate is therefore really just a query.

    In summary there are two APIs that you need to use - one for the Contract and one for the Client Application.