8.3.3.1 Interchain Application Contract based on Fabric

Interchain Application Contracts Development Guide

1. Preparation:

Run the code below to obtain the BSN interchain consuming contract help package (ICH.git). Currently, only the GO language version is supported but more versions will be added later.

ccd $GOPATH
mkdir -p src/github.com/BSNDA && cd src/github.com/BSNDA
git clone https://github.com/BSNDA/ICH.git

2. Initiate the interchain service request

After creating the Fabric chain code struct and invoke function, import below package:

import (
"github.com/BSNDA/ICH/sample/irita/consumers/fabric/crosschaincode"
)

3. Call the crosschaincode.CallService method in the invoke function using the parameters of the method as follows:

  • stub: shim.ChaincodeStubInterface
  • serviceName: the interchain service name to invoke, nft for ETH, and bcos-store for FISCO BCOS
  • input: the input object for interchain service
  • callbackCC: callback chaincode name
  • callbackFcn: callback chaincode function name
  • timeout: timeout

The input parameter varies according to the interchain service and type passed in. In the ETH service, the input structure is as follows:

type Input struct {
            ABIEncoded   string `json:"abi_encoded,omitempty"`
            To           string `json:"to"`
           AmountToMint string `json:"amount_to_mint"`
            MetaID       string `json:"meta_id"`
            SetPrice     string `json:"set_price"`
            IsForSale    bool   `json:"is_for_sale"`
        }

In the FISCO BCOS service, the input structure is as follows:

  type BcosInput struct {
            Value string `json:"value"`
        }

A unique request ID will be returned after successful invocation. Keep this value and use it to determine the cross-chain results in the callback function.

4. Implement the callback interface:

After the cross chaincode receives the service response from Fabric Relayer, the callback method name and callback chaincode name passed in will be called to return the service response. The first parameter of the call returns a JSON-formatted string. Below is the service response structure:

type ServiceResponse struct {
    RequestId   string `json:"requestID,omitempty"` 
       ErrMsg      string `json:"errMsg,omitempty"`
      Output      string `json:"output,omitempty"`
       IcRequestId string `json:"icRequestID,omitempty"`
    }

Method crosschaincode.GetCallBackInfo() could be called to Serialize the value. The requestID is unique and can be used to conduct the business processing. Output a JSON-formatted string which is the return value of the crosschain response. Below is the input data structure:

type InputData struct {
    Header interface{} `json:"header"`
    Body interface{}    `json:"body"`
    }

Parameter “Body” is the output object for the service. In ETH service, the input structure is as follows:

type Output struct {
        NftID string `json:"nft_id"`
    }

In FISCO BCOS service, the output structure is as follows:

type BcosOutput struct {
        Key string `json:"key"`
    }

5. Package the chaincode

ICH.git, which is imported by the chaincode, needs to be packaged with the chaincode together by govendor. If you haven’t installed govendor, you can install it as below: Install govendor:

    go get -u -v github.com/kardianos/govendor

Execute in the main method directory:

govendor init
govendor add -tree github.com/BSNDA/ICH/sample/irita/consumers/fabric/crosschaincode

After execution, the vendor folder will be generated. For the last step, compress the project and vendor folder together, then upload and deploy it in BSN portal.

Application Contract Example:

GitHub: https://github.com/BSNDA/ICH/tree/main/sample/irita/consumers/fabric/chaincode

© 2019-2021 Red Date (Hong Kong) Technology Limited. all right reserved,powered by GitbookFile Modify: 2021-07-26 20:18:48

results matching ""

    No results matching ""

    results matching ""

      No results matching ""